#include <BCDataSet.h>
Definition at line 36 of file BCDataSet.h.
Public Member Functions | |
Member functions (miscellaneous methods) | |
void | AddDataPoint (BCDataPoint *datapoint) |
void | Dump () |
int | ReadDataFromFile (const char *filename, int nvariables) |
int | ReadDataFromFile (const char *filename, const char *treename, const char *branchnames) |
int | ReadDataFromFileTree (const char *filename, const char *treename, const char *branchnames) |
int | ReadDataFromFileTxt (const char *filename, int nvariables) |
void | Reset () |
Constructors and destructors | |
BCDataSet () | |
virtual | ~BCDataSet () |
Member functions (get) | |
BCDataPoint * | GetDataPoint (unsigned int index) |
unsigned int | GetNDataPoints () |
unsigned int | GetNValuesPerPoint () |
Private Attributes | |
BCDataVector * | fBCDataVector |
BCDataSet::BCDataSet | ( | ) |
BCDataSet::~BCDataSet | ( | ) | [virtual] |
Definition at line 32 of file BCDataSet.cxx.
00033 { 00034 if (fBCDataVector) 00035 delete fBCDataVector; 00036 }
void BCDataSet::AddDataPoint | ( | BCDataPoint * | datapoint | ) |
Adds a data point to the data set.
datapoint | The data point to be added |
Definition at line 306 of file BCDataSet.cxx.
00307 { 00308 00309 // check if memory for the vector has been allocated and 00310 // allocate if not. 00311 if (fBCDataVector == 0) 00312 fBCDataVector = new BCDataVector(); 00313 00314 // add data point to the data set. 00315 fBCDataVector -> push_back(datapoint); 00316 00317 }
void BCDataSet::Dump | ( | ) |
Dump the data to the standard output
Definition at line 333 of file BCDataSet.cxx.
00334 { 00335 if (!fBCDataVector) 00336 { 00337 BCLog::Out(BCLog::error, BCLog::error, "BCDataSet::Dump : Data set is empty. Nothing to dump."); 00338 return; 00339 } 00340 00341 std::cout << std::endl 00342 << "Dumping dataset:" << std::endl 00343 << "----------------" << std::endl 00344 << " - number of points: " << fBCDataVector -> size() << std::endl 00345 << " - number of values per point: " << this -> GetDataPoint(0) -> GetNValues() << std::endl 00346 << " - values:" << std::endl; 00347 unsigned int n = this -> GetDataPoint(0) -> GetNValues(); 00348 for (unsigned int i=0; i< fBCDataVector -> size(); i++) 00349 { 00350 std::cout << Form("%5d : ", i); 00351 for (unsigned int j=0; j<n; j++) 00352 std::cout << Form("%12.5g", this -> GetDataPoint(i) -> GetValue(j)); 00353 std::cout << std::endl; 00354 } 00355 std::cout << std::endl; 00356 00357 }
BCDataPoint * BCDataSet::GetDataPoint | ( | unsigned int | index | ) |
Definition at line 66 of file BCDataSet.cxx.
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 }
unsigned int BCDataSet::GetNDataPoints | ( | ) |
Definition at line 40 of file BCDataSet.cxx.
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 }
unsigned int BCDataSet::GetNValuesPerPoint | ( | ) |
Definition at line 53 of file BCDataSet.cxx.
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 }
int BCDataSet::ReadDataFromFile | ( | const char * | filename, | |
int | nvariables | |||
) | [inline] |
Definition at line 85 of file BCDataSet.h.
00086 { return this -> ReadDataFromFileTxt(filename, nvariables); };
int BCDataSet::ReadDataFromFile | ( | const char * | filename, | |
const char * | treename, | |||
const char * | branchnames | |||
) | [inline] |
Reads data from a file. For a description see the following member functions.
Definition at line 82 of file BCDataSet.h.
00083 { return this -> ReadDataFromFileTree(filename, treename, branchnames); };
int BCDataSet::ReadDataFromFileTree | ( | const char * | filename, | |
const char * | treename, | |||
const char * | branchnames | |||
) |
Reads a TTree from a .root file. Opens a .root file and gets a TTree. It creates data points containing the values read from the file.
filename | The name of the .root file. | |
treename | The name of the TTree. | |
branchnames | A list of the names of the branches separated by a comma |
ReadDataFromFileUser(const char * filename, std::vector<int> options_int, std::vector<double> options_double, const char * options_char);
Definition at line 85 of file BCDataSet.cxx.
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 }
int BCDataSet::ReadDataFromFileTxt | ( | const char * | filename, | |
int | nvariables | |||
) |
Reads data from a .txt file. Opens a .txt file and creates data objects containing the values read from the file.
filename | The name of the .txt file. | |
nvariables | The number of variables. |
ReadDataFromFileUser(const char * filename, std::vector<int> options_int, std::vector<double> options_double, const char * options_char);
Definition at line 230 of file BCDataSet.cxx.
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 for (int i = 0; i < nbranches; i++) 00269 file >> data[i]; 00270 00271 // create data point. 00272 BCDataPoint * datapoint = new BCDataPoint(nbranches); 00273 00274 // copy data into data point 00275 for (int i = 0; i < nbranches; i++) 00276 datapoint -> SetValue(i, data.at(i)); 00277 00278 // add data point to this data set. 00279 this -> AddDataPoint(datapoint); 00280 00281 // increase counter 00282 nentries++; 00283 } 00284 00285 // check if there are any events in the tree and close file if not. 00286 if (nentries <= 0) 00287 { 00288 BCLog::Out(BCLog::error, BCLog::error, 00289 Form("BCDataSet::ReadDataFromFileText : No events in the file %s.", filename)); 00290 00291 // close file 00292 file.close(); 00293 00294 return ERROR_NOEVENTS; 00295 } 00296 00297 // close file 00298 file.close(); 00299 00300 return 0; 00301 00302 }
void BCDataSet::Reset | ( | ) |
Resets the content of the data set
Definition at line 321 of file BCDataSet.cxx.
00322 { 00323 00324 // if memory has been allocated to the data set 00325 // clear the content. 00326 if (fBCDataVector != 0) 00327 fBCDataVector -> clear(); 00328 00329 }
BCDataVector* BCDataSet::fBCDataVector [private] |
Definition at line 131 of file BCDataSet.h.