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
35#include <RooNormSetCache.h>
36#include <RooHelpers.h>
37
38////////////////////////////////////////////////////////////////////////////////
39/// Clear contents
40
42{
43 {
44 PairIdxMapType tmpmap;
45 tmpmap.swap(_pairToIdx);
46 }
47 {
48 PairVectType tmppairvect;
49 tmppairvect.swap(_pairs);
50 }
51 _next = 0;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Add given pair of RooArgSet pointers to our store
56
57void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
58{
59 const Pair pair(set1, set2);
60 PairIdxMapType::iterator it = _pairToIdx.lower_bound(pair);
61 if (_pairToIdx.end() != it && it->first == pair) {
62 // not empty, and keys match - nothing to do
63 return;
64 }
65 // register pair -> index mapping
66 _pairToIdx.insert(it, std::make_pair(pair, _pairs.size()));
67 // save pair at that index
68 _pairs.push_back(pair);
69 // if the cache grew too large, start replacing in a round-robin fashion
70 while (_pairs.size() > _max) {
71 // new index of the pair: replace slot _next
72 it->second = _next;
73 // find and erase mapping of old pair in that slot
74 _pairToIdx.erase(_pairs[_next]);
75 // put new pair into new slot
76 _pairs[_next] = _pairs.back();
77 // and erase the copy we no longer need
78 _pairs.erase(_pairs.end() - 1);
79 ++_next;
80 _next %= _max;
81 }
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// If RooArgSets set1 and set2 or sets with similar contents have
86/// been seen by this cache manager before return `false` If not,
87/// return `true`. If sets have not been seen and doRefill is true,
88/// update cache reference to current input sets.
89
90bool RooNormSetCache::autoCache(const RooAbsArg* self, const RooArgSet* set1,
91 const RooArgSet* set2, const TNamed* set2RangeName, bool doRefill)
92{
93
94 // Automated cache management function - Returns `true` if cache is invalidated
95
96 // A - Check if set1/2 are in cache and range name is identical
97 if (set2RangeName == _set2RangeName && contains(set1,set2)) {
98 return false ;
99 }
100
101 // B - Check if dependents(set1/set2) are compatible with current cache
102
103// cout << "RooNormSetCache::autoCache set1 = " << (set1?*set1:RooArgSet()) << " set2 = " << (set2?*set2:RooArgSet()) << endl;
104// if (set1) set1->Print("v");
105// if (set2) set2->Print("v");
106 //if (self) self->Print("v");
107
108 RooArgSet set1d;
109 RooArgSet set2d;
110 if (self) {
111 if(set1) self->getObservables(set1,set1d,false);
112 if(set2) self->getObservables(set2,set2d,false);
113 } else {
114 if(set1) set1->snapshot(set1d);
115 if(set2) set2->snapshot(set2d);
116 }
117
118// cout << "RooNormSetCache::autoCache set1d = " << *set1d << " set2 = " << *set2d << endl;
119
121
124 && _set2RangeName == set2RangeName) {
125 // Compatible - Add current set1/2 to cache
126 add(set1,set2);
127
128 return false;
129 }
130
131 // C - Reset cache and refill with current state
132 if (doRefill) {
133 clear();
134 add(set1,set2);
137// cout << "RooNormSetCache::autoCache() _name1 refilled from " << *set1d << " to " ; _name1.printValue(cout) ; cout << endl;
138// cout << "RooNormSetCache::autoCache() _name2 refilled from " << *set2d << " to " ; _name2.printValue(cout) ; cout << endl;
139 _set2RangeName = (TNamed*) set2RangeName;
140 }
141
142 return true;
143}
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:71
RooArgSet * getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
Definition: RooAbsArg.h:308
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:179
bool autoCache(const RooAbsArg *self, const RooArgSet *set1, const RooArgSet *set2=0, const TNamed *set2RangeName=0, bool autoRefill=true)
If RooArgSets set1 and set2 or sets with similar contents have been seen by this cache manager before...
void clear()
Clear contents.
TNamed * _set2RangeName
!
std::string _name1
!
PairIdxMapType _pairToIdx
!
bool contains(const RooArgSet *set1, const RooArgSet *set2=0, const TNamed *set2RangeName=0)
std::size_t _max
!
std::size_t _next
!
void add(const RooArgSet *set1, const RooArgSet *set2=0)
Add given pair of RooArgSet pointers to our store.
std::string _name2
!
PairVectType _pairs
!
std::map< Pair, std::size_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:252