BCDataSet.cxx

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

Generated on Thu Feb 11 11:29:30 2010 for BayesianAnalysisToolkit by  doxygen 1.5.1