30 std::string
const &refCoefNormRange,
int verboseEval)
41 _suppNormList.reserve(pdfList.
size());
49 fullDepList.
remove(*iset,
true,
true);
56 if (nset2.
empty() && !refCoefNormSet.
empty()) {
58 nset2.
add(refCoefNormSet);
61 bool hasPdfWithDifferentRange =
false;
64 for (std::size_t i = 0; i < pdfList.
size(); ++i) {
65 auto pdf =
static_cast<const RooAbsPdf *
>(pdfList.
at(i));
66 auto coef =
static_cast<const RooAbsReal *
>(coefList.
at(i));
68 const std::string normRangeComponent = pdf->normRange() ? pdf->normRange() :
"";
69 const bool componentHasDifferentNormRange = normRangeComponent != normRange;
71 hasPdfWithDifferentRange |= componentHasDifferentNormRange;
77 if (
auto pdfDeps = std::unique_ptr<RooArgSet>{pdf->getObservables(nset)}) {
78 supNSet.remove(*pdfDeps,
true,
true);
82 if (
auto coefDeps = std::unique_ptr<RooArgSet>{coef ? coef->getObservables(nset) :
nullptr}) {
83 supNSet.remove(*coefDeps,
true,
true);
86 std::unique_ptr<RooAbsReal> snorm;
87 auto name = std::string(addPdf.
GetName()) +
"_" + pdf->GetName() +
"_SupNorm";
88 if (!supNSet.empty()) {
89 snorm = std::make_unique<RooRealIntegral>(
name.c_str(),
"Supplemental normalization integral",
92 <<
" making supplemental normalization set " << supNSet <<
" for pdf component "
93 << pdf->GetName() << std::endl;
96 if (componentHasDifferentNormRange) {
97 auto snormTerm = std::unique_ptr<RooAbsReal>(pdf->createIntegral(nset2, nset2, normRange.c_str()));
99 auto oldSnorm = std::move(snorm);
100 snorm = std::make_unique<RooProduct>(
"snorm",
"snorm", *oldSnorm.get(), *snormTerm);
101 snorm->addOwnedComponents(std::move(snormTerm), std::move(oldSnorm));
103 snorm = std::move(snormTerm);
106 _suppNormList.emplace_back(std::move(snorm));
109 if (verboseEval > 1) {
111 <<
") synching supplemental normalization list for norm"
112 << (nset ? *nset :
RooArgSet()) << std::endl;
117 const bool projectCoefsForRangeReasons = !refCoefNormRange.empty() || !normRange.empty() || hasPdfWithDifferentRange;
120 if (refCoefNormSet.
empty() && !projectCoefsForRangeReasons) {
124 if (!nset2.
equals(refCoefNormSet) || projectCoefsForRangeReasons) {
130 std::unique_ptr<RooAbsReal> pdfProj;
131 if (!refCoefNormSet.
empty() && !nset2.
equals(refCoefNormSet)) {
132 pdfProj = std::unique_ptr<RooAbsReal>{pdf->createIntegral(nset2, refCoefNormSet, normRange.c_str())};
133 pdfProj->setOperMode(addPdf.
operMode());
135 <<
")!=_refCoefNormSet(" << refCoefNormSet
136 <<
") --> pdfProj = " << pdfProj->GetName() << std::endl;
138 <<
") PP = " << pdfProj->GetName() << std::endl;
141 _projList.emplace_back(std::move(pdfProj));
145 auto deps = std::unique_ptr<RooArgSet>{pdf->getParameters(
RooArgSet())};
146 supNormSet.remove(*deps,
true,
true);
148 std::unique_ptr<RooAbsReal> snorm;
149 auto name = std::string(addPdf.
GetName()) +
"_" + pdf->GetName() +
"_ProjSupNorm";
150 if (!supNormSet.empty() && !nset2.
equals(refCoefNormSet)) {
151 snorm = std::make_unique<RooRealIntegral>(
name.c_str(),
"Projection Supplemental normalization integral",
154 <<
") SN = " << snorm->GetName() << std::endl;
156 _suppProjList.emplace_back(std::move(snorm));
159 std::unique_ptr<RooAbsReal> rangeProj2;
160 if (normRange != refCoefNormRange) {
162 pdf->getObservables(refCoefNormSet.
empty() ? nset : &refCoefNormSet, tmp);
163 auto int1 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(tmp, tmp, normRange.c_str())};
164 auto int2 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(tmp, tmp, refCoefNormRange.c_str())};
165 rangeProj2 = std::make_unique<RooRatio>(
"rangeProj",
"rangeProj", *int1, *int2);
166 rangeProj2->addOwnedComponents(std::move(int1), std::move(int2));
169 _rangeProjList.emplace_back(std::move(rangeProj2));
174void AddCacheElem::print()
const
176 auto printVector = [](
auto const &
vec,
const char *
name) {
177 std::cout <<
"+++ " <<
name <<
":" << std::endl;
178 for (
auto const &arg :
vec) {
183 std::cout <<
"nullptr" << std::endl;
188 printVector(_suppNormList,
"_suppNormList");
189 printVector(_projList,
"_projList");
190 printVector(_suppProjList,
"_suppProjList");
191 printVector(_rangeProjList,
"_rangeProjList");
201 for (
auto const &arg : _projList) {
205 for (
auto const &arg : _suppProjList) {
209 for (
auto const &arg : _rangeProjList) {
226void RooAddHelpers::updateCoefficients(
RooAbsPdf const &addPdf, std::size_t nPdfs, std::vector<double> &coefCache,
227 bool haveLastCoef, AddCacheElem &cache,
int &coefErrCount)
233 double coefSum = std::accumulate(coefCache.begin(), coefCache.end(), 0.0);
236 <<
") WARNING: sum of coefficients is zero 0" << std::endl;
238 const double invCoefSum = 1. / coefSum;
239 for (std::size_t j = 0; j < coefCache.size(); j++) {
240 coefCache[j] *= invCoefSum;
246 double lastCoef = 1.0 - std::accumulate(coefCache.begin(), coefCache.end() - 1, 0.0);
247 coefCache.back() = lastCoef;
250 const float coefDegen = lastCoef < 0. ? -lastCoef : (lastCoef > 1. ? lastCoef - 1. : 0.);
251 if (coefDegen > 1.E-5) {
254 std::stringstream msg;
255 if (coefErrCount-- > 0) {
256 msg <<
"RooAddPdf::updateCoefCache(" << addPdf.
GetName()
257 <<
" WARNING: sum of PDF coefficients not in range [0-1], value=" << 1 - lastCoef;
258 if (coefErrCount == 0) {
259 msg <<
" (no more will be printed)";
261 oocoutW(&addPdf, Eval) << msg.str() << std::endl;
267 if (!cache.doProjection()) {
273 for (std::size_t i = 0; i < nPdfs; i++) {
274 coefCache[i] *= cache.projVal(i) / cache.projSuppNormVal(i) * cache.rangeProjScaleFactor(i);
275 coefSum += coefCache[i];
280 for (std::size_t i = 0; i < nPdfs; ++i) {
281 ooccoutD(&addPdf, Caching) <<
" ALEX: POST-SYNC coef[" << i <<
"] = " << coefCache[i]
282 <<
" ( _coefCache[i]/coefSum = " << coefCache[i] * coefSum <<
"/" << coefSum
283 <<
" ) " << std::endl;
289 <<
") sum of coefficients is zero." << std::endl;
292 for (std::size_t i = 0; i < nPdfs; i++) {
293 coefCache[i] /= coefSum;
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
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.
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.
const char * normRange() const
Abstract base class for objects that represent a real value and implements functionality common to al...
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.
static double packFloatIntoNaN(float payload)
Pack float into mantissa of a NaN.