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