ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id: RooNormSetCache.cxx 29049 2009-06-17 09:42:55Z wouter $
 * 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 RooNormSet cache manage the bookkeeping of multiple instances
// of sets of integration and normalization observables that effectively
// have the same definition. In complex function expression many
// RooArgSets with the same contents may be passed to an object that
// caches intermediate results dependent on the normalization/integration set
// To avoid unnecessary cache faulting, This class tracks all instances
// with the same contents and reports to the owner if the present nset/iset
// is truely different from the current reference. Class RooNormSet only
// evaluates each RooArgSet pointer once, it therefore assumes that
// RooArgSets with normalization and/or integration sets are not changes
// during their lifetime. 
// END_HTML
//
#include "RooFit.h"

#include "RooNormSetCache.h"
#include "RooNormSetCache.h"
#include "RooArgSet.h"

ClassImp(RooNormSetCache)
;


#include <iostream>
using namespace std ;

//_____________________________________________________________________________
RooNormSetCache::RooNormSetCache(Int_t regSize) :
  _htable(0), _regSize(regSize), _nreg(0), _asArr(0), _set2RangeName(0)
{
  // Construct normalization set manager with given initial size
  _htable = regSize>16 ? new RooHashTable(regSize,RooHashTable::Intrinsic) : 0 ;
}



//_____________________________________________________________________________
RooNormSetCache::RooNormSetCache(const RooNormSetCache& other) :
  _htable(0), _regSize(other._regSize), _nreg(0), _asArr(0), _set2RangeName(0)
{
  // Copy constructor

  _htable = _regSize>16 ? new RooHashTable(_regSize,RooHashTable::Intrinsic) : 0 ;
}



//_____________________________________________________________________________
RooNormSetCache::~RooNormSetCache() 
{
  // Destructor

  delete[] _asArr ;
  if (_htable) delete _htable ;
}



//_____________________________________________________________________________
void RooNormSetCache::clear()
{
  // Clear contents 
  _nreg = 0 ;  
  if (_htable) {
    delete _htable ;
    _htable = 0 ;
  }
}



//_____________________________________________________________________________
void RooNormSetCache::initialize(const RooNormSetCache& other) 
{
  // Initialize cache from contents of given other cache
  clear() ;

  Int_t i ;
  for (i=0 ; i<other._nreg ; i++) {
    add(other._asArr[i]._set1,other._asArr[i]._set2) ;
  }

  _name1 = other._name1 ;
  _name2 = other._name2 ;

  _set2RangeName = other._set2RangeName ;
}



//_____________________________________________________________________________
void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
{
  // Add given pair of RooArgSet pointers to our store

  // If code list array has never been used, allocate and initialize here
  if (!_asArr) {
    _asArr = new RooSetPair[_regSize] ;
  }

  if (!contains(set1,set2)) {
    // Add to cache
    _asArr[_nreg]._set1 = (RooArgSet*)set1 ;
    _asArr[_nreg]._set2 = (RooArgSet*)set2 ;
    if (_htable) _htable->add((TObject*)&_asArr[_nreg]) ;
    _nreg++ ;
  }

  // Expand cache if full 
  if (_nreg==_regSize) expand() ;

}


//_____________________________________________________________________________
void RooNormSetCache::expand()
{
  // Expand registry size by doubling capacity

  Int_t newSize = _regSize*2 ;

  if (_htable) {
    delete _htable ;
    _htable = 0 ;
  }

  // Allocate increased buffer 
  RooSetPair* asArr_new = new RooSetPair[newSize] ;
  if (newSize>16) {
    //cout << "RooNormSetCache::add() instantiating hash table with size " << newSize << endl ;
    _htable = new RooHashTable(newSize,RooHashTable::Intrinsic) ;
  }

  // Copy old buffer 
  Int_t i ;
  for (i=0 ; i<_nreg ; i++) {
    asArr_new[i]._set1 = _asArr[i]._set1 ;
    asArr_new[i]._set2 = _asArr[i]._set2 ;
    if (_htable) _htable->add((TObject*)&asArr_new[i]) ;
  }
  
  // Delete old buffers 
  delete[] _asArr ;

  // Install new buffers
  _asArr = asArr_new ;
  _regSize = newSize ;
}



//_____________________________________________________________________________
Bool_t RooNormSetCache::autoCache(const RooAbsArg* self, const RooArgSet* set1, const RooArgSet* set2, const TNamed* set2RangeName, Bool_t doRefill) 
{
  // If RooArgSets set1 and set2 or sets with similar contents have
  // been seen by this cache manager before return kFALSE If not,
  // return kTRUE. If sets have not been seen and doRefill is true,
  // update cache reference to current input sets.
  

  // Automated cache management function - Returns kTRUE if cache is invalidated
  
  // A - Check if set1/2 are in cache and range name is identical
  if (set2RangeName==_set2RangeName && contains(set1,set2)) {
    return kFALSE ;
  }

  // B - Check if dependents(set1/set2) are compatible with current cache
  RooNameSet nset1d,nset2d ;

//   cout << "RooNormSetCache::autoCache set1 = " << (set1?*set1:RooArgSet()) << " set2 = " << (set2?*set2:RooArgSet()) << endl ;
//   if (set1) set1->Print("v") ;
//   if (set2) set2->Print("v") ;
  //if (self) self->Print("v") ;

  RooArgSet *set1d, *set2d ;
  if (self) {
    set1d = set1 ? self->getObservables(*set1,kFALSE) : new RooArgSet ;
    set2d = set2 ? self->getObservables(*set2,kFALSE) : new RooArgSet ;
  } else {
    set1d = set1 ? (RooArgSet*)set1->snapshot() : new RooArgSet ;
    set2d = set2 ? (RooArgSet*)set2->snapshot() : new RooArgSet ;
  }

//   cout << "RooNormSetCache::autoCache set1d = " << *set1d << " set2 = " << *set2d << endl ;

  nset1d.refill(*set1d) ;
  nset2d.refill(*set2d) ;

  if (nset1d==_name1&&nset2d==_name2&&_set2RangeName==set2RangeName) {
    // Compatible - Add current set1/2 to cache
    add(set1,set2) ;

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