Skip to content

Commit ba4e908

Browse files
committed
[RF] Fix RooBinIntegrator for ranges cutting through integrand bins
RooBinIntegrator builds its integration grid from the integrand's binBoundaries(xlo, xhi), which only returns the boundaries lying strictly inside the integration range. As a result, the integrator summed only the bins fully contained in the range and silently dropped the partial bins at the edges, because the integration limits were never added as the outermost bin boundaries. For example, integrating a 5-bin RooHistPdf (edges 0,2,4,6,8,10) over the sub-range [3,7] returned 2 instead of the correct 5.5. Fix this by prepending xmin and appending xmax as the outermost boundaries when they are not already present. The guard avoids zero-width bins when a range boundary coincides with a bin edge. Each resulting sub-bin then lies entirely within one original bin, so the midpoint rule stays exact for a piecewise-constant histogram. Add unit tests comparing the numeric RooBinIntegrator result against the analytic RooHistPdf integral for several range types, in both one and two dimensions. Closes #22858. 🤖 Done with the help of AI.
1 parent 648e366 commit ba4e908

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

roofit/roofitcore/src/RooBinIntegrator.cxx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,19 @@ RooBinIntegrator::RooBinIntegrator(const RooAbsFunc &function, int numBins)
9494
tmp->push_back(_xmin[i]+j*(_xmax[i]-_xmin[i])/_numBins) ;
9595
}
9696
}
97-
_binb.emplace_back(tmp->begin(), tmp->end());
97+
std::vector<double> binb{tmp->begin(), tmp->end()};
98+
99+
// The bin boundaries provided by the integrand only include the boundaries
100+
// that lie strictly inside the integration range. To integrate correctly
101+
// over partial bins at the edges of the range, the integration limits need
102+
// to be added as the outermost boundaries (if not already present).
103+
if (binb.empty() || binb.front() > _xmin[i]) {
104+
binb.insert(binb.begin(), _xmin[i]);
105+
}
106+
if (binb.back() < _xmax[i]) {
107+
binb.push_back(_xmax[i]);
108+
}
109+
_binb.emplace_back(std::move(binb));
98110

99111
}
100112
checkLimits();

roofit/roofitcore/test/testRooRealIntegral.cxx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,99 @@ TEST(RooRealIntegral, RooLinearVarModelIntegratedOverVariableClones)
389389
EXPECT_TRUE(static_cast<RooRealIntegral &>(*integral2).numIntRealVars().empty());
390390
}
391391

392+
// Make sure the RooBinIntegrator gives correct results when the integration
393+
// range boundaries lie inside the bins of the integrand, i.e. the range cuts
394+
// through bins. Previously, the RooBinIntegrator only summed the bins that were
395+
// fully contained in the range and dropped the partial bins at the edges,
396+
// because the integration limits were not added as the outermost bin
397+
// boundaries. Covers GitHub issue #22858.
398+
TEST(RooRealIntegral, RooBinIntegratorPartialRange)
399+
{
400+
using namespace RooFit;
401+
402+
RooHelpers::LocalChangeMsgLevel changeMsgLvl(RooFit::WARNING);
403+
404+
RooRealVar x{"x", "x", 0., 10.};
405+
x.setBins(5); // five bins of width 2: edges at 0, 2, 4, 6, 8, 10
406+
407+
const double heights[5] = {1.0, 3.0, 2.0, 4.0, 1.5};
408+
409+
RooDataHist dh{"dh", "", x};
410+
for (int i = 0; i < 5; ++i) {
411+
dh.set(i, heights[i], 0.0);
412+
}
413+
414+
// We compare the analytic RooHistPdf integral (which is known to be correct)
415+
// against the numeric one that uses the RooBinIntegrator, for various ranges
416+
// that cut through the bins in different ways.
417+
auto checkRange = [&](const char *name, double lo, double hi) {
418+
x.setRange(name, lo, hi);
419+
420+
RooHistPdf analytic{"analytic", "", x, dh, 0};
421+
RooHistPdf numeric{"numeric", "", x, dh, 0};
422+
numeric.forceNumInt(true);
423+
424+
std::unique_ptr<RooAbsReal> integAna{analytic.createIntegral(x, Range(name))};
425+
std::unique_ptr<RooAbsReal> integNum{numeric.createIntegral(x, Range(name))};
426+
427+
EXPECT_NEAR(integNum->getVal(), integAna->getVal(), 1e-12)
428+
<< "RooBinIntegrator disagrees with the analytic integral for range [" << lo << ", " << hi << "]";
429+
};
430+
431+
checkRange("full", 0.0, 10.0); // full range
432+
checkRange("partial", 3.0, 7.0); // both boundaries cut through bins (the original report)
433+
checkRange("aligned", 2.0, 8.0); // boundaries coincide with bin edges
434+
checkRange("singlebin", 3.0, 3.5); // range entirely inside a single bin
435+
checkRange("leftpart", 3.0, 10.0); // only the lower boundary cuts through a bin
436+
checkRange("rightpart", 0.0, 7.0); // only the upper boundary cuts through a bin
437+
438+
// Explicitly check the value from the original bug report as well.
439+
x.setRange("report", 3.0, 7.0);
440+
RooHistPdf numeric{"numeric", "", x, dh, 0};
441+
numeric.forceNumInt(true);
442+
std::unique_ptr<RooAbsReal> integNum{numeric.createIntegral(x, Range("report"))};
443+
const double binWidth = 2.0;
444+
const double expected = (heights[1] * 1.0 + heights[2] * 2.0 + heights[3] * 1.0) / binWidth; // = 5.5
445+
EXPECT_NEAR(integNum->getVal(), expected, 1e-12);
446+
}
447+
448+
// Like RooBinIntegratorPartialRange, but for a two-dimensional integrand to
449+
// make sure the fix for GitHub issue #22858 also holds for the N-dimensional
450+
// recursive integration path, where the range cuts through bins in every
451+
// dimension. This is the case most likely to regress silently.
452+
TEST(RooRealIntegral, RooBinIntegratorPartialRange2D)
453+
{
454+
using namespace RooFit;
455+
456+
RooHelpers::LocalChangeMsgLevel changeMsgLvl(RooFit::WARNING);
457+
458+
RooRealVar x{"x", "x", 0., 10.};
459+
RooRealVar y{"y", "y", 0., 6.};
460+
x.setBins(5); // edges at 0, 2, 4, 6, 8, 10
461+
y.setBins(3); // edges at 0, 2, 4, 6
462+
463+
RooDataHist dh{"dh", "", {x, y}};
464+
// Fill all 5x3 bins with distinct, non-trivial heights.
465+
for (int i = 0; i < dh.numEntries(); ++i) {
466+
dh.get(i);
467+
dh.set(i, 1.0 + 0.5 * i, 0.0);
468+
}
469+
470+
// The sub-ranges cut through the bins in both dimensions.
471+
x.setRange("sub", 3.0, 7.0); // cuts the x bins [2,4] and [6,8]
472+
y.setRange("sub", 1.0, 5.0); // cuts the y bins [0,2] and [4,6]
473+
474+
RooHistPdf analytic{"analytic", "", {x, y}, dh, 0};
475+
RooHistPdf numeric{"numeric", "", {x, y}, dh, 0};
476+
numeric.forceNumInt(true);
477+
478+
std::unique_ptr<RooAbsReal> integAna{analytic.createIntegral({x, y}, Range("sub"))};
479+
std::unique_ptr<RooAbsReal> integNum{numeric.createIntegral({x, y}, Range("sub"))};
480+
481+
EXPECT_NEAR(integNum->getVal(), integAna->getVal(), 1e-12)
482+
<< "RooBinIntegrator disagrees with the analytic integral for the 2D sub-range";
483+
}
484+
392485
// Make sure that RooFit realizes that Gaussian(x, mu, sigma(x)) needs to be
393486
// integrated analytically.
394487
// Covers GitHub issue #14320.

0 commit comments

Comments
 (0)