// RooExpensiveObjectCache is a singleton class that serves as repository
// for objects that are expensive to calculate. Owners of such objects
// can registers these here with associated parameter values for which
// the object is valid, so that other instances can, at a later moment
// retrieve these precalculated objects
// END_HTML
#include "TClass.h"
#include "RooFit.h"
#include "RooSentinel.h"
#include "RooAbsReal.h"
#include "RooAbsCategory.h"
#include "RooArgSet.h"
#include "RooMsgService.h"
#include <iostream>
#include <math.h>
using namespace std ;
#include "RooExpensiveObjectCache.h"
ClassImp(RooExpensiveObjectCache)
ClassImp(RooExpensiveObjectCache::ExpensiveObject)
;
RooExpensiveObjectCache* RooExpensiveObjectCache::_instance = 0 ;
RooExpensiveObjectCache::RooExpensiveObjectCache() : _nextUID(0)
{
}
RooExpensiveObjectCache::RooExpensiveObjectCache(const RooExpensiveObjectCache& other) :
TObject(other), _nextUID(0)
{
}
RooExpensiveObjectCache::~RooExpensiveObjectCache()
{
for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter!=_map.end() ; ++iter) {
delete iter->second ;
}
if (_instance == this) {
_instance = 0 ;
}
}
RooExpensiveObjectCache& RooExpensiveObjectCache::instance()
{
if (!_instance) {
_instance = new RooExpensiveObjectCache() ;
RooSentinel::activate() ;
}
return *_instance ;
}
void RooExpensiveObjectCache::cleanup()
{
delete _instance ;
}
Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, const RooArgSet& params)
{
TIterator* parIter = params.createIterator() ;
Bool_t ret = registerObject(ownerName,objectName,cacheObject,parIter) ;
delete parIter ;
return ret ;
}
Bool_t RooExpensiveObjectCache::registerObject(const char* ownerName, const char* objectName, TObject& cacheObject, TIterator* parIter)
{
ExpensiveObject* eo = _map[objectName] ;
Int_t olduid(-1) ;
if (eo) {
olduid = eo->uid() ;
delete eo ;
}
_map[objectName] = new ExpensiveObject(olduid!=-1?olduid:_nextUID++, ownerName,cacheObject,parIter) ;
return kFALSE ;
}
const TObject* RooExpensiveObjectCache::retrieveObject(const char* name, TClass* tc, const RooArgSet& params)
{
ExpensiveObject* eo = _map[name] ;
if (!eo) {
return 0 ;
}
if (eo->matches(tc,params)) {
return eo->payload() ;
}
return 0 ;
}
const TObject* RooExpensiveObjectCache::getObj(Int_t uid)
{
for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; iter++) {
if (iter->second->uid() == uid) {
return iter->second->payload() ;
}
}
return 0 ;
}
Bool_t RooExpensiveObjectCache::clearObj(Int_t uid)
{
for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; iter++) {
if (iter->second->uid() == uid) {
_map.erase(iter->first) ;
return kFALSE ;
}
}
return kTRUE ;
}
Bool_t RooExpensiveObjectCache::setObj(Int_t uid, TObject* obj)
{
for (std::map<TString,ExpensiveObject*>::iterator iter = _map.begin() ; iter !=_map.end() ; iter++) {
if (iter->second->uid() == uid) {
iter->second->setPayload(obj) ;
return kFALSE ;
}
}
return kTRUE ;
}
void RooExpensiveObjectCache::clearAll()
{
_map.clear() ;
}
RooExpensiveObjectCache::ExpensiveObject::ExpensiveObject(Int_t uidIn, const char* inOwnerName, TObject& inPayload, TIterator* parIter)
{
_uid = uidIn ;
_ownerName = inOwnerName;
_payload = &inPayload ;
RooAbsArg* arg ;
parIter->Reset() ;
while((arg=(RooAbsArg*)parIter->Next() )) {
RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
if (real) {
_realRefParams[real->GetName()] = real->getVal() ;
} else {
RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
if (cat) {
_catRefParams[cat->GetName()] = cat->getIndex() ;
} else {
oocoutW(&inPayload,Caching) << "RooExpensiveObject::registerObject() WARNING: ignoring non-RooAbsReal/non-RooAbsCategory reference parameter " << arg->GetName() << endl ;
}
}
}
}
RooExpensiveObjectCache::ExpensiveObject::ExpensiveObject(Int_t uidIn, const ExpensiveObject& other) :
_uid(uidIn),
_realRefParams(other._realRefParams),
_catRefParams(other._catRefParams),
_ownerName(other._ownerName)
{
_payload = other._payload->Clone() ;
}
RooExpensiveObjectCache::ExpensiveObject::~ExpensiveObject()
{
delete _payload ;
}
Bool_t RooExpensiveObjectCache::ExpensiveObject::matches(TClass* tc, const RooArgSet& params)
{
if (_payload->IsA() != tc) {
return kFALSE;
}
TIterator* iter = params.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next() )) {
RooAbsReal* real = dynamic_cast<RooAbsReal*>(arg) ;
if (real) {
if (fabs(real->getVal()-_realRefParams[real->GetName()])>1e-12) {
delete iter ;
return kFALSE ;
}
} else {
RooAbsCategory* cat = dynamic_cast<RooAbsCategory*>(arg) ;
if (cat) {
if (cat->getIndex() != _catRefParams[cat->GetName()]) {
delete iter ;
return kFALSE ;
}
}
}
}
delete iter ;
return kTRUE ;
}
void RooExpensiveObjectCache::print() const
{
map<TString,ExpensiveObject*>::const_iterator iter = _map.begin() ;
while(iter!=_map.end()) {
cout << "uid = " << iter->second->uid() << " key=" << iter->first << " value=" ;
iter->second->print() ;
++iter ;
}
}
void RooExpensiveObjectCache::ExpensiveObject::print()
{
cout << _payload->IsA()->GetName() << "::" << _payload->GetName() ;
if (_realRefParams.size()>0 || _catRefParams.size()>0) {
cout << " parameters=( " ;
map<TString,Double_t>::iterator iter = _realRefParams.begin() ;
while(iter!=_realRefParams.end()) {
cout << iter->first << "=" << iter->second << " " ;
++iter ;
}
map<TString,Int_t>::iterator iter2 = _catRefParams.begin() ;
while(iter2!=_catRefParams.end()) {
cout << iter2->first << "=" << iter2->second << " " ;
++iter2 ;
}
cout << ")" ;
}
cout << endl ;
}
void RooExpensiveObjectCache::importCacheObjects(RooExpensiveObjectCache& other, const char* ownerName, Bool_t verbose)
{
map<TString,ExpensiveObject*>::const_iterator iter = other._map.begin() ;
while(iter!=other._map.end()) {
if (string(ownerName)==iter->second->ownerName()) {
_map[iter->first.Data()] = new ExpensiveObject(_nextUID++, *iter->second) ;
if (verbose) {
oocoutI(iter->second->payload(),Caching) << "RooExpensiveObjectCache::importCache() importing cache object "
<< iter->first << " associated with object " << iter->second->ownerName() << endl ;
}
}
++iter ;
}
}
RooExpensiveObjectCache.cxx:1 RooExpensiveObjectCache.cxx:2 RooExpensiveObjectCache.cxx:3 RooExpensiveObjectCache.cxx:4 RooExpensiveObjectCache.cxx:5 RooExpensiveObjectCache.cxx:6 RooExpensiveObjectCache.cxx:7 RooExpensiveObjectCache.cxx:8 RooExpensiveObjectCache.cxx:9 RooExpensiveObjectCache.cxx:10 RooExpensiveObjectCache.cxx:11 RooExpensiveObjectCache.cxx:12 RooExpensiveObjectCache.cxx:13 RooExpensiveObjectCache.cxx:14 RooExpensiveObjectCache.cxx:15 RooExpensiveObjectCache.cxx:16 RooExpensiveObjectCache.cxx:17 RooExpensiveObjectCache.cxx:18 RooExpensiveObjectCache.cxx:19 RooExpensiveObjectCache.cxx:20 RooExpensiveObjectCache.cxx:21 RooExpensiveObjectCache.cxx:22 RooExpensiveObjectCache.cxx:23 RooExpensiveObjectCache.cxx:24 RooExpensiveObjectCache.cxx:25 RooExpensiveObjectCache.cxx:26 RooExpensiveObjectCache.cxx:27 RooExpensiveObjectCache.cxx:28 RooExpensiveObjectCache.cxx:29 RooExpensiveObjectCache.cxx:30 RooExpensiveObjectCache.cxx:31 RooExpensiveObjectCache.cxx:32 RooExpensiveObjectCache.cxx:33 RooExpensiveObjectCache.cxx:34 RooExpensiveObjectCache.cxx:35 RooExpensiveObjectCache.cxx:36 RooExpensiveObjectCache.cxx:37 RooExpensiveObjectCache.cxx:38 RooExpensiveObjectCache.cxx:39 RooExpensiveObjectCache.cxx:40 RooExpensiveObjectCache.cxx:41 RooExpensiveObjectCache.cxx:42 RooExpensiveObjectCache.cxx:43 RooExpensiveObjectCache.cxx:44 RooExpensiveObjectCache.cxx:45 RooExpensiveObjectCache.cxx:46 RooExpensiveObjectCache.cxx:47 RooExpensiveObjectCache.cxx:48 RooExpensiveObjectCache.cxx:49 RooExpensiveObjectCache.cxx:50 RooExpensiveObjectCache.cxx:51 RooExpensiveObjectCache.cxx:52 RooExpensiveObjectCache.cxx:53 RooExpensiveObjectCache.cxx:54 RooExpensiveObjectCache.cxx:55 RooExpensiveObjectCache.cxx:56 RooExpensiveObjectCache.cxx:57 RooExpensiveObjectCache.cxx:58 RooExpensiveObjectCache.cxx:59 RooExpensiveObjectCache.cxx:60 RooExpensiveObjectCache.cxx:61 RooExpensiveObjectCache.cxx:62 RooExpensiveObjectCache.cxx:63 RooExpensiveObjectCache.cxx:64 RooExpensiveObjectCache.cxx:65 RooExpensiveObjectCache.cxx:66 RooExpensiveObjectCache.cxx:67 RooExpensiveObjectCache.cxx:68 RooExpensiveObjectCache.cxx:69 RooExpensiveObjectCache.cxx:70 RooExpensiveObjectCache.cxx:71 RooExpensiveObjectCache.cxx:72 RooExpensiveObjectCache.cxx:73 RooExpensiveObjectCache.cxx:74 RooExpensiveObjectCache.cxx:75 RooExpensiveObjectCache.cxx:76 RooExpensiveObjectCache.cxx:77 RooExpensiveObjectCache.cxx:78 RooExpensiveObjectCache.cxx:79 RooExpensiveObjectCache.cxx:80 RooExpensiveObjectCache.cxx:81 RooExpensiveObjectCache.cxx:82 RooExpensiveObjectCache.cxx:83 RooExpensiveObjectCache.cxx:84 RooExpensiveObjectCache.cxx:85 RooExpensiveObjectCache.cxx:86 RooExpensiveObjectCache.cxx:87 RooExpensiveObjectCache.cxx:88 RooExpensiveObjectCache.cxx:89 RooExpensiveObjectCache.cxx:90 RooExpensiveObjectCache.cxx:91 RooExpensiveObjectCache.cxx:92 RooExpensiveObjectCache.cxx:93 RooExpensiveObjectCache.cxx:94 RooExpensiveObjectCache.cxx:95 RooExpensiveObjectCache.cxx:96 RooExpensiveObjectCache.cxx:97 RooExpensiveObjectCache.cxx:98 RooExpensiveObjectCache.cxx:99 RooExpensiveObjectCache.cxx:100 RooExpensiveObjectCache.cxx:101 RooExpensiveObjectCache.cxx:102 RooExpensiveObjectCache.cxx:103 RooExpensiveObjectCache.cxx:104 RooExpensiveObjectCache.cxx:105 RooExpensiveObjectCache.cxx:106 RooExpensiveObjectCache.cxx:107 RooExpensiveObjectCache.cxx:108 RooExpensiveObjectCache.cxx:109 RooExpensiveObjectCache.cxx:110 RooExpensiveObjectCache.cxx:111 RooExpensiveObjectCache.cxx:112 RooExpensiveObjectCache.cxx:113 RooExpensiveObjectCache.cxx:114 RooExpensiveObjectCache.cxx:115 RooExpensiveObjectCache.cxx:116 RooExpensiveObjectCache.cxx:117 RooExpensiveObjectCache.cxx:118 RooExpensiveObjectCache.cxx:119 RooExpensiveObjectCache.cxx:120 RooExpensiveObjectCache.cxx:121 RooExpensiveObjectCache.cxx:122 RooExpensiveObjectCache.cxx:123 RooExpensiveObjectCache.cxx:124 RooExpensiveObjectCache.cxx:125 RooExpensiveObjectCache.cxx:126 RooExpensiveObjectCache.cxx:127 RooExpensiveObjectCache.cxx:128 RooExpensiveObjectCache.cxx:129 RooExpensiveObjectCache.cxx:130 RooExpensiveObjectCache.cxx:131 RooExpensiveObjectCache.cxx:132 RooExpensiveObjectCache.cxx:133 RooExpensiveObjectCache.cxx:134 RooExpensiveObjectCache.cxx:135 RooExpensiveObjectCache.cxx:136 RooExpensiveObjectCache.cxx:137 RooExpensiveObjectCache.cxx:138 RooExpensiveObjectCache.cxx:139 RooExpensiveObjectCache.cxx:140 RooExpensiveObjectCache.cxx:141 RooExpensiveObjectCache.cxx:142 RooExpensiveObjectCache.cxx:143 RooExpensiveObjectCache.cxx:144 RooExpensiveObjectCache.cxx:145 RooExpensiveObjectCache.cxx:146 RooExpensiveObjectCache.cxx:147 RooExpensiveObjectCache.cxx:148 RooExpensiveObjectCache.cxx:149 RooExpensiveObjectCache.cxx:150 RooExpensiveObjectCache.cxx:151 RooExpensiveObjectCache.cxx:152 RooExpensiveObjectCache.cxx:153 RooExpensiveObjectCache.cxx:154 RooExpensiveObjectCache.cxx:155 RooExpensiveObjectCache.cxx:156 RooExpensiveObjectCache.cxx:157 RooExpensiveObjectCache.cxx:158 RooExpensiveObjectCache.cxx:159 RooExpensiveObjectCache.cxx:160 RooExpensiveObjectCache.cxx:161 RooExpensiveObjectCache.cxx:162 RooExpensiveObjectCache.cxx:163 RooExpensiveObjectCache.cxx:164 RooExpensiveObjectCache.cxx:165 RooExpensiveObjectCache.cxx:166 RooExpensiveObjectCache.cxx:167 RooExpensiveObjectCache.cxx:168 RooExpensiveObjectCache.cxx:169 RooExpensiveObjectCache.cxx:170 RooExpensiveObjectCache.cxx:171 RooExpensiveObjectCache.cxx:172 RooExpensiveObjectCache.cxx:173 RooExpensiveObjectCache.cxx:174 RooExpensiveObjectCache.cxx:175 RooExpensiveObjectCache.cxx:176 RooExpensiveObjectCache.cxx:177 RooExpensiveObjectCache.cxx:178 RooExpensiveObjectCache.cxx:179 RooExpensiveObjectCache.cxx:180 RooExpensiveObjectCache.cxx:181 RooExpensiveObjectCache.cxx:182 RooExpensiveObjectCache.cxx:183 RooExpensiveObjectCache.cxx:184 RooExpensiveObjectCache.cxx:185 RooExpensiveObjectCache.cxx:186 RooExpensiveObjectCache.cxx:187 RooExpensiveObjectCache.cxx:188 RooExpensiveObjectCache.cxx:189 RooExpensiveObjectCache.cxx:190 RooExpensiveObjectCache.cxx:191 RooExpensiveObjectCache.cxx:192 RooExpensiveObjectCache.cxx:193 RooExpensiveObjectCache.cxx:194 RooExpensiveObjectCache.cxx:195 RooExpensiveObjectCache.cxx:196 RooExpensiveObjectCache.cxx:197 RooExpensiveObjectCache.cxx:198 RooExpensiveObjectCache.cxx:199 RooExpensiveObjectCache.cxx:200 RooExpensiveObjectCache.cxx:201 RooExpensiveObjectCache.cxx:202 RooExpensiveObjectCache.cxx:203 RooExpensiveObjectCache.cxx:204 RooExpensiveObjectCache.cxx:205 RooExpensiveObjectCache.cxx:206 RooExpensiveObjectCache.cxx:207 RooExpensiveObjectCache.cxx:208 RooExpensiveObjectCache.cxx:209 RooExpensiveObjectCache.cxx:210 RooExpensiveObjectCache.cxx:211 RooExpensiveObjectCache.cxx:212 RooExpensiveObjectCache.cxx:213 RooExpensiveObjectCache.cxx:214 RooExpensiveObjectCache.cxx:215 RooExpensiveObjectCache.cxx:216 RooExpensiveObjectCache.cxx:217 RooExpensiveObjectCache.cxx:218 RooExpensiveObjectCache.cxx:219 RooExpensiveObjectCache.cxx:220 RooExpensiveObjectCache.cxx:221 RooExpensiveObjectCache.cxx:222 RooExpensiveObjectCache.cxx:223 RooExpensiveObjectCache.cxx:224 RooExpensiveObjectCache.cxx:225 RooExpensiveObjectCache.cxx:226 RooExpensiveObjectCache.cxx:227 RooExpensiveObjectCache.cxx:228 RooExpensiveObjectCache.cxx:229 RooExpensiveObjectCache.cxx:230 RooExpensiveObjectCache.cxx:231 RooExpensiveObjectCache.cxx:232 RooExpensiveObjectCache.cxx:233 RooExpensiveObjectCache.cxx:234 RooExpensiveObjectCache.cxx:235 RooExpensiveObjectCache.cxx:236 RooExpensiveObjectCache.cxx:237 RooExpensiveObjectCache.cxx:238 RooExpensiveObjectCache.cxx:239 RooExpensiveObjectCache.cxx:240 RooExpensiveObjectCache.cxx:241 RooExpensiveObjectCache.cxx:242 RooExpensiveObjectCache.cxx:243 RooExpensiveObjectCache.cxx:244 RooExpensiveObjectCache.cxx:245 RooExpensiveObjectCache.cxx:246 RooExpensiveObjectCache.cxx:247 RooExpensiveObjectCache.cxx:248 RooExpensiveObjectCache.cxx:249 RooExpensiveObjectCache.cxx:250 RooExpensiveObjectCache.cxx:251 RooExpensiveObjectCache.cxx:252 RooExpensiveObjectCache.cxx:253 RooExpensiveObjectCache.cxx:254 RooExpensiveObjectCache.cxx:255 RooExpensiveObjectCache.cxx:256 RooExpensiveObjectCache.cxx:257 RooExpensiveObjectCache.cxx:258 RooExpensiveObjectCache.cxx:259 RooExpensiveObjectCache.cxx:260 RooExpensiveObjectCache.cxx:261 RooExpensiveObjectCache.cxx:262 RooExpensiveObjectCache.cxx:263 RooExpensiveObjectCache.cxx:264 RooExpensiveObjectCache.cxx:265 RooExpensiveObjectCache.cxx:266 RooExpensiveObjectCache.cxx:267 RooExpensiveObjectCache.cxx:268 RooExpensiveObjectCache.cxx:269 RooExpensiveObjectCache.cxx:270 RooExpensiveObjectCache.cxx:271 RooExpensiveObjectCache.cxx:272 RooExpensiveObjectCache.cxx:273 RooExpensiveObjectCache.cxx:274 RooExpensiveObjectCache.cxx:275 RooExpensiveObjectCache.cxx:276 RooExpensiveObjectCache.cxx:277 RooExpensiveObjectCache.cxx:278 RooExpensiveObjectCache.cxx:279 RooExpensiveObjectCache.cxx:280 RooExpensiveObjectCache.cxx:281 RooExpensiveObjectCache.cxx:282 RooExpensiveObjectCache.cxx:283 RooExpensiveObjectCache.cxx:284 RooExpensiveObjectCache.cxx:285 RooExpensiveObjectCache.cxx:286 RooExpensiveObjectCache.cxx:287 RooExpensiveObjectCache.cxx:288 RooExpensiveObjectCache.cxx:289 RooExpensiveObjectCache.cxx:290 RooExpensiveObjectCache.cxx:291 RooExpensiveObjectCache.cxx:292 RooExpensiveObjectCache.cxx:293 RooExpensiveObjectCache.cxx:294 RooExpensiveObjectCache.cxx:295 RooExpensiveObjectCache.cxx:296 RooExpensiveObjectCache.cxx:297 RooExpensiveObjectCache.cxx:298 RooExpensiveObjectCache.cxx:299 RooExpensiveObjectCache.cxx:300 RooExpensiveObjectCache.cxx:301 RooExpensiveObjectCache.cxx:302 RooExpensiveObjectCache.cxx:303 RooExpensiveObjectCache.cxx:304 RooExpensiveObjectCache.cxx:305 RooExpensiveObjectCache.cxx:306 RooExpensiveObjectCache.cxx:307 RooExpensiveObjectCache.cxx:308 RooExpensiveObjectCache.cxx:309 RooExpensiveObjectCache.cxx:310 RooExpensiveObjectCache.cxx:311 RooExpensiveObjectCache.cxx:312 RooExpensiveObjectCache.cxx:313 RooExpensiveObjectCache.cxx:314 RooExpensiveObjectCache.cxx:315 RooExpensiveObjectCache.cxx:316 RooExpensiveObjectCache.cxx:317 RooExpensiveObjectCache.cxx:318 RooExpensiveObjectCache.cxx:319 RooExpensiveObjectCache.cxx:320 RooExpensiveObjectCache.cxx:321 RooExpensiveObjectCache.cxx:322 RooExpensiveObjectCache.cxx:323 RooExpensiveObjectCache.cxx:324 RooExpensiveObjectCache.cxx:325 RooExpensiveObjectCache.cxx:326 RooExpensiveObjectCache.cxx:327 RooExpensiveObjectCache.cxx:328 RooExpensiveObjectCache.cxx:329 RooExpensiveObjectCache.cxx:330 RooExpensiveObjectCache.cxx:331 RooExpensiveObjectCache.cxx:332 RooExpensiveObjectCache.cxx:333 RooExpensiveObjectCache.cxx:334 RooExpensiveObjectCache.cxx:335 RooExpensiveObjectCache.cxx:336 RooExpensiveObjectCache.cxx:337 RooExpensiveObjectCache.cxx:338 RooExpensiveObjectCache.cxx:339 RooExpensiveObjectCache.cxx:340 RooExpensiveObjectCache.cxx:341 RooExpensiveObjectCache.cxx:342 RooExpensiveObjectCache.cxx:343 RooExpensiveObjectCache.cxx:344 RooExpensiveObjectCache.cxx:345 RooExpensiveObjectCache.cxx:346 RooExpensiveObjectCache.cxx:347 RooExpensiveObjectCache.cxx:348 RooExpensiveObjectCache.cxx:349 RooExpensiveObjectCache.cxx:350 RooExpensiveObjectCache.cxx:351 RooExpensiveObjectCache.cxx:352 RooExpensiveObjectCache.cxx:353 RooExpensiveObjectCache.cxx:354 RooExpensiveObjectCache.cxx:355 RooExpensiveObjectCache.cxx:356 RooExpensiveObjectCache.cxx:357 RooExpensiveObjectCache.cxx:358 RooExpensiveObjectCache.cxx:359 RooExpensiveObjectCache.cxx:360 RooExpensiveObjectCache.cxx:361 RooExpensiveObjectCache.cxx:362 RooExpensiveObjectCache.cxx:363 RooExpensiveObjectCache.cxx:364 RooExpensiveObjectCache.cxx:365 RooExpensiveObjectCache.cxx:366 RooExpensiveObjectCache.cxx:367 RooExpensiveObjectCache.cxx:368 RooExpensiveObjectCache.cxx:369 RooExpensiveObjectCache.cxx:370 RooExpensiveObjectCache.cxx:371 RooExpensiveObjectCache.cxx:372 RooExpensiveObjectCache.cxx:373 RooExpensiveObjectCache.cxx:374