Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "RooAbsReal.h"
33#include "RooAbsCategory.h"
34#include "RooArgSet.h"
35#include "RooMsgService.h"
36#include <iostream>
37using namespace std ;
38
40
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// Constructor
47
49{
50}
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55/// Copy constructor
56
58 TObject(other), _nextUID(0)
59{
60}
61
62
63
64////////////////////////////////////////////////////////////////////////////////
65/// Destructor.
66
68{
69 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter!=_map.end() ; ++iter) {
70 delete iter->second ;
71 }
72}
73
74
75
76
77////////////////////////////////////////////////////////////////////////////////
78/// Return reference to singleton instance
79
81{
83 return instance;
84}
85
86
87////////////////////////////////////////////////////////////////////////////////
88/// Register object associated with given name and given associated parameters with given values in cache.
89/// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
90/// need to be the name of cacheObject and with given set of dependent parameters with validity for the
91/// current values of those parameters. It can be retrieved later by callin retrieveObject()
92
93Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, const RooArgSet& params)
94{
95 TIterator* parIter = params.createIterator() ;
96 Bool_t ret = registerObject(ownerName,objectName,cacheObject,parIter) ;
97 delete parIter ;
98
99 return ret ;
100}
101
102
103
104////////////////////////////////////////////////////////////////////////////////
105/// Register object associated with given name and given associated parameters with given values in cache.
106/// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
107/// need to be the name of cacheObject and with given set of dependent parameters with validity for the
108/// current values of those parameters. It can be retrieved later by callin retrieveObject()
109
110Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, TIterator* parIter)
111{
112 // Delete any previous object
113 ExpensiveObject* eo = _map[objectName] ;
114 Int_t olduid(-1) ;
115 if (eo) {
116 olduid = eo->uid() ;
117 delete eo ;
118 }
119 // Install new object
120 _map[objectName] = new ExpensiveObject(olduid!=-1?olduid:_nextUID++, ownerName,cacheObject,parIter) ;
121
122 return kFALSE ;
123}
124
125
126
127////////////////////////////////////////////////////////////////////////////////
128/// Retrieve object from cache that was registered under given name with given parameters, _if_
129/// current parameter values match those that were stored in the registry for this object.
130/// The return object is owned by the cache instance.
131
133{
134 ExpensiveObject* eo = _map[name] ;
135
136 // If no cache element found, return 0 ;
137 if (!eo) {
138 return 0 ;
139 }
140
141 // If parameters also match, return payload ;
142 if (eo->matches(tc,params)) {
143 return eo->payload() ;
144 }
145
146 return 0 ;
147}
148
149
150
151////////////////////////////////////////////////////////////////////////////////
152/// Retrieve payload object of cache element with given unique ID
153
155{
156 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
157 if (iter->second->uid() == uid) {
158 return iter->second->payload() ;
159 }
160 }
161 return 0 ;
162}
163
164
165
166////////////////////////////////////////////////////////////////////////////////
167/// Clear cache element with given unique ID
168/// Retrieve payload object of cache element with given unique ID
169
171{
172 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
173 if (iter->second->uid() == uid) {
174 _map.erase(iter->first) ;
175 return kFALSE ;
176 }
177 }
178 return kTRUE ;
179}
180
181
182
183////////////////////////////////////////////////////////////////////////////////
184/// Place new payload object in cache element with given unique ID. Cache
185/// will take ownership of provided object!
186
188{
189 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
190 if (iter->second->uid() == uid) {
191 iter->second->setPayload(obj) ;
192 return kFALSE ;
193 }
194 }
195 return kTRUE ;
196}
197
198
199
200////////////////////////////////////////////////////////////////////////////////
201/// Clear all cache elements
202
204{
205 _map.clear() ;
206}
207
208
209
210
211
212
213////////////////////////////////////////////////////////////////////////////////
214/// Construct ExpensiveObject oject for inPayLoad and store reference values
215/// for all RooAbsReal and RooAbsCategory parameters in params.
216
217RooExpensiveObjectCache::ExpensiveObject::ExpensiveObject(Int_t uidIn, const char* inOwnerName, TObject& inPayload, TIterator* parIter)
218{
219 _uid = uidIn ;
220 _ownerName = inOwnerName;
221
222 _payload = &inPayload ;
223
224 RooAbsArg* arg ;
225 parIter->Reset() ;
226 while((arg=(RooAbsArg*)parIter->Next() )) {
227 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
228 if (real) {
229 _realRefParams[real->GetName()] = real->getVal() ;
230 } else {
231 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
232 if (cat) {
233 _catRefParams[cat->GetName()] = cat->getCurrentIndex() ;
234 } else {
235 oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << endl ;
236 }
237 }
238 }
239
240}
241
242
243
244////////////////////////////////////////////////////////////////////////////////
245
247 _uid(uidIn),
248 _realRefParams(other._realRefParams),
249 _catRefParams(other._catRefParams),
250 _ownerName(other._ownerName)
251{
252 _payload = other._payload->Clone() ;
253}
254
255
256
257////////////////////////////////////////////////////////////////////////////////
258
260{
261 delete _payload ;
262}
263
264
265
266
267
268////////////////////////////////////////////////////////////////////////////////
269/// Check object type ;
270
272{
273 if (_payload->IsA() != tc) {
274 return kFALSE;
275 }
276
277 // Check parameters
278 TIterator* iter = params.createIterator() ;
279 RooAbsArg* arg ;
280 while((arg=(RooAbsArg*)iter->Next() )) {
281 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
282 if (real) {
283 if (fabs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
284 delete iter ;
285 return kFALSE ;
286 }
287 } else {
288 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
289 if (cat) {
290 if (cat->getCurrentIndex() != _catRefParams[cat->GetName()]) {
291 delete iter ;
292 return kFALSE ;
293 }
294 }
295 }
296 }
297 delete iter ;
298
299 return kTRUE ;
300
301}
302
303
304
305////////////////////////////////////////////////////////////////////////////////
306
308{
309 map<TString,ExpensiveObject*>::const_iterator iter = _map.begin() ;
310
311 while(iter!=_map.end()) {
312 cout << "uid = " << iter->second->uid() << " key=" << iter->first << " value=" ;
313 iter->second->print() ;
314 ++iter ;
315 }
316}
317
318
319
320////////////////////////////////////////////////////////////////////////////////
321
323{
324 cout << _payload->IsA()->GetName() << "::" << _payload->GetName() ;
325 if (_realRefParams.size()>0 || _catRefParams.size()>0) {
326 cout << " parameters=( " ;
327 auto iter = _realRefParams.begin() ;
328 while(iter!=_realRefParams.end()) {
329 cout << iter->first << "=" << iter->second << " " ;
330 ++iter ;
331 }
332 auto iter2 = _catRefParams.begin() ;
333 while(iter2!=_catRefParams.end()) {
334 cout << iter2->first << "=" << iter2->second << " " ;
335 ++iter2 ;
336 }
337 cout << ")" ;
338 }
339 cout << endl ;
340}
341
342
343
344
345////////////////////////////////////////////////////////////////////////////////
346
348{
349 map<TString,ExpensiveObject*>::const_iterator iter = other._map.begin() ;
350 while(iter!=other._map.end()) {
351 if (string(ownerName)==iter->second->ownerName()) {
352 _map[iter->first.Data()] = new ExpensiveObject(_nextUID++, *iter->second) ;
353 if (verbose) {
354 oocoutI(iter->second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
355 << iter->first << " associated with object " << iter->second->ownerName() << endl ;
356 }
357 }
358 ++iter ;
359 }
360
361}
#define e(i)
Definition RSha256.hxx:103
#define oocoutW(o, a)
#define oocoutI(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
virtual value_type getCurrentIndex() const
Return index number of current state.
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:64
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:94
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
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...
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...
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.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
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:41
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition TObject.cxx:216