11 #include "BCDataSet.h" 14 #include "BCDataPoint.h" 19 #include <TGraphErrors.h> 20 #include <TGraphAsymmErrors.h> 39 std::vector<double> components;
41 if (index >= fNValuesPerPoint)
45 components.reserve(fDataVector.size());
48 for (
unsigned i = 0; i < fDataVector.size(); ++i)
49 components.push_back(fDataVector[i][index]);
67 BCLog::OutError(
"BCDataSet::GetLowerBound : index out of range.");
68 return std::numeric_limits<double>::infinity();
70 if (std::isfinite(fUserLowerBounds[index]))
71 return fUserLowerBounds[index];
72 return fLowerBounds[index];
79 BCLog::OutError(
"BCDataSet::GetUpperBound : index out of range.");
80 return -std::numeric_limits<double>::infinity();
82 if (std::isfinite(fUserUpperBounds[index]))
83 return fUserUpperBounds[index];
84 return fUpperBounds[index];
91 TFile* file = TFile::Open(filename.data(),
"READ");
94 if (!file->IsOpen()) {
95 BCLog::OutError(
"BCDataSet::ReadDataFromFileTree : Could not open file " + filename +
".");
100 TTree* tree = (TTree*) file->Get(treename.data());
104 BCLog::OutError(
"BCDataSet::ReadDataFromFileTree : Could not find TTree " + treename +
".");
110 long nentries = tree->GetEntries();
114 BCLog::OutError(
"BCDataSet::ReadDataFromFileTree : No events in TTree " + treename +
".");
120 if (!fDataVector.empty()) {
122 BCLog::OutDetail(
"BCDataSet::ReadDataFromFileTree : Overwrite existing data.");
126 std::vector<std::string> branches;
128 std::stringstream branch_ss(branchnames);
129 std::string branchname;
130 while (std::getline(branch_ss, branchname, delim))
131 if (!branchname.empty())
132 branches.push_back(branchname);
135 std::vector<double> data(branches.size(), 0);
138 for (
unsigned i = 0; i < branches.size(); ++i)
139 tree->SetBranchAddress(branches[i].data(), &data[i]);
142 for (
long ientry = 0; ientry < nentries; ++ientry) {
143 tree->GetEntry(ientry);
161 file.open(filename.data(), std::fstream::in);
164 if (!file.is_open()) {
165 BCLog::OutError(
"BCDataSet::ReadDataFromFileText : Could not open file " + filename +
".");
170 if (!fDataVector.empty()) {
172 BCLog::OutDetail(
"BCDataSet::ReadDataFromFileTxt : Overwrite existing data.");
176 std::vector<double> data(nbranches, 0);
182 while (!file.eof()) {
186 while (file >> data[i]) {
187 if (i == nbranches - 1)
193 if (i == nbranches - 1) {
201 BCLog::OutError(
"BCDataSet::ReadDataFromFileText : No events in the file " + filename +
".");
205 return (nentries > 0);
211 if (fNValuesPerPoint == 0 && fDataVector.empty())
217 fDataVector.push_back(datapoint);
221 if (fDataVector.back()[i] < fLowerBounds[i])
222 fLowerBounds[i] = fDataVector.back()[i];
224 if (fDataVector.back()[i] > fUpperBounds[i])
225 fUpperBounds[i] = fDataVector.back()[i];
243 for (
unsigned j = 0; j < fDataVector.size(); ++j) {
246 if (fDataVector[j][i] - nSigma * fDataVector[j][i_err1] < fLowerBounds[i])
247 fLowerBounds[i] = fDataVector[j][i] - nSigma * fDataVector[j][i_err1];
250 if (fDataVector[j][i] + nSigma * fDataVector[j][i_err2] > fUpperBounds[i])
251 fUpperBounds[i] = fDataVector[j][i] + nSigma * fDataVector[j][i_err2];
258 fNValuesPerPoint = n;
259 fLowerBounds.
SetNValues(n, std::numeric_limits<double>::infinity());
260 fUpperBounds.
SetNValues(n, -std::numeric_limits<double>::infinity());
261 fUserLowerBounds.
SetNValues(n, std::numeric_limits<double>::infinity());
262 fUserUpperBounds.
SetNValues(n, -std::numeric_limits<double>::infinity());
263 fFixed.assign(n,
false);
270 BCLog::OutError(
"BCDataSet::SetBounds : index out of range.");
273 if (lower_bound >= upper_bound) {
274 BCLog::OutWarning(
"BCDataSet::SetBounds : lower bound is greater than or equal to upper_bound.");
277 fUserLowerBounds[index] = lower_bound;
278 fUserUpperBounds[index] = upper_bound;
279 fFixed[index] = fixed;
285 output(
"Data set summary:");
288 for (
unsigned i = 0; i < fDataVector.size(); ++i) {
289 output(Form(
"Data point %5u", i));
290 fDataVector[i].PrintSummary(output);
300 TGraph* G =
new TGraph();
303 for (
unsigned i = 0; i < fDataVector.size(); ++i)
304 G->SetPoint(i, fDataVector[i][x], fDataVector[i][y]);
316 TGraphErrors* G =
new TGraphErrors();
319 for (
unsigned i = 0; i < fDataVector.size(); ++i) {
320 G->SetPoint(i, fDataVector[i][x], fDataVector[i][y]);
321 double EX = (ex >= 0) ? fDataVector[i][ex] : 0;
322 double EY = (ey >= 0) ? fDataVector[i][ey] : 0;
323 G->SetPointError(i, EX, EY);
330 TGraphAsymmErrors*
BCDataSet::GetGraph(
unsigned x,
unsigned y,
int ex_below,
int ex_above,
int ey_below,
int ey_above)
const 337 TGraphAsymmErrors* G =
new TGraphAsymmErrors();
340 for (
unsigned i = 0; i < fDataVector.size(); ++i) {
341 G->SetPoint(i, fDataVector[i][x], fDataVector[i][y]);
342 double EXb = (ex_below >= 0) ? fDataVector[i][ex_below] : 0;
343 double EXa = (ex_above >= 0) ? fDataVector[i][ex_above] : 0;
344 double EYb = (ey_below >= 0) ? fDataVector[i][ey_below] : 0;
345 double EYa = (ey_above >= 0) ? fDataVector[i][ey_above] : 0;
346 G->SetPointError(i, EXb, EXa, EYb, EYa);
353 TH2*
BCDataSet::CreateH2(
const char* name,
const char* title,
unsigned x,
unsigned y,
unsigned nbins_x,
unsigned nbins_y,
double x_padding,
double y_padding)
const 367 double dX = x_padding * (x_high - x_low);
372 double dY = y_padding * (y_high - y_low);
380 res =
new TH2D(name, title, nbins_x, x_low, x_high, nbins_y, y_low, y_high);
void SetBounds(unsigned index, double lower_bound, double upper_bound, bool fixed=false)
Set bounds for data values.
bool AddDataPoint(const BCDataPoint &datapoint)
Adds a data point to the data set.
bool ReadDataFromFileTxt(const std::string &filename, int nvariables)
Reads data from a .txt file.
A class representing a data point.
void SetNValues(unsigned n, double val=0.)
Set the number of variables.
unsigned int GetNValues() const
Returns the number of values.
A guard object to prevent ROOT from taking over ownership of TNamed objects.
std::vector< double > GetDataComponents(unsigned index) const
Viewing the data set as a table with one row per point, this method returns a specified column...
bool ReadDataFromFileTree(const std::string &filename, const std::string &treename, const std::string &branchnames, char delim= ',')
Reads a TTree from a .root file.
TH2 * CreateH2(const char *name, const char *title, unsigned x, unsigned y, unsigned nbins_x=100, unsigned nbins_y=100, double x_padding=0.10, double y_padding=0.10) const
Get ROOT TH2 with ranges set to data bounds.
unsigned GetNDataPoints() const
void SetNValuesPerPoint(unsigned n)
Set number of values inside each data point.
double GetLowerBound(unsigned index) const
Return user-set lower bound on data, if set, otherwise actual lower bound.
void Reset()
Resets the content of the data set.
void AdjustBoundForUncertainties(unsigned i, double nSigma, unsigned i_err1, int i_err2=-1)
Recalculate a data axis bound accounting for uncertainties specified by other data axes...
void PrintSummary(void(*output)(const std::string &)=BCLog::OutSummary) const
Print summary to string handler.
BCDataSet(unsigned n=0)
Default constructor.
double GetUpperBound(unsigned index) const
Return user-set upper bound on data, if set, otherwise actual upper bound.
TGraph * GetGraph(unsigned x, unsigned y) const
Get data set as ROOT TGraph object,.
unsigned GetNValuesPerPoint() const