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/**
34\class RooCacheManager
35\ingroup Roofitcore
36
37Manages the storage of any type of data indexed on
38the choice of normalization and optionally the set of integrated observables.
39The purpose of this class is to facilitate storage of intermediate results
40in operator p.d.f.s whose value and inner working are often highly dependent
41on the user provided choice of normalization in getVal().
42
43For efficiency reasons these normalization set pointer are
44dereferenced as little as possible. This class contains a lookup
45table for RooArgSet pointer pairs -> normalization lists. Distinct
46pointer pairs that represent the same normalization/projection are
47recognized and will all point to the same normalization list. Lists
48for up to 'maxSize' different normalization/ projection
49configurations can be cached.
50**/
51
52template<class T>
54
55public:
56
58 RooCacheManager(RooAbsArg* owner, Int_t maxSize=2) ;
59 RooCacheManager(const RooCacheManager& other, RooAbsArg* owner=nullptr) ;
60 ~RooCacheManager() override ;
61
62 /// Getter function without integration set
63 T* getObj(const RooArgSet* nset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) {
64 return getObj(nset,nullptr,sterileIndex,isetRangeName) ;
65 }
66
67 /// Setter function without integration set
68 Int_t setObj(const RooArgSet* nset, T* obj, const TNamed* isetRangeName=nullptr) {
69 return setObj(nset,nullptr,obj,isetRangeName) ;
70 }
71
72 inline T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const char* isetRangeName) {
73 if (_wired) return _object[0] ;
74 return getObj(nset,iset,sterileIdx,RooNameReg::ptr(isetRangeName)) ;
75 }
76
77 T *getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIdx = nullptr,
78 const TNamed *isetRangeName = nullptr);
79 Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=nullptr) ;
80
81 void reset() ;
82 virtual void sterilize() ;
83
84 /// Return index of slot used in last get or set operation
85 Int_t lastIndex() const {
86 return _lastIndex ;
87 }
88 /// Return size of cache
89 Int_t cacheSize() const {
90 return _size ;
91 }
92
93 /// Interface function to intercept server redirects
94 bool redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/,
95 bool /*nameChange*/, bool /*isRecursive*/) override {
96 return false ;
97 }
98 /// Interface function to intercept cache operation mode changes
99 void operModeHook() override {
100 }
101 /// Interface function to cache add contents to output in tree printing mode
102 void printCompactTreeHook(std::ostream&, const char *) override {
103 }
104
106 RooArgSet selectFromSet1(RooArgSet const& argSet, int index) const ;
107 RooArgSet selectFromSet2(RooArgSet const& argSet, int index) const ;
108
109 /// Interface function to perform post-insert operations on cached object
110 virtual void insertObjectHook(T&) {
111 }
112
113 void wireCache() override {
114 if (_size==0) {
115 oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") no cached elements!" << std::endl ;
116 } else if (_size==1) {
117 oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") now wiring cache" << std::endl ;
118 _wired=true ;
119 } else if (_size>1) {
120 oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") cache cannot be wired because it contains more than one element" << std::endl ;
121 }
122 }
123
124protected:
125
126 Int_t _maxSize ; ///<! Maximum size
127 Int_t _size = 0; ///<! Actual use
128 Int_t _lastIndex = -1; ///<! Last slot accessed
129
130 std::vector<RooNormSetCache> _nsetCache ; ///<! Normalization/Integration set manager
131 std::vector<T*> _object ; ///<! Payload
132 bool _wired = false; ///<! In wired mode, there is a single payload which is returned always
133
134 ClassDefOverride(RooCacheManager,2) // Cache Manager class generic objects
135} ;
136
137
138/// Constructor for simple caches without RooAbsArg payload. A cache
139/// made with this constructor is not registered with its owner
140/// and will not receive information on server redirects and
141/// cache operation mode changes.
142template <class T>
143RooCacheManager<T>::RooCacheManager(Int_t maxSize) : _maxSize(maxSize)
144{
145
146 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
147 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
148}
149
150
151/// Constructor for simple caches with RooAbsArg derived payload. A cache
152/// made with this constructor is registered with its owner
153/// and will receive information on server redirects and
154/// cache operation mode changes.
155template <class T>
157 : RooAbsCache(owner), _maxSize(maxSize)
158{
159
160 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
161 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
162
163 Int_t i ;
164 for (i=0 ; i<_maxSize ; i++) {
165 _object[i]=nullptr ;
166 }
167
168}
169
170/// Copy constructor.
171template <class T>
173 : RooAbsCache(other, owner), _maxSize(other._maxSize), _size(other._size)
174{
175
176 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
177 _object.resize(_maxSize,nullptr) ; // = new T*[_maxSize] ;
178
179 //std::cout << "RooCacheManager:cctor(" << this << ") other = " << &other << " _size=" << _size << " _maxSize = " << _maxSize << std::endl ;
180
181 Int_t i ;
182 for (i=0 ; i<other._size ; i++) {
183 _nsetCache[i] = other._nsetCache[i];
184 _object[i] = nullptr ;
185 }
186
187 for (i=other._size ; i<_maxSize ; i++) {
188 _object[i] = nullptr ;
189 }
190}
191
192 /// Destructor
193template<class T>
195{
196 for (int i=0 ; i<_size ; i++) {
197 delete _object[i] ;
198 }
199}
200
201
202 /// Clear the cache
203template<class T>
205{
206 for (int i=0 ; i<_maxSize ; i++) {
207 delete _object[i] ;
208 _object[i]=nullptr ;
209 _nsetCache[i].clear() ;
210 }
211 _lastIndex = -1 ;
212 _size = 0 ;
213}
214
215
216/// Clear the cache payload but retain slot mapping w.r.t to
217/// normalization and integration sets.
218template<class T>
220{
221 Int_t i ;
222 for (i=0 ; i<_maxSize ; i++) {
223 delete _object[i] ;
224 _object[i]=nullptr ;
225 }
226}
227
228
229/// Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
230template<class T>
231Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName)
232{
233 // Check if object is already registered
234 Int_t sterileIdx(-1) ;
235 if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
236 delete obj; // important! do not forget to cleanup memory
237 return lastIndex() ;
238 }
239
240
241 if (sterileIdx>=0) {
242 // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
243
244 if (sterileIdx>=_maxSize) {
245 //cout << "RooCacheManager<T>::setObj()/SI increasing object cache size from " << _maxSize << " to " << sterileIdx+4 << std::endl ;
246 _maxSize = sterileIdx+4;
247 _object.resize(_maxSize,nullptr) ;
248 _nsetCache.resize(_maxSize) ;
249 }
250
251
252 _object[sterileIdx] = obj ;
253
254 // Allow optional post-processing of object inserted in cache
255 insertObjectHook(*obj) ;
256
257 return lastIndex() ;
258 }
259
260 if (_size>=_maxSize-1) {
261 //cout << "RooCacheManager<T>::setObj() increasing object cache size from " << _maxSize << " to " << _maxSize*2 << std::endl ;
262 _maxSize *=2 ;
263 _object.resize(_maxSize,nullptr) ;
264 _nsetCache.resize(_maxSize) ;
265 }
266
267 //cout << "RooCacheManager::setObj<T>(" << this << ") _size = " << _size << " _maxSize = " << _maxSize << std::endl ;
268 _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,true) ;
269 if (_object[_size]) {
270 delete _object[_size] ;
271 }
272
273 _object[_size] = obj ;
274 _size++ ;
275
276 // Allow optional post-processing of object inserted in cache
277 insertObjectHook(*obj) ;
278
279 // Unwire cache in case it was wired
280 _wired = false ;
281
282 return _size-1 ;
283}
284
285
286/// Retrieve payload object indexed on nset,uset amd isetRangeName
287/// If sterileIdx is not null, it is set to the index of the sterile
288/// slot in cacse such a slot is recycled.
289template<class T>
290T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName)
291{
292 // Fast-track for wired mode
293 if (_wired) {
294 if(_object[0]==nullptr && sterileIdx) *sterileIdx=0 ;
295 return _object[0] ;
296 }
297
298 Int_t i ;
299 for (i=0 ; i<_size ; i++) {
300 if (_nsetCache[i].contains(nset,iset,isetRangeName)==true) {
301 _lastIndex = i ;
302 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
303 return _object[i] ;
304 }
305 }
306
307 for (i=0 ; i<_size ; i++) {
308 if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,false)==false) {
309 _lastIndex = i ;
310 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
311 return _object[i] ;
312 }
313 }
314
315 return nullptr ;
316}
317
318
319/// Retrieve payload object by slot index.
320template<class T>
322{
323 if (index<0||index>=_size) {
324 oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
325 << index << ") out of range [0," << _size-1 << "]" << std::endl ;
326 return nullptr ;
327 }
328 return _object[index] ;
329}
330
331
332/// Create RooArgSet containing the objects that are both in the cached set 1
333/// with a given index and an input argSet.
334template <class T>
336{
337 RooArgSet output;
338 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet1(), ":")) {
339 if (RooAbsArg *arg = argSet.find(name.c_str())) {
340 output.add(*arg);
341 }
342 }
343 return output;
344}
345
346/// Create RooArgSet containing the objects that are both in the cached set 2
347/// with a given index and an input argSet.
348template <class T>
350{
351 RooArgSet output;
352 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet2(), ":")) {
353 if (RooAbsArg *arg = argSet.find(name.c_str())) {
354 output.add(*arg);
355 }
356 }
357 return output;
358}
359
360#endif
#define oocoutE(o, a)
#define oocoutI(o, a)
#define ClassDefOverride(name, id)
Definition Rtypes.h:347
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:142
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
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:60
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.
T * getObj(const RooArgSet *nset, const RooArgSet *iset, Int_t *sterileIdx=nullptr, const TNamed *isetRangeName=nullptr)
Retrieve payload object indexed on nset,uset amd isetRangeName If sterileIdx is not null,...
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.
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:49
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.