Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAbsCachedReal.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 RooAbsCachedReal.cxx
14\class RooAbsCachedReal
15\ingroup Roofitcore
16
17RooAbsCachedReal is the abstract base class for functions that need or
18want to cache their evaluate() output in a RooHistFunc defined in
19terms of the used observables. This base class manages the creation
20and storage of all RooHistFunc cache p.d.fs and the RooDataHists
21that define their shape. Implementations of RooAbsCachedReal must
22define member function fillCacheObject() which serves to fill an
23already created RooDataHist with the functions 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 "RooAbsCachedReal.h"
37#include "RooAbsReal.h"
38#include "RooMsgService.h"
39#include "RooDataHist.h"
40#include "RooHistFunc.h"
41#include "RooChangeTracker.h"
43
45
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Constructor
50
51RooAbsCachedReal::RooAbsCachedReal(const char *name, const char *title, Int_t ipOrder) :
52 RooAbsReal(name,title),
53 _cacheMgr(this,10),
54 _ipOrder(ipOrder),
55 _disableCache(kFALSE)
56 {
57 }
58
59
60
61////////////////////////////////////////////////////////////////////////////////
62/// Copy constructor
63
65 RooAbsReal(other,name),
66 _cacheMgr(other._cacheMgr,this),
67 _ipOrder(other._ipOrder),
68 _disableCache(other._disableCache)
69 {
70 }
71
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// Destructor
76
78{
79}
80
81
82
83////////////////////////////////////////////////////////////////////////////////
84/// Implementation of getVal() overriding default implementation
85/// of RooAbsReal. Return value stored in cache p.d.f
86/// rather than return value of evaluate() which is undefined
87/// for RooAbsCachedReal
88
90{
91 if (_disableCache) {
92 return RooAbsReal::getValV(nset) ;
93 }
94
95 // Cannot call cached p.d.f w.o nset
96 // if (!nset) return evaluate() ;
97
98 // Calculate current unnormalized value of object
99 // coverity[NULL_RETURNS]
100 FuncCacheElem* cache = getCache(nset) ;
101
102 _value = cache->func()->getVal() ;
103
104 return _value ;
105}
106
107
108
109////////////////////////////////////////////////////////////////////////////////
110/// Mark all bins as unitialized (value -1)
111
113{
114 cache.hist()->setAllWeights(-1) ;
115}
116
117
118
119////////////////////////////////////////////////////////////////////////////////
120/// Interface function to create an internal cache object that represent
121/// each cached function configuration. This interface allows to create and
122/// return a class derived from RooAbsCachedReal::FuncCacheElem so that
123/// a derived class fillCacheObject implementation can utilize extra functionality
124/// defined in such a derived cache class
125
127{
128 return new FuncCacheElem(const_cast<RooAbsCachedReal&>(*this),nset) ;
129}
130
131
132
133////////////////////////////////////////////////////////////////////////////////
134/// Retrieve cache corresponding to observables in nset
135
137{
138 // Check if this configuration was created becfore
139 Int_t sterileIdx(-1) ;
140 FuncCacheElem* cache = (FuncCacheElem*) _cacheMgr.getObj(nset,0,&sterileIdx) ;
141 if (cache) {
142 if (cache->paramTracker()->hasChanged(kTRUE)) {
143 ccoutD(Eval) << "RooAbsCachedReal::getCache(" << GetName() << ") cached function "
144 << cache->func()->GetName() << " requires recalculation as parameters changed" << endl ;
145 fillCacheObject(*cache) ;
146 cache->func()->setValueDirty() ;
147 }
148 return cache ;
149 }
150
151 cache = createCache(nset) ;
152
153 // Set cache function data to ADirty since function will need update every time in cache update process
154 RooFIter iarg( cache->hist()->get()->fwdIterator() );
155 RooAbsArg *arg(0);
156 while ( (arg=iarg.next()) ) {
157 arg->setOperMode(ADirty);
158 }
159
160 // Check if we have contents registered already in global expensive object cache
161 RooDataHist* htmp = (RooDataHist*) expensiveObjectCache().retrieveObject(cache->hist()->GetName(),RooDataHist::Class(),cache->paramTracker()->parameters()) ;
162
163 if (htmp) {
164
165 cache->hist()->reset() ;
166 cache->hist()->add(*htmp) ;
167
168 } else {
169
170 fillCacheObject(*cache) ;
171
172 RooDataHist* eoclone = new RooDataHist(*cache->hist()) ;
173 eoclone->removeSelfFromDir() ;
174 expensiveObjectCache().registerObject(GetName(),cache->hist()->GetName(),*eoclone,cache->paramTracker()->parameters()) ;
175 }
176
177 // Store this cache configuration
178 Int_t code = _cacheMgr.setObj(nset,0,((RooAbsCacheElement*)cache),0) ;
179 ccoutD(Caching) << "RooAbsCachedReal("<<this<<")::getCache(" << GetName() << ") creating new cache " << cache->func()->GetName() << " for nset " << (nset?*nset:RooArgSet()) << " with code " << code << endl ;
180
181 return cache ;
182}
183
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// Constructor of cache storage unit class
188///
189/// Create RooDataHist that will cache function values and create
190/// RooHistFunc that represent s RooDataHist shape as function, create
191/// meta object that tracks changes in declared parameters of p.d.f
192/// through actualParameters()
193
195{
196 // Disable source caching by default
198 _sourceClone = 0 ;
199
200 RooArgSet* nset2 = self.actualObservables(nset?*nset:RooArgSet()) ;
201
202 RooArgSet orderedObs ;
203 self.preferredObservableScanOrder(*nset2,orderedObs) ;
204
205 // Create RooDataHist
206 auto hname = std::string(self.inputBaseName()) + "_CACHEHIST" + self.cacheNameSuffix(*nset2).Data();
207
208 _hist = new RooDataHist(hname,hname,*nset2,self.binningName()) ;
210
211 RooArgSet* observables= self.actualObservables(*nset2) ;
212
213 // Create RooHistFunc
214 TString funcname = self.inputBaseName() ;
215 funcname.Append("_CACHE") ;
216 funcname.Append(self.cacheNameSuffix(*nset2)) ;
217 _func = new RooHistFunc(funcname,funcname,*observables,*_hist,self.getInterpolationOrder()) ;
218 if (self.operMode()==ADirty) _func->setOperMode(ADirty) ;
219
220 // Set initial state of cache to dirty
222
223 // Create pseudo-object that tracks changes in parameter values
224 RooArgSet* params = self.actualParameters(orderedObs) ;
225 string name= Form("%s_CACHEPARAMS",_func->GetName()) ;
226 _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,kTRUE) ;
227 _paramTracker->hasChanged(kTRUE) ; // clear dirty flag as cache is up-to-date upon creation
228
229 // Introduce formal dependency of RooHistFunc on parameters so that const optimization code
230 // makes the correct decisions
231 _func->addServerList(*params) ;
232
233
234 delete observables ;
235 delete params ;
236 delete nset2 ;
237
238}
239
240
241////////////////////////////////////////////////////////////////////////////////
242
244{
245 if (_sourceClone) { delete _sourceClone ; }
246 delete _paramTracker ;
247 delete _func ;
248 delete _hist ;
249}
250
251
252
253
254////////////////////////////////////////////////////////////////////////////////
255/// Construct unique suffix name for cache p.d.f object
256
258{
259 TString name ;
260 name.Append("_Obs[") ;
261 if (nset.getSize()>0) {
262 TIterator* iter = nset.createIterator() ;
263 RooAbsArg* arg ;
265 while((arg=(RooAbsArg*)iter->Next())) {
266 if (first) {
267 first=kFALSE ;
268 } else {
269 name.Append(",") ;
270 }
271 name.Append(arg->GetName()) ;
272 }
273 delete iter ;
274 }
275
276 name.Append("]") ;
277 const char* payloadUS = payloadUniqueSuffix() ;
278 if (payloadUS) {
279 name.Append(payloadUS) ;
280 }
281 return name ;
282}
283
284
285
286////////////////////////////////////////////////////////////////////////////////
287/// Set interpolation order of RooHistFunct representing cache histogram
288
290{
291 _ipOrder = order ;
292
293 for (Int_t i=0 ; i<_cacheMgr.cacheSize() ; i++) {
295 if (cache) {
296 cache->func()->setInterpolationOrder(order) ;
297 }
298 }
299}
300
301
302
303////////////////////////////////////////////////////////////////////////////////
304/// Return list of contained RooAbsArg objects
305
307{
308 RooArgList ret(*func()) ;
309
310 ret.add(*_paramTracker) ;
311 if (_sourceClone) {
312 ret.add(*_sourceClone) ;
313 }
314 return ret ;
315}
316
317
318////////////////////////////////////////////////////////////////////////////////
319/// Print contents of cache when printing self as part of object tree
320
321void RooAbsCachedReal::FuncCacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
322{
323 if (curElem==0) {
324 os << indent << "--- RooAbsCachedReal begin cache ---" << endl ;
325 }
326
327 TString indent2(indent) ;
328 indent2 += Form("[%d] ",curElem) ;
329 func()->printCompactTree(os,indent2) ;
330
331 if (curElem==maxElem) {
332 os << indent << "--- RooAbsCachedReal end cache --- " << endl ;
333 }
334}
335
336
337
338////////////////////////////////////////////////////////////////////////////////
339/// Return analytical integration capabilities of the RooHistFunc that corresponds to the set of observables in allVars
340
341Int_t RooAbsCachedReal::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
342{
343 FuncCacheElem* cache = getCache(normSet?normSet:&allVars) ;
344 Int_t code = cache->func()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
345 _anaIntMap[code].first = &allVars ;
346 _anaIntMap[code].second = normSet ;
347 return code ;
348}
349
350
351
352////////////////////////////////////////////////////////////////////////////////
353/// Forward call to implementation in relevant RooHistFunc instance
354
355Double_t RooAbsCachedReal::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
356{
357 if (code==0) {
358 return getVal(normSet) ;
359 }
360
361 const RooArgSet* anaVars = _anaIntMap[code].first ;
362 const RooArgSet* normSet2 = _anaIntMap[code].second ;
363
364 FuncCacheElem* cache = getCache(normSet2?normSet2:anaVars) ;
365 return cache->func()->analyticalIntegralWN(code,normSet,rangeName) ;
366
367}
368
369
370
371
372
#define ccoutD(a)
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:364
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
RooExpensiveObjectCache & expensiveObjectCache() const
friend class RooArgSet
Definition RooAbsArg.h:642
friend class RooHistFunc
Definition RooAbsArg.h:667
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Set the operation mode of this node.
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition RooAbsArg.h:505
void addServerList(RooAbsCollection &serverList, Bool_t valueProp=kTRUE, Bool_t shapeProp=kFALSE)
Register a list of RooAbsArg as servers to us by calling addServer() for each arg in the list.
OperMode operMode() const
Query the operation mode of this node.
Definition RooAbsArg.h:499
RooAbsCacheElement is the abstract base class for objects to be stored in RooAbsCache cache manager o...
virtual void printCompactTreeHook(std::ostream &, const char *, Int_t, Int_t)
Print contents of cache when printing self as part of object tree.
virtual RooArgList containedArgs(Action)
Return list of contained RooAbsArg objects.
RooChangeTracker * paramTracker()
FuncCacheElem(const RooAbsCachedReal &self, const RooArgSet *nset)
Constructor of cache storage unit class.
RooAbsCachedReal is the abstract base class for functions that need or want to cache their evaluate()...
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Forward call to implementation in relevant RooHistFunc instance.
void setInterpolationOrder(Int_t order)
Set interpolation order of RooHistFunct representing cache histogram.
virtual void fillCacheObject(FuncCacheElem &cache) const =0
virtual const char * inputBaseName() const =0
virtual Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet, const char *rangeName=0) const
Return analytical integration capabilities of the RooHistFunc that corresponds to the set of observab...
virtual FuncCacheElem * createCache(const RooArgSet *nset) const
Interface function to create an internal cache object that represent each cached function configurati...
TString cacheNameSuffix(const RooArgSet &nset) const
Construct unique suffix name for cache p.d.f object.
FuncCacheElem * getCache(const RooArgSet *nset) const
Retrieve cache corresponding to observables in nset.
Bool_t _disableCache
Map for analytical integration codes.
virtual Double_t getValV(const RooArgSet *set=0) const
Implementation of getVal() overriding default implementation of RooAbsReal.
RooObjCacheManager _cacheMgr
void clearCacheObject(FuncCacheElem &cache) const
Mark all bins as unitialized (value -1)
Int_t _ipOrder
The cache manager.
Int_t getInterpolationOrder() const
virtual RooArgSet * actualParameters(const RooArgSet &nset) const =0
std::map< Int_t, std::pair< const RooArgSet *, const RooArgSet * > > _anaIntMap
virtual const char * payloadUniqueSuffix() const
virtual RooArgSet * actualObservables(const RooArgSet &nset) const =0
virtual ~RooAbsCachedReal()
Destructor.
virtual const char * binningName() const
Int_t getSize() const
RooFIter fwdIterator() const
One-time forward iterator.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
RooAbsArg * first() const
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:64
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 ...
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
Double_t _value
Definition RooAbsReal.h:484
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:94
virtual void preferredObservableScanOrder(const RooArgSet &obs, RooArgSet &orderedObs) const
Interface method for function objects to indicate their preferred order of observables for scanning t...
virtual Double_t getValV(const RooArgSet *normalisationSet=nullptr) const
Return value of object.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
Int_t cacheSize() const
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
T * getObjByIndex(Int_t index) const
Retrieve payload object by slot index.
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 changed since last call with clearState=kTRUE.
RooArgSet parameters() const
The RooDataHist is a container class to hold N-dimensional binned data.
Definition RooDataHist.h:45
virtual void add(const RooArgSet &row, Double_t wgt=1.0)
Add wgt to the bin content enclosed by the coordinates passed in row.
Definition RooDataHist.h:74
void removeSelfFromDir()
void setAllWeights(Double_t value)
Set all the event weight of all bins to the specified value.
void reset() override
Reset all bin weights to zero.
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:84
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,...
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooAbsArg * next()
Return next element or nullptr if at end.
void setInterpolationOrder(Int_t order)
Set histogram interpolation order.
Definition RooHistFunc.h:54
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
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
TString & Append(const char *cs)
Definition TString.h:564
Definition first.py:1