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