ROOT  6.06/09
Reference Guide
RooProjectedPdf.cxx
Go to the documentation of this file.
1  /*****************************************************************************
2  * Project: RooFit *
3  * *
4  * Copyright (c) 2000-2005, Regents of the University of California *
5  * and Stanford University. All rights reserved. *
6  * *
7  * Redistribution and use in source and binary forms, *
8  * with or without modification, are permitted according to the terms *
9  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
10  *****************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////////
13 //
14 // BEGIN_HTML
15 // Class RooProjectedPdf is a RooAbsPdf implementation that represent a projection
16 // of a given input p.d.f and the object returned by RooAbsPdf::createProjection.
17 // <p>
18 // The actual projection integral for it value and normalization are
19 // calculated on the fly in getVal() once the normalization observables are known.
20 // Class RooProjectedPdf can cache projected p.d.f.s for multiple normalization
21 // observables simultaneously.
22 // <p>
23 // The createProjection() method of RooProjectedPdf is overloaded and will
24 // return a new RooProjectedPdf that performs the projection of itself
25 // and the requested additional projections in one integration step
26 // The performance of <pre>f->createProjection(x)->createProjection(y)</pre>
27 // is therefore identical to that of <pre>f->createProjection(RooArgSet(x,y))</pre>
28 // END_HTML
29 //
30 
31 #include "Riostream.h"
32 
33 #include "RooFit.h"
34 #include "RooProjectedPdf.h"
35 #include "RooMsgService.h"
36 #include "RooAbsReal.h"
37 #include "RooRealVar.h"
38 #include "RooNameReg.h"
39 
40 using namespace std;
41 
43  ;
44 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Default constructor
48 
50 {
51 }
52 
53 
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Construct projection of input pdf '_intpdf' over observables 'intObs'
57 
58  RooProjectedPdf::RooProjectedPdf(const char *name, const char *title, RooAbsReal& _intpdf, const RooArgSet& intObs) :
59  RooAbsPdf(name,title),
60  intpdf("!IntegratedPdf","intpdf",this,_intpdf,kFALSE,kFALSE),
61  intobs("!IntegrationObservables","intobs",this,kFALSE,kFALSE),
62  deps("!Dependents","deps",this,kTRUE,kTRUE),
63  _cacheMgr(this,10)
64  {
65  intobs.add(intObs) ;
66 
67  // Add all other dependens of projected p.d.f. directly
68  RooArgSet* tmpdeps = _intpdf.getParameters(intObs) ;
69  deps.add(*tmpdeps) ;
70  delete tmpdeps ;
71  }
72 
73 
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Copy constructor
77 
79  RooAbsPdf(other,name),
80  intpdf("!IntegratedPdf",this,other.intpdf),
81  intobs("!IntegrationObservable",this,other.intobs),
82  deps("!Dependents",this,other.deps),
83  _cacheMgr(other._cacheMgr,this)
84 {
85  }
86 
87 
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Special version of getVal() overrides RooAbsReal::getValF() to save value of current normalization set
91 
93 {
94  _curNormSet = (RooArgSet*)set ;
95 
96  return RooAbsPdf::getValV(set) ;
97 }
98 
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Evaluate projected p.d.f
103 
105 {
106  // Calculate current unnormalized value of object
107  int code ;
108  const RooAbsReal* proj = getProjection(&intobs,_curNormSet,0,code) ;
109 
110  return proj->getVal() ;
111 }
112 
113 
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Retrieve object representing projection integral of input p.d.f
117 /// over observables iset, while normalizing over observables
118 /// nset. The code argument returned by reference is the unique code
119 /// defining this particular projection configuration
120 
121 const RooAbsReal* RooProjectedPdf::getProjection(const RooArgSet* iset, const RooArgSet* nset, const char* rangeName, int& code) const
122 {
123 
124  // Check if this configuration was created before
125  Int_t sterileIdx(-1) ;
126  CacheElem* cache = (CacheElem*) _cacheMgr.getObj(iset,nset,&sterileIdx,RooNameReg::ptr(rangeName)) ;
127  if (cache) {
128  code = _cacheMgr.lastIndex() ;
129  return static_cast<const RooAbsReal*>(cache->_projection);
130  }
131 
132  RooArgSet* nset2 = intpdf.arg().getObservables(*nset) ;
133 
134  if (iset) {
135  nset2->add(*iset) ;
136  }
137  RooAbsReal* proj = intpdf.arg().createIntegral(iset?*iset:RooArgSet(),nset2,0,rangeName) ;
138  delete nset2 ;
139 
140  cache = new CacheElem ;
141  cache->_projection = proj ;
142 
143  code = _cacheMgr.setObj(iset,nset,(RooAbsCacheElement*)cache,RooNameReg::ptr(rangeName)) ;
144 
145  coutI(Integration) << "RooProjectedPdf::getProjection(" << GetName() << ") creating new projection " << proj->GetName() << " with code " << code << endl ;
146 
147  return proj ;
148 }
149 
150 
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Special version of RooAbsReal::createProjection that deals with
154 /// projections of projections. Instead of integrating twice, a new
155 /// RooProjectedPdf is returned that is configured to perform the
156 /// complete integration in one step
157 
159 {
160  RooArgSet combiset(iset) ;
161  combiset.add(intobs) ;
162  return static_cast<RooAbsPdf&>( const_cast<RooAbsReal&>(intpdf.arg()) ).createProjection(combiset) ;
163 }
164 
165 
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// Force RooRealIntegral to relegate integration of all observables to internal logic
169 
171 {
172  return kTRUE ;
173 }
174 
175 
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Mark all requested variables as internally integrated
179 
180 Int_t RooProjectedPdf::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
181 {
182  analVars.add(allVars) ;
183 
184  // Create the appropriate integral
185  int code ;
186  RooArgSet allVars2(allVars) ;
187  allVars2.add(intobs) ;
188  getProjection(&allVars2,normSet,rangeName,code) ;
189 
190  return code+1 ;
191 }
192 
193 
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Return analytical integral represent by appropriate element of projection cache
197 
198 Double_t RooProjectedPdf::analyticalIntegralWN(Int_t code, const RooArgSet* /*normSet*/, const char* rangeName) const
199 {
200  CacheElem *cache = (CacheElem*) _cacheMgr.getObjByIndex(code-1) ;
201 
202  if (cache) {
203  Double_t ret= cache->_projection->getVal() ;
204  return ret ;
205  } else {
206 
207  RooArgSet* vars = getParameters(RooArgSet()) ;
208  vars->add(intobs) ;
209  RooArgSet* iset = _cacheMgr.nameSet1ByIndex(code-1)->select(*vars) ;
210  RooArgSet* nset = _cacheMgr.nameSet2ByIndex(code-1)->select(*vars) ;
211 
212  Int_t code2(-1) ;
213  const RooAbsReal* proj = getProjection(iset,nset,rangeName,code2) ;
214 
215  delete vars ;
216  delete nset ;
217  delete iset ;
218 
219  Double_t ret = proj->getVal() ;
220  return ret ;
221  }
222 
223 }
224 
225 
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// No internal generator is implemented
229 
230 Int_t RooProjectedPdf::getGenerator(const RooArgSet& /*directVars*/, RooArgSet& /*generateVars*/, Bool_t /*staticInitOK*/) const
231  {
232  return 0 ;
233  }
234 
235 
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// No internal generator is implemented
239 
241  {
242  return;
243  }
244 
245 
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Intercept a server redirection all and update list of dependents if necessary
249 /// Specifically update the set proxy 'deps' which introduces the dependency
250 /// on server value dirty flags of ourselves
251 
252 Bool_t RooProjectedPdf::redirectServersHook(const RooAbsCollection& newServerList, Bool_t /*mustReplaceAll*/,
253  Bool_t /*nameChange*/, Bool_t /*isRecursive*/)
254 {
255  // Redetermine explicit list of dependents if intPdf is being replaced
256  RooAbsArg* newPdf = newServerList.find(intpdf.arg().GetName()) ;
257  if (newPdf) {
258 
259  // Determine if set of dependens of new p.d.f is different from old p.d.f.
260  RooArgSet olddeps(deps) ;
261  RooArgSet* newdeps = newPdf->getParameters(intobs) ;
262  RooArgSet* common = (RooArgSet*) newdeps->selectCommon(deps) ;
263  newdeps->remove(*common,kTRUE,kTRUE) ;
264  olddeps.remove(*common,kTRUE,kTRUE) ;
265 
266  // If so, adjust composition of deps Listproxy
267  if (newdeps->getSize()>0) {
268  deps.add(*newdeps) ;
269  }
270  if (olddeps.getSize()>0) {
271  deps.remove(olddeps,kTRUE,kTRUE) ;
272  }
273 
274  delete common ;
275  delete newdeps ;
276  }
277 
278  return kFALSE ;
279 }
280 
281 
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 /// Return RooAbsArg elements contained in projection cache element.
285 
287 {
288  RooArgList ret(*_projection) ;
289  return ret ;
290 }
291 
292 
293 
294 ////////////////////////////////////////////////////////////////////////////////
295 /// Customized printing of arguments of a RooRealIntegral to more intuitively reflect the contents of the
296 /// integration operation
297 
298 void RooProjectedPdf::printMetaArgs(ostream& os) const
299 {
300  os << "Int " << intpdf.arg().GetName() ;
301 
302  os << " d" ;
303  os << intobs ;
304  os << " " ;
305 
306 }
307 
308 
309 
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// Print contents of cache when printing self as part of object tree
313 
314 void RooProjectedPdf::CacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
315 {
316  if (curElem==0) {
317  os << indent << "RooProjectedPdf begin projection cache" << endl ;
318  }
319 
320  TString indent2(indent) ;
321  indent2 += Form("[%d] ",curElem) ;
322 
323  _projection->printCompactTree(os,indent2) ;
324 
325  if(curElem==maxElem) {
326  os << indent << "RooProjectedPdf end projection cache" << endl ;
327  }
328 }
329 
330 
RooSetProxy deps
virtual RooAbsPdf * createProjection(const RooArgSet &iset)
Special version of RooAbsReal::createProjection that deals with projections of projections.
RooRealProxy intpdf
RooAbsCollection * selectCommon(const RooAbsCollection &refColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
const RooNameSet * nameSet2ByIndex(Int_t index) const
#define coutI(a)
Definition: RooMsgService.h:32
ClassImp(RooProjectedPdf)
const RooNameSet * nameSet1ByIndex(Int_t index) const
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Definition: RooAbsArg.h:194
Int_t lastIndex() const
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, Bool_t staticInitOK=kTRUE) const
No internal generator is implemented.
STL namespace.
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
Bool_t redirectServersHook(const RooAbsCollection &newServerList, Bool_t, Bool_t, Bool_t)
The cache manager.
friend class CacheElem
Definition: RooAbsPdf.h:314
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove object 'var' from set and deregister 'var' as server to owner.
friend class RooArgSet
Definition: RooAbsArg.h:469
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual RooAbsPdf * createProjection(const RooArgSet &iset)
Return a p.d.f that represent a projection of this p.d.f integrated over given observables.
Definition: RooAbsPdf.cxx:2989
RooArgSet * getParameters(const RooAbsData *data, Bool_t stripDisconnected=kTRUE) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Definition: RooAbsArg.cxx:559
virtual RooArgList containedArgs(Action)
Return RooAbsArg elements contained in projection cache element.
RooObjCacheManager _cacheMgr
RooSetProxy intobs
Double_t evaluate() const
Evaluate projected p.d.f.
RooAbsArg * find(const char *name) const
Find object with given name in list.
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:124
char * Form(const char *fmt,...)
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Return analytical integral represent by appropriate element of projection cache.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static void indent(ostringstream &buf, int indent_level)
RooArgSet * _curNormSet
virtual Double_t getValV(const RooArgSet *set=0) const
Return current value, normalizated by integrating over the observables in 'nset'. ...
Definition: RooAbsPdf.cxx:252
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
const RooAbsReal * getProjection(const RooArgSet *iset, const RooArgSet *nset, const char *rangeName, int &code) const
Retrieve object representing projection integral of input p.d.f over observables iset, while normalizing over observables nset.
virtual Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet, const char *rangeName=0) const
Mark all requested variables as internally integrated.
RooProjectedPdf()
Default constructor.
virtual void printCompactTreeHook(std::ostream &, const char *, Int_t, Int_t)
Print contents of cache when printing self as part of object tree.
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
void generateEvent(Int_t code)
No internal generator is implemented.
T * getObjByIndex(Int_t index) const
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
virtual Bool_t forceAnalyticalInt(const RooAbsArg &dep) const
Force RooRealIntegral to relegate integration of all observables to internal logic.
void printMetaArgs(std::ostream &os) const
Customized printing of arguments of a RooRealIntegral to more intuitively reflect the contents of the...
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=0)
Int_t getSize() const
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
RooArgSet * select(const RooArgSet &list) const
Construct a RooArgSet of objects in input 'list' whose names match to those in the internal name list...
Definition: RooNameSet.cxx:188
virtual Double_t getValV(const RooArgSet *set=0) const
Special version of getVal() overrides RooAbsReal::getValF() to save value of current normalization se...
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448
RooAbsReal * createIntegral(const RooArgSet &iset, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Create an object that represents the integral of the function over one or more observables listed in ...
Definition: RooAbsReal.cxx:503
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Overloaded RooArgSet::add() method inserts 'var' into set and registers 'var' as server to owner with...