85 _funcList(
"!funcList",
"List of functions",this),
86 _coefList(
"!coefList",
"List of coefficients",this),
151 const std::string className = caller.
ClassName();
152 const std::string constructorName = className +
"::" + className;
154 if (!(inFuncList.
size()==inCoefList.
size()+1 || inFuncList.
size()==inCoefList.
size())) {
155 oocoutE(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName()
156 <<
") number of pdfs and coefficients inconsistent, must have Nfunc=Ncoef or Nfunc=Ncoef+1" << std::endl;
157 throw std::invalid_argument(className +
": Number of PDFs and coefficients is inconsistent.");
161 for (
unsigned int i = 0; i < inCoefList.
size(); ++i) {
162 const auto& func = inFuncList[i];
163 const auto& coef = inCoefList[i];
165 if (!
dynamic_cast<const RooAbsReal*
>(&coef)) {
166 oocoutW(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName() <<
") coefficient " << coef.GetName() <<
" is not of type RooAbsReal, ignored" << std::endl ;
169 if (!
dynamic_cast<const RooAbsReal*
>(&func)) {
170 oocoutW(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName() <<
") func " << func.GetName() <<
" is not of type RooAbsReal, ignored" << std::endl ;
177 if (inFuncList.
size() == inCoefList.
size() + 1) {
178 const auto& func = inFuncList[inFuncList.
size()-1];
179 if (!
dynamic_cast<const RooAbsReal*
>(&func)) {
180 oocoutE(&caller, InputArguments) << constructorName <<
"(" << caller.
GetName() <<
") last func " << func.GetName() <<
" is not of type RooAbsReal, fatal error" << std::endl ;
181 throw std::invalid_argument(className +
": Function passed as is not of type RooAbsReal.");
196 _normIntMgr(other._normIntMgr,this),
197 _funcList(
"!funcList",this,other._funcList),
198 _coefList(
"!coefList",this,other._coefList),
199 _extended(other._extended),
200 _doFloor(other._doFloor)
231 bool & hasWarnedBefore)
235 double sumCoeff = 0.;
239 const double coefVal = coef !=
nullptr ? coef->
getVal() : (1. - sumCoeff);
242 if (coef ==
nullptr && (coefVal < 0 || coefVal > 1.)) {
243 if (!hasWarnedBefore) {
245 <<
") WARNING: sum of FUNC coefficients not in range [0-1], value="
246 << sumCoeff <<
". This means that the PDF is not properly normalised."
247 <<
" If the PDF was meant to be extended, provide as many coefficients as functions." << std::endl;
248 hasWarnedBefore =
true;
254 if (func->isSelectedComp()) {
255 value += func->getVal() * coefVal;
276 for (
unsigned int j = 0; j < nEvents; ++j) {
280 double sumCoeff = 0.;
284 const double coefVal = coef !=
nullptr ? dataMap.
at(coef)[0] : (1. - sumCoeff);
286 if (func->isSelectedComp()) {
287 auto funcValues = dataMap.
at(func);
288 if(funcValues.size() == 1) {
289 for (
unsigned int j = 0; j < nEvents; ++j) {
290 output[j] += funcValues[0] * coefVal;
293 for (
unsigned int j = 0; j < nEvents; ++j) {
294 output[j] += funcValues[j] * coefVal;
300 if (coef ==
nullptr && (coefVal < 0 || coefVal > 1.)) {
302 coutW(Eval) <<
"RooRealSumPdf::computeBatch(" <<
GetName()
303 <<
") WARNING: sum of FUNC coefficients not in range [0-1], value="
304 << 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 ;
316 for (
unsigned int j = 0; j < nEvents; ++j) {
327 std::string lastCoeff;
337 lastCoeff +=
" - " + coeff;
350 std::string
const &coeffSize = std::to_string(
coefList.
size());
351 std::string
const &className = klass->
GetName();
355 ctx.
addToCodeBody(klass,
"double " +
sum +
" = 0, " + lastCoeff +
"= 0;\n");
357 std::string iterator =
"i_" + className;
358 std::string subscriptExpr =
"[" + iterator +
"]";
360 std::string code =
"for(int " + iterator +
" = 0; " + iterator +
" < " + coeffSize +
"; " + iterator +
"++) {\n" +
361 sum +
" += " + funcName + subscriptExpr +
" * " + coeffName + subscriptExpr +
";\n";
363 code += lastCoeff +
" += " + coeffName + subscriptExpr +
";\n";
367 code +=
sum +
" += " + funcName +
"[" + coeffSize +
"]" +
" * (1 - " + lastCoeff +
");\n";
388 if (func.observableOverlaps(nset, coef)) {
390 <<
"): ERROR: coefficient " << coef.GetName()
391 <<
" and FUNC " << func.GetName() <<
" have one or more observables in common" << std::endl;
394 if (coef.dependsOn(*nset)) {
396 <<
"): ERROR coefficient " << coef.GetName()
397 <<
" depends on one or more of the following observables" ; nset->
Print(
"1") ;
424 const RooArgSet* normSet2,
const char* rangeName)
const
433 const RooArgSet* normSet2,
const char* rangeName)
436 if (allVars.
empty())
return 0 ;
440 analVars.
add(allVars) ;
441 std::unique_ptr<RooArgSet> normSet;
443 normSet = std::make_unique<RooArgSet>();
449 Int_t sterileIdx(-1) ;
461 const auto func =
static_cast<RooAbsReal*
>(elm);
463 std::unique_ptr<RooAbsReal> funcInt{func->
createIntegral(analVars,rangeName)};
464 if(
auto funcRealInt =
dynamic_cast<RooRealIntegral*
>(funcInt.get())) funcRealInt->setAllowComponentSelection(
true);
465 cache->_funcIntList.addOwned(std::move(funcInt));
466 if (normSet && !normSet->empty()) {
467 cache->_funcNormList.addOwned(std::unique_ptr<RooAbsReal>{func->createIntegral(*normSet)});
493 bool hasWarnedBefore)
496 if (code==0)
return caller.
getVal(normSet2) ;
519 auto funcIntIt = cache->_funcIntList.begin();
520 for (
const auto coefArg :
coefList) {
522 const auto coef =
static_cast<const RooAbsReal*
>(coefArg);
523 const auto func =
static_cast<const RooAbsReal*
>(*funcIt++);
524 const auto funcInt =
static_cast<RooAbsReal*
>(*funcIntIt++);
526 double coefVal = coef->
getVal(normSet2) ;
529 if (normSet2 ==0 || func->isSelectedComp()) {
531 value += funcInt->getVal()*coefVal ;
533 lastCoef -= coef->getVal(normSet2) ;
541 const auto func =
static_cast<const RooAbsReal*
>(*funcIt);
542 const auto funcInt =
static_cast<RooAbsReal*
>(*funcIntIt);
545 if (normSet2 ==0 || func->isSelectedComp()) {
547 value += funcInt->getVal()*lastCoef ;
551 if (!hasWarnedBefore && (lastCoef<0 || lastCoef>1)) {
553 <<
" WARNING: sum of FUNC coefficients not in range [0-1], value="
554 << 1-lastCoef << endl ;
559 if (normSet2 && normSet2->
getSize()>0) {
563 auto funcNormIter = cache->_funcNormList.begin();
564 for (
const auto coefAsArg :
coefList) {
565 auto coef =
static_cast<RooAbsReal*
>(coefAsArg);
566 auto funcNorm =
static_cast<RooAbsReal*
>(*funcNormIter++);
568 double coefVal = coef->
getVal(normSet2);
571 normVal += funcNorm->getVal()*coefVal ;
577 auto funcNorm =
static_cast<RooAbsReal*
>(*funcNormIter);
580 normVal += funcNorm->getVal()*lastCoef;
584 return value / normVal;
610 std::list<double>* sumBinB =
nullptr;
611 bool needClean(
false) ;
614 for (
auto * func : static_range_cast<RooAbsReal*>(
funcList)) {
616 list<double>* funcBinB = func->binBoundaries(obs,xlo,xhi) ;
625 std::list<double>* newSumBinB =
new list<double>(sumBinB->size()+funcBinB->size()) ;
628 merge(funcBinB->begin(),funcBinB->end(),sumBinB->begin(),sumBinB->end(),newSumBinB->begin()) ;
633 sumBinB = newSumBinB ;
641 list<double>::iterator new_end = unique(sumBinB->begin(),sumBinB->end()) ;
642 sumBinB->erase(new_end,sumBinB->end()) ;
659 for (
auto* func : static_range_cast<RooAbsReal*>(
funcList)) {
661 if (func->dependsOn(obs) && !func->isBinnedDistribution(obs)) {
680 std::list<double>* sumHint =
nullptr;
681 bool needClean(
false) ;
698 auto* newSumHint =
new std::list<double>(sumHint->size()+funcHint->size()) ;
704 merge(funcHint->begin(),funcHint->end(),sumHint->begin(),sumHint->end(),newSumHint->begin()) ;
708 sumHint = newSumHint ;
716 sumHint->erase(std::unique(sumHint->begin(),sumHint->end()), sumHint->end()) ;
737 if (sarg->canNodeBeCached()==
Always) {
738 trackNodes.
add(*sarg) ;
769 const auto func = *(funcIter++);
770 os << coef->GetName() <<
" * " << func->GetName();
774 os <<
" + [%] * " << (*funcIter)->GetName() ;
784 os << func->GetName() ;
791std::unique_ptr<RooAbsArg>
797 std::unique_ptr<RooAbsPdf> pdfClone(
static_cast<RooAbsPdf *
>(this->
Clone()));
799 if (ctx.
likelihoodMode() && pdfClone->getAttribute(
"BinnedLikelihood")) {
808 pdfClone->setAttribute(
"BinnedLikelihoodActive");
819 pdfClone->setAttribute(
"BinnedLikelihoodActiveYields");
827 pdfClone->getObservables(&normSet, depList);
829 auto newArg = std::make_unique<RooNormalizedPdf>(*pdfClone, depList);
833 for (
RooAbsArg *server : newArg->servers()) {
837 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.
Int_t getSize() const
Return the number of elements in the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
const_iterator end() const
Storage_t::size_type size() const
const_iterator begin() const
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
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
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 const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
std::string getTmpVarName()
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)
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Class RooObjCacheManager is an implementation of class RooCacheManager<RooAbsCacheElement> and specia...
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.
const RooArgList & funcList() const
~RooRealSumPdf() override
Destructor.
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 computeBatch(double *output, size_t size, RooFit::Detail::DataMap const &) 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 void translateImpl(RooFit::Detail::CodeSquashContext &ctx, RooAbsArg const *klass, RooArgList const &funcList, RooArgList const &coefList)
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)