45#ifdef ROOFIT_CHECK_CACHED_VALUES
81 *(const
RooArgSet*)
RooCmdConfig::decodeObjOnTheFly(
"RooNLLVar::RooNLLVar",
"ProjectedObservables",0,&_emptySet
82 ,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
83 RooCmdConfig::decodeStringOnTheFly(
"RooNLLVar::RooNLLVar",
"RangeWithName",0,
"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9).c_str(),
84 RooCmdConfig::decodeStringOnTheFly(
"RooNLLVar::RooNLLVar",
"AddCoefRange",0,
"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9).c_str(),
85 RooCmdConfig::decodeIntOnTheFly(
"RooNLLVar::RooNLLVar",
"NumCPU",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
87 RooCmdConfig::decodeIntOnTheFly(
"RooNLLVar::RooNLLVar",
"Verbose",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
88 RooCmdConfig::decodeIntOnTheFly(
"RooNLLVar::RooNLLVar",
"SplitRange",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
89 RooCmdConfig::decodeIntOnTheFly(
"RooNLLVar::RooNLLVar",
"CloneData",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
90 RooCmdConfig::decodeDoubleOnTheFly(
"RooNLLVar::RooNLLVar",
"IntegrateBins", 0, -1., {arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9}))
94 pc.defineInt(
"extended",
"Extended",0,
kFALSE) ;
95 pc.defineInt(
"BatchMode",
"BatchMode", 0,
false);
97 pc.process(arg1) ; pc.process(arg2) ; pc.process(arg3) ;
98 pc.process(arg4) ; pc.process(arg5) ; pc.process(arg6) ;
99 pc.process(arg7) ; pc.process(arg8) ; pc.process(arg9) ;
120 Bool_t extended,
const char* rangeName,
const char* addCoefRangeName,
122 double integrateBinsPrecision) :
123 RooAbsOptTestStatistic(
name,title,pdf,indata,
RooArgSet(),rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData,
124 integrateBinsPrecision),
127 _first(
kTRUE), _offsetSaveW2(0.), _offsetCarrySaveW2(0.)
145 std::list<Double_t>::iterator biter = boundaries->begin() ;
146 _binw.resize(boundaries->size()-1) ;
150 while (biter!=boundaries->end()) {
151 _binw[ibin] = (*biter) - lastBound ;
152 lastBound = (*biter) ;
167 const RooArgSet& projDeps,
Bool_t extended,
const char* rangeName,
const char* addCoefRangeName,
169 double integrateBinsPrecision) :
170 RooAbsOptTestStatistic(
name,title,pdf,indata,projDeps,rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData,
171 integrateBinsPrecision),
174 _first(
kTRUE), _offsetSaveW2(0.), _offsetCarrySaveW2(0.)
189 std::list<Double_t>::iterator biter = boundaries->begin() ;
190 _binw.resize(boundaries->size()-1) ;
194 while (biter!=boundaries->end()) {
195 _binw[ibin] = (*biter) - lastBound ;
196 lastBound = (*biter) ;
211 _extended(other._extended),
212 _batchEvaluations(other._batchEvaluations),
213 _weightSq(other._weightSq),
214 _first(
kTRUE), _offsetSaveW2(other._offsetSaveW2),
215 _offsetCarrySaveW2(other._offsetCarrySaveW2),
225 const RooArgSet& projDeps,
const char* rangeName,
const char* addCoefRangeName,
231 projDeps, extendedPdf , rangeName, addCoefRangeName, nCPU, interleave, verbose, splitRange,
false, binnedL,
285 double result(0), carry(0), sumWeight(0);
297 double sumWeightCarry = 0.;
298 for (
auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
315 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N,(
unsigned long)i)) ;
317 }
else if (fabs(mu)<1
e-10 && fabs(
N)<1
e-10) {
328 Double_t y = eventWeight - sumWeightCarry;
330 sumWeightCarry = (t - sumWeight) -
y;
336 carry = (t - result) -
y;
345 std::tie(result, carry, sumWeight) =
computeBatched(stepSize, firstEvent, lastEvent);
346#ifdef ROOFIT_CHECK_CACHED_VALUES
348 double resultScalar, carryScalar, sumWeightScalar;
349 std::tie(resultScalar, carryScalar, sumWeightScalar) =
352 constexpr bool alwaysPrint =
false;
354 if (alwaysPrint || fabs(result - resultScalar)/resultScalar > 5.E-15) {
355 std::cerr <<
"RooNLLVar: result is off\n\t" << std::setprecision(15) << result
356 <<
"\n\t" << resultScalar << std::endl;
359 if (alwaysPrint || fabs(carry - carryScalar)/carryScalar > 500.) {
360 std::cerr <<
"RooNLLVar: carry is far off\n\t" << std::setprecision(15) << carry
361 <<
"\n\t" << carryScalar << std::endl;
364 if (alwaysPrint || fabs(sumWeight - sumWeightScalar)/sumWeightScalar > 1.E-15) {
365 std::cerr <<
"RooNLLVar: sumWeight is off\n\t" << std::setprecision(15) << sumWeight
366 <<
"\n\t" << sumWeightScalar << std::endl;
371 std::tie(result, carry, sumWeight) =
computeScalar(stepSize, firstEvent, lastEvent);
385 sumW2carry = (t - sumW2) -
y;
415 carry = (t - result) -
y;
420 carry = (t - result) -
y;
432 carry = (t - result) -
y;
448 if (
_offset==0 && result !=0 ) {
449 coutI(Minimization) <<
"RooNLLVar::evaluatePartition(" <<
GetName() <<
") first = "<< firstEvent <<
" last = " << lastEvent <<
" Likelihood offset now set to " << result << std::endl ;
457 carry = (t - result) -
y;
476 const auto nEvents = lastEvent - firstEvent;
479 throw std::invalid_argument(std::string(
"Error in ") + __FILE__ +
": Step size for batch computations can only be 1.");
495#ifdef ROOFIT_CHECK_CACHED_VALUES
497 for (std::size_t evtNo = firstEvent; evtNo < std::min(lastEvent, firstEvent + 10); ++evtNo) {
503 }
catch (std::exception&
e) {
504 std::cerr << __FILE__ <<
":" << __LINE__ <<
" ERROR when checking batch computation for event " << evtNo <<
":\n"
505 <<
e.what() << std::endl;
510 }
catch (std::exception& e2) {
522 const bool retrieveSquaredWeights =
_weightSq;
523 auto retrieveWeight = [&eventWeights, retrieveSquaredWeights](std::size_t i) {
524 return retrieveSquaredWeights ? eventWeights[i] * eventWeights[i] : eventWeights[i];
529 double uniformSingleEventWeight{0.0};
531 if (eventWeights.
empty()) {
533 sumOfWeights = nEvents * uniformSingleEventWeight;
534 for (std::size_t i = 0; i < results.size(); ++i) {
535 kahanProb.
AddIndexed(-uniformSingleEventWeight * results[i], i);
538 assert(results.size() == eventWeights.
size());
540 for (std::size_t i = 0; i < results.size(); ++i) {
541 const double weight = retrieveWeight(i);
542 kahanProb.
AddIndexed(-weight * results[i], i);
545 sumOfWeights = kahanWeight.
Sum();
548 if (std::isnan(kahanProb.
Sum())) {
553 for (std::size_t i = 0; i < results.size(); ++i) {
554 double weight = eventWeights.
empty() ? uniformSingleEventWeight : retrieveWeight(i);
559 if (std::isnan(results[i])) {
562 kahanSanitised += -weight * results[i];
568 return std::tuple<double, double, double>{nanPacker.
getNaNWithPayload(), 0., sumOfWeights};
570 return std::tuple<double, double, double>{kahanSanitised.
Sum(), kahanSanitised.
Carry(), sumOfWeights};
574 return std::tuple<double, double, double>{kahanProb.
Sum(), kahanProb.
Carry(), sumOfWeights};
578std::tuple<double, double, double>
RooNLLVar::computeScalar(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent)
const {
585 for (
auto i=firstEvent; i<lastEvent; i+=stepSize) {
591 if (0. == eventWeight * eventWeight) continue ;
594 const double term = -eventWeight * pdfClone->getLogVal(
_normSet);
596 kahanWeight.
Add(eventWeight);
603 return std::tuple<double, double, double>{packedNaN.
getNaNWithPayload(), 0., kahanWeight.
Sum()};
606 return std::tuple<double, double, double>{kahanProb.
Sum(), kahanProb.
Carry(), kahanWeight.
Sum()};
char * Form(const char *fmt,...)
static void checkBatchComputation(const RooAbsReal &theReal, const RooBatchCompute::RunContext &evalData, std::size_t evtNo, const RooArgSet *normSet=nullptr, double relAccuracy=1.E-13)
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
void AddIndexed(T input, std::size_t index)
Add input to the sum.
void Add(T x)
Single-element accumulation. Will not vectorise.
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Given a set of possible observables, return the observables that this PDF depends on.
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
RooAbsArg * first() const
virtual void recalculateCache(const RooArgSet *, Int_t, Int_t, Int_t, Bool_t)
RooAbsData is the common abstract base class for binned and unbinned datasets.
virtual const RooArgSet * get() const
RooAbsDataStore * store()
virtual void getBatches(RooBatchCompute::RunContext &evalData, std::size_t first=0, std::size_t len=std::numeric_limits< std::size_t >::max()) const =0
Retrieve batches of data for each real-valued variable in this dataset.
virtual RooSpan< const double > getWeightBatch(std::size_t first, std::size_t len) const =0
Return event weights of all events in range [first, first+len).
virtual Double_t sumEntries() const =0
Return effective number of entries in dataset, i.e., sum all weights.
virtual Bool_t valid() const
virtual Double_t weight() const =0
virtual Double_t weightSquared() const =0
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
RooAbsOptTestStatistic is the abstract base class for test statistics objects that evaluate a functio...
double _integrateBinsPrecision
Bool_t canBeExtended() const
If true, PDF can provide extended likelihood term.
virtual Double_t extendedTerm(Double_t observedEvents, const RooArgSet *nset=0) const
Return the extended likelihood term ( ) of this PDF for the given number of observed events.
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events from this p.d.f for use in extended likelihood calculations.
virtual Double_t getMax(const char *name=0) const
Get maximum of currently defined range.
virtual Double_t getMin(const char *name=0) const
Get miniminum of currently defined range.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
void logEvalError(const char *message, const char *serverValueString=0) const
Log evaluation error message.
RooAbsTestStatistic is the abstract base class for all test statistics.
Int_t _nGof
Number of designated set to calculated extended term.
Int_t _nCPU
GOF MP Split mode specified by component (when Auto is active)
pRooAbsTestStatistic * _gofArray
GOFOpMode _gofOpMode
Is object initialized
Double_t _evalCarry
avoids loss of precision
pRooRealMPFE * _mpfeArray
Double_t _offsetCarry
Offset.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooCmdArg is a named container for two doubles, two integers two object points and three string point...
Class RooCmdConfig is a configurable parser for RooCmdArg named arguments.
Class RooNLLVar implements a -log(likelihood) calculation from a dataset and a PDF.
void applyWeightSquared(Bool_t flag)
RooRealSumPdf * _binnedPdf
virtual RooAbsTestStatistic * create(const char *name, const char *title, RooAbsReal &pdf, RooAbsData &adata, const RooArgSet &projDeps, const char *rangeName, const char *addCoefRangeName=0, Int_t nCPU=1, RooFit::MPSplit interleave=RooFit::BulkPartition, Bool_t verbose=kTRUE, Bool_t splitRange=kFALSE, Bool_t binnedL=kFALSE)
Create a test statistic using several properties of the current instance.
static RooArgSet _emptySet
virtual ~RooNLLVar()
Destructor.
std::vector< Double_t > _binw
Double_t _offsetCarrySaveW2
virtual Double_t evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize) const
Calculate and return likelihood on subset of data.
std::tuple< double, double, double > computeBatched(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent) const
Compute probabilites of all data events.
std::tuple< double, double, double > computeScalar(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent) const
std::unique_ptr< RooBatchCompute::RunContext > _evalData
The class RooRealSumPdf implements a PDF constructed from a sum of functions:
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &, Double_t, Double_t) const
Retrieve bin boundaries if this distribution is binned in obs.
RooRealVar represents a variable that can be changed from the outside.
A simple container to hold a batch of data values.
constexpr std::span< T >::index_type size() const noexcept
constexpr bool empty() const noexcept
virtual const char * GetName() const
Returns name of object.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Double_t LnGamma(Double_t z)
Computation of ln[gamma(z)] for all z.
This struct enables passing computation data around between elements of a computation graph.
Little struct that can pack a float into the unused bits of the mantissa of a NaN double.
float getPayload() const
Retrieve packed float.
double getNaNWithPayload() const
Retrieve a NaN with the current float payload packed into the mantissa.
void accumulate(double val)
Accumulate a packed float from another NaN into this.