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 | |
BCDataSet & | operator= (const BCDataSet &bcdataset) |
Member functions (get) | |
unsigned int | GetNDataPoints () |
unsigned int | GetNValuesPerPoint () |
BCDataPoint * | GetDataPoint (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 | |
BCDataVector * | fBCDataVector |
A class representing a set of data points.
Definition at line 36 of file BCDataSet.h.
BCDataSet::BCDataSet | ( | ) |
BCDataSet::BCDataSet | ( | const BCDataSet & | bcdataset | ) |
The copy constructor
Definition at line 43 of file BCDataSet.cxx.
{ if (bcdataset.fBCDataVector) { fBCDataVector = new BCDataVector(); for (int i = 0; i < int(bcdataset.fBCDataVector->size()); ++i) { if (bcdataset.fBCDataVector->at(i)) fBCDataVector->push_back(new BCDataPoint(*(bcdataset.fBCDataVector->at(i)))); else fBCDataVector->push_back(0); } } else fBCDataVector = 0; }
BCDataSet::~BCDataSet | ( | ) | [virtual] |
Default destructor
Definition at line 31 of file BCDataSet.cxx.
{ if (fBCDataVector) { int ndatapoints = int(fBCDataVector->size()); for (int i = 0; i < ndatapoints; ++i) delete fBCDataVector->at(i); fBCDataVector->clear(); delete fBCDataVector; } }
void BCDataSet::AddDataPoint | ( | BCDataPoint * | datapoint | ) |
Adds a data point to the data set.
datapoint | The data point to be added |
Definition at line 364 of file BCDataSet.cxx.
{ // check if memory for the vector has been allocated and // allocate if not. if (fBCDataVector == 0) fBCDataVector = new BCDataVector(); // add data point to the data set. fBCDataVector->push_back(datapoint); }
void BCDataSet::Dump | ( | ) |
Dump the data to the standard output
Definition at line 391 of file BCDataSet.cxx.
{ if (!fBCDataVector) { BCLog::OutError("BCDataSet::Dump : Data set is empty. Nothing to dump."); return; } std::cout << std::endl << "Dumping dataset:" << std::endl << "----------------" << std::endl << " - number of points: " << fBCDataVector->size() << std::endl << " - number of values per point: " << GetDataPoint(0)->GetNValues() << std::endl << " - values:" << std::endl; unsigned int n = GetDataPoint(0)->GetNValues(); for (unsigned int i=0; i< fBCDataVector->size(); i++) { std::cout << Form("%5d : ", i); for (unsigned int j=0; j<n; j++) std::cout << Form("%12.5g", GetDataPoint(i)->GetValue(j)); std::cout << std::endl; } std::cout << std::endl; }
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.
index | The index of the component to be returned. |
Definition at line 122 of file BCDataSet.cxx.
{ unsigned int N = GetNDataPoints(); std::vector<double> components( N , 0.0 ); BCDataPoint* point=0; for (unsigned int i = 0; i < N; ++i) { //rely on index checking in Get... methods point = GetDataPoint(i); components[i] = point->GetValue(index); } return components; }
BCDataPoint * BCDataSet::GetDataPoint | ( | unsigned int | index | ) |
index | The index of the data point to be returned. |
Definition at line 104 of file BCDataSet.cxx.
{ if (!fBCDataVector || GetNDataPoints()==0 ) { BCLog::OutError("BCDataSet::GetDataPoint : Dataset is empty."); return 0; } // check if index is within range. Return the data point if true ... if(index < GetNDataPoints()) return fBCDataVector->at(index); // ... or give out warning and return 0 if not. BCLog::OutError("BCDataSet::GetDataPoint : Index out of range. Return 0."); return 0; }
unsigned int BCDataSet::GetNDataPoints | ( | ) |
The | vector of data points. |
Definition at line 79 of file BCDataSet.cxx.
{ // check if vector exists. Return number of data points if true ... if (fBCDataVector) return fBCDataVector->size(); // ... or give out error and return 0 if not. BCLog::OutError("BCDataSet::GetNDataPoints : DataSet not yet created."); return 0; }
unsigned int BCDataSet::GetNValuesPerPoint | ( | ) |
Definition at line 92 of file BCDataSet.cxx.
{ // check if vector exists and contains datapoints if (fBCDataVector && fBCDataVector->size() > 0) return GetDataPoint(0)->GetNValues(); BCLog::OutError("BCDataSet::GetNValuesPerPoint : Data set doesn't exist yet"); return 0; }
Defaut assignment operator
Definition at line 59 of file BCDataSet.cxx.
{ if (bcdataset.fBCDataVector) { fBCDataVector = new BCDataVector(); for (int i = 0; i < int(bcdataset.fBCDataVector->size()); ++i) { if (bcdataset.fBCDataVector->at(i)) fBCDataVector->push_back(new BCDataPoint(*(bcdataset.fBCDataVector->at(i)))); else fBCDataVector->push_back(0); } } else fBCDataVector = 0; // return this return *this; }
int BCDataSet::ReadDataFromFile | ( | const char * | filename, | |
int | nvariables | |||
) | [inline] |
Definition at line 103 of file BCDataSet.h.
{ return this -> ReadDataFromFileTxt(filename, nvariables); };
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 100 of file BCDataSet.h.
{ return this -> ReadDataFromFileTree(filename, treename, branchnames); };
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.
filename | The name of the .root file. | |
treename | The name of the TTree. | |
branchnames | A list of the names of the branches separated by a comma |
Definition at line 141 of file BCDataSet.cxx.
{ // open root file TFile * file = new TFile(filename, "READ"); // check if file is open and warn if not. if (!file->IsOpen()) { BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : Could not open file %s.", filename)); return ERROR_FILENOTFOUND; } // get tree TTree * tree = (TTree*) file->Get(treename); // check if tree is there and warn if not. if (!tree) { BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : Could not find TTree %s.", treename)); // close file file->Close(); return ERROR_TREENOTFOUND; } // if data set contains data, clear data object container ... if (fBCDataVector != 0) { fBCDataVector->clear(); BCLog::OutDetail("BCDataSet::ReadDataFromFileTree : Overwrite existing data."); } // ... or allocate memory for the vector if not. else fBCDataVector = new BCDataVector(); // get branch names. // first, copy the branchnames into a std::string. std::string branches(branchnames); // define a vector of std::strings which contain the tree names. std::vector<std::string> * branchnamevector = new std::vector<std::string>; // the names are supposed to be separated by commas. find first comma // entry in the string. int temp_index = branches.find_first_of(","); // reset number of branches int nbranches = 0; // repeat until the is nothing left in the string. while(branches.size() > 0) { // temporary string which contains the name of the current branch std::string branchname; // get current branch name // if there is no comma the current branchname corresponds to the whole string, ... if (temp_index == -1) branchname = branches; // ... if there is a comma, copy that part of the string into the current branchname. else branchname.assign(branches, 0, temp_index); // write branch name to a vector branchnamevector->push_back(branchname); // increase the number of branches found nbranches++; // cut remaining string with branchnames // if there is no comma left empty the string, ... if (temp_index == -1) branches = ""; // ... if there is a comma remove the current branchname from the string. else branches.erase(0, temp_index + 1); // find the next comma temp_index = branches.find_first_of(","); } // create temporary vector with data and assign some zeros. std::vector<double> data; data.assign(nbranches, 0.0); // set the branch address. for (int i = 0; i < nbranches; i++) tree->SetBranchAddress(branchnamevector->at(i).data(), &data.at(i)); // calculate maximum number of entries int nentries = tree->GetEntries(); // check if there are any events in the tree and close file if not. if (nentries <= 0) { BCLog::OutError(Form("BCDataSet::ReadDataFromFileTree : No events in TTree %s.", treename)); // close file file->Close(); return ERROR_NOEVENTS; } // loop over entries for (int ientry = 0; ientry < nentries; ientry++) { // get entry tree->GetEntry(ientry); // create data object BCDataPoint * datapoint = new BCDataPoint(nbranches); // copy data for (int i = 0; i < nbranches; i++) datapoint->SetValue(i, data.at(i)); // add data point to this data set. AddDataPoint(datapoint); } // close file file->Close(); // remove file pointer. if (file) delete file; return 0; }
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.
filename | The name of the .txt file. | |
nvariables | The number of variables. |
Definition at line 283 of file BCDataSet.cxx.
{ // open text file. std::fstream file; file.open(filename, std::fstream::in); // check if file is open and warn if not. if (!file.is_open()) { BCLog::OutError(Form("BCDataSet::ReadDataFromFileText : Could not open file %s.", filename)); return ERROR_FILENOTFOUND; } // if data set contains data, clear data object container ... if (fBCDataVector != 0) { fBCDataVector->clear(); BCLog::OutDetail("BCDataSet::ReadDataFromFileTxt : Overwrite existing data."); } // ... or allocate memory for the vector if not. else fBCDataVector = new BCDataVector(); // create temporary vector with data and assign some zeros. std::vector<double> data; data.assign(nbranches, 0.0); // reset counter int nentries = 0; // read data and create data points. while (!file.eof()) { // read data from file int i=0; while(file >> data[i]) { if (i==nbranches-1) break; i++; } // create data point. if(i == nbranches-1) { BCDataPoint * datapoint = new BCDataPoint(nbranches); // copy data into data point for (int i = 0; i < nbranches; i++) datapoint->SetValue(i, data.at(i)); // add data point to this data set. AddDataPoint(datapoint); // increase counter nentries++; } } // check if there are any events in the tree and close file if not. if (nentries <= 0) { BCLog::OutError(Form("BCDataSet::ReadDataFromFileText : No events in the file %s.", filename)); // close file file.close(); return ERROR_NOEVENTS; } // close file file.close(); return 0; }
void BCDataSet::Reset | ( | ) |
Resets the content of the data set
Definition at line 379 of file BCDataSet.cxx.
{ // if memory has been allocated to the data set // clear the content. if (fBCDataVector != 0) fBCDataVector->clear(); }
BCDataVector* BCDataSet::fBCDataVector [private] |
A vector containing the data points
Definition at line 149 of file BCDataSet.h.