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