62using std::list, std::endl, std::ostream;
80 _funcList(
"!funcList",
"List of functions",this),
81 _coefList(
"!coefList",
"List of coefficients",this),
143 const std::string className = caller.
ClassName();
144 const std::string constructorName = className +
"::" + className;
146 if (!(inFuncList.
size()==inCoefList.
size()+1 || inFuncList.
size()==inCoefList.
size())) {
147 oocoutE(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName()
148 <<
") number of pdfs and coefficients inconsistent, must have Nfunc=Ncoef or Nfunc=Ncoef+1" << std::endl;
149 throw std::invalid_argument(className +
": Number of PDFs and coefficients is inconsistent.");
153 for (
unsigned int i = 0; i < inCoefList.
size(); ++i) {
154 const auto& func = inFuncList[i];
155 const auto& coef = inCoefList[i];
157 if (!
dynamic_cast<const RooAbsReal*
>(&coef)) {
158 oocoutW(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName() <<
") coefficient " << coef.GetName() <<
" is not of type RooAbsReal, ignored" << std::endl ;
161 if (!
dynamic_cast<const RooAbsReal*
>(&func)) {
162 oocoutW(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName() <<
") func " << func.GetName() <<
" is not of type RooAbsReal, ignored" << std::endl ;
169 if (inFuncList.
size() == inCoefList.
size() + 1) {
170 const auto& func = inFuncList[inFuncList.
size()-1];
171 if (!
dynamic_cast<const RooAbsReal*
>(&func)) {
172 oocoutE(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName() <<
") last func " << func.GetName() <<
" is not of type RooAbsReal, fatal error" << std::endl ;
173 throw std::invalid_argument(className +
": Function passed as is not of type RooAbsReal.");
188 _normIntMgr(other._normIntMgr,this),
189 _funcList(
"!funcList",this,other._funcList),
190 _coefList(
"!coefList",this,other._coefList),
192 _doFloor(other._doFloor)
209 bool & hasWarnedBefore)
213 double sumCoeff = 0.;
217 const double coefVal = coef !=
nullptr ? coef->
getVal() : (1. - sumCoeff);
220 if (coef ==
nullptr && (coefVal < 0 || coefVal > 1.)) {
221 if (!hasWarnedBefore) {
223 <<
") WARNING: sum of FUNC coefficients not in range [0-1], value="
224 << sumCoeff <<
". This means that the PDF is not properly normalised."
225 <<
" If the PDF was meant to be extended, provide as many coefficients as functions." << std::endl;
226 hasWarnedBefore =
true;
232 if (func->isSelectedComp()) {
233 value += func->getVal() * coefVal;
255 std::size_t nEvents =
output.size();
260 double sumCoeff = 0.;
264 const double coefVal = coef !=
nullptr ? ctx.
at(coef)[0] : (1. - sumCoeff);
266 if (func->isSelectedComp()) {
267 auto funcValues = ctx.
at(func);
268 if(funcValues.size() == 1) {
269 for (
unsigned int j = 0; j < nEvents; ++j) {
270 output[j] += funcValues[0] * coefVal;
273 for (
unsigned int j = 0; j < nEvents; ++j) {
274 output[j] += funcValues[j] * coefVal;
280 if (coef ==
nullptr && (coefVal < 0 || coefVal > 1.)) {
283 <<
") WARNING: sum of FUNC coefficients not in range [0-1], value="
284 << sumCoeff <<
". This means that the PDF is not properly normalised. If the PDF was meant to be extended, provide as many coefficients as functions." << endl ;
296 for (
unsigned int j = 0; j < nEvents; ++j) {
309 std::string
const &coeffSize = std::to_string(
coefList.
size());
313 ctx.
addToCodeBody(klass,
"double " +
sum +
" = 0;\ndouble " + coeffSum +
"= 0;\n");
316 std::string subscriptExpr =
"[" + iterator +
"]";
318 std::string code =
"for(int " + iterator +
" = 0; " + iterator +
" < " + coeffSize +
"; " + iterator +
"++) {\n" +
319 sum +
" += " + funcName + subscriptExpr +
" * " + coeffName + subscriptExpr +
";\n";
320 code += coeffSum +
" += " + coeffName + subscriptExpr +
";\n";
324 code +=
sum +
" += " + funcName +
"[" + coeffSize +
"]" +
" * (1 - " + coeffSum +
");\n";
325 }
else if (normalize) {
326 code +=
sum +
" /= " + coeffSum +
";\n";
347 if (func.observableOverlaps(nset, coef)) {
349 <<
"): ERROR: coefficient " << coef.GetName()
350 <<
" and FUNC " << func.GetName() <<
" have one or more observables in common" << std::endl;
353 if (coef.dependsOn(*nset)) {
355 <<
"): ERROR coefficient " << coef.GetName()
356 <<
" depends on one or more of the following observables" ; nset->
Print(
"1") ;
383 const RooArgSet* normSet2,
const char* rangeName)
const
392 const RooArgSet* normSet2,
const char* rangeName)
395 if (allVars.
empty())
return 0 ;
399 analVars.
add(allVars) ;
400 std::unique_ptr<RooArgSet> normSet;
402 normSet = std::make_unique<RooArgSet>();
408 Int_t sterileIdx(-1) ;
420 const auto func =
static_cast<RooAbsReal*
>(elm);
422 std::unique_ptr<RooAbsReal> funcInt{func->
createIntegral(analVars,rangeName)};
423 if(
auto funcRealInt =
dynamic_cast<RooRealIntegral*
>(funcInt.get())) funcRealInt->setAllowComponentSelection(
true);
424 cache->_funcIntList.addOwned(std::move(funcInt));
425 if (normSet && !normSet->empty()) {
426 cache->_funcNormList.addOwned(std::unique_ptr<RooAbsReal>{func->createIntegral(*normSet)});
452 bool hasWarnedBefore)
455 if (code==0)
return caller.
getVal(normSet2) ;
460 if (cache==
nullptr) {
478 auto funcIntIt = cache->_funcIntList.begin();
479 for (
const auto coefArg :
coefList) {
481 const auto coef =
static_cast<const RooAbsReal*
>(coefArg);
482 const auto func =
static_cast<const RooAbsReal*
>(*funcIt++);
483 const auto funcInt =
static_cast<RooAbsReal*
>(*funcIntIt++);
485 double coefVal = coef->
getVal(normSet2) ;
488 if (normSet2 ==
nullptr || func->isSelectedComp()) {
490 value += funcInt->getVal()*coefVal ;
492 lastCoef -= coef->getVal(normSet2) ;
500 const auto func =
static_cast<const RooAbsReal*
>(*funcIt);
501 const auto funcInt =
static_cast<RooAbsReal*
>(*funcIntIt);
504 if (normSet2 ==
nullptr || func->isSelectedComp()) {
506 value += funcInt->getVal()*lastCoef ;
510 if (!hasWarnedBefore && (lastCoef<0 || lastCoef>1)) {
512 <<
" WARNING: sum of FUNC coefficients not in range [0-1], value="
513 << 1-lastCoef << endl ;
518 if (normSet2 && !normSet2->
empty()) {
522 auto funcNormIter = cache->_funcNormList.begin();
523 for (
const auto coefAsArg :
coefList) {
524 auto coef =
static_cast<RooAbsReal*
>(coefAsArg);
525 auto funcNorm =
static_cast<RooAbsReal*
>(*funcNormIter++);
527 double coefVal = coef->
getVal(normSet2);
530 normVal += funcNorm->getVal()*coefVal ;
536 auto funcNorm =
static_cast<RooAbsReal*
>(*funcNormIter);
539 normVal += funcNorm->getVal()*lastCoef;
543 return value / normVal;
569 std::list<double>* sumBinB =
nullptr;
570 bool needClean(
false) ;
573 for (
auto * func : static_range_cast<RooAbsReal*>(
funcList)) {
575 list<double>* funcBinB = func->binBoundaries(obs,xlo,xhi) ;
584 std::list<double>* newSumBinB =
new list<double>(sumBinB->size()+funcBinB->size()) ;
587 merge(funcBinB->begin(),funcBinB->end(),sumBinB->begin(),sumBinB->end(),newSumBinB->begin()) ;
592 sumBinB = newSumBinB ;
600 list<double>::iterator new_end = unique(sumBinB->begin(),sumBinB->end()) ;
601 sumBinB->erase(new_end,sumBinB->end()) ;
618 for (
auto* func : static_range_cast<RooAbsReal*>(
funcList)) {
620 if (func->dependsOn(obs) && !func->isBinnedDistribution(obs)) {
639 std::list<double>* sumHint =
nullptr;
640 bool needClean(
false) ;
657 auto* newSumHint =
new std::list<double>(sumHint->size()+funcHint->size()) ;
663 merge(funcHint->begin(),funcHint->end(),sumHint->begin(),sumHint->end(),newSumHint->begin()) ;
667 sumHint = newSumHint ;
675 sumHint->erase(std::unique(sumHint->begin(),sumHint->end()), sumHint->end()) ;
696 if (sarg->canNodeBeCached()==
Always) {
697 trackNodes.
add(*sarg) ;
728 const auto func = *(funcIter++);
729 os << coef->GetName() <<
" * " << func->GetName();
733 os <<
" + [%] * " << (*funcIter)->GetName() ;
743 os << func->GetName() ;
750std::unique_ptr<RooAbsArg>
756 std::unique_ptr<RooAbsPdf> pdfClone(
static_cast<RooAbsPdf *
>(this->
Clone()));
758 if (ctx.
likelihoodMode() && pdfClone->getAttribute(
"BinnedLikelihood")) {
767 pdfClone->setAttribute(
"BinnedLikelihoodActive");
778 pdfClone->setAttribute(
"BinnedLikelihoodActiveYields");
786 pdfClone->getObservables(&normSet, depList);
788 auto newArg = std::make_unique<RooNormalizedPdf>(*pdfClone, depList);
792 for (
RooAbsArg *server : newArg->servers()) {
796 newArg->addOwnedComponents(std::move(pdfClone));
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Common abstract base class for objects that represent a value and a "shape" in RooFit.
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
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.
TObject * Clone(const char *newname=nullptr) const override
Make a clone of an object using the Streamer facility.
Abstract base class for objects to be stored in RooAbsCache cache manager objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
TIterator end() and range-based for loops.")
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Abstract interface for all probability density functions.
double getNorm(const RooArgSet &nset) const
Get normalisation term needed to normalise the raw values returned by getVal().
std::unique_ptr< RooAbsArg > compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const override
const char * normRange() const
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
Abstract base class for objects that represent a real value and implements functionality common to al...
RooFit::OwningPtr< RooAbsReal > createIntegral(const RooArgSet &iset, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Create an object that represents the integral of the function over one or more observables listed in ...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
virtual Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet, const char *rangeName=nullptr) const
Variant of getAnalyticalIntegral that is also passed the normalization set that should be applied to ...
bool getForceNumInt() const
void logEvalError(const char *message, const char *serverValueString=nullptr) const
Log evaluation error message.
const RooNumIntConfig * getIntegratorConfig() const
Return the numeric integration configuration used for this object.
virtual std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=nullptr)
Setter function without integration set.
RooArgSet selectFromSet1(RooArgSet const &argSet, int index) const
Create RooArgSet containing the objects that are both in the cached set 1 with a given index and an i...
T * getObjByIndex(Int_t index) const
Retrieve payload object by slot index.
RooArgSet selectFromSet2(RooArgSet const &argSet, int index) const
Create RooArgSet containing the objects that are both in the cached set 2 with a given index and an i...
Int_t lastIndex() const
Return index of slot used in last get or set operation.
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=nullptr, const TNamed *isetRangeName=nullptr)
Getter function without integration set.
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
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 getTmpVarName() const
Get a unique variable name to be used in the generated code.
std::string buildArg(RooAbsCollection const &x)
Function to save a RooListProxy as an array in the squashed code.
void setBinnedLikelihoodMode(bool flag)
void markAsCompiled(RooAbsArg &arg) const
void compileServers(RooAbsArg &arg, RooArgSet const &normSet)
bool likelihoodMode() const
bool binWidthFuncFlag() const
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Implementation of a RooCacheManager<RooAbsCacheElement> that specializes in the storage of cache elem...
Performs hybrid numerical/analytical integrals of RooAbsReal objects.
Implements a PDF constructed from a sum of functions:
void setCacheAndTrackHints(RooArgSet &) override
Label OK'ed components of a RooRealSumPdf with cache-and-track.
static bool _doFloorGlobal
Global flag for introducing floor at zero in pdf.
bool _doFloor
Introduce floor at zero in pdf.
std::unique_ptr< RooAbsReal > createExpectedEventsFunc(const RooArgSet *nset) const override
Returns an object that represents the expected number of events for a given normalization set,...
bool checkObservables(const RooArgSet *nset) const override
Check if FUNC is valid for given normalization set.
void setExtended(bool extended)
const RooArgList & funcList() const
RooObjCacheManager _normIntMgr
! The integration cache manager
std::unique_ptr< RooAbsArg > compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const override
std::list< double > * plotSamplingHint(RooAbsRealLValue &, double, double) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
double evaluate() const override
Calculate the current value.
ExtendMode extendMode() const override
Returns ability of PDF to provide extended likelihood terms.
RooListProxy _coefList
List of coefficients.
bool selfNormalized() const override
Shows if a PDF is self-normalized, which means that no attempt is made to add a normalization term.
RooListProxy _funcList
List of component FUNCs.
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
Retrieve bin boundaries if this distribution is binned in obs.
RooRealSumPdf()
Default constructor coverity[UNINIT_CTOR].
void printMetaArgs(std::ostream &os) const override
Customized printing of arguments of a RooRealSumPdf to more intuitively reflect the contents of the p...
Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &numVars, const RooArgSet *normSet, const char *rangeName=nullptr) const override
Advertise that all integrals can be handled internally.
double expectedEvents(const RooArgSet *nset) const override
Return expected number of events for extended likelihood calculation, which is the sum of all coeffic...
void doEval(RooFit::EvalContext &) const override
Base function for computing multiple values of a RooAbsReal.
double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=nullptr) const override
Implement analytical integrations by deferring integration of component functions to integrators of c...
static std::string translateImpl(RooFit::Detail::CodeSquashContext &ctx, RooAbsArg const *klass, RooArgList const &funcList, RooArgList const &coefList, bool normalize=false)
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 ...
static void initializeFuncsAndCoefs(RooAbsReal const &caller, const RooArgList &inFuncList, const RooArgList &inCoefList, RooArgList &funcList, RooArgList &coefList)
const RooArgList & coefList() const
bool _extended
Allow use as extended p.d.f.
bool isBinnedDistribution(const RooArgSet &obs) const override
Check if all components that depend on obs are binned.
const char * GetName() const override
Returns name of object.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
__roodevice__ static __roohost__ double packFloatIntoNaN(float payload)
Pack float into mantissa of a NaN.
static uint64_t sum(uint64_t i)