From 4023cd69be814b2ffbcaf966215aca56d77c3af8 Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Mon, 20 Apr 2026 14:32:57 +0200 Subject: [PATCH 1/4] [RF] Generalize boundary finding for RooHistPdf and RooHistFunc The interface to return bin boundaries and sampling hints for plotting in RooHistPdf and RooHistFunc doesn't consider that the histogram variable might be a transformation of the variable for which the bin boundaries or plot hints are requested. This commit improves the situation by implementing an algorithm that assumes that plot variable and histogram variable are linearly related, and numerically figures out the inverse transformation for transforming histogram bins to plot bins if needed. Non-linear transformations are not inverted exactly, but linear transformations are very common, even explicitly via the `RooLinearVar`, and supporting those is already a good improvement in user experience. For a non-linear transformation, a warning is now emitted so the user knows that the bin boundaries are only approximate. The inverse is deliberately computed arithmetically rather than via the l-value `setVal()` interface, because inverting a boundary can land outside the range of the plot observable, which would throw. Closes #13030. --- roofit/roofitcore/inc/RooHistPdf.h | 5 + roofit/roofitcore/src/RooHistFunc.cxx | 88 ++--------- roofit/roofitcore/src/RooHistPdf.cxx | 184 ++++++++++++++++------ roofit/roofitcore/test/testRooHistPdf.cxx | 102 ++++++++++++ 4 files changed, 255 insertions(+), 124 deletions(-) diff --git a/roofit/roofitcore/inc/RooHistPdf.h b/roofit/roofitcore/inc/RooHistPdf.h index 2ca27da4e22cf..fca7be4daa530 100644 --- a/roofit/roofitcore/inc/RooHistPdf.h +++ b/roofit/roofitcore/inc/RooHistPdf.h @@ -22,6 +22,7 @@ #include "RooDataHist.h" #include +#include class RooRealVar; class RooAbsReal; @@ -145,6 +146,10 @@ class RooHistPdf : public RooAbsPdf { double xlo, double xhi); + static std::vector histogramBoundariesInPlotObs(RooDataHist const &dataHist, RooArgSet const &pdfObsList, + RooArgSet const &histObsList, RooAbsRealLValue &obs, + double xlo, double xhi); + inline void initializeOwnedDataHist(std::unique_ptr &&dataHist) { // The constructor may already have taken ownership of a sanitized clone diff --git a/roofit/roofitcore/src/RooHistFunc.cxx b/roofit/roofitcore/src/RooHistFunc.cxx index ab1d372238e3d..22a7d78e2ec0b 100644 --- a/roofit/roofitcore/src/RooHistFunc.cxx +++ b/roofit/roofitcore/src/RooHistFunc.cxx @@ -318,11 +318,11 @@ std::list* RooHistFunc::plotSamplingHint(RooAbsRealLValue& obs, double x return RooHistPdf::plotSamplingHint(*_dataHist, _depList, _histObsList, _intOrder, obs, xlo, xhi); } - //////////////////////////////////////////////////////////////////////////////// -/// Return sampling hint for making curves of (projections) of this function -/// as the recursive division strategy of RooCurve cannot deal efficiently -/// with the vertical lines that occur in a non-interpolated histogram +/// Return the histogram bin boundaries mapped into the plot-observable +/// coordinate. Handles the case where the function observable is a +/// transformation (shift, scale, ...) of the plot observable so that the +/// returned boundaries are placed at the correct positions in the plot. std::list* RooHistFunc::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const { @@ -331,79 +331,21 @@ std::list* RooHistFunc::binBoundaries(RooAbsRealLValue& obs, double xlo, return nullptr ; } - // Find histogram observable corresponding to pdf observable - RooAbsArg* hobs(nullptr) ; - for (auto i = 0u; i < _histObsList.size(); ++i) { - const auto harg = _histObsList[i]; - const auto parg = _depList[i]; - if (std::string(parg->GetName())==obs.GetName()) { - hobs=harg ; - } + const std::vector boundaries = + RooHistPdf::histogramBoundariesInPlotObs(*_dataHist, _depList, _histObsList, obs, xlo, xhi); + if (boundaries.empty()) { + return nullptr; } - RooAbsRealLValue* transform = nullptr; - if (!hobs) { - - // Considering alternate: input observable is histogram observable and pdf observable is transformation in terms of it - RooAbsArg* pobs = nullptr; - for (auto i = 0u; i < _histObsList.size(); ++i) { - const auto harg = _histObsList[i]; - const auto parg = _depList[i]; - if (std::string(harg->GetName())==obs.GetName()) { - pobs=parg ; - hobs=harg ; - } - } - - // Not found, or check that matching pdf observable is an l-value dependent on histogram observable fails - if (!hobs || !(pobs->dependsOn(obs) && dynamic_cast(pobs))) { - std::cout << "RooHistFunc::binBoundaries(" << GetName() << ") obs = " << obs.GetName() << " hobs is not found, returning null" << std::endl ; - return nullptr ; - } - - // Now we are in business - we are in a situation where the pdf observable LV(x), mapping to a histogram observable x - // We can return bin boundaries by mapping the histogram boundaties through the inverse of the LV(x) transformation - transform = dynamic_cast(pobs) ; - } - - - // Check that observable is in dataset, if not no hint is generated - RooAbsArg* xtmp = _dataHist->get()->find(hobs->GetName()) ; - if (!xtmp) { - std::cout << "RooHistFunc::binBoundaries(" << GetName() << ") hobs = " << hobs->GetName() << " is not found in dataset?" << std::endl ; - _dataHist->get()->Print("v") ; - return nullptr ; - } - RooAbsLValue* lvarg = dynamic_cast(_dataHist->get()->find(hobs->GetName())) ; - if (!lvarg) { - std::cout << "RooHistFunc::binBoundaries(" << GetName() << ") hobs = " << hobs->GetName() << " but is not an LV, returning null" << std::endl ; - return nullptr ; - } - - // Retrieve position of all bin boundaries - const RooAbsBinning* binning = lvarg->getBinningPtr(nullptr); - double* boundaries = binning->array() ; - - auto hint = new std::list ; - - double delta = (xhi-xlo)*1e-8 ; - - // Construct array with pairs of points positioned epsilon to the left and - // right of the bin boundaries - for (Int_t i=0 ; inumBoundaries() ; i++) { - if (boundaries[i]>xlo-delta && boundaries[i]setVal(boundary) ; - hint->push_back(obs.getVal()) ; - } else { - hint->push_back(boundary) ; - } - } + auto hint = new std::list; + const double delta = (xhi - xlo) * 1e-8; + for (double b : boundaries) { + if (b > xlo - delta && b < xhi + delta) { + hint->push_back(b); + } } - return hint ; + return hint; } diff --git a/roofit/roofitcore/src/RooHistPdf.cxx b/roofit/roofitcore/src/RooHistPdf.cxx index 1802cb426ad01..3731c59be6c4b 100644 --- a/roofit/roofitcore/src/RooHistPdf.cxx +++ b/roofit/roofitcore/src/RooHistPdf.cxx @@ -471,6 +471,122 @@ bool RooHistPdf::forceAnalyticalInt(const RooAbsArg& dep) const return forceAnalyticalInt(_pdfObsList, dep); } +//////////////////////////////////////////////////////////////////////////////// +/// Return the histogram bin boundaries mapped to the coordinates of the plot +/// observable `obs`. This handles the case where the p.d.f. observable is a +/// transformation of the plot observable (for example a shift like +/// `x_shifted = x - shift`), so that the returned boundaries are the positions +/// of the histogram bin edges as they appear when the p.d.f. is plotted as a +/// function of `obs`. +/// +/// Returns an empty vector if no corresponding histogram observable can be +/// located, or if a non-invertible transformation is detected. + +std::vector RooHistPdf::histogramBoundariesInPlotObs(RooDataHist const &dataHist, RooArgSet const &pdfObsList, + RooArgSet const &histObsList, RooAbsRealLValue &obs, + double xlo, double xhi) +{ + std::vector result; + + // Locate the histogram observable that corresponds to the plot observable. + // These are the scenarios we handle: + // 1. The plot observable `obs` has the same name as one of the p.d.f. + // observables. That p.d.f. observable is the histogram observable + // itself (no transformation). + // 2. The p.d.f. observable is a non-trivial function of `obs` (e.g. a + // shift or scale). We have to invert that function to map the + // histogram bin boundaries back into the plot-observable coordinate. + RooAbsArg *histObs = nullptr; + RooAbsArg *pdfObs = nullptr; + bool useTransform = false; + for (unsigned int i = 0; i < pdfObsList.size(); ++i) { + if (std::string(obs.GetName()) == pdfObsList[i]->GetName()) { + histObs = histObsList[i]; + break; + } + } + if (!histObs) { + for (unsigned int i = 0; i < pdfObsList.size(); ++i) { + auto *po = dynamic_cast(pdfObsList[i]); + if (po && po->dependsOn(obs)) { + histObs = histObsList[i]; + pdfObs = pdfObsList[i]; + useTransform = true; + break; + } + } + } + if (!histObs) { + return result; + } + + auto *lval = dynamic_cast(dataHist.get()->find(histObs->GetName())); + if (!lval) { + return result; + } + + const RooAbsBinning *binning = lval->getBinningPtr(nullptr); + const int nBoundaries = binning->numBoundaries(); + const double *rawBoundaries = binning->array(); + + if (!useTransform) { + result.assign(rawBoundaries, rawBoundaries + nBoundaries); + return result; + } + + // We need to invert the transformation `pdfObs = T(obs)` to map the + // histogram bin boundaries (which live in the `pdfObs` coordinate) back into + // the plot-observable coordinate `obs`. + // + // We deliberately do *not* invert l-values (e.g. RooLinearVar) via their + // setVal() member, even though that would be exact: inverting a boundary + // often lands outside the range of `obs` (a bin edge at the border of the + // plot maps beyond it), and assigning an out-of-range value to a RooRealVar + // throws. Instead we invert arithmetically by linearly approximating `T` on + // the plot range and solving for `obs` at each histogram bin boundary. This + // never assigns out-of-range values, is exact for linear transformations + // (l-value or not), and can represent boundaries outside the plot range. + auto *pdfObsReal = static_cast(pdfObs); + const double probeLo = std::max(xlo, obs.getMin()); + const double probeHi = std::min(xhi, obs.getMax()); + if (probeHi > probeLo) { + const double obsSave = obs.getVal(); + obs.setVal(probeLo); + const double plo = pdfObsReal->getVal(); + obs.setVal(probeHi); + const double phi = pdfObsReal->getVal(); + // Probe the midpoint to check whether the linear assumption actually holds. + const double probeMid = 0.5 * (probeLo + probeHi); + obs.setVal(probeMid); + const double pmid = pdfObsReal->getVal(); + obs.setVal(obsSave); + const double slope = (phi - plo) / (probeHi - probeLo); + + // Warn if the transformation is visibly non-linear: the boundaries are + // then placed only approximately and the plotted curve may look wrong. + const double pmidLinear = plo + slope * (probeMid - probeLo); + const double scale = std::max(std::abs(plo), std::abs(phi)); + if (std::abs(pmid - pmidLinear) > 1e-7 * (scale > 0.0 ? scale : 1.0)) { + oocoutW(nullptr, Plotting) + << "RooHistPdf::histogramBoundariesInPlotObs: the histogram observable '" << pdfObsReal->GetName() + << "' appears to be a non-linear transformation of the plot observable '" << obs.GetName() + << "'. The bin boundaries used for plotting and integration are approximated assuming a linear " + "relation and may be placed incorrectly." + << std::endl; + } + + if (slope != 0.0) { + result.reserve(nBoundaries); + for (int i = 0; i < nBoundaries; ++i) { + result.push_back(probeLo + (rawBoundaries[i] - plo) / slope); + } + } + } + + // The transformation may reverse the order (negative slope). + std::sort(result.begin(), result.end()); + return result; +} //////////////////////////////////////////////////////////////////////////////// /// Return sampling hint for making curves of (projections) of this function @@ -496,40 +612,21 @@ std::list* RooHistPdf::plotSamplingHint(RooDataHist const& dataHist, return nullptr; } - // Check that observable is in dataset, if not no hint is generated - RooAbsArg* dhObs = nullptr; - for (unsigned int i=0; i < pdfObsList.size(); ++i) { - RooAbsArg* histObs = histObsList[i]; - RooAbsArg* pdfObs = pdfObsList[i]; - if (std::string(obs.GetName())==pdfObs->GetName()) { - dhObs = dataHist.get()->find(histObs->GetName()) ; - break; - } + const std::vector boundaries = histogramBoundariesInPlotObs(dataHist, pdfObsList, histObsList, obs, xlo, xhi); + if (boundaries.empty()) { + return nullptr; } - if (!dhObs) { - return nullptr; - } - RooAbsLValue* lval = dynamic_cast(dhObs) ; - if (!lval) { - return nullptr; - } - - // Retrieve position of all bin boundaries - - const RooAbsBinning* binning = lval->getBinningPtr(nullptr); - std::span boundaries{binning->array(), static_cast(binning->numBoundaries())}; - // Use the helper function from RooCurve to make sure to get sampling hints // that work with the RooFitPlotting. return RooCurve::plotSamplingHintForBinBoundaries(boundaries, xlo, xhi); } - //////////////////////////////////////////////////////////////////////////////// -/// Return sampling hint for making curves of (projections) of this function -/// as the recursive division strategy of RooCurve cannot deal efficiently -/// with the vertical lines that occur in a non-interpolated histogram +/// Return the histogram bin boundaries in the plot-observable coordinate. +/// The returned list is used by RooCurve's adaptive sampler to avoid stepping +/// over the discontinuities of a non-interpolated histogram, and by +/// RooBinIntegrator when integrating over this p.d.f. std::list* RooHistPdf::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const { @@ -538,35 +635,20 @@ std::list* RooHistPdf::binBoundaries(RooAbsRealLValue& obs, double xlo, return nullptr; } - // Check that observable is in dataset, if not no hint is generated. The pdf - // observable can be named differently, e.g. when it was prefixed for a - // RooSimultaneous channel in the likelihood. - RooAbsArg *dhObs = _dataHist->get()->find(obs.GetName()); - for (unsigned int i = 0; !dhObs && i < _pdfObsList.size(); ++i) { - if (std::string(obs.GetName()) == _pdfObsList[i]->GetName()) { - dhObs = _dataHist->get()->find(_histObsList[i]->GetName()); - } - } - RooAbsLValue *lvarg = dynamic_cast(dhObs); - if (!lvarg) { - return nullptr ; + const std::vector boundaries = + histogramBoundariesInPlotObs(*_dataHist, _pdfObsList, _histObsList, obs, xlo, xhi); + if (boundaries.empty()) { + return nullptr; } - // Retrieve position of all bin boundaries - const RooAbsBinning* binning = lvarg->getBinningPtr(nullptr); - double* boundaries = binning->array() ; - - auto hint = new std::list ; - - // Construct array with pairs of points positioned epsilon to the left and - // right of the bin boundaries - for (Int_t i=0 ; inumBoundaries() ; i++) { - if (boundaries[i]>=xlo && boundaries[i]<=xhi) { - hint->push_back(boundaries[i]) ; - } + auto hint = new std::list; + for (double b : boundaries) { + if (b >= xlo && b <= xhi) { + hint->push_back(b); + } } - return hint ; + return hint; } diff --git a/roofit/roofitcore/test/testRooHistPdf.cxx b/roofit/roofitcore/test/testRooHistPdf.cxx index 7a25829fb0888..7aeb08ad6dea3 100644 --- a/roofit/roofitcore/test/testRooHistPdf.cxx +++ b/roofit/roofitcore/test/testRooHistPdf.cxx @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -11,10 +12,13 @@ #include #include +#include + #include #include #include +#include #include #include @@ -277,3 +281,101 @@ TEST(RooHistPdf, BinnedLikelihoodInSimultaneous) EXPECT_DOUBLE_EQ(nllSim->getVal(), nllA->getVal()); } + +// GitHub issue #13030: plotting a RooHistPdf whose observable is a +// transformation of the plot variable (e.g. a shift `x_shifted = x - shift`) +// produced visually broken curves because `binBoundaries` and +// `plotSamplingHint` returned the raw histogram boundaries in the histogram +// observable coordinate instead of the plot observable coordinate. +TEST(RooHistPdf, ShiftedBinBoundaries) +{ + RooRealVar x{"x", "x", 1000, 1500}; + x.setBins(5); // bin edges in x: 1000, 1100, 1200, 1300, 1400, 1500 + + RooRealVar shift{"shift", "shift", 25.0, -100, 100}; + + TH1D h{"h", "", x.numBins(), x.getMin(), x.getMax()}; + for (int i = 1; i <= h.GetNbinsX(); ++i) { + h.SetBinContent(i, 1.0); + } + RooDataHist dh{"dh", "", x, &h}; + + // Case 1: pdfObs = x - shift as a RooFormulaVar (no l-value inverse). + { + RooFormulaVar xShifted{"x_shifted", "x - shift", {x, shift}}; + RooHistPdf pdf{"pdf_f", "", xShifted, x, dh, 0}; + std::unique_ptr> boundaries{pdf.binBoundaries(x, 1000.0, 1600.0)}; + ASSERT_TRUE(boundaries); + // Hist bin boundary `b` satisfies `x - shift = b`, so plot_x = b + shift. + const std::vector expected{1025.0, 1125.0, 1225.0, 1325.0, 1425.0, 1525.0}; + ASSERT_EQ(boundaries->size(), expected.size()); + auto it = boundaries->begin(); + for (double e : expected) { + EXPECT_DOUBLE_EQ(*it++, e); + } + } + + // Case 2: pdfObs = RooLinearVar (slope=1, offset=shift -> xShifted = x + shift). + { + RooRealVar slope{"slope", "slope", 1.0}; + RooLinearVar xShifted{"x_shifted_lv", "", x, slope, shift}; + RooHistPdf pdf{"pdf_lv", "", RooArgList{xShifted}, RooArgList{x}, dh, 0}; + std::unique_ptr> boundaries{pdf.binBoundaries(x, 1000.0, 1500.0)}; + ASSERT_TRUE(boundaries); + // xShifted = x + 25 -> plot_x = b - 25; only values inside [1000, 1500] kept. + const std::vector expected{1075.0, 1175.0, 1275.0, 1375.0, 1475.0}; + ASSERT_EQ(boundaries->size(), expected.size()); + auto it = boundaries->begin(); + for (double e : expected) { + EXPECT_DOUBLE_EQ(*it++, e); + } + } + + // Case 3: identity (no transformation) keeps the raw boundaries. + { + RooHistPdf pdf{"pdf_id", "", x, dh, 0}; + std::unique_ptr> boundaries{pdf.binBoundaries(x, 1000.0, 1500.0)}; + ASSERT_TRUE(boundaries); + const std::vector expected{1000.0, 1100.0, 1200.0, 1300.0, 1400.0, 1500.0}; + ASSERT_EQ(boundaries->size(), expected.size()); + auto it = boundaries->begin(); + for (double e : expected) { + EXPECT_DOUBLE_EQ(*it++, e); + } + } +} + +// GitHub issue #13030: a non-linear transformation of the plot observable +// cannot be inverted exactly by the (linear) boundary-finding algorithm. In +// that case the boundaries are still returned, but only approximately, and the +// user must be warned that the plotted curve may be placed incorrectly. +TEST(RooHistPdf, NonLinearBinBoundariesWarn) +{ + RooRealVar x{"x", "x", 1.0, 5.0}; + x.setBins(4); + + TH1D h{"h", "", x.numBins(), x.getMin(), x.getMax()}; + for (int i = 1; i <= h.GetNbinsX(); ++i) { + h.SetBinContent(i, 1.0); + } + RooDataHist dh{"dh", "", x, &h}; + + // A genuinely non-linear transformation (not an l-value, so it goes through + // the linear-approximation path). + RooFormulaVar xSquared{"x_squared", "x * x", x}; + RooHistPdf pdf{"pdf_nl", "", xSquared, x, dh, 0}; + + std::unique_ptr> boundaries; + std::string msg; + { + RooHelpers::HijackMessageStream hijack{RooFit::WARNING, RooFit::Plotting}; + boundaries.reset(pdf.binBoundaries(x, x.getMin(), x.getMax())); + msg = hijack.str(); + } + + // Boundaries are still produced (approximately), and a warning was emitted. + ASSERT_TRUE(boundaries); + EXPECT_FALSE(boundaries->empty()); + EXPECT_TRUE(msg.find("non-linear") != std::string::npos) + << "Expected a non-linearity warning, but got: '" << msg << "'"; +} From d91f22ea88aa01e7be70442358f166f4e6852e01 Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Fri, 20 Nov 2026 13:01:40 +0000 Subject: [PATCH 2/4] [RF] Test more paths of the RooHistPdf bin boundary mapping Cover RooHistFunc, a transformation with negative slope and plotSamplingHint() for transformed histogram observables. On master, RooHistFunc throws for a RooLinearVar observable and plotSamplingHint() returns no hint for a shifted RooHistPdf. Assisted-by: claude-code:claude-opus-5-5 --- roofit/roofitcore/test/testRooHistPdf.cxx | 91 +++++++++++++---------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/roofit/roofitcore/test/testRooHistPdf.cxx b/roofit/roofitcore/test/testRooHistPdf.cxx index 7aeb08ad6dea3..a89fc36247c06 100644 --- a/roofit/roofitcore/test/testRooHistPdf.cxx +++ b/roofit/roofitcore/test/testRooHistPdf.cxx @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include // Verify that RooFit correctly uses analytic integration when having a // RooLinearVar as the observable of a RooHistPdf. @@ -282,6 +284,20 @@ TEST(RooHistPdf, BinnedLikelihoodInSimultaneous) EXPECT_DOUBLE_EQ(nllSim->getVal(), nllA->getVal()); } +namespace { + +// Takes ownership of a list returned by binBoundaries() or plotSamplingHint(). +std::vector toVector(std::list *list) +{ + std::unique_ptr> owned{list}; + if (!owned) { + return {}; + } + return {owned->begin(), owned->end()}; +} + +} // namespace + // GitHub issue #13030: plotting a RooHistPdf whose observable is a // transformation of the plot variable (e.g. a shift `x_shifted = x - shift`) // produced visually broken curves because `binBoundaries` and @@ -289,6 +305,8 @@ TEST(RooHistPdf, BinnedLikelihoodInSimultaneous) // observable coordinate instead of the plot observable coordinate. TEST(RooHistPdf, ShiftedBinBoundaries) { + using Vec = std::vector; + RooRealVar x{"x", "x", 1000, 1500}; x.setBins(5); // bin edges in x: 1000, 1100, 1200, 1300, 1400, 1500 @@ -300,48 +318,39 @@ TEST(RooHistPdf, ShiftedBinBoundaries) } RooDataHist dh{"dh", "", x, &h}; - // Case 1: pdfObs = x - shift as a RooFormulaVar (no l-value inverse). - { - RooFormulaVar xShifted{"x_shifted", "x - shift", {x, shift}}; - RooHistPdf pdf{"pdf_f", "", xShifted, x, dh, 0}; - std::unique_ptr> boundaries{pdf.binBoundaries(x, 1000.0, 1600.0)}; - ASSERT_TRUE(boundaries); - // Hist bin boundary `b` satisfies `x - shift = b`, so plot_x = b + shift. - const std::vector expected{1025.0, 1125.0, 1225.0, 1325.0, 1425.0, 1525.0}; - ASSERT_EQ(boundaries->size(), expected.size()); - auto it = boundaries->begin(); - for (double e : expected) { - EXPECT_DOUBLE_EQ(*it++, e); - } - } - - // Case 2: pdfObs = RooLinearVar (slope=1, offset=shift -> xShifted = x + shift). - { - RooRealVar slope{"slope", "slope", 1.0}; - RooLinearVar xShifted{"x_shifted_lv", "", x, slope, shift}; - RooHistPdf pdf{"pdf_lv", "", RooArgList{xShifted}, RooArgList{x}, dh, 0}; - std::unique_ptr> boundaries{pdf.binBoundaries(x, 1000.0, 1500.0)}; - ASSERT_TRUE(boundaries); - // xShifted = x + 25 -> plot_x = b - 25; only values inside [1000, 1500] kept. - const std::vector expected{1075.0, 1175.0, 1275.0, 1375.0, 1475.0}; - ASSERT_EQ(boundaries->size(), expected.size()); - auto it = boundaries->begin(); - for (double e : expected) { - EXPECT_DOUBLE_EQ(*it++, e); - } - } + // pdfObs = x - shift as a RooFormulaVar (no l-value inverse). Hist bin + // boundary `b` satisfies `x - shift = b`, so plot_x = b + shift. + RooFormulaVar xShifted{"x_shifted", "x - shift", {x, shift}}; + RooHistPdf pdfF{"pdf_f", "", xShifted, x, dh, 0}; + EXPECT_EQ(toVector(pdfF.binBoundaries(x, 1000.0, 1600.0)), (Vec{1025, 1125, 1225, 1325, 1425, 1525})); - // Case 3: identity (no transformation) keeps the raw boundaries. - { - RooHistPdf pdf{"pdf_id", "", x, dh, 0}; - std::unique_ptr> boundaries{pdf.binBoundaries(x, 1000.0, 1500.0)}; - ASSERT_TRUE(boundaries); - const std::vector expected{1000.0, 1100.0, 1200.0, 1300.0, 1400.0, 1500.0}; - ASSERT_EQ(boundaries->size(), expected.size()); - auto it = boundaries->begin(); - for (double e : expected) { - EXPECT_DOUBLE_EQ(*it++, e); - } + // pdfObs = RooLinearVar x + shift -> plot_x = b - 25; only values inside [1000, 1500] kept. + RooRealVar slope{"slope", "slope", 1.0}; + RooLinearVar xShiftedLV{"x_shifted_lv", "", x, slope, shift}; + RooHistPdf pdfLV{"pdf_lv", "", RooArgList{xShiftedLV}, RooArgList{x}, dh, 0}; + EXPECT_EQ(toVector(pdfLV.binBoundaries(x, 1000.0, 1500.0)), (Vec{1075, 1175, 1275, 1375, 1475})); + + // Negative slope: plot_x = 2525 - b, still returned in increasing order. + RooFormulaVar xMirrored{"x_mirrored", "2525 - x", {x}}; + RooHistPdf pdfM{"pdf_m", "", xMirrored, x, dh, 0}; + EXPECT_EQ(toVector(pdfM.binBoundaries(x, 1000.0, 1500.0)), (Vec{1025, 1125, 1225, 1325, 1425})); + + // Identity (no transformation) keeps the raw boundaries. + RooHistPdf pdfId{"pdf_id", "", x, dh, 0}; + EXPECT_EQ(toVector(pdfId.binBoundaries(x, 1000.0, 1500.0)), (Vec{1000, 1100, 1200, 1300, 1400, 1500})); + + // RooHistFunc uses the same mapping. + RooHistFunc funcF{"func_f", "", RooArgList{xShifted}, RooArgList{x}, dh, 0}; + RooHistFunc funcLV{"func_lv", "", RooArgList{xShiftedLV}, RooArgList{x}, dh, 0}; + EXPECT_EQ(toVector(funcF.binBoundaries(x, 1000.0, 1500.0)), (Vec{1025, 1125, 1225, 1325, 1425})); + EXPECT_EQ(toVector(funcLV.binBoundaries(x, 1000.0, 1500.0)), (Vec{1075, 1175, 1275, 1375, 1475})); + + // The sampling hint has a point next to each plot limit and a pair of + // points around each boundary inside the plot range. + const Vec hint = toVector(pdfF.plotSamplingHint(x, 1000.0, 1500.0)); + ASSERT_EQ(hint.size(), 12u); + for (std::size_t i = 0; i < 5; ++i) { + EXPECT_NEAR(0.5 * (hint[2 * i + 1] + hint[2 * i + 2]), 1025.0 + 100.0 * i, 1e-9); } } From 727fde2f88ff1a0752cb9d7c0c7e1e8ec368dabf Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Fri, 20 Nov 2026 13:03:39 +0000 Subject: [PATCH 3/4] [RF] Don't throw when restoring the plot observable in RooHistPdf To map the bin boundaries, histogramBoundariesInPlotObs() sets the plot observable to probe the transformation and restored it with setVal(). That throws if an outer integral over a wider range had set the observable outside its default range. Restore it with assign() instead. Also fix the binBoundaries() documentation, which named RooCurve as a user (RooCurve uses plotSamplingHint()), and shorten the comments. Assisted-by: claude-code:claude-opus-5-5 --- roofit/roofitcore/src/RooHistPdf.cxx | 58 +++++++++-------------- roofit/roofitcore/test/testRooHistPdf.cxx | 20 ++++++++ 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/roofit/roofitcore/src/RooHistPdf.cxx b/roofit/roofitcore/src/RooHistPdf.cxx index 3731c59be6c4b..2f8438e4848eb 100644 --- a/roofit/roofitcore/src/RooHistPdf.cxx +++ b/roofit/roofitcore/src/RooHistPdf.cxx @@ -488,14 +488,8 @@ std::vector RooHistPdf::histogramBoundariesInPlotObs(RooDataHist const & { std::vector result; - // Locate the histogram observable that corresponds to the plot observable. - // These are the scenarios we handle: - // 1. The plot observable `obs` has the same name as one of the p.d.f. - // observables. That p.d.f. observable is the histogram observable - // itself (no transformation). - // 2. The p.d.f. observable is a non-trivial function of `obs` (e.g. a - // shift or scale). We have to invert that function to map the - // histogram bin boundaries back into the plot-observable coordinate. + // The histogram observable for `obs` belongs to the p.d.f. observable with + // the same name, or else to the first one that is a function of `obs`. RooAbsArg *histObs = nullptr; RooAbsArg *pdfObs = nullptr; bool useTransform = false; @@ -534,36 +528,32 @@ std::vector RooHistPdf::histogramBoundariesInPlotObs(RooDataHist const & return result; } - // We need to invert the transformation `pdfObs = T(obs)` to map the - // histogram bin boundaries (which live in the `pdfObs` coordinate) back into - // the plot-observable coordinate `obs`. - // - // We deliberately do *not* invert l-values (e.g. RooLinearVar) via their - // setVal() member, even though that would be exact: inverting a boundary - // often lands outside the range of `obs` (a bin edge at the border of the - // plot maps beyond it), and assigning an out-of-range value to a RooRealVar - // throws. Instead we invert arithmetically by linearly approximating `T` on - // the plot range and solving for `obs` at each histogram bin boundary. This - // never assigns out-of-range values, is exact for linear transformations - // (l-value or not), and can represent boundaries outside the plot range. + // Invert `pdfObs = T(obs)` with a linear approximation of `T` on the plot + // range, which is exact for linear transformations. Inverting with the + // setVal() of an l-value like RooLinearVar would throw for boundaries that + // map outside the range of `obs`. auto *pdfObsReal = static_cast(pdfObs); const double probeLo = std::max(xlo, obs.getMin()); const double probeHi = std::min(xhi, obs.getMax()); if (probeHi > probeLo) { - const double obsSave = obs.getVal(); - obs.setVal(probeLo); - const double plo = pdfObsReal->getVal(); - obs.setVal(probeHi); - const double phi = pdfObsReal->getVal(); - // Probe the midpoint to check whether the linear assumption actually holds. + // Restore `obs` with assign(), because setVal() throws if an outer + // integral has set it outside of its default range. + RooArgSet obsLeaves; + obs.leafNodeServerList(&obsLeaves); + RooArgSet obsSaved; + obsLeaves.snapshot(obsSaved, false); + auto evalAt = [&](double val) { + obs.setVal(val); + return pdfObsReal->getVal(); + }; const double probeMid = 0.5 * (probeLo + probeHi); - obs.setVal(probeMid); - const double pmid = pdfObsReal->getVal(); - obs.setVal(obsSave); + const double plo = evalAt(probeLo); + const double phi = evalAt(probeHi); + const double pmid = evalAt(probeMid); + obsLeaves.assign(obsSaved); const double slope = (phi - plo) / (probeHi - probeLo); - // Warn if the transformation is visibly non-linear: the boundaries are - // then placed only approximately and the plotted curve may look wrong. + // The midpoint reveals non-linear transformations. const double pmidLinear = plo + slope * (probeMid - probeLo); const double scale = std::max(std::abs(plo), std::abs(phi)); if (std::abs(pmid - pmidLinear) > 1e-7 * (scale > 0.0 ? scale : 1.0)) { @@ -623,10 +613,8 @@ std::list* RooHistPdf::plotSamplingHint(RooDataHist const& dataHist, } //////////////////////////////////////////////////////////////////////////////// -/// Return the histogram bin boundaries in the plot-observable coordinate. -/// The returned list is used by RooCurve's adaptive sampler to avoid stepping -/// over the discontinuities of a non-interpolated histogram, and by -/// RooBinIntegrator when integrating over this p.d.f. +/// Return the histogram bin boundaries in the plot-observable coordinate, +/// used for example by the RooBinIntegrator and the binned likelihood. std::list* RooHistPdf::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const { diff --git a/roofit/roofitcore/test/testRooHistPdf.cxx b/roofit/roofitcore/test/testRooHistPdf.cxx index a89fc36247c06..05441a51db233 100644 --- a/roofit/roofitcore/test/testRooHistPdf.cxx +++ b/roofit/roofitcore/test/testRooHistPdf.cxx @@ -354,6 +354,26 @@ TEST(RooHistPdf, ShiftedBinBoundaries) } } +// The plot observable can hold a value outside of its default range, e.g. set +// by an outer integral over a wider range. Mapping the bin boundaries must +// restore that value without throwing. +TEST(RooHistPdf, ShiftedBinBoundariesKeepOutOfRangeValue) +{ + RooRealVar x{"x", "x", 1000, 1500}; + x.setBins(5); + x.setRange("wide", 0, 2000); + RooRealVar shift{"shift", "shift", 25.0, -100, 100}; + + RooDataHist dh{"dh", "", x}; + RooFormulaVar xShifted{"x_shifted", "x - shift", {x, shift}}; + RooHistPdf pdf{"pdf", "", xShifted, x, dh, 0}; + + x.setVal(500, "wide"); + ASSERT_EQ(x.getVal(), 500.0); + EXPECT_EQ(toVector(pdf.binBoundaries(x, 1000.0, 1500.0)), (std::vector{1025, 1125, 1225, 1325, 1425})); + EXPECT_EQ(x.getVal(), 500.0); +} + // GitHub issue #13030: a non-linear transformation of the plot observable // cannot be inverted exactly by the (linear) boundary-finding algorithm. In // that case the boundaries are still returned, but only approximately, and the From 89283a20a3544a25ad7d3c1da734d4becf9312fc Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Fri, 20 Nov 2026 13:12:49 +0000 Subject: [PATCH 4/4] [RF] Get the RooBinIntegrator bin boundaries for each integral The RooBinIntegrator took the bin boundaries from the integrand only when it was created, but RooRealIntegral keeps reusing it when the parameters change. For a RooHistPdf or RooHistFunc of a shifted observable, the boundaries move with the shift, so the integral was only right at the shift of its first evaluation. Afterwards, it could count an edge bin twice. Get the boundaries for each integral instead. This costs one binBoundaries() call per integral, which in the RooFit tests and tutorials is less than 0.3 % of the run time. Assisted-by: claude-code:claude-opus-5-5 --- roofit/roofitcore/src/RooBinIntegrator.cxx | 31 +++++++++++++----- roofit/roofitcore/src/RooBinIntegrator.h | 1 + roofit/roofitcore/test/testRooHistPdf.cxx | 38 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/roofit/roofitcore/src/RooBinIntegrator.cxx b/roofit/roofitcore/src/RooBinIntegrator.cxx index 1a86b2899389c..a94d647ce8249 100644 --- a/roofit/roofitcore/src/RooBinIntegrator.cxx +++ b/roofit/roofitcore/src/RooBinIntegrator.cxx @@ -83,18 +83,35 @@ RooBinIntegrator::RooBinIntegrator(const RooAbsFunc &function, int numBins) for (UInt_t i=0 ; i<_function->getDimension() ; i++) { _xmin[i]= _function->getMinLimit(i); _xmax[i]= _function->getMaxLimit(i); + } + updateBinBoundaries(/*warnIfNoBinning=*/true); + checkLimits(); + +} + + +//////////////////////////////////////////////////////////////////////////////// +/// Retrieve the bin boundaries from the integrand. They can depend on the +/// parameters, e.g. for a RooHistPdf whose observable is a shifted variable, +/// so this is done for each integral. - // Retrieve bin configuration from integrand +void RooBinIntegrator::updateBinBoundaries(bool warnIfNoBinning) +{ + _binb.resize(_function->getDimension()); + for (UInt_t i=0 ; i<_function->getDimension() ; i++) { std::unique_ptr> tmp{ _function->binBoundaries(i) }; if (!tmp) { - oocoutW(nullptr,Integration) << "RooBinIntegrator::RooBinIntegrator WARNING: integrand provide no binning definition observable #" - << i << " substituting default binning of " << _numBins << " bins" << std::endl ; + if (warnIfNoBinning) { + oocoutW(nullptr,Integration) << "RooBinIntegrator::RooBinIntegrator WARNING: integrand provide no binning definition observable #" + << i << " substituting default binning of " << _numBins << " bins" << std::endl ; + } tmp = std::make_unique>( ); for (Int_t j=0 ; j<=_numBins ; j++) { tmp->push_back(_xmin[i]+j*(_xmax[i]-_xmin[i])/_numBins) ; } } - std::vector binb{tmp->begin(), tmp->end()}; + std::vector &binb = _binb[i]; + binb.assign(tmp->begin(), tmp->end()); // The bin boundaries provided by the integrand only include the boundaries // that lie strictly inside the integration range. To integrate correctly @@ -106,11 +123,7 @@ RooBinIntegrator::RooBinIntegrator(const RooAbsFunc &function, int numBins) if (binb.back() < _xmax[i]) { binb.push_back(_xmax[i]); } - _binb.emplace_back(std::move(binb)); - } - checkLimits(); - } @@ -177,6 +190,8 @@ double RooBinIntegrator::integral(const double *) assert(isValid()); if (_function->getDimension() < 1) return 0.; + updateBinBoundaries(/*warnIfNoBinning=*/false); + ROOT::Math::KahanSum sum; recursive_integration(0,1.,sum); diff --git a/roofit/roofitcore/src/RooBinIntegrator.h b/roofit/roofitcore/src/RooBinIntegrator.h index 83687f66a8422..a639dabd4cb00 100644 --- a/roofit/roofitcore/src/RooBinIntegrator.h +++ b/roofit/roofitcore/src/RooBinIntegrator.h @@ -54,6 +54,7 @@ class RooBinIntegrator : public RooAbsIntegrator { bool _useIntegrandLimits = false; ///< If true limits of function binding are used + void updateBinBoundaries(bool warnIfNoBinning); void recursive_integration(const UInt_t d, const double delta, ROOT::Math::KahanSum& sum); std::vector _x; /// integral{pdf.createIntegral(x)}; + for (double s : {25., -30., 0., 60.}) { + shift.setVal(s); + EXPECT_NEAR(integral->getVal(), expected(s), 1e-10) << "shift = " << s; + } +} + // GitHub issue #13030: a non-linear transformation of the plot observable // cannot be inverted exactly by the (linear) boundary-finding algorithm. In // that case the boundaries are still returned, but only approximately, and the