00001
00002
00003
00004
00005
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
00043 if (fBCDataVector)
00044 return fBCDataVector -> size();
00045
00046
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
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
00075 if(index >= 0 && index < this -> GetNDataPoints())
00076 return fBCDataVector -> at(index);
00077
00078
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
00088 TFile * file = new TFile(filename, "READ");
00089
00090
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
00099 TTree * tree = (TTree*) file -> Get(treename);
00100
00101
00102 if (!tree)
00103 {
00104 BCLog::Out(BCLog::error, BCLog::error,
00105 Form("BCDataSet::ReadDataFromFileTree : Could not find TTree %s.", treename));
00106
00107
00108 file -> Close();
00109
00110 return ERROR_TREENOTFOUND;
00111 }
00112
00113
00114 if (fBCDataVector != 0)
00115 {
00116 fBCDataVector -> clear();
00117
00118 BCLog::Out(BCLog::detail, BCLog::detail,"BCDataSet::ReadDataFromFileTree : Overwrite existing data.");
00119 }
00120
00121
00122 else
00123 fBCDataVector = new BCDataVector();
00124
00125
00126
00127
00128 std::string branches(branchnames);
00129
00130
00131 std::vector<std::string> * branchnamevector = new std::vector<std::string>;
00132
00133
00134
00135 int temp_index = branches.find_first_of(",");
00136
00137
00138 int nbranches = 0;
00139
00140
00141 while(branches.size() > 0)
00142 {
00143
00144 std::string branchname;
00145
00146
00147
00148
00149 if (temp_index == -1)
00150 branchname = branches;
00151
00152
00153 else
00154 branchname.assign(branches, 0, temp_index);
00155
00156
00157 branchnamevector -> push_back(branchname);
00158
00159
00160 nbranches++;
00161
00162
00163
00164
00165 if (temp_index == -1)
00166 branches = "";
00167
00168
00169 else
00170 branches.erase(0, temp_index + 1);
00171
00172
00173 temp_index = branches.find_first_of(",");
00174 }
00175
00176
00177 std::vector<double> data;
00178 data.assign(nbranches, 0.0);
00179
00180
00181 for (int i = 0; i < nbranches; i++)
00182 tree -> SetBranchAddress(branchnamevector -> at(i).data(), &data.at(i));
00183
00184
00185 int nentries = tree -> GetEntries();
00186
00187
00188 if (nentries <= 0)
00189 {
00190 BCLog::Out(BCLog::error, BCLog::error,
00191 Form("BCDataSet::ReadDataFromFileTree : No events in TTree %s.", treename));
00192
00193
00194 file -> Close();
00195
00196 return ERROR_NOEVENTS;
00197 }
00198
00199
00200 for (int ientry = 0; ientry < nentries; ientry++)
00201 {
00202
00203 tree -> GetEntry(ientry);
00204
00205
00206 BCDataPoint * datapoint = new BCDataPoint(nbranches);
00207
00208
00209
00210 for (int i = 0; i < nbranches; i++)
00211 datapoint -> SetValue(i, data.at(i));
00212
00213
00214 this -> AddDataPoint(datapoint);
00215 }
00216
00217
00218 file -> Close();
00219
00220
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
00233 std::fstream file;
00234 file.open(filename, std::fstream::in);
00235
00236
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
00246 if (fBCDataVector != 0)
00247 {
00248 fBCDataVector -> clear();
00249
00250 BCLog::Out(BCLog::detail, BCLog::detail,"BCDataSet::ReadDataFromFileTxt : Overwrite existing data.");
00251 }
00252
00253
00254 else
00255 fBCDataVector = new BCDataVector();
00256
00257
00258 std::vector<double> data;
00259 data.assign(nbranches, 0.0);
00260
00261
00262 int nentries = 0;
00263
00264
00265 while (!file.eof())
00266 {
00267
00268 int i=0;
00269 while(file >> data[i])
00270 {
00271 if (i==nbranches-1)
00272 break;
00273 i++;
00274 }
00275
00276
00277 if(i == nbranches-1)
00278 {
00279 BCDataPoint * datapoint = new BCDataPoint(nbranches);
00280
00281
00282 for (int i = 0; i < nbranches; i++)
00283 datapoint -> SetValue(i, data.at(i));
00284
00285
00286 this -> AddDataPoint(datapoint);
00287
00288
00289 nentries++;
00290 }
00291 }
00292
00293
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
00300 file.close();
00301
00302 return ERROR_NOEVENTS;
00303 }
00304
00305
00306 file.close();
00307
00308 return 0;
00309
00310 }
00311
00312
00313
00314 void BCDataSet::AddDataPoint(BCDataPoint * datapoint)
00315 {
00316
00317
00318
00319 if (fBCDataVector == 0)
00320 fBCDataVector = new BCDataVector();
00321
00322
00323 fBCDataVector -> push_back(datapoint);
00324
00325 }
00326
00327
00328
00329 void BCDataSet::Reset()
00330 {
00331
00332
00333
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