Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions roofit/roofitcore/inc/RooAbsData.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ class RooAbsData : public TNamed, public RooPrintable {

Int_t defaultPrintContents(Option_t* opt) const override ;

/// Print the contents of the dataset to the specified output stream.
virtual void printContents(std::ostream& os = std::cout) const = 0;

void setDirtyProp(bool flag) ;

double moment(const RooRealVar& var, double order, const char* cutSpec=nullptr, const char* cutRange=nullptr) const ;
Expand Down
4 changes: 3 additions & 1 deletion roofit/roofitcore/inc/RooDataHist.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class RooDataHist : public RooAbsData, public RooDirItem {
void printMultiline(std::ostream& os, Int_t content, bool verbose=false, TString indent="") const override;
void printArgs(std::ostream& os) const override;
void printValue(std::ostream& os) const override;
void printDataHistogram(std::ostream& os, RooRealVar* obs) const;

/// Print the contents of the dataset to the specified output stream.
void printContents(std::ostream& os = std::cout) const override;

void SetName(const char *name) override;
void SetNameTitle(const char *name, const char* title) override;
Expand Down
3 changes: 3 additions & 0 deletions roofit/roofitcore/inc/RooDataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class RooDataSet : public RooAbsData, public RooDirItem {
void printArgs(std::ostream& os) const override;
void printValue(std::ostream& os) const override;

/// Print the contents of the dataset to the specified output stream.
void printContents(std::ostream& os = std::cout) const override;

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

Expand Down
48 changes: 42 additions & 6 deletions roofit/roofitcore/src/RooDataHist.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2303,13 +2303,49 @@ void RooDataHist::printMultiline(ostream& os, Int_t content, bool verbose, TStri
}
}

void RooDataHist::printDataHistogram(ostream& os, RooRealVar* obs) const
/**
* \brief Prints the contents of the RooDataHist to the specified output stream.
*
* This function iterates through all bins of the histogram and prints the
* coordinates of each bin, along with its weight and statistical error.
* It is designed to be robust, handling empty or invalid datasets,
* and works for histograms of any dimension.
*
* \param os The output stream (e.g., std::cout) to write the contents to.
*/
void RooDataHist::printContents(std::ostream& os) const
{
for(Int_t i=0; i<obs->getBins(); ++i){
this->get(i);
obs->setBin(i);
os << this->weight() << " +/- " << this->weightSquared() << std::endl;
}
os << "Contents of RooDataHist \"" << GetName() << "\"" << std::endl;

if (numEntries() == 0) {
os << "(dataset is empty)" << std::endl;
return;
}

for (int i = 0; i < numEntries(); ++i) {
const RooArgSet* obs = get(i); // load i-th bin
os << " Bin " << i << ": ";

bool first = true;
for (const auto* var : *obs) {
if (!first) os << ", ";
first = false;

os << var->GetName() << "=";
if (auto realVar = dynamic_cast<const RooRealVar*>(var)) {
os << realVar->getVal();
} else if (auto catVar = dynamic_cast<const RooCategory*>(var)) {
os << catVar->getCurrentLabel();
} else {
os << "(unsupported type)"; //added as a precaution
}
}

double lo, hi;
weightError(lo, hi, RooAbsData::SumW2);
os << ", weight=" << weight() << " +/- [" << lo << "," << hi << "]"
<< std::endl;
}
}


Expand Down
55 changes: 55 additions & 0 deletions roofit/roofitcore/src/RooDataSet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1689,3 +1689,58 @@ void RooDataSet::loadValuesFromSlices(RooCategory &indexCat, std::map<std::strin
_dstore->loadValues(sliceData->store(), cutVar, rangeName);
}
}

/**
* \brief Prints the contents of the RooDataSet to the specified output stream.
*
* This function iterates through all events (rows) of the dataset and prints
* the value of each observable, along with the event's weight.
* It is designed to be robust, handling empty or invalid datasets gracefully,
* and works for datasets of any dimension.
*
* \param os The output stream (e.g., std::cout) to write the contents to.
*/
void RooDataSet::printContents(std::ostream& os) const
{
os << "Contents of RooDataSet \"" << GetName() << "\"" << std::endl;

if (numEntries() == 0) {
os << "(dataset is empty)" << std::endl;
return;
}

if (get() == nullptr || get()->empty()) {
os << "(dataset has no observables)" << std::endl;
return;
}

for (int i = 0; i < numEntries(); ++i) {
const RooArgSet* row = get(i); // reuses internal buffers
os << " Entry " << i << ": ";

bool first = true;
for (const auto* var : *row) {
if (!first) os << ", ";
first = false;

os << var->GetName() << "=";
if (auto realVar = dynamic_cast<const RooRealVar*>(var)) {
os << realVar->getVal();
} else if (auto catVar = dynamic_cast<const RooCategory*>(var)) {
os << catVar->getLabel();
} else {
os << "(unsupported type)"; //added as a precaution
}
}

os << ", weight=" << weight();

double lo, hi;
weightError(lo, hi);
if (lo != 0.0 || hi != 0.0) {
os << " ±[" << lo << "," << hi << "]";
}

os << std::endl;
}
}
Loading