64 _observables(
"!observables",
"List of observables", this),
65 _weightVar{
"!weightVar",
"weightVar", this, weightVar, true, false}
68 _observables.add(*obs);
71 RooOffsetPdf(
const RooOffsetPdf &other,
const char *
name =
nullptr)
73 _observables(
"!servers", this, other._observables),
74 _weightVar{
"!weightVar", this, other._weightVar}
77 TObject *
clone(
const char *newname)
const override {
return new RooOffsetPdf(*
this, newname); }
81 std::span<const double> weights = dataMap.
at(_weightVar);
89 for (std::size_t i = 0; i < nEvents; ++i) {
91 var->setVal(dataMap.
at(var)[i]);
93 dataHist.
add(_observables, weights[weights.size() == 1 ? 0 : i]);
97 RooHistPdf pdf{
"offsetPdf",
"offsetPdf", _observables, dataHist};
98 for (std::size_t i = 0; i < nEvents; ++i) {
100 var->setVal(dataMap.
at(var)[i]);
107 double evaluate()
const override {
return 0.0; }
125 _pdf{
"pdf",
"pdf", this, pdf},
126 _weightVar{
"weightVar",
"weightVar", this, *dummyVar(weightVarName), true, false, true},
127 _weightSquaredVar{weightVarNameSumW2, weightVarNameSumW2, this, *dummyVar(
"weightSquardVar"), true, false, true},
128 _binnedL{pdf.getAttribute(
"BinnedLikelihoodActive")}
142 if (expectedEvents) {
144 std::make_unique<RooTemplateProxy<RooAbsReal>>(
"expectedEvents",
"expectedEvents",
this, *expectedEvents);
156 auto offsetPdf = std::make_unique<RooOffsetPdf>(
"_offset_pdf",
"_offset_pdf", obs, *
_weightVar);
157 _offsetPdf = std::make_unique<RooTemplateProxy<RooAbsPdf>>(
"offsetPdf",
"offsetPdf",
this, *offsetPdf);
164 _pdf{
"pdf", this, other._pdf},
165 _weightVar{
"weightVar", this, other._weightVar},
166 _weightSquaredVar{
"weightSquaredVar", this, other._weightSquaredVar},
167 _weightSquared{other._weightSquared},
168 _binnedL{other._binnedL},
169 _doOffset{other._doOffset},
170 _simCount{other._simCount},
171 _prefix{other._prefix},
182 if (!
_binw.empty()) {
186 if (observables.
size() != 1) {
187 throw std::runtime_error(
"BinnedPdf optimization only works with a 1D pdf.");
190 std::list<double> *boundaries = pdf.
binBoundaries(*var, var->getMin(), var->getMax());
191 std::list<double>::iterator biter = boundaries->begin();
192 _binw.resize(boundaries->size() - 1);
193 double lastBound = (*biter);
196 while (biter != boundaries->end()) {
197 _binw[ibin] = (*biter) - lastBound;
198 lastBound = (*biter);
210 const bool predsAreYields =
_binw.empty();
212 for (std::size_t i = 0; i < preds.size(); ++i) {
214 double eventWeight = weights[i];
217 double N = eventWeight;
218 double mu = preds[i];
219 if (!predsAreYields) {
223 if (mu <= 0 && N > 0) {
226 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N, (
unsigned long)i));
228 }
else if (std::abs(mu) < 1
e-10 && std::abs(
N) < 1
e-10) {
237 term -= -mu +
N +
N * (std::log(mu) - std::log(
N));
242 sumWeightKahanSum += eventWeight;
258 std::span<const double> weights = dataMap.
at(
_weightVar);
266 auto config = dataMap.
config(
this);
268 auto probas = dataMap.
at(
_pdf);
270 _sumWeight = weights.size() == 1 ? weights[0] * probas.size()
273 _sumWeight2 = weights.size() == 1 ? weightsSumW2[0] * probas.size()
280 if (nllOut.nLargeValues > 0) {
282 <<
") WARNING: top-level pdf has unexpectedly large values" << std::endl;
284 for (std::size_t i = 0; i < nllOut.nNonPositiveValues; ++i) {
287 for (std::size_t i = 0; i < nllOut.nNaNValues; ++i) {
321 (*_offsetPdf)->SetName((
_prefix +
"_offset_pdf").c_str());
378 std::string simCountStr = std::to_string(
static_cast<double>(
_simCount));
379 ctx.
addToCodeBody(resName +
" += " + weightSumName +
" * std::log(" + simCountStr +
");\n");
393 std::stringstream errorMsg;
394 errorMsg <<
"RooNLLVarNew::translate(): binned likelihood optimization is only supported when raw pdf "
395 "values can be interpreted as yields."
396 <<
" This is not the case for HistFactory models written with ROOT versions before 6.26.00";
397 coutE(InputArguments) << errorMsg.str() << std::endl;
398 throw std::runtime_error(errorMsg.str());
400 std::string muName = pdfName;
401 ctx.
addToCodeBody(
this, resName +
" += -1 * (-" + muName +
" + " + weight +
" * std::log(" + muName +
402 ") - TMath::LnGamma(" + weight +
"+ 1));\n");
404 ctx.
addToCodeBody(
this, resName +
" -= " + weight +
" * std::log(" + pdfName +
");\n");
409 ctx.
addToCodeBody(resName +
" += " + expected +
" - " + weightSumName +
" * std::log(" + expected +
");\n");
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
TObject * clone(const char *newname) const override
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.
void SetName(const char *name) override
Set the name of the TNamed.
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
Storage_t::size_type size() const
RooAbsArg * first() const
Abstract interface for all probability density functions.
virtual std::unique_ptr< RooAbsReal > createExpectedEventsFunc(const RooArgSet *nset) const
Returns an object that represents the expected number of events for a given normalization set,...
double extendedTerm(double sumEntries, double expected, double sumEntriesW2=0.0, bool doOffset=false) const
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.
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
void logEvalError(const char *message, const char *serverValueString=nullptr) const
Log evaluation error message.
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooConstVar represent a constant real-valued object.
The RooDataHist is a 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.
A class to maintain the context for squashing of RooFit models into code.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
void addToCodeBody(RooAbsArg const *klass, std::string const &in)
Adds the input string to the squashed code body.
std::string const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
void addToGlobalScope(std::string const &str)
Adds the given string to the string block that will be emitted at the top of the squashed function.
std::unique_ptr< LoopScope > beginLoop(RooAbsArg const *in)
Create a RAII scope for iterating over vector observables.
RooBatchCompute::Config config(RooAbsArg const *arg) const
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
RooHistPdf implements a propability density function sampled from a multidimensional histogram.
This is a simple class designed to produce the nll values needed by the fitter.
ROOT::Math::KahanSum< double > _offset
! Offset as KahanSum to avoid loss of precision
void enableOffsetting(bool) override
std::unique_ptr< RooTemplateProxy< RooAbsReal > > _expectedEvents
RooNLLVarNew(const char *name, const char *title, RooAbsPdf &pdf, RooArgSet const &observables, bool isExtended, RooFit::OffsetMode offsetMode)
Construct a RooNLLVarNew.
void translate(RooFit::Detail::CodeSquashContext &ctx) const override
This function defines a translation for each RooAbsReal based object that can be used to express the ...
void applyWeightSquared(bool flag) override
Toggles the weight square correction.
void getParametersHook(const RooArgSet *nset, RooArgSet *list, bool stripDisconnected) const override
void enableBinOffsetting(bool on=true)
std::unique_ptr< RooTemplateProxy< RooAbsPdf > > _offsetPdf
void computeBatch(double *output, size_t nOut, RooFit::Detail::DataMap const &) const override
Compute multiple negative logs of probabilities.
void setPrefix(std::string const &prefix)
Sets the prefix for the special variables of this NLL, like weights or bin volumes.
void fillBinWidthsFromPdfBoundaries(RooAbsReal const &pdf, RooArgSet const &observables)
void resetWeightVarNames()
std::vector< double > _binw
RooTemplateProxy< RooAbsPdf > _pdf
double computeBatchBinnedL(std::span< const double > preds, std::span< const double > weights) const
RooTemplateProxy< RooAbsReal > _weightVar
static constexpr const char * weightVarName
double finalizeResult(ROOT::Math::KahanSum< double > result, double weightSum) const
static constexpr const char * weightVarNameSumW2
RooTemplateProxy< RooAbsReal > _weightSquaredVar
RooRealVar represents a variable that can be changed from the outside.
const T & arg() const
Return reference to object held in proxy.
const char * GetName() const override
Returns name of object.
Mother of all ROOT objects.
double reduceSum(Config cfg, InputArr input, size_t n)
ReduceNLLOutput reduceNLL(Config cfg, std::span< const double > probas, std::span< const double > weights, std::span< const double > offsetProbas)
std::string makeValidVarName(std::string const &in)
OffsetMode
For setting the offset mode with the Offset() command argument to RooAbsPdf::fitTo()
void evaluate(typename Architecture_t::Tensor_t &A, EActivationFunction f)
Apply the given activation function to each value in the given tensor A.
Double_t LnGamma(Double_t z)
Computation of ln[gamma(z)] for all z.