BAT  0.9.4
The Bayesian analysis toolkit
 All Classes Namespaces Functions Variables Enumerations
BCDataSet.cxx
1 /*
2  * Copyright (C) 2007-2014, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 // ---------------------------------------------------------
10 
11 #include "BCDataSet.h"
12 
13 #include "BCDataPoint.h"
14 #include "BCLog.h"
15 
16 #include <TFile.h>
17 #include <TTree.h>
18 #include <TString.h>
19 
20 #include <iostream>
21 #include <fstream>
22 
23 // ---------------------------------------------------------
25 {
26  fBCDataVector = 0;
27 }
28 
29 // ---------------------------------------------------------
30 
32 {
33  if (fBCDataVector) {
34  int ndatapoints = int(fBCDataVector->size());
35  for (int i = 0; i < ndatapoints; ++i)
36  delete fBCDataVector->at(i);
37  fBCDataVector->clear();
38  delete fBCDataVector;
39  }
40 }
41 
42 // ---------------------------------------------------------
43 BCDataSet::BCDataSet(const BCDataSet & bcdataset)
44 {
45  if (bcdataset.fBCDataVector) {
46  fBCDataVector = new BCDataVector();
47  for (int i = 0; i < int(bcdataset.fBCDataVector->size()); ++i) {
48  if (bcdataset.fBCDataVector->at(i))
49  fBCDataVector->push_back(new BCDataPoint(*(bcdataset.fBCDataVector->at(i))));
50  else
51  fBCDataVector->push_back(0);
52  }
53  }
54  else
55  fBCDataVector = 0;
56 }
57 
58 // ---------------------------------------------------------
60 {
61  if (bcdataset.fBCDataVector) {
62  fBCDataVector = new BCDataVector();
63  for (int i = 0; i < int(bcdataset.fBCDataVector->size()); ++i) {
64  if (bcdataset.fBCDataVector->at(i))
65  fBCDataVector->push_back(new BCDataPoint(*(bcdataset.fBCDataVector->at(i))));
66  else
67  fBCDataVector->push_back(0);
68  }
69  }
70  else
71  fBCDataVector = 0;
72 
73  // return this
74  return *this;
75 }
76 
77 // ---------------------------------------------------------
78 
80 {
81  return fBCDataVector ? fBCDataVector->size() : 0;
82 }
83 
84 // ---------------------------------------------------------
85 
87 {
88  // check if vector exists and contains datapoints
89  if (fBCDataVector && fBCDataVector->size() > 0)
90  return GetDataPoint(0)->GetNValues();
91 
92  BCLog::OutError("BCDataSet::GetNValuesPerPoint : Data set doesn't exist yet");
93  return 0;
94 }
95 
96 // ---------------------------------------------------------
97 
98 BCDataPoint * BCDataSet::GetDataPoint(unsigned int index)
99 {
100  if (!fBCDataVector || GetNDataPoints()==0 )
101  {
102  BCLog::OutError("BCDataSet::GetDataPoint : Dataset is empty.");
103  return 0;
104  }
105 
106  // check if index is within range. Return the data point if true ...
107  if(index < GetNDataPoints())
108  return fBCDataVector->at(index);
109 
110  // ... or give out warning and return 0 if not.
111  BCLog::OutError("BCDataSet::GetDataPoint : Index out of range. Return 0.");
112  return 0;
113 }
114 
115 // ---------------------------------------------------------
116 std::vector<double> BCDataSet::GetDataComponents( int index)
117 {
118  unsigned int N = GetNDataPoints();
119  std::vector<double> components( N , 0.0 );
120 
121  BCDataPoint* point=0;
122  for (unsigned int i = 0; i < N; ++i) {
123  //rely on index checking in Get... methods
124  point = GetDataPoint(i);
125  components[i] = point->GetValue(index);
126  }
127 
128  return components;
129 }
130 
131 
132 
133 // ---------------------------------------------------------
134 
135 int BCDataSet::ReadDataFromFileTree(const char * filename, const char * treename, const char * branchnames)
136 {
137  // open root file
138  TFile * file = TFile::Open(filename, "READ");
139 
140  // check if file is open and warn if not.
141  if (!file->IsOpen())
142  {
143  BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : Could not open file %s.", filename));
144  return -1;
145  }
146 
147  // get tree
148  TTree * tree = (TTree*) file->Get(treename);
149 
150  // check if tree is there and warn if not.
151  if (!tree)
152  {
153  BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : Could not find TTree %s.", treename));
154 
155  // close file
156  file->Close();
157 
158  return -1;
159  }
160 
161  // if data set contains data, clear data object container ...
162  if (fBCDataVector != 0)
163  {
164  fBCDataVector->clear();
165 
166  BCLog::OutDetail("BCDataSet::ReadDataFromFileTree : Overwrite existing data.");
167  }
168 
169  // ... or allocate memory for the vector if not.
170  else
171  fBCDataVector = new BCDataVector();
172 
173  // get branch names.
174 
175  // first, copy the branchnames into a std::string.
176  std::string branches(branchnames);
177 
178  // define a vector of std::strings which contain the tree names.
179  std::vector<std::string> * branchnamevector = new std::vector<std::string>;
180 
181  // the names are supposed to be separated by commas. find first comma
182  // entry in the string.
183  int temp_index = branches.find_first_of(",");
184 
185  // reset number of branches
186  int nbranches = 0;
187 
188  // repeat until the is nothing left in the string.
189  while(branches.size() > 0)
190  {
191  // temporary string which contains the name of the current branch
192  std::string branchname;
193 
194  // get current branch name
195 
196  // if there is no comma the current branchname corresponds to the whole string, ...
197  if (temp_index == -1)
198  branchname = branches;
199 
200  // ... if there is a comma, copy that part of the string into the current branchname.
201  else
202  branchname.assign(branches, 0, temp_index);
203 
204  // write branch name to a vector
205  branchnamevector->push_back(branchname);
206 
207  // increase the number of branches found
208  nbranches++;
209 
210  // cut remaining string with branchnames
211 
212  // if there is no comma left empty the string, ...
213  if (temp_index == -1)
214  branches = "";
215 
216  // ... if there is a comma remove the current branchname from the string.
217  else
218  branches.erase(0, temp_index + 1);
219 
220  // find the next comma
221  temp_index = branches.find_first_of(",");
222  }
223 
224  // create temporary vector with data and assign some zeros.
225  std::vector<double> data;
226  data.assign(nbranches, 0.0);
227 
228  // set the branch address.
229  for (int i = 0; i < nbranches; i++)
230  tree->SetBranchAddress(branchnamevector->at(i).data(), &data.at(i));
231 
232  // calculate maximum number of entries
233  int nentries = tree->GetEntries();
234 
235  // check if there are any events in the tree and close file if not.
236  if (nentries <= 0)
237  {
238  BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : No events in TTree %s.", treename));
239 
240  // close file
241  file->Close();
242 
243  return -1;
244  }
245 
246  // loop over entries
247  for (int ientry = 0; ientry < nentries; ientry++)
248  {
249  // get entry
250  tree->GetEntry(ientry);
251 
252  // create data object
253  BCDataPoint * datapoint = new BCDataPoint(nbranches);
254 
255  // copy data
256 
257  for (int i = 0; i < nbranches; i++)
258  datapoint->SetValue(i, data.at(i));
259 
260  // add data point to this data set.
261  AddDataPoint(datapoint);
262  }
263 
264  // close file
265  file->Close();
266 
267  // remove file pointer.
268  if (file)
269  delete file;
270 
271  return 0;
272 
273 }
274 
275 // ---------------------------------------------------------
276 
277 int BCDataSet::ReadDataFromFileTxt(const char * filename, int nbranches)
278 {
279  // open text file.
280  std::fstream file;
281  file.open(filename, std::fstream::in);
282 
283  // check if file is open and warn if not.
284  if (!file.is_open())
285  {
286  BCLog::OutError(Form("BCDataSet::ReadDataFromFileText : Could not open file %s.", filename));
287  return -1;
288  }
289 
290  // if data set contains data, clear data object container ...
291  if (fBCDataVector != 0)
292  {
293  fBCDataVector->clear();
294 
295  BCLog::OutDetail("BCDataSet::ReadDataFromFileTxt : Overwrite existing data.");
296  }
297 
298  // ... or allocate memory for the vector if not.
299  else
300  fBCDataVector = new BCDataVector();
301 
302  // create temporary vector with data and assign some zeros.
303  std::vector<double> data;
304  data.assign(nbranches, 0.0);
305 
306  // reset counter
307  int nentries = 0;
308 
309  // read data and create data points.
310  while (!file.eof())
311  {
312  // read data from file
313  int i=0;
314  while(file >> data[i])
315  {
316  if (i==nbranches-1)
317  break;
318  i++;
319  }
320 
321  // create data point.
322  if(i == nbranches-1)
323  {
324  BCDataPoint * datapoint = new BCDataPoint(nbranches);
325 
326  // copy data into data point
327  for (int i = 0; i < nbranches; i++)
328  datapoint->SetValue(i, data.at(i));
329 
330  // add data point to this data set.
331  AddDataPoint(datapoint);
332 
333  // increase counter
334  nentries++;
335  }
336  }
337 
338  // check if there are any events in the tree and close file if not.
339  if (nentries <= 0)
340  {
341  BCLog::OutError(Form("BCDataSet::ReadDataFromFileText : No events in the file %s.", filename));
342 
343  // close file
344  file.close();
345 
346  return -1;
347  }
348 
349  // close file
350  file.close();
351 
352  return 0;
353 
354 }
355 
356 // ---------------------------------------------------------
357 
359 {
360 
361  // check if memory for the vector has been allocated and
362  // allocate if not.
363  if (fBCDataVector == 0)
364  fBCDataVector = new BCDataVector();
365 
366  // add data point to the data set.
367  fBCDataVector->push_back(datapoint);
368 
369 }
370 
371 // ---------------------------------------------------------
372 
374 {
375 
376  // if memory has been allocated to the data set
377  // clear the content.
378  if (fBCDataVector != 0)
379  fBCDataVector->clear();
380 
381 }
382 
383 // ---------------------------------------------------------
384 
386 {
387  if (!fBCDataVector) {
388  BCLog::OutError("BCDataSet::Dump : Data set is empty. Nothing to dump.");
389  return;
390  }
391 
392  BCLog::OutSummary("Data set summary:");
393  BCLog::OutSummary(Form("Number of points : %d", int(fBCDataVector->size())));
394  BCLog::OutSummary(Form("Number of values per point : %d", GetDataPoint(0)->GetNValues()));
395  unsigned int n = GetDataPoint(0)->GetNValues();
396  for (unsigned int i=0; i< fBCDataVector->size(); i++) {
397  BCLog::OutSummary(Form("Data point %5d : ", i));
398  for (unsigned int j=0; j<n; j++)
399  BCLog::OutSummary(Form("%d : %12.5g", j, GetDataPoint(i)->GetValue(j)));
400  }
401 }
A class representing a data point.
Definition: BCDataPoint.h:31
unsigned int GetNValues() const
Definition: BCDataPoint.h:65
unsigned int GetNValuesPerPoint()
Definition: BCDataSet.cxx:86
A class representing a set of data points.
Definition: BCDataSet.h:37
BCDataSet & operator=(const BCDataSet &bcdataset)
Definition: BCDataSet.cxx:59
int ReadDataFromFileTree(const char *filename, const char *treename, const char *branchnames)
Definition: BCDataSet.cxx:135
void Reset()
Definition: BCDataSet.cxx:373
std::vector< double > GetDataComponents(int index)
Definition: BCDataSet.cxx:116
virtual ~BCDataSet()
Definition: BCDataSet.cxx:31
double GetValue(unsigned index) const
Definition: BCDataPoint.cxx:34
int ReadDataFromFileTxt(const char *filename, int nvariables)
Definition: BCDataSet.cxx:277
void Dump()
Definition: BCDataSet.cxx:385
BCDataPoint * GetDataPoint(unsigned int index)
Definition: BCDataSet.cxx:98
unsigned int GetNDataPoints()
Definition: BCDataSet.cxx:79
void AddDataPoint(BCDataPoint *datapoint)
Definition: BCDataSet.cxx:358
void SetValue(unsigned index, double value)
Definition: BCDataPoint.cxx:54