52constexpr const char *RooNLLVarNew::weightVarName;
53constexpr const char *RooNLLVarNew::weightVarNameSumW2;
54constexpr const char *RooNLLVarNew::binVolumeVarName;
55constexpr const char *RooNLLVarNew::weightErrorLoVarName;
56constexpr const char *RooNLLVarNew::weightErrorHiVarName;
61std::unique_ptr<RooConstVar> dummyVar(
const char *
name)
63 return std::make_unique<RooConstVar>(
name,
name, 1.0);
71 _observables(
"!observables",
"List of observables", this),
72 _weightVar{
"!weightVar",
"weightVar", this, weightVar, true, false}
75 _observables.add(*obs);
78 RooOffsetPdf(
const RooOffsetPdf &other,
const char *
name =
nullptr)
80 _observables(
"!servers", this, other._observables),
81 _weightVar{
"!weightVar", this, other._weightVar}
84 TObject *clone(
const char *newname)
const override {
return new RooOffsetPdf(*
this, newname); }
88 std::span<double> output = ctx.
output();
89 std::size_t nEvents = output.size();
91 std::span<const double> weights = ctx.
at(_weightVar);
99 for (std::size_t i = 0; i < nEvents; ++i) {
101 var->setVal(ctx.
at(var)[i]);
103 dataHist.
add(_observables, weights[weights.size() == 1 ? 0 : i]);
107 RooHistPdf pdf{
"offsetPdf",
"offsetPdf", _observables, dataHist};
108 for (std::size_t i = 0; i < nEvents; ++i) {
110 var->setVal(ctx.
at(var)[i]);
112 output[i] = pdf.getVal(_observables);
117 double evaluate()
const override {
return 0.0; }
131 _func{
"func",
"func", this, func},
132 _weightVar{
"weightVar",
"weightVar", this, dummyVar(weightVarName)},
133 _weightSquaredVar{weightVarNameSumW2, weightVarNameSumW2, this, dummyVar(
"weightSquardVar")},
134 _statistic{cfg.statistic},
135 _chi2ErrorType{cfg.chi2ErrorType}
137 auto *pdf =
dynamic_cast<RooAbsPdf *
>(&func);
139 if (_statistic == Statistic::Chi2) {
143 setAttribute(
"Chi2EvaluationActive");
144 _funcMode = !pdf ? FuncMode::Function : (cfg.extended ? FuncMode::ExtendedPdf : FuncMode::Pdf);
146 _binnedL = pdf && pdf->getAttribute(
"BinnedLikelihoodActive");
157 const bool wantsExpectedEvents = pdf && ((_statistic == Statistic::NLL && cfg.extended && !_binnedL) ||
158 (_statistic == Statistic::Chi2 && _funcMode == FuncMode::ExtendedPdf));
159 if (wantsExpectedEvents) {
160 std::unique_ptr<RooAbsReal> expectedEvents = pdf->createExpectedEventsFunc(&obs);
161 if (expectedEvents) {
163 std::make_unique<RooTemplateProxy<RooAbsReal>>(
"expectedEvents",
"expectedEvents",
this, *expectedEvents);
164 addOwnedComponents(std::move(expectedEvents));
168 if (_statistic == Statistic::NLL) {
173 if (_binnedL && !pdf->getAttribute(
"BinnedLikelihoodActiveYields")) {
174 fillBinWidthsFromPdfBoundaries(*pdf, obs);
182 if (!_binnedL && _doBinOffset) {
183 auto offsetPdf = std::make_unique<RooOffsetPdf>(
"_offset_func",
"_offset_func", obs, *_weightVar);
184 _offsetPdf = std::make_unique<RooTemplateProxy<RooAbsPdf>>(
"offsetPdf",
"offsetPdf",
this, *offsetPdf);
185 addOwnedComponents(std::move(offsetPdf));
190 auto binVolumeDummy = std::make_unique<RooConstVar>(binVolumeVarName, binVolumeVarName, 1.0);
191 _binVolumes = std::make_unique<RooTemplateProxy<RooAbsReal>>(binVolumeVarName, binVolumeVarName,
this,
192 *binVolumeDummy,
true,
false);
193 addOwnedComponents(std::move(binVolumeDummy));
196 auto errLoDummy = std::make_unique<RooConstVar>(weightErrorLoVarName, weightErrorLoVarName, 1.0);
197 auto errHiDummy = std::make_unique<RooConstVar>(weightErrorHiVarName, weightErrorHiVarName, 1.0);
198 _weightErrLo = std::make_unique<RooTemplateProxy<RooAbsReal>>(weightErrorLoVarName, weightErrorLoVarName,
this,
199 *errLoDummy,
true,
false);
200 _weightErrHi = std::make_unique<RooTemplateProxy<RooAbsReal>>(weightErrorHiVarName, weightErrorHiVarName,
this,
201 *errHiDummy,
true,
false);
202 addOwnedComponents(std::move(errLoDummy));
203 addOwnedComponents(std::move(errHiDummy));
207 resetWeightVarNames();
210RooNLLVarNew::RooNLLVarNew(
const RooNLLVarNew &other,
const char *
name)
212 _func{
"func", this, other._func},
213 _weightVar{
"weightVar", this, other._weightVar},
214 _weightSquaredVar{
"weightSquaredVar", this, other._weightSquaredVar},
215 _weightSquared{other._weightSquared},
216 _binnedL{other._binnedL},
217 _doOffset{other._doOffset},
218 _doBinOffset{other._doBinOffset},
219 _statistic{other._statistic},
220 _funcMode{other._funcMode},
221 _chi2ErrorType{other._chi2ErrorType},
222 _simCount{other._simCount},
223 _logSimCount{other._logSimCount},
224 _prefix{other._prefix},
227 if (other._expectedEvents) {
228 _expectedEvents = std::make_unique<RooTemplateProxy<RooAbsReal>>(
"expectedEvents",
this, *other._expectedEvents);
230 if (other._binVolumes) {
231 _binVolumes = std::make_unique<RooTemplateProxy<RooAbsReal>>(binVolumeVarName,
this, *other._binVolumes);
233 if (other._weightErrLo) {
234 _weightErrLo = std::make_unique<RooTemplateProxy<RooAbsReal>>(weightErrorLoVarName,
this, *other._weightErrLo);
236 if (other._weightErrHi) {
237 _weightErrHi = std::make_unique<RooTemplateProxy<RooAbsReal>>(weightErrorHiVarName,
this, *other._weightErrHi);
241void RooNLLVarNew::fillBinWidthsFromPdfBoundaries(
RooAbsReal const &pdf,
RooArgSet const &observables)
244 if (!_binw.empty()) {
248 if (observables.
size() != 1) {
249 throw std::runtime_error(
"BinnedPdf optimization only works with a 1D pdf.");
252 std::unique_ptr<std::list<double>> boundaries{pdf.
binBoundaries(*var, var->getMin(), var->getMax())};
254 throw std::runtime_error(std::string(
"BinnedPdf optimization requires the pdf \"") + pdf.
GetName() +
255 "\" to provide bin boundaries for \"" + var->GetName() +
"\".");
257 std::list<double>::iterator biter = boundaries->begin();
258 _binw.resize(boundaries->size() - 1);
259 double lastBound = (*biter);
262 while (biter != boundaries->end()) {
263 _binw[ibin] = (*biter) - lastBound;
264 lastBound = (*biter);
272 std::span<const double> weights)
const
277 const bool predsAreYields = _binw.empty();
279 for (std::size_t i = 0; i < preds.size(); ++i) {
282 double N = weights[i];
283 double mu = preds[i];
284 if (!predsAreYields) {
288 if (mu <= 0 && N > 0) {
290 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N, (
unsigned long)i));
293 sumWeightKahanSum +=
N;
297 finalizeResult(ctx,
result, sumWeightKahanSum.Sum());
305double RooNLLVarNew::sumOfWeights(
RooFit::EvalContext &ctx, std::span<const double> weights,
bool squared)
const
307 std::size_t &generation = squared ? _sumWeight2Gen : _sumWeightGen;
308 double &cache = squared ? _sumWeight2Cache : _sumWeightCache;
316void RooNLLVarNew::doEvalChi2(
RooFit::EvalContext &ctx, std::span<const double> preds, std::span<const double> weights,
317 std::span<const double> weightsSumW2)
const
326 std::span<const double> binVol = ctx.
at(*_binVolumes);
327 std::span<const double> errLo = _weightErrLo ? ctx.
at(*_weightErrLo) : std::span<const double>{};
328 std::span<const double> errHi = _weightErrHi ? ctx.
at(*_weightErrHi) : std::span<const double>{};
330 const double sumWeight = sumOfWeights(ctx, weights,
false);
332 double normFactor = 1.0;
334 case FuncMode::Pdf: normFactor = sumWeight;
break;
335 case FuncMode::ExtendedPdf: normFactor = ctx.
at(*_expectedEvents)[0];
break;
336 case FuncMode::Function: normFactor = 1.0;
break;
342 for (std::size_t i = 0; i < preds.size(); ++i) {
343 const double N = weights[i];
344 const double mu = preds[i] * normFactor * binVol[i];
345 const double diff = mu -
N;
348 switch (_chi2ErrorType) {
352 const double err = diff > 0 ? errHi[i] : errLo[i];
356 default: sigma2 = mu;
break;
360 if (sigma2 == 0.0 &&
N == 0.0 && mu == 0.0) {
364 logEvalError(
Form(
"chi2 bin %lu has non-positive error; term replaced with NaN", (
unsigned long)i));
365 result += std::numeric_limits<double>::quiet_NaN();
369 result += diff * diff / sigma2;
370 sumWeightKahanSum +=
N;
373 finalizeResult(ctx,
result, sumWeightKahanSum.Sum());
377bool RooNLLVarNew::canUseLogarithmOf(
RooAbsArg const &server,
bool )
const
379 return _statistic == Statistic::NLL && !_binnedL && &server == &*_func;
384 std::span<const double> weights = ctx.
at(_weightVar);
385 std::span<const double> weightsSumW2 = ctx.
at(_weightSquaredVar);
387 if (_statistic == Statistic::Chi2) {
388 return doEvalChi2(ctx, ctx.
at(&*_func), weights, weightsSumW2);
392 return doEvalBinnedL(ctx, ctx.
at(&*_func), _weightSquared ? weightsSumW2 : weights);
395 auto config = ctx.
config(
this);
399 double sumWeight = sumOfWeights(ctx, weights,
false);
400 double sumWeight2 = 0.;
401 if (_expectedEvents && _weightSquared) {
402 sumWeight2 = sumOfWeights(ctx, weightsSumW2,
true);
406 _doBinOffset ? ctx.at(*_offsetPdf) : std::span<const
double>{},
409 if (nllOut.nInfiniteValues > 0) {
410 oocoutW(&*_func, Eval) <<
"RooAbsPdf::getLogVal(" << _func->GetName()
411 <<
") WARNING: top-level pdf has some infinite values" << std::endl;
413 for (std::size_t i = 0; i < nllOut.nNonPositiveValues; ++i) {
414 _func->logEvalError(
"getLogVal() top-level p.d.f not greater than zero");
416 for (std::size_t i = 0; i < nllOut.nNaNValues; ++i) {
417 _func->logEvalError(
"getLogVal() top-level p.d.f evaluates to NaN");
420 if (_expectedEvents) {
423 std::span<const double> expected = ctx.
at(*_expectedEvents);
424 nllOut.nllSum += pdf.extendedTerm(sumWeight, expected[0], _weightSquared ? sumWeight2 : 0.0, _doBinOffset);
427 finalizeResult(ctx, {nllOut.nllSum, nllOut.nllSumCarry}, sumWeight);
434void RooNLLVarNew::setPrefix(std::string
const &prefix)
438 resetWeightVarNames();
441void RooNLLVarNew::resetWeightVarNames()
443 _weightVar->SetName((_prefix + weightVarName).c_str());
444 _weightSquaredVar->SetName((_prefix + weightVarNameSumW2).c_str());
446 (*_offsetPdf)->SetName((_prefix +
"_offset_func").c_str());
449 (*_binVolumes)->SetName((_prefix + binVolumeVarName).c_str());
452 (*_weightErrLo)->SetName((_prefix + weightErrorLoVarName).c_str());
455 (*_weightErrHi)->SetName((_prefix + weightErrorHiVarName).c_str());
461void RooNLLVarNew::applyWeightSquared(
bool flag)
463 if (_statistic == Statistic::Chi2) {
465 coutW(Fitting) <<
"RooNLLVarNew::applyWeightSquared(" <<
GetName()
466 <<
") has no effect on a chi-squared evaluator; ignoring." << std::endl;
470 _weightSquared = flag;
473void RooNLLVarNew::enableOffsetting(
bool flag)
485 if (_statistic == Statistic::NLL && !_doBinOffset && _simCount > 1) {
486 result += weightSum * _logSimCount;
493 if (_offset.Sum() == 0 && _offset.Carry() == 0 && (
result.Sum() != 0 ||
result.Carry() != 0)) {
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
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.
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Common abstract base class for objects that represent a value and a "shape" in RooFit.
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.
Storage_t::size_type size() const
RooAbsArg * first() const
Abstract interface for all probability density functions.
Abstract base class for objects that represent a real value and implements functionality common to al...
virtual std::list< double > * binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const
Retrieve bin boundaries if this distribution is binned in obs.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Container class to hold N-dimensional binned data.
void add(const RooArgSet &row, double wgt=1.0) override
Add wgt to the bin content enclosed by the coordinates passed in row.
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
bool outputsLogarithm(RooAbsArg const *arg) const
Whether the output of the given node is the logarithm of its value.
RooBatchCompute::Config config(RooAbsArg const *arg) const
std::size_t inputGeneration() const
A counter that is incremented every time new input data is loaded into the evaluation context.
void setOutputWithOffset(RooAbsArg const *arg, ROOT::Math::KahanSum< double > val, ROOT::Math::KahanSum< double > const &offset)
Sets the output value with an offset.
A probability density function sampled from a multidimensional histogram.
Variable that can be changed from the outside.
const char * GetName() const override
Returns name of object.
Mother of all ROOT objects.
ReduceNLLOutput reduceNLL(Config cfg, std::span< const double > probas, std::span< const double > weights, std::span< const double > offsetProbas, bool logProbas=false)
double reduceSum(Config cfg, InputArr input, size_t n)
double nll(double pdf, double weight, int doBinOffset)
The negative log-likelihood of one bin in a binned fit.
void evaluate(typename Architecture_t::Tensor_t &A, EActivationFunction f)
Apply the given activation function to each value in the given tensor A.
void probas(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
std::string GetName(TCppScope_t)