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
17Abstract 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 <string>
32#include <ostream>
33 using std::string, std::endl, std::ostream;
34
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
44////////////////////////////////////////////////////////////////////////////////
45/// Constructor
46
47RooAbsCachedReal::RooAbsCachedReal(const char *name, const char *title, Int_t ipOrder)
48 : RooAbsReal(name, title), _cacheMgr(this, 10), _ipOrder(ipOrder), _disableCache(false)
49{
50}
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55/// Copy constructor
56
59 _cacheMgr(other._cacheMgr,this),
60 _ipOrder(other._ipOrder),
61 _disableCache(other._disableCache)
62 {
63 }
64
65////////////////////////////////////////////////////////////////////////////////
66/// Implementation of getVal() overriding default implementation
67/// of RooAbsReal. Return value stored in cache p.d.f
68/// rather than return value of evaluate() which is undefined
69/// for RooAbsCachedReal
70
71double RooAbsCachedReal::getValV(const RooArgSet* nset) const
72{
73 if (_disableCache) {
74 return RooAbsReal::getValV(nset) ;
75 }
76
77 // Cannot call cached p.d.f w.o nset
78 // if (!nset) return evaluate() ;
79
80 // Calculate current unnormalized value of object
81 // coverity[NULL_RETURNS]
82 FuncCacheElem* cache = getCache(nset) ;
83
84 _value = cache->func()->getVal() ;
85
86 return _value ;
87}
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// Interface function to create an internal cache object that represent
92/// each cached function configuration. This interface allows to create and
93/// return a class derived from RooAbsCachedReal::FuncCacheElem so that
94/// a derived class fillCacheObject implementation can utilize extra functionality
95/// defined in such a derived cache class
96
98{
99 return new FuncCacheElem(const_cast<RooAbsCachedReal&>(*this),nset) ;
100}
101
102
103
104////////////////////////////////////////////////////////////////////////////////
105/// Retrieve cache corresponding to observables in nset
106
108{
109 // Check if this configuration was created becfore
110 Int_t sterileIdx(-1) ;
111 FuncCacheElem* cache = static_cast<FuncCacheElem*>(_cacheMgr.getObj(nset,nullptr,&sterileIdx)) ;
112 if (cache) {
113 if (cache->paramTracker()->hasChanged(true)) {
114 ccoutD(Eval) << "RooAbsCachedReal::getCache(" << GetName() << ") cached function "
115 << cache->func()->GetName() << " requires recalculation as parameters changed" << std::endl ;
116 fillCacheObject(*cache) ;
117 cache->func()->setValueDirty() ;
118 }
119 return cache ;
120 }
121
122 cache = createCache(nset) ;
123
124 // Set cache function data to ADirty since function will need update every time in cache update process
125 for (auto* arg : *(cache->hist()->get()) ) {
126 arg->setOperMode(ADirty);
127 }
128
129 fillCacheObject(*cache) ;
130
131 RooDataHist* eoclone = new RooDataHist(*cache->hist()) ;
132 eoclone->removeSelfFromDir() ;
133
134 // Store this cache configuration
135 Int_t code = _cacheMgr.setObj(nset,nullptr,((RooAbsCacheElement*)cache),nullptr) ;
136 ccoutD(Caching) << "RooAbsCachedReal("<<this<<")::getCache(" << GetName() << ") creating new cache " << cache->func()->GetName() << " for nset " << (nset?*nset:RooArgSet()) << " with code " << code << std::endl ;
137
138 return cache ;
139}
140
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Constructor of cache storage unit class
145///
146/// Create RooDataHist that will cache function values and create
147/// RooHistFunc that represent s RooDataHist shape as function, create
148/// meta object that tracks changes in declared parameters of p.d.f
149/// through actualParameters()
150
152 : _sourceClone(nullptr), _cacheSource(false)
153{
154 // Disable source caching by default
155
156 std::unique_ptr<RooArgSet> nset2{self.actualObservables(nset?*nset:RooArgSet())};
157
159 self.preferredObservableScanOrder(*nset2,orderedObs) ;
160
161 // Create RooDataHist
162 auto hname = std::string(self.inputBaseName()) + "_CACHEHIST" + self.cacheNameSuffix(*nset2).Data();
163
164 _hist = new RooDataHist(hname,hname,*nset2,self.binningName()) ;
166
167 std::unique_ptr<RooArgSet> observables{self.actualObservables(*nset2)};
168
169 // Create RooHistFunc
170 TString funcname = self.inputBaseName() ;
171 funcname.Append("_CACHE") ;
172 funcname.Append(self.cacheNameSuffix(*nset2)) ;
173 _func = new RooHistFunc(funcname,funcname,*observables,*_hist,self.getInterpolationOrder()) ;
174 if (self.operMode()==ADirty) _func->setOperMode(ADirty) ;
175
176 // Set initial state of cache to dirty
178
179 // Create pseudo-object that tracks changes in parameter values
180 std::unique_ptr<RooArgSet> params{self.actualParameters(orderedObs)};
181 string name= Form("%s_CACHEPARAMS",_func->GetName()) ;
182 _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,true) ;
183 _paramTracker->hasChanged(true) ; // clear dirty flag as cache is up-to-date upon creation
184
185 // Introduce formal dependency of RooHistFunc on parameters so that const optimization code
186 // makes the correct decisions
187 _func->addServerList(*params) ;
188}
189
190
191////////////////////////////////////////////////////////////////////////////////
192
194{
195 if (_sourceClone) { delete _sourceClone ; }
196 delete _paramTracker ;
197 delete _func ;
198 delete _hist ;
199}
200
201
202
203
204////////////////////////////////////////////////////////////////////////////////
205/// Construct unique suffix name for cache p.d.f object
206
208{
209 TString name ;
210 name.Append("_Obs[") ;
211 if (!nset.empty()) {
212 bool first(true) ;
213 for (RooAbsArg * arg : nset) {
214 if (first) {
215 first=false ;
216 } else {
217 name.Append(",") ;
218 }
219 name.Append(arg->GetName()) ;
220 }
221 }
222
223 name.Append("]") ;
224 const char* payloadUS = payloadUniqueSuffix() ;
225 if (payloadUS) {
226 name.Append(payloadUS) ;
227 }
228 return name ;
229}
230
231
232
233////////////////////////////////////////////////////////////////////////////////
234/// Set interpolation order of RooHistFunct representing cache histogram
235
237{
238 _ipOrder = order ;
239
240 for (Int_t i=0 ; i<_cacheMgr.cacheSize() ; i++) {
241 FuncCacheElem* cache = static_cast<FuncCacheElem*>(_cacheMgr.getObjByIndex(i)) ;
242 if (cache) {
243 cache->func()->setInterpolationOrder(order) ;
244 }
245 }
246}
247
248
249
250////////////////////////////////////////////////////////////////////////////////
251/// Return list of contained RooAbsArg objects
252
254{
255 RooArgList ret(*func()) ;
256
257 ret.add(*_paramTracker) ;
258 if (_sourceClone) {
259 ret.add(*_sourceClone) ;
260 }
261 return ret ;
262}
263
264
265////////////////////////////////////////////////////////////////////////////////
266/// Print contents of cache when printing self as part of object tree
267
269{
270 if (curElem==0) {
271 os << indent << "--- RooAbsCachedReal begin cache ---" << std::endl ;
272 }
273
275 indent2 += Form("[%d] ",curElem) ;
276 func()->printCompactTree(os,indent2) ;
277
278 if (curElem==maxElem) {
279 os << indent << "--- RooAbsCachedReal end cache --- " << std::endl ;
280 }
281}
282
283
284
285////////////////////////////////////////////////////////////////////////////////
286/// Return analytical integration capabilities of the RooHistFunc that corresponds to the set of observables in allVars
287
289{
290 FuncCacheElem* cache = getCache(normSet?normSet:&allVars) ;
291 Int_t code = cache->func()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
292 _anaIntMap[code].first = &allVars ;
293 _anaIntMap[code].second = normSet ;
294 return code ;
295}
296
297
298
299////////////////////////////////////////////////////////////////////////////////
300/// Forward call to implementation in relevant RooHistFunc instance
301
303{
304 if (code==0) {
305 return getVal(normSet) ;
306 }
307
308 const RooArgSet* anaVars = _anaIntMap[code].first ;
309 const RooArgSet* normSet2 = _anaIntMap[code].second ;
310
312 return cache->func()->analyticalIntegralWN(code,normSet,rangeName) ;
313
314}
315
316
317
318
319
#define ccoutD(a)
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
void setOperMode(OperMode mode, bool recurseADirty=true)
Set the operation mode of this node.
friend class RooHistFunc
Definition RooAbsArg.h:558
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:404
Abstract base class for objects to be stored in RooAbsCache cache manager objects.
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.
Abstract base class for functions that need or want to cache their evaluate() output in a RooHistFunc...
void setInterpolationOrder(Int_t order)
Set interpolation order of RooHistFunct representing cache histogram.
virtual void fillCacheObject(FuncCacheElem &cache) 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
Int_t _ipOrder
Interpolation order for cache histograms.
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
double getValV(const RooArgSet *set=nullptr) const override
Implementation of getVal() overriding default implementation of RooAbsReal.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
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:541
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:24
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.
Meta object that tracks value changes in a given set of RooAbsArgs by registering itself as value cli...
bool hasChanged(bool clearState)
Returns true if state has changed since last call with clearState=true.
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:40
void removeSelfFromDir()
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:82
void setInterpolationOrder(Int_t order)
Set histogram interpolation order.
Definition RooHistFunc.h:61
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Basic string class.
Definition TString.h:137