RooCacheManager.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  *    File: $Id: RooCacheManager.h 35300 2010-09-15 12:58:04Z pcanal $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 #ifndef ROO_CACHE_MANAGER
00017 #define ROO_CACHE_MANAGER
00018 
00019 #include "Rtypes.h"
00020 
00021 #include "Riostream.h"
00022 #include "RooMsgService.h"
00023 #include "RooNormSetCache.h"
00024 #include "RooAbsReal.h"
00025 #include "RooArgSet.h"
00026 #include "RooArgList.h"
00027 #include "RooAbsCache.h"
00028 #include "RooAbsCacheElement.h"
00029 #include <vector>
00030 
00031 class RooNameSet ;
00032 
00033 
00034 template<class T>
00035 class RooCacheManager : public RooAbsCache {
00036 
00037 public:
00038 
00039   RooCacheManager(Int_t maxSize=10) ;
00040   RooCacheManager(RooAbsArg* owner, Int_t maxSize=10) ;
00041   RooCacheManager(const RooCacheManager& other, RooAbsArg* owner=0) ;
00042   virtual ~RooCacheManager() ;
00043   
00044   T* getObj(const RooArgSet* nset, Int_t* sterileIndex=0, const TNamed* isetRangeName=0) {
00045     // Getter function without integration set 
00046     return getObj(nset,0,sterileIndex,isetRangeName) ;
00047   }
00048 
00049   Int_t setObj(const RooArgSet* nset, T* obj, const TNamed* isetRangeName=0) {
00050     // Setter function without integration set 
00051     return setObj(nset,0,obj,isetRangeName) ;
00052   }
00053 
00054   T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIndex=0, const TNamed* isetRangeName=0) ;
00055   Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=0) ;  
00056 
00057   void reset() ;
00058   void sterilize() ;
00059 
00060   Int_t lastIndex() const { 
00061     // Return index of slot used in last get or set operation
00062     return _lastIndex ; 
00063   }
00064   Int_t cacheSize() const { 
00065     // Return size of cache
00066     return _size ; 
00067   }
00068 
00069   virtual Bool_t redirectServersHook(const RooAbsCollection& /*newServerList*/, Bool_t /*mustReplaceAll*/, 
00070                                      Bool_t /*nameChange*/, Bool_t /*isRecursive*/) { 
00071     // Interface function to intercept server redirects
00072     return kFALSE ; 
00073   }
00074   virtual void operModeHook() {
00075     // Interface function to intercept cache operation mode changes
00076   }
00077   virtual void printCompactTreeHook(std::ostream&, const char *) {
00078     // Interface function to cache add contents to output in tree printing mode
00079   } 
00080 
00081   T* getObjByIndex(Int_t index) const ;
00082   const RooNameSet* nameSet1ByIndex(Int_t index) const ;
00083   const RooNameSet* nameSet2ByIndex(Int_t index) const ;
00084 
00085   virtual void insertObjectHook(T&) {
00086     // Interface function to perform post-insert operations on cached object
00087   } 
00088  
00089 protected:
00090 
00091   Int_t _maxSize ;    // Maximum size
00092   Int_t _size ;       // Actual use
00093   Int_t _lastIndex ;  // Last slot accessed
00094 
00095   RooNormSetCache* _nsetCache ; //! Normalization/Integration set manager
00096   T** _object ;                 //! Payload
00097 
00098   ClassDef(RooCacheManager,1) // Cache Manager class generic objects
00099 } ;
00100 
00101 
00102 template<class T>
00103 RooCacheManager<T>::RooCacheManager(Int_t maxSize) : RooAbsCache(0)
00104 {
00105   // Constructor for simple caches without RooAbsArg payload. A cache
00106   // made with this constructor is not registered with its owner
00107   // and will not receive information on server redirects and
00108   // cache operation mode changes
00109 
00110   _maxSize = maxSize ;
00111   _nsetCache = new RooNormSetCache[maxSize] ;
00112   _object = new T*[maxSize] ;
00113 }
00114 
00115 template<class T>
00116 RooCacheManager<T>::RooCacheManager(RooAbsArg* owner, Int_t maxSize) : RooAbsCache(owner)
00117 {
00118   // Constructor for simple caches with RooAbsArg derived payload. A cache
00119   // made with this constructor is registered with its owner
00120   // and will receive information on server redirects and
00121   // cache operation mode changes
00122   
00123   _maxSize = maxSize ;
00124   _size = 0 ;
00125 
00126   _nsetCache = new RooNormSetCache[maxSize] ;
00127   _object = new T*[maxSize] ;
00128   _lastIndex = -1 ;
00129 
00130   Int_t i ;
00131   for (i=0 ; i<_maxSize ; i++) {
00132     _object[i]=0 ;
00133   }
00134 
00135 }
00136 
00137 
00138 template<class T>
00139 RooCacheManager<T>::RooCacheManager(const RooCacheManager& other, RooAbsArg* owner) : RooAbsCache(other,owner)
00140 {
00141   // Copy constructor
00142 
00143   _maxSize = other._maxSize ;
00144   _size = other._size ;
00145   
00146   _nsetCache = new RooNormSetCache[_maxSize] ;
00147   _object = new T*[_maxSize] ;
00148   _lastIndex = -1 ;
00149 
00150 //   cout << "RooCacheManager:cctor(" << this << ")" << endl ;
00151 
00152   Int_t i ;
00153   for (i=0 ; i<other._size ; i++) {    
00154     _nsetCache[i].initialize(other._nsetCache[i]) ;
00155     _object[i] = 0 ;
00156   }
00157 
00158   for (i=other._size ; i<_maxSize ; i++) {    
00159     _object[i] = 0 ;
00160   }
00161 }
00162 
00163 
00164 template<class T>
00165 RooCacheManager<T>::~RooCacheManager()
00166 {
00167   // Destructor
00168 
00169   delete[] _nsetCache ;  
00170   Int_t i ;
00171   for (i=0 ; i<_size ; i++) {
00172     delete _object[i] ;
00173   }
00174   delete[] _object ;
00175 }
00176 
00177 
00178 template<class T>
00179 void RooCacheManager<T>::reset() 
00180 {
00181   // Clear the cache
00182 
00183   Int_t i ;
00184   for (i=0 ; i<_maxSize ; i++) {
00185     delete _object[i] ;
00186     _object[i]=0 ;
00187     _nsetCache[i].clear() ;
00188   }  
00189   _lastIndex = -1 ;
00190   _size = 0 ;
00191 }
00192   
00193 
00194 
00195 template<class T>
00196 void RooCacheManager<T>::sterilize() 
00197 {
00198   // Clear the cache payload but retain slot mapping w.r.t to
00199   // normalization and integration sets.
00200 
00201   Int_t i ;
00202   for (i=0 ; i<_maxSize ; i++) {
00203     delete _object[i] ;
00204     _object[i]=0 ;
00205   }  
00206 }
00207   
00208 
00209 
00210 template<class T>
00211 Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName) 
00212 {
00213   // Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName
00214 
00215   // Check if object is already registered
00216   Int_t sterileIdx(-1) ;
00217   if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
00218     return lastIndex() ;
00219   } 
00220 
00221 
00222   if (sterileIdx>=0) {
00223     // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
00224     _object[sterileIdx] = obj ;
00225 
00226     // Allow optional post-processing of object inserted in cache
00227     insertObjectHook(*obj) ;
00228 
00229     return lastIndex() ;
00230   }
00231 
00232   if (_size==_maxSize) {
00233     return -1 ;
00234   }
00235 
00236   _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,kTRUE) ;
00237   if (_object[_size]) {
00238     delete _object[_size] ;
00239   }
00240 
00241   _object[_size] = obj ;
00242   _size++ ;
00243 
00244   // Allow optional post-processing of object inserted in cache
00245   insertObjectHook(*obj) ;
00246 
00247   return _size-1 ;
00248 }
00249 
00250 
00251 template<class T>
00252 T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName) 
00253 {
00254   // Retrieve payload object indexed on nset,uset amd isetRangeName
00255   // If sterileIdx is not null, it is set to the index of the sterile
00256   // slot in cacse such a slot is recycled
00257 
00258   Int_t i ;
00259   for (i=0 ; i<_size ; i++) {
00260     if (_nsetCache[i].contains(nset,iset,isetRangeName)==kTRUE) {      
00261       _lastIndex = i ;
00262       if(_object[i]==0 && sterileIdx) *sterileIdx=i ;
00263       return _object[i] ;
00264     }
00265   }
00266 
00267   for (i=0 ; i<_size ; i++) {
00268     if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,kFALSE)==kFALSE) {
00269       _lastIndex = i ;
00270       if(_object[i]==0 && sterileIdx) *sterileIdx=i ;
00271       return _object[i] ;
00272     }
00273   }
00274   return 0 ;
00275 }
00276 
00277 
00278 
00279 template<class T>
00280 T* RooCacheManager<T>::getObjByIndex(Int_t index) const 
00281 {
00282   // Retrieve payload object by slot index
00283 
00284   if (index<0||index>=_size) {
00285     oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index (" 
00286                                    << index << ") out of range [0," << _size-1 << "]" << endl ;
00287     return 0 ;
00288   }
00289   return _object[index] ;
00290 }
00291 
00292 template<class T>
00293 const RooNameSet* RooCacheManager<T>::nameSet1ByIndex(Int_t index) const
00294 {
00295   // Retrieve RooNameSet associated with slot at given index
00296 
00297   if (index<0||index>=_size) {
00298     oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index (" 
00299                                    << index << ") out of range [0," << _size-1 << "]" << endl ;
00300     return 0 ;
00301   }
00302   return &_nsetCache[index].nameSet1() ;
00303 }
00304 
00305 template<class T>
00306 const RooNameSet* RooCacheManager<T>::nameSet2ByIndex(Int_t index) const 
00307 {
00308   // Retrieve RooNameSet associated with slot at given index
00309 
00310   if (index<0||index>=_size) {
00311     oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index (" 
00312                                    << index << ") out of range [0," << _size-1 << "]" << endl ;
00313     return 0 ;
00314   }
00315   return &_nsetCache[index].nameSet2() ;
00316 }
00317 
00318 
00319 #endif 

Generated on Tue Jul 5 14:26:05 2011 for ROOT_528-00b_version by  doxygen 1.5.1