@@ -2303,13 +2303,49 @@ void RooDataHist::printMultiline(ostream& os, Int_t content, bool verbose, TStri
2303
2303
}
2304
2304
}
2305
2305
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
2307
2317
{
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
+ }
2313
2349
}
2314
2350
2315
2351
0 commit comments