ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// Class RooObjCacheManager is an implementation of class RooCacheManager<RooAbsCacheElement>
// and specializes in the storage of cache elements that contain RooAbsArg objects.
// Caches with RooAbsArg derived payload require special care as server redirects
// cache operation mode changes and constant term optimization calls may need to be
// forwarded to such cache payload. This cache manager takes are of all these operations
// by forwarding these calls to the RooAbsCacheElement interface functions, which
// have a sensible default implementation. 
// END_HTML
//

#include "RooFit.h"
#include "Riostream.h"
#include <vector>
#include "RooObjCacheManager.h"
#include "RooMsgService.h"

using namespace std ;

ClassImp(RooObjCacheManager)
   ;


Bool_t RooObjCacheManager::_clearObsList(kFALSE) ;

//_____________________________________________________________________________
RooObjCacheManager::RooObjCacheManager(RooAbsArg* owner, Int_t maxSize, Bool_t clearCacheOnServerRedirect, Bool_t allowOptimize) : 
  RooCacheManager<RooAbsCacheElement>(owner,maxSize), 
  _clearOnRedirect(clearCacheOnServerRedirect), 
  _allowOptimize(allowOptimize),
  _optCacheModeSeen(kFALSE),
  _optCacheObservables(0)
{
  // Constructor of object cache manager for given owner. If clearCacheOnServerRedirect is true
  // all cache elements will be cleared when a server redirect is intercepted by the cache manager.
  // This is the default strategy and should only be overridden when you really understand
  // what you're doing as properly implementing server redirect in cache elements can get very
  // complicated, especially if there are (cyclical) reference back to the owning object
}


//_____________________________________________________________________________
RooObjCacheManager::RooObjCacheManager(const RooObjCacheManager& other, RooAbsArg* owner) : 
  RooCacheManager<RooAbsCacheElement>(other,owner),
  _clearOnRedirect(other._clearOnRedirect),
  _allowOptimize(other._allowOptimize),
  _optCacheModeSeen(kFALSE), // cache mode properties are not transferred in copy ctor
  _optCacheObservables(0)
{
  // Copy constructor
}


//_____________________________________________________________________________
RooObjCacheManager::~RooObjCacheManager()
{
  // Destructor
  if (_optCacheObservables) {
    delete _optCacheObservables ;
  }
}


//_____________________________________________________________________________
Bool_t RooObjCacheManager::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t isRecursive) 
{ 
  // Intercept server redirect calls. If clearOnRedirect was set, sterilize
  // the cache (i.e. keep the structure but delete all contents). If not
  // forward serverRedirect to cache elements

  if (_clearOnRedirect) {

    sterilize() ;
    
  } else {

    for (Int_t i=0 ; i<cacheSize() ; i++) {
      if (_object[i]) {
	_object[i]->redirectServersHook(newServerList,mustReplaceAll,nameChange,isRecursive) ;
      }
    }

  }

  return kFALSE ; 
} 



//_____________________________________________________________________________
void RooObjCacheManager::operModeHook() 
{
  // Intercept changes to cache operation mode and forward to cache elements

  if (!_owner) {
    return ;
  }

  for (Int_t i=0 ; i<cacheSize() ; i++) {
    if (_object[i]) {
      _object[i]->operModeHook(_owner->operMode()) ;
    }
  }
} 



//_____________________________________________________________________________
void RooObjCacheManager::optimizeCacheMode(const RooArgSet& obs, RooArgSet& optNodes, RooLinkedList& processedNodes) 
{
  // Intercept calls to perform automatic optimization of cache mode operation. 
  // Forward calls to existing cache elements and save configuration of
  // cache mode optimization so that it can be applied on new cache elements
  // upon insertion 

  oocxcoutD(_owner,Caching) << "RooObjCacheManager::optimizeCacheMode(owner=" << _owner->GetName() << ") obs = " << obs << endl ;

  _optCacheModeSeen = kTRUE ;

  if (_optCacheObservables) {
    _optCacheObservables->removeAll() ;
    _optCacheObservables->add(obs) ;
  } else {
    _optCacheObservables = (RooArgSet*) new RooArgSet(obs) ;
  }
  
  for (Int_t i=0 ; i<cacheSize() ; i++) {
    if (_object[i]) {
      _object[i]->optimizeCacheMode(obs,optNodes,processedNodes) ;
    }
  }
}


//_____________________________________________________________________________
void RooObjCacheManager::sterilize() 
{
  RooCacheManager<RooAbsCacheElement>::sterilize() ;

  // WVE - adding this causes trouble with IntegralMorpht????
  // Perhaps this should not be done in sterilize, but be a separate operation
  // called specially from RooAbsObsTestStatistic::setData()

  if (_optCacheObservables && _clearObsList) {
    delete _optCacheObservables ;
    _optCacheObservables = 0 ;
    _optCacheModeSeen = kFALSE ;
  }
  
}



//_____________________________________________________________________________
void RooObjCacheManager::insertObjectHook(RooAbsCacheElement& obj) 
{
  // Set owner link on all object inserted into cache.
  // Also if cache mode optimization was requested, apply
  // it now to cache element being inserted

  obj.setOwner(_owner) ;

  // If value caching mode optimization has happened, process it now on object being inserted
  if (_optCacheModeSeen) {
    RooLinkedList l ;
    RooArgSet s ;
    obj.optimizeCacheMode(*_optCacheObservables,s,l) ;
  }

}




//_____________________________________________________________________________
void RooObjCacheManager::printCompactTreeHook(std::ostream& os, const char *indent)
{
  // Add details on cache contents when printing in tree mode

  for (Int_t i=0 ; i<cacheSize() ; i++) {
    if (_object[i]) {
      _object[i]->printCompactTreeHook(os,indent,i,cacheSize()-1) ;
    }
  }  
}



//_____________________________________________________________________________
void RooObjCacheManager::findConstantNodes(const RooArgSet& obs, RooArgSet& cacheList, RooLinkedList& processedNodes) 
{
  // If clearOnRedirect is false, forward constant term optimization calls to
  // cache elements

  if (!_allowOptimize) {
    return ;
  }
  
  for (Int_t i=0 ; i<cacheSize() ; i++) {
    if (_object[i]) {
      _object[i]->findConstantNodes(obs,cacheList, processedNodes) ;
    }
  }
}


 RooObjCacheManager.cxx:1
 RooObjCacheManager.cxx:2
 RooObjCacheManager.cxx:3
 RooObjCacheManager.cxx:4
 RooObjCacheManager.cxx:5
 RooObjCacheManager.cxx:6
 RooObjCacheManager.cxx:7
 RooObjCacheManager.cxx:8
 RooObjCacheManager.cxx:9
 RooObjCacheManager.cxx:10
 RooObjCacheManager.cxx:11
 RooObjCacheManager.cxx:12
 RooObjCacheManager.cxx:13
 RooObjCacheManager.cxx:14
 RooObjCacheManager.cxx:15
 RooObjCacheManager.cxx:16
 RooObjCacheManager.cxx:17
 RooObjCacheManager.cxx:18
 RooObjCacheManager.cxx:19
 RooObjCacheManager.cxx:20
 RooObjCacheManager.cxx:21
 RooObjCacheManager.cxx:22
 RooObjCacheManager.cxx:23
 RooObjCacheManager.cxx:24
 RooObjCacheManager.cxx:25
 RooObjCacheManager.cxx:26
 RooObjCacheManager.cxx:27
 RooObjCacheManager.cxx:28
 RooObjCacheManager.cxx:29
 RooObjCacheManager.cxx:30
 RooObjCacheManager.cxx:31
 RooObjCacheManager.cxx:32
 RooObjCacheManager.cxx:33
 RooObjCacheManager.cxx:34
 RooObjCacheManager.cxx:35
 RooObjCacheManager.cxx:36
 RooObjCacheManager.cxx:37
 RooObjCacheManager.cxx:38
 RooObjCacheManager.cxx:39
 RooObjCacheManager.cxx:40
 RooObjCacheManager.cxx:41
 RooObjCacheManager.cxx:42
 RooObjCacheManager.cxx:43
 RooObjCacheManager.cxx:44
 RooObjCacheManager.cxx:45
 RooObjCacheManager.cxx:46
 RooObjCacheManager.cxx:47
 RooObjCacheManager.cxx:48
 RooObjCacheManager.cxx:49
 RooObjCacheManager.cxx:50
 RooObjCacheManager.cxx:51
 RooObjCacheManager.cxx:52
 RooObjCacheManager.cxx:53
 RooObjCacheManager.cxx:54
 RooObjCacheManager.cxx:55
 RooObjCacheManager.cxx:56
 RooObjCacheManager.cxx:57
 RooObjCacheManager.cxx:58
 RooObjCacheManager.cxx:59
 RooObjCacheManager.cxx:60
 RooObjCacheManager.cxx:61
 RooObjCacheManager.cxx:62
 RooObjCacheManager.cxx:63
 RooObjCacheManager.cxx:64
 RooObjCacheManager.cxx:65
 RooObjCacheManager.cxx:66
 RooObjCacheManager.cxx:67
 RooObjCacheManager.cxx:68
 RooObjCacheManager.cxx:69
 RooObjCacheManager.cxx:70
 RooObjCacheManager.cxx:71
 RooObjCacheManager.cxx:72
 RooObjCacheManager.cxx:73
 RooObjCacheManager.cxx:74
 RooObjCacheManager.cxx:75
 RooObjCacheManager.cxx:76
 RooObjCacheManager.cxx:77
 RooObjCacheManager.cxx:78
 RooObjCacheManager.cxx:79
 RooObjCacheManager.cxx:80
 RooObjCacheManager.cxx:81
 RooObjCacheManager.cxx:82
 RooObjCacheManager.cxx:83
 RooObjCacheManager.cxx:84
 RooObjCacheManager.cxx:85
 RooObjCacheManager.cxx:86
 RooObjCacheManager.cxx:87
 RooObjCacheManager.cxx:88
 RooObjCacheManager.cxx:89
 RooObjCacheManager.cxx:90
 RooObjCacheManager.cxx:91
 RooObjCacheManager.cxx:92
 RooObjCacheManager.cxx:93
 RooObjCacheManager.cxx:94
 RooObjCacheManager.cxx:95
 RooObjCacheManager.cxx:96
 RooObjCacheManager.cxx:97
 RooObjCacheManager.cxx:98
 RooObjCacheManager.cxx:99
 RooObjCacheManager.cxx:100
 RooObjCacheManager.cxx:101
 RooObjCacheManager.cxx:102
 RooObjCacheManager.cxx:103
 RooObjCacheManager.cxx:104
 RooObjCacheManager.cxx:105
 RooObjCacheManager.cxx:106
 RooObjCacheManager.cxx:107
 RooObjCacheManager.cxx:108
 RooObjCacheManager.cxx:109
 RooObjCacheManager.cxx:110
 RooObjCacheManager.cxx:111
 RooObjCacheManager.cxx:112
 RooObjCacheManager.cxx:113
 RooObjCacheManager.cxx:114
 RooObjCacheManager.cxx:115
 RooObjCacheManager.cxx:116
 RooObjCacheManager.cxx:117
 RooObjCacheManager.cxx:118
 RooObjCacheManager.cxx:119
 RooObjCacheManager.cxx:120
 RooObjCacheManager.cxx:121
 RooObjCacheManager.cxx:122
 RooObjCacheManager.cxx:123
 RooObjCacheManager.cxx:124
 RooObjCacheManager.cxx:125
 RooObjCacheManager.cxx:126
 RooObjCacheManager.cxx:127
 RooObjCacheManager.cxx:128
 RooObjCacheManager.cxx:129
 RooObjCacheManager.cxx:130
 RooObjCacheManager.cxx:131
 RooObjCacheManager.cxx:132
 RooObjCacheManager.cxx:133
 RooObjCacheManager.cxx:134
 RooObjCacheManager.cxx:135
 RooObjCacheManager.cxx:136
 RooObjCacheManager.cxx:137
 RooObjCacheManager.cxx:138
 RooObjCacheManager.cxx:139
 RooObjCacheManager.cxx:140
 RooObjCacheManager.cxx:141
 RooObjCacheManager.cxx:142
 RooObjCacheManager.cxx:143
 RooObjCacheManager.cxx:144
 RooObjCacheManager.cxx:145
 RooObjCacheManager.cxx:146
 RooObjCacheManager.cxx:147
 RooObjCacheManager.cxx:148
 RooObjCacheManager.cxx:149
 RooObjCacheManager.cxx:150
 RooObjCacheManager.cxx:151
 RooObjCacheManager.cxx:152
 RooObjCacheManager.cxx:153
 RooObjCacheManager.cxx:154
 RooObjCacheManager.cxx:155
 RooObjCacheManager.cxx:156
 RooObjCacheManager.cxx:157
 RooObjCacheManager.cxx:158
 RooObjCacheManager.cxx:159
 RooObjCacheManager.cxx:160
 RooObjCacheManager.cxx:161
 RooObjCacheManager.cxx:162
 RooObjCacheManager.cxx:163
 RooObjCacheManager.cxx:164
 RooObjCacheManager.cxx:165
 RooObjCacheManager.cxx:166
 RooObjCacheManager.cxx:167
 RooObjCacheManager.cxx:168
 RooObjCacheManager.cxx:169
 RooObjCacheManager.cxx:170
 RooObjCacheManager.cxx:171
 RooObjCacheManager.cxx:172
 RooObjCacheManager.cxx:173
 RooObjCacheManager.cxx:174
 RooObjCacheManager.cxx:175
 RooObjCacheManager.cxx:176
 RooObjCacheManager.cxx:177
 RooObjCacheManager.cxx:178
 RooObjCacheManager.cxx:179
 RooObjCacheManager.cxx:180
 RooObjCacheManager.cxx:181
 RooObjCacheManager.cxx:182
 RooObjCacheManager.cxx:183
 RooObjCacheManager.cxx:184
 RooObjCacheManager.cxx:185
 RooObjCacheManager.cxx:186
 RooObjCacheManager.cxx:187
 RooObjCacheManager.cxx:188
 RooObjCacheManager.cxx:189
 RooObjCacheManager.cxx:190
 RooObjCacheManager.cxx:191
 RooObjCacheManager.cxx:192
 RooObjCacheManager.cxx:193
 RooObjCacheManager.cxx:194
 RooObjCacheManager.cxx:195
 RooObjCacheManager.cxx:196
 RooObjCacheManager.cxx:197
 RooObjCacheManager.cxx:198
 RooObjCacheManager.cxx:199
 RooObjCacheManager.cxx:200
 RooObjCacheManager.cxx:201
 RooObjCacheManager.cxx:202
 RooObjCacheManager.cxx:203
 RooObjCacheManager.cxx:204
 RooObjCacheManager.cxx:205
 RooObjCacheManager.cxx:206
 RooObjCacheManager.cxx:207
 RooObjCacheManager.cxx:208
 RooObjCacheManager.cxx:209
 RooObjCacheManager.cxx:210
 RooObjCacheManager.cxx:211
 RooObjCacheManager.cxx:212
 RooObjCacheManager.cxx:213
 RooObjCacheManager.cxx:214
 RooObjCacheManager.cxx:215
 RooObjCacheManager.cxx:216
 RooObjCacheManager.cxx:217
 RooObjCacheManager.cxx:218
 RooObjCacheManager.cxx:219
 RooObjCacheManager.cxx:220
 RooObjCacheManager.cxx:221
 RooObjCacheManager.cxx:222
 RooObjCacheManager.cxx:223
 RooObjCacheManager.cxx:224