46#ifdef ROOFIT_CHECK_CACHED_VALUES
56 template<
class ...Args>
99 "RooNLLVar::RooNLLVar",
"ProjectedObservables",0,&_emptySet,
100 arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
101 makeRooAbsTestStatisticCfg(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))
105 pc.
defineInt(
"extended",
"Extended",0,
false) ;
106 pc.
defineInt(
"BatchMode",
"BatchMode", 0,
false);
153 auto biter = boundaries->begin() ;
154 _binw.reserve(boundaries->size()-1) ;
155 double lastBound = (*biter) ;
157 while (biter!=boundaries->end()) {
158 _binw.push_back((*biter) - lastBound);
159 lastBound = (*biter) ;
173 _extended(other._extended),
174 _batchEvaluations(other._batchEvaluations),
175 _weightSq(other._weightSq),
176 _offsetSaveW2(other._offsetSaveW2),
178 _binnedPdf{other._binnedPdf}
192 auto testStat =
new RooNLLVar(
name, title, thePdf, adata, projDeps, extendedPdf, cfg);
209 for (
int i=0 ; i<
_nCPU ; i++)
235 double sumWeight{0.0};
248 for (
auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
256 double N = eventWeight ;
263 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N,(
unsigned long)i)) ;
265 }
else if (std::abs(mu)<1
e-10 && std::abs(
N)<1
e-10) {
273 sumWeightKahanSum += eventWeight;
278 sumWeight = sumWeightKahanSum.Sum();
284#ifdef ROOFIT_CHECK_CACHED_VALUES
287 std::tie(resultScalar, sumWeightScalar) =
computeScalar(stepSize, firstEvent, lastEvent);
288 double carryScalar = resultScalar.
Carry();
290 constexpr bool alwaysPrint =
false;
292 if (alwaysPrint || std::abs(
result - resultScalar)/resultScalar > 5.E-15) {
293 std::cerr <<
"RooNLLVar: result is off\n\t" << std::setprecision(15) <<
result
294 <<
"\n\t" << resultScalar << std::endl;
297 if (alwaysPrint || std::abs(carry - carryScalar)/carryScalar > 500.) {
298 std::cerr <<
"RooNLLVar: carry is far off\n\t" << std::setprecision(15) << carry
299 <<
"\n\t" << carryScalar << std::endl;
302 if (alwaysPrint || std::abs(sumWeight - sumWeightScalar)/sumWeightScalar > 1.E-15) {
303 std::cerr <<
"RooNLLVar: sumWeight is off\n\t" << std::setprecision(15) << sumWeight
304 <<
"\n\t" << sumWeightScalar << std::endl;
338 coutI(Minimization) <<
"RooNLLVar::evaluatePartition(" <<
GetName() <<
") first = "<< firstEvent <<
" last = " << lastEvent <<
" Likelihood offset now set to " <<
result.Sum() << std::endl ;
366 std::unique_ptr<RooBatchCompute::RunContext> &evalData,
367 RooArgSet *normSet,
bool weightSq, std::size_t stepSize,
368 std::size_t firstEvent, std::size_t lastEvent)
370 const auto nEvents = lastEvent - firstEvent;
373 throw std::invalid_argument(std::string(
"Error in ") + __FILE__ +
": Step size for batch computations can only be 1.");
380 evalData = std::make_unique<RooBatchCompute::RunContext>();
383 evalData->spans = dataClone->
getBatches(firstEvent, nEvents);
387#ifdef ROOFIT_CHECK_CACHED_VALUES
389 for (std::size_t evtNo = firstEvent; evtNo < std::min(lastEvent, firstEvent + 10); ++evtNo) {
390 dataClone->
get(evtNo);
391 if (dataClone->
weight() == 0.)
397 }
catch (std::exception&
e) {
398 std::cerr << __FILE__ <<
":" << __LINE__ <<
" ERROR when checking batch computation for event " << evtNo <<
":\n"
399 <<
e.what() << std::endl;
404 }
catch (std::exception& e2) {
418 double uniformSingleEventWeight{0.0};
420 if (eventWeights.
empty()) {
422 sumOfWeights = nEvents * uniformSingleEventWeight;
423 for (std::size_t i = 0; i < results.size(); ++i) {
424 kahanProb.
AddIndexed(-uniformSingleEventWeight * results[i], i);
427 assert(results.size() == eventWeights.
size());
429 for (std::size_t i = 0; i < results.size(); ++i) {
430 const double weight = eventWeights[i];
431 kahanProb.
AddIndexed(-weight * results[i], i);
434 sumOfWeights = kahanWeight.
Sum();
437 if (std::isnan(kahanProb.
Sum())) {
442 for (std::size_t i = 0; i < results.size(); ++i) {
443 double weight = eventWeights.
empty() ? uniformSingleEventWeight : eventWeights[i];
448 if (std::isnan(results[i])) {
451 kahanSanitised += -weight * results[i];
459 return {kahanSanitised, sumOfWeights};
463 return {kahanProb, sumOfWeights};
474 RooArgSet *normSet,
bool weightSq, std::size_t stepSize,
475 std::size_t firstEvent, std::size_t lastEvent,
bool doBinOffset)
480 const double logSumW = std::log(dataClone->
sumEntries());
482 auto* dataHist = doBinOffset ?
static_cast<RooDataHist*
>(dataClone) :
nullptr;
484 for (
auto i=firstEvent; i<lastEvent; i+=stepSize) {
487 double weight = dataClone->
weight();
488 const double ni = weight;
490 if (0. == weight * weight) continue ;
493 double logProba = pdfClone->
getLogVal(normSet);
496 logProba -= std::log(ni) - std::log(dataHist->binVolume(i)) - logSumW;
499 const double term = -weight * logProba;
501 kahanWeight.
Add(weight);
511 return {kahanProb, kahanWeight.
Sum()};
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
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.
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
void setAttribute(const Text_t *name, bool value=true)
Set (default) or clear a named boolean attribute of this object.
Storage_t::size_type size() const
RooAbsArg * first() const
virtual void recalculateCache(const RooArgSet *, Int_t, Int_t, Int_t, bool)
RooAbsData is the common abstract base class for binned and unbinned datasets.
virtual double weight() const =0
virtual double sumEntries() const =0
Return effective number of entries in dataset, i.e., sum all weights.
virtual const RooArgSet * get() const
RooAbsDataStore * store()
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 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).
virtual double weightSquared() const =0
RooAbsOptTestStatistic is the abstract base class for test statistics objects that evaluate a functio...
RooAbsReal * _funcClone
Pointer to internal clone of input function.
RooArgSet * _normSet
Pointer to set with observables used for normalization.
RooAbsData * _dataClone
Pointer to internal clone if input data.
RooArgSet * _projDeps
Set of projected observable.
RooSpan< const double > getLogProbabilities(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
Compute the log-likelihoods for all events in the requested batch.
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
virtual double getLogVal(const RooArgSet *set=nullptr) 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 getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
void logEvalError(const char *message, const char *serverValueString=nullptr) const
Log evaluation error message.
RooAbsTestStatistic is the abstract base class for all test statistics.
Int_t _setNum
Partition number of this instance in parallel calculation mode.
double _evalCarry
! carry of Kahan sum in evaluatePartition
Int_t _nCPU
Number of processors to use in parallel calculation mode.
GOFOpMode _gofOpMode
Operation mode of test statistic instance.
Int_t _simCount
Total number of component p.d.f.s in RooSimultaneous (if any)
ROOT::Math::KahanSum< double > _offset
! Offset as KahanSum to avoid loss of precision
Int_t _extSet
! Number of designated set to calculated extended term
std::vector< std::unique_ptr< RooAbsTestStatistic > > _gofArray
! Array of sub-contexts representing part of the combined test statistic
pRooRealMPFE * _mpfeArray
! Array of parallel execution frond ends
bool _doOffset
Apply interval value offset to control numeric precision?
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 process(const RooCmdArg &arg)
Process given RooCmdArg.
Int_t getInt(const char *name, Int_t defaultValue=0)
Return integer property registered with name 'name'.
bool 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 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.
void allowUndefined(bool flag=true)
If flag is true the processing of unrecognized RooCmdArgs is not considered an error.
static std::string decodeStringOnTheFly(const char *callerID, const char *cmdArgName, Int_t intIdx, const char *defVal, Args_t &&...args)
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, Args_t &&...args)
Static decoder function allows to retrieve integer property from set of RooCmdArgs For use in base me...
The RooDataHist is a container class to hold N-dimensional binned data.
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
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, bool doBinOffset=false)
RooRealSumPdf * _binnedPdf
!
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
void applyWeightSquared(bool flag) override
Disables or enables the usage of squared weights.
std::vector< double > _binw
!
std::pair< ROOT::Math::KahanSum< double >, double > ComputeResult
RooAbsTestStatistic * create(const char *name, const char *title, RooAbsReal &pdf, RooAbsData &adata, const RooArgSet &projDeps, RooAbsTestStatistic::Configuration const &cfg) override
Create a test statistic using several properties of the current instance.
std::unique_ptr< RooBatchCompute::RunContext > _evalData
! Struct to store function evaluation workspaces.
ComputeResult computeBatched(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent) const
Compute probabilites of all data events.
bool _weightSq
Apply weights squared?
double evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize) const override
Calculate and return likelihood on subset of data.
The class RooRealSumPdf implements a PDF constructed from a sum of functions:
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
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::size_t size() const noexcept
constexpr bool empty() const noexcept
const char * GetName() const override
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
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.