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 = 0; ///<! Actual use
109 Int_t _lastIndex = -1; ///<! Last slot accessed
110
111 std::vector<RooNormSetCache> _nsetCache ; ///<! Normalization/Integration set manager
112 std::vector<T*> _object ; ///<! Payload
113 bool _wired = false; ///<! 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>
124RooCacheManager<T>::RooCacheManager(Int_t maxSize) : _maxSize(maxSize)
125{
126
127 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
128 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
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 : RooAbsCache(owner), _maxSize(maxSize)
139{
140
141 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
142 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
143
144 Int_t i ;
145 for (i=0 ; i<_maxSize ; i++) {
146 _object[i]=nullptr ;
147 }
148
149}
150
151/// Copy constructor.
152template <class T>
154 : RooAbsCache(other, owner), _maxSize(other._maxSize), _size(other._size)
155{
156
157 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
158 _object.resize(_maxSize,nullptr) ; // = new T*[_maxSize] ;
159
160 //std::cout << "RooCacheManager:cctor(" << this << ") other = " << &other << " _size=" << _size << " _maxSize = " << _maxSize << std::endl ;
161
162 Int_t i ;
163 for (i=0 ; i<other._size ; i++) {
164 _nsetCache[i] = other._nsetCache[i];
165 _object[i] = nullptr ;
166 }
167
168 for (i=other._size ; i<_maxSize ; i++) {
169 _object[i] = nullptr ;
170 }
171}
172
173 /// Destructor
174template<class T>
176{
177 for (int i=0 ; i<_size ; i++) {
178 delete _object[i] ;
179 }
180}
181
182
183 /// Clear the cache
184template<class T>
186{
187 for (int i=0 ; i<_maxSize ; i++) {
188 delete _object[i] ;
189 _object[i]=nullptr ;
190 _nsetCache[i].clear() ;
191 }
192 _lastIndex = -1 ;
193 _size = 0 ;
194}
195
196
197/// Clear the cache payload but retain slot mapping w.r.t to
198/// normalization and integration sets.
199template<class T>
201{
202 Int_t i ;
203 for (i=0 ; i<_maxSize ; i++) {
204 delete _object[i] ;
205 _object[i]=nullptr ;
206 }
207}
208
209
210/// Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
211template<class T>
212Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName)
213{
214 // Check if object is already registered
215 Int_t sterileIdx(-1) ;
216 if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
217 delete obj; // important! do not forget to cleanup memory
218 return lastIndex() ;
219 }
220
221
222 if (sterileIdx>=0) {
223 // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
224
225 if (sterileIdx>=_maxSize) {
226 //cout << "RooCacheManager<T>::setObj()/SI increasing object cache size from " << _maxSize << " to " << sterileIdx+4 << endl ;
227 _maxSize = sterileIdx+4;
228 _object.resize(_maxSize,nullptr) ;
229 _nsetCache.resize(_maxSize) ;
230 }
231
232
233 _object[sterileIdx] = obj ;
234
235 // Allow optional post-processing of object inserted in cache
236 insertObjectHook(*obj) ;
237
238 return lastIndex() ;
239 }
240
241 if (_size>=_maxSize-1) {
242 //cout << "RooCacheManager<T>::setObj() increasing object cache size from " << _maxSize << " to " << _maxSize*2 << endl ;
243 _maxSize *=2 ;
244 _object.resize(_maxSize,nullptr) ;
245 _nsetCache.resize(_maxSize) ;
246 }
247
248 //cout << "RooCacheManager::setObj<T>(" << this << ") _size = " << _size << " _maxSize = " << _maxSize << endl ;
249 _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,true) ;
250 if (_object[_size]) {
251 delete _object[_size] ;
252 }
253
254 _object[_size] = obj ;
255 _size++ ;
256
257 // Allow optional post-processing of object inserted in cache
258 insertObjectHook(*obj) ;
259
260 // Unwire cache in case it was wired
261 _wired = false ;
262
263 return _size-1 ;
264}
265
266
267/// Retrieve payload object indexed on nset,uset amd isetRangeName
268/// If sterileIdx is not null, it is set to the index of the sterile
269/// slot in cacse such a slot is recycled.
270template<class T>
271T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName)
272{
273 // Fast-track for wired mode
274 if (_wired) {
275 if(_object[0]==nullptr && sterileIdx) *sterileIdx=0 ;
276 return _object[0] ;
277 }
278
279 Int_t i ;
280 for (i=0 ; i<_size ; i++) {
281 if (_nsetCache[i].contains(nset,iset,isetRangeName)==true) {
282 _lastIndex = i ;
283 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
284 return _object[i] ;
285 }
286 }
287
288 for (i=0 ; i<_size ; i++) {
289 if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,false)==false) {
290 _lastIndex = i ;
291 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
292 return _object[i] ;
293 }
294 }
295
296 return nullptr ;
297}
298
299
300/// Retrieve payload object by slot index.
301template<class T>
303{
304 if (index<0||index>=_size) {
305 oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
306 << index << ") out of range [0," << _size-1 << "]" << std::endl ;
307 return nullptr ;
308 }
309 return _object[index] ;
310}
311
312
313/// Create RooArgSet containing the objects that are both in the cached set 1
314/// with a given index and an input argSet.
315template <class T>
317{
319 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet1(), ":")) {
320 if (RooAbsArg *arg = argSet.find(name.c_str())) {
321 output.add(*arg);
322 }
323 }
324 return output;
325}
326
327/// Create RooArgSet containing the objects that are both in the cached set 2
328/// with a given index and an input argSet.
329template <class T>
331{
333 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet2(), ":")) {
334 if (RooAbsArg *arg = argSet.find(name.c_str())) {
335 output.add(*arg);
336 }
337 }
338 return output;
339}
340
341#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:24
Manages the storage of any type of data indexed on the choice of normalization and optionally the set...
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()