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 "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/// Interface function to create an internal cache object that represent
109/// each cached function configuration. This interface allows to create and
110/// return a class derived from RooAbsCachedReal::FuncCacheElem so that
111/// a derived class fillCacheObject implementation can utilize extra functionality
112/// defined in such a derived cache class
113
115{
116 return new FuncCacheElem(const_cast<RooAbsCachedReal&>(*this),nset) ;
117}
118
119
120
121////////////////////////////////////////////////////////////////////////////////
122/// Retrieve cache corresponding to observables in nset
123
125{
126 // Check if this configuration was created becfore
127 Int_t sterileIdx(-1) ;
128 FuncCacheElem* cache = (FuncCacheElem*) _cacheMgr.getObj(nset,0,&sterileIdx) ;
129 if (cache) {
130 if (cache->paramTracker()->hasChanged(true)) {
131 ccoutD(Eval) << "RooAbsCachedReal::getCache(" << GetName() << ") cached function "
132 << cache->func()->GetName() << " requires recalculation as parameters changed" << endl ;
133 fillCacheObject(*cache) ;
134 cache->func()->setValueDirty() ;
135 }
136 return cache ;
137 }
138
139 cache = createCache(nset) ;
140
141 // Set cache function data to ADirty since function will need update every time in cache update process
142 for (auto* arg : *(cache->hist()->get()) ) {
143 arg->setOperMode(ADirty);
144 }
145
146 // Check if we have contents registered already in global expensive object cache
148
149 if (htmp) {
150
151 cache->hist()->reset() ;
152 cache->hist()->add(*htmp) ;
153
154 } else {
155
156 fillCacheObject(*cache) ;
157
158 RooDataHist* eoclone = new RooDataHist(*cache->hist()) ;
159 eoclone->removeSelfFromDir() ;
160 expensiveObjectCache().registerObject(GetName(),cache->hist()->GetName(),*eoclone,cache->paramTracker()->parameters()) ;
161 }
162
163 // Store this cache configuration
164 Int_t code = _cacheMgr.setObj(nset,0,((RooAbsCacheElement*)cache),0) ;
165 ccoutD(Caching) << "RooAbsCachedReal("<<this<<")::getCache(" << GetName() << ") creating new cache " << cache->func()->GetName() << " for nset " << (nset?*nset:RooArgSet()) << " with code " << code << endl ;
166
167 return cache ;
168}
169
170
171
172////////////////////////////////////////////////////////////////////////////////
173/// Constructor of cache storage unit class
174///
175/// Create RooDataHist that will cache function values and create
176/// RooHistFunc that represent s RooDataHist shape as function, create
177/// meta object that tracks changes in declared parameters of p.d.f
178/// through actualParameters()
179
181{
182 // Disable source caching by default
183 _cacheSource = false ;
184 _sourceClone = 0 ;
185
186 std::unique_ptr<RooArgSet> nset2{self.actualObservables(nset?*nset:RooArgSet())};
187
188 RooArgSet orderedObs ;
189 self.preferredObservableScanOrder(*nset2,orderedObs) ;
190
191 // Create RooDataHist
192 auto hname = std::string(self.inputBaseName()) + "_CACHEHIST" + self.cacheNameSuffix(*nset2).Data();
193
194 _hist = new RooDataHist(hname,hname,*nset2,self.binningName()) ;
196
197 std::unique_ptr<RooArgSet> observables{self.actualObservables(*nset2)};
198
199 // Create RooHistFunc
200 TString funcname = self.inputBaseName() ;
201 funcname.Append("_CACHE") ;
202 funcname.Append(self.cacheNameSuffix(*nset2)) ;
203 _func = new RooHistFunc(funcname,funcname,*observables,*_hist,self.getInterpolationOrder()) ;
204 if (self.operMode()==ADirty) _func->setOperMode(ADirty) ;
205
206 // Set initial state of cache to dirty
208
209 // Create pseudo-object that tracks changes in parameter values
210 std::unique_ptr<RooArgSet> params{self.actualParameters(orderedObs)};
211 string name= Form("%s_CACHEPARAMS",_func->GetName()) ;
212 _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,true) ;
213 _paramTracker->hasChanged(true) ; // clear dirty flag as cache is up-to-date upon creation
214
215 // Introduce formal dependency of RooHistFunc on parameters so that const optimization code
216 // makes the correct decisions
217 _func->addServerList(*params) ;
218}
219
220
221////////////////////////////////////////////////////////////////////////////////
222
224{
225 if (_sourceClone) { delete _sourceClone ; }
226 delete _paramTracker ;
227 delete _func ;
228 delete _hist ;
229}
230
231
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Construct unique suffix name for cache p.d.f object
236
238{
239 TString name ;
240 name.Append("_Obs[") ;
241 if (nset.getSize()>0) {
242 bool first(true) ;
243 for (RooAbsArg * arg : nset) {
244 if (first) {
245 first=false ;
246 } else {
247 name.Append(",") ;
248 }
249 name.Append(arg->GetName()) ;
250 }
251 }
252
253 name.Append("]") ;
254 const char* payloadUS = payloadUniqueSuffix() ;
255 if (payloadUS) {
256 name.Append(payloadUS) ;
257 }
258 return name ;
259}
260
261
262
263////////////////////////////////////////////////////////////////////////////////
264/// Set interpolation order of RooHistFunct representing cache histogram
265
267{
268 _ipOrder = order ;
269
270 for (Int_t i=0 ; i<_cacheMgr.cacheSize() ; i++) {
272 if (cache) {
273 cache->func()->setInterpolationOrder(order) ;
274 }
275 }
276}
277
278
279
280////////////////////////////////////////////////////////////////////////////////
281/// Return list of contained RooAbsArg objects
282
284{
285 RooArgList ret(*func()) ;
286
287 ret.add(*_paramTracker) ;
288 if (_sourceClone) {
289 ret.add(*_sourceClone) ;
290 }
291 return ret ;
292}
293
294
295////////////////////////////////////////////////////////////////////////////////
296/// Print contents of cache when printing self as part of object tree
297
298void RooAbsCachedReal::FuncCacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
299{
300 if (curElem==0) {
301 os << indent << "--- RooAbsCachedReal begin cache ---" << endl ;
302 }
303
304 TString indent2(indent) ;
305 indent2 += Form("[%d] ",curElem) ;
306 func()->printCompactTree(os,indent2) ;
307
308 if (curElem==maxElem) {
309 os << indent << "--- RooAbsCachedReal end cache --- " << endl ;
310 }
311}
312
313
314
315////////////////////////////////////////////////////////////////////////////////
316/// Return analytical integration capabilities of the RooHistFunc that corresponds to the set of observables in allVars
317
318Int_t RooAbsCachedReal::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
319{
320 FuncCacheElem* cache = getCache(normSet?normSet:&allVars) ;
321 Int_t code = cache->func()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
322 _anaIntMap[code].first = &allVars ;
323 _anaIntMap[code].second = normSet ;
324 return code ;
325}
326
327
328
329////////////////////////////////////////////////////////////////////////////////
330/// Forward call to implementation in relevant RooHistFunc instance
331
332double RooAbsCachedReal::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
333{
334 if (code==0) {
335 return getVal(normSet) ;
336 }
337
338 const RooArgSet* anaVars = _anaIntMap[code].first ;
339 const RooArgSet* normSet2 = _anaIntMap[code].second ;
340
341 FuncCacheElem* cache = getCache(normSet2?normSet2:anaVars) ;
342 return cache->func()->analyticalIntegralWN(code,normSet,rangeName) ;
343
344}
345
346
347
348
349
RooAbsReal * _func
Definition RooMinuit.h:90
#define ccoutD(a)
#define ClassImp(name)
Definition Rtypes.h:377
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:2467
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
RooExpensiveObjectCache & expensiveObjectCache() const
void setOperMode(OperMode mode, bool recurseADirty=true)
Set the operation mode of this node.
friend class RooHistFunc
Definition RooAbsArg.h:644
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.
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition RooAbsArg.h:487
OperMode operMode() const
Query the operation mode of this node.
Definition RooAbsArg.h:481
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...
virtual RooFit::OwningPtr< RooArgSet > actualParameters(const RooArgSet &nset) const =0
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
Int_t _ipOrder
Interpolation order for cache histograms.
~RooAbsCachedReal() override
Destructor.
Int_t getInterpolationOrder() const
virtual RooFit::OwningPtr< RooArgSet > actualObservables(const RooArgSet &nset) const =0
std::map< Int_t, std::pair< const RooArgSet *, const RooArgSet * > > _anaIntMap
! Map for analytical integration codes
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
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.
RooAbsArg * first() const
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.
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 ...
double _value
Cache for current value of object.
Definition RooAbsReal.h:509
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.
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:55
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:39
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:66
static TClass * Class()
void removeSelfFromDir()
void reset() override
Reset all bin weights to zero.
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:76
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:63
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
TString & Append(const char *cs)
Definition TString.h:576
Definition first.py:1