Logo ROOT  
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 #include "RooHelpers.h"
39 
41 ;
42 
43 
44 #include <iostream>
45 using namespace std ;
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 
50  _max(max), _next(0), _set2RangeName(0)
51 {
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Destructor
56 
58 {
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Clear contents
63 
65 {
66  {
67  PairIdxMapType tmpmap;
68  tmpmap.swap(_pairToIdx);
69  }
70  {
71  PairVectType tmppairvect;
72  tmppairvect.swap(_pairs);
73  }
74  _next = 0;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Add given pair of RooArgSet pointers to our store
79 
80 void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
81 {
82  const Pair pair(set1, set2);
83  PairIdxMapType::iterator it = _pairToIdx.lower_bound(pair);
84  if (_pairToIdx.end() != it && !PairCmp()(it->first, pair) &&
85  !PairCmp()(pair, it->first)) {
86  // not empty, and keys match - nothing to do
87  return;
88  }
89  // register pair -> index mapping
90  _pairToIdx.insert(it, std::make_pair(pair, ULong_t(_pairs.size())));
91  // save pair at that index
92  _pairs.push_back(pair);
93  // if the cache grew too large, start replacing in a round-robin fashion
94  while (_pairs.size() > _max) {
95  // new index of the pair: replace slot _next
96  it->second = _next;
97  // find and erase mapping of old pair in that slot
98  _pairToIdx.erase(_pairs[_next]);
99  // put new pair into new slot
100  _pairs[_next] = _pairs.back();
101  // and erase the copy we no longer need
102  _pairs.erase(_pairs.end() - 1);
103  ++_next;
104  _next %= _max;
105  }
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// If RooArgSets set1 and set2 or sets with similar contents have
110 /// been seen by this cache manager before return kFALSE If not,
111 /// return kTRUE. If sets have not been seen and doRefill is true,
112 /// update cache reference to current input sets.
113 
115  const RooArgSet* set2, const TNamed* set2RangeName, Bool_t doRefill)
116 {
117 
118  // Automated cache management function - Returns kTRUE if cache is invalidated
119 
120  // A - Check if set1/2 are in cache and range name is identical
121  if (set2RangeName == _set2RangeName && contains(set1,set2)) {
122  return kFALSE ;
123  }
124 
125  // B - Check if dependents(set1/set2) are compatible with current cache
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 
144 
145  if ( getColonSeparatedNameString(*set1d) == _name1
146  && getColonSeparatedNameString(*set2d) == _name2
147  && _set2RangeName == set2RangeName) {
148  // Compatible - Add current set1/2 to cache
149  add(set1,set2);
150 
151  delete set1d;
152  delete set2d;
153  return kFALSE;
154  }
155 
156  // C - Reset cache and refill with current state
157  if (doRefill) {
158  clear();
159  add(set1,set2);
162 // cout << "RooNormSetCache::autoCache() _name1 refilled from " << *set1d << " to " ; _name1.printValue(cout) ; cout << endl;
163 // cout << "RooNormSetCache::autoCache() _name2 refilled from " << *set2d << " to " ; _name2.printValue(cout) ; cout << endl;
164  _set2RangeName = (TNamed*) set2RangeName;
165  }
166 
167  delete set1d;
168  delete set2d;
169  return kTRUE;
170 }
RooNormSetCache
Class RooNormSet cache manage the bookkeeping of multiple instances of sets of integration and normal...
Definition: RooNormSetCache.h:30
RooNormSetCache::PairIdxMapType
std::map< Pair, ULong_t > PairIdxMapType
Definition: RooNormSetCache.h:42
RooHelpers.h
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
RooFit.h
RooArgSet.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooNormSetCache::_name1
std::string _name1
Definition: RooNormSetCache.h:97
RooNormSetCache::_next
ULong_t _next
Definition: RooNormSetCache.h:95
RooNormSetCache::_pairToIdx
PairIdxMapType _pairToIdx
Definition: RooNormSetCache.h:93
RooNormSetCache::_pairs
PairVectType _pairs
Definition: RooNormSetCache.h:92
RooNormSetCache::contains
Bool_t contains(const RooArgSet *set1, const RooArgSet *set2=0, const TNamed *set2RangeName=0)
Definition: RooNormSetCache.h:63
RooNormSetCache::RooNormSetCache
RooNormSetCache(ULong_t max=32)
Definition: RooNormSetCache.cxx:49
bool
RooNormSetCache.h
RooArgSet::snapshot
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:115
RooNormSetCache::_set2RangeName
TNamed * _set2RangeName
Definition: RooNormSetCache.h:99
RooHelpers::getColonSeparatedNameString
std::string getColonSeparatedNameString(RooArgSet const &argSet)
Create a string with all sorted names of RooArgSet elements separated by colons.
Definition: RooHelpers.cxx:271
RooNormSetCache::_name2
std::string _name2
Definition: RooNormSetCache.h:98
RooNormSetCache::_max
ULong_t _max
Definition: RooNormSetCache.h:94
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
RooNormSetCache::add
void add(const RooArgSet *set1, const RooArgSet *set2=0)
Add given pair of RooArgSet pointers to our store.
Definition: RooNormSetCache.cxx:80
RooNormSetCache::PairCmp
Definition: RooNormSetCache.h:33
ULong_t
unsigned long ULong_t
Definition: RtypesCore.h:55
RooNormSetCache::clear
void clear()
Clear contents.
Definition: RooNormSetCache.cxx:64
RooNormSetCache::Pair
std::pair< const RooArgSet *, const RooArgSet * > Pair
Definition: RooNormSetCache.h:32
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooNormSetCache::autoCache
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...
Definition: RooNormSetCache.cxx:114
RooNormSetCache::~RooNormSetCache
virtual ~RooNormSetCache()
Destructor.
Definition: RooNormSetCache.cxx:57
RooNormSetCache::PairVectType
std::vector< Pair > PairVectType
Definition: RooNormSetCache.h:41
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:33