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 for (int i = 0; i < nbranches; i++)
00269 file >> data[i];
00270
00271
00272 BCDataPoint * datapoint = new BCDataPoint(nbranches);
00273
00274
00275 for (int i = 0; i < nbranches; i++)
00276 datapoint -> SetValue(i, data.at(i));
00277
00278
00279 this -> AddDataPoint(datapoint);
00280
00281
00282 nentries++;
00283 }
00284
00285
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
00292 file.close();
00293
00294 return ERROR_NOEVENTS;
00295 }
00296
00297
00298 file.close();
00299
00300 return 0;
00301
00302 }
00303
00304
00305
00306 void BCDataSet::AddDataPoint(BCDataPoint * datapoint)
00307 {
00308
00309
00310
00311 if (fBCDataVector == 0)
00312 fBCDataVector = new BCDataVector();
00313
00314
00315 fBCDataVector -> push_back(datapoint);
00316
00317 }
00318
00319
00320
00321 void BCDataSet::Reset()
00322 {
00323
00324
00325
00326 if (fBCDataVector != 0)
00327 fBCDataVector -> clear();
00328
00329 }
00330
00331
00332
00333 void BCDataSet::Dump()
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 }
00358
00359
00360