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
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0045 T* getObj(const RooArgSet* nset, Int_t* sterileIndex=nullptr, const TNamed* isetRangeName=nullptr) {
0046 return getObj(nset,nullptr,sterileIndex,isetRangeName) ;
0047 }
0048
0049
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
0066 Int_t lastIndex() const {
0067 return _lastIndex ;
0068 }
0069
0070 Int_t cacheSize() const {
0071 return _size ;
0072 }
0073
0074
0075 bool redirectServersHook(const RooAbsCollection& , bool ,
0076 bool , bool ) override {
0077 return false ;
0078 }
0079
0080 void operModeHook() override {
0081 }
0082
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
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 ;
0108 Int_t _size = 0;
0109 Int_t _lastIndex = -1;
0110
0111 std::vector<RooNormSetCache> _nsetCache ;
0112 std::vector<T*> _object ;
0113 bool _wired = false;
0114
0115 ClassDefOverride(RooCacheManager,2)
0116 } ;
0117
0118
0119
0120
0121
0122
0123 template <class T>
0124 RooCacheManager<T>::RooCacheManager(Int_t maxSize) : _maxSize(maxSize)
0125 {
0126
0127 _nsetCache.resize(_maxSize) ;
0128 _object.resize(_maxSize,nullptr) ;
0129 }
0130
0131
0132
0133
0134
0135
0136 template <class T>
0137 RooCacheManager<T>::RooCacheManager(RooAbsArg *owner, Int_t maxSize)
0138 : RooAbsCache(owner), _maxSize(maxSize)
0139 {
0140
0141 _nsetCache.resize(_maxSize) ;
0142 _object.resize(_maxSize,nullptr) ;
0143
0144 Int_t i ;
0145 for (i=0 ; i<_maxSize ; i++) {
0146 _object[i]=nullptr ;
0147 }
0148
0149 }
0150
0151
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) ;
0158 _object.resize(_maxSize,nullptr) ;
0159
0160
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
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
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
0198
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
0211 template<class T>
0212 Int_t RooCacheManager<T>::setObj(const RooArgSet* nset, const RooArgSet* iset, T* obj, const TNamed* isetRangeName)
0213 {
0214
0215 Int_t sterileIdx(-1) ;
0216 if (getObj(nset,iset,&sterileIdx,isetRangeName)) {
0217 delete obj;
0218 return lastIndex() ;
0219 }
0220
0221
0222 if (sterileIdx>=0) {
0223
0224
0225 if (sterileIdx>=_maxSize) {
0226
0227 _maxSize = sterileIdx+4;
0228 _object.resize(_maxSize,nullptr) ;
0229 _nsetCache.resize(_maxSize) ;
0230 }
0231
0232
0233 _object[sterileIdx] = obj ;
0234
0235
0236 insertObjectHook(*obj) ;
0237
0238 return lastIndex() ;
0239 }
0240
0241 if (_size>=_maxSize-1) {
0242
0243 _maxSize *=2 ;
0244 _object.resize(_maxSize,nullptr) ;
0245 _nsetCache.resize(_maxSize) ;
0246 }
0247
0248
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
0258 insertObjectHook(*obj) ;
0259
0260
0261 _wired = false ;
0262
0263 return _size-1 ;
0264 }
0265
0266
0267
0268
0269
0270 template<class T>
0271 T* RooCacheManager<T>::getObj(const RooArgSet* nset, const RooArgSet* iset, Int_t* sterileIdx, const TNamed* isetRangeName)
0272 {
0273
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
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
0314
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
0328
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