A class representing a set of data points. More...
#include <BCDataSet.h>
Public Member Functions | |
Constructors and destructors | |
| BCDataSet () | |
| virtual | ~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 | ( | ) |
Definition at line 25 of file BCDataSet.cxx.
{
fBCDataVector = 0;
}
| BCDataSet::~BCDataSet | ( | ) | [virtual] |
Definition at line 32 of file BCDataSet.cxx.
{
if (fBCDataVector)
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 332 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 359 of file BCDataSet.cxx.
{
if (!fBCDataVector)
{
BCLog::Out(BCLog::error, BCLog::error, "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: " << this -> GetDataPoint(0) -> GetNValues() << std::endl
<< " - values:" << std::endl;
unsigned int n = this -> 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", this -> 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 84 of file BCDataSet.cxx.
{
unsigned int N = this->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 = this -> GetDataPoint(i);
components[i] = point -> GetValue(index);
}
return components;
}
| BCDataPoint * BCDataSet::GetDataPoint | ( | unsigned int | index | ) |
Definition at line 66 of file BCDataSet.cxx.
{
if (!fBCDataVector || this -> GetNDataPoints()==0 )
{
BCLog::Out(BCLog::error, BCLog::error,"BCDataSet::GetDataPoint : Dataset is empty.");
return 0;
}
// check if index is within range. Return the data point if true ...
if(index >= 0 && index < this -> GetNDataPoints())
return fBCDataVector -> at(index);
// ... or give out warning and return 0 if not.
BCLog::Out(BCLog::error, BCLog::error,"BCDataSet::GetDataPoint : Index out of range. Return 0.");
return 0;
}
| unsigned int BCDataSet::GetNDataPoints | ( | ) |
Definition at line 40 of file BCDataSet.cxx.
{
// check if vector exists. Return number of data points if true ...
if (fBCDataVector)
return fBCDataVector -> size();
// ... or give out warning and return 0 if not.
BCLog::Out(BCLog::warning, BCLog::warning,"BCDataSet::GetNDataPoints : DataSet not yet created.");
return 0;
}
| unsigned int BCDataSet::GetNValuesPerPoint | ( | ) |
Definition at line 53 of file BCDataSet.cxx.
{
// check if vector exists and contains datapoints
if (fBCDataVector && fBCDataVector -> size() > 0)
return this -> GetDataPoint(0) -> GetNValues();
BCLog::Out(BCLog::error, BCLog::error,
"BCDataSet::GetNValuesPerPoint : Data set doesn't exist yet");
return 0;
}
| 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 89 of file BCDataSet.h.
{ return this -> ReadDataFromFileTree(filename, treename, branchnames); };
| int BCDataSet::ReadDataFromFile | ( | const char * | filename, | |
| int | nvariables | |||
| ) | [inline] |
Definition at line 92 of file BCDataSet.h.
{ 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.
| 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 103 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::Out(BCLog::error, BCLog::error,
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::Out(BCLog::error, BCLog::error,
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::Out(BCLog::detail, BCLog::detail,"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::Out(BCLog::error, BCLog::error,
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.
this -> 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 248 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::Out(BCLog::error, BCLog::error,
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::Out(BCLog::detail, BCLog::detail,"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.
this -> AddDataPoint(datapoint);
// increase counter
nentries++;
}
}
// check if there are any events in the tree and close file if not.
if (nentries <= 0)
{
BCLog::Out(BCLog::error, BCLog::error,
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 347 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] |
Definition at line 138 of file BCDataSet.h.
1.7.1