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 
17 RooAbsCachedReal is the abstract base class for functions that need or
18 want to cache their evaluate() output in a RooHistFunc defined in
19 terms of the used observables. This base class manages the creation
20 and storage of all RooHistFunc cache p.d.fs and the RooDataHists
21 that define their shape. Implementations of RooAbsCachedReal must
22 define member function fillCacheObject() which serves to fill an
23 already created RooDataHist with the functions function values. In
24 addition the member functions actualObservables() and
25 actualParameters() must be define which report what the actual
26 observables to be cached are for a given set of observables passed
27 by the user to getVal() and on which parameters need to be tracked
28 for changes to trigger a refilling of the cache histogram.
29 **/
30 
31 #include "Riostream.h"
32 using 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 
51 RooAbsCachedReal::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
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
197  _cacheSource = kFALSE ;
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  TString hname = self.inputBaseName() ;
207  hname.Append("_CACHEHIST") ;
208  hname.Append(self.cacheNameSuffix(*nset2)) ;
209 
210  _hist = new RooDataHist(hname,hname,*nset2,self.binningName()) ;
212 
213  RooArgSet* observables= self.actualObservables(*nset2) ;
214 
215  // Create RooHistFunc
216  TString funcname = self.inputBaseName() ;
217  funcname.Append("_CACHE") ;
218  funcname.Append(self.cacheNameSuffix(*nset2)) ;
219  _func = new RooHistFunc(funcname,funcname,*observables,*_hist,self.getInterpolationOrder()) ;
220  if (self.operMode()==ADirty) _func->setOperMode(ADirty) ;
221 
222  // Set initial state of cache to dirty
223  _func->setValueDirty() ;
224 
225  // Create pseudo-object that tracks changes in parameter values
226  RooArgSet* params = self.actualParameters(orderedObs) ;
227  string name= Form("%s_CACHEPARAMS",_func->GetName()) ;
228  _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,kTRUE) ;
229  _paramTracker->hasChanged(kTRUE) ; // clear dirty flag as cache is up-to-date upon creation
230 
231  // Introduce formal dependency of RooHistFunc on parameters so that const optimization code
232  // makes the correct decisions
233  _func->addServerList(*params) ;
234 
235 
236  delete observables ;
237  delete params ;
238  delete nset2 ;
239 
240 }
241 
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 
246 {
247  if (_sourceClone) { delete _sourceClone ; }
248  delete _paramTracker ;
249  delete _func ;
250  delete _hist ;
251 }
252 
253 
254 
255 
256 ////////////////////////////////////////////////////////////////////////////////
257 /// Construct unique suffix name for cache p.d.f object
258 
260 {
261  TString name ;
262  name.Append("_Obs[") ;
263  if (nset.getSize()>0) {
264  TIterator* iter = nset.createIterator() ;
265  RooAbsArg* arg ;
266  Bool_t first(kTRUE) ;
267  while((arg=(RooAbsArg*)iter->Next())) {
268  if (first) {
269  first=kFALSE ;
270  } else {
271  name.Append(",") ;
272  }
273  name.Append(arg->GetName()) ;
274  }
275  delete iter ;
276  }
277 
278  name.Append("]") ;
279  const char* payloadUS = payloadUniqueSuffix() ;
280  if (payloadUS) {
281  name.Append(payloadUS) ;
282  }
283  return name ;
284 }
285 
286 
287 
288 ////////////////////////////////////////////////////////////////////////////////
289 /// Set interpolation order of RooHistFunct representing cache histogram
290 
292 {
293  _ipOrder = order ;
294 
295  for (Int_t i=0 ; i<_cacheMgr.cacheSize() ; i++) {
297  if (cache) {
298  cache->func()->setInterpolationOrder(order) ;
299  }
300  }
301 }
302 
303 
304 
305 ////////////////////////////////////////////////////////////////////////////////
306 /// Return list of contained RooAbsArg objects
307 
309 {
310  RooArgList ret(*func()) ;
311 
312  ret.add(*_paramTracker) ;
313  if (_sourceClone) {
314  ret.add(*_sourceClone) ;
315  }
316  return ret ;
317 }
318 
319 
320 ////////////////////////////////////////////////////////////////////////////////
321 /// Print contents of cache when printing self as part of object tree
322 
323 void RooAbsCachedReal::FuncCacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
324 {
325  if (curElem==0) {
326  os << indent << "--- RooAbsCachedReal begin cache ---" << endl ;
327  }
328 
329  TString indent2(indent) ;
330  indent2 += Form("[%d] ",curElem) ;
331  func()->printCompactTree(os,indent2) ;
332 
333  if (curElem==maxElem) {
334  os << indent << "--- RooAbsCachedReal end cache --- " << endl ;
335  }
336 }
337 
338 
339 
340 ////////////////////////////////////////////////////////////////////////////////
341 /// Return analytical integration capabilities of the RooHistFunc that corresponds to the set of observables in allVars
342 
343 Int_t RooAbsCachedReal::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
344 {
345  FuncCacheElem* cache = getCache(normSet?normSet:&allVars) ;
346  Int_t code = cache->func()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
347  _anaIntMap[code].first = &allVars ;
348  _anaIntMap[code].second = normSet ;
349  return code ;
350 }
351 
352 
353 
354 ////////////////////////////////////////////////////////////////////////////////
355 /// Forward call to implementation in relevant RooHistFunc instance
356 
357 Double_t RooAbsCachedReal::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
358 {
359  if (code==0) {
360  return getVal(normSet) ;
361  }
362 
363  const RooArgSet* anaVars = _anaIntMap[code].first ;
364  const RooArgSet* normSet2 = _anaIntMap[code].second ;
365 
366  FuncCacheElem* cache = getCache(normSet2?normSet2:anaVars) ;
367  return cache->func()->analyticalIntegralWN(code,normSet,rangeName) ;
368 
369 }
370 
371 
372 
373 
374 
RooAbsCachedReal::_cacheMgr
RooObjCacheManager _cacheMgr
Definition: RooAbsCachedReal.h:103
RooExpensiveObjectCache::registerObject
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...
Definition: RooExpensiveObjectCache.cxx:110
RooCacheManager::cacheSize
Int_t cacheSize() const
Definition: RooCacheManager.h:69
RooCacheManager::setObj
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=0)
Definition: RooCacheManager.h:49
RooHistFunc::setInterpolationOrder
void setInterpolationOrder(Int_t order)
Set histogram interpolation order.
Definition: RooHistFunc.h:50
RooAbsArg::ADirty
@ ADirty
Definition: RooAbsArg.h:491
RooAbsCachedReal::getAnalyticalIntegralWN
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...
Definition: RooAbsCachedReal.cxx:343
first
Definition: first.py:1
RooAbsCachedReal::FuncCacheElem::FuncCacheElem
FuncCacheElem(const RooAbsCachedReal &self, const RooArgSet *nset)
Constructor of cache storage unit class.
Definition: RooAbsCachedReal.cxx:194
RooAbsArg::operMode
OperMode operMode() const
Definition: RooAbsArg.h:492
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooAbsReal.h
RooMsgService.h
ccoutD
#define ccoutD(a)
Definition: RooMsgService.h:37
RooDataHist::setAllWeights
void setAllWeights(Double_t value)
Set all the event weight of all bins to the specified value.
Definition: RooDataHist.cxx:1833
RooAbsCachedReal::_ipOrder
Int_t _ipOrder
The cache manager.
Definition: RooAbsCachedReal.h:106
RooAbsCachedReal::FuncCacheElem::_sourceClone
RooAbsReal * _sourceClone
Definition: RooAbsCachedReal.h:83
RooFit.h
RooHistFunc.h
RooAbsCachedReal::FuncCacheElem::func
RooHistFunc * func()
Definition: RooAbsCachedReal.h:68
RooCacheManager::getObj
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
Definition: RooCacheManager.h:44
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
Form
char * Form(const char *fmt,...)
RooAbsReal::getValV
virtual Double_t getValV(const RooArgSet *normalisationSet=nullptr) const
Return value of object.
Definition: RooAbsReal.cxx:271
RooAbsCollection::fwdIterator
RooFIter fwdIterator() const
One-time forward iterator.
Definition: RooAbsCollection.h:133
RooAbsCachedReal::~RooAbsCachedReal
virtual ~RooAbsCachedReal()
Destructor.
Definition: RooAbsCachedReal.cxx:77
RooArgList
Definition: RooArgList.h:21
RooAbsCachedReal::RooAbsCachedReal
RooAbsCachedReal()
Definition: RooAbsCachedReal.h:27
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooDataHist::get
virtual const RooArgSet * get() const
Definition: RooDataHist.h:78
RooAbsCachedReal::binningName
virtual const char * binningName() const
Definition: RooAbsCachedReal.h:93
RooAbsCachedReal::getCache
FuncCacheElem * getCache(const RooArgSet *nset) const
Retrieve cache corresponding to observables in nset.
Definition: RooAbsCachedReal.cxx:136
RooAbsArg::setOperMode
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Change cache operation mode to given mode.
Definition: RooAbsArg.cxx:1720
RooAbsCachedReal::fillCacheObject
virtual void fillCacheObject(FuncCacheElem &cache) const =0
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
RooAbsReal
Definition: RooAbsReal.h:61
RooAbsCachedReal::FuncCacheElem::_cacheSource
Bool_t _cacheSource
Definition: RooAbsCachedReal.h:84
RooAbsCachedReal::payloadUniqueSuffix
virtual const char * payloadUniqueSuffix() const
Definition: RooAbsCachedReal.h:90
RooAbsCacheElement
Definition: RooAbsCacheElement.h:26
TString
Definition: TString.h:136
RooAbsArg::expensiveObjectCache
RooExpensiveObjectCache & expensiveObjectCache() const
Definition: RooAbsArg.cxx:2170
RooExpensiveObjectCache::retrieveObject
const TObject * retrieveObject(const char *name, TClass *tclass, const RooArgSet &params)
Retrieve object from cache that was registered under given name with given parameters,...
Definition: RooExpensiveObjectCache.cxx:132
TString.h
bool
TIterator
Definition: TIterator.h:30
RooAbsCachedReal::getInterpolationOrder
Int_t getInterpolationOrder() const
Definition: RooAbsCachedReal.h:39
RooDataHist::add
virtual void add(const RooArgSet &row, Double_t wgt=1.0)
Definition: RooDataHist.h:66
RooChangeTracker
Definition: RooChangeTracker.h:26
RooAbsCachedReal::clearCacheObject
void clearCacheObject(FuncCacheElem &cache) const
Mark all bins as unitialized (value -1)
Definition: RooAbsCachedReal.cxx:112
RooAbsCachedReal::FuncCacheElem
friend class FuncCacheElem
Definition: RooAbsCachedReal.h:92
RooChangeTracker::parameters
RooArgSet parameters() const
Definition: RooChangeTracker.cxx:216
RooAbsCachedReal::FuncCacheElem::~FuncCacheElem
virtual ~FuncCacheElem()
Definition: RooAbsCachedReal.cxx:245
RooDataHist
Definition: RooDataHist.h:39
RooCacheManager::getObjByIndex
T * getObjByIndex(Int_t index) const
Retrieve payload object by slot index.
Definition: RooCacheManager.h:308
RooDataHist::removeSelfFromDir
void removeSelfFromDir()
Definition: RooDataHist.h:140
RooAbsCachedReal::_disableCache
Bool_t _disableCache
Map for analytical integration codes.
Definition: RooAbsCachedReal.h:115
RooAbsCachedReal::FuncCacheElem::containedArgs
virtual RooArgList containedArgs(Action)
Return list of contained RooAbsArg objects.
Definition: RooAbsCachedReal.cxx:308
RooAbsReal::analyticalIntegralWN
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
Definition: RooAbsReal.cxx:401
RooFIter
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
Definition: RooLinkedListIter.h:39
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
RooDataHist.h
RooAbsArg::RooHistFunc
friend class RooHistFunc
Definition: RooAbsArg.h:603
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:118
RooFIter::next
RooAbsArg * next()
Return next element or nullptr if at end.
Definition: RooLinkedListIter.h:49
RooAbsArg::RooArgSet
friend class RooArgSet
Definition: RooAbsArg.h:579
RooAbsCachedReal::FuncCacheElem::hist
RooDataHist * hist()
Definition: RooAbsCachedReal.h:69
RooAbsCollection::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:437
RooAbsReal::_value
Double_t _value
Definition: RooAbsReal.h:450
RooChangeTracker::hasChanged
Bool_t hasChanged(Bool_t clearState)
Returns true if state has changed since last call with clearState=kTRUE.
Definition: RooChangeTracker.cxx:120
RooAbsCachedReal::analyticalIntegralWN
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Forward call to implementation in relevant RooHistFunc instance.
Definition: RooAbsCachedReal.cxx:357
RooAbsCachedReal::createCache
virtual FuncCacheElem * createCache(const RooArgSet *nset) const
Interface function to create an internal cache object that represent each cached function configurati...
Definition: RooAbsCachedReal.cxx:126
TIterator::Next
virtual TObject * Next()=0
RooAbsCachedReal::setInterpolationOrder
void setInterpolationOrder(Int_t order)
Set interpolation order of RooHistFunct representing cache histogram.
Definition: RooAbsCachedReal.cxx:291
RooAbsCachedReal::FuncCacheElem::_paramTracker
RooChangeTracker * _paramTracker
Definition: RooAbsCachedReal.h:81
RooExpensiveObjectCache.h
RooAbsCachedReal::cacheNameSuffix
TString cacheNameSuffix(const RooArgSet &nset) const
Construct unique suffix name for cache p.d.f object.
Definition: RooAbsCachedReal.cxx:259
RooAbsReal::getAnalyticalIntegralWN
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:372
Double_t
double Double_t
Definition: RtypesCore.h:59
RooAbsCachedReal.h
RooChangeTracker.h
RooAbsCacheElement::Action
Action
Definition: RooAbsCacheElement.h:39
RooDataHist::reset
virtual void reset()
Reset all bin weights to zero.
Definition: RooDataHist.cxx:1767
name
char name[80]
Definition: TGX11.cxx:110
RooFit::Caching
@ Caching
Definition: RooGlobalFunc.h:68
RooAbsArg
Definition: RooAbsArg.h:73
RooAbsCachedReal::FuncCacheElem::_hist
RooDataHist * _hist
Definition: RooAbsCachedReal.h:82
RooAbsArg::setValueDirty
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:505
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
Class
void Class()
Definition: Class.C:29
RooFit::Eval
@ Eval
Definition: RooGlobalFunc.h:68
Riostream.h
RooAbsCachedReal::getValV
virtual Double_t getValV(const RooArgSet *set=0) const
Implementation of getVal() overriding default implementation of RooAbsReal.
Definition: RooAbsCachedReal.cxx:89
RooAbsCachedReal::FuncCacheElem
Definition: RooAbsCachedReal.h:59
RooAbsArg::addServerList
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.
Definition: RooAbsArg.cxx:392
RooAbsCachedReal::_anaIntMap
std::map< Int_t, std::pair< const RooArgSet *, const RooArgSet * > > _anaIntMap
Definition: RooAbsCachedReal.h:110
RooAbsCachedReal::FuncCacheElem::paramTracker
RooChangeTracker * paramTracker()
Definition: RooAbsCachedReal.h:70
RooAbsCachedReal::FuncCacheElem::_func
RooHistFunc * _func
Definition: RooAbsCachedReal.h:80
RooAbsCachedReal
Definition: RooAbsCachedReal.h:24
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:171
RooArgSet
Definition: RooArgSet.h:28
int
RooAbsCachedReal::FuncCacheElem::printCompactTreeHook
virtual void printCompactTreeHook(std::ostream &, const char *, Int_t, Int_t)
Print contents of cache when printing self as part of object tree.
Definition: RooAbsCachedReal.cxx:323