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 "RooMsgService.h"
20#include "RooNormSetCache.h"
21#include "RooAbsReal.h"
22#include "RooArgSet.h"
23#include "RooArgList.h"
24#include "RooAbsCache.h"
25#include "RooAbsCacheElement.h"
26#include "RooNameReg.h"
27
28#include <ROOT/StringUtils.hxx>
29#include "Rtypes.h"
30
31#include <vector>
32
33
34template<class T>
36
37public:
38
40 RooCacheManager(RooAbsArg* owner, Int_t maxSize=2) ;
41 RooCacheManager(const RooCacheManager& other, RooAbsArg* owner=nullptr) ;
42 ~RooCacheManager() override ;
43
44 /// Getter function without integration set
45 T* getObj(const RooArgSet* nset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) {
46 return getObj(nset,nullptr,sterileIndex,isetRangeName) ;
47 }
48
49 /// Setter function without integration set
50 Int_t setObj(const RooArgSet* nset, T* obj, const TNamed* isetRangeName=nullptr) {
51 return setObj(nset,nullptr,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=nullptr, const TNamed* isetRangeName=nullptr) ;
60 Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=nullptr) ;
61
62 void reset() ;
63 virtual void sterilize() ;
64
65 /// Return index of slot used in last get or set operation
66 Int_t lastIndex() const {
67 return _lastIndex ;
68 }
69 /// Return size of cache
70 Int_t cacheSize() const {
71 return _size ;
72 }
73
74 /// Interface function to intercept server redirects
75 bool redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/,
76 bool /*nameChange*/, bool /*isRecursive*/) override {
77 return false ;
78 }
79 /// Interface function to intercept cache operation mode changes
80 void operModeHook() override {
81 }
82 /// Interface function to cache add contents to output in tree printing mode
83 void printCompactTreeHook(std::ostream&, const char *) override {
84 }
85
87 RooArgSet selectFromSet1(RooArgSet const& argSet, int index) const ;
88 RooArgSet selectFromSet2(RooArgSet const& argSet, int index) const ;
89
90 /// Interface function to perform post-insert operations on cached object
91 virtual void insertObjectHook(T&) {
92 }
93
94 void wireCache() override {
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=true ;
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
105protected:
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 _wired ; ///<! In wired mode, there is a single payload which is returned always
114
115 ClassDefOverride(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.
123template<class T>
125{
126 _maxSize = maxSize ;
127 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
128 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
129 _wired = false ;
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.
137template<class T>
139{
140 _maxSize = maxSize ;
141 _size = 0 ;
142
143 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
144 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
145 _wired = false ;
146 _lastIndex = -1 ;
147
148 Int_t i ;
149 for (i=0 ; i<_maxSize ; i++) {
150 _object[i]=nullptr ;
151 }
152
153}
154
155/// Copy constructor.
156template<class T>
158{
159 _maxSize = other._maxSize ;
160 _size = other._size ;
161
162 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
163 _object.resize(_maxSize,nullptr) ; // = new T*[_maxSize] ;
164 _wired = false ;
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] = other._nsetCache[i];
172 _object[i] = nullptr ;
173 }
174
175 for (i=other._size ; i<_maxSize ; i++) {
176 _object[i] = nullptr ;
177 }
178}
179
180 /// Destructor
181template<class T>
183{
184 for (int i=0 ; i<_size ; i++) {
185 delete _object[i] ;
186 }
187}
188
189
190 /// Clear the cache
191template<class T>
193{
194 for (int i=0 ; i<_maxSize ; i++) {
195 delete _object[i] ;
196 _object[i]=nullptr ;
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.
206template<class T>
208{
209 Int_t i ;
210 for (i=0 ; i<_maxSize ; i++) {
211 delete _object[i] ;
212 _object[i]=nullptr ;
213 }
214}
215
216
217/// Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
218template<class T>
219Int_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,nullptr) ;
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,nullptr) ;
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,true) ;
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 = false ;
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.
277template<class T>
278T* 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]==nullptr && 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)==true) {
289 _lastIndex = i ;
290 if(_object[i]==nullptr && 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,false)==false) {
297 _lastIndex = i ;
298 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
299 return _object[i] ;
300 }
301 }
302
303 return nullptr ;
304}
305
306
307/// Retrieve payload object by slot index.
308template<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 nullptr ;
315 }
316 return _object[index] ;
317}
318
319
320/// Create RooArgSet containing the objects that are both in the cached set 1
321/// with a given index and an input argSet.
322template <class T>
324{
326 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet1(), ":")) {
327 if (RooAbsArg *arg = argSet.find(name.c_str())) {
328 output.add(*arg);
329 }
330 }
331 return output;
332}
333
334/// Create RooArgSet containing the objects that are both in the cached set 2
335/// with a given index and an input argSet.
336template <class T>
338{
340 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet2(), ":")) {
341 if (RooAbsArg *arg = argSet.find(name.c_str())) {
342 output.add(*arg);
343 }
344 }
345 return output;
346}
347
348#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
char name[80]
Definition TGX11.cxx:110
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
Abstract base class for data members of RooAbsArgs that cache other (composite) RooAbsArg expressions...
Definition RooAbsCache.h:27
RooAbsArg * _owner
Pointer to owning RooAbsArg.
Definition RooAbsCache.h:61
Abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsArg * find(const char *name) const
Find object with given name in list.
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 containing the objects that are both in the cached set 1 with a given index and an i...
T * getObjByIndex(Int_t index) const
Retrieve payload object by slot index.
RooArgSet selectFromSet2(RooArgSet const &argSet, int index) const
Create RooArgSet containing the objects that are both in the cached set 2 with a given index and an i...
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
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
static void output()