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
22Class RooNormSet cache manage the bookkeeping of multiple instances
23of sets of integration and normalization observables that effectively
24have the same definition. In complex function expression many
25RooArgSets with the same contents may be passed to an object that
26caches intermediate results dependent on the normalization/integration set
27To avoid unnecessary cache faulting, This class tracks all instances
28with the same contents and reports to the owner if the present nset/iset
29is truely different from the current reference. Class RooNormSet only
30evaluates each RooArgSet pointer once, it therefore assumes that
31RooArgSets with normalization and/or integration sets are not changes
32during 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>
45using 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
80void 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
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}
const Bool_t kFALSE
Definition: RtypesCore.h:101
unsigned long ULong_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:75
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Given a set of possible observables, return the observables that this PDF depends on.
Definition: RooAbsArg.h:315
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:35
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:158
Class RooNormSet cache manage the bookkeeping of multiple instances of sets of integration and normal...
void clear()
Clear contents.
virtual ~RooNormSetCache()
Destructor.
std::pair< const RooArgSet *, const RooArgSet * > Pair
TNamed * _set2RangeName
std::string _name1
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...
PairIdxMapType _pairToIdx
RooNormSetCache(ULong_t max=32)
void add(const RooArgSet *set1, const RooArgSet *set2=0)
Add given pair of RooArgSet pointers to our store.
std::string _name2
Bool_t contains(const RooArgSet *set1, const RooArgSet *set2=0, const TNamed *set2RangeName=0)
PairVectType _pairs
std::map< Pair, ULong_t > PairIdxMapType
std::vector< Pair > PairVectType
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
std::string getColonSeparatedNameString(RooArgSet const &argSet)
Create a string with all sorted names of RooArgSet elements separated by colons.
Definition: RooHelpers.cxx:248