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
22Singleton 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 (auto& item : _map) {
48 delete item.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
179 RooArgSet const &params)
180 : _uid(uidIn), _payload(&inPayload), _ownerName(inOwnerName)
181{
182
183 for(RooAbsArg * arg : params) {
184 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
185 if (real) {
186 _realRefParams[real->GetName()] = real->getVal() ;
187 } else {
188 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
189 if (cat) {
190 _catRefParams[cat->GetName()] = cat->getCurrentIndex() ;
191 } else {
192 oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << std::endl ;
193 }
194 }
195 }
196
197}
198
199
200
201////////////////////////////////////////////////////////////////////////////////
202
204 : _uid(uidIn),
205 _payload(other._payload->Clone()),
206 _realRefParams(other._realRefParams),
207 _catRefParams(other._catRefParams),
208 _ownerName(other._ownerName)
209{
210}
211
212
213
214////////////////////////////////////////////////////////////////////////////////
215
217{
218 delete _payload ;
219}
220
221
222
223
224
225////////////////////////////////////////////////////////////////////////////////
226/// Check object type ;
227
229{
230 if (_payload->IsA() != tc) {
231 return false;
232 }
233
234 // Check parameters
235 for(RooAbsArg * arg : params) {
236 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
237 if (real) {
238 if (std::abs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
239 return false ;
240 }
241 } else {
242 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
243 if (cat) {
244 if (cat->getCurrentIndex() != _catRefParams[cat->GetName()]) {
245 return false ;
246 }
247 }
248 }
249 }
250
251 return true ;
252
253}
254
255
256
257////////////////////////////////////////////////////////////////////////////////
258
260{
261 for(auto const& item : _map) {
262 std::cout << "uid = " << item.second->uid() << " key=" << item.first << " value=" ;
263 item.second->print() ;
264 }
265}
266
267
268
269////////////////////////////////////////////////////////////////////////////////
270
272{
273 std::cout << _payload->ClassName() << "::" << _payload->GetName() ;
274 if (!_realRefParams.empty() || !_catRefParams.empty()) {
275 std::cout << " parameters=( " ;
276 auto iter = _realRefParams.begin() ;
277 while(iter!=_realRefParams.end()) {
278 std::cout << iter->first << "=" << iter->second << " " ;
279 ++iter ;
280 }
281 auto iter2 = _catRefParams.begin() ;
282 while(iter2!=_catRefParams.end()) {
283 std::cout << iter2->first << "=" << iter2->second << " " ;
284 ++iter2 ;
285 }
286 std::cout << ")" ;
287 }
288 std::cout << std::endl ;
289}
290
291
292
293
294////////////////////////////////////////////////////////////////////////////////
295
296void RooExpensiveObjectCache::importCacheObjects(RooExpensiveObjectCache& other, const char* ownerName, bool verbose)
297{
298 for(auto const& item : other._map) {
299 if (std::string(ownerName)==item.second->ownerName()) {
300 _map[item.first.Data()] = new ExpensiveObject(_nextUID++, *item.second) ;
301 if (verbose) {
302 oocoutI(item.second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
303 << item.first << " associated with object " << item.second->ownerName() << std::endl ;
304 }
305 }
306 }
307
308}
#define e(i)
Definition RSha256.hxx:103
#define oocoutW(o, a)
#define oocoutI(o, a)
#define ClassImp(name)
Definition Rtypes.h:382
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
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:24
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.
Singleton class that serves as repository for objects that are expensive to calculate.
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:241
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:225