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
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] ;
75 }
76
77 T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) ;
78 Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=nullptr) ;
79
80 void reset() ;
81 virtual void sterilize() ;
82
83 /// Return index of slot used in last get or set operation
84 Int_t lastIndex() const {
85 return _lastIndex ;
86 }
87 /// Return size of cache
88 Int_t cacheSize() const {
89 return _size ;
90 }
91
92 /// Interface function to intercept server redirects
93 bool redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/,
94 bool /*nameChange*/, bool /*isRecursive*/) override {
95 return false ;
96 }
97 /// Interface function to intercept cache operation mode changes
98 void operModeHook() override {
99 }
100 /// Interface function to cache add contents to output in tree printing mode
101 void printCompactTreeHook(std::ostream&, const char *) override {
102 }
103
105 RooArgSet selectFromSet1(RooArgSet const& argSet, int index) const ;
106 RooArgSet selectFromSet2(RooArgSet const& argSet, int index) const ;
107
108 /// Interface function to perform post-insert operations on cached object
109 virtual void insertObjectHook(T&) {
110 }
111
112 void wireCache() override {
113 if (_size==0) {
114 oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") no cached elements!" << std::endl ;
115 } else if (_size==1) {
116 oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") now wiring cache" << std::endl ;
117 _wired=true ;
118 } else if (_size>1) {
119 oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") cache cannot be wired because it contains more than one element" << std::endl ;
120 }
121 }
122
123protected:
124
125 Int_t _maxSize ; ///<! Maximum size
126 Int_t _size = 0; ///<! Actual use
127 Int_t _lastIndex = -1; ///<! Last slot accessed
128
129 std::vector<RooNormSetCache> _nsetCache ; ///<! Normalization/Integration set manager
130 std::vector<T*> _object ; ///<! Payload
131 bool _wired = false; ///<! In wired mode, there is a single payload which is returned always
132
133 ClassDefOverride(RooCacheManager,2) // Cache Manager class generic objects
134} ;
135
136
137/// Constructor for simple caches without RooAbsArg payload. A cache
138/// made with this constructor is not registered with its owner
139/// and will not receive information on server redirects and
140/// cache operation mode changes.
141template <class T>
143{
144
145 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
146 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
147}
148
149
150/// Constructor for simple caches with RooAbsArg derived payload. A cache
151/// made with this constructor is registered with its owner
152/// and will receive information on server redirects and
153/// cache operation mode changes.
154template <class T>
156 : RooAbsCache(owner), _maxSize(maxSize)
157{
158
159 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
160 _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
161
162 Int_t i ;
163 for (i=0 ; i<_maxSize ; i++) {
164 _object[i]=nullptr ;
165 }
166
167}
168
169/// Copy constructor.
170template <class T>
172 : RooAbsCache(other, owner), _maxSize(other._maxSize), _size(other._size)
173{
174
175 _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
176 _object.resize(_maxSize,nullptr) ; // = new T*[_maxSize] ;
177
178 //std::cout << "RooCacheManager:cctor(" << this << ") other = " << &other << " _size=" << _size << " _maxSize = " << _maxSize << std::endl ;
179
180 Int_t i ;
181 for (i=0 ; i<other._size ; i++) {
182 _nsetCache[i] = other._nsetCache[i];
183 _object[i] = nullptr ;
184 }
185
186 for (i=other._size ; i<_maxSize ; i++) {
187 _object[i] = nullptr ;
188 }
189}
190
191 /// Destructor
192template<class T>
194{
195 for (int i=0 ; i<_size ; i++) {
196 delete _object[i] ;
197 }
198}
199
200
201 /// Clear the cache
202template<class T>
204{
205 for (int i=0 ; i<_maxSize ; i++) {
206 delete _object[i] ;
207 _object[i]=nullptr ;
208 _nsetCache[i].clear() ;
209 }
210 _lastIndex = -1 ;
211 _size = 0 ;
212}
213
214
215/// Clear the cache payload but retain slot mapping w.r.t to
216/// normalization and integration sets.
217template<class T>
219{
220 Int_t i ;
221 for (i=0 ; i<_maxSize ; i++) {
222 delete _object[i] ;
223 _object[i]=nullptr ;
224 }
225}
226
227
228/// Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
229template<class T>
231{
232 // Check if object is already registered
233 Int_t sterileIdx(-1) ;
234 if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
235 delete obj; // important! do not forget to cleanup memory
236 return lastIndex() ;
237 }
238
239
240 if (sterileIdx>=0) {
241 // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
242
243 if (sterileIdx>=_maxSize) {
244 //cout << "RooCacheManager<T>::setObj()/SI increasing object cache size from " << _maxSize << " to " << sterileIdx+4 << std::endl ;
245 _maxSize = sterileIdx+4;
246 _object.resize(_maxSize,nullptr) ;
247 _nsetCache.resize(_maxSize) ;
248 }
249
250
251 _object[sterileIdx] = obj ;
252
253 // Allow optional post-processing of object inserted in cache
254 insertObjectHook(*obj) ;
255
256 return lastIndex() ;
257 }
258
259 if (_size>=_maxSize-1) {
260 //cout << "RooCacheManager<T>::setObj() increasing object cache size from " << _maxSize << " to " << _maxSize*2 << std::endl ;
261 _maxSize *=2 ;
262 _object.resize(_maxSize,nullptr) ;
263 _nsetCache.resize(_maxSize) ;
264 }
265
266 //cout << "RooCacheManager::setObj<T>(" << this << ") _size = " << _size << " _maxSize = " << _maxSize << std::endl ;
267 _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,true) ;
268 if (_object[_size]) {
269 delete _object[_size] ;
270 }
271
272 _object[_size] = obj ;
273 _size++ ;
274
275 // Allow optional post-processing of object inserted in cache
276 insertObjectHook(*obj) ;
277
278 // Unwire cache in case it was wired
279 _wired = false ;
280
281 return _size-1 ;
282}
283
284
285/// Retrieve payload object indexed on nset,uset amd isetRangeName
286/// If sterileIdx is not null, it is set to the index of the sterile
287/// slot in cacse such a slot is recycled.
288template<class T>
290{
291 // Fast-track for wired mode
292 if (_wired) {
293 if(_object[0]==nullptr && sterileIdx) *sterileIdx=0 ;
294 return _object[0] ;
295 }
296
297 Int_t i ;
298 for (i=0 ; i<_size ; i++) {
299 if (_nsetCache[i].contains(nset,iset,isetRangeName)==true) {
300 _lastIndex = i ;
301 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
302 return _object[i] ;
303 }
304 }
305
306 for (i=0 ; i<_size ; i++) {
307 if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,false)==false) {
308 _lastIndex = i ;
309 if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
310 return _object[i] ;
311 }
312 }
313
314 return nullptr ;
315}
316
317
318/// Retrieve payload object by slot index.
319template<class T>
321{
322 if (index<0||index>=_size) {
323 oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
324 << index << ") out of range [0," << _size-1 << "]" << std::endl ;
325 return nullptr ;
326 }
327 return _object[index] ;
328}
329
330
331/// Create RooArgSet containing the objects that are both in the cached set 1
332/// with a given index and an input argSet.
333template <class T>
335{
337 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet1(), ":")) {
338 if (RooAbsArg *arg = argSet.find(name.c_str())) {
339 output.add(*arg);
340 }
341 }
342 return output;
343}
344
345/// Create RooArgSet containing the objects that are both in the cached set 2
346/// with a given index and an input argSet.
347template <class T>
349{
351 for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet2(), ":")) {
352 if (RooAbsArg *arg = argSet.find(name.c_str())) {
353 output.add(*arg);
354 }
355 }
356 return output;
357}
358
359#endif
#define oocoutE(o, a)
#define oocoutI(o, a)
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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: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:61
Abstract container object that can hold multiple RooAbsArg objects.
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()