Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooProdPdf.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18\file RooProdPdf.cxx
19\class RooProdPdf
20\ingroup Roofitcore
21
22Efficient implementation of a product of PDFs of the form
23\f[ \prod_{i=1}^{N} \mathrm{PDF}_i (x, \ldots) \f]
24
25PDFs may share observables. If that is the case any irreducible subset
26of PDFs that share observables will be normalised with explicit numeric
27integration as any built-in normalisation will no longer be valid.
28
29Alternatively, products using conditional PDFs can be defined, *e.g.*
30
31\f[ F(x|y) \cdot G(y), \f]
32
33meaning a PDF \f$ F(x) \f$ **given** \f$ y \f$ and a PDF \f$ G(y) \f$.
34In this construction, \f$ F \f$ is only
35normalised w.r.t \f$ x\f$, and \f$ G \f$ is normalised w.r.t \f$ y \f$. The product in this construction
36is properly normalised.
37
38If exactly one of the component PDFs supports extended likelihood fits, the
39product will also be usable in extended mode, returning the number of expected
40events from the extendable component PDF. The extendable component does not
41have to appear in any specific place in the list.
42**/
43
44#include "RooProdPdf.h"
45#include "RooBatchCompute.h"
46#include "RooRealProxy.h"
47#include "RooProdGenContext.h"
48#include "RooGenProdProj.h"
49#include "RooProduct.h"
50#include "RooNameReg.h"
51#include "RooMsgService.h"
52#include "RooFormulaVar.h"
53#include "RooRealVar.h"
54#include "RooAddition.h"
55#include "RooGlobalFunc.h"
56#include "RooConstVar.h"
57#include "RooWorkspace.h"
58#include "RooRangeBoolean.h"
59#include "RooCustomizer.h"
60#include "RooRealIntegral.h"
61#include "RooFitImplHelpers.h"
63#include "strtok.h"
64
65#include <ROOT/StringUtils.hxx>
66
67#include <algorithm>
68#include <array>
69#include <cstring>
70#include <sstream>
71#include <string>
72#include <unordered_map>
73#include <unordered_set>
74
75#ifndef _WIN32
76#include <strings.h>
77#endif
78
79using std::endl, std::string, std::vector, std::list, std::ostream, std::map, std::ostringstream;
80
81
82
83////////////////////////////////////////////////////////////////////////////////
84/// Default constructor
85
87 _cacheMgr(this,10)
88{
89 // Default constructor
90}
91
92
93////////////////////////////////////////////////////////////////////////////////
94/// Constructor with 2 PDFs (most frequent use case).
95///
96/// The optional cutOff parameter can be used as a speed optimization if
97/// one or more of the PDF have sizable regions with very small values,
98/// which would pull the entire product of PDFs to zero in those regions.
99///
100/// After each PDF multiplication, the running product is compared with
101/// the cutOff parameter. If the running product is smaller than the
102/// cutOff value, the product series is terminated and remaining PDFs
103/// are not evaluated.
104///
105/// There is no magic value of the cutOff, the user should experiment
106/// to find the appropriate balance between speed and precision.
107/// If a cutoff is specified, the PDFs most likely to be small should
108/// be put first in the product. The default cutOff value is zero.
109///
110
111RooProdPdf::RooProdPdf(const char *name, const char *title,
112 RooAbsPdf& pdf1, RooAbsPdf& pdf2, double cutOff) :
113 RooAbsPdf(name,title),
114 _cacheMgr(this,10),
115 _cutOff(cutOff),
116 _pdfList("!pdfs","List of PDFs",this)
117{
118 _pdfList.add(pdf1) ;
119 _pdfNSetList.emplace_back(std::make_unique<RooArgSet>("nset")) ;
120 if (pdf1.canBeExtended()) {
121 _extendedIndex = _pdfList.index(&pdf1) ;
122 }
123
124 _pdfList.add(pdf2) ;
125 _pdfNSetList.emplace_back(std::make_unique<RooArgSet>("nset")) ;
126
127 if (pdf2.canBeExtended()) {
128 if (_extendedIndex>=0) {
129 // Protect against multiple extended terms
130 coutW(InputArguments) << "RooProdPdf::RooProdPdf(" << GetName()
131 << ") multiple components with extended terms detected,"
132 << " product will not be extendable." << std::endl ;
133 _extendedIndex=-1 ;
134 } else {
136 }
137 }
138}
139
140
141
142////////////////////////////////////////////////////////////////////////////////
143/// Constructor from a list of PDFs.
144///
145/// The optional cutOff parameter can be used as a speed optimization if
146/// one or more of the PDF have sizable regions with very small values,
147/// which would pull the entire product of PDFs to zero in those regions.
148///
149/// After each PDF multiplication, the running product is compared with
150/// the cutOff parameter. If the running product is smaller than the
151/// cutOff value, the product series is terminated and remaining PDFs
152/// are not evaluated.
153///
154/// There is no magic value of the cutOff, the user should experiment
155/// to find the appropriate balance between speed and precision.
156/// If a cutoff is specified, the PDFs most likely to be small should
157/// be put first in the product. The default cutOff value is zero.
158
159RooProdPdf::RooProdPdf(const char* name, const char* title, const RooArgList& inPdfList, double cutOff) :
160 RooAbsPdf(name,title),
161 _cacheMgr(this,10),
162 _cutOff(cutOff),
163 _pdfList("!pdfs","List of PDFs",this)
164{
165 addPdfs(inPdfList);
166}
167
168
169
170////////////////////////////////////////////////////////////////////////////////
171/// Constructor from named argument list.
172/// \param[in] name Name used by RooFit
173/// \param[in] title Title used for plotting
174/// \param[in] fullPdfSet Set of "regular" PDFs that are normalised over all their observables
175/// \param[in] arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8 Optional arguments according to table below.
176///
177/// <table>
178/// <tr><th> Argument <th> Description
179/// <tr><td> `Conditional(pdfSet,depSet,depsAreCond=false)` <td> Add PDF to product with condition that it
180/// only be normalized over specified observables. Any remaining observables will be conditional observables.
181/// (Setting `depsAreCond` to true inverts this, so the observables in depSet will be the conditional observables.)
182/// </table>
183///
184/// For example, given a PDF \f$ F(x,y) \f$ and \f$ G(y) \f$,
185///
186/// `RooProdPdf("P", "P", G, Conditional(F,x))` will construct a 2-dimensional PDF as follows:
187/// \f[
188/// P(x,y) = \frac{G(y)}{\int_y G(y)} \cdot \frac{F(x,y)}{\int_x F(x,y)},
189/// \f]
190///
191/// which is a well normalised and properly defined PDF, but different from
192/// \f[
193/// P'(x,y) = \frac{F(x,y) \cdot G(y)}{\int_x\int_y F(x,y) \cdot G(y)}.
194/// \f]
195///
196/// In the former case, the \f$ y \f$ distribution of \f$ P \f$ is identical to that of \f$ G \f$, while
197/// \f$ F \f$ only is used to determine the correlation between \f$ X \f$ and \f$ Y \f$. In the latter
198/// case, the \f$ Y \f$ distribution is defined by the product of \f$ F \f$ and \f$ G \f$.
199///
200/// This \f$ P(x,y) \f$ construction is analogous to generating events from \f$ F(x,y) \f$ with
201/// a prototype dataset sampled from \f$ G(y) \f$.
202
203RooProdPdf::RooProdPdf(const char* name, const char* title, const RooArgSet& fullPdfSet,
204 const RooCmdArg& arg1, const RooCmdArg& arg2,
205 const RooCmdArg& arg3, const RooCmdArg& arg4,
206 const RooCmdArg& arg5, const RooCmdArg& arg6,
207 const RooCmdArg& arg7, const RooCmdArg& arg8) :
208 RooAbsPdf(name,title),
209 _cacheMgr(this,10),
210 _pdfList("!pdfs","List of PDFs",this)
211{
213 l.Add((TObject*)&arg1) ; l.Add((TObject*)&arg2) ;
214 l.Add((TObject*)&arg3) ; l.Add((TObject*)&arg4) ;
215 l.Add((TObject*)&arg5) ; l.Add((TObject*)&arg6) ;
216 l.Add((TObject*)&arg7) ; l.Add((TObject*)&arg8) ;
217
218 initializeFromCmdArgList(fullPdfSet,l) ;
219}
220
221
222
223////////////////////////////////////////////////////////////////////////////////
224/// Constructor from named argument list
225
226RooProdPdf::RooProdPdf(const char* name, const char* title,
227 const RooCmdArg& arg1, const RooCmdArg& arg2,
228 const RooCmdArg& arg3, const RooCmdArg& arg4,
229 const RooCmdArg& arg5, const RooCmdArg& arg6,
230 const RooCmdArg& arg7, const RooCmdArg& arg8) :
231 RooAbsPdf(name,title),
232 _cacheMgr(this,10),
233 _pdfList("!pdfList","List of PDFs",this)
234{
236 l.Add((TObject*)&arg1) ; l.Add((TObject*)&arg2) ;
237 l.Add((TObject*)&arg3) ; l.Add((TObject*)&arg4) ;
238 l.Add((TObject*)&arg5) ; l.Add((TObject*)&arg6) ;
239 l.Add((TObject*)&arg7) ; l.Add((TObject*)&arg8) ;
240
242}
243
244
245
246////////////////////////////////////////////////////////////////////////////////
247/// Internal constructor from list of named arguments
248
249RooProdPdf::RooProdPdf(const char* name, const char* title, const RooArgSet& fullPdfSet, const RooLinkedList& cmdArgList) :
250 RooAbsPdf(name,title),
251 _cacheMgr(this,10),
252 _pdfList("!pdfs","List of PDFs",this)
253{
254 initializeFromCmdArgList(fullPdfSet, cmdArgList) ;
255}
256
257
258
259////////////////////////////////////////////////////////////////////////////////
260/// Copy constructor
261
262RooProdPdf::RooProdPdf(const RooProdPdf& other, const char* name) :
263 RooAbsPdf(other,name),
264 _cacheMgr(other._cacheMgr,this),
265 _genCode(other._genCode),
266 _cutOff(other._cutOff),
267 _pdfList("!pdfs",this,other._pdfList),
268 _extendedIndex(other._extendedIndex),
269 _useDefaultGen(other._useDefaultGen),
270 _refRangeName(other._refRangeName),
271 _selfNorm(other._selfNorm),
272 _defNormSet(other._defNormSet)
273{
274 // Clone contents of normalizarion set list
275 for(auto const& nset : other._pdfNSetList) {
276 _pdfNSetList.emplace_back(std::make_unique<RooArgSet>(nset->GetName()));
277 nset->snapshot(*_pdfNSetList.back());
278 }
279}
280
281
282
283////////////////////////////////////////////////////////////////////////////////
284/// Initialize RooProdPdf configuration from given list of RooCmdArg configuration arguments
285/// and set of 'regular' p.d.f.s in product
286
288{
289 Int_t numExtended(0) ;
290
291 // Process set of full PDFS
292 for(auto const* pdf : static_range_cast<RooAbsPdf*>(fullPdfSet)) {
293 _pdfList.add(*pdf) ;
294 _pdfNSetList.emplace_back(std::make_unique<RooArgSet>("nset")) ;
295
296 if (pdf->canBeExtended()) {
298 numExtended++ ;
299 }
300
301 }
302
303 // Process list of conditional PDFs
304 for(auto * carg : static_range_cast<RooCmdArg*>(l)) {
305
306 if (0 == strcmp(carg->GetName(), "Conditional")) {
307
308 Int_t argType = carg->getInt(0) ;
309 auto pdfSet = static_cast<RooArgSet const*>(carg->getSet(0));
310 auto normSet = static_cast<RooArgSet const*>(carg->getSet(1));
311
312 for(auto * thePdf : static_range_cast<RooAbsPdf*>(*pdfSet)) {
313 _pdfList.add(*thePdf) ;
314
315 _pdfNSetList.emplace_back(std::make_unique<RooArgSet>(0 == argType ? "nset" : "cset"));
316 normSet->snapshot(*_pdfNSetList.back());
317
318 if (thePdf->canBeExtended()) {
319 _extendedIndex = _pdfList.index(thePdf) ;
320 numExtended++ ;
321 }
322
323 }
324
325 } else if (0 != strlen(carg->GetName())) {
326 coutW(InputArguments) << "Unknown arg: " << carg->GetName() << std::endl ;
327 }
328 }
329
330 // Protect against multiple extended terms
331 if (numExtended>1) {
332 coutW(InputArguments) << "RooProdPdf::RooProdPdf(" << GetName()
333 << ") WARNING: multiple components with extended terms detected,"
334 << " product will not be extendable." << std::endl ;
335 _extendedIndex = -1 ;
336 }
337
338
339}
340
341
342
343////////////////////////////////////////////////////////////////////////////////
344/// Destructor
345
349
350
352 int code ;
353 auto cache = static_cast<CacheElem*>(_cacheMgr.getObj(nset, nullptr, &code)) ;
354
355 // If cache doesn't have our configuration, recalculate here
356 if (!cache) {
357 code = getPartIntList(nset, nullptr) ;
358 cache = static_cast<CacheElem*>(_cacheMgr.getObj(nset, nullptr, &code)) ;
359 }
360 return cache;
361}
362
363
364////////////////////////////////////////////////////////////////////////////////
365/// Calculate current value of object
366
368{
370}
371
372
373
374////////////////////////////////////////////////////////////////////////////////
375/// Calculate running product of pdfs terms, using the supplied
376/// normalization set in 'normSetList' for each component
377
378double RooProdPdf::calculate(const RooProdPdf::CacheElem& cache, bool /*verbose*/) const
379{
380 if (cache._isRearranged) {
381 if (dologD(Eval)) {
382 cxcoutD(Eval) << "RooProdPdf::calculate(" << GetName() << ") rearranged product calculation"
383 << " calculate: num = " << cache._rearrangedNum->GetName() << " = " << cache._rearrangedNum->getVal() << std::endl ;
384// cache._rearrangedNum->printComponentTree("",0,5) ;
385 cxcoutD(Eval) << "calculate: den = " << cache._rearrangedDen->GetName() << " = " << cache._rearrangedDen->getVal() << std::endl ;
386// cache._rearrangedDen->printComponentTree("",0,5) ;
387 }
388
389 return cache._rearrangedNum->getVal() / cache._rearrangedDen->getVal();
390 } else {
391
392 double value = 1.0;
393 assert(cache._normList.size() == cache._partList.size());
394 for (std::size_t i = 0; i < cache._partList.size(); ++i) {
395 const auto& partInt = static_cast<const RooAbsReal&>(cache._partList[i]);
396 const auto normSet = cache._normList[i].get();
397
398 const double piVal = partInt.getVal(!normSet->empty() ? normSet : nullptr);
399 value *= piVal ;
400 if (value <= _cutOff) break;
401 }
402
403 return value ;
404 }
405}
406
407namespace {
408
409template<class T>
410void eraseNullptrs(std::vector<T*>& v) {
411 v.erase(std::remove_if(v.begin(), v.end(), [](T* x){ return x == nullptr; } ), v.end());
412}
413
414void removeCommon(std::vector<RooAbsArg*> &v, std::span<RooAbsArg * const> other) {
415
416 for (auto const& arg : other) {
417 auto namePtrMatch = [&arg](const RooAbsArg* elm) {
418 return elm != nullptr && elm->namePtr() == arg->namePtr();
419 };
420
421 auto found = std::find_if(v.begin(), v.end(), namePtrMatch);
422 if(found != v.end()) {
423 *found = nullptr;
424 }
425 }
426 eraseNullptrs(v);
427}
428
429void addCommon(std::vector<RooAbsArg*> &v, std::vector<RooAbsArg*> const& o1, std::vector<RooAbsArg*> const& o2) {
430
431 for (auto const& arg : o1) {
432 auto namePtrMatch = [&arg](const RooAbsArg* elm) {
433 return elm->namePtr() == arg->namePtr();
434 };
435
436 if(std::find_if(o2.begin(), o2.end(), namePtrMatch) != o2.end()) {
437 v.push_back(arg);
438 }
439 }
440}
441
442bool isRangeIdentical(RooArgSet const &observables, TString const &normRange, TNamed *refRangeName)
443{
444 // FK: Here the refRange should be compared to normRange, if it's set, and to the normObs range if it's not set
445 const char *range = normRange.Length() > 0 ? normRange.Data() : nullptr;
446 const char *refRange = RooNameReg::str(refRangeName);
447 for (auto const *normObs : static_range_cast<RooRealVar *>(observables)) {
448 if (normObs->getMin(range) != normObs->getMin(refRange) || normObs->getMax(range) != normObs->getMax(refRange))
449 return false;
450 }
451 return true;
452}
453
454}
455
456
457////////////////////////////////////////////////////////////////////////////////
458/// Factorize product in irreducible terms for given choice of integration/normalization
459
460void RooProdPdf::factorizeProduct(const RooArgSet& normSet, const RooArgSet& intSet, Factorized &factorized) const
461{
462 // List of all term dependents: normalization and imported
463 std::vector<RooArgSet> depAllList;
464 std::vector<RooArgSet> depIntNoNormList;
465
466 // Setup lists for factorization terms and their dependents
467 RooArgSet* term(nullptr);
468 RooArgSet* termIntDeps(nullptr);
469 RooArgSet* termIntNoNormDeps(nullptr);
470
471 std::vector<RooAbsArg*> pdfIntNoNormDeps;
472 std::vector<RooAbsArg*> pdfIntSet;
473 std::vector<RooAbsArg*> pdfNSet;
474 std::vector<RooAbsArg*> pdfCSet;
475 std::vector<RooAbsArg*> pdfNormDeps; // Dependents to be normalized for the PDF
476 std::vector<RooAbsArg*> pdfAllDeps; // All dependents of this PDF
477
478 // Loop over the PDFs
479 for(std::size_t iPdf = 0; iPdf < _pdfList.size(); ++iPdf) {
480 RooAbsPdf& pdf = static_cast<RooAbsPdf&>(_pdfList[iPdf]);
481 RooArgSet& pdfNSetOrig = *_pdfNSetList[iPdf];
482
483 pdfNSet.clear();
484 pdfCSet.clear();
485
486 // Make iterator over tree leaf node list to get the observables.
487 // This code is borrowed from RooAbsPdf::getObservables().
488 // RooAbsArg::treeNodeServer list is relatively expensive, so we only do it
489 // once and use it in a lambda function.
490 RooArgSet pdfLeafList("leafNodeServerList") ;
491 pdf.treeNodeServerList(&pdfLeafList,nullptr,false,true,true) ;
492 auto getObservablesOfCurrentPdf = [&pdfLeafList](
493 std::vector<RooAbsArg*> & out,
494 const RooArgSet& dataList) {
495 for (const auto arg : pdfLeafList) {
496 if (arg->dependsOnValue(dataList) && arg->isLValue()) {
497 out.push_back(arg) ;
498 }
499 }
500 };
501
502 // Reduce pdfNSet to actual dependents
503 if (0 == strcmp("cset", pdfNSetOrig.GetName())) {
504 getObservablesOfCurrentPdf(pdfNSet, normSet);
505 removeCommon(pdfNSet, pdfNSetOrig.get());
506 pdfCSet = pdfNSetOrig.get();
507 } else {
508 // Interpret at NSet
509 getObservablesOfCurrentPdf(pdfNSet, pdfNSetOrig);
510 }
511
512
513 pdfNormDeps.clear();
514 pdfAllDeps.clear();
515
516 // Make list of all dependents of this PDF
517 getObservablesOfCurrentPdf(pdfAllDeps, normSet);
518
519
520 // Make list of normalization dependents for this PDF;
521 if (!pdfNSet.empty()) {
522 // PDF is conditional
523 addCommon(pdfNormDeps, pdfAllDeps, pdfNSet);
524 } else {
525 // PDF is regular
526 pdfNormDeps = pdfAllDeps;
527 }
528
529 pdfIntSet.clear();
530 getObservablesOfCurrentPdf(pdfIntSet, intSet) ;
531
532 // WVE if we have no norm deps, conditional observables should be taken out of pdfIntSet
533 if (pdfNormDeps.empty() && !pdfCSet.empty()) {
534 removeCommon(pdfIntSet, pdfCSet);
535 }
536
537 pdfIntNoNormDeps.clear();
538 pdfIntNoNormDeps = pdfIntSet;
539 removeCommon(pdfIntNoNormDeps, pdfNormDeps);
540
541 // Check if this PDF has dependents overlapping with one of the existing terms
542 bool done = false;
543 int j = 0;
544 auto lIter = factorized.terms.begin();
545 auto ldIter = factorized.norms.begin();
546 for(;lIter != factorized.terms.end(); (++lIter, ++ldIter, ++j)) {
547 RooArgSet *termNormDeps = static_cast<RooArgSet*>(*ldIter);
548 term = static_cast<RooArgSet*>(*lIter);
549 // PDF should be added to existing term if
550 // 1) It has overlapping normalization dependents with any other PDF in existing term
551 // 2) It has overlapping dependents of any class for which integration is requested
552 // 3) If normalization happens over multiple ranges, and those ranges are both defined
553 // in either observable
554
555 bool normOverlap = termNormDeps->overlaps(pdfNormDeps.begin(), pdfNormDeps.end());
556 //bool intOverlap = pdfIntSet->overlaps(*termAllDeps);
557
558 if (normOverlap) {
559 term->add(pdf);
560 termNormDeps->add(pdfNormDeps.begin(), pdfNormDeps.end(), false);
561 depAllList[j].add(pdfAllDeps.begin(), pdfAllDeps.end(), false);
562 if (termIntDeps) {
563 termIntDeps->add(pdfIntSet.begin(), pdfIntSet.end(), false);
564 }
565 if (termIntNoNormDeps) {
566 termIntNoNormDeps->add(pdfIntNoNormDeps.begin(), pdfIntNoNormDeps.end(), false);
567 }
568 termIntNoNormDeps->add(pdfIntNoNormDeps.begin(), pdfIntNoNormDeps.end(), false);
569 done = true;
570 }
571 }
572
573 // If not, create a new term
574 if (!done) {
575 if (!(pdfNormDeps.empty() && pdfAllDeps.empty() &&
576 pdfIntSet.empty()) || normSet.empty()) {
577 term = new RooArgSet("term");
578 RooArgSet *termNormDeps = new RooArgSet("termNormDeps");
579 depAllList.emplace_back(pdfAllDeps.begin(), pdfAllDeps.end(), "termAllDeps");
580 termIntDeps = new RooArgSet(pdfIntSet.begin(), pdfIntSet.end(), "termIntDeps");
581 depIntNoNormList.emplace_back(pdfIntNoNormDeps.begin(), pdfIntNoNormDeps.end(), "termIntNoNormDeps");
582 termIntNoNormDeps = &depIntNoNormList.back();
583
584 term->add(pdf);
585 termNormDeps->add(pdfNormDeps.begin(), pdfNormDeps.end(), false);
586
587 factorized.terms.Add(term);
588 factorized.norms.Add(termNormDeps);
589 factorized.ints.Add(termIntDeps);
590 }
591 }
592
593 }
594
595 // Loop over list of terms again to determine 'imported' observables
596 int i = 0;
597 RooArgSet *normDeps;
598 auto lIter = factorized.terms.begin();
599 auto ldIter = factorized.norms.begin();
600 for(;lIter != factorized.terms.end(); (++lIter, ++ldIter, ++i)) {
601 normDeps = static_cast<RooArgSet*>(*ldIter);
602 term = static_cast<RooArgSet*>(*lIter);
603 // Make list of wholly imported dependents
604 RooArgSet impDeps(depAllList[i]);
605 impDeps.remove(*normDeps, true, true);
606 auto snap = new RooArgSet;
607 impDeps.snapshot(*snap);
608 factorized.imps.Add(snap);
609
610 // Make list of cross dependents (term is self contained for these dependents,
611 // but components import dependents from other components)
612 auto crossDeps = std::unique_ptr<RooAbsCollection>{depIntNoNormList[i].selectCommon(*normDeps)};
613 snap = new RooArgSet;
614 crossDeps->snapshot(*snap);
615 factorized.cross.Add(snap);
616 }
617}
618
619
620
621
622////////////////////////////////////////////////////////////////////////////////
623/// Return list of (partial) integrals of product terms for integration
624/// of p.d.f over observables iset while normalization over observables nset.
625/// Also return list of normalization sets to be used to evaluate
626/// each component in the list correctly.
627
628Int_t RooProdPdf::getPartIntList(const RooArgSet* nset, const RooArgSet* iset, const char* isetRangeName) const
629{
630 // Check if this configuration was created before
631 Int_t sterileIdx(-1);
632
633 if (static_cast<CacheElem*>(_cacheMgr.getObj(nset,iset,&sterileIdx,isetRangeName))) {
634 return _cacheMgr.lastIndex();
635 }
636
637 std::unique_ptr<CacheElem> cache = createCacheElem(nset, iset, isetRangeName);
638
639 // Store the partial integral list and return the assigned code
640 return _cacheMgr.setObj(nset, iset, cache.release(), RooNameReg::ptr(isetRangeName));
641}
642
643
644
645std::unique_ptr<RooProdPdf::CacheElem> RooProdPdf::createCacheElem(const RooArgSet* nset,
646 const RooArgSet* iset,
647 const char* isetRangeName) const
648{
649 // Create containers for partial integral components to be generated
650 auto cache = std::make_unique<CacheElem>();
651
652 // Factorize the product in irreducible terms for this nset
653 Factorized factorized;
654
655 // Normalization set used for factorization
656 RooArgSet factNset(nset ? (*nset) : _defNormSet);
657
658 factorizeProduct(factNset, iset ? (*iset) : RooArgSet(), factorized);
659
660 // Group irriducible terms that need to be (partially) integrated together
661 std::list<std::vector<RooArgSet*>> groupedList;
662 RooArgSet outerIntDeps;
663 groupProductTerms(groupedList, outerIntDeps, factorized);
664
665 // Loop over groups
666 // Find groups of type F(x|y), i.e. termImpSet!=0, construct ratio object
667 std::map<std::string, RooArgSet> ratioTerms;
668 for (auto const& group : groupedList) {
669 if (1 == group.size()) {
670 RooArgSet* term = group[0];
671
672 Int_t termIdx = factorized.terms.IndexOf(term);
673 RooArgSet *norm=static_cast<RooArgSet*>(factorized.norms.At(termIdx));
674 RooArgSet *imps=static_cast<RooArgSet*>(factorized.imps.At(termIdx));
675 RooArgSet termNSet(*norm);
676 RooArgSet termImpSet(*imps);
677
678 if (!termImpSet.empty() && nullptr != _refRangeName) {
679
680 // WVE we can skip this if the ref range is equal to the normalization range
681 // LM : avoid making integral ratio if range is the same. Why was not included ??? (same at line 857)
682 if (!isRangeIdentical(termNSet, _normRange, _refRangeName)) {
683 auto ratio = makeCondPdfRatioCorr(*static_cast<RooAbsReal*>(term->first()), termNSet, termImpSet, normRange(), RooNameReg::str(_refRangeName));
684 std::ostringstream str; termImpSet.printValue(str);
685 ratioTerms[str.str()].addOwned(std::move(ratio));
686 }
687 }
688
689 } else {
690 for (auto const& term : group) {
691
692 Int_t termIdx = factorized.terms.IndexOf(term);
693 RooArgSet *norm=static_cast<RooArgSet*>(factorized.norms.At(termIdx));
694 RooArgSet *imps=static_cast<RooArgSet*>(factorized.imps.At(termIdx));
695 RooArgSet termNSet(*norm);
696 RooArgSet termImpSet(*imps);
697
698 if (!termImpSet.empty() && nullptr != _refRangeName) {
699
700 // WVE we can skip this if the ref range is equal to the normalization range
701 if (!isRangeIdentical(termNSet, _normRange, _refRangeName)) {
702 auto ratio = makeCondPdfRatioCorr(*static_cast<RooAbsReal*>(term->first()), termNSet, termImpSet, normRange(), RooNameReg::str(_refRangeName));
703 std::ostringstream str; termImpSet.printValue(str);
704 ratioTerms[str.str()].addOwned(std::move(ratio));
705 }
706 }
707 }
708 }
709
710 }
711
712 // Find groups with y as termNSet
713 // Replace G(y) with (G(y),ratio)
714 for (auto const& group : groupedList) {
715 for (auto const& term : group) {
716 Int_t termIdx = factorized.terms.IndexOf(term);
717 RooArgSet *norm = static_cast<RooArgSet*>(factorized.norms.At(termIdx));
718 RooArgSet termNSet(*norm);
719
720 // If termNset matches index of ratioTerms, insert ratio here
721 ostringstream str; termNSet.printValue(str);
722 if (!ratioTerms[str.str()].empty()) {
723 term->add(ratioTerms[str.str()]);
724 cache->_ownedList.addOwned(std::move(ratioTerms[str.str()]));
725 }
726 }
727 }
728
729 for (auto const& group : groupedList) {
730 if (1 == group.size()) {
731 RooArgSet* term = group[0];
732
733 Int_t termIdx = factorized.terms.IndexOf(term);
734 RooArgSet *norm = factorized.termNormDeps(termIdx);
735 RooArgSet *integ = factorized.termIntDeps(termIdx);
736
737 // Take list of normalization and integrated dependents from
738 // factorization algorithm
739 RooArgSet termNSet{*norm};
740 RooArgSet termISet{*integ};
741
742 // Add prefab term to partIntList.
743 auto func = processProductTerm(nset, iset, isetRangeName, term, termNSet, termISet);
744 if (func.x0) {
745 cache->_partList.add(*func.x0);
746 if (func.isOwned) cache->_ownedList.addOwned(std::unique_ptr<RooAbsArg>{func.x0});
747
748 cache->_normList.emplace_back(std::make_unique<RooArgSet>());
749 norm->snapshot(*cache->_normList.back(), false);
750
751 cache->_numList.addOwned(std::move(func.x1));
752 cache->_denList.addOwned(std::move(func.x2));
753 }
754 } else {
755 RooArgSet compTermSet;
756 RooArgSet compTermNorm;
757 RooArgSet compTermNum;
758 RooArgSet compTermDen;
759 for (auto const &term : group) {
760 Int_t termIdx = factorized.terms.IndexOf(term);
761 RooArgSet *norm = factorized.termNormDeps(termIdx);
762 RooArgSet *integ = factorized.termIntDeps(termIdx);
763
764 RooArgSet termNSet{*norm};
765 RooArgSet termISet{*integ};
766
767 // Remove outer integration dependents from termISet
768 termISet.remove(outerIntDeps, true, true);
769
770 auto func = processProductTerm(nset, iset, isetRangeName, term, termNSet, termISet, true);
771 if (func.x0) {
772 compTermSet.add(*func.x0);
773 if (func.isOwned) cache->_ownedList.addOwned(std::unique_ptr<RooAbsArg>{func.x0});
774 compTermNorm.add(*norm, false);
775
776 compTermNum.add(*func.x1.release());
777 compTermDen.add(*func.x2.release());
778
779 }
780 }
781
782 // WVE THIS NEEDS TO BE REARRANGED
783
784 // compTermset is set van partial integrals to be multiplied
785 // prodtmp = product (compTermSet)
786 // inttmp = int ( prodtmp ) d (outerIntDeps) _range_isetRangeName
787
788 const std::string prodname = makeRGPPName("SPECPROD", compTermSet, outerIntDeps, RooArgSet(), isetRangeName);
789 auto prodtmp = std::make_unique<RooProduct>(prodname.c_str(), prodname.c_str(), compTermSet);
790
791 const std::string intname = makeRGPPName("SPECINT", compTermSet, outerIntDeps, RooArgSet(), isetRangeName);
792 auto inttmp = std::make_unique<RooRealIntegral>(intname.c_str(), intname.c_str(), *prodtmp, outerIntDeps, nullptr, nullptr, isetRangeName);
793 inttmp->setStringAttribute("PROD_TERM_TYPE", "SPECINT");
794
795 cache->_partList.add(*inttmp);
796
797 // Product of numerator terms
798 const string prodname_num = makeRGPPName("SPECPROD_NUM", compTermNum, RooArgSet(), RooArgSet(), nullptr);
799 auto prodtmp_num = std::make_unique<RooProduct>(prodname_num.c_str(), prodname_num.c_str(), compTermNum);
800 prodtmp_num->addOwnedComponents(compTermNum);
801
802 // Product of denominator terms
803 const string prodname_den = makeRGPPName("SPECPROD_DEN", compTermDen, RooArgSet(), RooArgSet(), nullptr);
804 auto prodtmp_den = std::make_unique<RooProduct>(prodname_den.c_str(), prodname_den.c_str(), compTermDen);
805 prodtmp_den->addOwnedComponents(compTermDen);
806
807 // Ratio
808 std::string name = Form("SPEC_RATIO(%s,%s)", prodname_num.c_str(), prodname_den.c_str());
809 auto ndr = std::make_unique<RooFormulaVar>(name.c_str(), "@0/@1", RooArgList(*prodtmp_num, *prodtmp_den));
810
811 // Integral of ratio
812 std::unique_ptr<RooAbsReal> numtmp{ndr->createIntegral(outerIntDeps,isetRangeName)};
813 numtmp->addOwnedComponents(std::move(ndr));
814
815 cache->_ownedList.addOwned(std::move(prodtmp));
816 cache->_ownedList.addOwned(std::move(inttmp));
817 cache->_ownedList.addOwned(std::move(prodtmp_num));
818 cache->_ownedList.addOwned(std::move(prodtmp_den));
819 cache->_numList.addOwned(std::move(numtmp));
820 cache->_denList.addOwned(std::unique_ptr<RooAbsArg>{static_cast<RooAbsArg*>(RooFit::RooConst(1).clone("1"))});
821 cache->_normList.emplace_back(std::make_unique<RooArgSet>());
822 compTermNorm.snapshot(*cache->_normList.back(), false);
823 }
824 }
825
826 // Need to rearrange product in case of multiple ranges
827 if (_normRange.Contains(",")) {
828 rearrangeProduct(*cache);
829 }
830
831 return cache;
832}
833
834
836{
837 // We own contents of all lists filled by factorizeProduct()
838 terms.Delete();
839 ints.Delete();
840 imps.Delete();
841 norms.Delete();
842 cross.Delete();
843}
844
845
846////////////////////////////////////////////////////////////////////////////////
847/// For single normalization ranges
848
849std::unique_ptr<RooAbsReal> RooProdPdf::makeCondPdfRatioCorr(RooAbsReal& pdf, const RooArgSet& termNset, const RooArgSet& /*termImpSet*/, const char* normRangeTmp, const char* refRange) const
850{
851 std::unique_ptr<RooAbsReal> ratio_num{pdf.createIntegral(termNset,normRangeTmp)};
852 std::unique_ptr<RooAbsReal> ratio_den{pdf.createIntegral(termNset,refRange)};
853 auto ratio = std::make_unique<RooFormulaVar>(Form("ratio(%s,%s)",ratio_num->GetName(),ratio_den->GetName()),"@0/@1",
854 RooArgList(*ratio_num,*ratio_den)) ;
855
856 ratio->addOwnedComponents(std::move(ratio_num));
857 ratio->addOwnedComponents(std::move(ratio_den));
858 ratio->setAttribute("RATIO_TERM") ;
859 return ratio ;
860}
861
862
863
864
865////////////////////////////////////////////////////////////////////////////////
866
868{
869 RooArgSet nomList ;
870
871 std::vector<std::string> rangeComps = ROOT::Split(_normRange.Data(), ",", /*skipEmpty=*/true);
872
873 std::map<std::string,RooArgSet> denListList ;
874 RooArgSet specIntDeps ;
875 string specIntRange ;
876
877 for (std::size_t i = 0; i < cache._partList.size(); i++) {
878
879 RooAbsReal *part = static_cast<RooAbsReal*>(cache._partList.at(i));
880 RooAbsReal *num = static_cast<RooAbsReal*>(cache._numList.at(i));
881 RooAbsReal *den = static_cast<RooAbsReal*>(cache._denList.at(i));
882 i++;
883
884
885 RooFormulaVar* ratio(nullptr) ;
886 RooArgSet origNumTerm ;
887
888 if (string("SPECINT")==part->getStringAttribute("PROD_TERM_TYPE")) {
889
890 RooRealIntegral* orig = static_cast<RooRealIntegral*>(num);
891 auto specratio = static_cast<RooFormulaVar const*>(&orig->integrand()) ;
892 RooProduct* func = static_cast<RooProduct*>(specratio->getParameter(0)) ;
893
894 std::unique_ptr<RooArgSet> components{orig->getComponents()};
895 for(RooAbsArg * carg : *components) {
896 if (carg->getAttribute("RATIO_TERM")) {
897 ratio = static_cast<RooFormulaVar*>(carg) ;
898 break ;
899 }
900 }
901
902 if (ratio) {
903 RooCustomizer cust(*func,"blah") ;
904 cust.replaceArg(*ratio,RooFit::RooConst(1)) ;
905 nomList.add(*cust.build()) ;
906 } else {
907 nomList.add(*func) ;
908 }
909
910
911 } else {
912
913 // Find the ratio term
914 RooAbsReal* func = num;
915 // If top level object is integral, navigate to integrand
917 func = const_cast<RooAbsReal*>(&static_cast<RooRealIntegral*>(func)->integrand());
918 }
919 if (func->InheritsFrom(RooProduct::Class())) {
920 for(RooAbsArg * arg : static_cast<RooProduct*>(func)->components()) {
921 if (arg->getAttribute("RATIO_TERM")) {
922 ratio = static_cast<RooFormulaVar*>(arg) ;
923 } else {
924 origNumTerm.add(*arg) ;
925 }
926 }
927 }
928
929 if (ratio) {
930 nomList.add(origNumTerm) ;
931 } else {
932 nomList.add(*num) ;
933 }
934
935 }
936
937 for (auto iter = rangeComps.begin() ; iter != rangeComps.end() ; ++iter) {
938 // If denominator is an integral, make a clone with the integration range adjusted to
939 // the selected component of the normalization integral
940
941 if (string("SPECINT")==part->getStringAttribute("PROD_TERM_TYPE")) {
942
943 RooRealIntegral* orig = static_cast<RooRealIntegral*>(num);
944 auto specRatio = static_cast<RooFormulaVar const*>(&orig->integrand()) ;
945 specIntDeps.add(orig->intVars()) ;
946 if (orig->intRange()) {
947 specIntRange = orig->intRange() ;
948 }
949 //RooProduct* numtmp = (RooProduct*) specRatio->getParameter(0) ;
950 RooProduct* dentmp = static_cast<RooProduct*>(specRatio->getParameter(1)) ;
951
952 for (auto* parg : static_range_cast<RooAbsReal*>(dentmp->components())) {
953 if (ratio && parg->dependsOn(*ratio)) {
954 // Make specialize ratio instance
955 std::unique_ptr<RooAbsReal> specializedRatio{specializeRatio(*(RooFormulaVar*)ratio,iter->c_str())};
956
957 // Replace generic ratio with specialized ratio
958 RooAbsArg *partCust(nullptr) ;
959 if (parg->InheritsFrom(RooAddition::Class())) {
960
961
962
963 RooAddition* tmpadd = static_cast<RooAddition*>(parg) ;
964
965 RooCustomizer cust(*tmpadd->list1().first(), ("blah_" + *iter).c_str());
966 cust.replaceArg(*ratio,*specializedRatio) ;
967 partCust = cust.build() ;
968
969 } else {
970 RooCustomizer cust(*parg, ("blah_" + *iter).c_str());
971 cust.replaceArg(*ratio, *specializedRatio);
972 partCust = cust.build();
973 }
974
975 // Print customized denominator
976
977 std::unique_ptr<RooAbsReal> specializedPartCust{specializeIntegral(*static_cast<RooAbsReal*>(partCust),iter->c_str())};
978
979 // Finally divide again by ratio
980 string name = Form("%s_divided_by_ratio",specializedPartCust->GetName()) ;
981 auto specIntFinal = std::make_unique<RooFormulaVar>(name.c_str(),"@0/@1",RooArgList(*specializedPartCust,*specializedRatio)) ;
982 specIntFinal->addOwnedComponents(std::move(specializedPartCust));
983 specIntFinal->addOwnedComponents(std::move(specializedRatio));
984
985 denListList[*iter].addOwned(std::move(specIntFinal));
986 } else {
987
988 denListList[*iter].addOwned(specializeIntegral(*parg,iter->c_str()));
989
990 }
991 }
992 } else {
993
994 if (ratio) {
995
996 std::unique_ptr<RooAbsReal> specRatio{specializeRatio(*(RooFormulaVar*)ratio,iter->c_str())};
997
998 // If integral is 'Int r(y)*g(y) dy ' then divide a posteriori by r(y)
999
1000 RooArgSet tmp(origNumTerm) ;
1001 tmp.add(*specRatio) ;
1002 const string pname = makeRGPPName("PROD",tmp,RooArgSet(),RooArgSet(),nullptr) ;
1003 auto specDenProd = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),tmp) ;
1004 std::unique_ptr<RooAbsReal> specInt;
1005
1006 if (den->InheritsFrom(RooRealIntegral::Class())) {
1007 specInt = std::unique_ptr<RooAbsReal>{specDenProd->createIntegral((static_cast<RooRealIntegral*>(den))->intVars(),iter->c_str())};
1008 specInt->addOwnedComponents(std::move(specDenProd));
1009 } else if (den->InheritsFrom(RooAddition::Class())) {
1010 RooAddition* orig = static_cast<RooAddition*>(den) ;
1011 RooRealIntegral* origInt = static_cast<RooRealIntegral*>(orig->list1().first()) ;
1012 specInt = std::unique_ptr<RooAbsReal>{specDenProd->createIntegral(origInt->intVars(),iter->c_str())};
1013 specInt->addOwnedComponents(std::move(specDenProd));
1014 } else {
1015 throw string("this should not happen") ;
1016 }
1017
1018 //RooAbsReal* specInt = specializeIntegral(*den,iter->c_str()) ;
1019 string name = Form("%s_divided_by_ratio",specInt->GetName()) ;
1020 auto specIntFinal = std::make_unique<RooFormulaVar>(name.c_str(),"@0/@1",RooArgList(*specInt,*specRatio)) ;
1021 specIntFinal->addOwnedComponents(std::move(specInt));
1022 specIntFinal->addOwnedComponents(std::move(specRatio));
1023 denListList[*iter].addOwned(std::move(specIntFinal));
1024 } else {
1025 denListList[*iter].addOwned(specializeIntegral(*den,iter->c_str()));
1026 }
1027
1028 }
1029 }
1030
1031 }
1032
1033 // Do not rearrange terms if numerator and denominator are effectively empty
1034 if (nomList.empty()) {
1035 return ;
1036 }
1037
1038 string name = std::string{GetName()} + "_numerator";
1039 // WVE FIX THIS (2)
1040
1041 std::unique_ptr<RooAbsReal> numerator = std::make_unique<RooProduct>(name.c_str(),name.c_str(),nomList) ;
1042
1043 RooArgSet products ;
1044 for (map<string,RooArgSet>::iterator iter = denListList.begin() ; iter != denListList.end() ; ++iter) {
1045 name = Form("%s_denominator_comp_%s",GetName(),iter->first.c_str()) ;
1046 // WVE FIX THIS (2)
1047 RooProduct* prod_comp = new RooProduct(name.c_str(),name.c_str(),iter->second) ;
1048 prod_comp->addOwnedComponents(std::move(iter->second));
1049 products.add(*prod_comp) ;
1050 }
1051 name = Form("%s_denominator_sum",GetName()) ;
1052 RooAbsReal* norm = new RooAddition(name.c_str(),name.c_str(),products) ;
1053 norm->addOwnedComponents(products) ;
1054
1055 if (!specIntDeps.empty()) {
1056 // Apply posterior integration required for SPECINT case
1057
1058 string namesr = Form("SPEC_RATIO(%s,%s)",numerator->GetName(),norm->GetName()) ;
1059 RooFormulaVar* ndr = new RooFormulaVar(namesr.c_str(),"@0/@1",RooArgList(*numerator,*norm)) ;
1060 ndr->addOwnedComponents(std::move(numerator));
1061
1062 // Integral of ratio
1063 numerator = std::unique_ptr<RooAbsReal>{ndr->createIntegral(specIntDeps,specIntRange.c_str())};
1064
1065 norm = static_cast<RooAbsReal*>(RooFit::RooConst(1).Clone()) ;
1066 }
1067
1068
1069
1070 // WVE DEBUG
1071 //RooMsgService::instance().debugWorkspace()->import(RooArgSet(*numerator,*norm)) ;
1072
1073 cache._rearrangedNum = std::move(numerator);
1074 cache._rearrangedDen.reset(norm);
1075 cache._isRearranged = true ;
1076
1077}
1078
1079
1080////////////////////////////////////////////////////////////////////////////////
1081
1082std::unique_ptr<RooAbsReal> RooProdPdf::specializeRatio(RooFormulaVar& input, const char* targetRangeName) const
1083{
1084 RooRealIntegral* numint = static_cast<RooRealIntegral*>(input.getParameter(0)) ;
1085 RooRealIntegral* denint = static_cast<RooRealIntegral*>(input.getParameter(1)) ;
1086
1087 std::unique_ptr<RooAbsReal> numint_spec{specializeIntegral(*numint,targetRangeName)};
1088
1089 std::unique_ptr<RooAbsReal> ret = std::make_unique<RooFormulaVar>(Form("ratio(%s,%s)",numint_spec->GetName(),denint->GetName()),"@0/@1",RooArgList(*numint_spec,*denint)) ;
1090 ret->addOwnedComponents(std::move(numint_spec));
1091
1092 return ret;
1093}
1094
1095
1096
1097////////////////////////////////////////////////////////////////////////////////
1098
1099std::unique_ptr<RooAbsReal> RooProdPdf::specializeIntegral(RooAbsReal& input, const char* targetRangeName) const
1100{
1101 if (input.InheritsFrom(RooRealIntegral::Class())) {
1102
1103 // If input is integral, recreate integral but override integration range to be targetRangeName
1104 RooRealIntegral* orig = static_cast<RooRealIntegral*>(&input) ;
1105 return std::unique_ptr<RooAbsReal>{orig->integrand().createIntegral(orig->intVars(),targetRangeName)};
1106
1107 } else if (input.InheritsFrom(RooAddition::Class())) {
1108
1109 // If input is sum of integrals, recreate integral from first component of set, but override integration range to be targetRangeName
1110 RooAddition* orig = static_cast<RooAddition*>(&input) ;
1111 RooRealIntegral* origInt = static_cast<RooRealIntegral*>(orig->list1().first()) ;
1112 return std::unique_ptr<RooAbsReal>{origInt->integrand().createIntegral(origInt->intVars(),targetRangeName)};
1113 }
1114
1115 std::stringstream errMsg;
1116 errMsg << "specializeIntegral: unknown input type " << input.ClassName() << "::" << input.GetName();
1117 throw std::runtime_error(errMsg.str());
1118}
1119
1120
1121////////////////////////////////////////////////////////////////////////////////
1122/// Group product into terms that can be calculated independently
1123
1124void RooProdPdf::groupProductTerms(std::list<std::vector<RooArgSet*>>& groupedTerms, RooArgSet& outerIntDeps,
1125 Factorized const &factorized) const
1126{
1127 // Start out with each term in its own group
1128 for(auto * term : static_range_cast<RooArgSet*>(factorized.terms)) {
1129 groupedTerms.emplace_back();
1130 groupedTerms.back().emplace_back(term) ;
1131 }
1132
1133 // Make list of imported dependents that occur in any term
1134 RooArgSet allImpDeps ;
1135 for(auto * impDeps : static_range_cast<RooArgSet*>(factorized.imps)) {
1136 allImpDeps.add(*impDeps,false) ;
1137 }
1138
1139 // Make list of integrated dependents that occur in any term
1140 RooArgSet allIntDeps ;
1141 for(auto * intDeps : static_range_cast<RooArgSet*>(factorized.ints)) {
1142 allIntDeps.add(*intDeps,false) ;
1143 }
1144
1145 outerIntDeps.removeAll() ;
1146 outerIntDeps.add(*std::unique_ptr<RooArgSet>{allIntDeps.selectCommon(allImpDeps)});
1147
1148 // Now iteratively merge groups that should be (partially) integrated together
1149 for(RooAbsArg * outerIntDep : outerIntDeps) {
1150
1151 // Collect groups that feature this dependent
1152 std::vector<RooArgSet*>* newGroup = nullptr ;
1153
1154 // Loop over groups
1155 bool needMerge = false ;
1156 auto group = groupedTerms.begin();
1157 auto nGroups = groupedTerms.size();
1158 for (size_t iGroup = 0; iGroup < nGroups; ++iGroup) {
1159
1160 // See if any term in this group depends in any ay on outerDepInt
1161 for (auto const& term2 : *group) {
1162
1163 Int_t termIdx = factorized.terms.IndexOf(term2) ;
1164 if (factorized.termNormDeps(termIdx)->contains(*outerIntDep) ||
1165 factorized.termIntDeps(termIdx)->contains(*outerIntDep) ||
1166 factorized.termImpDeps(termIdx)->contains(*outerIntDep)) {
1167 needMerge = true ;
1168 }
1169
1170 }
1171
1172 if (needMerge) {
1173 // Create composite group if not yet existing
1174 if (newGroup==nullptr) {
1175 groupedTerms.emplace_back() ;
1176 newGroup = &groupedTerms.back() ;
1177 }
1178
1179 // Add terms of this group to new term
1180 for (auto& term2 : *group) {
1181 newGroup->emplace_back(term2) ;
1182 }
1183
1184 // Remove this non-owning group from list
1185 group = groupedTerms.erase(group);
1186 } else {
1187 ++group;
1188 }
1189 }
1190
1191 }
1192}
1193
1194
1195
1196////////////////////////////////////////////////////////////////////////////////
1197/// Calculate integrals of factorized product terms over observables iset while normalized
1198/// to observables in nset.
1199
1201 const RooArgSet* term,const RooArgSet& termNSet, const RooArgSet& termISet,
1202 bool forceWrap) const
1203{
1205
1206 // CASE I: factorizing term: term is integrated over all normalizing observables
1207 // -----------------------------------------------------------------------------
1208 // Check if all observbales of this term are integrated. If so the term cancels
1209 if (!termNSet.empty() && termNSet.size()==termISet.size() && isetRangeName==nullptr) {
1210 // Term factorizes
1211 return ret ;
1212 }
1213
1214 // CASE II: Dropped terms: if term is entirely unnormalized, it should be dropped
1215 // ------------------------------------------------------------------------------
1216 if (nset && termNSet.empty()) {
1217 // Drop terms that are not asked to be normalized
1218 return ret ;
1219 }
1220
1221 if (iset && !termISet.empty()) {
1222 if (term->size()==1) {
1223
1224 // CASE IIIa: Normalized and partially integrated single PDF term
1225 //---------------------------------------------------------------
1226
1227 RooAbsPdf* pdf = static_cast<RooAbsPdf*>(term->first()) ;
1228
1229 ret.x0 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(termISet,termNSet,isetRangeName)}.release();
1230 ret.x0->setOperMode(operMode()) ;
1231 ret.x0->setStringAttribute("PROD_TERM_TYPE","IIIa") ;
1232
1233 ret.isOwned=true ;
1234
1235 // Split mode results
1236 ret.x1 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(termISet,isetRangeName)};
1237 ret.x2 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(termNSet,normRange())};
1238
1239 return ret ;
1240
1241 } else {
1242
1243 // CASE IIIb: Normalized and partially integrated composite PDF term
1244 //---------------------------------------------------------------
1245
1246 // Use auxiliary class RooGenProdProj to calculate this term
1247 const std::string name = makeRGPPName("GENPROJ_",*term,termISet,termNSet,isetRangeName) ;
1248 ret.x0 = new RooGenProdProj(name.c_str(),name.c_str(),*term,termISet,termNSet,isetRangeName) ;
1249 ret.x0->setStringAttribute("PROD_TERM_TYPE","IIIb") ;
1250 ret.x0->setOperMode(operMode()) ;
1251
1252 ret.isOwned=true ;
1253
1254 const std::string name1 = makeRGPPName("PROD",*term,RooArgSet(),RooArgSet(),nullptr) ;
1255
1256 // WVE FIX THIS
1257 RooProduct* tmp_prod = new RooProduct(name1.c_str(),name1.c_str(),*term) ;
1258
1259 ret.x1 = std::unique_ptr<RooAbsReal>{tmp_prod->createIntegral(termISet,isetRangeName)};
1260 ret.x2 = std::unique_ptr<RooAbsReal>{tmp_prod->createIntegral(termNSet,normRange())};
1261
1262 return ret ;
1263 }
1264 }
1265
1266 // CASE IVa: Normalized non-integrated composite PDF term
1267 // -------------------------------------------------------
1268 if (nset && !nset->empty() && term->size()>1) {
1269 // Composite term needs normalized integration
1270
1271 const std::string name = makeRGPPName("GENPROJ_",*term,termISet,termNSet,isetRangeName) ;
1272 ret.x0 = new RooGenProdProj(name.c_str(),name.c_str(),*term,termISet,termNSet,isetRangeName,normRange()) ;
1273 ret.x0->setExpensiveObjectCache(expensiveObjectCache()) ;
1274
1275 ret.x0->setStringAttribute("PROD_TERM_TYPE","IVa") ;
1276 ret.x0->setOperMode(operMode()) ;
1277
1278 ret.isOwned=true ;
1279
1280 const std::string name1 = makeRGPPName("PROD",*term,RooArgSet(),RooArgSet(),nullptr) ;
1281
1282 // WVE FIX THIS
1283 RooProduct* tmp_prod = new RooProduct(name1.c_str(),name1.c_str(),*term) ;
1284
1285 ret.x1 = std::unique_ptr<RooAbsReal>{tmp_prod->createIntegral(termISet,isetRangeName)};
1286 ret.x2 = std::unique_ptr<RooAbsReal>{tmp_prod->createIntegral(termNSet,normRange())};
1287
1288 return ret ;
1289 }
1290
1291 // CASE IVb: Normalized, non-integrated single PDF term
1292 // -----------------------------------------------------
1293 for (auto* pdf : static_range_cast<RooAbsPdf*>(*term)) {
1294
1295 ret.isOwned = false;
1296 RooAbsReal *ret0 = pdf;
1297
1298 if (forceWrap) {
1299 ret.isOwned = true;
1300 // Construct representative name of normalization wrapper
1301 std::string name = pdf->GetName() + ("_NORM[" + RooHelpers::getColonSeparatedNameString(termNSet, ','));
1302 name += normRange() ? ('|' + std::string{normRange()} + ']') : "]";
1303 ret0 = new RooRealIntegral(name.c_str(),name.c_str(),*pdf,RooArgSet(),&termNSet);
1304 }
1305
1306 ret0->setStringAttribute("PROD_TERM_TYPE","IVb") ;
1307 ret.x0 = ret0;
1308 ret.x1 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(RooArgSet())};
1309 ret.x2 = std::unique_ptr<RooAbsReal>{pdf->createIntegral(termNSet,normRange())};
1310 return ret;
1311 }
1312
1313 coutE(Eval) << "RooProdPdf::processProductTerm(" << GetName() << ") unidentified term!!!" << std::endl ;
1314 return ret ;
1315}
1316
1317
1318
1319
1320////////////////////////////////////////////////////////////////////////////////
1321/// Make an appropriate automatic name for a RooGenProdProj object in getPartIntList()
1322
1323std::string RooProdPdf::makeRGPPName(const char* pfx, const RooArgSet& term, const RooArgSet& iset,
1324 const RooArgSet& nset, const char* isetRangeName) const
1325{
1326 // Make an appropriate automatic name for a RooGenProdProj object in getPartIntList()
1327
1328 std::ostringstream os;
1329 os << pfx;
1330 os << "[";
1331
1332 // Encode component names
1333 bool first(true) ;
1334 for (auto const* pdf : static_range_cast<RooAbsPdf*>(term)) {
1335 if (!first) os << "_X_";
1336 first = false;
1337 os << pdf->GetName();
1338 }
1339 os << "]" << integralNameSuffix(iset,&nset,isetRangeName,true);
1340
1341 return os.str();
1342}
1343
1344
1345
1346////////////////////////////////////////////////////////////////////////////////
1347/// Force RooRealIntegral to offer all observables for internal integration
1348
1350{
1351 return true ;
1352}
1353
1354
1355
1356////////////////////////////////////////////////////////////////////////////////
1357/// Determine which part (if any) of given integral can be performed analytically.
1358/// If any analytical integration is possible, return integration scenario code.
1359///
1360/// RooProdPdf implements two strategies in implementing analytical integrals
1361///
1362/// First, PDF components whose entire set of dependents are requested to be integrated
1363/// can be dropped from the product, as they will integrate out to 1 by construction
1364///
1365/// Second, RooProdPdf queries each remaining component PDF for its analytical integration
1366/// capability of the requested set ('allVars'). It finds the largest common set of variables
1367/// that can be integrated by all remaining components. If such a set exists, it reconfirms that
1368/// each component is capable of analytically integrating the common set, and combines the components
1369/// individual integration codes into a single integration code valid for RooProdPdf.
1370
1372 const RooArgSet* normSet, const char* rangeName) const
1373{
1374 if (_forceNumInt) return 0 ;
1375
1376 // Declare that we can analytically integrate all requested observables
1377 analVars.add(allVars) ;
1378
1379 // Retrieve (or create) the required partial integral list
1380 Int_t code = getPartIntList(normSet,&allVars,rangeName);
1381
1382 return code+1 ;
1383}
1384
1385
1386
1387
1388////////////////////////////////////////////////////////////////////////////////
1389/// Return analytical integral defined by given scenario code
1390
1391double RooProdPdf::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
1392{
1393 // No integration scenario
1394 if (code==0) {
1395 return getVal(normSet) ;
1396 }
1397
1398
1399 // WVE needs adaptation for rangename feature
1400
1401 // Partial integration scenarios
1402 CacheElem* cache = static_cast<CacheElem*>(_cacheMgr.getObjByIndex(code-1)) ;
1403
1404 // If cache has been sterilized, revive this slot
1405 if (cache==nullptr) {
1406 std::unique_ptr<RooArgSet> vars{getParameters(RooArgSet())} ;
1407 RooArgSet nset = _cacheMgr.selectFromSet1(*vars, code-1) ;
1408 RooArgSet iset = _cacheMgr.selectFromSet2(*vars, code-1) ;
1409
1410 Int_t code2 = getPartIntList(&nset, &iset, rangeName) ;
1411
1412 // preceding call to getPartIntList guarantees non-null return
1413 // coverity[NULL_RETURNS]
1414 cache = static_cast<CacheElem*>(_cacheMgr.getObj(&nset,&iset,&code2,rangeName)) ;
1415 }
1416
1417 double val = calculate(*cache,true) ;
1418
1419 return val ;
1420}
1421
1422
1423
1424////////////////////////////////////////////////////////////////////////////////
1425/// If this product contains exactly one extendable p.d.f return the extension abilities of
1426/// that p.d.f, otherwise return CanNotBeExtended
1427
1429{
1430 return (_extendedIndex>=0) ? (static_cast<RooAbsPdf*>(_pdfList.at(_extendedIndex)))->extendMode() : CanNotBeExtended ;
1431}
1432
1433
1434
1435////////////////////////////////////////////////////////////////////////////////
1436/// Return the expected number of events associated with the extendable input PDF
1437/// in the product. If there is no extendable term, abort.
1438
1439double RooProdPdf::expectedEvents(const RooArgSet* nset) const
1440{
1441 if (_extendedIndex<0) {
1442 coutF(Generation) << "Requesting expected number of events from a RooProdPdf that does not contain an extended p.d.f" << std::endl ;
1443 throw std::logic_error(std::string("RooProdPdf ") + GetName() + " could not be extended.");
1444 }
1445
1446 return static_cast<RooAbsPdf*>(_pdfList.at(_extendedIndex))->expectedEvents(nset) ;
1447}
1448
1449std::unique_ptr<RooAbsReal> RooProdPdf::createExpectedEventsFunc(const RooArgSet* nset) const
1450{
1451 if (_extendedIndex<0) {
1452 coutF(Generation) << "Requesting expected number of events from a RooProdPdf that does not contain an extended p.d.f" << std::endl ;
1453 throw std::logic_error(std::string("RooProdPdf ") + GetName() + " could not be extended.");
1454 }
1455
1456 return static_cast<RooAbsPdf*>(_pdfList.at(_extendedIndex))->createExpectedEventsFunc(nset);
1457}
1458
1459
1460////////////////////////////////////////////////////////////////////////////////
1461/// Return generator context optimized for generating events from product p.d.f.s
1462
1464 const RooArgSet* auxProto, bool verbose) const
1465{
1466 if (_useDefaultGen) return RooAbsPdf::genContext(vars,prototype,auxProto,verbose) ;
1467 return new RooProdGenContext(*this,vars,prototype,auxProto,verbose) ;
1468}
1469
1470
1471
1472////////////////////////////////////////////////////////////////////////////////
1473/// Query internal generation capabilities of component p.d.f.s and aggregate capabilities
1474/// into master configuration passed to the generator context
1475
1476Int_t RooProdPdf::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, bool staticInitOK) const
1477{
1478 if (!_useDefaultGen) return 0 ;
1479
1480 // Find the subset directVars that only depend on a single PDF in the product
1481 RooArgSet directSafe ;
1482 for (auto const* arg : directVars) {
1483 if (isDirectGenSafe(*arg)) directSafe.add(*arg) ;
1484 }
1485
1486
1487 // Now find direct integrator for relevant components ;
1488 std::vector<Int_t> code;
1489 code.reserve(64);
1490 for (auto const* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1491 RooArgSet pdfDirect ;
1492 Int_t pdfCode = pdf->getGenerator(directSafe,pdfDirect,staticInitOK);
1493 code.push_back(pdfCode);
1494 if (pdfCode != 0) {
1495 generateVars.add(pdfDirect) ;
1496 }
1497 }
1498
1499
1500 if (!generateVars.empty()) {
1501 auto found = std::find(_genCode.begin(), _genCode.end(), code);
1502 // If a generator for the codes was already cached, return the index to the
1503 // corresponding caching index plus one.
1504 if (found != _genCode.end()) {
1505 return std::distance(_genCode.begin(), found) + 1;
1506 }
1507 _genCode.emplace_back(std::move(code));
1508 return _genCode.size();
1509 }
1510 return 0;
1511}
1512
1513
1514
1515////////////////////////////////////////////////////////////////////////////////
1516/// Forward one-time initialization call to component generation initialization
1517/// methods.
1518
1520{
1521 if (!_useDefaultGen) return ;
1522
1523 const std::vector<int>& codeList = _genCode[code-1];
1524 Int_t i(0) ;
1525 for (auto* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1526 if (codeList[i]!=0) {
1527 pdf->initGenerator(codeList[i]) ;
1528 }
1529 i++ ;
1530 }
1531}
1532
1533
1534
1535////////////////////////////////////////////////////////////////////////////////
1536/// Generate a single event with configuration specified by 'code'
1537/// Defer internal generation to components as encoded in the _genCode
1538/// registry for given generator code.
1539
1541{
1542 if (!_useDefaultGen) return ;
1543
1544 const std::vector<Int_t>& codeList = _genCode[code-1];
1545 Int_t i(0) ;
1546 for (auto* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1547 if (codeList[i]!=0) {
1548 pdf->generateEvent(codeList[i]) ;
1549 }
1550 i++ ;
1551 }
1552}
1553
1554
1555
1556////////////////////////////////////////////////////////////////////////////////
1557/// Return RooAbsArg components contained in the cache
1558
1560{
1561 RooArgList ret ;
1562 ret.add(_partList) ;
1563 ret.add(_numList) ;
1564 ret.add(_denList) ;
1565 if (_rearrangedNum) ret.add(*_rearrangedNum) ;
1566 if (_rearrangedDen) ret.add(*_rearrangedDen) ;
1567 return ret ;
1568
1569}
1570
1571
1572
1573////////////////////////////////////////////////////////////////////////////////
1574/// Hook function to print cache contents in tree printing of RooProdPdf
1575
1576void RooProdPdf::CacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
1577{
1578 if (curElem==0) {
1579 os << indent << "RooProdPdf begin partial integral cache" << std::endl ;
1580 }
1581
1582 auto indent2 = std::string(indent) + "[" + std::to_string(curElem) + "]";
1583 for(auto const& arg : _partList) {
1584 arg->printCompactTree(os,indent2.c_str()) ;
1585 }
1586
1587 if (curElem==maxElem) {
1588 os << indent << "RooProdPdf end partial integral cache" << std::endl ;
1589 }
1590}
1591
1592
1593
1594////////////////////////////////////////////////////////////////////////////////
1595/// Forward determination of safety of internal generator code to
1596/// component p.d.f that would generate the given observable
1597
1599{
1600 // Only override base class behaviour if default generator method is enabled
1601 if (!_useDefaultGen) return RooAbsPdf::isDirectGenSafe(arg) ;
1602
1603 // Argument may appear in only one PDF component
1604 RooAbsPdf* thePdf(nullptr) ;
1605 for (auto* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1606
1607 if (pdf->dependsOn(arg)) {
1608 // Found PDF depending on arg
1609
1610 // If multiple PDFs depend on arg directGen is not safe
1611 if (thePdf) return false ;
1612
1613 thePdf = pdf ;
1614 }
1615 }
1616 // Forward call to relevant component PDF
1617 return thePdf?(thePdf->isDirectGenSafe(arg)):false ;
1618}
1619
1620
1621
1622////////////////////////////////////////////////////////////////////////////////
1623/// Look up user specified normalization set for given input PDF component
1624
1626{
1627 Int_t idx = _pdfList.index(&pdf);
1628 return idx < 0 ? nullptr : _pdfNSetList[idx].get();
1629}
1630
1631
1632
1633/// Add some full PDFs to the factors of this RooProdPdf.
1635{
1636 size_t numExtended = (_extendedIndex==-1) ? 0 : 1;
1637
1638 for(auto arg : pdfs) {
1639 RooAbsPdf* pdf = dynamic_cast<RooAbsPdf*>(arg);
1640 if (!pdf) {
1641 coutW(InputArguments) << "RooProdPdf::addPdfs(" << GetName() << ") list arg "
1642 << arg->GetName() << " is not a PDF, ignored" << std::endl ;
1643 continue;
1644 }
1645 if(pdf->canBeExtended()) {
1646 if (_extendedIndex == -1) {
1648 } else {
1649 numExtended++;
1650 }
1651 }
1652 _pdfList.add(*pdf);
1653 _pdfNSetList.emplace_back(std::make_unique<RooArgSet>("nset"));
1654 }
1655
1656 // Protect against multiple extended terms
1657 if (numExtended>1) {
1658 coutW(InputArguments) << "RooProdPdf::addPdfs(" << GetName()
1659 << ") WARNING: multiple components with extended terms detected,"
1660 << " product will not be extendable." << std::endl ;
1661 _extendedIndex = -1 ;
1662 }
1663
1664 // Reset cache
1665 _cacheMgr.reset() ;
1666
1667}
1668
1669/// Remove some PDFs from the factors of this RooProdPdf.
1671{
1672 // Remember what the extended PDF is
1673 RooAbsArg const* extPdf = _extendedIndex >= 0 ? &_pdfList[_extendedIndex] : nullptr;
1674
1675 // Actually remove the PDFs and associated nsets
1676 for(size_t i=0;i < _pdfList.size(); i++) {
1677 if(pdfs.contains(_pdfList[i])) {
1679 _pdfNSetList.erase(_pdfNSetList.begin()+i);
1680 i--;
1681 }
1682 }
1683
1684 // Since we may have removed PDFs from the list, the index of the extended
1685 // PDF in the list needs to be updated. The new index might also be -1 if the
1686 // extended PDF got removed.
1687 if(extPdf) {
1688 _extendedIndex = _pdfList.index(*extPdf);
1689 }
1690
1691 // Reset cache
1692 _cacheMgr.reset() ;
1693}
1694
1695
1696namespace {
1697
1698std::vector<TNamed const*> sortedNamePtrs(RooAbsCollection const& col)
1699{
1700 std::vector<TNamed const*> ptrs;
1701 ptrs.reserve(col.size());
1702 for(RooAbsArg* arg : col) {
1703 ptrs.push_back(arg->namePtr());
1704 }
1705 std::sort(ptrs.begin(), ptrs.end());
1706 return ptrs;
1707}
1708
1709bool sortedNamePtrsOverlap(std::vector<TNamed const*> const& ptrsA, std::vector<TNamed const*> const& ptrsB)
1710{
1711 auto pA = ptrsA.begin();
1712 auto pB = ptrsB.begin();
1713 while (pA != ptrsA.end() && pB != ptrsB.end()) {
1714 if (*pA < *pB) {
1715 ++pA;
1716 } else if (*pB < *pA) {
1717 ++pB;
1718 } else {
1719 return true;
1720 }
1721 }
1722 return false;
1723}
1724
1725} // namespace
1726
1727
1728////////////////////////////////////////////////////////////////////////////////
1729/// Return all parameter constraint p.d.f.s on parameters listed in constrainedParams.
1730/// The observables set is required to distinguish unambiguously p.d.f in terms
1731/// of observables and parameters, which are not constraints, and p.d.fs in terms
1732/// of parameters only, which can serve as constraints p.d.f.s
1733/// The pdfParams output parameter communicates to the caller which parameter
1734/// are used in the pdfs that are not constraints.
1735
1736std::unique_ptr<RooArgSet>
1737RooProdPdf::getConstraints(const RooArgSet &observables, RooArgSet const &constrainedParams, RooArgSet &pdfParams) const
1738{
1739 auto constraints = std::make_unique<RooArgSet>("constraints");
1740
1741 // For the optimized implementation of checking if two collections overlap by name.
1742 auto observablesNamePtrs = sortedNamePtrs(observables);
1743 auto constrainedParamsNamePtrs = sortedNamePtrs(constrainedParams);
1744
1745 // Loop over PDF components
1746 for (std::size_t iPdf = 0; iPdf < _pdfList.size(); ++iPdf) {
1747 auto * pdf = static_cast<RooAbsPdf*>(&_pdfList[iPdf]);
1748
1749 RooArgSet tmp;
1750 pdf->getParameters(nullptr, tmp);
1751
1752 // A constraint term is a p.d.f that doesn't contribute to the
1753 // expectedEvents() and does not depend on any of the listed observables
1754 // but does depends on any of the parameters that should be constrained
1755 bool isConstraint = false;
1756
1757 if(static_cast<int>(iPdf) != _extendedIndex) {
1758 auto tmpNamePtrs = sortedNamePtrs(tmp);
1759 // Before, there were calls to `pdf->dependsOn()` here, but they were very
1760 // expensive for large computation graphs! Given that we have to traverse
1761 // the computation graph with a call to `pdf->getParameters()` anyway, we
1762 // can just check if the set of all variables operlaps with the observables
1763 // or constraind parameters.
1764 //
1765 // We are using an optimized implementation of overlap checking. Because
1766 // the overlap is checked by name, we can check overlap of the
1767 // corresponding name pointers. The optimization can't be in
1768 // RooAbsCollection itself, because it is crucial that the memory for the
1769 // non-tmp name pointers is not reallocated for each pdf.
1770 isConstraint = !sortedNamePtrsOverlap(tmpNamePtrs, observablesNamePtrs) &&
1771 sortedNamePtrsOverlap(tmpNamePtrs, constrainedParamsNamePtrs);
1772 }
1773 if (isConstraint) {
1774 constraints->add(*pdf) ;
1775 } else {
1776 // We only want to add parameter, not observables. Since a call like
1777 // `pdf->getParameters(&observables)` would be expensive, we take the set
1778 // of all variables and remove the ovservables, which is much cheaper. In
1779 // a call to `pdf->getParameters(&observables)`, the observables are
1780 // matched by name, so we have to pass the `matchByNameOnly` here.
1781 tmp.remove(observables, /*silent=*/false, /*matchByNameOnly=*/true);
1782 pdfParams.add(tmp,true) ;
1783 }
1784 }
1785
1786 return constraints;
1787}
1788
1789
1790
1791
1792////////////////////////////////////////////////////////////////////////////////
1793/// Return all parameter constraint p.d.f.s on parameters listed in constrainedParams.
1794/// The observables set is required to distinguish unambiguously p.d.f in terms
1795/// of observables and parameters, which are not constraints, and p.d.fs in terms
1796/// of parameters only, which can serve as constraints p.d.f.s
1797
1799{
1800 RooArgSet* connectedPars = new RooArgSet("connectedPars") ;
1801 for (std::size_t iPdf = 0; iPdf < _pdfList.size(); ++iPdf) {
1802 auto * pdf = static_cast<RooAbsPdf*>(&_pdfList[iPdf]);
1803 // Check if term is relevant, either because it provides a propablity
1804 // density in the observables or because it is used for the expected
1805 // events.
1806 if (static_cast<int>(iPdf) == _extendedIndex || pdf->dependsOn(observables)) {
1807 RooArgSet tmp;
1808 pdf->getParameters(&observables, tmp);
1809 connectedPars->add(tmp) ;
1810 }
1811 }
1812 return connectedPars ;
1813}
1814
1815////////////////////////////////////////////////////////////////////////////////
1816/// Interface function used by test statistics to freeze choice of range
1817/// for interpretation of conditional product terms
1818
1819void RooProdPdf::selectNormalizationRange(const char* rangeName, bool force)
1820{
1821 if (!force && _refRangeName) {
1822 return ;
1823 }
1824
1825 fixRefRange(rangeName) ;
1826}
1827
1828
1829
1830
1831////////////////////////////////////////////////////////////////////////////////
1832
1833void RooProdPdf::fixRefRange(const char* rangeName)
1834{
1835 _refRangeName = const_cast<TNamed*>(RooNameReg::ptr(rangeName));
1836}
1837
1838
1839
1840////////////////////////////////////////////////////////////////////////////////
1841/// Forward the plot sampling hint from the p.d.f. that defines the observable obs
1842
1843std::list<double>* RooProdPdf::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
1844{
1845 for (auto const* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1846 if (std::list<double>* hint = pdf->plotSamplingHint(obs,xlo,xhi)) {
1847 return hint ;
1848 }
1849 }
1850
1851 return nullptr;
1852}
1853
1854
1855
1856////////////////////////////////////////////////////////////////////////////////
1857/// If all components that depend on obs are binned that so is the product
1858
1860{
1861 for (auto const* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1862 if (pdf->dependsOn(obs) && !pdf->isBinnedDistribution(obs)) {
1863 return false ;
1864 }
1865 }
1866
1867 return true ;
1868}
1869
1870
1871
1872
1873
1874
1875////////////////////////////////////////////////////////////////////////////////
1876/// Forward the plot sampling hint from the p.d.f. that defines the observable obs
1877
1878std::list<double>* RooProdPdf::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const
1879{
1880 for (auto const* pdf : static_range_cast<RooAbsPdf*>(_pdfList)) {
1881 if (std::list<double>* hint = pdf->binBoundaries(obs,xlo,xhi)) {
1882 return hint ;
1883 }
1884 }
1885
1886 return nullptr;
1887}
1888
1889
1890
1891////////////////////////////////////////////////////////////////////////////////
1892/// Customized printing of arguments of a RooProdPdf to more intuitively reflect the contents of the
1893/// product operator construction
1894
1895void RooProdPdf::printMetaArgs(ostream& os) const
1896{
1897 for (std::size_t i=0 ; i<_pdfList.size() ; i++) {
1898 if (i>0) os << " * " ;
1899 RooArgSet* ncset = _pdfNSetList[i].get() ;
1900 os << _pdfList.at(i)->GetName() ;
1901 if (!ncset->empty()) {
1902 if (string("nset")==ncset->GetName()) {
1903 os << *ncset ;
1904 } else {
1905 os << "|" ;
1906 bool first(true) ;
1907 for (auto const* arg : *ncset) {
1908 if (!first) {
1909 os << "," ;
1910 } else {
1911 first = false ;
1912 }
1913 os << arg->GetName() ;
1914 }
1915 }
1916 }
1917 }
1918 os << " " ;
1919}
1920
1921
1922
1923////////////////////////////////////////////////////////////////////////////////
1924/// Implement support for node removal
1925
1926bool RooProdPdf::redirectServersHook(const RooAbsCollection& newServerList, bool mustReplaceAll, bool nameChange, bool isRecursive)
1927{
1928 if (nameChange && _pdfList.find("REMOVAL_DUMMY")) {
1929
1930 cxcoutD(LinkStateMgmt) << "RooProdPdf::redirectServersHook(" << GetName() << "): removing REMOVAL_DUMMY" << std::endl ;
1931
1932 // Remove node from _pdfList proxy and remove corresponding entry from normset list
1933 RooAbsArg* pdfDel = _pdfList.find("REMOVAL_DUMMY") ;
1934
1935 _pdfNSetList.erase(_pdfNSetList.begin() + _pdfList.index("REMOVAL_DUMMY")) ;
1936 _pdfList.remove(*pdfDel) ;
1937
1938 // Clear caches
1939 _cacheMgr.reset() ;
1940 }
1941
1942 // If the replaced server is an observable that is used in any of the
1943 // normalization sets for conditional fits, replace the element in the
1944 // normalization set too.
1945 for(std::unique_ptr<RooArgSet> const& normSet : _pdfNSetList) {
1946 for(RooAbsArg * arg : *normSet) {
1947 if(RooAbsArg * newArg = arg->findNewServer(newServerList, nameChange)) {
1948 // Since normSet is owning, the original arg is now deleted.
1949 normSet->replace(arg, std::unique_ptr<RooAbsArg>{newArg->cloneTree()});
1950 }
1951 }
1952 }
1953
1954 return RooAbsPdf::redirectServersHook(newServerList, mustReplaceAll, nameChange, isRecursive);
1955}
1956
1957void RooProdPdf::CacheElem::writeToStream(std::ostream& os) const {
1958 using namespace RooHelpers;
1959 os << "_partList\n";
1960 os << getColonSeparatedNameString(_partList) << "\n";
1961 os << "_numList\n";
1962 os << getColonSeparatedNameString(_numList) << "\n";
1963 os << "_denList\n";
1964 os << getColonSeparatedNameString(_denList) << "\n";
1965 os << "_ownedList\n";
1966 os << getColonSeparatedNameString(_ownedList) << "\n";
1967 os << "_normList\n";
1968 for(auto const& set : _normList) {
1969 os << getColonSeparatedNameString(*set) << "\n";
1970 }
1971 os << "_isRearranged" << "\n";
1972 os << _isRearranged << "\n";
1973 os << "_rearrangedNum" << "\n";
1974 if(_rearrangedNum) {
1975 os << getColonSeparatedNameString(*_rearrangedNum) << "\n";
1976 } else {
1977 os << "nullptr" << "\n";
1978 }
1979 os << "_rearrangedDen" << "\n";
1980 if(_rearrangedDen) {
1981 os << getColonSeparatedNameString(*_rearrangedDen) << "\n";
1982 } else {
1983 os << "nullptr" << "\n";
1984 }
1985}
1986
1987std::unique_ptr<RooArgSet> RooProdPdf::fillNormSetForServer(RooArgSet const &normSet, RooAbsArg const &server) const
1988{
1989 if (normSet.empty())
1990 return nullptr;
1991 auto *pdfNset = findPdfNSet(static_cast<RooAbsPdf const &>(server));
1992 if (pdfNset && !pdfNset->empty()) {
1993 std::unique_ptr<RooArgSet> out;
1994 if (0 == strcmp("cset", pdfNset->GetName())) {
1995 // If the name of the normalization set is "cset", it doesn't contain the
1996 // normalization set but the conditional observables that should *not* be
1997 // normalized over.
1998 out = std::make_unique<RooArgSet>(normSet);
1999 RooArgSet common;
2000 out->selectCommon(*pdfNset, common);
2001 out->remove(common);
2002 } else {
2003 out = std::make_unique<RooArgSet>(*pdfNset);
2004 }
2005 // prefix also the arguments in the normSets if they have not already been
2006 if (auto prefix = getStringAttribute("__prefix__")) {
2007 for (RooAbsArg *arg : *out) {
2008 if (!arg->getStringAttribute("__prefix__")) {
2009 arg->SetName((std::string(prefix) + arg->GetName()).c_str());
2010 arg->setStringAttribute("__prefix__", prefix);
2011 }
2012 }
2013 }
2014 return out;
2015 } else {
2016 return nullptr;
2017 }
2018}
2019
2020std::unique_ptr<RooAbsArg>
2022{
2023 if (ctx.likelihoodMode()) {
2024 auto binnedInfo = RooHelpers::getBinnedL(*this);
2025 if (binnedInfo.binnedPdf && binnedInfo.binnedPdf != this) {
2026 return binnedInfo.binnedPdf->compileForNormSet(normSet, ctx);
2027 }
2028 }
2029
2030 std::unique_ptr<RooProdPdf> prodPdfClone{static_cast<RooProdPdf *>(this->Clone())};
2031 ctx.markAsCompiled(*prodPdfClone);
2032
2033 // If this RooProdPdf has a normalization range (e.g. the fit range in a
2034 // ranged fit), propagate it to the component pdfs while they are compiled,
2035 // so that a nested RooAddPdf reinterprets its coefficients with respect to
2036 // the full range exactly like a top-level RooAddPdf would. Otherwise, a
2037 // RooProdPdf wrapping an extended RooAddPdf (the common way to attach
2038 // constraint terms) gives a different yield than the bare RooAddPdf in a
2039 // ranged fit (GitHub issue #16673).
2040 //
2041 // This must not change how constraint terms are normalized, so the range is
2042 // only set on components whose normalization observables define it.
2043 // Constraint pdfs are normalized over the nuisance parameters, which don't
2044 // define the fit range, so they are skipped. Forcing the range on them would
2045 // be wrong, and for a multi-range fit it even throws, because each undefined
2046 // sub-range falls back to the parameter's full range and the sub-ranges then
2047 // overlap.
2048 std::vector<std::pair<RooAbsPdf *, std::string>> restoreNormRanges;
2049 if (const char *prodNormRange = normRange()) {
2050 std::vector<std::string> rangeTokens = ROOT::Split(prodNormRange, ",", /*skipEmpty=*/true);
2051 for (auto *pdf : static_range_cast<RooAbsPdf *>(prodPdfClone->_pdfList)) {
2052 RooArgSet pdfObs;
2053 pdf->getObservables(&normSet, pdfObs);
2054 bool rangeDefined = !pdfObs.empty();
2055 for (RooAbsArg *obs : pdfObs) {
2056 auto *lval = dynamic_cast<RooAbsRealLValue *>(obs);
2057 for (auto const &token : rangeTokens) {
2058 if (!lval || !lval->hasRange(token.c_str())) {
2059 rangeDefined = false;
2060 }
2061 }
2062 }
2063 if (rangeDefined && std::string(pdf->normRange() ? pdf->normRange() : "") != prodNormRange) {
2064 restoreNormRanges.emplace_back(pdf, pdf->normRange() ? pdf->normRange() : "");
2065 pdf->setNormRange(prodNormRange);
2066 }
2067 }
2068 }
2069
2070 // Pdfs with overlapping normalization observables end up in the same term,
2071 // which a RooGenProdProj normalizes as a whole. Like in the scalar
2072 // evaluation, it should integrate the unnormalized pdfs. The RooGenProdProj
2073 // clones its pdfs, and the clone of a RooNormalizedPdf owns a copy of the
2074 // normalization integral that the RooFit::Evaluator doesn't keep up to
2075 // date. Also, the integrals of normalized pdfs are smaller, so the absolute
2076 // tolerance of the numerical integration costs more precision.
2077 std::unordered_set<std::string> jointlyNormalized;
2078 if (!normSet.empty()) {
2079 Factorized factorized;
2080 prodPdfClone->factorizeProduct(normSet, RooArgSet{}, factorized);
2081 for (auto *term : static_range_cast<RooArgSet *>(factorized.terms)) {
2082 if (term->size() > 1) {
2083 for (RooAbsArg *pdf : *term) {
2084 jointlyNormalized.insert(pdf->GetName());
2085 }
2086 }
2087 }
2088 }
2089
2090 for (const auto server : prodPdfClone->servers()) {
2091 auto nsetForServer = fillNormSetForServer(normSet, *server);
2092 RooArgSet const &nset = nsetForServer ? *nsetForServer : normSet;
2093
2094 RooArgSet depList;
2095 server->getObservables(&nset, depList);
2096
2097 ctx.compileServer(*server, *prodPdfClone, depList);
2098 }
2099
2100 for (auto const &[pdf, oldRange] : restoreNormRanges) {
2101 pdf->setNormRange(oldRange.empty() ? nullptr : oldRange.c_str());
2102 }
2103
2104 // The computation graph is created from a shallow clone with the jointly
2105 // normalized pdfs unwrapped. The RooFixedProdPdf forwards analytical
2106 // integrals to prodPdfClone, which keeps them wrapped so that these
2107 // integrals match the normalized value, e.g. in a nested RooProdPdf.
2108 std::unordered_map<RooAbsArg *, RooAbsArg *> unwrapped;
2109 for (RooAbsArg *pdf : prodPdfClone->_pdfList) {
2110 auto *normalized = dynamic_cast<RooFit::Detail::RooNormalizedPdf *>(pdf);
2111 if (normalized && jointlyNormalized.count(normalized->pdf().GetName())) {
2112 unwrapped[normalized] = const_cast<RooAbsPdf *>(&normalized->pdf());
2113 }
2114 }
2115 std::unique_ptr<RooProdPdf> cacheProdPdf;
2116 if (!unwrapped.empty()) {
2117 cacheProdPdf.reset(static_cast<RooProdPdf *>(prodPdfClone->Clone()));
2118 cacheProdPdf->redirectServers(unwrapped);
2119 }
2120
2121 // The RooFixedProdPdf keeps the normalization set around to create its
2122 // cache elements and to evaluate the expected number of events. It must
2123 // refer to the observables in the compiled computation graph, because the
2124 // original ones can get out of sync with it: RooSimultaneous prefixes the
2125 // observable names of each channel after compilation, and the stale
2126 // observables would then not be found anymore by name.
2127 auto fixedProdPdf = std::make_unique<RooFit::Detail::RooFixedProdPdf>(
2128 std::move(prodPdfClone), ctx.mapToCompiled(normSet), cacheProdPdf.get());
2129 ctx.markAsCompiled(*fixedProdPdf);
2130
2131 return fixedProdPdf;
2132}
2133
2134namespace RooFit::Detail {
2135
2136/// \param[in] prodPdf The product with the fixed normalization set.
2137/// \param[in] normSet The normalization set.
2138/// \param[in] cacheProdPdf If not null, the cache elements that make up the
2139/// computation graph are created from this product instead of
2140/// `prodPdf`. It must have the same observables and the same pdfs,
2141/// but they may be normalized differently.
2142RooFixedProdPdf::RooFixedProdPdf(std::unique_ptr<RooProdPdf> &&prodPdf, RooArgSet const &normSet,
2143 RooProdPdf const *cacheProdPdf)
2144 : RooAbsPdf(prodPdf->GetName(), prodPdf->GetTitle()),
2145 _normSet{normSet},
2146 _servers("!servers", "List of servers", this),
2147 _prodPdf{std::move(prodPdf)}
2148{
2149 auto cache = (cacheProdPdf ? cacheProdPdf : _prodPdf.get())->createCacheElem(&_normSet, nullptr);
2150 _isRearranged = cache->_isRearranged;
2151
2152 // We don't want to carry the full cache object around, so we let it go out
2153 // of scope and transfer the ownership of the args that we actually need.
2154 cache->_ownedList.releaseOwnership();
2155 cache->_numList.releaseOwnership();
2156 cache->_denList.releaseOwnership();
2157 addOwnedComponents(cache->_ownedList);
2158 addOwnedComponents(cache->_numList);
2159 addOwnedComponents(cache->_denList);
2160
2161 // The actual servers for a given normalization set depend on whether the
2162 // cache is rearranged or not. See RooProdPdf::calculate to see
2163 // which args in the cache are used directly.
2164 if (_isRearranged) {
2165 _servers.add(*cache->_rearrangedNum);
2166 _servers.add(*cache->_rearrangedDen);
2167 addOwnedComponents(std::move(cache->_rearrangedNum));
2168 addOwnedComponents(std::move(cache->_rearrangedDen));
2169 return;
2170 }
2171 for (RooAbsArg *arg : cache->_partList) {
2172 _servers.add(*arg);
2173 }
2174}
2175
2177 : RooAbsPdf(other, name),
2178 _normSet{other._normSet},
2179 _servers("!servers", this, other._servers),
2180 _prodPdf{static_cast<RooProdPdf *>(other._prodPdf->Clone())},
2181 _isRearranged{other._isRearranged}
2182{
2183}
2184
2185////////////////////////////////////////////////////////////////////////////////
2186/// Evaluate product of PDFs in batch mode.
2187
2189{
2190 if (_isRearranged) {
2191 auto numerator = ctx.at(rearrangedNum());
2192 auto denominator = ctx.at(rearrangedDen());
2193 RooBatchCompute::compute(ctx.config(this), RooBatchCompute::Ratio, ctx.output(), {numerator, denominator});
2194 return;
2195 }
2196 std::vector<std::span<const double>> factors;
2197 factors.reserve(partList()->size());
2198 if (ctx.outputsLogarithm(this)) {
2199 // The factors that provide their logarithms go first.
2200 for (const RooAbsArg *arg : *partList()) {
2201 if (ctx.outputsLogarithm(arg))
2202 factors.push_back(ctx.at(arg));
2203 }
2204 std::array<double, 1> nLogFactors{static_cast<double>(factors.size())};
2205 for (const RooAbsArg *arg : *partList()) {
2206 if (!ctx.outputsLogarithm(arg))
2207 factors.push_back(ctx.at(arg));
2208 }
2209 RooBatchCompute::compute(ctx.config(this), RooBatchCompute::LogProdPdf, ctx.output(), factors, nLogFactors);
2210 return;
2211 }
2212 for (const RooAbsArg *arg : *partList()) {
2213 auto span = ctx.at(arg);
2214 factors.push_back(span);
2215 }
2216 std::array<double, 1> special{static_cast<double>(factors.size())};
2217 RooBatchCompute::compute(ctx.config(this), RooBatchCompute::ProdPdf, ctx.output(), factors, special);
2218}
2219
2221{
2222 if (_isRearranged) {
2223 return rearrangedNum()->getVal() / rearrangedDen()->getVal();
2224 }
2225 double value = 1.0;
2226
2227 for (auto *arg : static_range_cast<RooAbsReal *>(*partList())) {
2228 value *= arg->getVal();
2229 }
2230 return value;
2231}
2232
2233} // namespace RooFit::Detail
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define cxcoutD(a)
#define coutW(a)
#define dologD(a)
#define coutF(a)
#define coutE(a)
static void indent(ostringstream &buf, int indent_level)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
RooExpensiveObjectCache & expensiveObjectCache() const
void treeNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=nullptr, bool doBranch=true, bool doLeaf=true, bool valueOnly=false, bool recurseFundamental=false) const
Fill supplied list with nodes of the arg tree, following all server links, starting with ourself as t...
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...
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
const Text_t * getStringAttribute(const Text_t *key) const
Get string attribute mapped under key 'key'.
bool redirectServers(const RooAbsCollection &newServerList, bool mustReplaceAll=false, bool nameChange=false, bool isRecursionStep=false)
Replace all direct servers of this object with the new servers in newServerList.
friend class RooRealIntegral
Definition RooAbsArg.h:557
RooFit::OwningPtr< RooArgSet > getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
TObject * Clone(const char *newname=nullptr) const override
Make a clone of an object using the Streamer facility.
Definition RooAbsArg.h:89
RooAbsArg * findNewServer(const RooAbsCollection &newSet, bool nameChange) const
Find the new server in the specified set that matches the old server.
OperMode operMode() const
Query the operation mode of this node.
Definition RooAbsArg.h:400
Abstract container object that can hold multiple RooAbsArg objects.
bool contains(const char *name) const
Check if collection contains an argument with a specific name.
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.
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in the collection.
bool overlaps(Iterator_t otherCollBegin, Iterator_t otherCollEnd) const
Storage_t::size_type size() const
const_iterator begin() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for generator contexts of RooAbsPdf objects.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
TString _normRange
Normalization range.
Definition RooAbsPdf.h:336
virtual bool isDirectGenSafe(const RooAbsArg &arg) const
Check if given observable can be safely generated using the pdfs internal generator mechanism (if tha...
RooArgSet const * _normSet
! Normalization set with for above integral
Definition RooAbsPdf.h:314
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:214
@ CanNotBeExtended
Definition RooAbsPdf.h:208
const char * normRange() const
Definition RooAbsPdf.h:246
bool redirectServersHook(const RooAbsCollection &newServerList, bool mustReplaceAll, bool nameChange, bool isRecursiveStep) override
Hook function intercepting redirectServer calls.
virtual RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=nullptr, const RooArgSet *auxProto=nullptr, bool verbose=false) const
Interface function to create a generator context from a p.d.f.
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...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
bool _forceNumInt
Force numerical integration if flag set.
Definition RooAbsReal.h:545
TString integralNameSuffix(const RooArgSet &iset, const RooArgSet *nset=nullptr, const char *rangeName=nullptr, bool omitEmpty=false) const
Construct string with unique suffix name to give to integral object that encodes integrated observabl...
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 ...
Calculates the sum of a set of RooAbsReal terms, or when constructed with two sets,...
Definition RooAddition.h:27
static TClass * Class()
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:159
RooArgSet * selectCommon(const RooAbsCollection &refColl) const
Use RooAbsCollection::selecCommon(), but return as RooArgSet.
Definition RooArgSet.h:154
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...
void reset()
Clear the cache.
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.
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
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...
bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false) override
Remove object 'var' from set and deregister 'var' as server to owner.
TObject * clone(const char *newname=nullptr) const override
Definition RooConstVar.h:29
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Container class to hold unbinned data.
Definition RooDataSet.h:32
void markAsCompiled(RooAbsArg &arg) const
A RooProdPdf with a fixed normalization set can be replaced by this class.
Definition RooProdPdf.h:212
RooArgSet const * partList() const
Definition RooProdPdf.h:270
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void doEval(RooFit::EvalContext &ctx) const override
Evaluate product of PDFs in batch mode.
std::unique_ptr< RooProdPdf > _prodPdf
Definition RooProdPdf.h:277
RooAbsReal const * rearrangedDen() const
Definition RooProdPdf.h:265
RooFixedProdPdf(std::unique_ptr< RooProdPdf > &&prodPdf, RooArgSet const &normSet, RooProdPdf const *cacheProdPdf=nullptr)
RooAbsReal const * rearrangedNum() const
Definition RooProdPdf.h:261
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Collection class for internal use, storing a collection of RooAbsArg pointers in a doubly linked list...
void Delete(Option_t *o=nullptr) override
Remove all elements in collection and delete all elements NB: Collection does not own elements,...
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition RooNameReg.h:39
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
RooArgList containedArgs(Action) override
Return RooAbsArg components contained in the cache.
std::unique_ptr< RooAbsReal > _rearrangedNum
Definition RooProdPdf.h:116
void printCompactTreeHook(std::ostream &, const char *, Int_t, Int_t) override
Hook function to print cache contents in tree printing of RooProdPdf.
std::vector< std::unique_ptr< RooArgSet > > _normList
Definition RooProdPdf.h:114
std::unique_ptr< RooAbsReal > _rearrangedDen
Definition RooProdPdf.h:117
void writeToStream(std::ostream &os) const
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:35
std::unique_ptr< RooArgSet > getConstraints(const RooArgSet &observables, RooArgSet const &constrainedParams, RooArgSet &pdfParams) const override
Return all parameter constraint p.d.f.s on parameters listed in constrainedParams.
Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, bool staticInitOK=true) const override
Query internal generation capabilities of component p.d.f.s and aggregate capabilities into master co...
void rearrangeProduct(CacheElem &) const
double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=nullptr) const override
Return analytical integral defined by given scenario code.
~RooProdPdf() override
Destructor.
Int_t _extendedIndex
Index of extended PDF (if any)
Definition RooProdPdf.h:193
std::unique_ptr< RooAbsReal > specializeRatio(RooFormulaVar &input, const char *targetRangeName) const
RooProdPdf()
Default constructor.
void removePdfs(RooAbsCollection const &pdfs)
Remove some PDFs from the factors of this RooProdPdf.
bool _useDefaultGen
Use default or distributed event generator.
Definition RooProdPdf.h:196
std::vector< std::unique_ptr< RooArgSet > > _pdfNSetList
List of PDF component normalization sets.
Definition RooProdPdf.h:192
std::unique_ptr< RooArgSet > fillNormSetForServer(RooArgSet const &normSet, RooAbsArg const &server) const
void factorizeProduct(const RooArgSet &normSet, const RooArgSet &intSet, Factorized &factorized) const
Factorize product in irreducible terms for given choice of integration/normalization.
bool forceAnalyticalInt(const RooAbsArg &dep) const override
Force RooRealIntegral to offer all observables for internal integration.
std::unique_ptr< RooAbsArg > compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const override
RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=nullptr, const RooArgSet *auxProto=nullptr, bool verbose=false) const override
Return generator context optimized for generating events from product p.d.f.s.
TNamed * _refRangeName
Reference range name for interpretation of conditional products.
Definition RooProdPdf.h:198
void addPdfs(RooAbsCollection const &pdfs)
Add some full PDFs to the factors of this RooProdPdf.
RooListProxy _pdfList
List of PDF components.
Definition RooProdPdf.h:191
Int_t getPartIntList(const RooArgSet *nset, const RooArgSet *iset, const char *isetRangeName=nullptr) const
Return list of (partial) integrals of product terms for integration of p.d.f over observables iset wh...
void printMetaArgs(std::ostream &os) const override
Customized printing of arguments of a RooProdPdf to more intuitively reflect the contents of the prod...
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
Forward the plot sampling hint from the p.d.f. that defines the observable obs.
RooObjCacheManager _cacheMgr
! The cache manager
Definition RooProdPdf.h:174
std::string makeRGPPName(const char *pfx, const RooArgSet &term, const RooArgSet &iset, const RooArgSet &nset, const char *isetRangeName) const
Make an appropriate automatic name for a RooGenProdProj object in getPartIntList()
bool isDirectGenSafe(const RooAbsArg &arg) const override
Forward determination of safety of internal generator code to component p.d.f that would generate the...
ProcessProductTermOutput processProductTerm(const RooArgSet *nset, const RooArgSet *iset, const char *isetRangeName, const RooArgSet *term, const RooArgSet &termNSet, const RooArgSet &termISet, bool forceWrap=false) const
Calculate integrals of factorized product terms over observables iset while normalized to observables...
RooArgSet * findPdfNSet(RooAbsPdf const &pdf) const
Look up user specified normalization set for given input PDF component.
ExtendMode extendMode() const override
If this product contains exactly one extendable p.d.f return the extension abilities of that p....
std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const override
Forward the plot sampling hint from the p.d.f. that defines the observable obs.
double expectedEvents(const RooArgSet *nset) const override
Return the expected number of events associated with the extendable input PDF in the product.
bool isBinnedDistribution(const RooArgSet &obs) const override
If all components that depend on obs are binned that so is the product.
friend class RooProdGenContext
Definition RooProdPdf.h:182
RooArgSet * getConnectedParameters(const RooArgSet &observables) const
Return all parameter constraint p.d.f.s on parameters listed in constrainedParams.
double calculate(const RooProdPdf::CacheElem &cache, bool verbose=false) const
Calculate running product of pdfs terms, using the supplied normalization set in 'normSetList' for ea...
RooArgSet _defNormSet
Default normalization set.
Definition RooProdPdf.h:201
std::unique_ptr< RooAbsReal > specializeIntegral(RooAbsReal &input, const char *targetRangeName) const
Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet, const char *rangeName=nullptr) const override
Determine which part (if any) of given integral can be performed analytically.
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,...
CacheElem * getCacheElem(RooArgSet const *nset) const
bool redirectServersHook(const RooAbsCollection &, bool, bool, bool) override
Implement support for node removal.
void groupProductTerms(std::list< std::vector< RooArgSet * > > &groupedTerms, RooArgSet &outerIntDeps, Factorized const &factorized) const
Group product into terms that can be calculated independently.
std::vector< std::vector< int > > _genCode
! Registry of composite direct generator codes
Definition RooProdPdf.h:188
void initGenerator(Int_t code) override
Forward one-time initialization call to component generation initialization methods.
void generateEvent(Int_t code) override
Generate a single event with configuration specified by 'code' Defer internal generation to component...
void fixRefRange(const char *rangeName)
std::unique_ptr< CacheElem > createCacheElem(const RooArgSet *nset, const RooArgSet *iset, const char *isetRangeName=nullptr) const
double evaluate() const override
Calculate current value of object.
void initializeFromCmdArgList(const RooArgSet &fullPdfSet, const RooLinkedList &l)
Initialize RooProdPdf configuration from given list of RooCmdArg configuration arguments and set of '...
void selectNormalizationRange(const char *rangeName=nullptr, bool force=false) override
Interface function used by test statistics to freeze choice of range for interpretation of conditiona...
double _cutOff
Cutoff parameter for running product.
Definition RooProdPdf.h:190
std::unique_ptr< RooAbsReal > makeCondPdfRatioCorr(RooAbsReal &pdf, const RooArgSet &termNset, const RooArgSet &termImpSet, const char *normRange, const char *refRange) const
For single normalization ranges.
Represents the product of a given set of RooAbsReal objects.
Definition RooProduct.h:29
static TClass * Class()
Performs hybrid numerical/analytical integrals of RooAbsReal objects.
RooFit::OwningPtr< RooAbsReal > createIntegral(const RooArgSet &iset, const RooArgSet *nset=nullptr, const RooNumIntConfig *cfg=nullptr, const char *rangeName=nullptr) const override
Create an object that represents the integral of the function over one or more observables listed in ...
const RooAbsReal & integrand() const
static TClass * Class()
Variable that can be changed from the outside.
Definition RooRealVar.h:37
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:42
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:548
Basic string class.
Definition TString.h:137
Ssiz_t Length() const
Definition TString.h:426
const char * Data() const
Definition TString.h:385
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
RooConstVar & RooConst(double val)
Double_t x[n]
Definition legend1.C:17
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan extraArgs={})
BinnedLOutput getBinnedL(RooAbsPdf const &pdf)
std::string getColonSeparatedNameString(RooArgSet const &argSet, char delim=':')
RooLinkedList cross
Definition RooProdPdf.h:148
RooLinkedList norms
Definition RooProdPdf.h:145
RooLinkedList terms
Definition RooProdPdf.h:144
TLine l
Definition textangle.C:4