Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooGenProdProj.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 RooGenProdProj.cxx
19\class RooGenProdProj
20\ingroup Roofitcore
21
22
23RooGenProdProj is an auxiliary class for RooProdPdf that calculates
24a general normalised projection of a product of non-factorising PDFs, e.g.
25\f[
26 P_{x,xy} = \frac{\int ( P1 * P2 * \ldots) \mathrm{d}x}{\int ( P1 * P2 * \ldots ) \mathrm{d}x \mathrm{d}y}
27\f]
28
29Partial integrals, which factorise and can be calculated, are calculated
30analytically. Remaining non-factorising observables are integrated numerically.
31**/
32
33
34#include "Riostream.h"
35#include <cmath>
36
37#include "RooGenProdProj.h"
38#include "RooAbsReal.h"
39#include "RooAbsPdf.h"
40#include "RooErrorHandler.h"
41#include "RooProduct.h"
42
43
44////////////////////////////////////////////////////////////////////////////////
45/// Constructor for a normalization projection of the product of p.d.f.s _prodSet
46/// integrated over _intSet in range isetRangeName while normalized over _normSet
47
48RooGenProdProj::RooGenProdProj(const char *name, const char *title, const RooArgSet& _prodSet, const RooArgSet& _intSet,
49 const RooArgSet& _normSet, const char* isetRangeName, const char* normRangeName, bool doFactorize) :
50 RooAbsReal(name, title),
51 _compSetN("compSetN","Set of integral components owned by numerator",this,false),
52 _compSetD("compSetD","Set of integral components owned by denominator",this,false),
53 _intList("intList","List of integrals",this,true)
54{
55 // Set expensive object cache to that of first item in prodSet
57
58 // Create owners of components created in ctor
59 _compSetOwnedN = std::make_unique<RooArgSet>();
60 _compSetOwnedD = std::make_unique<RooArgSet>();
61
62 RooAbsReal* numerator = makeIntegral("numerator",_prodSet,_intSet,*_compSetOwnedN,isetRangeName,doFactorize) ;
63 RooAbsReal* denominator = makeIntegral("denominator",_prodSet,_normSet,*_compSetOwnedD,normRangeName,doFactorize) ;
64
65// cout << "RooGenProdPdf::ctor(" << GetName() << ") numerator = " << numerator->GetName() << endl ;
66// numerator->printComponentTree() ;
67// cout << "RooGenProdPdf::ctor(" << GetName() << ") denominator = " << denominator->GetName() << endl ;
68// denominator->printComponentTree() ;
69
70 // Copy all components in (non-owning) set proxy
73
74 _intList.add(*numerator) ;
75 if (denominator) {
76 _intList.add(*denominator) ;
77 _haveD = true ;
78 }
79}
80
81
82
83////////////////////////////////////////////////////////////////////////////////
84/// Copy constructor
85
87 RooAbsReal(other, name),
88 _compSetN("compSetN","Set of integral components owned by numerator",this),
89 _compSetD("compSetD","Set of integral components owned by denominator",this),
90 _intList("intList","List of integrals",this)
91{
92 // Copy constructor
93 _compSetOwnedN = std::make_unique<RooArgSet>();
96
97 _compSetOwnedD = std::make_unique<RooArgSet>();
100
101 for (RooAbsArg * arg : *_compSetOwnedN) {
102 arg->setOperMode(_operMode) ;
103 }
104 for (RooAbsArg * arg : *_compSetOwnedD) {
105 arg->setOperMode(_operMode) ;
106 }
107
108 // Fill _intList
109 _haveD = other._haveD ;
110 _intList.add(*_compSetN.find(other._intList.at(0)->GetName())) ;
111 if (other._haveD) {
112 _intList.add(*_compSetD.find(other._intList.at(1)->GetName())) ;
113 }
114}
115
116
117////////////////////////////////////////////////////////////////////////////////
118/// Utility function to create integral for product over certain observables.
119/// \param[in] name Name of integral to be created.
120/// \param[in] compSet All components of the product.
121/// \param[in] intSet Observables to be integrated.
122/// \param[out] saveSet All component objects needed to represent the product integral are added as owned members to saveSet.
123/// \note The set owns new components that are created for the integral.
124/// \param[in] isetRangeName Integral range.
125/// \param[in] doFactorize
126///
127/// \return A RooAbsReal object representing the requested integral. The object is owned by `saveSet`.
128///
129/// The integration is factorized into components as much as possible and done analytically as far as possible.
130RooAbsReal* RooGenProdProj::makeIntegral(const char* name, const RooArgSet& compSet, const RooArgSet& intSet,
131 RooArgSet& saveSet, const char* isetRangeName, bool doFactorize)
132{
133 RooArgSet anaIntSet, numIntSet ;
134
135 // First determine subset of observables in intSet that are factorizable
136 for (const auto arg : intSet) {
137 auto count = std::count_if(compSet.begin(), compSet.end(), [arg](const RooAbsArg* pdfAsArg){
138 auto pdf = static_cast<const RooAbsPdf*>(pdfAsArg);
139 return (pdf->dependsOn(*arg));
140 });
141
142 if (count==1) {
143 anaIntSet.add(*arg) ;
144 }
145 }
146
147 // Determine which of the factorizable integrals can be done analytically
148 RooArgSet prodSet ;
149 numIntSet.add(intSet) ;
150
151 // The idea of the RooGenProdProj is that we divide two integral objects each
152 // created with this makeIntegral() function to get the normalized integral of
153 // a product. Therefore, we don't need to normalize the numerater and
154 // denominator integrals themselves. Doing the normalization would be
155 // expensive and it would cancel out anyway. However, if we don't specify an
156 // explicit normalization integral in createIntegral(), the last-used
157 // normalization set might be used to normalize the pdf, resulting in
158 // redundant computations.
159 //
160 // For this reason, the normalization set of the integrated pdfs is fixed to
161 // an empty set in this case. Note that in RooFit, a nullptr normalization
162 // set and an empty normalization set is not equivalent. The former implies
163 // taking the last-used normalization set, and the latter means explicitly no
164 // normalization.
165 RooArgSet emptyNormSet{};
166
167 RooArgSet keepAlive;
168
169 for (const auto pdfAsArg : compSet) {
170 auto pdf = static_cast<const RooAbsPdf*>(pdfAsArg);
171
172 if (doFactorize && pdf->dependsOn(anaIntSet)) {
173 RooArgSet anaSet ;
174 Int_t code = pdf->getAnalyticalIntegralWN(anaIntSet,anaSet,nullptr,isetRangeName) ;
175 if (code!=0) {
176 // Analytical integral, create integral object
177 std::unique_ptr<RooAbsReal> pai{pdf->createIntegral(anaSet,emptyNormSet,isetRangeName)};
178 pai->setOperMode(_operMode) ;
179
180 // Add to integral to product
181 prodSet.add(*pai) ;
182
183 // Remove analytically integratable observables from numeric integration list
184 numIntSet.remove(anaSet) ;
185
186 // Keep integral alive until the prodSet is cloned later
187 keepAlive.addOwned(std::move(pai));
188 } else {
189 // Analytic integration of factorizable observable not possible, add straight pdf to product
190 prodSet.add(*pdf) ;
191 }
192 } else {
193 // Non-factorizable observables, add straight pdf to product
194 prodSet.add(*pdf) ;
195 }
196 }
197
198 // Create product of (partial) analytical integrals
199 TString prodName ;
200 if (isetRangeName) {
201 prodName = Form("%s_%s_Range[%s]",GetName(),name,isetRangeName) ;
202 } else {
203 prodName = Form("%s_%s",GetName(),name) ;
204 }
205
206 // Create clones of the elements in prodSet. These need to be cloned
207 // because when caching optimisation lvl 2 is activated, pre-computed
208 // values are side-loaded into the elements.
209 // Those pre-cached values already contain normalisation constants, so
210 // the integral comes out wrongly. Therefore, we create here nodes that
211 // don't participate in any caching, which are used to compute integrals.
212 RooArgSet prodSetClone;
213 prodSet.snapshot(prodSetClone, false);
214
215 auto prod = std::make_unique<RooProduct>(prodName, "product", prodSetClone);
216 prod->setExpensiveObjectCache(expensiveObjectCache()) ;
217 prod->setOperMode(_operMode) ;
218
219 // Create integral performing remaining numeric integration over (partial) analytic product
220 std::unique_ptr<RooAbsReal> integral{prod->createIntegral(numIntSet,emptyNormSet,isetRangeName)};
221 integral->setOperMode(_operMode) ;
222 auto ret = integral.get();
223
224 // Declare ownership of prodSet, product, and integral
225 saveSet.addOwned(std::move(prodSetClone));
226 saveSet.addOwned(std::move(prod));
227 saveSet.addOwned(std::move(integral)) ;
228
229
230 // Caller owners returned master integral object
231 return ret ;
232}
233
234
235
236////////////////////////////////////////////////////////////////////////////////
237/// Calculate and return value of normalization projection
238
240{
241 RooArgSet const* nset = _intList.nset();
242
243 double nom = static_cast<RooAbsReal*>(_intList.at(0))->getVal(nset);
244
245 if (!_haveD) return nom ;
246
247 double den = static_cast<RooAbsReal*>(_intList.at(1))->getVal(nset);
248
249 //cout << "RooGenProdProj::eval(" << GetName() << ") nom = " << nom << " den = " << den << endl ;
250
251 return nom / den ;
252}
253
254
255
256////////////////////////////////////////////////////////////////////////////////
257/// Intercept cache mode operation changes and propagate them to the components
258
260{
261 // WVE use cache manager here!
262
263 for(RooAbsArg * arg : *_compSetOwnedN) {
264 arg->setOperMode(_operMode) ;
265 }
266
267 for(RooAbsArg * arg : *_compSetOwnedD) {
268 arg->setOperMode(_operMode) ;
269 }
270
272 if (_haveD) _intList.at(1)->setOperMode(Auto) ; // Denominator always stays in Auto mode (normalization integral)
273}
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
RooExpensiveObjectCache & expensiveObjectCache() const
void setOperMode(OperMode mode, bool recurseADirty=true)
Set the operation mode of this node.
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:503
OperMode _operMode
Definition RooAbsArg.h:716
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.
const_iterator end() const
RooAbsArg * first() const
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
const_iterator begin() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
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:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
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...
General form of projected integral of product of PDFs, utility class for RooProdPdf.
void operModeHook() override
Intercept cache mode operation changes and propagate them to the components.
double evaluate() const override
Calculate and return value of normalization projection.
RooAbsReal * makeIntegral(const char *name, const RooArgSet &compSet, const RooArgSet &intSet, RooArgSet &saveSet, const char *isetRangeName, bool doFactorize)
Utility function to create integral for product over certain observables.
RooSetProxy _compSetD
Set proxy for denominator components.
RooListProxy _intList
Master integrals representing numerator and denominator.
std::unique_ptr< RooArgSet > _compSetOwnedN
Owner of numerator components.
std::unique_ptr< RooArgSet > _compSetOwnedD
Owner of denominator components.
bool _haveD
Do we have a denominator term?
RooGenProdProj(const char *name, const char *title, const RooArgSet &_prodSet, const RooArgSet &_intSet, const RooArgSet &_normSet, const char *isetRangeName, const char *normRangeName=nullptr, bool doFactorize=true)
Constructor for a normalization projection of the product of p.d.f.s _prodSet integrated over _intSet...
RooSetProxy _compSetN
Set proxy for numerator components.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139