Logo ROOT   6.08/07
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 \file RooNormSetCache.cxx
19 \class RooNormSetCache
20 \ingroup Roofitcore
21 
22 Class RooNormSet cache manage the bookkeeping of multiple instances
23 of sets of integration and normalization observables that effectively
24 have the same definition. In complex function expression many
25 RooArgSets with the same contents may be passed to an object that
26 caches intermediate results dependent on the normalization/integration set
27 To avoid unnecessary cache faulting, This class tracks all instances
28 with the same contents and reports to the owner if the present nset/iset
29 is truely different from the current reference. Class RooNormSet only
30 evaluates each RooArgSet pointer once, it therefore assumes that
31 RooArgSets with normalization and/or integration sets are not changes
32 during their lifetime.
33 **/
34 #include "RooFit.h"
35 
36 #include "RooNormSetCache.h"
37 #include "RooArgSet.h"
38 
40 ;
41 
42 
43 #include <iostream>
44 using namespace std ;
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 
49  _max(max), _next(0), _set2RangeName(0)
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Destructor
55 
57 {
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Clear contents
62 
64 {
65  {
66  PairIdxMapType tmpmap;
67  tmpmap.swap(_pairToIdx);
68  }
69  {
70  PairVectType tmppairvect;
71  tmppairvect.swap(_pairs);
72  }
73  _next = 0;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Add given pair of RooArgSet pointers to our store
78 
79 void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
80 {
81  const Pair pair(set1, set2);
82  PairIdxMapType::iterator it = _pairToIdx.lower_bound(pair);
83  if (_pairToIdx.end() != it && !PairCmp()(it->first, pair) &&
84  !PairCmp()(pair, it->first)) {
85  // not empty, and keys match - nothing to do
86  return;
87  }
88  // register pair -> index mapping
89  _pairToIdx.insert(it, std::make_pair(pair, ULong_t(_pairs.size())));
90  // save pair at that index
91  _pairs.push_back(pair);
92  // if the cache grew too large, start replacing in a round-robin fashion
93  while (_pairs.size() > _max) {
94  // new index of the pair: replace slot _next
95  it->second = _next;
96  // find and erase mapping of old pair in that slot
97  _pairToIdx.erase(_pairs[_next]);
98  // put new pair into new slot
99  _pairs[_next] = _pairs.back();
100  // and erase the copy we no longer need
101  _pairs.erase(_pairs.end() - 1);
102  ++_next;
103  _next %= _max;
104  }
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// If RooArgSets set1 and set2 or sets with similar contents have
109 /// been seen by this cache manager before return kFALSE If not,
110 /// return kTRUE. If sets have not been seen and doRefill is true,
111 /// update cache reference to current input sets.
112 
114  const RooArgSet* set2, const TNamed* set2RangeName, Bool_t doRefill)
115 {
116 
117  // Automated cache management function - Returns kTRUE if cache is invalidated
118 
119  // A - Check if set1/2 are in cache and range name is identical
120  if (set2RangeName == _set2RangeName && contains(set1,set2)) {
121  return kFALSE ;
122  }
123 
124  // B - Check if dependents(set1/set2) are compatible with current cache
125  RooNameSet nset1d, nset2d;
126 
127 // cout << "RooNormSetCache::autoCache set1 = " << (set1?*set1:RooArgSet()) << " set2 = " << (set2?*set2:RooArgSet()) << endl;
128 // if (set1) set1->Print("v");
129 // if (set2) set2->Print("v");
130  //if (self) self->Print("v");
131 
132  RooArgSet *set1d, *set2d ;
133  if (self) {
134  set1d = set1 ? self->getObservables(*set1,kFALSE) : new RooArgSet;
135  set2d = set2 ? self->getObservables(*set2,kFALSE) : new RooArgSet;
136  } else {
137  set1d = set1 ? (RooArgSet*)set1->snapshot() : new RooArgSet;
138  set2d = set2 ? (RooArgSet*)set2->snapshot() : new RooArgSet;
139  }
140 
141 // cout << "RooNormSetCache::autoCache set1d = " << *set1d << " set2 = " << *set2d << endl;
142 
143  nset1d.refill(*set1d);
144  nset2d.refill(*set2d);
145 
146  if (nset1d == _name1 && nset2d == _name2 && _set2RangeName == set2RangeName) {
147  // Compatible - Add current set1/2 to cache
148  add(set1,set2);
149 
150  delete set1d;
151  delete set2d;
152  return kFALSE;
153  }
154 
155  // C - Reset cache and refill with current state
156  if (doRefill) {
157  clear();
158  add(set1,set2);
159  _name1.refill(*set1d);
160  _name2.refill(*set2d);
161 // cout << "RooNormSetCache::autoCache() _name1 refilled from " << *set1d << " to " ; _name1.printValue(cout) ; cout << endl;
162 // cout << "RooNormSetCache::autoCache() _name2 refilled from " << *set2d << " to " ; _name2.printValue(cout) ; cout << endl;
163  _set2RangeName = (TNamed*) set2RangeName;
164  }
165 
166  delete set1d;
167  delete set2d;
168  return kTRUE;
169 }
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.
Class RooNormSet cache manage the bookkeeping of multiple instances of sets of integration and normal...
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.
RooNameSet is a utility class that stores the names the objects in a RooArget.
Definition: RooNameSet.h:24
TNamed * _set2RangeName
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
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:154
#define ClassImp(name)
Definition: Rtypes.h:279
unsigned long ULong_t
Definition: RtypesCore.h:51
PairVectType _pairs
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.