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