Logo ROOT  
Reference Guide
RooCacheManager.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 #ifndef ROO_CACHE_MANAGER
17 #define ROO_CACHE_MANAGER
18 
19 #include "Rtypes.h"
20 
21 #include "RooMsgService.h"
22 #include "RooNormSetCache.h"
23 #include "RooAbsReal.h"
24 #include "RooArgSet.h"
25 #include "RooArgList.h"
26 #include "RooAbsCache.h"
27 #include "RooAbsCacheElement.h"
28 #include "RooNameReg.h"
29 #include <vector>
30 
31 class RooNameSet ;
32 
33 
34 template<class T>
35 class RooCacheManager : public RooAbsCache {
36 
37 public:
38 
39  RooCacheManager(Int_t maxSize=2) ;
40  RooCacheManager(RooAbsArg* owner, Int_t maxSize=2) ;
41  RooCacheManager(const RooCacheManager& other, RooAbsArg* owner=0) ;
42  virtual ~RooCacheManager() ;
43 
44  T* getObj(const RooArgSet* nset, Int_t* sterileIndex=0, const TNamed* isetRangeName=0) {
45  // Getter function without integration set
46  return getObj(nset,0,sterileIndex,isetRangeName) ;
47  }
48 
49  Int_t setObj(const RooArgSet* nset, T* obj, const TNamed* isetRangeName=0) {
50  // Setter function without integration set
51  return setObj(nset,0,obj,isetRangeName) ;
52  }
53 
54  inline T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const char* isetRangeName) {
55  if (_wired) return _object[0] ;
56  return getObj(nset,iset,sterileIdx,RooNameReg::ptr(isetRangeName)) ;
57  }
58 
59  T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIndex=0, const TNamed* isetRangeName=0) ;
60  Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=0) ;
61 
62  void reset() ;
63  virtual void sterilize() ;
64 
65  Int_t lastIndex() const {
66  // Return index of slot used in last get or set operation
67  return _lastIndex ;
68  }
69  Int_t cacheSize() const {
70  // Return size of cache
71  return _size ;
72  }
73 
74  virtual Bool_t redirectServersHook(const RooAbsCollection& /*newServerList*/, Bool_t /*mustReplaceAll*/,
75  Bool_t /*nameChange*/, Bool_t /*isRecursive*/) {
76  // Interface function to intercept server redirects
77  return kFALSE ;
78  }
79  virtual void operModeHook() {
80  // Interface function to intercept cache operation mode changes
81  }
82  virtual void printCompactTreeHook(std::ostream&, const char *) {
83  // Interface function to cache add contents to output in tree printing mode
84  }
85 
86  T* getObjByIndex(Int_t index) const ;
87  const RooNameSet* nameSet1ByIndex(Int_t index) const ;
88  const RooNameSet* nameSet2ByIndex(Int_t index) const ;
89 
90  virtual void insertObjectHook(T&) {
91  // Interface function to perform post-insert operations on cached object
92  }
93 
94  void wireCache() {
95  if (_size==0) {
96  oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") no cached elements!" << std::endl ;
97  } else if (_size==1) {
98  oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") now wiring cache" << std::endl ;
99  _wired=kTRUE ;
100  } else if (_size>1) {
101  oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") cache cannot be wired because it contains more than one element" << std::endl ;
102  }
103  }
104 
105 protected:
106 
107  Int_t _maxSize ; //! Maximum size
108  Int_t _size ; //! Actual use
109  Int_t _lastIndex ; //! Last slot accessed
110 
111  std::vector<RooNormSetCache> _nsetCache ; //! Normalization/Integration set manager
112  std::vector<T*> _object ; //! Payload
113  Bool_t _wired ; //! In wired mode, there is a single payload which is returned always
114 
115  ClassDef(RooCacheManager,2) // Cache Manager class generic objects
116 } ;
117 
118 
119 /// Constructor for simple caches without RooAbsArg payload. A cache
120 /// made with this constructor is not registered with its owner
121 /// and will not receive information on server redirects and
122 /// cache operation mode changes.
123 template<class T>
125 {
126  _maxSize = maxSize ;
127  _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
128  _object.resize(_maxSize,0) ; // = new T*[maxSize] ;
129  _wired = kFALSE ;
130 }
131 
132 
133 /// Constructor for simple caches with RooAbsArg derived payload. A cache
134 /// made with this constructor is registered with its owner
135 /// and will receive information on server redirects and
136 /// cache operation mode changes.
137 template<class T>
139 {
140  _maxSize = maxSize ;
141  _size = 0 ;
142 
143  _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
144  _object.resize(_maxSize,0) ; // = new T*[maxSize] ;
145  _wired = kFALSE ;
146  _lastIndex = -1 ;
147 
148  Int_t i ;
149  for (i=0 ; i<_maxSize ; i++) {
150  _object[i]=0 ;
151  }
152 
153 }
154 
155 /// Copy constructor.
156 template<class T>
158 {
159  _maxSize = other._maxSize ;
160  _size = other._size ;
161 
162  _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
163  _object.resize(_maxSize,0) ; // = new T*[_maxSize] ;
164  _wired = kFALSE ;
165  _lastIndex = -1 ;
166 
167  //std::cout << "RooCacheManager:cctor(" << this << ") other = " << &other << " _size=" << _size << " _maxSize = " << _maxSize << std::endl ;
168 
169  Int_t i ;
170  for (i=0 ; i<other._size ; i++) {
171  _nsetCache[i].initialize(other._nsetCache[i]) ;
172  _object[i] = 0 ;
173  }
174 
175  for (i=other._size ; i<_maxSize ; i++) {
176  _object[i] = 0 ;
177  }
178 }
179 
180  /// Destructor
181 template<class T>
183 {
184  for (int i=0 ; i<_size ; i++) {
185  delete _object[i] ;
186  }
187 }
188 
189 
190  /// Clear the cache
191 template<class T>
193 {
194  for (int i=0 ; i<_maxSize ; i++) {
195  delete _object[i] ;
196  _object[i]=0 ;
197  _nsetCache[i].clear() ;
198  }
199  _lastIndex = -1 ;
200  _size = 0 ;
201 }
202 
203 
204 /// Clear the cache payload but retain slot mapping w.r.t to
205 /// normalization and integration sets.
206 template<class T>
208 {
209  Int_t i ;
210  for (i=0 ; i<_maxSize ; i++) {
211  delete _object[i] ;
212  _object[i]=0 ;
213  }
214 }
215 
216 
217 /// Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
218 template<class T>
219 Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName)
220 {
221  // Check if object is already registered
222  Int_t sterileIdx(-1) ;
223  if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
224  delete obj; // important! do not forget to cleanup memory
225  return lastIndex() ;
226  }
227 
228 
229  if (sterileIdx>=0) {
230  // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
231 
232  if (sterileIdx>=_maxSize) {
233  //cout << "RooCacheManager<T>::setObj()/SI increasing object cache size from " << _maxSize << " to " << sterileIdx+4 << endl ;
234  _maxSize = sterileIdx+4;
235  _object.resize(_maxSize,0) ;
236  _nsetCache.resize(_maxSize) ;
237  }
238 
239 
240  _object[sterileIdx] = obj ;
241 
242  // Allow optional post-processing of object inserted in cache
243  insertObjectHook(*obj) ;
244 
245  return lastIndex() ;
246  }
247 
248  if (_size>=_maxSize-1) {
249  //cout << "RooCacheManager<T>::setObj() increasing object cache size from " << _maxSize << " to " << _maxSize*2 << endl ;
250  _maxSize *=2 ;
251  _object.resize(_maxSize,0) ;
252  _nsetCache.resize(_maxSize) ;
253  }
254 
255  //cout << "RooCacheManager::setObj<T>(" << this << ") _size = " << _size << " _maxSize = " << _maxSize << endl ;
256  _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,kTRUE) ;
257  if (_object[_size]) {
258  delete _object[_size] ;
259  }
260 
261  _object[_size] = obj ;
262  _size++ ;
263 
264  // Allow optional post-processing of object inserted in cache
265  insertObjectHook(*obj) ;
266 
267  // Unwire cache in case it was wired
268  _wired = kFALSE ;
269 
270  return _size-1 ;
271 }
272 
273 
274 /// Retrieve payload object indexed on nset,uset amd isetRangeName
275 /// If sterileIdx is not null, it is set to the index of the sterile
276 /// slot in cacse such a slot is recycled.
277 template<class T>
278 T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName)
279 {
280  // Fast-track for wired mode
281  if (_wired) {
282  if(_object[0]==0 && sterileIdx) *sterileIdx=0 ;
283  return _object[0] ;
284  }
285 
286  Int_t i ;
287  for (i=0 ; i<_size ; i++) {
288  if (_nsetCache[i].contains(nset,iset,isetRangeName)==kTRUE) {
289  _lastIndex = i ;
290  if(_object[i]==0 && sterileIdx) *sterileIdx=i ;
291  return _object[i] ;
292  }
293  }
294 
295  for (i=0 ; i<_size ; i++) {
296  if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,kFALSE)==kFALSE) {
297  _lastIndex = i ;
298  if(_object[i]==0 && sterileIdx) *sterileIdx=i ;
299  return _object[i] ;
300  }
301  }
302 
303  return 0 ;
304 }
305 
306 
307 /// Retrieve payload object by slot index.
308 template<class T>
310 {
311  if (index<0||index>=_size) {
312  oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
313  << index << ") out of range [0," << _size-1 << "]" << std::endl ;
314  return 0 ;
315  }
316  return _object[index] ;
317 }
318 
319 
320 /// Retrieve RooNameSet associated with slot at given index.
321 template<class T>
323 {
324  if (index<0||index>=_size) {
325  oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
326  << index << ") out of range [0," << _size-1 << "]" << std::endl ;
327  return 0 ;
328  }
329  return &_nsetCache[index].nameSet1() ;
330 }
331 
332 
333 /// Retrieve RooNameSet associated with slot at given index.
334 template<class T>
336 {
337  if (index<0||index>=_size) {
338  oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
339  << index << ") out of range [0," << _size-1 << "]" << std::endl ;
340  return 0 ;
341  }
342  return &_nsetCache[index].nameSet2() ;
343 }
344 
345 
346 #endif
RooCacheManager::reset
void reset()
Clear the cache.
Definition: RooCacheManager.h:192
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
RooCacheManager::~RooCacheManager
virtual ~RooCacheManager()
Destructor.
Definition: RooCacheManager.h:182
RooCacheManager::sterilize
virtual void sterilize()
Clear the cache payload but retain slot mapping w.r.t to normalization and integration sets.
Definition: RooCacheManager.h:207
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:95
RooAbsReal.h
RooMsgService.h
RooNameReg::ptr
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:93
RooCacheManager::_nsetCache
std::vector< RooNormSetCache > _nsetCache
Last slot accessed.
Definition: RooCacheManager.h:111
oocoutI
#define oocoutI(o, a)
Definition: RooMsgService.h:45
RooCacheManager::getObj
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
Definition: RooCacheManager.h:44
RooAbsCacheElement.h
RooArgSet.h
RooFit::Optimization
@ Optimization
Definition: RooGlobalFunc.h:68
RooCacheManager::insertObjectHook
virtual void insertObjectHook(T &)
Definition: RooCacheManager.h:90
RooCacheManager::nameSet2ByIndex
const RooNameSet * nameSet2ByIndex(Int_t index) const
Retrieve RooNameSet associated with slot at given index.
Definition: RooCacheManager.h:335
RooCacheManager::_wired
Bool_t _wired
Payload.
Definition: RooCacheManager.h:113
oocoutE
#define oocoutE(o, a)
Definition: RooMsgService.h:48
RooCacheManager::_lastIndex
Int_t _lastIndex
Actual use.
Definition: RooCacheManager.h:109
bool
RooCacheManager::operModeHook
virtual void operModeHook()
Interface for operation mode changes.
Definition: RooCacheManager.h:79
RooNormSetCache.h
RooCacheManager::RooCacheManager
RooCacheManager(const RooCacheManager &other, RooAbsArg *owner=0)
Copy constructor.
Definition: RooCacheManager.h:157
RooCacheManager::getObjByIndex
T * getObjByIndex(Int_t index) const
Retrieve payload object by slot index.
Definition: RooCacheManager.h:309
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:96
RooCacheManager::_maxSize
Int_t _maxSize
Definition: RooCacheManager.h:107
RooAbsCollection
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
Definition: RooAbsCollection.h:31
RooCacheManager::nameSet1ByIndex
const RooNameSet * nameSet1ByIndex(Int_t index) const
Retrieve RooNameSet associated with slot at given index.
Definition: RooCacheManager.h:322
RooFit::ObjectHandling
@ ObjectHandling
Definition: RooGlobalFunc.h:68
RooCacheManager::_object
std::vector< T * > _object
Normalization/Integration set manager.
Definition: RooCacheManager.h:112
RooCacheManager::_size
Int_t _size
Maximum size.
Definition: RooCacheManager.h:108
RooNameSet
RooNameSet is a utility class that stores the names the objects in a RooArget.
Definition: RooNameSet.h:24
RooCacheManager::setObj
Int_t setObj(const RooArgSet *nset, const RooArgSet *iset, T *obj, const TNamed *isetRangeName=0)
Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
Definition: RooCacheManager.h:219
RooCacheManager::RooCacheManager
RooCacheManager(RooAbsArg *owner, Int_t maxSize=2)
Constructor for simple caches with RooAbsArg derived payload.
Definition: RooCacheManager.h:138
RooAbsCache
RooAbsCache is the abstract base class for data members of RooAbsArgs that cache other (composite) Ro...
Definition: RooAbsCache.h:27
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
RooCacheManager::printCompactTreeHook
virtual void printCompactTreeHook(std::ostream &, const char *)
Interface for printing of cache guts in tree mode printing.
Definition: RooCacheManager.h:82
RooCacheManager::wireCache
void wireCache()
Definition: RooCacheManager.h:94
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
RooCacheManager::lastIndex
Int_t lastIndex() const
Definition: RooCacheManager.h:65
RooCacheManager::getObj
T * getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIdx, const char *isetRangeName)
Definition: RooCacheManager.h:54
RooCacheManager
Template class RooCacheManager manages the storage of any type of data indexed on the choice of norma...
Definition: RooCacheManager.h:35
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooAbsCache.h
RooCacheManager::RooCacheManager
RooCacheManager(Int_t maxSize=2)
Constructor for simple caches without RooAbsArg payload.
Definition: RooCacheManager.h:124
RooArgList.h
RooCacheManager::getObj
T * getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
Retrieve payload object indexed on nset,uset amd isetRangeName If sterileIdx is not null,...
Definition: RooCacheManager.h:278
Rtypes.h
RooNameReg.h
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
RooCacheManager::redirectServersHook
virtual Bool_t redirectServersHook(const RooAbsCollection &, Bool_t, Bool_t, Bool_t)
Interface for server redirect calls.
Definition: RooCacheManager.h:74
RooAbsCache::_owner
RooAbsArg * _owner
Definition: RooAbsCache.h:44
int