ROOT  6.06/09
Reference Guide
RooNormSetCache.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
20 // Class RooNormSet cache manage the bookkeeping of multiple instances
21 // of sets of integration and normalization observables that effectively
22 // have the same definition. In complex function expression many
23 // RooArgSets with the same contents may be passed to an object that
24 // caches intermediate results dependent on the normalization/integration set
25 // To avoid unnecessary cache faulting, This class tracks all instances
26 // with the same contents and reports to the owner if the present nset/iset
27 // is truely different from the current reference. Class RooNormSet only
28 // evaluates each RooArgSet pointer once, it therefore assumes that
29 // RooArgSets with normalization and/or integration sets are not changes
30 // during their lifetime.
31 // END_HTML
32 //
33 #include "RooFit.h"
34 
35 #include "RooNormSetCache.h"
36 #include "RooArgSet.h"
37 
39 ;
40 
41 
42 #include <iostream>
43 using namespace std ;
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 
48  _max(max), _next(0), _set2RangeName(0)
49 {
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Destructor
54 
56 {
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Clear contents
61 
63 {
64  {
65  PairIdxMapType tmpmap;
66  tmpmap.swap(_pairToIdx);
67  }
68  {
69  PairVectType tmppairvect;
70  tmppairvect.swap(_pairs);
71  }
72  _next = 0;
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Add given pair of RooArgSet pointers to our store
77 
78 void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
79 {
80  const Pair pair(set1, set2);
81  PairIdxMapType::iterator it = _pairToIdx.lower_bound(pair);
82  if (_pairToIdx.end() != it && !PairCmp()(it->first, pair) &&
83  !PairCmp()(pair, it->first)) {
84  // not empty, and keys match - nothing to do
85  return;
86  }
87  // register pair -> index mapping
88  _pairToIdx.insert(it, std::make_pair(pair, ULong_t(_pairs.size())));
89  // save pair at that index
90  _pairs.push_back(pair);
91  // if the cache grew too large, start replacing in a round-robin fashion
92  while (_pairs.size() > _max) {
93  // new index of the pair: replace slot _next
94  it->second = _next;
95  // find and erase mapping of old pair in that slot
96  _pairToIdx.erase(_pairs[_next]);
97  // put new pair into new slot
98  _pairs[_next] = _pairs.back();
99  // and erase the copy we no longer need
100  _pairs.erase(_pairs.end() - 1);
101  ++_next;
102  _next %= _max;
103  }
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// If RooArgSets set1 and set2 or sets with similar contents have
108 /// been seen by this cache manager before return kFALSE If not,
109 /// return kTRUE. If sets have not been seen and doRefill is true,
110 /// update cache reference to current input sets.
111 
113  const RooArgSet* set2, const TNamed* set2RangeName, Bool_t doRefill)
114 {
115 
116  // Automated cache management function - Returns kTRUE if cache is invalidated
117 
118  // A - Check if set1/2 are in cache and range name is identical
119  if (set2RangeName == _set2RangeName && contains(set1,set2)) {
120  return kFALSE ;
121  }
122 
123  // B - Check if dependents(set1/set2) are compatible with current cache
124  RooNameSet nset1d, nset2d;
125 
126 // cout << "RooNormSetCache::autoCache set1 = " << (set1?*set1:RooArgSet()) << " set2 = " << (set2?*set2:RooArgSet()) << endl;
127 // if (set1) set1->Print("v");
128 // if (set2) set2->Print("v");
129  //if (self) self->Print("v");
130 
131  RooArgSet *set1d, *set2d ;
132  if (self) {
133  set1d = set1 ? self->getObservables(*set1,kFALSE) : new RooArgSet;
134  set2d = set2 ? self->getObservables(*set2,kFALSE) : new RooArgSet;
135  } else {
136  set1d = set1 ? (RooArgSet*)set1->snapshot() : new RooArgSet;
137  set2d = set2 ? (RooArgSet*)set2->snapshot() : new RooArgSet;
138  }
139 
140 // cout << "RooNormSetCache::autoCache set1d = " << *set1d << " set2 = " << *set2d << endl;
141 
142  nset1d.refill(*set1d);
143  nset2d.refill(*set2d);
144 
145  if (nset1d == _name1 && nset2d == _name2 && _set2RangeName == set2RangeName) {
146  // Compatible - Add current set1/2 to cache
147  add(set1,set2);
148 
149  delete set1d;
150  delete set2d;
151  return kFALSE;
152  }
153 
154  // C - Reset cache and refill with current state
155  if (doRefill) {
156  clear();
157  add(set1,set2);
158  _name1.refill(*set1d);
159  _name2.refill(*set2d);
160 // cout << "RooNormSetCache::autoCache() _name1 refilled from " << *set1d << " to " ; _name1.printValue(cout) ; cout << endl;
161 // cout << "RooNormSetCache::autoCache() _name2 refilled from " << *set2d << " to " ; _name2.printValue(cout) ; cout << endl;
162  _set2RangeName = (TNamed*) set2RangeName;
163  }
164 
165  delete set1d;
166  delete set2d;
167  return kTRUE;
168 }
ClassImp(RooNormSetCache)
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
Bool_t contains(const RooArgSet *set1, const RooArgSet *set2=0, const TNamed *set2RangeName=0)
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
PairIdxMapType _pairToIdx
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
void add(const RooArgSet *set1, const RooArgSet *set2=0)
Add given pair of RooArgSet pointers to our store.
TNamed * _set2RangeName
RooNameSet _name1
RooNormSetCache(ULong_t max=32)
std::pair< const RooArgSet *, const RooArgSet * > Pair
void refill(const RooArgSet &argSet)
Refill internal contents from names in given argSet.
Definition: RooNameSet.cxx:153
unsigned long ULong_t
Definition: RtypesCore.h:51
PairVectType _pairs
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
void clear()
Clear contents.
std::vector< Pair > PairVectType
std::map< Pair, ULong_t > PairIdxMapType
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
Bool_t autoCache(const RooAbsArg *self, const RooArgSet *set1, const RooArgSet *set2=0, const TNamed *set2RangeName=0, Bool_t autoRefill=kTRUE)
If RooArgSets set1 and set2 or sets with similar contents have been seen by this cache manager before...
virtual ~RooNormSetCache()
Destructor.