Logo ROOT   6.16/01
Reference Guide
RooAbsCachedPdf.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\file RooAbsCachedPdf.cxx
14\class RooAbsCachedPdf
15\ingroup Roofitcore
16
17RooAbsCachedPdf is the abstract base class for p.d.f.s that need or
18want to cache their evaluate() output in a RooHistPdf defined in
19terms of the used observables. This base class manages the creation
20and storage of all RooHistPdf cache p.d.fs and the RooDataHists
21that define their shape. Implementations of RooAbsCachedPdf must
22define member function fillCacheObject() which serves to fill an
23already created RooDataHist with the p.d.fs function values. In
24addition the member functions actualObservables() and
25actualParameters() must be define which report what the actual
26observables to be cached are for a given set of observables passed
27by the user to getVal() and on which parameters need to be tracked
28for changes to trigger a refilling of the cache histogram.
29**/
30
31#include "Riostream.h"
32using namespace std ;
33
34#include "RooFit.h"
35#include "TString.h"
36#include "RooAbsCachedPdf.h"
37#include "RooAbsReal.h"
38#include "RooMsgService.h"
39#include "RooDataHist.h"
40#include "RooHistPdf.h"
41#include "RooGlobalFunc.h"
42#include "RooRealVar.h"
43#include "RooChangeTracker.h"
45
47
48
49
50////////////////////////////////////////////////////////////////////////////////
51/// Constructor
52
53RooAbsCachedPdf::RooAbsCachedPdf(const char *name, const char *title, Int_t ipOrder) :
54 RooAbsPdf(name,title),
55 _cacheMgr(this,10),
56 _ipOrder(ipOrder),
57 _disableCache(kFALSE)
58 {
59 }
60
61
62
63////////////////////////////////////////////////////////////////////////////////
64/// Copy constructor
65
67 RooAbsPdf(other,name),
68 _cacheMgr(other._cacheMgr,this),
69 _ipOrder(other._ipOrder),
70 _disableCache(other._disableCache)
71 {
72 }
73
74
75
76////////////////////////////////////////////////////////////////////////////////
77/// Destructor
78
80{
81}
82
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// Implementation of getVal() overriding default implementation
87/// of RooAbsPdf. Return normalized value stored in cache p.d.f
88/// rather than return value of evaluate() which is undefined
89/// for RooAbsCachedPdf
90
92{
93 if (_disableCache) {
94 return RooAbsPdf::getValV(nset) ;
95 }
96
97 // Calculate current unnormalized value of object
98 PdfCacheElem* cache = getCache(nset) ;
99
100 Double_t value = cache->pdf()->getVal(nset) ;
101
102 _value = value ;
103 return _value ;
104}
105
106
107
108////////////////////////////////////////////////////////////////////////////////
109/// Return pointer to RooHistPdf cache pdf for given choice of observables
110
112{
113 PdfCacheElem* cache = getCache(nset) ;
114
115 if (cache) {
116 return cache->pdf() ;
117 } else {
118 return 0 ;
119 }
120}
121
122
123////////////////////////////////////////////////////////////////////////////////
124/// Return pointer to RooDataHist cache histogram for given choice of observables
125
127{
128 PdfCacheElem* cache = getCache(nset) ;
129
130 if (cache) {
131 return cache->hist() ;
132 } else {
133 return 0 ;
134 }
135}
136
137
138////////////////////////////////////////////////////////////////////////////////
139/// Mark all bins of given cache as unitialized (value -1)
140
142{
143 cache.hist()->setAllWeights(-1) ;
144}
145
146
147
148////////////////////////////////////////////////////////////////////////////////
149/// Retrieve cache object associated with given choice of observables. If cache object
150/// does not exist, create and fill and register it on the fly. If recalculate=false
151/// recalculation of cache contents of existing caches that are marked dirty due to
152/// dependent parameter changes is suppressed.
153
155{
156 // Check if this configuration was created becfore
157 Int_t sterileIdx(-1) ;
158 PdfCacheElem* cache = (PdfCacheElem*) _cacheMgr.getObj(nset,0,&sterileIdx) ;
159
160 // Check if we have a cache histogram in the global expensive object cache
161 if (cache) {
162 if (cache->paramTracker()->hasChanged(kTRUE) && (recalculate || !cache->pdf()->haveUnitNorm()) ) {
163 cxcoutD(Eval) << "RooAbsCachedPdf::getCache(" << GetName() << ") cache " << cache << " pdf "
164 << cache->pdf()->GetName() << " requires recalculation as parameters changed" << endl ;
165 fillCacheObject(*cache) ;
166 cache->pdf()->setValueDirty() ;
167 }
168 return cache ;
169 }
170
171 // Create and fill cache
172 cache = createCache(nset) ;
173
174 // Check if we have contents registered already in global expensive object cache
176
177 if (htmp) {
178
179 cache->hist()->reset() ;
180 cache->hist()->add(*htmp) ;
181
182 } else {
183
184 fillCacheObject(*cache) ;
185
186 RooDataHist* eoclone = new RooDataHist(*cache->hist()) ;
187 eoclone->removeSelfFromDir() ;
188 expensiveObjectCache().registerObject(GetName(),cache->hist()->GetName(),*eoclone,cache->paramTracker()->parameters()) ;
189
190 }
191
192
193 // Store this cache configuration
194 Int_t code = _cacheMgr.setObj(nset,0,((RooAbsCacheElement*)cache),0) ;
195
196 coutI(Caching) << "RooAbsCachedPdf::getCache(" << GetName() << ") creating new cache " << cache << " with pdf "
197 << cache->pdf()->GetName() << " for nset " << (nset?*nset:RooArgSet()) << " with code " << code ;
198 if (htmp) {
199 ccoutI(Caching) << " from preexisting content." ;
200 }
201 ccoutI(Caching) << endl ;
202
203 return cache ;
204}
205
206
207
208
209////////////////////////////////////////////////////////////////////////////////
210/// Constructor of cache object which owns RooDataHist cache histogram,
211/// RooHistPdf pdf that represents is shape and RooChangeTracker meta
212/// object that tracks changes in listed dependent parameter of cache.
213
215 _pdf(0), _paramTracker(0), _hist(0), _norm(0)
216{
217 // Create cache object itself -- Default implementation is a RooHistPdf
218 RooArgSet* nset2 = self.actualObservables(nsetIn?*nsetIn:RooArgSet()) ;
219
220 RooArgSet orderedObs ;
221 if (nset2) {
222 self.preferredObservableScanOrder(*nset2,orderedObs) ;
223 }
224
225 // Create RooDataHist
226 TString hname = self.GetName() ;
227 hname.Append("_") ;
228 hname.Append(self.inputBaseName()) ;
229 hname.Append("_CACHEHIST") ;
230 hname.Append(self.cacheNameSuffix(orderedObs)) ;
231 hname.Append(self.histNameSuffix()) ;
232 _hist = new RooDataHist(hname,hname,orderedObs,self.binningName()) ;
234
235 //RooArgSet* observables= self.getObservables(orderedObs) ;
236 // cout << "orderedObs = " << orderedObs << " observables = " << *observables << endl ;
237
238 // Get set of p.d.f. observable corresponding to set of histogram observables
239 RooArgSet pdfObs ;
240 RooArgSet pdfFinalObs ;
241 TIterator* iter = orderedObs.createIterator() ;
242 RooAbsArg* harg ;
243 while((harg=(RooAbsArg*)iter->Next())) {
244 RooAbsArg& po = self.pdfObservable(*harg) ;
245 pdfObs.add(po) ;
246 if (po.isFundamental()) {
247 pdfFinalObs.add(po) ;
248 } else {
249 RooArgSet* tmp = po.getVariables() ;
250 pdfFinalObs.add(*tmp) ;
251 delete tmp ;
252 }
253 }
254 delete iter ;
255
256 // Create RooHistPdf
257 TString pdfname = self.inputBaseName() ;
258 pdfname.Append("_CACHE") ;
259 pdfname.Append(self.cacheNameSuffix(pdfFinalObs)) ;
260 _pdf = new RooHistPdf(pdfname,pdfname,pdfObs,orderedObs,*_hist,self.getInterpolationOrder()) ;
261 if (nsetIn) {
262 _nset.addClone(*nsetIn) ;
263 }
264
265 // Create pseudo-object that tracks changes in parameter values
266
267 RooArgSet* params = self.actualParameters(pdfFinalObs) ;
268 params->remove(pdfFinalObs,kTRUE,kTRUE) ;
269
270 string name= Form("%s_CACHEPARAMS",_pdf->GetName()) ;
271 _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,kTRUE) ;
272 _paramTracker->hasChanged(kTRUE) ; // clear dirty flag as cache is up-to-date upon creation
273
274 // Introduce formal dependency of RooHistPdf on parameters so that const optimization code
275 // makes the correct decisions
276 _pdf->addServerList(*params) ;
277
278 // Set initial state of cache to dirty
280
281 //delete observables ;
282 delete params ;
283 delete nset2 ;
284
285}
286
287
288
289////////////////////////////////////////////////////////////////////////////////
290/// Construct string with unique suffix for cache objects based on
291/// observable names that define cache configuration
292
294{
295 TString name ;
296 name.Append("_Obs[") ;
297 if (nset.getSize()>0) {
298 TIterator* iter = nset.createIterator() ;
299 RooAbsArg* arg ;
301 while((arg=(RooAbsArg*)iter->Next())) {
302 if (first) {
303 first=kFALSE ;
304 } else {
305 name.Append(",") ;
306 }
307 name.Append(arg->GetName()) ;
308 }
309 delete iter ;
310 }
311
312 name.Append("]") ;
313 const char* payloadUS = payloadUniqueSuffix() ;
314 if (payloadUS) {
315 name.Append(payloadUS) ;
316 }
317 return name ;
318}
319
320
321
322////////////////////////////////////////////////////////////////////////////////
323/// Change the interpolation order that is used in RooHistPdf cache
324/// representation smoothing the RooDataHist shapes.
325
327{
328 _ipOrder = order ;
329
330 Int_t i ;
331 for (i=0 ; i<_cacheMgr.cacheSize() ; i++) {
333 if (cache) {
334 cache->pdf()->setInterpolationOrder(order) ;
335 }
336 }
337}
338
339
340
341////////////////////////////////////////////////////////////////////////////////
342/// Returns all RooAbsArg objects contained in the cache element
343
345{
346 RooArgList ret(*_pdf) ;
347 ret.add(*_paramTracker) ;
348 if (_norm) ret.add(*_norm) ;
349 return ret ;
350}
351
352
353
354////////////////////////////////////////////////////////////////////////////////
355/// Cache element destructor
356
358{
359 if (_norm) {
360 delete _norm ;
361 }
362 if (_pdf) {
363 delete _pdf ;
364 }
365 if (_paramTracker) {
366 delete _paramTracker ;
367 }
368 if (_hist) {
369 delete _hist ;
370 }
371}
372
373
374
375////////////////////////////////////////////////////////////////////////////////
376/// Print contents of cache when printing self as part of object tree
377
378void RooAbsCachedPdf::PdfCacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
379{
380 if (curElem==0) {
381 os << indent << "--- RooAbsCachedPdf begin cache ---" << endl ;
382 }
383
384 TString indent2(indent) ;
385 os << Form("[%d] Configuration for observables ",curElem) << _nset << endl ;
386 indent2 += Form("[%d] ",curElem) ;
387 _pdf->printCompactTree(os,indent2) ;
388 if (_norm) {
389 os << Form("[%d] Norm ",curElem) ;
391 }
392
393 if (curElem==maxElem) {
394 os << indent << "--- RooAbsCachedPdf end cache --- " << endl ;
395 }
396}
397
398
399
400////////////////////////////////////////////////////////////////////////////////
401/// Force RooRealIntegral to offer all our actual observable for internal
402/// integration
403
405{
406 RooArgSet* actObs = actualObservables(dep) ;
407 Bool_t ret = (actObs->getSize()>0) ;
408 delete actObs ;
409 return ret ;
410}
411
412
413
414////////////////////////////////////////////////////////////////////////////////
415/// Advertises internal (analytical) integration capabilities. Call
416/// is forwarded to RooHistPdf cache p.d.f of cache that is used for
417/// given choice of observables
418
419Int_t RooAbsCachedPdf::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
420{
421 if (allVars.getSize()==0) {
422 return 0 ;
423 }
424
425 PdfCacheElem* cache = getCache(normSet?normSet:&allVars) ;
426 Int_t code = cache->pdf()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
427
428 if (code==0) {
429 return 0 ;
430 }
431
432 RooArgSet* all = new RooArgSet ;
433 RooArgSet* ana = new RooArgSet ;
434 RooArgSet* nrm = new RooArgSet ;
435 all->addClone(allVars) ;
436 ana->addClone(analVars) ;
437 if (normSet) {
438 nrm->addClone(*normSet) ;
439 }
440 std::vector<Int_t> codeList(2);
441 codeList[0] = code ;
442 codeList[1] = cache->pdf()->haveUnitNorm() ? 1 : 0 ;
443 Int_t masterCode = _anaReg.store(codeList,all,ana,nrm)+1 ; // takes ownership of all sets
444
445
446 // Mark all observables as internally integrated
447 if (cache->pdf()->haveUnitNorm()) {
448 analVars.add(allVars,kTRUE) ;
449 }
450
451 return masterCode ;
452}
453
454
455
456////////////////////////////////////////////////////////////////////////////////
457/// Implements internal (analytical) integration capabilities. Call
458/// is forwarded to RooHistPdf cache p.d.f of cache that is used for
459/// given choice of observables
460
461Double_t RooAbsCachedPdf::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
462{
463 if (code==0) {
464 return getVal(normSet) ;
465 }
466
467 RooArgSet *allVars(0),*anaVars(0),*normSet2(0),*dummy(0) ;
468 const std::vector<Int_t> codeList = _anaReg.retrieve(code-1,allVars,anaVars,normSet2,dummy) ;
469
470 PdfCacheElem* cache = getCache(normSet2?normSet2:anaVars,kFALSE) ;
471 Double_t ret = cache->pdf()->analyticalIntegralWN(codeList[0],normSet,rangeName) ;
472
473 if (codeList[1]>0) {
474 RooArgSet factObs(*allVars) ;
475 factObs.remove(*anaVars,kTRUE,kTRUE) ;
476 TIterator* iter = factObs.createIterator() ;
477 RooAbsLValue* arg ;
478 while((arg=dynamic_cast<RooAbsLValue*>(iter->Next()))) {
479 ret *= arg->volume(rangeName) ;
480 }
481 delete iter ;
482 }
483
484 return ret ;
485}
486
487
488
489
490
void Class()
Definition: Class.C:29
static RooMathCoreReg dummy
#define coutI(a)
Definition: RooMsgService.h:31
#define cxcoutD(a)
Definition: RooMsgService.h:79
#define ccoutI(a)
Definition: RooMsgService.h:38
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:363
char * Form(const char *fmt,...)
const std::vector< Int_t > & retrieve(Int_t masterCode) const
Retrieve the array of integer codes associated with the given master code.
Int_t store(const std::vector< Int_t > &codeList, RooArgSet *set1=0, RooArgSet *set2=0, RooArgSet *set3=0, RooArgSet *set4=0)
Store given arrays of integer codes, and up to four RooArgSets in the registry (each setX pointer may...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
RooExpensiveObjectCache & expensiveObjectCache() const
Definition: RooAbsArg.cxx:2334
friend class RooArgSet
Definition: RooAbsArg.h:471
virtual Bool_t isFundamental() const
Definition: RooAbsArg.h:157
RooArgSet * getVariables(Bool_t stripDisconnected=kTRUE) const
Return RooArgSet with all variables (tree leaf nodes of expresssion tree)
Definition: RooAbsArg.cxx:2074
friend class RooHistPdf
Definition: RooAbsArg.h:514
void addServerList(RooAbsCollection &serverList, Bool_t valueProp=kTRUE, Bool_t shapeProp=kFALSE)
Register a list of RooAbsArg as servers to us by calls addServer() for each arg in the list.
Definition: RooAbsArg.cxx:371
void setValueDirty() const
Definition: RooAbsArg.h:441
RooAbsCacheElement is the abstract base class for objects to be stored in RooAbsCache cache manager o...
virtual RooArgList containedArgs(Action)
Returns all RooAbsArg objects contained in the cache element.
RooChangeTracker * paramTracker()
PdfCacheElem(const RooAbsCachedPdf &self, const RooArgSet *nset)
Constructor of cache object which owns RooDataHist cache histogram, RooHistPdf pdf that represents is...
virtual ~PdfCacheElem()
Cache element destructor.
RooChangeTracker * _paramTracker
virtual void printCompactTreeHook(std::ostream &, const char *, Int_t, Int_t)
Print contents of cache when printing self as part of object tree.
RooAbsCachedPdf is the abstract base class for p.d.f.s that need or want to cache their evaluate() ou...
Int_t getInterpolationOrder() const
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Implements internal (analytical) integration capabilities.
TString cacheNameSuffix(const RooArgSet &nset) const
Construct string with unique suffix for cache objects based on observable names that define cache con...
virtual RooAbsArg & pdfObservable(RooAbsArg &histObservable) const
virtual PdfCacheElem * createCache(const RooArgSet *nset) const
void setInterpolationOrder(Int_t order)
Change the interpolation order that is used in RooHistPdf cache representation smoothing the RooDataH...
virtual RooArgSet * actualParameters(const RooArgSet &nset) const =0
RooAICRegistry _anaReg
virtual TString histNameSuffix() const
virtual const char * binningName() const
virtual const char * inputBaseName() const =0
virtual Double_t getValV(const RooArgSet *set=0) const
Implementation of getVal() overriding default implementation of RooAbsPdf.
Bool_t _disableCache
Map for analytical integration codes.
PdfCacheElem * getCache(const RooArgSet *nset, Bool_t recalculate=kTRUE) const
Retrieve cache object associated with given choice of observables.
virtual Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet, const char *rangeName=0) const
Advertises internal (analytical) integration capabilities.
virtual ~RooAbsCachedPdf()
Destructor.
virtual Bool_t forceAnalyticalInt(const RooAbsArg &dep) const
Force RooRealIntegral to offer all our actual observable for internal integration.
RooAbsPdf * getCachePdf(const RooArgSet &nset) const
RooDataHist * getCacheHist(const RooArgSet &nset) const
virtual void fillCacheObject(PdfCacheElem &cache) const =0
void clearCacheObject(PdfCacheElem &cache) const
Mark all bins of given cache as unitialized (value -1)
RooObjCacheManager _cacheMgr
virtual const char * payloadUniqueSuffix() const
virtual RooArgSet * actualObservables(const RooArgSet &nset) const =0
Int_t getSize() const
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
TIterator * createIterator(Bool_t dir=kIterForward) const
Abstract base class for objects that are lvalues, i.e.
Definition: RooAbsLValue.h:26
virtual Double_t volume(const char *rangeName) const =0
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Analytical integral with normalization (see RooAbsReal::analyticalIntegralWN() for further informatio...
Definition: RooAbsPdf.cxx:322
RooAbsReal * _norm
Definition: RooAbsPdf.h:315
virtual Double_t getValV(const RooArgSet *set=0) const
Return current value, normalized by integrating over the observables in 'nset'.
Definition: RooAbsPdf.cxx:253
virtual Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet, const char *rangeName=0) const
Variant of getAnalyticalIntegral that is also passed the normalization set that should be applied to ...
Definition: RooAbsReal.cxx:319
Double_t _value
Definition: RooAbsReal.h:391
Double_t getVal(const RooArgSet *set=0) const
Evaluate object. Returns either cached value or triggers a recalculation.
Definition: RooAbsReal.h:64
virtual void preferredObservableScanOrder(const RooArgSet &obs, RooArgSet &orderedObs) const
Interface method for function objects to indicate their prefferred order of observables for scanning ...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
virtual void addClone(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:96
Int_t cacheSize() const
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
T * getObjByIndex(Int_t index) const
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=0)
RooChangeTracker is a meta object that tracks value changes in a given set of RooAbsArgs by registeri...
Bool_t hasChanged(Bool_t clearState)
Returns true if state has changes since last call with clearState=kTRUE If clearState is true,...
RooArgSet parameters() const
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
virtual void reset()
Reset all bin weights to zero.
virtual void add(const RooArgSet &row, Double_t wgt=1.0)
Definition: RooDataHist.h:65
void removeSelfFromDir()
Definition: RooDataHist.h:131
void setAllWeights(Double_t value)
Set all the event weight of all bins to the specified value.
Bool_t registerObject(const char *ownerName, const char *objectName, TObject &cacheObject, TIterator *paramIter)
Register object associated with given name and given associated parameters with given values in cache...
const TObject * retrieveObject(const char *name, TClass *tclass, const RooArgSet &params)
Retrieve object from cache that was registered under given name with given parameters,...
Bool_t haveUnitNorm() const
Definition: RooHistPdf.h:71
void setInterpolationOrder(Int_t order)
Definition: RooHistPdf.h:46
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer,...
Iterator abstract base class.
Definition: TIterator.h:30
virtual TObject * Next()=0
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Definition: first.py:1
STL namespace.