Logo ROOT   6.10/09
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 
22 RooExpensiveObjectCache is a singleton class that serves as repository
23 for objects that are expensive to calculate. Owners of such objects
24 can registers these here with associated parameter values for which
25 the object is valid, so that other instances can, at a later moment
26 retrieve these precalculated objects
27 **/
28 
29 
30 #include "TClass.h"
31 #include "RooFit.h"
32 #include "RooSentinel.h"
33 #include "RooAbsReal.h"
34 #include "RooAbsCategory.h"
35 #include "RooArgSet.h"
36 #include "RooMsgService.h"
37 #include <iostream>
38 #include <math.h>
39 using namespace std ;
40 
42 
45  ;
46 
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Constructor
52 
54 {
55 }
56 
57 
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Copy constructor
61 
63  TObject(other), _nextUID(0)
64 {
65 }
66 
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Destructor.
71 
73 {
74  for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter!=_map.end() ; ++iter) {
75  delete iter->second ;
76  }
77 
78  if (_instance == this) {
79  _instance = 0 ;
80  }
81 }
82 
83 
84 
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Return reference to singleton instance
88 
90 {
91  if (!_instance) {
94  }
95  return *_instance ;
96 }
97 
98 
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Static function called by RooSentinel atexit() handler to cleanup at end of program
103 
105 {
106  delete _instance ;
107 }
108 
109 
110 
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Register object associated with given name and given associated parameters with given values in cache.
114 /// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
115 /// need to be the name of cacheObject and with given set of dependent parameters with validity for the
116 /// current values of those parameters. It can be retrieved later by callin retrieveObject()
117 
118 Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, const RooArgSet& params)
119 {
120  TIterator* parIter = params.createIterator() ;
121  Bool_t ret = registerObject(ownerName,objectName,cacheObject,parIter) ;
122  delete parIter ;
123 
124  return ret ;
125 }
126 
127 
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Register object associated with given name and given associated parameters with given values in cache.
131 /// The cache will take _ownership_of_object_ and is indexed under the given name (which does not
132 /// need to be the name of cacheObject and with given set of dependent parameters with validity for the
133 /// current values of those parameters. It can be retrieved later by callin retrieveObject()
134 
135 Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, TIterator* parIter)
136 {
137  // Delete any previous object
138  ExpensiveObject* eo = _map[objectName] ;
139  Int_t olduid(-1) ;
140  if (eo) {
141  olduid = eo->uid() ;
142  delete eo ;
143  }
144  // Install new object
145  _map[objectName] = new ExpensiveObject(olduid!=-1?olduid:_nextUID++, ownerName,cacheObject,parIter) ;
146 
147  return kFALSE ;
148 }
149 
150 
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Retrieve object from cache that was registered under given name with given parameters, _if_
154 /// current parameter values match those that were stored in the registry for this object.
155 /// The return object is owned by the cache instance.
156 
157 const TObject* RooExpensiveObjectCache::retrieveObject(const char* name, TClass* tc, const RooArgSet& params)
158 {
159  ExpensiveObject* eo = _map[name] ;
160 
161  // If no cache element found, return 0 ;
162  if (!eo) {
163  return 0 ;
164  }
165 
166  // If parameters also match, return payload ;
167  if (eo->matches(tc,params)) {
168  return eo->payload() ;
169  }
170 
171  return 0 ;
172 }
173 
174 
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Retrieve payload object of cache element with given unique ID
178 
180 {
181  for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; iter++) {
182  if (iter->second->uid() == uid) {
183  return iter->second->payload() ;
184  }
185  }
186  return 0 ;
187 }
188 
189 
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Clear cache element with given unique ID
193 /// Retrieve payload object of cache element with given unique ID
194 
196 {
197  for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; iter++) {
198  if (iter->second->uid() == uid) {
199  _map.erase(iter->first) ;
200  return kFALSE ;
201  }
202  }
203  return kTRUE ;
204 }
205 
206 
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Place new payload object in cache element with given unique ID. Cache
210 /// will take ownership of provided object!
211 
213 {
214  for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; iter++) {
215  if (iter->second->uid() == uid) {
216  iter->second->setPayload(obj) ;
217  return kFALSE ;
218  }
219  }
220  return kTRUE ;
221 }
222 
223 
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Clear all cache elements
227 
229 {
230  _map.clear() ;
231 }
232 
233 
234 
235 
236 
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// Construct ExpensiveObject oject for inPayLoad and store reference values
240 /// for all RooAbsReal and RooAbsCategory parameters in params.
241 
242 RooExpensiveObjectCache::ExpensiveObject::ExpensiveObject(Int_t uidIn, const char* inOwnerName, TObject& inPayload, TIterator* parIter)
243 {
244  _uid = uidIn ;
245  _ownerName = inOwnerName;
246 
247  _payload = &inPayload ;
248 
249  RooAbsArg* arg ;
250  parIter->Reset() ;
251  while((arg=(RooAbsArg*)parIter->Next() )) {
252  RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
253  if (real) {
254  _realRefParams[real->GetName()] = real->getVal() ;
255  } else {
256  RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
257  if (cat) {
258  _catRefParams[cat->GetName()] = cat->getIndex() ;
259  } else {
260  oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << endl ;
261  }
262  }
263  }
264 
265 }
266 
267 
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 
272  _uid(uidIn),
273  _realRefParams(other._realRefParams),
274  _catRefParams(other._catRefParams),
275  _ownerName(other._ownerName)
276 {
277  _payload = other._payload->Clone() ;
278 }
279 
280 
281 
282 ////////////////////////////////////////////////////////////////////////////////
283 
285 {
286  delete _payload ;
287 }
288 
289 
290 
291 
292 
293 ////////////////////////////////////////////////////////////////////////////////
294 /// Check object type ;
295 
297 {
298  if (_payload->IsA() != tc) {
299  return kFALSE;
300  }
301 
302  // Check parameters
303  TIterator* iter = params.createIterator() ;
304  RooAbsArg* arg ;
305  while((arg=(RooAbsArg*)iter->Next() )) {
306  RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
307  if (real) {
308  if (fabs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
309  delete iter ;
310  return kFALSE ;
311  }
312  } else {
313  RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
314  if (cat) {
315  if (cat->getIndex() != _catRefParams[cat->GetName()]) {
316  delete iter ;
317  return kFALSE ;
318  }
319  }
320  }
321  }
322  delete iter ;
323 
324  return kTRUE ;
325 
326 }
327 
328 
329 
330 ////////////////////////////////////////////////////////////////////////////////
331 
333 {
334  map<TString,ExpensiveObject*>::const_iterator iter = _map.begin() ;
335 
336  while(iter!=_map.end()) {
337  cout << "uid = " << iter->second->uid() << " key=" << iter->first << " value=" ;
338  iter->second->print() ;
339  ++iter ;
340  }
341 }
342 
343 
344 
345 ////////////////////////////////////////////////////////////////////////////////
346 
348 {
349  cout << _payload->IsA()->GetName() << "::" << _payload->GetName() ;
350  if (_realRefParams.size()>0 || _catRefParams.size()>0) {
351  cout << " parameters=( " ;
352  map<TString,Double_t>::iterator iter = _realRefParams.begin() ;
353  while(iter!=_realRefParams.end()) {
354  cout << iter->first << "=" << iter->second << " " ;
355  ++iter ;
356  }
357  map<TString,Int_t>::iterator iter2 = _catRefParams.begin() ;
358  while(iter2!=_catRefParams.end()) {
359  cout << iter2->first << "=" << iter2->second << " " ;
360  ++iter2 ;
361  }
362  cout << ")" ;
363  }
364  cout << endl ;
365 }
366 
367 
368 
369 
370 ////////////////////////////////////////////////////////////////////////////////
371 
373 {
374  map<TString,ExpensiveObject*>::const_iterator iter = other._map.begin() ;
375  while(iter!=other._map.end()) {
376  if (string(ownerName)==iter->second->ownerName()) {
377  _map[iter->first.Data()] = new ExpensiveObject(_nextUID++, *iter->second) ;
378  if (verbose) {
379  oocoutI(iter->second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
380  << iter->first << " associated with object " << iter->second->ownerName() << endl ;
381  }
382  }
383  ++iter ;
384  }
385 
386 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
void clearAll()
Clear all cache elements.
TIterator * createIterator(Bool_t dir=kIterForward) const
static RooExpensiveObjectCache * _instance
Bool_t clearObj(Int_t uniqueID)
Clear cache element with given unique ID Retrieve payload object of cache element with given unique I...
virtual void Reset()=0
static void cleanup()
Static function called by RooSentinel atexit() handler to cleanup at end of program.
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...
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
#define oocoutI(o, a)
Definition: RooMsgService.h:44
std::map< TString, ExpensiveObject * > _map
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Int_t getIndex() const
Return index number of current state.
STL namespace.
virtual ~RooExpensiveObjectCache()
Destructor.
const TObject * retrieveObject(const char *name, TClass *tclass, const RooArgSet &params)
Retrieve object from cache that was registered under given name with given parameters, if current parameter values match those that were stored in the registry for this object.
Iterator abstract base class.
Definition: TIterator.h:30
void importCacheObjects(RooExpensiveObjectCache &other, const char *ownerName, Bool_t verbose=kFALSE)
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
bool verbose
std::map< TString, Double_t > _realRefParams
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
Bool_t 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.
const Bool_t kFALSE
Definition: RtypesCore.h:92
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:71
#define ClassImp(name)
Definition: Rtypes.h:336
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
#define oocoutW(o, a)
Definition: RooMsgService.h:46
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:151
virtual TObject * Next()=0
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:364
const Bool_t kTRUE
Definition: RtypesCore.h:91
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...