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

BCDataSet.cxx

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

Generated by  doxygen 1.7.1