Logo ROOT   6.08/07
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 template<class T>
121 {
122  // Constructor for simple caches without RooAbsArg payload. A cache
123  // made with this constructor is not registered with its owner
124  // and will not receive information on server redirects and
125  // cache operation mode changes
126 
127  _maxSize = maxSize ;
128  _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
129  _object.resize(_maxSize,0) ; // = new T*[maxSize] ;
130  _wired = kFALSE ;
131 }
132 
133 template<class T>
135 {
136  // Constructor for simple caches with RooAbsArg derived payload. A cache
137  // made with this constructor is registered with its owner
138  // and will receive information on server redirects and
139  // cache operation mode changes
140 
141  _maxSize = maxSize ;
142  _size = 0 ;
143 
144  _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
145  _object.resize(_maxSize,0) ; // = new T*[maxSize] ;
146  _wired = kFALSE ;
147  _lastIndex = -1 ;
148 
149  Int_t i ;
150  for (i=0 ; i<_maxSize ; i++) {
151  _object[i]=0 ;
152  }
153 
154 }
155 
156 
157 template<class T>
159 {
160  // Copy constructor
161 
162  _maxSize = other._maxSize ;
163  _size = other._size ;
164 
165  _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
166  _object.resize(_maxSize,0) ; // = new T*[_maxSize] ;
167  _wired = kFALSE ;
168  _lastIndex = -1 ;
169 
170  //std::cout << "RooCacheManager:cctor(" << this << ") other = " << &other << " _size=" << _size << " _maxSize = " << _maxSize << std::endl ;
171 
172  Int_t i ;
173  for (i=0 ; i<other._size ; i++) {
174  _nsetCache[i].initialize(other._nsetCache[i]) ;
175  _object[i] = 0 ;
176  }
177 
178  for (i=other._size ; i<_maxSize ; i++) {
179  _object[i] = 0 ;
180  }
181 }
182 
183 
184 template<class T>
186 {
187  // Destructor
188 
189  //delete[] _nsetCache ;
190  Int_t i ;
191  for (i=0 ; i<_size ; i++) {
192  delete _object[i] ;
193  }
194  //delete[] _object ;
195 }
196 
197 
198 template<class T>
200 {
201  // Clear the cache
202 
203  Int_t i ;
204  for (i=0 ; i<_maxSize ; i++) {
205  delete _object[i] ;
206  _object[i]=0 ;
207  _nsetCache[i].clear() ;
208  }
209  _lastIndex = -1 ;
210  _size = 0 ;
211 }
212 
213 
214 
215 template<class T>
217 {
218  // Clear the cache payload but retain slot mapping w.r.t to
219  // normalization and integration sets.
220 
221  Int_t i ;
222  for (i=0 ; i<_maxSize ; i++) {
223  delete _object[i] ;
224  _object[i]=0 ;
225  }
226 }
227 
228 
229 
230 template<class T>
231 Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName)
232 {
233  // Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName
234 
235  // Check if object is already registered
236  Int_t sterileIdx(-1) ;
237  if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
238  return lastIndex() ;
239  }
240 
241 
242  if (sterileIdx>=0) {
243  // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
244 
245  if (sterileIdx>=_maxSize) {
246  //cout << "RooCacheManager<T>::setObj()/SI increasing object cache size from " << _maxSize << " to " << sterileIdx+4 << endl ;
247  _maxSize = sterileIdx+4;
248  _object.resize(_maxSize,0) ;
249  _nsetCache.resize(_maxSize) ;
250  }
251 
252 
253  _object[sterileIdx] = obj ;
254 
255  // Allow optional post-processing of object inserted in cache
256  insertObjectHook(*obj) ;
257 
258  return lastIndex() ;
259  }
260 
261  if (_size>=_maxSize-1) {
262  //cout << "RooCacheManager<T>::setObj() increasing object cache size from " << _maxSize << " to " << _maxSize*2 << endl ;
263  _maxSize *=2 ;
264  _object.resize(_maxSize,0) ;
265  _nsetCache.resize(_maxSize) ;
266  }
267 
268  //cout << "RooCacheManager::setObj<T>(" << this << ") _size = " << _size << " _maxSize = " << _maxSize << endl ;
269  _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,kTRUE) ;
270  if (_object[_size]) {
271  delete _object[_size] ;
272  }
273 
274  _object[_size] = obj ;
275  _size++ ;
276 
277  // Allow optional post-processing of object inserted in cache
278  insertObjectHook(*obj) ;
279 
280  // Unwire cache in case it was wired
281  _wired = kFALSE ;
282 
283  return _size-1 ;
284 }
285 
286 
287 
288 template<class T>
289 T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName)
290 {
291  // Retrieve payload object indexed on nset,uset amd isetRangeName
292  // If sterileIdx is not null, it is set to the index of the sterile
293  // slot in cacse such a slot is recycled
294 
295  // Fast-track for wired mode
296  if (_wired) {
297  if(_object[0]==0 && sterileIdx) *sterileIdx=0 ;
298  return _object[0] ;
299  }
300 
301  Int_t i ;
302  for (i=0 ; i<_size ; i++) {
303  if (_nsetCache[i].contains(nset,iset,isetRangeName)==kTRUE) {
304  _lastIndex = i ;
305  if(_object[i]==0 && sterileIdx) *sterileIdx=i ;
306  return _object[i] ;
307  }
308  }
309 
310  for (i=0 ; i<_size ; i++) {
311  if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,kFALSE)==kFALSE) {
312  _lastIndex = i ;
313  if(_object[i]==0 && sterileIdx) *sterileIdx=i ;
314  return _object[i] ;
315  }
316  }
317 
318  return 0 ;
319 }
320 
321 
322 
323 template<class T>
325 {
326  // Retrieve payload object by slot index
327 
328  if (index<0||index>=_size) {
329  oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
330  << index << ") out of range [0," << _size-1 << "]" << std::endl ;
331  return 0 ;
332  }
333  return _object[index] ;
334 }
335 
336 template<class T>
338 {
339  // Retrieve RooNameSet associated with slot at given index
340 
341  if (index<0||index>=_size) {
342  oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
343  << index << ") out of range [0," << _size-1 << "]" << std::endl ;
344  return 0 ;
345  }
346  return &_nsetCache[index].nameSet1() ;
347 }
348 
349 template<class T>
351 {
352  // Retrieve RooNameSet associated with slot at given index
353 
354  if (index<0||index>=_size) {
355  oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
356  << index << ") out of range [0," << _size-1 << "]" << std::endl ;
357  return 0 ;
358  }
359  return &_nsetCache[index].nameSet2() ;
360 }
361 
362 
363 #endif
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooCacheManager(Int_t maxSize=2)
virtual void insertObjectHook(T &)
double T(double x)
Definition: ChebyshevPol.h:34
RooAbsArg * _owner
Definition: RooAbsCache.h:45
#define oocoutI(o, a)
Definition: RooMsgService.h:45
virtual void sterilize()
T * getObjByIndex(Int_t index) const
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
RooAbsCache is the abstract base class for data members of RooAbsArgs that cache other (composite) Ro...
Definition: RooAbsCache.h:28
const RooNameSet * nameSet1ByIndex(Int_t index) const
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual void printCompactTreeHook(std::ostream &, const char *)
Interface for printing of cache guts in tree mode printing.
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
#define oocoutE(o, a)
Definition: RooMsgService.h:48
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
Int_t _size
Maximum size.
Int_t cacheSize() const
RooNameSet is a utility class that stores the names the objects in a RooArget.
Definition: RooNameSet.h:24
Bool_t _wired
Payload.
T * getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIdx, const char *isetRangeName)
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:125
Int_t lastIndex() const
const RooNameSet * nameSet2ByIndex(Int_t index) const
virtual ~RooCacheManager()
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects...
virtual void operModeHook()
Interface for operation mode changes.
virtual Bool_t redirectServersHook(const RooAbsCollection &, Bool_t, Bool_t, Bool_t)
Interface for server redirect calls.
Int_t _lastIndex
Actual use.
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=0)
Template class RooCacheManager manages the storage of any type of data indexed on the choice of norma...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
std::vector< T * > _object
Normalization/Integration set manager.
std::vector< RooNormSetCache > _nsetCache
Last slot accessed.