53RooNLLVar::~RooNLLVar() {}
61 bool extended, RooAbsTestStatistic::Configuration
const& cfg) :
70 bool extended, RooAbsTestStatistic::Configuration
const &cfg)
71 : RooAbsOptTestStatistic(
name, title, pdf, indata, projDeps, cfg),
73 _binnedPdf(cfg.binnedL ? static_cast<
RooRealSumPdf *>(_funcClone) : nullptr)
82 _binnedPdf->setAttribute(
"BinnedLikelihoodActive") ;
85 _funcClone->getObservables(_dataClone->get(), obs);
90 std::unique_ptr<std::list<double>> boundaries{_binnedPdf->binBoundaries(*var,var->getMin(),var->getMax())};
91 auto biter = boundaries->begin() ;
92 _binw.reserve(boundaries->size()-1) ;
93 double lastBound = (*biter) ;
95 while (biter!=boundaries->end()) {
96 _binw.push_back((*biter) - lastBound);
97 lastBound = (*biter) ;
102 _skipZeroWeights =
false;
104 _skipZeroWeights =
true;
113RooNLLVar::RooNLLVar(
const RooNLLVar& other,
const char*
name) :
114 RooAbsOptTestStatistic(other,
name),
115 _extended(other._extended),
116 _weightSq(other._weightSq),
117 _offsetSaveW2(other._offsetSaveW2),
119 _binnedPdf{other._binnedPdf}
128 const RooArgSet& projDeps, RooAbsTestStatistic::Configuration
const& cfg) {
133 auto testStat =
new RooNLLVar(
name, title, thePdf, adata, projDeps, extendedPdf, cfg);
140void RooNLLVar::applyWeightSquared(
bool flag)
142 if (_gofOpMode==Slave) {
143 if (flag != _weightSq) {
145 std::swap(_offset, _offsetSaveW2);
148 }
else if ( _gofOpMode==MPMaster) {
149 for (
int i=0 ; i<_nCPU ; i++)
150 _mpfeArray[i]->applyNLLWeightSquared(flag);
151 }
else if ( _gofOpMode==SimMaster) {
152 for(
auto& gof : _gofArray)
153 static_cast<RooNLLVar&
>(*gof).applyWeightSquared(flag);
168double RooNLLVar::evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize)
const
175 double sumWeight{0.0};
177 auto * pdfClone =
static_cast<RooAbsPdf*
>(_funcClone);
183 for (
auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
187 double eventWeight = _dataClone->weight();
191 double N = eventWeight ;
192 double mu = _binnedPdf->getVal()*_binw[i] ;
198 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N,(
unsigned long)i)) ;
200 }
else if (std::abs(mu)<1
e-10 && std::abs(
N)<1
e-10) {
209 term -= -mu +
N +
N * (std::log(mu) - std::log(
N));
214 sumWeightKahanSum += eventWeight;
219 sumWeight = sumWeightKahanSum.
Sum();
223 std::tie(result, sumWeight) = computeScalar(stepSize, firstEvent, lastEvent);
226 if(_extended && _setNum==_extSet) {
227 result += pdfClone->extendedTerm(*_dataClone, _weightSq, _doBinOffset);
235 if (!_doBinOffset && _simCount>1) {
236 result += sumWeight * std::log(
static_cast<double>(_simCount));
243 _funcClone->wireAllCaches() ;
251 if (_offset.Sum() == 0 && _offset.Carry() == 0 && (result.
Sum() != 0 || result.
Carry() != 0)) {
252 coutI(Minimization) <<
"RooNLLVar::evaluatePartition(" << GetName() <<
") first = "<< firstEvent <<
" last = " << lastEvent <<
" Likelihood offset now set to " << result.
Sum() << std::endl ;
260 _evalCarry = result.
Carry();
261 return result.
Sum() ;
264RooNLLVar::ComputeResult RooNLLVar::computeScalar(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent)
const {
265 auto pdfClone =
static_cast<const RooAbsPdf*
>(_funcClone);
266 return computeScalarFunc(pdfClone, _dataClone, _normSet, _weightSq, stepSize, firstEvent, lastEvent, _offsetPdf.get());
269RooNLLVar::ComputeResult RooNLLVar::computeScalarFunc(
const RooAbsPdf *pdfClone,
RooAbsData *dataClone,
270 RooArgSet *normSet,
bool weightSq, std::size_t stepSize,
271 std::size_t firstEvent, std::size_t lastEvent,
RooAbsPdf const* offsetPdf)
277 for (
auto i=firstEvent; i<lastEvent; i+=stepSize) {
280 double weight = dataClone->
weight();
282 if (0. == weight * weight) continue ;
285 double logProba = pdfClone->
getLogVal(normSet);
288 logProba -= offsetPdf->
getLogVal(normSet);
291 const double term = -weight * logProba;
293 kahanWeight.
Add(weight);
295 packedNaN.accumulate(term);
298 if (packedNaN.getPayload() != 0.) {
303 return {kahanProb, kahanWeight.
Sum()};
306bool RooNLLVar::setDataSlave(
RooAbsData &indata,
bool cloneData,
bool ownNewData)
308 bool ret = RooAbsOptTestStatistic::setDataSlave(indata, cloneData, ownNewData);
311 enableBinOffsetting(_doBinOffset);
315void RooNLLVar::enableBinOffsetting(
bool flag)
325 bool needsResetting =
true;
327 switch (operMode()) {
330 for (
auto &gof : _gofArray) {
331 static_cast<RooNLLVar &
>(*gof).enableBinOffsetting(flag);
333 needsResetting =
false;
337 for (
int i = 0; i < _nCPU; ++i) {
338 static_cast<RooNLLVar &
>(_mpfeArray[i]->arg()).enableBinOffsetting(flag);
340 needsResetting =
false;
348 if (flag && !_offsetPdf) {
349 std::string
name = std::string{GetName()} +
"_offsetPdf";
350 std::unique_ptr<RooDataHist> dataTemplate;
351 if (
auto dh =
dynamic_cast<RooDataHist *
>(_dataClone)) {
352 dataTemplate = std::make_unique<RooDataHist>(*dh);
354 dataTemplate = std::unique_ptr<RooDataHist>(
static_cast<RooDataSet const &
>(*_dataClone).
binnedClone());
356 _offsetPdf = std::make_unique<RooHistPdf>(
name.c_str(),
name.c_str(), *_funcObsSet, std::move(dataTemplate));
357 _offsetPdf->setOperMode(ADirty);
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
void Add(T x)
Single-element accumulation. Will not vectorise.
Storage_t::size_type size() const
RooAbsArg * first() const
Abstract base class for binned and unbinned datasets.
virtual double weight() const =0
virtual const RooArgSet * get() const
virtual double weightSquared() const =0
Abstract interface for all probability density functions.
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...
Abstract base class for objects that represent a real value and implements functionality common to al...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Container class to hold N-dimensional binned data.
Container class to hold unbinned data.
RooFit::OwningPtr< RooDataHist > binnedClone(const char *newName=nullptr, const char *newTitle=nullptr) const
Return binned clone of this dataset.
Implements a PDF constructed from a sum of functions:
Variable that can be changed from the outside.
Double_t LnGamma(Double_t z)
Computation of ln[gamma(z)] for all z.
Little struct that can pack a float into the unused bits of the mantissa of a NaN double.