49constexpr const char *RooNLLVarNew::weightVarName;
50constexpr const char *RooNLLVarNew::weightVarNameSumW2;
54std::unique_ptr<RooAbsReal>
55createFractionInRange(
RooAbsPdf const &pdf,
RooArgSet const &observables, std::string
const &rangeNames)
57 return std::unique_ptr<RooAbsReal>{
62double kahanSum(Input
const &input)
71 return observablesInPdf;
83 bool isExtended, std::string
const &rangeName,
bool doOffset)
84 :
RooAbsReal(
name, title), _pdf{
"pdf",
"pdf", this, pdf}, _observables{getObservablesInPdf(pdf, observables)},
85 _isExtended{isExtended}, _doOffset{doOffset},
86 _weightVar{
"weightVar",
"weightVar", this, *new
RooRealVar(weightVarName, weightVarName, 1.0), true, false, true},
87 _weightSquaredVar{weightVarNameSumW2,
90 *new
RooRealVar(
"weightSquardVar",
"weightSquaredVar", 1.0),
100 }
else if (pdf.IsA()->
InheritsFrom(RooProdPdf::Class())) {
103 if (component->getAttribute(
"BinnedLikelihood") && component->IsA()->InheritsFrom(RooRealSumPdf::Class())) {
104 actualPdf =
static_cast<RooAbsPdf *
>(component);
110 if (actualPdf != &pdf) {
116 throw std::runtime_error(
"BinnedPdf optimization only works with a 1D pdf.");
119 std::list<double> *boundaries = actualPdf->
binBoundaries(*var, var->getMin(), var->getMax());
120 std::list<double>::iterator biter = boundaries->begin();
121 _binw.resize(boundaries->size() - 1);
122 double lastBound = (*biter);
125 while (biter != boundaries->end()) {
126 _binw[ibin] = (*biter) - lastBound;
127 lastBound = (*biter);
134 if (!rangeName.empty()) {
135 auto term = createFractionInRange(*actualPdf,
_observables, rangeName);
137 std::make_unique<RooTemplateProxy<RooAbsReal>>(
"_fractionInRange",
"_fractionInRange",
this, *term);
145 :
RooAbsReal(other,
name), _pdf{
"pdf", this, other._pdf}, _observables{other._observables},
146 _isExtended{other._isExtended}, _weightSquared{other._weightSquared}, _binnedL{other._binnedL},
147 _prefix{other._prefix}, _weightVar{
"weightVar", this, other._weightVar}, _weightSquaredVar{
"weightSquaredVar",
149 other._weightSquaredVar}
153 std::make_unique<RooTemplateProxy<RooAbsReal>>(
"_fractionInRange",
this, *other.
_fractionInRange);
166 std::size_t nEvents = dataMap.
at(
_pdf).size();
175 auto preds = dataMap.
at(&*
_pdf);
177 for (std::size_t i = 0; i < nEvents; ++i) {
179 double eventWeight = weightSpan[i];
182 double N = eventWeight;
183 double mu = preds[i] *
_binw[i];
185 if (mu <= 0 && N > 0) {
188 logEvalError(
Form(
"Observed %f events in bin %lu with zero event yield",
N, (
unsigned long)i));
190 }
else if (std::abs(mu) < 1
e-10 && std::abs(
N) < 1
e-10) {
198 sumWeightKahanSum += eventWeight;
202 result += sumWeightKahanSum.Sum();
208 if (
_offset == 0 && result != 0) {
221 auto probas = dataMap.
at(
_pdf);
227 _sumWeight = weights.size() == 1 ? weights[0] * nEvents : kahanSum(weights);
230 _sumWeight2 = weights.size() == 1 ? weightsSumW2[0] * nEvents : kahanSum(weightsSumW2);
232 double sumCorrectionTerm = 0;
235 if (fractionInRangeSpan.size() == 1) {
238 if (weightSpan.size() == 1) {
239 double fractionInRangeLogSum = 0.0;
240 for (std::size_t i = 0; i < fractionInRangeSpan.size(); ++i) {
241 fractionInRangeLogSum += std::log(fractionInRangeSpan[i]);
243 sumCorrectionTerm = weightSpan[0] * fractionInRangeLogSum;
247 sumCorrectionTerm = 0.0;
248 for (std::size_t i = 0; i < nEvents; ++i) {
249 sumCorrectionTerm += weightSpan[i] * std::log(fractionInRangeSpan[i]);
258 for (std::size_t i = 0; i < nEvents; ++i) {
260 double eventWeight = weightSpan.size() > 1 ? weightSpan[i] : weightSpan[0];
261 if (0. == eventWeight * eventWeight)
284 kahanProb += sumCorrectionTerm;
291 if (
_offset == 0 && kahanProb != 0) {
326 for (
auto *arg : static_range_cast<RooRealVar *>(obsClones)) {
327 arg->setAttribute((std::string(
"ORIGNAME:") + arg->GetName()).c_str());
328 arg->SetName((prefix + arg->GetName()).c_str());
342 return newObservables;
358std::unique_ptr<RooArgSet>
362 return std::make_unique<RooArgSet>();
char * Form(const char *fmt,...)
std::unique_ptr< RooTemplateProxy< RooAbsReal > > _fractionInRange
RooTemplateProxy< RooAbsReal > _weightVar
void computeBatch(cudaStream_t *, double *output, size_t nOut, RooFit::Detail::DataMap const &) const override
Compute multiple negative logs of propabilities.
void applyWeightSquared(bool flag) override
Toggles the weight square correction.
ROOT::Math::KahanSum< double > _offset
! Offset as KahanSum to avoid loss of precision
RooTemplateProxy< RooAbsPdf > _pdf
std::vector< double > _logProbasBuffer
!
void resetWeightVarNames()
RooTemplateProxy< RooAbsReal > _weightSquaredVar
void getParametersHook(const RooArgSet *nset, RooArgSet *list, Bool_t stripDisconnected) const override
std::vector< double > _binw
!
static constexpr const char * weightVarName
std::unique_ptr< RooArgSet > fillNormSetForServer(RooArgSet const &normSet, RooAbsArg const &server) const override
Fills a RooArgSet to be used as the normalization set for a server, given a normalization set for thi...
RooArgSet prefixObservableAndWeightNames(std::string const &prefix)
Replaces all observables and the weight variable of this NLL with clones that only differ by a prefix...
static constexpr const char * weightVarNameSumW2
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
static KahanSum< T, N > Accumulate(Iterator begin, Iterator end, T initialValue=T{})
Iterate over a range and return an instance of a KahanSum.
void Add(T x)
Single-element accumulation. Will not vectorise.
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Given a set of possible observables, return the observables that this PDF depends on.
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
Bool_t getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Recursively replace all servers with the new servers in newSet.
void SetName(const char *name)
Set the name of the TNamed.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Storage_t::size_type size() const
RooAbsArg * first() const
void clear()
Clear contents. If the collection is owning, it will also delete the contents.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
double extendedTerm(double sumEntries, double expected, double sumEntriesW2=0.0) const
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
RooAbsReal * createIntegral(const RooArgSet &iset, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Create an object that represents the integral of the function over one or more observables listed in ...
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &obs, Double_t xlo, Double_t xhi) const
Retrieve bin boundaries if this distribution is binned in obs.
const RooNumIntConfig * getIntegratorConfig() const
Return the numeric integration configuration used for this object.
void logEvalError(const char *message, const char *serverValueString=0) 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.
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
auto & at(RooAbsArg const *arg, RooAbsArg const *=nullptr)
RooProdPdf is an efficient implementation of a product of PDFs of the form.
RooRealVar represents a variable that can be changed from the outside.
bool setArg(T &newRef)
Change object held in proxy into newRef.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
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.
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.
static void output(int code)