27 std::string
const &refCoefNormRange,
int verboseEval)
46 fullDepList.
remove(*iset,
true,
true);
53 if (nset2.
empty() && !refCoefNormSet.
empty()) {
55 nset2.
add(refCoefNormSet);
58 bool hasPdfWithCustomRange =
false;
61 for (std::size_t i = 0; i < pdfList.
size(); ++i) {
62 auto pdf =
static_cast<const RooAbsPdf *
>(pdfList.
at(i));
63 auto coef =
static_cast<const RooAbsReal *
>(coefList.
at(i));
65 hasPdfWithCustomRange |= pdf->normRange() !=
nullptr;
71 if (
auto pdfDeps = std::unique_ptr<RooArgSet>{pdf->getObservables(nset)}) {
72 supNSet.
remove(*pdfDeps,
true,
true);
76 if (
auto coefDeps = std::unique_ptr<RooArgSet>{coef ? coef->getObservables(nset) :
nullptr}) {
77 supNSet.
remove(*coefDeps,
true,
true);
80 std::unique_ptr<RooAbsReal> snorm;
81 auto name = std::string(addPdf.
GetName()) +
"_" + pdf->GetName() +
"_SupNorm";
82 if (!supNSet.
empty()) {
83 snorm = std::make_unique<RooRealIntegral>(
name.c_str(),
"Supplemental normalization integral",
86 <<
" making supplemental normalization set " << supNSet <<
" for pdf component "
90 if (!normRange.empty()) {
91 auto snormTerm = std::unique_ptr<RooAbsReal>(pdf->createIntegral(nset2, nset2, normRange.c_str()));
93 auto oldSnorm = std::move(snorm);
94 snorm = std::make_unique<RooProduct>(
"snorm",
"snorm", *oldSnorm.get(), *snormTerm.get());
95 snorm->addOwnedComponents(std::move(snormTerm), std::move(oldSnorm));
97 snorm = std::move(snormTerm);
103 if (verboseEval > 1) {
105 <<
") synching supplemental normalization list for norm"
106 << (nset ? *nset :
RooArgSet()) << std::endl;
111 const bool projectCoefsForRangeReasons = !refCoefNormRange.empty() || !normRange.empty() || hasPdfWithCustomRange;
114 if (refCoefNormSet.
empty() && !projectCoefsForRangeReasons) {
118 if (!nset2.
equals(refCoefNormSet) || projectCoefsForRangeReasons) {
121 for (
auto *pdf : static_range_cast<const RooAbsPdf *>(pdfList)) {
124 std::unique_ptr<RooAbsReal> pdfProj;
125 if (!refCoefNormSet.
empty() && !nset2.
equals(refCoefNormSet)) {
126 pdfProj = std::unique_ptr<RooAbsReal>{pdf->createIntegral(nset2, refCoefNormSet, normRange.c_str())};
127 pdfProj->setOperMode(addPdf.
operMode());
129 <<
")!=_refCoefNormSet(" << refCoefNormSet
130 <<
") --> pdfProj = " << pdfProj->
GetName() << std::endl;
132 <<
") PP = " << pdfProj->GetName() << std::endl;
135 _projList.emplace_back(std::move(pdfProj));
139 auto deps = std::unique_ptr<RooArgSet>{pdf->getParameters(
RooArgSet())};
140 supNormSet.
remove(*deps,
true,
true);
142 std::unique_ptr<RooAbsReal> snorm;
143 auto name = std::string(addPdf.
GetName()) +
"_" + pdf->GetName() +
"_ProjSupNorm";
144 if (!supNormSet.
empty() && !nset2.
equals(refCoefNormSet)) {
145 snorm = std::make_unique<RooRealIntegral>(
name.c_str(),
"Projection Supplemental normalization integral",
148 <<
") SN = " << snorm->GetName() << std::endl;
153 std::unique_ptr<RooAbsReal> rangeProj2;
154 if (normRange != refCoefNormRange) {
156 pdf->getObservables(refCoefNormSet.
empty() ? nset : &refCoefNormSet, tmp);
157 auto int1 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(tmp, tmp, normRange.c_str())};
158 auto int2 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(tmp, tmp, refCoefNormRange.c_str())};
159 rangeProj2 = std::make_unique<RooRatio>(
"rangeProj",
"rangeProj", *int1, *int2);
160 rangeProj2->addOwnedComponents(std::move(int1), std::move(int2));
170 auto printVector = [](
auto const &
vec,
const char *
name) {
171 std::cout <<
"+++ " <<
name <<
":" << std::endl;
172 for (
auto const &arg :
vec) {
177 std::cout <<
"nullptr" << std::endl;
230 for (
auto arg : pdfList) {
231 auto pdf =
static_cast<RooAbsPdf *
>(arg);
233 coefSum += coefCache[i];
239 <<
") WARNING: total number of expected events is 0" << std::endl;
241 for (std::size_t j = 0; j < pdfList.
size(); j++) {
242 coefCache[j] /= coefSum;
250 double coefSum = std::accumulate(coefCache.begin(), coefCache.end(), 0.0);
253 <<
") WARNING: sum of coefficients is zero 0" << std::endl;
255 const double invCoefSum = 1. / coefSum;
256 for (std::size_t j = 0; j < coefCache.size(); j++) {
257 coefCache[j] *= invCoefSum;
263 double lastCoef = 1.0 - std::accumulate(coefCache.begin(), coefCache.end() - 1, 0.0);
264 coefCache.back() = lastCoef;
267 const float coefDegen = lastCoef < 0. ? -lastCoef : (lastCoef > 1. ? lastCoef - 1. : 0.);
268 if (coefDegen > 1.E-5) {
271 std::stringstream msg;
272 if (coefErrCount-- > 0) {
273 msg <<
"RooAddPdf::updateCoefCache(" << addPdf.
GetName()
274 <<
" WARNING: sum of PDF coefficients not in range [0-1], value=" << 1 - lastCoef;
275 if (coefErrCount == 0) {
276 msg <<
" (no more will be printed)";
278 oocoutW(&addPdf, Eval) << msg.str() << std::endl;
291 for (std::size_t i = 0; i < pdfList.
size(); i++) {
293 coefSum += coefCache[i];
298 for (std::size_t i = 0; i < pdfList.
size(); ++i) {
299 ooccoutD(&addPdf, Caching) <<
" ALEX: POST-SYNC coef[" << i <<
"] = " << coefCache[i]
300 <<
" ( _coefCache[i]/coefSum = " << coefCache[i] * coefSum <<
"/" << coefSum
301 <<
" ) " << std::endl;
307 <<
") sum of coefficients is zero." << std::endl;
310 for (std::size_t i = 0; i < pdfList.
size(); i++) {
311 coefCache[i] /= coefSum;
double projVal(std::size_t idx) const
OwningArgVector _projList
Projection integrals to be multiplied with coefficients.
AddCacheElem(RooAbsPdf const &addPdf, RooArgList const &pdfList, RooArgList const &coefList, const RooArgSet *nset, const RooArgSet *iset, RooArgSet const &refCoefNormSet, std::string const &refCoefNormRange, int verboseEval)
Create a RooAddPdf cache element for a given normalization set and projection configuration.
double projSuppNormVal(std::size_t idx) const
RooArgList containedArgs(Action) override
List all RooAbsArg derived contents in this cache element.
OwningArgVector _rangeProjList
Range integrals to be multiplied with coefficients (reference to target range)
double rangeProjScaleFactor(std::size_t idx) const
OwningArgVector _suppNormList
Supplemental normalization list.
bool doProjection() const
OwningArgVector _suppProjList
Projection integrals to multiply with coefficients for supplemental normalization.
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.
OperMode operMode() const
Query the operation mode of this node.
bool equals(const RooAbsCollection &otherColl) const
Check if this and other collection have identically-named contents.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
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
Abstract interface for all probability density functions.
virtual double expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
const char * normRange() const
Abstract base class for objects that represent a real value and implements functionality common to al...
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.
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.
static RooMsgService & instance()
Return reference to singleton instance.
static RooConstVar & value(double value)
Return a constant value object with given value.
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.