Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/RooCacheManager.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id$
0005  * Authors:                                                                  *
0006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0008  *                                                                           *
0009  * Copyright (c) 2000-2005, Regents of the University of California          *
0010  *                          and Stanford University. All rights reserved.    *
0011  *                                                                           *
0012  * Redistribution and use in source and binary forms,                        *
0013  * with or without modification, are permitted according to the terms        *
0014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
0015  *****************************************************************************/
0016 #ifndef ROO_CACHE_MANAGER
0017 #define ROO_CACHE_MANAGER
0018 
0019 #include "RooMsgService.h"
0020 #include "RooNormSetCache.h"
0021 #include "RooAbsReal.h"
0022 #include "RooArgSet.h"
0023 #include "RooArgList.h"
0024 #include "RooAbsCache.h"
0025 #include "RooAbsCacheElement.h"
0026 #include "RooNameReg.h"
0027 
0028 #include <ROOT/StringUtils.hxx>
0029 #include "Rtypes.h"
0030 
0031 #include <vector>
0032 
0033 
0034 template<class T>
0035 class RooCacheManager : public RooAbsCache {
0036 
0037 public:
0038 
0039   RooCacheManager(Int_t maxSize=2) ;
0040   RooCacheManager(RooAbsArg* owner, Int_t maxSize=2) ;
0041   RooCacheManager(const RooCacheManager& other, RooAbsArg* owner=nullptr) ;
0042   ~RooCacheManager() override ;
0043 
0044   /// Getter function without integration set
0045   T* getObj(const RooArgSet* nset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) {
0046     return getObj(nset,nullptr,sterileIndex,isetRangeName) ;
0047   }
0048 
0049   /// Setter function without integration set
0050   Int_t setObj(const RooArgSet* nset, T* obj, const TNamed* isetRangeName=nullptr) {
0051     return setObj(nset,nullptr,obj,isetRangeName) ;
0052   }
0053 
0054   inline T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const char* isetRangeName)  {
0055     if (_wired) return _object[0] ;
0056     return getObj(nset,iset,sterileIdx,RooNameReg::ptr(isetRangeName)) ;
0057   }
0058 
0059   T* getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) ;
0060   Int_t setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName=nullptr) ;
0061 
0062   void reset() ;
0063   virtual void sterilize() ;
0064 
0065   /// Return index of slot used in last get or set operation
0066   Int_t lastIndex() const {
0067     return _lastIndex ;
0068   }
0069   /// Return size of cache
0070   Int_t cacheSize() const {
0071     return _size ;
0072   }
0073 
0074   /// Interface function to intercept server redirects
0075   bool redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/,
0076                  bool /*nameChange*/, bool /*isRecursive*/) override {
0077     return false ;
0078   }
0079   /// Interface function to intercept cache operation mode changes
0080   void operModeHook() override {
0081   }
0082   /// Interface function to cache add contents to output in tree printing mode
0083   void printCompactTreeHook(std::ostream&, const char *) override {
0084   }
0085 
0086   T* getObjByIndex(Int_t index) const ;
0087   RooArgSet selectFromSet1(RooArgSet const& argSet, int index) const ;
0088   RooArgSet selectFromSet2(RooArgSet const& argSet, int index) const ;
0089 
0090   /// Interface function to perform post-insert operations on cached object
0091   virtual void insertObjectHook(T&) {
0092   }
0093 
0094   void wireCache() override {
0095     if (_size==0) {
0096       oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") no cached elements!" << std::endl ;
0097     } else if (_size==1) {
0098       oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") now wiring cache" << std::endl ;
0099       _wired=true ;
0100     } else if (_size>1) {
0101       oocoutI(_owner,Optimization) << "RooCacheManager::wireCache(" << _owner->GetName() << ") cache cannot be wired because it contains more than one element" << std::endl ;
0102     }
0103   }
0104 
0105 protected:
0106 
0107   Int_t _maxSize ;       ///<! Maximum size
0108   Int_t _size = 0;       ///<! Actual use
0109   Int_t _lastIndex = -1; ///<! Last slot accessed
0110 
0111   std::vector<RooNormSetCache> _nsetCache ; ///<! Normalization/Integration set manager
0112   std::vector<T*> _object ;                 ///<! Payload
0113   bool _wired = false;               ///<! In wired mode, there is a single payload which is returned always
0114 
0115   ClassDefOverride(RooCacheManager,2) // Cache Manager class generic objects
0116 } ;
0117 
0118 
0119 /// Constructor for simple caches without RooAbsArg payload. A cache
0120 /// made with this constructor is not registered with its owner
0121 /// and will not receive information on server redirects and
0122 /// cache operation mode changes.
0123 template <class T>
0124 RooCacheManager<T>::RooCacheManager(Int_t maxSize) : _maxSize(maxSize)
0125 {
0126 
0127   _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
0128   _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
0129 }
0130 
0131 
0132 /// Constructor for simple caches with RooAbsArg derived payload. A cache
0133 /// made with this constructor is registered with its owner
0134 /// and will receive information on server redirects and
0135 /// cache operation mode changes.
0136 template <class T>
0137 RooCacheManager<T>::RooCacheManager(RooAbsArg *owner, Int_t maxSize)
0138    : RooAbsCache(owner), _maxSize(maxSize)
0139 {
0140 
0141   _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[maxSize] ;
0142   _object.resize(_maxSize,nullptr) ; // = new T*[maxSize] ;
0143 
0144   Int_t i ;
0145   for (i=0 ; i<_maxSize ; i++) {
0146     _object[i]=nullptr ;
0147   }
0148 
0149 }
0150 
0151 /// Copy constructor.
0152 template <class T>
0153 RooCacheManager<T>::RooCacheManager(const RooCacheManager &other, RooAbsArg *owner)
0154    : RooAbsCache(other, owner), _maxSize(other._maxSize), _size(other._size)
0155 {
0156 
0157   _nsetCache.resize(_maxSize) ; // = new RooNormSetCache[_maxSize] ;
0158   _object.resize(_maxSize,nullptr) ; // = new T*[_maxSize] ;
0159 
0160   //std::cout << "RooCacheManager:cctor(" << this << ") other = " << &other << " _size=" << _size << " _maxSize = " << _maxSize << std::endl ;
0161 
0162   Int_t i ;
0163   for (i=0 ; i<other._size ; i++) {
0164     _nsetCache[i] = other._nsetCache[i];
0165     _object[i] = nullptr ;
0166   }
0167 
0168   for (i=other._size ; i<_maxSize ; i++) {
0169     _object[i] = nullptr ;
0170   }
0171 }
0172 
0173   /// Destructor
0174 template<class T>
0175 RooCacheManager<T>::~RooCacheManager()
0176 {
0177   for (int i=0 ; i<_size ; i++) {
0178     delete _object[i] ;
0179   }
0180 }
0181 
0182 
0183   /// Clear the cache
0184 template<class T>
0185 void RooCacheManager<T>::reset()
0186 {
0187   for (int i=0 ; i<_maxSize ; i++) {
0188     delete _object[i] ;
0189     _object[i]=nullptr ;
0190     _nsetCache[i].clear() ;
0191   }
0192   _lastIndex = -1 ;
0193   _size = 0 ;
0194 }
0195 
0196 
0197 /// Clear the cache payload but retain slot mapping w.r.t to
0198 /// normalization and integration sets.
0199 template<class T>
0200 void RooCacheManager<T>::sterilize()
0201 {
0202   Int_t i ;
0203   for (i=0 ; i<_maxSize ; i++) {
0204     delete _object[i] ;
0205     _object[i]=nullptr ;
0206   }
0207 }
0208 
0209 
0210 /// Insert payload object 'obj' in cache indexed on nset,iset and isetRangeName.
0211 template<class T>
0212 Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName)
0213 {
0214   // Check if object is already registered
0215   Int_t sterileIdx(-1) ;
0216   if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
0217     delete obj; // important! do not forget to cleanup memory
0218     return lastIndex() ;
0219   }
0220 
0221 
0222   if (sterileIdx>=0) {
0223     // Found sterile slot that can should be recycled [ sterileIndex only set if isetRangeName matches ]
0224 
0225     if (sterileIdx>=_maxSize) {
0226       //cout << "RooCacheManager<T>::setObj()/SI increasing object cache size from " << _maxSize << " to " << sterileIdx+4 << endl ;
0227       _maxSize = sterileIdx+4;
0228       _object.resize(_maxSize,nullptr) ;
0229       _nsetCache.resize(_maxSize) ;
0230     }
0231 
0232 
0233     _object[sterileIdx] = obj ;
0234 
0235     // Allow optional post-processing of object inserted in cache
0236     insertObjectHook(*obj) ;
0237 
0238     return lastIndex() ;
0239   }
0240 
0241   if (_size>=_maxSize-1) {
0242     //cout << "RooCacheManager<T>::setObj() increasing object cache size from " << _maxSize << " to " << _maxSize*2 << endl ;
0243     _maxSize *=2 ;
0244     _object.resize(_maxSize,nullptr) ;
0245     _nsetCache.resize(_maxSize) ;
0246   }
0247 
0248   //cout << "RooCacheManager::setObj<T>(" << this << ") _size = " << _size << " _maxSize = " << _maxSize << endl ;
0249   _nsetCache[_size].autoCache(_owner,nset,iset,isetRangeName,true) ;
0250   if (_object[_size]) {
0251     delete _object[_size] ;
0252   }
0253 
0254   _object[_size] = obj ;
0255   _size++ ;
0256 
0257   // Allow optional post-processing of object inserted in cache
0258   insertObjectHook(*obj) ;
0259 
0260   // Unwire cache in case it was wired
0261   _wired = false ;
0262 
0263   return _size-1 ;
0264 }
0265 
0266 
0267 /// Retrieve payload object indexed on nset,uset amd isetRangeName
0268 /// If sterileIdx is not null, it is set to the index of the sterile
0269 /// slot in cacse such a slot is recycled.
0270 template<class T>
0271 T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName)
0272 {
0273   // Fast-track for wired mode
0274   if (_wired) {
0275     if(_object[0]==nullptr && sterileIdx) *sterileIdx=0 ;
0276     return _object[0] ;
0277   }
0278 
0279   Int_t i ;
0280   for (i=0 ; i<_size ; i++) {
0281     if (_nsetCache[i].contains(nset,iset,isetRangeName)==true) {
0282       _lastIndex = i ;
0283       if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
0284       return _object[i] ;
0285     }
0286   }
0287 
0288   for (i=0 ; i<_size ; i++) {
0289     if (_nsetCache[i].autoCache(_owner,nset,iset,isetRangeName,false)==false) {
0290       _lastIndex = i ;
0291       if(_object[i]==nullptr && sterileIdx) *sterileIdx=i ;
0292       return _object[i] ;
0293     }
0294   }
0295 
0296   return nullptr ;
0297 }
0298 
0299 
0300 /// Retrieve payload object by slot index.
0301 template<class T>
0302 T* RooCacheManager<T>::getObjByIndex(Int_t index) const
0303 {
0304   if (index<0||index>=_size) {
0305     oocoutE(_owner,ObjectHandling) << "RooCacheManager::getNormListByIndex: ERROR index ("
0306                << index << ") out of range [0," << _size-1 << "]" << std::endl ;
0307     return nullptr ;
0308   }
0309   return _object[index] ;
0310 }
0311 
0312 
0313 /// Create RooArgSet containing the objects that are both in the cached set 1
0314 /// with a given index and an input argSet.
0315 template <class T>
0316 RooArgSet RooCacheManager<T>::selectFromSet1(RooArgSet const &argSet, int index) const
0317 {
0318    RooArgSet output;
0319    for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet1(), ":")) {
0320       if (RooAbsArg *arg = argSet.find(name.c_str())) {
0321          output.add(*arg);
0322       }
0323    }
0324    return output;
0325 }
0326 
0327 /// Create RooArgSet containing the objects that are both in the cached set 2
0328 /// with a given index and an input argSet.
0329 template <class T>
0330 RooArgSet RooCacheManager<T>::selectFromSet2(RooArgSet const &argSet, int index) const
0331 {
0332    RooArgSet output;
0333    for (auto const &name : ROOT::Split(_nsetCache.at(index).nameSet2(), ":")) {
0334       if (RooAbsArg *arg = argSet.find(name.c_str())) {
0335          output.add(*arg);
0336       }
0337    }
0338    return output;
0339 }
0340 
0341 #endif