Skip to content

Commit 9a3caef

Browse files
committed
[RF] Don't override integral normalization set in RooNormalizedPdf
We should not override the normalization set for integrals of a RooNormalizedPdf, because sometimes, the requested normalization set is explicitly different from the internal normalization set of the normalized pdf. Closes #18718. (cherry picked from commit 075d432)
1 parent c8df502 commit 9a3caef

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

roofit/roofitcore/inc/RooFit/Detail/RooNormalizedPdf.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
#include <RooAbsPdf.h>
1717
#include <RooRealProxy.h>
1818

19-
namespace RooFit {
20-
namespace Detail {
19+
namespace RooFit::Detail {
2120

2221
class RooNormalizedPdf : public RooAbsPdf {
2322
public:
@@ -51,16 +50,15 @@ class RooNormalizedPdf : public RooAbsPdf {
5150

5251
bool forceAnalyticalInt(const RooAbsArg & /*dep*/) const override { return true; }
5352
/// Forward determination of analytical integration capabilities to input p.d.f
54-
Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet * /*normSet*/,
53+
Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet,
5554
const char *rangeName = nullptr) const override
5655
{
57-
return _pdf->getAnalyticalIntegralWN(allVars, analVars, &_normSet, rangeName);
56+
return _pdf->getAnalyticalIntegralWN(allVars, analVars, normSet ? normSet : &_normSet, rangeName);
5857
}
5958
/// Forward calculation of analytical integrals to input p.d.f
60-
double
61-
analyticalIntegralWN(Int_t code, const RooArgSet * /*normSet*/, const char *rangeName = nullptr) const override
59+
double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName = nullptr) const override
6260
{
63-
return _pdf->analyticalIntegralWN(code, &_normSet, rangeName);
61+
return _pdf->analyticalIntegralWN(code, normSet ? normSet : &_normSet, rangeName);
6462
}
6563

6664
ExtendMode extendMode() const override { return static_cast<RooAbsPdf &>(*_pdf).extendMode(); }
@@ -97,7 +95,6 @@ class RooNormalizedPdf : public RooAbsPdf {
9795
ClassDefOverride(RooFit::Detail::RooNormalizedPdf, 0);
9896
};
9997

100-
} // namespace Detail
101-
} // namespace RooFit
98+
} // namespace RooFit::Detail
10299

103100
#endif

roofit/roofitcore/src/RooNormalizedPdf.cxx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616

1717
#include <array>
1818

19-
2019
/**
2120
* \class RooNormalizedPdf
2221
*
2322
* A RooNormalizedPdf wraps a pdf divided by its integral for a given
2423
* normalization set into a new self-normalized pdf.
2524
*/
2625

27-
namespace RooFit {
28-
namespace Detail {
26+
namespace RooFit::Detail {
2927

3028
void RooNormalizedPdf::doEval(RooFit::EvalContext &ctx) const
3129
{
@@ -53,5 +51,4 @@ void RooNormalizedPdf::doEval(RooFit::EvalContext &ctx) const
5351
}
5452
}
5553

56-
} // namespace Detail
57-
} // namespace RooFit
54+
} // namespace RooFit::Detail

roofit/roofitcore/test/testRooSimultaneous.cxx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Authors: Jonas Rembser, CERN 06/2021
33

44
#include <Roo1DTable.h>
5+
#include <RooAddPdf.h>
56
#include <RooAddition.h>
67
#include <RooCategory.h>
78
#include <RooConstVar.h>
89
#include <RooDataSet.h>
10+
#include <RooExponential.h>
911
#include <RooFitResult.h>
1012
#include <RooGenericPdf.h>
1113
#include <RooHelpers.h>
@@ -511,6 +513,51 @@ TEST_P(TestStatisticTest, RooSimultaneousSingleChannelCrossCheckWithCondVar)
511513
<< "Inconsistency in RooSimultaneous wrapping with ConditionalObservables";
512514
}
513515

516+
/// GitHub issue #18718.
517+
/// Make sure that we can do a ranged fit on an extended RooAddPdf in a
518+
/// RooSimultaneous with the new CPU backend.
519+
TEST(RooSimultaneous, RangedExtendedRooAddPdf)
520+
{
521+
522+
const double nBkgA_nom = 9000;
523+
const double nBkgB_nom = 10000;
524+
525+
RooRealVar x("x", "Observable", 100, 150);
526+
x.setRange("fitRange", 100, 130);
527+
528+
RooRealVar nBkgA("nBkgA", "", nBkgA_nom, 0.8 * nBkgA_nom, 1.2 * nBkgA_nom);
529+
RooRealVar nBkgB("nBkgB", "", nBkgB_nom, 0.8 * nBkgB_nom, 1.2 * nBkgB_nom);
530+
531+
RooExponential expA("expA", "", x, RooFit::RooConst(-0.06));
532+
RooAddPdf modelA("modelA", "", {expA}, {nBkgA});
533+
534+
RooExponential expB("expB", "", x, RooFit::RooConst(-0.09));
535+
RooAddPdf modelB("modelB", "", {expB}, {nBkgB});
536+
537+
RooCategory runCat("runCat", "", {{"RunA", 0}, {"RunB", 1}});
538+
539+
RooSimultaneous simPdf("simPdf", "", {{"RunA", &modelA}, {"RunB", &modelB}}, runCat);
540+
541+
using namespace RooFit;
542+
543+
std::unique_ptr<RooDataSet> combData{simPdf.generate(RooArgSet(x, runCat), Extended())};
544+
545+
using Res = std::unique_ptr<RooFitResult>;
546+
547+
RooArgSet params;
548+
RooArgSet paramsSnap;
549+
simPdf.getParameters(combData->get(), params);
550+
params.snapshot(paramsSnap);
551+
552+
Res fitResult{simPdf.fitTo(*combData, Save(), Range("fitRange"), EvalBackend(RooFit::EvalBackend::Cpu()))};
553+
554+
params.assign(paramsSnap);
555+
556+
Res fitResultRef{simPdf.fitTo(*combData, Save(), Range("fitRange"), EvalBackend(RooFit::EvalBackend::Legacy()))};
557+
558+
EXPECT_TRUE(fitResult->isIdentical(*fitResultRef));
559+
}
560+
514561
/// GitHub issue #20383.
515562
/// Check that the the simultaneous pdf is normalized correctly when plotting
516563
/// with a projection dataset.

0 commit comments

Comments
 (0)