RooNormSetCache.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooNormSetCache.cxx 29049 2009-06-17 09:42:55Z wouter $
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 
00017 //////////////////////////////////////////////////////////////////////////////
00018 //
00019 // BEGIN_HTML
00020 // Class RooNormSet cache manage the bookkeeping of multiple instances
00021 // of sets of integration and normalization observables that effectively
00022 // have the same definition. In complex function expression many
00023 // RooArgSets with the same contents may be passed to an object that
00024 // caches intermediate results dependent on the normalization/integration set
00025 // To avoid unnecessary cache faulting, This class tracks all instances
00026 // with the same contents and reports to the owner if the present nset/iset
00027 // is truely different from the current reference. Class RooNormSet only
00028 // evaluates each RooArgSet pointer once, it therefore assumes that
00029 // RooArgSets with normalization and/or integration sets are not changes
00030 // during their lifetime. 
00031 // END_HTML
00032 //
00033 #include "RooFit.h"
00034 
00035 #include "RooNormSetCache.h"
00036 #include "RooNormSetCache.h"
00037 #include "RooArgSet.h"
00038 
00039 ClassImp(RooNormSetCache)
00040 ;
00041 
00042 
00043 #include <iostream>
00044 using namespace std ;
00045 
00046 //_____________________________________________________________________________
00047 RooNormSetCache::RooNormSetCache(Int_t regSize) :
00048   _htable(0), _regSize(regSize), _nreg(0), _asArr(0), _set2RangeName(0)
00049 {
00050   // Construct normalization set manager with given initial size
00051   _htable = regSize>16 ? new RooHashTable(regSize,RooHashTable::Intrinsic) : 0 ;
00052 }
00053 
00054 
00055 
00056 //_____________________________________________________________________________
00057 RooNormSetCache::RooNormSetCache(const RooNormSetCache& other) :
00058   _htable(0), _regSize(other._regSize), _nreg(0), _asArr(0), _set2RangeName(0)
00059 {
00060   // Copy constructor
00061 
00062   _htable = _regSize>16 ? new RooHashTable(_regSize,RooHashTable::Intrinsic) : 0 ;
00063 }
00064 
00065 
00066 
00067 //_____________________________________________________________________________
00068 RooNormSetCache::~RooNormSetCache() 
00069 {
00070   // Destructor
00071 
00072   delete[] _asArr ;
00073   if (_htable) delete _htable ;
00074 }
00075 
00076 
00077 
00078 //_____________________________________________________________________________
00079 void RooNormSetCache::clear()
00080 {
00081   // Clear contents 
00082   _nreg = 0 ;  
00083   if (_htable) {
00084     delete _htable ;
00085     _htable = 0 ;
00086   }
00087 }
00088 
00089 
00090 
00091 //_____________________________________________________________________________
00092 void RooNormSetCache::initialize(const RooNormSetCache& other) 
00093 {
00094   // Initialize cache from contents of given other cache
00095   clear() ;
00096 
00097   Int_t i ;
00098   for (i=0 ; i<other._nreg ; i++) {
00099     add(other._asArr[i]._set1,other._asArr[i]._set2) ;
00100   }
00101 
00102   _name1 = other._name1 ;
00103   _name2 = other._name2 ;
00104 
00105   _set2RangeName = other._set2RangeName ;
00106 }
00107 
00108 
00109 
00110 //_____________________________________________________________________________
00111 void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
00112 {
00113   // Add given pair of RooArgSet pointers to our store
00114 
00115   // If code list array has never been used, allocate and initialize here
00116   if (!_asArr) {
00117     _asArr = new RooSetPair[_regSize] ;
00118   }
00119 
00120   if (!contains(set1,set2)) {
00121     // Add to cache
00122     _asArr[_nreg]._set1 = (RooArgSet*)set1 ;
00123     _asArr[_nreg]._set2 = (RooArgSet*)set2 ;
00124     if (_htable) _htable->add((TObject*)&_asArr[_nreg]) ;
00125     _nreg++ ;
00126   }
00127 
00128   // Expand cache if full 
00129   if (_nreg==_regSize) expand() ;
00130 
00131 }
00132 
00133 
00134 //_____________________________________________________________________________
00135 void RooNormSetCache::expand()
00136 {
00137   // Expand registry size by doubling capacity
00138 
00139   Int_t newSize = _regSize*2 ;
00140 
00141   if (_htable) {
00142     delete _htable ;
00143     _htable = 0 ;
00144   }
00145 
00146   // Allocate increased buffer 
00147   RooSetPair* asArr_new = new RooSetPair[newSize] ;
00148   if (newSize>16) {
00149     //cout << "RooNormSetCache::add() instantiating hash table with size " << newSize << endl ;
00150     _htable = new RooHashTable(newSize,RooHashTable::Intrinsic) ;
00151   }
00152 
00153   // Copy old buffer 
00154   Int_t i ;
00155   for (i=0 ; i<_nreg ; i++) {
00156     asArr_new[i]._set1 = _asArr[i]._set1 ;
00157     asArr_new[i]._set2 = _asArr[i]._set2 ;
00158     if (_htable) _htable->add((TObject*)&asArr_new[i]) ;
00159   }
00160   
00161   // Delete old buffers 
00162   delete[] _asArr ;
00163 
00164   // Install new buffers
00165   _asArr = asArr_new ;
00166   _regSize = newSize ;
00167 }
00168 
00169 
00170 
00171 //_____________________________________________________________________________
00172 Bool_t RooNormSetCache::autoCache(const RooAbsArg* self, const RooArgSet* set1, const RooArgSet* set2, const TNamed* set2RangeName, Bool_t doRefill) 
00173 {
00174   // If RooArgSets set1 and set2 or sets with similar contents have
00175   // been seen by this cache manager before return kFALSE If not,
00176   // return kTRUE. If sets have not been seen and doRefill is true,
00177   // update cache reference to current input sets.
00178   
00179 
00180   // Automated cache management function - Returns kTRUE if cache is invalidated
00181   
00182   // A - Check if set1/2 are in cache and range name is identical
00183   if (set2RangeName==_set2RangeName && contains(set1,set2)) {
00184     return kFALSE ;
00185   }
00186 
00187   // B - Check if dependents(set1/set2) are compatible with current cache
00188   RooNameSet nset1d,nset2d ;
00189 
00190 //   cout << "RooNormSetCache::autoCache set1 = " << (set1?*set1:RooArgSet()) << " set2 = " << (set2?*set2:RooArgSet()) << endl ;
00191 //   if (set1) set1->Print("v") ;
00192 //   if (set2) set2->Print("v") ;
00193   //if (self) self->Print("v") ;
00194 
00195   RooArgSet *set1d, *set2d ;
00196   if (self) {
00197     set1d = set1 ? self->getObservables(*set1,kFALSE) : new RooArgSet ;
00198     set2d = set2 ? self->getObservables(*set2,kFALSE) : new RooArgSet ;
00199   } else {
00200     set1d = set1 ? (RooArgSet*)set1->snapshot() : new RooArgSet ;
00201     set2d = set2 ? (RooArgSet*)set2->snapshot() : new RooArgSet ;
00202   }
00203 
00204 //   cout << "RooNormSetCache::autoCache set1d = " << *set1d << " set2 = " << *set2d << endl ;
00205 
00206   nset1d.refill(*set1d) ;
00207   nset2d.refill(*set2d) ;
00208 
00209   if (nset1d==_name1&&nset2d==_name2&&_set2RangeName==set2RangeName) {
00210     // Compatible - Add current set1/2 to cache
00211     add(set1,set2) ;
00212 
00213     delete set1d ;
00214     delete set2d ;
00215     return kFALSE ;
00216   }
00217   
00218   // C - Reset cache and refill with current state
00219   if (doRefill) {
00220     clear() ;
00221     add(set1,set2) ;
00222     _name1.refill(*set1d) ;
00223     _name2.refill(*set2d) ;
00224 //     cout << "RooNormSetCache::autoCache() _name1 refilled from " << *set1d << " to " ; _name1.printValue(cout) ; cout << endl ;
00225 //     cout << "RooNormSetCache::autoCache() _name2 refilled from " << *set2d << " to " ; _name2.printValue(cout) ; cout << endl ;
00226     _set2RangeName = (TNamed*) set2RangeName ;
00227   }
00228   
00229   delete set1d ;
00230   delete set2d ;
00231   return kTRUE ;
00232 }

Generated on Tue Jul 5 15:06:53 2011 for ROOT_528-00b_version by  doxygen 1.5.1