Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 _pairSet.clear();
44 _pairs.clear();
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Add given pair of RooArgSet pointers to our store
49
50void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
51{
52 const Pair_t pair{RooFit::getUniqueId(set1), RooFit::getUniqueId(set2)};
53 auto it = _pairSet.find(pair);
54 if (it != _pairSet.end()) {
55 // not empty, and keys match - nothing to do
56 return;
57 }
58 // register pair -> index mapping
59 _pairSet.emplace(pair);
60 // save pair at that index
61 _pairs.emplace_back(pair);
62 // if the cache grew too large, start replacing in a round-robin fashion
63 while (_pairs.size() > _max) {
64 _pairSet.erase(_pairs.front());
65 _pairs.pop_front();
66 }
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// If RooArgSets set1 and set2 or sets with similar contents have
71/// been seen by this cache manager before return `false` If not,
72/// return `true`. If sets have not been seen and doRefill is true,
73/// update cache reference to current input sets.
74
75bool RooNormSetCache::autoCache(const RooAbsArg* self, const RooArgSet* set1,
76 const RooArgSet* set2, const TNamed* set2RangeName, bool doRefill)
77{
78
79 // Automated cache management function - Returns `true` if cache is invalidated
80
81 // A - Check if set1/2 are in cache and range name is identical
82 if (set2RangeName == _set2RangeName && contains(set1,set2)) {
83 return false ;
84 }
85
86 // B - Check if dependents(set1/set2) are compatible with current cache
87
88// cout << "RooNormSetCache::autoCache set1 = " << (set1?*set1:RooArgSet()) << " set2 = " << (set2?*set2:RooArgSet()) << endl;
89// if (set1) set1->Print("v");
90// if (set2) set2->Print("v");
91 //if (self) self->Print("v");
92
93 RooArgSet set1d;
94 RooArgSet set2d;
95 if (self) {
96 if(set1) self->getObservables(set1,set1d,false);
97 if(set2) self->getObservables(set2,set2d,false);
98 } else {
99 if(set1) set1->snapshot(set1d);
100 if(set2) set2->snapshot(set2d);
101 }
102
103// cout << "RooNormSetCache::autoCache set1d = " << *set1d << " set2 = " << *set2d << endl;
104
106
107 if ( getColonSeparatedNameString(set1d) == _name1
108 && getColonSeparatedNameString(set2d) == _name2
109 && _set2RangeName == set2RangeName) {
110 // Compatible - Add current set1/2 to cache
111 add(set1,set2);
112
113 return false;
114 }
115
116 // C - Reset cache and refill with current state
117 if (doRefill) {
118 clear();
119 add(set1,set2);
120 _name1 = getColonSeparatedNameString(set1d);
121 _name2 = getColonSeparatedNameString(set2d);
122// cout << "RooNormSetCache::autoCache() _name1 refilled from " << *set1d << " to " ; _name1.printValue(cout) ; cout << endl;
123// cout << "RooNormSetCache::autoCache() _name2 refilled from " << *set2d << " to " ; _name2.printValue(cout) ; cout << endl;
124 _set2RangeName = (TNamed*) set2RangeName;
125 }
126
127 return true;
128}
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
void clear()
Clear contents.
TNamed * _set2RangeName
!
bool autoCache(const RooAbsArg *self, const RooArgSet *set1, const RooArgSet *set2=nullptr, const TNamed *set2RangeName=nullptr, bool autoRefill=true)
If RooArgSets set1 and set2 or sets with similar contents have been seen by this cache manager before...
std::string _name1
!
std::set< Pair_t > _pairSet
!
std::size_t _max
!
std::deque< Pair_t > _pairs
!
bool contains(const RooArgSet *set1, const RooArgSet *set2=nullptr, const TNamed *set2RangeName=nullptr)
std::string _name2
!
void add(const RooArgSet *set1, const RooArgSet *set2=nullptr)
Add given pair of RooArgSet pointers to our store.
std::pair< Value_t, Value_t > Pair_t
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
UniqueId_t const & getUniqueId(Class const *ptr)
A helper function to replace pointer comparisons with UniqueId comparisons.
Definition UniqueId.h:89
std::string getColonSeparatedNameString(RooArgSet const &argSet)
Create a string with all sorted names of RooArgSet elements separated by colons.