Logo ROOT   6.16/01
Reference Guide
RooExpensiveObjectCache.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$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
17/**
18\file RooExpensiveObjectCache.cxx
19\class RooExpensiveObjectCache
20\ingroup Roofitcore
21
22RooExpensiveObjectCache is a singleton class that serves as repository
23for objects that are expensive to calculate. Owners of such objects
24can registers these here with associated parameter values for which
25the object is valid, so that other instances can, at a later moment
26retrieve these precalculated objects
27**/
28
29
30#include "TClass.h"
31#include "RooFit.h"
32#include "RooSentinel.h"
33#include "RooAbsReal.h"
34#include "RooAbsCategory.h"
35#include "RooArgSet.h"
36#include "RooMsgService.h"
37#include <iostream>
38using namespace std ;
39
41
44 ;
45
47
48
49////////////////////////////////////////////////////////////////////////////////
50/// Constructor
51
53{
54}
55
56
57
58////////////////////////////////////////////////////////////////////////////////
59/// Copy constructor
60
62 TObject(other), _nextUID(0)
63{
64}
65
66
67
68////////////////////////////////////////////////////////////////////////////////
69/// Destructor.
70
72{
73 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter!=_map.end() ; ++iter) {
74 delete iter->second ;
75 }
76
77 if (_instance == this) {
78 _instance = nullptr ;
79 }
80}
81
82
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// Return reference to singleton instance
87
89{
90 if (!_instance) {
93 }
94 return *_instance ;
95}
96
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Static function called by RooSentinel atexit() handler to cleanup at end of program
102
104{
105 delete _instance ;
106}
107
108
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// Register object associated with given name and given associated parameters with given values in cache.
113/// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
114/// need to be the name of cacheObject and with given set of dependent parameters with validity for the
115/// current values of those parameters. It can be retrieved later by callin retrieveObject()
116
117Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, const RooArgSet& params)
118{
119 TIterator* parIter = params.createIterator() ;
120 Bool_t ret = registerObject(ownerName,objectName,cacheObject,parIter) ;
121 delete parIter ;
122
123 return ret ;
124}
125
126
127
128////////////////////////////////////////////////////////////////////////////////
129/// Register object associated with given name and given associated parameters with given values in cache.
130/// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
131/// need to be the name of cacheObject and with given set of dependent parameters with validity for the
132/// current values of those parameters. It can be retrieved later by callin retrieveObject()
133
134Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, TIterator* parIter)
135{
136 // Delete any previous object
137 ExpensiveObject* eo = _map[objectName] ;
138 Int_t olduid(-1) ;
139 if (eo) {
140 olduid = eo->uid() ;
141 delete eo ;
142 }
143 // Install new object
144 _map[objectName] = new ExpensiveObject(olduid!=-1?olduid:_nextUID++, ownerName,cacheObject,parIter) ;
145
146 return kFALSE ;
147}
148
149
150
151////////////////////////////////////////////////////////////////////////////////
152/// Retrieve object from cache that was registered under given name with given parameters, _if_
153/// current parameter values match those that were stored in the registry for this object.
154/// The return object is owned by the cache instance.
155
157{
158 ExpensiveObject* eo = _map[name] ;
159
160 // If no cache element found, return 0 ;
161 if (!eo) {
162 return 0 ;
163 }
164
165 // If parameters also match, return payload ;
166 if (eo->matches(tc,params)) {
167 return eo->payload() ;
168 }
169
170 return 0 ;
171}
172
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// Retrieve payload object of cache element with given unique ID
177
179{
180 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
181 if (iter->second->uid() == uid) {
182 return iter->second->payload() ;
183 }
184 }
185 return 0 ;
186}
187
188
189
190////////////////////////////////////////////////////////////////////////////////
191/// Clear cache element with given unique ID
192/// Retrieve payload object of cache element with given unique ID
193
195{
196 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
197 if (iter->second->uid() == uid) {
198 _map.erase(iter->first) ;
199 return kFALSE ;
200 }
201 }
202 return kTRUE ;
203}
204
205
206
207////////////////////////////////////////////////////////////////////////////////
208/// Place new payload object in cache element with given unique ID. Cache
209/// will take ownership of provided object!
210
212{
213 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
214 if (iter->second->uid() == uid) {
215 iter->second->setPayload(obj) ;
216 return kFALSE ;
217 }
218 }
219 return kTRUE ;
220}
221
222
223
224////////////////////////////////////////////////////////////////////////////////
225/// Clear all cache elements
226
228{
229 _map.clear() ;
230}
231
232
233
234
235
236
237////////////////////////////////////////////////////////////////////////////////
238/// Construct ExpensiveObject oject for inPayLoad and store reference values
239/// for all RooAbsReal and RooAbsCategory parameters in params.
240
241RooExpensiveObjectCache::ExpensiveObject::ExpensiveObject(Int_t uidIn, const char* inOwnerName, TObject& inPayload, TIterator* parIter)
242{
243 _uid = uidIn ;
244 _ownerName = inOwnerName;
245
246 _payload = &inPayload ;
247
248 RooAbsArg* arg ;
249 parIter->Reset() ;
250 while((arg=(RooAbsArg*)parIter->Next() )) {
251 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
252 if (real) {
253 _realRefParams[real->GetName()] = real->getVal() ;
254 } else {
255 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
256 if (cat) {
257 _catRefParams[cat->GetName()] = cat->getIndex() ;
258 } else {
259 oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << endl ;
260 }
261 }
262 }
263
264}
265
266
267
268////////////////////////////////////////////////////////////////////////////////
269
271 _uid(uidIn),
272 _realRefParams(other._realRefParams),
273 _catRefParams(other._catRefParams),
274 _ownerName(other._ownerName)
275{
276 _payload = other._payload->Clone() ;
277}
278
279
280
281////////////////////////////////////////////////////////////////////////////////
282
284{
285 delete _payload ;
286}
287
288
289
290
291
292////////////////////////////////////////////////////////////////////////////////
293/// Check object type ;
294
296{
297 if (_payload->IsA() != tc) {
298 return kFALSE;
299 }
300
301 // Check parameters
302 TIterator* iter = params.createIterator() ;
303 RooAbsArg* arg ;
304 while((arg=(RooAbsArg*)iter->Next() )) {
305 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
306 if (real) {
307 if (fabs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
308 delete iter ;
309 return kFALSE ;
310 }
311 } else {
312 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
313 if (cat) {
314 if (cat->getIndex() != _catRefParams[cat->GetName()]) {
315 delete iter ;
316 return kFALSE ;
317 }
318 }
319 }
320 }
321 delete iter ;
322
323 return kTRUE ;
324
325}
326
327
328
329////////////////////////////////////////////////////////////////////////////////
330
332{
333 map<TString,ExpensiveObject*>::const_iterator iter = _map.begin() ;
334
335 while(iter!=_map.end()) {
336 cout << "uid = " << iter->second->uid() << " key=" << iter->first << " value=" ;
337 iter->second->print() ;
338 ++iter ;
339 }
340}
341
342
343
344////////////////////////////////////////////////////////////////////////////////
345
347{
348 cout << _payload->IsA()->GetName() << "::" << _payload->GetName() ;
349 if (_realRefParams.size()>0 || _catRefParams.size()>0) {
350 cout << " parameters=( " ;
351 auto iter = _realRefParams.begin() ;
352 while(iter!=_realRefParams.end()) {
353 cout << iter->first << "=" << iter->second << " " ;
354 ++iter ;
355 }
356 auto iter2 = _catRefParams.begin() ;
357 while(iter2!=_catRefParams.end()) {
358 cout << iter2->first << "=" << iter2->second << " " ;
359 ++iter2 ;
360 }
361 cout << ")" ;
362 }
363 cout << endl ;
364}
365
366
367
368
369////////////////////////////////////////////////////////////////////////////////
370
372{
373 map<TString,ExpensiveObject*>::const_iterator iter = other._map.begin() ;
374 while(iter!=other._map.end()) {
375 if (string(ownerName)==iter->second->ownerName()) {
376 _map[iter->first.Data()] = new ExpensiveObject(_nextUID++, *iter->second) ;
377 if (verbose) {
378 oocoutI(iter->second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
379 << iter->first << " associated with object " << iter->second->ownerName() << endl ;
380 }
381 }
382 ++iter ;
383 }
384
385}
#define e(i)
Definition: RSha256.hxx:103
#define oocoutW(o, a)
Definition: RooMsgService.h:46
#define oocoutI(o, a)
Definition: RooMsgService.h:44
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:363
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
virtual Int_t getIndex() const
Return index number of current state.
TIterator * createIterator(Bool_t dir=kIterForward) const
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
Double_t getVal(const RooArgSet *set=0) const
Evaluate object. Returns either cached value or triggers a recalculation.
Definition: RooAbsReal.h:64
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
std::map< TString, Double_t > _realRefParams
Bool_t matches(TClass *tc, const RooArgSet &params)
Check object type ;.
RooExpensiveObjectCache is a singleton class that serves as repository for objects that are expensive...
static void cleanup()
Static function called by RooSentinel atexit() handler to cleanup at end of program.
const TObject * getObj(Int_t uniqueID)
Retrieve payload object of cache element with given unique ID
Bool_t registerObject(const char *ownerName, const char *objectName, TObject &cacheObject, TIterator *paramIter)
Register object associated with given name and given associated parameters with given values in cache...
static RooExpensiveObjectCache * _instance
std::map< TString, ExpensiveObject * > _map
const TObject * retrieveObject(const char *name, TClass *tclass, const RooArgSet &params)
Retrieve object from cache that was registered under given name with given parameters,...
Bool_t clearObj(Int_t uniqueID)
Clear cache element with given unique ID Retrieve payload object of cache element with given unique I...
void importCacheObjects(RooExpensiveObjectCache &other, const char *ownerName, Bool_t verbose=kFALSE)
void clearAll()
Clear all cache elements.
virtual ~RooExpensiveObjectCache()
Destructor.
static RooExpensiveObjectCache & instance()
Return reference to singleton instance.
Bool_t setObj(Int_t uniqueID, TObject *obj)
Place new payload object in cache element with given unique ID.
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:73
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
Iterator abstract base class.
Definition: TIterator.h:30
virtual void Reset()=0
virtual TObject * Next()=0
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:144
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
STL namespace.