Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
38
39
40////////////////////////////////////////////////////////////////////////////////
41/// Destructor.
42
44{
45 for (auto& item : _map) {
46 delete item.second;
47 }
48}
49
50
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Return reference to singleton instance
55
61
62
63////////////////////////////////////////////////////////////////////////////////
64/// Register object associated with given name and given associated parameters with given values in cache.
65/// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
66/// need to be the name of cacheObject and with given set of dependent parameters with validity for the
67/// current values of those parameters. It can be retrieved later by callin retrieveObject()
68
69bool RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, const RooArgSet& params)
70{
71 // Delete any previous object
72 ExpensiveObject* eo = _map[objectName] ;
73 Int_t olduid(-1) ;
74 if (eo) {
75 olduid = eo->uid() ;
76 delete eo ;
77 }
78 // Install new object
79 _map[objectName] = new ExpensiveObject(olduid!=-1?olduid:_nextUID++, ownerName,cacheObject,params) ;
80
81 return false ;
82}
83
84
85
86////////////////////////////////////////////////////////////////////////////////
87/// Retrieve object from cache that was registered under given name with given parameters, _if_
88/// current parameter values match those that were stored in the registry for this object.
89/// The return object is owned by the cache instance.
90
92{
94
95 // If no cache element found, return 0 ;
96 if (!eo) {
97 return nullptr ;
98 }
99
100 // If parameters also match, return payload ;
101 if (eo->matches(tc,params)) {
102 return eo->payload() ;
103 }
104
105 return nullptr ;
106}
107
108
109
110////////////////////////////////////////////////////////////////////////////////
111/// Retrieve payload object of cache element with given unique ID
112
114{
115 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
116 if (iter->second->uid() == uid) {
117 return iter->second->payload() ;
118 }
119 }
120 return nullptr ;
121}
122
123
124
125////////////////////////////////////////////////////////////////////////////////
126/// Clear cache element with given unique ID
127/// Retrieve payload object of cache element with given unique ID
128
130{
131 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
132 if (iter->second->uid() == uid) {
133 _map.erase(iter->first) ;
134 return false ;
135 }
136 }
137 return true ;
138}
139
140
141
142////////////////////////////////////////////////////////////////////////////////
143/// Place new payload object in cache element with given unique ID. Cache
144/// will take ownership of provided object!
145
147{
148 for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; ++iter) {
149 if (iter->second->uid() == uid) {
150 iter->second->setPayload(obj) ;
151 return false ;
152 }
153 }
154 return true ;
155}
156
157
158
159////////////////////////////////////////////////////////////////////////////////
160/// Clear all cache elements
161
163{
164 _map.clear() ;
165}
166
167
168
169
170
171
172////////////////////////////////////////////////////////////////////////////////
173/// Construct ExpensiveObject object for inPayLoad and store reference values
174/// for all RooAbsReal and RooAbsCategory parameters in params.
175
177 RooArgSet const &params)
178 : _uid(uidIn), _payload(&inPayload), _ownerName(inOwnerName)
179{
180
181 for(RooAbsArg * arg : params) {
182 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
183 if (real) {
184 _realRefParams[real->GetName()] = real->getVal() ;
185 } else {
186 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
187 if (cat) {
188 _catRefParams[cat->GetName()] = cat->getCurrentIndex() ;
189 } else {
190 oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << std::endl ;
191 }
192 }
193 }
194
195}
196
197
198
199////////////////////////////////////////////////////////////////////////////////
200
202 : _uid(uidIn),
203 _payload(other._payload->Clone()),
204 _realRefParams(other._realRefParams),
205 _catRefParams(other._catRefParams),
206 _ownerName(other._ownerName)
207{
208}
209
210
211
212////////////////////////////////////////////////////////////////////////////////
213
218
219
220
221
222
223////////////////////////////////////////////////////////////////////////////////
224/// Check object type ;
225
227{
228 if (_payload->IsA() != tc) {
229 return false;
230 }
231
232 // Check parameters
233 for(RooAbsArg * arg : params) {
234 RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
235 if (real) {
236 if (std::abs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
237 return false ;
238 }
239 } else {
240 RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
241 if (cat) {
242 if (cat->getCurrentIndex() != _catRefParams[cat->GetName()]) {
243 return false ;
244 }
245 }
246 }
247 }
248
249 return true ;
250
251}
252
253
254
255////////////////////////////////////////////////////////////////////////////////
256
258{
259 for(auto const& item : _map) {
260 std::cout << "uid = " << item.second->uid() << " key=" << item.first << " value=" ;
261 item.second->print() ;
262 }
263}
264
265
266
267////////////////////////////////////////////////////////////////////////////////
268
270{
271 std::cout << _payload->ClassName() << "::" << _payload->GetName() ;
272 if (!_realRefParams.empty() || !_catRefParams.empty()) {
273 std::cout << " parameters=( " ;
274 auto iter = _realRefParams.begin() ;
275 while(iter!=_realRefParams.end()) {
276 std::cout << iter->first << "=" << iter->second << " " ;
277 ++iter ;
278 }
279 auto iter2 = _catRefParams.begin() ;
280 while(iter2!=_catRefParams.end()) {
281 std::cout << iter2->first << "=" << iter2->second << " " ;
282 ++iter2 ;
283 }
284 std::cout << ")" ;
285 }
286 std::cout << std::endl ;
287}
288
289
290
291
292////////////////////////////////////////////////////////////////////////////////
293
295{
296 for(auto const& item : other._map) {
297 if (std::string(ownerName)==item.second->ownerName()) {
298 _map[item.first.Data()] = new ExpensiveObject(_nextUID++, *item.second) ;
299 if (verbose) {
300 oocoutI(item.second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
301 << item.first << " associated with object " << item.second->ownerName() << std::endl ;
302 }
303 }
304 }
305
306}
#define e(i)
Definition RSha256.hxx:103
#define oocoutW(o, a)
#define oocoutI(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
const_iterator begin() const
const_iterator end() const
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
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:84
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