97 _refCoefNorm(
"!refCoefNorm",
"Reference coefficient normalization set",this,false,false),
98 _projCacheMgr(this,10),
99 _pdfList(
"!pdfs",
"List of PDFs",this),
100 _coefList(
"!coefficients",
"List of coefficients",this),
101 _coefErrCount{_errorCount}
111 using PdfInfo = std::pair<std::string,RooAbsArg*>;
112 std::set<PdfInfo> seen;
114 PdfInfo elem{pdf->
GetName(), pdf};
115 auto comp = [&](PdfInfo
const&
p){
return p.first == elem.first &&
p.second != elem.second; };
116 auto found = std::find_if(seen.begin(), seen.end(), comp);
117 if(found != seen.end()) {
118 std::stringstream errorMsg;
119 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName()
120 <<
") pdf list contains pdfs with duplicate name \"" << pdf->GetName() <<
"\".";
121 coutE(InputArguments) << errorMsg.str() << std::endl;
122 throw std::invalid_argument(errorMsg.str().c_str());
160 if (inPdfList.
size()>inCoefList.
size()+1 || inPdfList.
size()<inCoefList.
size()) {
161 std::stringstream errorMsg;
162 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName()
163 <<
") number of pdfs and coefficients inconsistent, must have Npdf=Ncoef or Npdf=Ncoef+1.";
164 coutE(InputArguments) << errorMsg.str() << std::endl;
165 throw std::invalid_argument(errorMsg.str().c_str());
168 if (recursiveFractions && inPdfList.
size()!=inCoefList.
size()+1) {
169 std::stringstream errorMsg;
170 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName()
171 <<
"): Recursive fractions option can only be used if Npdf=Ncoef+1.";
172 coutE(InputArguments) << errorMsg.str() << std::endl;
173 throw std::invalid_argument(errorMsg.str());
180 partinCoefList.
add(coef) ;
181 if(partinCoefList.
size() == 1) {
186 std::stringstream rfracName;
187 rfracName <<
GetName() <<
"_recursive_fraction_" << pdf.
GetName() <<
"_" << partinCoefList.
size();
188 auto rfrac = std::make_unique<RooRecursiveFraction>(rfracName.str().c_str(),
"Recursive Fraction",partinCoefList) ;
189 auto & rfracRef = *rfrac;
194 for (
auto i = 0u; i < inCoefList.
size(); ++i) {
195 auto coef =
dynamic_cast<RooAbsReal*
>(inCoefList.
at(i));
196 auto pdf =
dynamic_cast<RooAbsPdf*
>(inPdfList.
at(i));
197 if (inPdfList.
at(i) ==
nullptr) {
198 std::stringstream errorMsg;
199 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName()
200 <<
") number of pdfs and coefficients inconsistent, must have Npdf=Ncoef or Npdf=Ncoef+1";
201 coutE(InputArguments) << errorMsg.str() << std::endl;
202 throw std::invalid_argument(errorMsg.str());
205 std::stringstream errorMsg;
206 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName() <<
") coefficient " << (coef ? coef->GetName() :
"") <<
" is not of type RooAbsReal, ignored";
207 coutE(InputArguments) << errorMsg.str() << std::endl;
208 throw std::invalid_argument(errorMsg.str());
211 std::stringstream errorMsg;
212 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName() <<
") pdf " << (pdf ? pdf->
GetName() :
"") <<
" is not of type RooAbsPdf, ignored";
213 coutE(InputArguments) << errorMsg.str() << std::endl;
214 throw std::invalid_argument(errorMsg.str());
219 _coefList.
add(recursiveFractions ? addRecursiveCoef(*pdf, *coef) : *coef);
222 if (inPdfList.
size() == inCoefList.
size() + 1) {
226 coutE(InputArguments) <<
"RooAddPdf::RooAddPdf(" <<
GetName() <<
") last argument " << inPdfList.
at(inCoefList.
size())->
GetName() <<
" is not of type RooAbsPdf." << std::endl;
227 throw std::invalid_argument(
"Last argument for RooAddPdf is not a PDF.");
232 if (recursiveFractions) {
258 for (
const auto pdfArg : inPdfList) {
259 auto pdf =
dynamic_cast<const RooAbsPdf*
>(pdfArg);
262 std::stringstream errorMsg;
263 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName() <<
") pdf " << (pdf ? pdf->GetName() :
"")
264 <<
" is not of type RooAbsPdf, RooAddPdf constructor call is invalid!";
265 coutE(InputArguments) << errorMsg.str() << std::endl;
266 throw std::invalid_argument(errorMsg.str().c_str());
268 if (!pdf->canBeExtended()) {
269 std::stringstream errorMsg;
270 errorMsg <<
"RooAddPdf::RooAddPdf(" <<
GetName() <<
") pdf " << pdf->GetName()
271 <<
" is not extendable, RooAddPdf constructor call is invalid!";
272 coutE(InputArguments) << errorMsg.str() << std::endl;
273 throw std::invalid_argument(errorMsg.str().c_str());
287 _refCoefNorm(
"!refCoefNorm",this,other._refCoefNorm),
288 _refCoefRangeName((
TNamed*)other._refCoefRangeName),
289 _projCacheMgr(other._projCacheMgr,this),
290 _codeReg(other._codeReg),
291 _pdfList(
"!pdfs",this,other._pdfList),
292 _coefList(
"!coefficients",this,other._coefList),
293 _haveLastCoef(other._haveLastCoef),
294 _allExtendable(other._allExtendable),
295 _recursive(other._recursive)
315 if (refCoefNorm.
empty()) {
415 if(nset && nset->
empty()) nset =
nullptr;
417 if (nset ==
nullptr) {
439 if(nset ==
nullptr) {
447 if (nset ==
nullptr) {
448 coutW(Eval) <<
"Evaluating RooAddPdf " <<
GetName() <<
" without a defined normalization set. This can lead to ambiguous "
449 "coefficients definition and incorrect results."
450 <<
" Use RooAddPdf::fixCoefNormalization(nset) to provide a normalization set for "
451 "defining uniquely RooAddPdf coefficients!"
459 return {nset, cache};
474 bool nsetChanged(
false) ;
485 double snormVal = 1.;
488 double pdfVal = pdf.getVal(nset);
489 if (pdf.isSelectedComp()) {
511 if(coefVals.size() > 1) {
513 throw std::runtime_error(
"The RooAddPdf doesn't support per-event coefficients in CUDA mode yet!");
530 for (
unsigned int pdfNo = 0; pdfNo <
_pdfList.
size(); ++pdfNo)
533 if (pdf->isSelectedComp())
535 pdfs.push_back(dataMap.
at(pdf));
578 const RooArgSet* normSet,
const char* rangeName)
const
586 for (
const auto pdfArg :
_pdfList) {
587 auto pdf =
static_cast<const RooAbsPdf *
>(pdfArg);
589 pdf->getAnalyticalIntegralWN(allVars,subAnalVars,normSet,rangeName) ;
592 for (
const auto arg : allVars) {
593 if (!subAnalVars.
find(arg->GetName()) && pdf->
dependsOn(*arg)) {
594 allAnalVars.
remove(*arg,
true,
true) ;
601 if (allAnalVars.empty()) {
611 auto pdf =
static_cast<const RooAbsPdf *
>(arg);
613 auto allAnalVars2 = std::unique_ptr<RooArgSet>{pdf->getObservables(allAnalVars)} ;
614 subCode[
n] = pdf->getAnalyticalIntegralWN(*allAnalVars2,subAnalVars,normSet,rangeName) ;
615 if (subCode[
n]==0 && !allAnalVars2->empty()) {
616 coutE(InputArguments) <<
"RooAddPdf::getAnalyticalIntegral(" <<
GetName() <<
") WARNING: component PDF " << pdf->
GetName()
617 <<
" advertises inconsistent set of integrals (e.g. (X,Y) but not X or Y individually."
618 <<
" Distributed analytical integration disabled. Please fix PDF" << std::endl ;
628 analVars.
add(allAnalVars) ;
652 if (subCode.empty()) {
653 std::stringstream errorMsg;
654 errorMsg <<
"RooAddPdf::analyticalIntegral(" <<
GetName() <<
"): ERROR unrecognized integration code, " << code;
655 coutE(InputArguments) << errorMsg.str() << std::endl;
656 throw std::invalid_argument(errorMsg.str().c_str());
659 cxcoutD(Caching) <<
"RooAddPdf::aiWN(" <<
GetName() <<
") calling getProjCache with nset = " << (normSet?*normSet:
RooArgSet()) << std::endl ;
683 double val = pdf->analyticalIntegralWN(subCode[i],normSet,rangeName) ;
684 if (pdf->isSelectedComp()) {
702 double expectedTotal{0.0};
704 cxcoutD(Caching) <<
"RooAddPdf::expectedEvents(" <<
GetName() <<
") calling getProjCache with nset = " << (nset?*nset:
RooArgSet()) << std::endl ;
730 return expectedTotal ;
736 std::unique_ptr<RooAbsReal> out;
738 auto name = std::string(
GetName()) +
"_expectedEvents";
741 for (
auto *pdf : static_range_cast<RooAbsPdf *>(
_pdfList)) {
742 sumSet.
addOwned(pdf->createExpectedEventsFunc(nset));
744 out = std::make_unique<RooAddition>(
name.c_str(),
name.c_str(), sumSet);
745 out->addOwnedComponents(std::move(sumSet));
763 std::unique_ptr<RooAbsReal> owner;
771 for (
auto *pdf : static_range_cast<RooAbsPdf *>(
_pdfList)) {
772 auto next = std::unique_ptr<RooAbsReal>{pdf->createIntegral(*nset, *nset,
_normRange)};
775 next->addOwnedComponents(std::move(owner));
776 owner = std::move(next);
778 auto fracIntegName = std::string(
GetName()) +
"_integSum";
780 std::make_unique<RooRealSumFunc>(fracIntegName.c_str(), fracIntegName.c_str(),
_coefList, terms);
781 fracInteg->addOwnedComponents(std::move(owner));
783 out = std::move(fracInteg);
787 std::string finalName = std::string(out->GetName()) +
"_finalized";
788 if (prodList.
empty()) {
794 auto finalOut = std::make_unique<RooProduct>(finalName.c_str(), finalName.c_str(), prodList);
795 finalOut->addOwnedComponents(std::move(prodList));
841 const RooArgSet* auxProto,
bool verbose)
const
895 bool nameChange,
bool isRecursiveStep)
904std::unique_ptr<RooAbsArg>
907 auto newArg = std::unique_ptr<RooAbsReal>{
static_cast<RooAbsReal *
>(
Clone())};
941 std::string finalName = std::string(
GetName()) +
"_conditional";
942 std::unique_ptr<RooAbsReal> denom{newArg->createIntegral(normSet,
_refCoefNorm)};
943 auto finalArg = std::make_unique<RooGenericPdf>(finalName.c_str(),
"@0/@1",
RooArgList{*newArg, *denom});
948 finalArg->addOwnedComponents(std::move(newArg));
949 finalArg->addOwnedComponents(std::move(denom));
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
double rangeProjScaleFactor(std::size_t idx) const
bool doProjection() const
double suppNormVal(std::size_t idx) const
const std::vector< Int_t > & retrieve(Int_t masterCode) const
Retrieve the array of integer codes associated with the given master code.
Int_t store(const std::vector< Int_t > &codeList, RooArgSet *set1=nullptr, RooArgSet *set2=nullptr, RooArgSet *set3=nullptr, RooArgSet *set4=nullptr)
Store given arrays of integer codes, and up to four RooArgSets in the registry (each setX pointer may...
void clearValueAndShapeDirty() const
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
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.
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
bool isValueDirty() const
TObject * Clone(const char *newname=nullptr) const override
Make a clone of an object using the Streamer facility.
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
bool equals(const RooAbsCollection &otherColl) const
Check if this and other collection have identically-named contents.
RooFit::UniqueId< RooAbsCollection > const & uniqueId() const
Returns a unique ID that is different for every instantiated RooAbsCollection.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
Storage_t const & get() const
Const access to the underlying stl container.
const char * GetName() const override
Returns name of object.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
RooAbsArg * first() const
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsGenContext is the abstract base class for generator contexts of RooAbsPdf objects.
virtual bool syncNormalization(const RooArgSet *dset, bool adjustProxies=true) const
Verify that the normalization integral cached with this PDF is valid for given set of normalization o...
virtual void resetErrorCounters(Int_t resetValue=10)
Reset error counter to given value, limiting the number of future error messages for this pdf to 'res...
bool isActiveNormSet(RooArgSet const *normSet) const
Checks if normSet is the currently active normalization set of this PDF, meaning is exactly the same ...
TString _normRange
Normalization range.
Int_t _errorCount
Number of errors remaining to print.
const char * normRange() const
bool redirectServersHook(const RooAbsCollection &newServerList, bool mustReplaceAll, bool nameChange, bool isRecursiveStep) override
The cache manager.
static Int_t _verboseEval
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
RooFit::OwningPtr< 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 std::liste...
double _value
Cache for current value of object.
virtual void computeBatch(cudaStream_t *, double *output, size_t size, RooFit::Detail::DataMap const &) const
Base function for computing multiple values of a RooAbsReal.
static std::unique_ptr< RooAbsGenContext > create(const Pdf_t &pdf, const RooArgSet &vars, const RooDataSet *prototype, const RooArgSet *auxProto, bool verbose)
Returns a RooAddGenContext if possible, or, if the RooAddGenContext doesn't support this particular R...
static void updateCoefficients(RooAbsPdf const &addPdf, std::vector< double > &coefCache, RooArgList const &pdfList, bool haveLastCoef, AddCacheElem &cache, const RooArgSet *nset, RooArgSet const &refCoefNormSet, bool allExtendable, int &coefErrCount)
Update the RooAddPdf coefficients for a given normalization set and projection configuration.
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
RooListProxy _coefList
List of coefficients.
bool _allExtendable
Flag indicating if all PDF components are extendable.
RooAICRegistry _codeReg
! Registry of component analytical integration codes
RooFit::UniqueId< RooArgSet >::Value_t _idOfLastUsedNormSet
!
double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=nullptr) const override
Return analytical integral defined by given scenario code.
std::unique_ptr< const RooArgSet > _copyOfLastNormSet
!
void updateCoefficients(AddCacheElem &cache, const RooArgSet *nset, bool syncCoefValues=true) const
Update the coefficient values in the given cache element: calculate new remainder fraction,...
Int_t _coefErrCount
! Coefficient error counter
bool _haveLastCoef
Flag indicating if last PDFs coefficient was supplied in the ctor.
void selectNormalization(const RooArgSet *depSet=nullptr, bool force=false) override
Interface function used by test statistics to freeze choice of observables for interpretation of frac...
void printMetaArgs(std::ostream &os) const override
Customized printing of arguments of a RooAddPdf to more intuitively reflect the contents of the produ...
void finalizeConstruction()
void setCacheAndTrackHints(RooArgSet &) override
Label OK'ed components of a RooAddPdf with cache-and-track.
bool _recursive
Flag indicating is fractions are treated recursively.
RooObjCacheManager _projCacheMgr
RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=nullptr, const RooArgSet *auxProto=nullptr, bool verbose=false) const override
Return specialized context to efficiently generate toy events from RooAddPdfs return RooAbsPdf::genCo...
bool checkObservables(const RooArgSet *nset) const override
Check if PDF is valid for given normalization set.
void fixCoefNormalization(const RooArgSet &refCoefNorm)
By default the interpretation of the fraction coefficients is performed in the contextual choice of o...
std::pair< const RooArgSet *, AddCacheElem * > getNormAndCache(const RooArgSet *nset) const
Look up projection cache and per-PDF norm sets.
RooSetProxy _refCoefNorm
Reference observable set for coefficient interpretation.
void selectNormalizationRange(const char *rangeName=nullptr, bool force=false) override
Interface function used by test statistics to freeze choice of range for interpretation of fraction c...
Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &numVars, const RooArgSet *normSet, const char *rangeName=nullptr) const override
Determine which part (if any) of given integral can be performed analytically.
void resetErrorCounters(Int_t resetValue=10) override
Reset error counter to given value, limiting the number of future error messages for this pdf to 'res...
double expectedEvents(const RooArgSet *nset) const override
Return expected number of events for extended likelihood calculation, which is the sum of all coeffic...
double getValV(const RooArgSet *set=nullptr) const override
Calculate and return the current value.
std::unique_ptr< RooAbsArg > compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const override
void fixCoefRange(const char *rangeName)
By default, fraction coefficients are assumed to refer to the default fit range.
AddCacheElem * getProjCache(const RooArgSet *nset, const RooArgSet *iset=nullptr) const
Manager of cache with coefficient projections and transformations.
std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const override
Loop over components for plot sampling hints and merge them if there are multiple.
RooListProxy _pdfList
List of component PDFs.
TNamed * _refCoefRangeName
Reference range name for coefficient interpreation.
void computeBatch(cudaStream_t *, double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Compute addition of PDFs in batches.
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 isBinnedDistribution(const RooArgSet &obs) const override
If all components that depend on obs are binned, so is their sum.
bool redirectServersHook(const RooAbsCollection &, bool, bool, bool) override
The cache manager.
std::vector< double > _coefCache
! Transient cache with transformed values of coefficients
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
Loop over components for plot sampling hints and merge them if there are multiple.
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
virtual void compute(cudaStream_t *, Computer, RestrictArr, size_t, const VarVector &, ArgVector &)=0
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=nullptr)
Setter function without integration set.
void reset()
Clear the cache.
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=nullptr, const TNamed *isetRangeName=nullptr)
Getter function without integration set.
void removeAll() override
Remove all argument inset using remove(const RooAbsArg&).
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...
RooDataSet is a container class to hold unbinned data.
void markAsCompiled(RooAbsArg &arg) const
void compileServers(RooAbsArg &arg, RooArgSet const &normSet)
RooSpan< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
void setCacheAndTrackHints(RooArgSet &) override
Label OK'ed components of a RooRealSumPdf with cache-and-track.
bool checkObservables(const RooArgSet *nset) const override
Check if FUNC is valid for given normalization set.
std::list< double > * plotSamplingHint(RooAbsRealLValue &, double, double) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
Retrieve bin boundaries if this distribution is binned in obs.
void printMetaArgs(std::ostream &os) const override
Customized printing of arguments of a RooRealSumPdf to more intuitively reflect the contents of the p...
bool isBinnedDistribution(const RooArgSet &obs) const override
Check if all components that depend on obs are binned.
The TNamed class is the base class for all named ROOT classes.
const char * GetName() const override
Returns name of object.
RooConstVar & RooConst(double val)
std::vector< RooSpan< const double > > VarVector
R__EXTERN RooBatchComputeInterface * dispatchCUDA
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
std::vector< double > ArgVector