69 _observables(
"!observables",
"List of observables", this),
70 _weightVar{
"!weightVar",
"weightVar", this, weightVar, true, false}
73 _observables.add(*obs);
76 RooOffsetPdf(
const RooOffsetPdf &other,
const char *
name =
nullptr)
78 _observables(
"!servers", this, other._observables),
79 _weightVar{
"!weightVar", this, other._weightVar}
82 TObject *
clone(
const char *newname)
const override {
return new RooOffsetPdf(*
this, newname); }
86 std::span<const double> weights = dataMap.
at(_weightVar);
94 for (std::size_t i = 0; i < nEvents; ++i) {
96 var->setVal(dataMap.
at(var)[i]);
98 dataHist.
add(_observables, weights[weights.size() == 1 ? 0 : i]);
102 RooHistPdf pdf{
"offsetPdf",
"offsetPdf", _observables, dataHist};
103 for (std::size_t i = 0; i < nEvents; ++i) {
105 var->setVal(dataMap.
at(var)[i]);
112 double evaluate()
const override {
return 0.0; }
130 _pdf{
"pdf",
"pdf", this, pdf},
131 _weightVar{
"weightVar",
"weightVar", this, *dummyVar(weightVarName), true, false, true},
132 _weightSquaredVar{weightVarNameSumW2, weightVarNameSumW2, this, *dummyVar(
"weightSquardVar"), true, false, true},
133 _binnedL{pdf.getAttribute(
"BinnedLikelihoodActive")}
146 if (expectedEvents) {
148 std::make_unique<RooTemplateProxy<RooAbsReal>>(
"expectedEvents",
"expectedEvents",
this, *expectedEvents);
158 auto offsetPdf = std::make_unique<RooOffsetPdf>(
"_offset_pdf",
"_offset_pdf", observables, *
_weightVar);
159 _offsetPdf = std::make_unique<RooTemplateProxy<RooAbsPdf>>(
"offsetPdf",
"offsetPdf",
this, *offsetPdf);
166 _pdf{
"pdf", this, other._pdf},
167 _weightVar{
"weightVar", this, other._weightVar},
168 _weightSquaredVar{
"weightSquaredVar", this, other._weightSquaredVar},
169 _weightSquared{other._weightSquared},
170 _binnedL{other._binnedL},
171 _doOffset{other._doOffset},
172 _simCount{other._simCount},
173 _prefix{other._prefix},
184 if (!
_binw.empty()) {
188 if (observables.
size() != 1) {
189 throw std::runtime_error(
"BinnedPdf optimization only works with a 1D pdf.");
192 std::list<double> *boundaries = pdf.
binBoundaries(*var, var->getMin(), var->getMax());
193 std::list<double>::iterator biter = boundaries->begin();
194 _binw.resize(boundaries->size() - 1);
195 double lastBound = (*biter);
198 while (biter != boundaries->end()) {
199 _binw[ibin] = (*biter) - lastBound;
200 lastBound = (*biter);
212 const bool predsAreYields =
_binw.empty();
214 for (std::size_t i = 0; i < preds.size(); ++i) {
216 double eventWeight = weights[i];
219 double N = eventWeight;
220 double mu = preds[i];
221 if (!predsAreYields) {
225 if (mu <= 0 && N > 0) {
228 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N, (
unsigned long)i));
230 }
else if (std::abs(mu) < 1
e-10 && std::abs(
N) < 1
e-10) {
238 sumWeightKahanSum += eventWeight;
254 std::span<const double> weights = dataMap.
at(
_weightVar);
262 auto config = dataMap.
config(
this);
264 auto probas = dataMap.
at(
_pdf);
266 _sumWeight = weights.size() == 1 ? weights[0] * probas.size()
269 _sumWeight2 = weights.size() == 1 ? weightsSumW2[0] * probas.size()
276 if (nllOut.nLargeValues > 0) {
278 <<
") WARNING: top-level pdf has unexpectedly large values" << std::endl;
280 for (std::size_t i = 0; i < nllOut.nNonPositiveValues; ++i) {
283 for (std::size_t i = 0; i < nllOut.nNaNValues; ++i) {
373 std::string simCountStr = std::to_string(
static_cast<double>(
_simCount));
374 ctx.
addToCodeBody(resName +
" += " + weightSumName +
" * std::log(" + simCountStr +
");\n");
388 std::stringstream errorMsg;
389 errorMsg <<
"RooNLLVarNew::translate(): binned likelihood optimization is only supported when raw pdf "
390 "values can be interpreted as yields."
391 <<
" This is not the case for HistFactory models written with ROOT versions before 6.26.00";
392 coutE(InputArguments) << errorMsg.str() << std::endl;
393 throw std::runtime_error(errorMsg.str());
395 std::string muName = pdfName;
396 ctx.
addToCodeBody(
this, resName +
" += -1 * (-" + muName +
" + " + weight +
" * std::log(" + muName +
397 ") - TMath::LnGamma(" + weight +
"+ 1));\n");
399 ctx.
addToCodeBody(
this, resName +
" -= " + weight +
" * std::log(" + pdfName +
");\n");
404 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...
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
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
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
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
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.
std::string makeValidVarName(TString in) const
Transform a string into a valid C++ variable name by replacing forbidden.
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)
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.