45#ifdef ROOFIT_CHECK_CACHED_VALUES
55 template<
class ...Args>
98 "RooNLLVar::RooNLLVar",
"ProjectedObservables",0,&_emptySet,
99 arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)),
100 makeRooAbsTestStatisticCfg(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))
104 pc.
defineInt(
"extended",
"Extended",0,
false) ;
105 pc.
defineInt(
"BatchMode",
"BatchMode", 0,
false);
152 auto biter = boundaries->begin() ;
153 _binw.reserve(boundaries->size()-1) ;
154 double lastBound = (*biter) ;
156 while (biter!=boundaries->end()) {
157 _binw.push_back((*biter) - lastBound);
158 lastBound = (*biter) ;
172 _extended(other._extended),
173 _batchEvaluations(other._batchEvaluations),
174 _weightSq(other._weightSq),
175 _offsetSaveW2(other._offsetSaveW2),
177 _binnedPdf{other._binnedPdf}
191 auto testStat =
new RooNLLVar(
name, title, thePdf, adata, projDeps, extendedPdf, cfg);
208 for (
int i=0 ; i<
_nCPU ; i++)
211 for (
int i=0 ; i<
_nGof ; i++)
234 double sumWeight{0.0};
247 for (
auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
257 double N = eventWeight ;
264 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N,(
unsigned long)i)) ;
266 }
else if (std::abs(mu)<1
e-10 && std::abs(
N)<1
e-10) {
274 sumWeightKahanSum += eventWeight;
279 sumWeight = sumWeightKahanSum.Sum();
284 std::tie(result, sumWeight) =
computeBatched(stepSize, firstEvent, lastEvent);
285#ifdef ROOFIT_CHECK_CACHED_VALUES
288 std::tie(resultScalar, sumWeightScalar) =
computeScalar(stepSize, firstEvent, lastEvent);
289 double carryScalar = resultScalar.
Carry();
291 constexpr bool alwaysPrint =
false;
293 if (alwaysPrint || std::abs(result - resultScalar)/resultScalar > 5.E-15) {
294 std::cerr <<
"RooNLLVar: result is off\n\t" << std::setprecision(15) << result
295 <<
"\n\t" << resultScalar << std::endl;
298 if (alwaysPrint || std::abs(carry - carryScalar)/carryScalar > 500.) {
299 std::cerr <<
"RooNLLVar: carry is far off\n\t" << std::setprecision(15) << carry
300 <<
"\n\t" << carryScalar << std::endl;
303 if (alwaysPrint || std::abs(sumWeight - sumWeightScalar)/sumWeightScalar > 1.E-15) {
304 std::cerr <<
"RooNLLVar: sumWeight is off\n\t" << std::setprecision(15) << sumWeight
305 <<
"\n\t" << sumWeightScalar << std::endl;
310 std::tie(result, sumWeight) =
computeScalar(stepSize, firstEvent, lastEvent);
323 result += sumWeight * std::log(
static_cast<double>(
_simCount));
338 if (
_offset==0 && result !=0 ) {
339 coutI(Minimization) <<
"RooNLLVar::evaluatePartition(" <<
GetName() <<
") first = "<< firstEvent <<
" last = " << lastEvent <<
" Likelihood offset now set to " << result << std::endl ;
348 return result.Sum() ;
367 std::unique_ptr<RooBatchCompute::RunContext> &evalData,
368 RooArgSet *normSet,
bool weightSq, std::size_t stepSize,
369 std::size_t firstEvent, std::size_t lastEvent)
371 const auto nEvents = lastEvent - firstEvent;
374 throw std::invalid_argument(std::string(
"Error in ") + __FILE__ +
": Step size for batch computations can only be 1.");
384 evalData->spans = dataClone->
getBatches(firstEvent, nEvents);
388#ifdef ROOFIT_CHECK_CACHED_VALUES
390 for (std::size_t evtNo = firstEvent; evtNo < std::min(lastEvent, firstEvent + 10); ++evtNo) {
391 dataClone->
get(evtNo);
392 if (dataClone->
weight() == 0.)
395 assert(dataClone->
valid());
399 }
catch (std::exception&
e) {
400 std::cerr << __FILE__ <<
":" << __LINE__ <<
" ERROR when checking batch computation for event " << evtNo <<
":\n"
401 <<
e.what() << std::endl;
406 }
catch (std::exception& e2) {
420 double uniformSingleEventWeight{0.0};
422 if (eventWeights.
empty()) {
424 sumOfWeights = nEvents * uniformSingleEventWeight;
425 for (std::size_t i = 0; i < results.size(); ++i) {
426 kahanProb.
AddIndexed(-uniformSingleEventWeight * results[i], i);
429 assert(results.size() == eventWeights.
size());
431 for (std::size_t i = 0; i < results.size(); ++i) {
432 const double weight = eventWeights[i];
433 kahanProb.
AddIndexed(-weight * results[i], i);
436 sumOfWeights = kahanWeight.
Sum();
439 if (std::isnan(kahanProb.
Sum())) {
444 for (std::size_t i = 0; i < results.size(); ++i) {
445 double weight = eventWeights.
empty() ? uniformSingleEventWeight : eventWeights[i];
450 if (std::isnan(results[i])) {
453 kahanSanitised += -weight * results[i];
461 return {kahanSanitised, sumOfWeights};
465 return {kahanProb, sumOfWeights};
476 RooArgSet *normSet,
bool weightSq, std::size_t stepSize,
477 std::size_t firstEvent, std::size_t lastEvent)
483 for (
auto i=firstEvent; i<lastEvent; i+=stepSize) {
486 if (!dataClone->
valid())
continue;
488 double eventWeight = dataClone->
weight();
489 if (0. == eventWeight * eventWeight) continue ;
492 const double term = -eventWeight * pdfClone->
getLogVal(normSet);
494 kahanWeight.
Add(eventWeight);
504 return {kahanProb, 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.
Storage_t::size_type size() const
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 Bool_t valid() const
RealSpans getBatches(std::size_t first=0, std::size_t len=std::numeric_limits< std::size_t >::max()) const
Write information to retrieve data columns into evalData.spans.
virtual Double_t weight() const =0
virtual Double_t weightSquared() const =0
virtual RooSpan< const double > getWeightBatch(std::size_t first, std::size_t len, bool sumW2=false) const =0
Return event weights of all events in range [first, first+len).
RooAbsOptTestStatistic is the abstract base class for test statistics objects that evaluate a functio...
RooSpan< const double > getLogProbabilities(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
Compute the log-likelihoods for all events in the requested batch.
Bool_t canBeExtended() const
If true, PDF can provide extended likelihood term.
virtual Double_t getLogVal(const RooArgSet *set=0) const
Return the log of the current value with given normalization An error message is printed if the argum...
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
ROOT::Math::KahanSum< double > _offset
Double_t _evalCarry
Offset as KahanSum to avoid loss of precision.
pRooRealMPFE * _mpfeArray
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.
Bool_t defineInt(const char *name, const char *argName, Int_t intNum, Int_t defValue=0)
Define integer property name 'name' mapped to integer in slot 'intNum' in RooCmdArg with name argName...
static std::string decodeStringOnTheFly(const char *callerID, const char *cmdArgName, Int_t intIdx, const char *defVal, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Static decoder function allows to retrieve string property from set of RooCmdArgs For use in base mem...
static Int_t decodeIntOnTheFly(const char *callerID, const char *cmdArgName, Int_t intIdx, Int_t defVal, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Static decoder function allows to retrieve integer property from set of RooCmdArgs For use in base me...
Int_t getInt(const char *name, Int_t defaultValue=0)
Return integer property registered with name 'name'.
void allowUndefined(Bool_t flag=kTRUE)
static double decodeDoubleOnTheFly(const char *callerID, const char *cmdArgName, int idx, double defVal, std::initializer_list< std::reference_wrapper< const RooCmdArg > > args)
Find a given double in a list of RooCmdArg.
Bool_t process(const RooCmdArg &arg)
Process given RooCmdArg.
Class RooNLLVar implements a -log(likelihood) calculation from a dataset and a PDF.
ComputeResult computeScalar(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent) const
void applyWeightSquared(bool flag)
Disables or enables the usage of squared weights.
RooRealSumPdf * _binnedPdf
static RooNLLVar::ComputeResult computeScalarFunc(const RooAbsPdf *pdfClone, RooAbsData *dataClone, RooArgSet *normSet, bool weightSq, std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent)
ROOT::Math::KahanSum< double > _offsetSaveW2
static RooNLLVar::ComputeResult computeBatchedFunc(const RooAbsPdf *pdfClone, RooAbsData *dataClone, std::unique_ptr< RooBatchCompute::RunContext > &evalData, RooArgSet *normSet, bool weightSq, std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent)
static RooArgSet _emptySet
std::pair< ROOT::Math::KahanSum< double >, double > ComputeResult
std::vector< Double_t > _binw
virtual RooAbsTestStatistic * create(const char *name, const char *title, RooAbsReal &pdf, RooAbsData &adata, const RooArgSet &projDeps, RooAbsTestStatistic::Configuration const &cfg)
Create a test statistic using several properties of the current instance.
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::unique_ptr< RooBatchCompute::RunContext > _evalData
ComputeResult computeBatched(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent) const
Compute probabilites of all data events.
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.
Double_t LnGamma(Double_t z)
Computation of ln[gamma(z)] for all z.
std::string rangeName
Stores the configuration parameters for RooAbsTestStatistic.
std::string addCoefRangeName
double integrateOverBinsPrecision
RooFit::MPSplit interleave
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.