Skip to content

Commit 111908f

Browse files
committed
[RF] Move normalizeWithNaNPacking to RooFit::Detail namespace
This means that other RooFit classes can use this function in their implementation without having to rely on friend classes.
1 parent f6c7b98 commit 111908f

File tree

8 files changed

+56
-61
lines changed

8 files changed

+56
-61
lines changed

roofit/batchcompute/res/RooNaNPacker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef ROOFIT_BATCHCOMPUTE_ROONANPACKER_H
1414
#define ROOFIT_BATCHCOMPUTE_ROONANPACKER_H
1515

16-
#include <RooBatchComputeTypes.h>
16+
#include "./RooBatchComputeTypes.h"
1717

1818
#include <TError.h>
1919

roofit/roofitcore/inc/RooAbsPdf.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ class RooAbsPdf : public RooAbsReal {
296296
return RooFit::getUniqueId(normSet).value() == _normSetId;
297297
}
298298

299-
friend class NaNPackingNormalizer;
300-
301-
double normalizeWithNaNPacking(double rawVal, double normVal) const;
302-
303299
RooPlot *plotOn(RooPlot *frame, PlotOpt o) const override;
304300

305301
friend class RooMCStudy ;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ class RooNormalizedPdf : public RooAbsPdf {
8282
// still need it to support printing of the object.
8383
return getValV(nullptr);
8484
}
85-
double getValV(const RooArgSet * /*normSet*/) const override
86-
{
87-
return normalizeWithNaNPacking(_pdf->getVal(), _normIntegral->getVal());
88-
};
85+
double getValV(const RooArgSet * normSet) const override;
8986

9087
private:
9188
RooTemplateProxy<RooAbsPdf> _pdf;

roofit/roofitcore/res/RooFitImplHelpers.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
#ifndef RooFit_RooFitImplHelpers_h
1212
#define RooFit_RooFitImplHelpers_h
1313

14-
#include <RooMsgService.h>
1514
#include <RooAbsArg.h>
15+
#include <RooAbsPdf.h>
1616
#include <RooAbsReal.h>
17+
#include <RooMsgService.h>
18+
19+
#include "../../batchcompute/res/RooNaNPacker.h"
20+
21+
#include <TMath.h>
1722

1823
#include <sstream>
1924
#include <string>
@@ -104,6 +109,33 @@ void replaceAll(std::string &inOut, std::string_view what, std::string_view with
104109

105110
std::string makeSliceCutString(RooArgSet const &sliceDataSet);
106111

112+
// Inlined because this is called inside RooAbsPdf::getValV(), and therefore
113+
// performance critical.
114+
inline double normalizeWithNaNPacking(RooAbsPdf const &pdf, double rawVal, double normVal)
115+
{
116+
117+
if (normVal < 0. || (normVal == 0. && rawVal != 0)) {
118+
// Unreasonable normalisations. A zero integral can be tolerated if the function vanishes, though.
119+
const std::string msg = "p.d.f normalization integral is zero or negative: " + std::to_string(normVal);
120+
pdf.logEvalError(msg.c_str());
121+
return RooNaNPacker::packFloatIntoNaN(-normVal + (rawVal < 0. ? -rawVal : 0.));
122+
}
123+
124+
if (rawVal < 0.) {
125+
std::stringstream ss;
126+
ss << "p.d.f value is less than zero (" << rawVal << "), trying to recover";
127+
pdf.logEvalError(ss.str().c_str());
128+
return RooNaNPacker::packFloatIntoNaN(-rawVal);
129+
}
130+
131+
if (TMath::IsNaN(rawVal)) {
132+
pdf.logEvalError("p.d.f value is Not-a-Number");
133+
return rawVal;
134+
}
135+
136+
return (rawVal == 0. && normVal == 0.) ? 0. : rawVal / normVal;
137+
}
138+
107139
} // namespace RooFit::Detail
108140

109141
double toDouble(const char *s);

roofit/roofitcore/src/RooAbsPdf.cxx

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -286,34 +286,6 @@ RooAbsPdf::~RooAbsPdf()
286286
}
287287

288288

289-
double RooAbsPdf::normalizeWithNaNPacking(double rawVal, double normVal) const {
290-
291-
if (normVal < 0. || (normVal == 0. && rawVal != 0)) {
292-
//Unreasonable normalisations. A zero integral can be tolerated if the function vanishes, though.
293-
const std::string msg = "p.d.f normalization integral is zero or negative: " + std::to_string(normVal);
294-
logEvalError(msg.c_str());
295-
clearValueAndShapeDirty();
296-
return RooNaNPacker::packFloatIntoNaN(-normVal + (rawVal < 0. ? -rawVal : 0.));
297-
}
298-
299-
if (rawVal < 0.) {
300-
std::stringstream ss;
301-
ss << "p.d.f value is less than zero (" << rawVal << "), trying to recover";
302-
logEvalError(ss.str().c_str());
303-
clearValueAndShapeDirty();
304-
return RooNaNPacker::packFloatIntoNaN(-rawVal);
305-
}
306-
307-
if (TMath::IsNaN(rawVal)) {
308-
logEvalError("p.d.f value is Not-a-Number");
309-
clearValueAndShapeDirty();
310-
return rawVal;
311-
}
312-
313-
return (rawVal == 0. && normVal == 0.) ? 0. : rawVal / normVal;
314-
}
315-
316-
317289
////////////////////////////////////////////////////////////////////////////////
318290
/// Return current value, normalized by integrating over
319291
/// the observables in `nset`. If `nset` is 0, the unnormalized value
@@ -354,7 +326,7 @@ double RooAbsPdf::getValV(const RooArgSet* nset) const
354326
// Evaluate denominator
355327
const double normVal = _norm->getVal();
356328

357-
_value = normalizeWithNaNPacking(rawVal, normVal);
329+
_value = RooFit::Detail::normalizeWithNaNPacking(*this, rawVal, normVal);
358330

359331
clearValueAndShapeDirty();
360332
}

roofit/roofitcore/src/RooFFTConvPdf.cxx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
#include "RooGlobalFunc.h"
124124
#include "RooConstVar.h"
125125
#include "RooUniformBinning.h"
126+
#include "RooFitImplHelpers.h"
126127

127128
#include "TClass.h"
128129
#include "TComplex.h"
@@ -205,15 +206,6 @@ void RooFFTConvPdf_doFFT(int n, double *input1, double *input2, double *output)
205206

206207
#endif
207208

208-
class NaNPackingNormalizer {
209-
public:
210-
double getVal(RooAbsPdf const &pdf, double normVal)
211-
{
212-
double rawVal = pdf.getVal();
213-
return pdf.normalizeWithNaNPacking(rawVal, normVal);
214-
};
215-
};
216-
217209
using std::endl, std::string, std::ostream;
218210

219211

@@ -718,8 +710,10 @@ std::vector<double> RooFFTConvPdf::scanPdf(RooRealVar &obs, RooAbsPdf &pdf, doub
718710
while(zeroBin<0) zeroBin+= N2 ;
719711

720712
// To mimic exactly the normalization code in RooAbsPdf::getValV()
721-
NaNPackingNormalizer npn;
722-
713+
auto getVal = [](RooAbsPdf const &p, double normVal) {
714+
double rawVal = p.getVal();
715+
return RooFit::Detail::normalizeWithNaNPacking(p, rawVal, normVal);
716+
};
723717

724718
// First scan hist into temp array
725719
std::vector<double> tmp(N2);
@@ -730,7 +724,7 @@ std::vector<double> RooFFTConvPdf::scanPdf(RooRealVar &obs, RooAbsPdf &pdf, doub
730724
// Sample entire extended range (N2 samples)
731725
for (k=0 ; k<N2 ; k++) {
732726
histX->setBin(k) ;
733-
tmp[k] = npn.getVal(pdf, normVal);
727+
tmp[k] = getVal(pdf, normVal);
734728
}
735729
break ;
736730

@@ -739,16 +733,16 @@ std::vector<double> RooFFTConvPdf::scanPdf(RooRealVar &obs, RooAbsPdf &pdf, doub
739733
// bins with p.d.f. value at respective boundary
740734
{
741735
histX->setBin(0) ;
742-
double val = npn.getVal(pdf, normVal);
736+
double val = getVal(pdf, normVal);
743737
for (k=0 ; k<Nbuf ; k++) {
744738
tmp[k] = val ;
745739
}
746740
for (k=0 ; k<N ; k++) {
747741
histX->setBin(k) ;
748-
tmp[k+Nbuf] = npn.getVal(pdf, normVal);
742+
tmp[k+Nbuf] = getVal(pdf, normVal);
749743
}
750744
histX->setBin(N-1) ;
751-
val = npn.getVal(pdf, normVal);
745+
val = getVal(pdf, normVal);
752746
for (k=0 ; k<Nbuf ; k++) {
753747
tmp[N+Nbuf+k] = val ;
754748
}
@@ -760,13 +754,13 @@ std::vector<double> RooFFTConvPdf::scanPdf(RooRealVar &obs, RooAbsPdf &pdf, doub
760754
// bins with mirror image of sampled range
761755
for (k=0 ; k<N ; k++) {
762756
histX->setBin(k) ;
763-
tmp[k+Nbuf] = npn.getVal(pdf, normVal);
757+
tmp[k+Nbuf] = getVal(pdf, normVal);
764758
}
765759
for (k=1 ; k<=Nbuf ; k++) {
766760
histX->setBin(k) ;
767-
tmp[Nbuf-k] = npn.getVal(pdf, normVal);
761+
tmp[Nbuf-k] = getVal(pdf, normVal);
768762
histX->setBin(N-k) ;
769-
tmp[Nbuf+N+k-1] = npn.getVal(pdf, normVal);
763+
tmp[Nbuf+N+k-1] = getVal(pdf, normVal);
770764
}
771765
break ;
772766
}

roofit/roofitcore/src/RooFitImplHelpers.cxx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ RooAbsArg *cloneTreeWithSameParametersImpl(RooAbsArg const &arg, RooArgSet const
288288

289289
} // namespace RooHelpers
290290

291-
namespace RooFit {
292-
namespace Detail {
291+
namespace RooFit::Detail {
293292

294293
/// Transform a string into a valid C++ variable name by replacing forbidden
295294
/// characters with underscores.
@@ -335,8 +334,7 @@ std::string makeSliceCutString(RooArgSet const &sliceDataSet)
335334
return cutString.str();
336335
}
337336

338-
} // namespace Detail
339-
} // namespace RooFit
337+
} // namespace RooFit::Detail
340338

341339
namespace {
342340

roofit/roofitcore/src/RooNormalizedPdf.cxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "RooFit/Detail/RooNormalizedPdf.h"
1414

1515
#include "RooBatchCompute.h"
16+
#include "RooFitImplHelpers.h"
1617

1718
#include <array>
1819

@@ -51,4 +52,9 @@ void RooNormalizedPdf::doEval(RooFit::EvalContext &ctx) const
5152
}
5253
}
5354

55+
double RooNormalizedPdf::getValV(const RooArgSet * /*normSet*/) const
56+
{
57+
return normalizeWithNaNPacking(*_pdf, _pdf->getVal(), _normIntegral->getVal());
58+
}
59+
5460
} // namespace RooFit::Detail

0 commit comments

Comments
 (0)