ROOT  6.06/09
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 //
14 // BEGIN_HTML
15 // RooAbsCachedReal is the abstract base class for functions that need or
16 // want to cache their evaluate() output in a RooHistFunc defined in
17 // terms of the used observables. This base class manages the creation
18 // and storage of all RooHistFunc cache p.d.fs and the RooDataHists
19 // that define their shape. Implementations of RooAbsCachedReal must
20 // define member function fillCacheObject() which serves to fill an
21 // already created RooDataHist with the functions function values. In
22 // addition the member functions actualObservables() and
23 // actualParameters() must be define which report what the actual
24 // observables to be cached are for a given set of observables passed
25 // by the user to getVal() and on which parameters need to be tracked
26 // for changes to trigger a refilling of the cache histogram.
27 // END_HTML
28 //
29 
30 #include "Riostream.h"
31 using namespace std ;
32 
33 #include "RooFit.h"
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 
50 RooAbsCachedReal::RooAbsCachedReal(const char *name, const char *title, Int_t ipOrder) :
51  RooAbsReal(name,title),
52  _cacheMgr(this,10),
53  _ipOrder(ipOrder),
54  _disableCache(kFALSE)
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 
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(kTRUE)) {
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  RooFIter iarg( cache->hist()->get()->fwdIterator() );
154  RooAbsArg *arg(0);
155  while ( (arg=iarg.next()) ) {
156  arg->setOperMode(ADirty);
157  }
158 
159  // Check if we have contents registered already in global expensive object cache
161 
162  if (htmp) {
163 
164  cache->hist()->reset() ;
165  cache->hist()->add(*htmp) ;
166 
167  } else {
168 
169  fillCacheObject(*cache) ;
170 
171  RooDataHist* eoclone = new RooDataHist(*cache->hist()) ;
172  eoclone->removeSelfFromDir() ;
173  expensiveObjectCache().registerObject(GetName(),cache->hist()->GetName(),*eoclone,cache->paramTracker()->parameters()) ;
174  }
175 
176  // Store this cache configuration
177  Int_t code = _cacheMgr.setObj(nset,0,((RooAbsCacheElement*)cache),0) ;
178  ccoutD(Caching) << "RooAbsCachedReal("<<this<<")::getCache(" << GetName() << ") creating new cache " << cache->func()->GetName() << " for nset " << (nset?*nset:RooArgSet()) << " with code " << code << endl ;
179 
180  return cache ;
181 }
182 
183 
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Constructor of cache storage unit class
187 ///
188 /// Create RooDataHist that will cache function values and create
189 /// RooHistFunc that represent s RooDataHist shape as function, create
190 /// meta object that tracks changes in declared parameters of p.d.f
191 /// through actualParameters()
192 
194 {
195  // Disable source caching by default
196  _cacheSource = kFALSE ;
197  _sourceClone = 0 ;
198 
199  RooArgSet* nset2 = self.actualObservables(nset?*nset:RooArgSet()) ;
200 
201  RooArgSet orderedObs ;
202  self.preferredObservableScanOrder(*nset2,orderedObs) ;
203 
204  // Create RooDataHist
205  TString hname = self.inputBaseName() ;
206  hname.Append("_CACHEHIST") ;
207  hname.Append(self.cacheNameSuffix(*nset2)) ;
208 
209  _hist = new RooDataHist(hname,hname,*nset2,self.binningName()) ;
211 
212  RooArgSet* observables= self.actualObservables(*nset2) ;
213 
214  // Create RooHistFunc
215  TString funcname = self.inputBaseName() ;
216  funcname.Append("_CACHE") ;
217  funcname.Append(self.cacheNameSuffix(*nset2)) ;
218  _func = new RooHistFunc(funcname,funcname,*observables,*_hist,self.getInterpolationOrder()) ;
219  if (self.operMode()==ADirty) _func->setOperMode(ADirty) ;
220 
221  // Set initial state of cache to dirty
222  _func->setValueDirty() ;
223 
224  // Create pseudo-object that tracks changes in parameter values
225  RooArgSet* params = self.actualParameters(orderedObs) ;
226  string name= Form("%s_CACHEPARAMS",_func->GetName()) ;
227  _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,kTRUE) ;
228  _paramTracker->hasChanged(kTRUE) ; // clear dirty flag as cache is up-to-date upon creation
229 
230  // Introduce formal dependency of RooHistFunc on parameters so that const optimization code
231  // makes the correct decisions
232  _func->addServerList(*params) ;
233 
234 
235  delete observables ;
236  delete params ;
237  delete nset2 ;
238 
239 }
240 
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 
245 {
246  if (_sourceClone) { delete _sourceClone ; }
247  delete _paramTracker ;
248  delete _func ;
249  delete _hist ;
250 }
251 
252 
253 
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Construct unique suffix name for cache p.d.f object
257 
259 {
260  TString name ;
261  name.Append("_Obs[") ;
262  if (nset.getSize()>0) {
263  TIterator* iter = nset.createIterator() ;
264  RooAbsArg* arg ;
265  Bool_t first(kTRUE) ;
266  while((arg=(RooAbsArg*)iter->Next())) {
267  if (first) {
268  first=kFALSE ;
269  } else {
270  name.Append(",") ;
271  }
272  name.Append(arg->GetName()) ;
273  }
274  delete iter ;
275  }
276 
277  name.Append("]") ;
278  const char* payloadUS = payloadUniqueSuffix() ;
279  if (payloadUS) {
280  name.Append(payloadUS) ;
281  }
282  return name ;
283 }
284 
285 
286 
287 ////////////////////////////////////////////////////////////////////////////////
288 /// Set interpolation order of RooHistFunct representing cache histogram
289 
291 {
292  _ipOrder = order ;
293 
294  for (Int_t i=0 ; i<_cacheMgr.cacheSize() ; i++) {
296  if (cache) {
297  cache->func()->setInterpolationOrder(order) ;
298  }
299  }
300 }
301 
302 
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 /// Return list of contained RooAbsArg objects
306 
308 {
309  RooArgList ret(*func()) ;
310 
311  ret.add(*_paramTracker) ;
312  if (_sourceClone) {
313  ret.add(*_sourceClone) ;
314  }
315  return ret ;
316 }
317 
318 
319 ////////////////////////////////////////////////////////////////////////////////
320 /// Print contents of cache when printing self as part of object tree
321 
322 void RooAbsCachedReal::FuncCacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
323 {
324  if (curElem==0) {
325  os << indent << "--- RooAbsCachedReal begin cache ---" << endl ;
326  }
327 
328  TString indent2(indent) ;
329  indent2 += Form("[%d] ",curElem) ;
330  func()->printCompactTree(os,indent2) ;
331 
332  if (curElem==maxElem) {
333  os << indent << "--- RooAbsCachedReal end cache --- " << endl ;
334  }
335 }
336 
337 
338 
339 ////////////////////////////////////////////////////////////////////////////////
340 /// Return analytical integration capabilities of the RooHistFunc that corresponds to the set of observables in allVars
341 
342 Int_t RooAbsCachedReal::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
343 {
344  FuncCacheElem* cache = getCache(normSet?normSet:&allVars) ;
345  Int_t code = cache->func()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
346  _anaIntMap[code].first = &allVars ;
347  _anaIntMap[code].second = normSet ;
348  return code ;
349 }
350 
351 
352 
353 ////////////////////////////////////////////////////////////////////////////////
354 /// Forward call to implementation in relevant RooHistFunc instance
355 
356 Double_t RooAbsCachedReal::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
357 {
358  if (code==0) {
359  return getVal(normSet) ;
360  }
361 
362  const RooArgSet* anaVars = _anaIntMap[code].first ;
363  const RooArgSet* normSet2 = _anaIntMap[code].second ;
364 
365  FuncCacheElem* cache = getCache(normSet2?normSet2:anaVars) ;
366  return cache->func()->analyticalIntegralWN(code,normSet,rangeName) ;
367 
368 }
369 
370 
371 
372 
373 
Int_t cacheSize() const
TString cacheNameSuffix(const RooArgSet &nset) const
Construct unique suffix name for cache p.d.f object.
ClassImp(RooAbsCachedReal) RooAbsCachedReal
Constructor.
void setInterpolationOrder(Int_t order)
Set interpolation order of RooHistFunct representing cache histogram.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
void setInterpolationOrder(Int_t order)
Definition: RooHistFunc.h:47
RooFIter fwdIterator() const
virtual Double_t getValV(const RooArgSet *set=0) const
Implementation of getVal() overriding default implementation of RooAbsReal.
Bool_t _disableCache
Map for analytical integration codes.
virtual FuncCacheElem * createCache(const RooArgSet *nset) const
Interface function to create an internal cache object that represent each cached function configurati...
Bool_t hasChanged(Bool_t clearState)
Returns true if state has changes since last call with clearState=kTRUE If clearState is true...
Basic string class.
Definition: TString.h:137
std::map< Int_t, std::pair< const RooArgSet *, const RooArgSet * > > _anaIntMap
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
RooExpensiveObjectCache & expensiveObjectCache() const
Definition: RooAbsArg.cxx:2341
const TObject * retrieveObject(const char *name, TClass *tclass, const RooArgSet &params)
Retrieve object from cache that was registered under given name with given parameters, if current parameter values match those that were stored in the registry for this object.
Iterator abstract base class.
Definition: TIterator.h:32
virtual const RooArgSet * get() const
Definition: RooDataHist.h:77
FuncCacheElem * getCache(const RooArgSet *nset) const
Retrieve cache corresponding to observables in nset.
void Class()
Definition: Class.C:29
RooArgSet parameters() const
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:349
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
TIterator * createIterator(Bool_t dir=kIterForward) const
TString & Append(const char *cs)
Definition: TString.h:492
friend class RooArgSet
Definition: RooAbsArg.h:469
virtual const char * payloadUniqueSuffix() const
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual Double_t getValV(const RooArgSet *set=0) const
Return value of object.
Definition: RooAbsReal.cxx:249
RooChangeTracker * paramTracker()
void addServerList(RooAbsCollection &serverList, Bool_t valueProp=kTRUE, Bool_t shapeProp=kFALSE)
Register a list of RooAbsArg as servers to us by calls addServer() for each arg in the list...
Definition: RooAbsArg.cxx:397
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:320
Int_t getInterpolationOrder() const
virtual void add(const RooArgSet &row, Double_t wgt=1.0)
Definition: RooDataHist.h:65
char * Form(const char *fmt,...)
virtual RooArgList containedArgs(Action)
Return list of contained RooAbsArg objects.
friend class FuncCacheElem
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
void removeSelfFromDir()
Definition: RooDataHist.h:131
static void indent(ostringstream &buf, int indent_level)
FuncCacheElem(const RooAbsCachedReal &self, const RooArgSet *nset)
Constructor of cache storage unit class.
void clearCacheObject(FuncCacheElem &cache) const
Mark all bins as unitialized (value -1)
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Int_t _ipOrder
The cache manager.
RooObjCacheManager _cacheMgr
void setValueDirty() const
Definition: RooAbsArg.h:439
#define name(a, b)
Definition: linkTestLib0.cpp:5
Double_t _value
Definition: RooAbsReal.h:389
virtual void fillCacheObject(FuncCacheElem &cache) const =0
virtual void printCompactTreeHook(std::ostream &, const char *, Int_t, Int_t)
Print contents of cache when printing self as part of object tree.
T * getObjByIndex(Int_t index) const
virtual void reset()
Reset all bin weights to zero.
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 TObject * Next()=0
OperMode operMode() const
Definition: RooAbsArg.h:399
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Change cache operation mode to given mode.
Definition: RooAbsArg.cxx:1753
virtual const char * binningName() const
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=0)
Int_t getSize() const
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
friend class RooHistFunc
Definition: RooAbsArg.h:510
const Bool_t kTRUE
Definition: Rtypes.h:91
#define ccoutD(a)
Definition: RooMsgService.h:38
void setAllWeights(Double_t value)
Set all the event weight of all bins to the specified value.
virtual ~RooAbsCachedReal()
Destructor.
virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName=0) const
Forward call to implementation in relevant RooHistFunc instance.
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...