Skip to content

Commit 3e28f0f

Browse files
JasMehta08guitargeek
authored andcommitted
[RF] Add and refactor printContents() for RooAbsData
Signed-off-by: JasMehta08 <[email protected]>
1 parent a1da2be commit 3e28f0f

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

roofit/roofitcore/inc/RooAbsData.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ class RooAbsData : public TNamed, public RooPrintable {
236236

237237
Int_t defaultPrintContents(Option_t* opt) const override ;
238238

239+
/// Print the contents of the dataset to the specified output stream.
240+
virtual void printContents(std::ostream& os = std::cout) const = 0;
241+
239242
void setDirtyProp(bool flag) ;
240243

241244
double moment(const RooRealVar& var, double order, const char* cutSpec=nullptr, const char* cutRange=nullptr) const ;

roofit/roofitcore/inc/RooDataHist.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ class RooDataHist : public RooAbsData, public RooDirItem {
139139
void printMultiline(std::ostream& os, Int_t content, bool verbose=false, TString indent="") const override;
140140
void printArgs(std::ostream& os) const override;
141141
void printValue(std::ostream& os) const override;
142-
void printDataHistogram(std::ostream& os, RooRealVar* obs) const;
142+
143+
/// Print the contents of the dataset to the specified output stream.
144+
void printContents(std::ostream& os = std::cout) const override;
143145

144146
void SetName(const char *name) override;
145147
void SetNameTitle(const char *name, const char* title) override;

roofit/roofitcore/inc/RooDataSet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class RooDataSet : public RooAbsData, public RooDirItem {
102102
void printArgs(std::ostream& os) const override;
103103
void printValue(std::ostream& os) const override;
104104

105+
/// Print the contents of the dataset to the specified output stream.
106+
void printContents(std::ostream& os = std::cout) const override;
107+
105108
void SetName(const char *name) override;
106109
void SetNameTitle(const char *name, const char* title) override;
107110

roofit/roofitcore/src/RooDataHist.cxx

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,13 +2303,49 @@ void RooDataHist::printMultiline(ostream& os, Int_t content, bool verbose, TStri
23032303
}
23042304
}
23052305

2306-
void RooDataHist::printDataHistogram(ostream& os, RooRealVar* obs) const
2306+
/**
2307+
* \brief Prints the contents of the RooDataHist to the specified output stream.
2308+
*
2309+
* This function iterates through all bins of the histogram and prints the
2310+
* coordinates of each bin, along with its weight and statistical error.
2311+
* It is designed to be robust, handling empty or invalid datasets,
2312+
* and works for histograms of any dimension.
2313+
*
2314+
* \param os The output stream (e.g., std::cout) to write the contents to.
2315+
*/
2316+
void RooDataHist::printContents(std::ostream& os) const
23072317
{
2308-
for(Int_t i=0; i<obs->getBins(); ++i){
2309-
this->get(i);
2310-
obs->setBin(i);
2311-
os << this->weight() << " +/- " << this->weightSquared() << std::endl;
2312-
}
2318+
os << "Contents of RooDataHist \"" << GetName() << "\"" << std::endl;
2319+
2320+
if (numEntries() == 0) {
2321+
os << "(dataset is empty)" << std::endl;
2322+
return;
2323+
}
2324+
2325+
for (int i = 0; i < numEntries(); ++i) {
2326+
const RooArgSet* obs = get(i); // load i-th bin
2327+
os << " Bin " << i << ": ";
2328+
2329+
bool first = true;
2330+
for (const auto* var : *obs) {
2331+
if (!first) os << ", ";
2332+
first = false;
2333+
2334+
os << var->GetName() << "=";
2335+
if (auto realVar = dynamic_cast<const RooRealVar*>(var)) {
2336+
os << realVar->getVal();
2337+
} else if (auto catVar = dynamic_cast<const RooCategory*>(var)) {
2338+
os << catVar->getCurrentLabel();
2339+
} else {
2340+
os << "(unsupported type)"; //added as a precaution
2341+
}
2342+
}
2343+
2344+
double lo, hi;
2345+
weightError(lo, hi, RooAbsData::SumW2);
2346+
os << ", weight=" << weight() << " +/- [" << lo << "," << hi << "]"
2347+
<< std::endl;
2348+
}
23132349
}
23142350

23152351

roofit/roofitcore/src/RooDataSet.cxx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,3 +1689,58 @@ void RooDataSet::loadValuesFromSlices(RooCategory &indexCat, std::map<std::strin
16891689
_dstore->loadValues(sliceData->store(), cutVar, rangeName);
16901690
}
16911691
}
1692+
1693+
/**
1694+
* \brief Prints the contents of the RooDataSet to the specified output stream.
1695+
*
1696+
* This function iterates through all events (rows) of the dataset and prints
1697+
* the value of each observable, along with the event's weight.
1698+
* It is designed to be robust, handling empty or invalid datasets gracefully,
1699+
* and works for datasets of any dimension.
1700+
*
1701+
* \param os The output stream (e.g., std::cout) to write the contents to.
1702+
*/
1703+
void RooDataSet::printContents(std::ostream& os) const
1704+
{
1705+
os << "Contents of RooDataSet \"" << GetName() << "\"" << std::endl;
1706+
1707+
if (numEntries() == 0) {
1708+
os << "(dataset is empty)" << std::endl;
1709+
return;
1710+
}
1711+
1712+
if (get() == nullptr || get()->empty()) {
1713+
os << "(dataset has no observables)" << std::endl;
1714+
return;
1715+
}
1716+
1717+
for (int i = 0; i < numEntries(); ++i) {
1718+
const RooArgSet* row = get(i); // reuses internal buffers
1719+
os << " Entry " << i << ": ";
1720+
1721+
bool first = true;
1722+
for (const auto* var : *row) {
1723+
if (!first) os << ", ";
1724+
first = false;
1725+
1726+
os << var->GetName() << "=";
1727+
if (auto realVar = dynamic_cast<const RooRealVar*>(var)) {
1728+
os << realVar->getVal();
1729+
} else if (auto catVar = dynamic_cast<const RooCategory*>(var)) {
1730+
os << catVar->getLabel();
1731+
} else {
1732+
os << "(unsupported type)"; //added as a precaution
1733+
}
1734+
}
1735+
1736+
os << ", weight=" << weight();
1737+
1738+
double lo, hi;
1739+
weightError(lo, hi);
1740+
if (lo != 0.0 || hi != 0.0) {
1741+
os << " ±[" << lo << "," << hi << "]";
1742+
}
1743+
1744+
os << std::endl;
1745+
}
1746+
}

0 commit comments

Comments
 (0)