00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BCDataSet.h"
00011 #include "BCLog.h"
00012 #include "BCErrorCodes.h"
00013
00014 #include <TFile.h>
00015 #include <TTree.h>
00016
00017
00018
00019 BCDataSet::BCDataSet()
00020 {
00021
00022 fBCDataVector = 0;
00023
00024 }
00025
00026
00027
00028 BCDataSet::~BCDataSet()
00029 {
00030
00031 if (fBCDataVector)
00032 delete fBCDataVector;
00033
00034 }
00035
00036
00037
00038 int BCDataSet::GetNDataPoints()
00039 {
00040
00041 int ndatapoints = 0;
00042
00043
00044
00045 if (fBCDataVector)
00046 ndatapoints = int(fBCDataVector -> size());
00047
00048
00049
00050 else
00051 BCLog::Out(BCLog::warning, BCLog::warning,"BCDataSet::GetNDataPoints. Memory for vector fBCDataVector not yet allocated. Return 0.");
00052
00053 return ndatapoints;
00054
00055 }
00056
00057
00058
00059 BCDataPoint * BCDataSet::GetDataPoint(int index)
00060 {
00061
00062 BCDataPoint * datapoint = 0;
00063
00064
00065
00066 if (fBCDataVector && index >= 0 && index < this -> GetNDataPoints())
00067 datapoint = fBCDataVector -> at(index);
00068
00069
00070
00071 else
00072 BCLog::Out(BCLog::warning, BCLog::warning,"BCDataSet::GetDataPoint. Index out of range. Return 0.");
00073
00074 return datapoint;
00075
00076 }
00077
00078
00079
00080 int BCDataSet::ReadDataFromFileTree(const char * filename, const char * treename, const char * branchnames)
00081 {
00082
00083
00084
00085 if (fBCDataVector != 0)
00086 {
00087 fBCDataVector -> clear();
00088
00089 BCLog::Out(BCLog::detail, BCLog::detail,"BCDataSet::ReadDataFromFileTree. Overwrite existing data.");
00090 }
00091
00092
00093
00094 else
00095 fBCDataVector = new BCDataVector();
00096
00097
00098
00099 TFile * file = new TFile(filename, "READ");
00100
00101
00102
00103 if (!file -> IsOpen())
00104 {
00105 BCLog::Out(BCLog::warning, BCLog::warning,
00106 Form("BCDataSet::ReadDataFromFileTree. Could not open file %s.", filename));
00107
00108 return ERROR_FILENOTFOUND;
00109 }
00110
00111
00112
00113 TTree * tree = (TTree*) file -> Get(treename);
00114
00115
00116
00117 if (!tree)
00118 {
00119 BCLog::Out(BCLog::warning, BCLog::warning,
00120 Form("BCDataSet::ReadDataFromFileTree. Could not find TTree %s.", treename));
00121
00122
00123
00124 file -> Close();
00125
00126 return ERROR_TREENOTFOUND;
00127 }
00128
00129
00130
00131
00132
00133 std::string branches(branchnames);
00134
00135
00136
00137 std::vector<std::string> * branchnamevector = new std::vector<std::string>;
00138
00139
00140
00141
00142 int temp_index = branches.find_first_of(",");
00143
00144
00145
00146 int nbranches = 0;
00147
00148
00149
00150 while(branches.size() > 0)
00151 {
00152
00153
00154 std::string branchname;
00155
00156
00157
00158
00159
00160 if (temp_index == -1)
00161 branchname = branches;
00162
00163
00164
00165 else
00166 branchname.assign(branches, 0, temp_index);
00167
00168
00169
00170 branchnamevector -> push_back(branchname);
00171
00172
00173
00174 nbranches++;
00175
00176
00177
00178
00179
00180 if (temp_index == -1)
00181 branches = "";
00182
00183
00184
00185 else
00186 branches.erase(0, temp_index + 1);
00187
00188
00189
00190 temp_index = branches.find_first_of(",");
00191 }
00192
00193
00194
00195 std::vector<double> data;
00196 data.assign(nbranches, 0.0);
00197
00198
00199
00200 for (int i = 0; i < nbranches; i++)
00201 tree -> SetBranchAddress(branchnamevector -> at(i).data(), &data.at(i));
00202
00203
00204
00205 int nentries = tree -> GetEntries();
00206
00207
00208
00209 if (nentries <= 0)
00210 {
00211 BCLog::Out(BCLog::warning, BCLog::warning,
00212 Form("BCDataSet::ReadDataFromFileTree. No events in TTree %s.", treename));
00213
00214
00215
00216 file -> Close();
00217
00218 return ERROR_NOEVENTS;
00219 }
00220
00221
00222
00223 for (int ientry = 0; ientry < nentries; ientry++)
00224 {
00225
00226
00227 tree -> GetEntry(ientry);
00228
00229
00230
00231 BCDataPoint * datapoint = new BCDataPoint(nbranches);
00232
00233
00234
00235 for (int i = 0; i < nbranches; i++)
00236 datapoint -> SetValue(i, data.at(i));
00237
00238
00239
00240 this -> AddDataPoint(datapoint);
00241 }
00242
00243
00244
00245 file -> Close();
00246
00247
00248
00249 if (file)
00250 delete file;
00251
00252 return 0;
00253
00254 }
00255
00256
00257
00258 int BCDataSet::ReadDataFromFileTxt(const char * filename, int nbranches)
00259 {
00260
00261
00262
00263 if (fBCDataVector != 0)
00264 {
00265 fBCDataVector -> clear();
00266
00267 BCLog::Out(BCLog::detail, BCLog::detail,"BCDataSet::ReadDataFromFileTxt. Overwrite existing data.");
00268 }
00269
00270
00271
00272 else
00273 fBCDataVector = new BCDataVector();
00274
00275
00276
00277 std::fstream file;
00278
00279 file.open(filename, std::fstream::in);
00280
00281
00282
00283 if (!file.is_open())
00284 {
00285 BCLog::Out(BCLog::warning, BCLog::warning,
00286 Form("BCDataSet::ReadDataFromFileText. Could not open file %s.", filename));
00287
00288 return ERROR_FILENOTFOUND;
00289 }
00290
00291
00292
00293 std::vector<double> data;
00294 data.assign(nbranches, 0.0);
00295
00296
00297
00298 int nentries = 0;
00299
00300
00301
00302 while (!file.eof())
00303 {
00304
00305
00306 for (int i = 0; i < nbranches; i++)
00307 file >> data[i];
00308
00309
00310
00311 BCDataPoint * datapoint = new BCDataPoint(nbranches);
00312
00313
00314
00315 for (int i = 0; i < nbranches; i++)
00316 datapoint -> SetValue(i, data.at(i));
00317
00318
00319
00320 this -> AddDataPoint(datapoint);
00321
00322
00323
00324 nentries++;
00325 }
00326
00327
00328
00329 if (nentries <= 0)
00330 {
00331 BCLog::Out(BCLog::warning, BCLog::warning,
00332 Form("BCDataSet::ReadDataFromFileText. No events in the file %s.", filename));
00333
00334
00335
00336 file.close();
00337
00338 return ERROR_NOEVENTS;
00339 }
00340
00341
00342
00343 file.close();
00344
00345 return 0;
00346
00347 }
00348
00349
00350
00351 int BCDataSet::ReadDataFromFileUser(const char * filename, std::vector<int> options_int, std::vector<double> options_double, const char * options_char)
00352 {
00353
00354
00355
00356 BCLog::Out(BCLog::warning, BCLog::warning,
00357 "BCDataSet::ReadDataFromFileUser. Function needs to be overloaded by the user.");
00358
00359 return ERROR_METHODNOTOVERLOADED;
00360
00361 }
00362
00363
00364
00365 void BCDataSet::AddDataPoint(BCDataPoint * datapoint)
00366 {
00367
00368
00369
00370
00371 if (fBCDataVector == 0)
00372 fBCDataVector = new BCDataVector();
00373
00374
00375
00376 fBCDataVector -> push_back(datapoint);
00377
00378 };
00379
00380
00381
00382 void BCDataSet::Reset()
00383 {
00384
00385
00386
00387
00388 if (fBCDataVector != 0)
00389 fBCDataVector -> clear();
00390
00391 };
00392
00393