BayesianAnalysisToolkit  0.9.3
Private Attributes | List of all members
BCDataSet Class Reference

A class representing a set of data points. More...

#include <BCDataSet.h>

Public Member Functions

Constructors and destructors
 BCDataSet ()
 
 BCDataSet (const BCDataSet &bcdataset)
 
virtual ~BCDataSet ()
 
Assignment operators
BCDataSetoperator= (const BCDataSet &bcdataset)
 
Member functions (get)
unsigned int GetNDataPoints ()
 
unsigned int GetNValuesPerPoint ()
 
BCDataPointGetDataPoint (unsigned int index)
 
std::vector< double > GetDataComponents (int index)
 
Member functions (miscellaneous methods)
int ReadDataFromFile (const char *filename, const char *treename, const char *branchnames)
 
int ReadDataFromFile (const char *filename, int nvariables)
 
int ReadDataFromFileTree (const char *filename, const char *treename, const char *branchnames)
 
int ReadDataFromFileTxt (const char *filename, int nvariables)
 
void AddDataPoint (BCDataPoint *datapoint)
 
void Reset ()
 
void Dump ()
 

Private Attributes

BCDataVectorfBCDataVector
 

Detailed Description

A class representing a set of data points.

Author
Daniel Kollar
Kevin Kröninger
Version
1.0
Date
08.2008 This class represents a data set containing a set of data points. The data points are organized in a vector. The class provides functions to read in data from a file.

Definition at line 37 of file BCDataSet.h.

Constructor & Destructor Documentation

BCDataSet::BCDataSet ( )

Default constructor

Definition at line 24 of file BCDataSet.cxx.

25 {
26  fBCDataVector = 0;
27 }
BCDataSet::BCDataSet ( const BCDataSet bcdataset)

The copy constructor

Definition at line 43 of file BCDataSet.cxx.

44 {
45  if (bcdataset.fBCDataVector) {
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 }
BCDataSet::~BCDataSet ( )
virtual

Default destructor

Definition at line 31 of file BCDataSet.cxx.

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 }

Member Function Documentation

void BCDataSet::AddDataPoint ( BCDataPoint datapoint)

Adds a data point to the data set.

Parameters
datapointThe data point to be added

Definition at line 358 of file BCDataSet.cxx.

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 }
void BCDataSet::Dump ( )

Dump the data to the standard output

Definition at line 385 of file BCDataSet.cxx.

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 }
std::vector< double > BCDataSet::GetDataComponents ( int  index)

Viewing the data set as a table with one row per point, this method returns a specified column.

Parameters
indexThe index of the component to be returned.
Returns
The (index)th component of all data points

Definition at line 116 of file BCDataSet.cxx.

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 }
BCDataPoint * BCDataSet::GetDataPoint ( unsigned int  index)
Parameters
indexThe index of the data point to be returned.
Returns
The data point at the index.

Definition at line 98 of file BCDataSet.cxx.

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 }
unsigned int BCDataSet::GetNDataPoints ( )
Returns
The number of data points.

Definition at line 79 of file BCDataSet.cxx.

80 {
81  return fBCDataVector ? fBCDataVector->size() : 0;
82 }
unsigned int BCDataSet::GetNValuesPerPoint ( )
Returns
number of values per data point (dimension of data).

Definition at line 86 of file BCDataSet.cxx.

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 }
BCDataSet & BCDataSet::operator= ( const BCDataSet bcdataset)

Defaut assignment operator

Definition at line 59 of file BCDataSet.cxx.

60 {
61  if (bcdataset.fBCDataVector) {
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 }
int BCDataSet::ReadDataFromFile ( const char *  filename,
const char *  treename,
const char *  branchnames 
)
inline

Reads data from a file. For a description see the following member functions.

Definition at line 96 of file BCDataSet.h.

97  { return this -> ReadDataFromFileTree(filename, treename, branchnames); };
int BCDataSet::ReadDataFromFile ( const char *  filename,
int  nvariables 
)
inline

Definition at line 99 of file BCDataSet.h.

100  { return this -> ReadDataFromFileTxt(filename, nvariables); };
int BCDataSet::ReadDataFromFileTree ( const char *  filename,
const char *  treename,
const char *  branchnames 
)

Reads a TTree from a .root file. Opens a .root file and gets a TTree. It creates data points containing the values read from the file.

Parameters
filenameThe name of the .root file.
treenameThe name of the TTree.
branchnamesA list of the names of the branches separated by a comma
Returns
An error code.
See Also
ReadDataFromFileTxt(char* filename, int nbranches);
ReadDataFromFileUser(const char * filename, std::vector<int> options_int, std::vector<double> options_double, const char * options_char);

Definition at line 135 of file BCDataSet.cxx.

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 }
int BCDataSet::ReadDataFromFileTxt ( const char *  filename,
int  nvariables 
)

Reads data from a .txt file. Opens a .txt file and creates data objects containing the values read from the file.

Parameters
filenameThe name of the .txt file.
nvariablesThe number of variables.
See Also
ReadDataFromFileTree(char* filename, char* treename, std::vector<char*> branchnames)
ReadDataFromFileUser(const char * filename, std::vector<int> options_int, std::vector<double> options_double, const char * options_char);

Definition at line 277 of file BCDataSet.cxx.

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 }
void BCDataSet::Reset ( )

Resets the content of the data set

Definition at line 373 of file BCDataSet.cxx.

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 }

Member Data Documentation

BCDataVector* BCDataSet::fBCDataVector
private

A vector containing the data points

Definition at line 145 of file BCDataSet.h.


The documentation for this class was generated from the following files: