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
30
31#include "TClass.h"
32#include "RooAbsReal.h"
33#include "RooAbsCategory.h"
34#include "RooArgSet.h"
35#include "RooMsgService.h"
36#include <iostream>
37
40
41
42////////////////////////////////////////////////////////////////////////////////
43/// Destructor.
44
46{
47 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter!=_map.end() ; ++iter) {
48 delete iter->second ;
49 }
50}
51
52
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Return reference to singleton instance
57
59{
61 return instance;
62}
63
64
65////////////////////////////////////////////////////////////////////////////////
66/// Register object associated with given name and given associated parameters with given values in cache.
67/// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
68/// need to be the name of cacheObject and with given set of dependent parameters with validity for the
69/// current values of those parameters. It can be retrieved later by callin retrieveObject()
70
71bool RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, const RooArgSet& params)
72{
73 // Delete any previous object
74 ExpensiveObject* eo = _map[objectName] ;
75 Int_t olduid(-1) ;
76 if (eo) {
77 olduid = eo->uid() ;
78 delete eo ;
79 }
80 // Install new object
81 _map[objectName] = new ExpensiveObject(olduid!=-1?olduid:_nextUID++, ownerName,cacheObject,params) ;
82
83 return false ;
84}
85
86
87
88////////////////////////////////////////////////////////////////////////////////
89/// Retrieve object from cache that was registered under given name with given parameters, _if_
90/// current parameter values match those that were stored in the registry for this object.
91/// The return object is owned by the cache instance.
92
94{
96
97 // If no cache element found, return 0 ;
98 if (!eo) {
99 return nullptr ;
100 }
101
102 // If parameters also match, return payload ;
103 if (eo->matches(tc,params)) {
104 return eo->payload() ;
105 }
106
107 return nullptr ;
108}
109
110
111
112////////////////////////////////////////////////////////////////////////////////
113/// Retrieve payload object of cache element with given unique ID
114
116{
117 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
118 if (iter->second->uid() == uid) {
119 return iter->second->payload() ;
120 }
121 }
122 return nullptr ;
123}
124
125
126
127////////////////////////////////////////////////////////////////////////////////
128/// Clear cache element with given unique ID
129/// Retrieve payload object of cache element with given unique ID
130
132{
133 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
134 if (iter->second->uid() == uid) {
135 _map.erase(iter->first) ;
136 return false ;
137 }
138 }
139 return true ;
140}
141
142
143
144////////////////////////////////////////////////////////////////////////////////
145/// Place new payload object in cache element with given unique ID. Cache
146/// will take ownership of provided object!
147
149{
150 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
151 if (iter->second->uid() == uid) {
152 iter->second->setPayload(obj) ;
153 return false ;
154 }
155 }
156 return true ;
157}
158
159
160
161////////////////////////////////////////////////////////////////////////////////
162/// Clear all cache elements
163
165{
166 _map.clear() ;
167}
168
169
170
171
172
173
174////////////////////////////////////////////////////////////////////////////////
175/// Construct ExpensiveObject object for inPayLoad and store reference values
176/// for all RooAbsReal and RooAbsCategory parameters in params.
177
178RooExpensiveObjectCache::ExpensiveObject::ExpensiveObject(Int_t uidIn, const char* inOwnerName, TObject& inPayload, RooArgSet const& params)
179{
180 _uid = uidIn ;
181 _ownerName = inOwnerName;
182
183 _payload = &inPayload ;
184
185 for(RooAbsArg * arg : params) {
186 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
187 if (real) {
188 _realRefParams[real->GetName()] = real->getVal() ;
189 } else {
190 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
191 if (cat) {
192 _catRefParams[cat->GetName()] = cat->getCurrentIndex() ;
193 } else {
194 oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << std::endl ;
195 }
196 }
197 }
198
199}
200
201
202
203////////////////////////////////////////////////////////////////////////////////
204
206 _uid(uidIn),
207 _realRefParams(other._realRefParams),
208 _catRefParams(other._catRefParams),
209 _ownerName(other._ownerName)
210{
211 _payload = other._payload->Clone() ;
212}
213
214
215
216////////////////////////////////////////////////////////////////////////////////
217
219{
220 delete _payload ;
221}
222
223
224
225
226
227////////////////////////////////////////////////////////////////////////////////
228/// Check object type ;
229
231{
232 if (_payload->IsA() != tc) {
233 return false;
234 }
235
236 // Check parameters
237 for(RooAbsArg * arg : params) {
238 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
239 if (real) {
240 if (std::abs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
241 return false ;
242 }
243 } else {
244 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
245 if (cat) {
246 if (cat->getCurrentIndex() != _catRefParams[cat->GetName()]) {
247 return false ;
248 }
249 }
250 }
251 }
252
253 return true ;
254
255}
256
257
258
259////////////////////////////////////////////////////////////////////////////////
260
262{
263 for(auto const& item : _map) {
264 std::cout << "uid = " << item.second->uid() << " key=" << item.first << " value=" ;
265 item.second->print() ;
266 }
267}
268
269
270
271////////////////////////////////////////////////////////////////////////////////
272
274{
275 std::cout << _payload->ClassName() << "::" << _payload->GetName() ;
276 if (!_realRefParams.empty() || !_catRefParams.empty()) {
277 std::cout << " parameters=( " ;
278 auto iter = _realRefParams.begin() ;
279 while(iter!=_realRefParams.end()) {
280 std::cout << iter->first << "=" << iter->second << " " ;
281 ++iter ;
282 }
283 auto iter2 = _catRefParams.begin() ;
284 while(iter2!=_catRefParams.end()) {
285 std::cout << iter2->first << "=" << iter2->second << " " ;
286 ++iter2 ;
287 }
288 std::cout << ")" ;
289 }
290 std::cout << std::endl ;
291}
292
293
294
295
296////////////////////////////////////////////////////////////////////////////////
297
298void RooExpensiveObjectCache::importCacheObjects(RooExpensiveObjectCache& other, const char* ownerName, bool verbose)
299{
300 for(auto const& item : other._map) {
301 if (std::string(ownerName)==item.second->ownerName()) {
302 _map[item.first.Data()] = new ExpensiveObject(_nextUID++, *item.second) ;
303 if (verbose) {
304 oocoutI(item.second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
305 << item.first << " associated with object " << item.second->ownerName() << std::endl ;
306 }
307 }
308 }
309
310}
#define e(i)
Definition RSha256.hxx:103
#define oocoutW(o, a)
#define oocoutI(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
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:79
A space to attach TBranches.
virtual value_type getCurrentIndex() const
Return index number of current state.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
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 registerObject(const char *ownerName, const char *objectName, TObject &cacheObject, const RooArgSet &params)
Register object associated with given name and given associated parameters with given values in cache...
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.
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:81
const char * GetName() const override
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:223
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207