Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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=nullptr) ;
41 ~RooCacheManager() override ;
42
43 /// Getter function without integration set
44 T* getObj(const RooArgSet* nset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) {
45 return getObj(nset,nullptr,sterileIndex,isetRangeName) ;
46 }
47
48 /// Setter function without integration set
49 Int_t setObj(const RooArgSet* nset, T* obj, const TNamed* isetRangeName=nullptr) {
50 return setObj(nset,nullptr,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=nullptr, const TNamed* isetRangeName=nullptr) ;
59 Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=nullptr) ;
60
61 void reset() ;
62 virtual void sterilize() ;
63
64 /// Return index of slot used in last get or set operation
65 Int_t lastIndex() const {
66 return _lastIndex ;
67 }
68 /// Return size of cache
69 Int_t cacheSize() const {
70 return _size ;
71 }
72
73 /// Interface function to intercept server redirects
74 bool redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/,
75 bool /*nameChange*/, bool /*isRecursive*/) override {
76 return false ;
77 }
78 /// Interface function to intercept cache operation mode changes
79 void operModeHook() override {
80 }
81 /// Interface function to cache add contents to output in tree printing mode
82 void printCompactTreeHook(std::ostream&, const char *) override {
83 }
84
86 RooArgSet selectFromSet1(RooArgSet const& argSet, int index) const ;
87 RooArgSet selectFromSet2(RooArgSet const& argSet, int index) const ;
88
89 /// Interface function to perform post-insert operations on cached object
90 virtual void insertObjectHook(T&) {
91 }
92
93 void wireCache() override {
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 ;
98 _wired=true ;
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 _wired ; ///<! In wired mode, there is a single payload which is returned always
113
114 ClassDefOverride(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,nullptr) ; // = new T*[maxSize] ;
128 _wired = false ;
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,nullptr) ; // = new T*[maxSize] ;
144 _wired = false ;
145 _lastIndex = -1 ;
146
147 Int_t i ;
148 for (i=0 ; i<_maxSize ; i++) {
149 _object[i]=nullptr ;
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,nullptr) ; // = new T*[_maxSize] ;
163 _wired = false ;
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] = other._nsetCache[i];
171 _object[i] = nullptr ;
172 }
173
174 for (i=other._size ; i<_maxSize ; i++) {
175 _object[i] = nullptr ;
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]=nullptr ;
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]=nullptr ;
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,nullptr) ;
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,nullptr) ;
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,true) ;
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 = false ;
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]==nullptr && 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)==true) {
288 _lastIndex = i ;
289 if(_object[i]==nullptr && 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,false)==false) {
296 _lastIndex = i ;
297 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
298 return _object[i] ;
299 }
300 }
301
302 return nullptr ;
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 nullptr ;
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)
#define oocoutI(o, a)
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
RooAbsCache is the abstract base class for data members of RooAbsArgs that cache other (composite) Ro...
Definition RooAbsCache.h:27
RooAbsArg * _owner
Pointer to owning RooAbsArg.
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:55
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=nullptr)
Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=nullptr)
Setter function without integration set.
RooCacheManager(Int_t maxSize=2)
Constructor for simple caches without RooAbsArg payload.
RooCacheManager(const RooCacheManager &other, RooAbsArg *owner=nullptr)
Copy constructor.
Int_t cacheSize() const
Return size of cache.
void printCompactTreeHook(std::ostream &, const char *) override
Interface function to cache add contents to output in tree printing mode.
virtual void insertObjectHook(T &)
Interface function to perform post-insert operations on cached object.
virtual void sterilize()
Clear the cache payload but retain slot mapping w.r.t to normalization and integration sets.
std::vector< T * > _object
! Payload
std::vector< RooNormSetCache > _nsetCache
! Normalization/Integration set manager
~RooCacheManager() override
Destructor.
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.
bool redirectServersHook(const RooAbsCollection &, bool, bool, bool) override
Interface function to intercept server redirects.
void reset()
Clear the cache.
Int_t lastIndex() const
Return index of slot used in last get or set operation.
void operModeHook() override
Interface function to intercept cache operation mode changes.
T * getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIndex=nullptr, const TNamed *isetRangeName=nullptr)
Retrieve payload object indexed on nset,uset amd isetRangeName If sterileIdx is not null,...
Int_t _maxSize
! Maximum size
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=nullptr, const TNamed *isetRangeName=nullptr)
Getter function without integration set.
void wireCache() override
T * getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIdx, const char *isetRangeName)
RooCacheManager(RooAbsArg *owner, Int_t maxSize=2)
Constructor for simple caches with RooAbsArg derived payload.
Int_t _lastIndex
! Last slot accessed
Int_t _size
! Actual use
bool _wired
! In wired mode, there is a single payload which is returned always
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
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.