• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCDataSet.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2010, Daniel Kollar and Kevin Kroeninger.
00003  * All rights reserved.
00004  *
00005  * For the licensing terms see doc/COPYING.
00006  */
00007 
00008 // ---------------------------------------------------------
00009 
00010 #include "BAT/BCDataSet.h"
00011 
00012 #include "BAT/BCDataPoint.h"
00013 #include "BAT/BCLog.h"
00014 #include "BAT/BCErrorCodes.h"
00015 
00016 #include <TFile.h>
00017 #include <TTree.h>
00018 #include <TString.h>
00019 
00020 #include <iostream>
00021 #include <fstream>
00022 
00023 // ---------------------------------------------------------
00024 
00025 BCDataSet::BCDataSet()
00026 {
00027    fBCDataVector = 0;
00028 }
00029 
00030 // ---------------------------------------------------------
00031 
00032 BCDataSet::~BCDataSet()
00033 {
00034    if (fBCDataVector)
00035       delete fBCDataVector;
00036 }
00037 
00038 // ---------------------------------------------------------
00039 
00040 unsigned int BCDataSet::GetNDataPoints()
00041 {
00042    // check if vector exists. Return number of data points if true ...
00043    if (fBCDataVector)
00044       return fBCDataVector->size();
00045 
00046    // ... or give out error and return 0 if not.
00047    BCLog::OutError("BCDataSet::GetNDataPoints : DataSet not yet created.");
00048    return 0;
00049 }
00050 
00051 // ---------------------------------------------------------
00052 
00053 unsigned int BCDataSet::GetNValuesPerPoint()
00054 {
00055    // check if vector exists and contains datapoints
00056    if (fBCDataVector && fBCDataVector->size() > 0)
00057       return GetDataPoint(0)->GetNValues();
00058 
00059    BCLog::OutError("BCDataSet::GetNValuesPerPoint : Data set doesn't exist yet");
00060    return 0;
00061 }
00062 
00063 // ---------------------------------------------------------
00064 
00065 BCDataPoint * BCDataSet::GetDataPoint(unsigned int index)
00066 {
00067    if (!fBCDataVector || GetNDataPoints()==0 )
00068    {
00069       BCLog::OutError("BCDataSet::GetDataPoint : Dataset is empty.");
00070       return 0;
00071    }
00072 
00073    // check if index is within range. Return the data point if true ...
00074    if(index >= 0 && index < GetNDataPoints())
00075       return fBCDataVector->at(index);
00076 
00077    // ... or give out warning and return 0 if not.
00078    BCLog::OutError("BCDataSet::GetDataPoint : Index out of range. Return 0.");
00079    return 0;
00080 }
00081 
00082 // ---------------------------------------------------------
00083 std::vector<double> BCDataSet::GetDataComponents( int index)
00084 {
00085    unsigned int N = GetNDataPoints();
00086    std::vector<double> components( N , 0.0 );
00087 
00088    BCDataPoint* point=0;
00089    for (unsigned int i = 0; i < N; ++i) {
00090       //rely on index checking in Get... methods
00091       point = GetDataPoint(i);
00092       components[i] = point->GetValue(index);
00093    }
00094 
00095    return components;
00096 }
00097 
00098 
00099 
00100 // ---------------------------------------------------------
00101 
00102 int BCDataSet::ReadDataFromFileTree(const char * filename, const char * treename, const char * branchnames)
00103 {
00104    // open root file
00105    TFile * file = new TFile(filename, "READ");
00106 
00107    // check if file is open and warn if not.
00108    if (!file->IsOpen())
00109    {
00110       BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : Could not open file %s.", filename));
00111       return ERROR_FILENOTFOUND;
00112    }
00113 
00114    // get tree
00115    TTree * tree = (TTree*) file->Get(treename);
00116 
00117    // check if tree is there and warn if not.
00118    if (!tree)
00119    {
00120       BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : Could not find TTree %s.", treename));
00121 
00122       // close file
00123       file->Close();
00124 
00125       return ERROR_TREENOTFOUND;
00126    }
00127 
00128    // if data set contains data, clear data object container ...
00129    if (fBCDataVector != 0)
00130    {
00131       fBCDataVector->clear();
00132 
00133       BCLog::OutDetail("BCDataSet::ReadDataFromFileTree : Overwrite existing data.");
00134    }
00135 
00136    // ... or allocate memory for the vector if not.
00137    else
00138       fBCDataVector = new BCDataVector();
00139 
00140    // get branch names.
00141 
00142    // first, copy the branchnames into a std::string.
00143    std::string branches(branchnames);
00144 
00145    // define a vector of std::strings which contain the tree names.
00146    std::vector<std::string> * branchnamevector = new std::vector<std::string>;
00147 
00148    // the names are supposed to be separated by commas. find first comma
00149    // entry in the string.
00150    int temp_index = branches.find_first_of(",");
00151 
00152    // reset number of branches
00153    int nbranches = 0;
00154 
00155    // repeat until the is nothing left in the string.
00156    while(branches.size() > 0)
00157    {
00158       // temporary string which contains the name of the current branch
00159       std::string branchname;
00160 
00161       // get current branch name
00162 
00163       // if there is no comma the current branchname corresponds to the whole string, ...
00164       if (temp_index == -1)
00165          branchname = branches;
00166 
00167       // ... if there is a comma, copy that part of the string into the current branchname.
00168       else
00169          branchname.assign(branches, 0, temp_index);
00170 
00171       // write branch name to a vector
00172       branchnamevector->push_back(branchname);
00173 
00174       // increase the number of branches found
00175       nbranches++;
00176 
00177       // cut remaining string with branchnames
00178 
00179       // if there is no comma left empty the string, ...
00180       if (temp_index == -1)
00181             branches = "";
00182 
00183       // ... if there is a comma remove the current branchname from the string.
00184       else
00185          branches.erase(0, temp_index + 1);
00186 
00187       // find the next comma
00188       temp_index = branches.find_first_of(",");
00189    }
00190 
00191    // create temporary vector with data and assign some zeros.
00192    std::vector<double> data;
00193    data.assign(nbranches, 0.0);
00194 
00195    // set the branch address.
00196    for (int i = 0; i < nbranches; i++)
00197       tree->SetBranchAddress(branchnamevector->at(i).data(), &data.at(i));
00198 
00199    // calculate maximum number of entries
00200    int nentries = tree->GetEntries();
00201 
00202    // check if there are any events in the tree and close file if not.
00203    if (nentries <= 0)
00204    {
00205       BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : No events in TTree %s.", treename));
00206 
00207       // close file
00208       file->Close();
00209 
00210       return ERROR_NOEVENTS;
00211    }
00212 
00213    // loop over entries
00214    for (int ientry = 0; ientry < nentries; ientry++)
00215    {
00216       // get entry
00217       tree->GetEntry(ientry);
00218 
00219       // create data object
00220       BCDataPoint * datapoint = new BCDataPoint(nbranches);
00221 
00222       // copy data
00223 
00224       for (int i = 0; i < nbranches; i++)
00225          datapoint->SetValue(i, data.at(i));
00226 
00227       // add data point to this data set.
00228       AddDataPoint(datapoint);
00229    }
00230 
00231    // close file
00232    file->Close();
00233 
00234    // remove file pointer.
00235    if (file)
00236       delete file;
00237 
00238    return 0;
00239 
00240 }
00241 
00242 // ---------------------------------------------------------
00243 
00244 int BCDataSet::ReadDataFromFileTxt(const char * filename, int nbranches)
00245 {
00246    // open text file.
00247    std::fstream file;
00248    file.open(filename, std::fstream::in);
00249 
00250    // check if file is open and warn if not.
00251    if (!file.is_open())
00252    {
00253       BCLog::OutError(Form("BCDataSet::ReadDataFromFileText : Could not open file %s.", filename));
00254       return ERROR_FILENOTFOUND;
00255    }
00256 
00257    // if data set contains data, clear data object container ...
00258    if (fBCDataVector != 0)
00259    {
00260       fBCDataVector->clear();
00261 
00262       BCLog::OutDetail("BCDataSet::ReadDataFromFileTxt : Overwrite existing data.");
00263    }
00264 
00265    // ... or allocate memory for the vector if not.
00266    else
00267       fBCDataVector = new BCDataVector();
00268 
00269    // create temporary vector with data and assign some zeros.
00270    std::vector<double> data;
00271    data.assign(nbranches, 0.0);
00272 
00273    // reset counter
00274    int nentries = 0;
00275 
00276    // read data and create data points.
00277    while (!file.eof())
00278    {
00279       // read data from file
00280       int i=0;
00281       while(file >> data[i])
00282       {
00283          if (i==nbranches-1)
00284             break;
00285          i++;
00286       }
00287 
00288       // create data point.
00289       if(i == nbranches-1)
00290       {
00291          BCDataPoint * datapoint = new BCDataPoint(nbranches);
00292 
00293          // copy data into data point
00294          for (int i = 0; i < nbranches; i++)
00295             datapoint->SetValue(i, data.at(i));
00296 
00297          // add data point to this data set.
00298          AddDataPoint(datapoint);
00299 
00300          // increase counter
00301          nentries++;
00302       }
00303    }
00304 
00305    // check if there are any events in the tree and close file if not.
00306    if (nentries <= 0)
00307    {
00308       BCLog::OutError(Form("BCDataSet::ReadDataFromFileText : No events in the file %s.", filename));
00309 
00310       // close file
00311       file.close();
00312 
00313       return ERROR_NOEVENTS;
00314    }
00315 
00316    // close file
00317    file.close();
00318 
00319    return 0;
00320 
00321 }
00322 
00323 // ---------------------------------------------------------
00324 
00325 void BCDataSet::AddDataPoint(BCDataPoint * datapoint)
00326 {
00327 
00328    // check if memory for the vector has been allocated and
00329    // allocate if not.
00330    if (fBCDataVector == 0)
00331       fBCDataVector = new BCDataVector();
00332 
00333    // add data point to the data set.
00334    fBCDataVector->push_back(datapoint);
00335 
00336 }
00337 
00338 // ---------------------------------------------------------
00339 
00340 void BCDataSet::Reset()
00341 {
00342 
00343    // if memory has been allocated to the data set
00344    // clear the content.
00345    if (fBCDataVector != 0)
00346       fBCDataVector->clear();
00347 
00348 }
00349 
00350 // ---------------------------------------------------------
00351 
00352 void BCDataSet::Dump()
00353 {
00354    if (!fBCDataVector)
00355    {
00356       BCLog::OutError("BCDataSet::Dump : Data set is empty. Nothing to dump.");
00357       return;
00358    }
00359 
00360    std::cout << std::endl
00361       << "Dumping dataset:" << std::endl
00362       << "----------------" << std::endl
00363       << " - number of points:            " << fBCDataVector->size() << std::endl
00364       << " - number of values per point:  " << GetDataPoint(0)->GetNValues() << std::endl
00365       << " - values:" << std::endl;
00366    unsigned int n = GetDataPoint(0)->GetNValues();
00367    for (unsigned int i=0; i< fBCDataVector->size(); i++)
00368    {
00369       std::cout << Form("%5d :  ", i);
00370       for (unsigned int j=0; j<n; j++)
00371          std::cout << Form("%12.5g", GetDataPoint(i)->GetValue(j));
00372       std::cout << std::endl;
00373    }
00374    std::cout << std::endl;
00375 
00376 }
00377 
00378 
00379 // ---------------------------------------------------------

Generated on Fri Nov 19 2010 17:02:52 for Bayesian Analysis Toolkit by  doxygen 1.7.1