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