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 truly 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
37#include "RooFitImplHelpers.h"
38
39////////////////////////////////////////////////////////////////////////////////
40/// Clear contents
41
43{
44 _pairSet.clear();
45 _pairs.clear();
46}
47
48////////////////////////////////////////////////////////////////////////////////
49/// Add given pair of RooArgSet pointers to our store
50
51void RooNormSetCache::add(const RooArgSet* set1, const RooArgSet* set2)
52{
53 const Pair_t pair{RooFit::getUniqueId(set1), RooFit::getUniqueId(set2)};
54 auto it = _pairSet.find(pair);
55 if (it != _pairSet.end()) {
56 // not empty, and keys match - nothing to do
57 return;
58 }
59 // register pair -> index mapping
60 _pairSet.emplace(pair);
61 // save pair at that index
62 _pairs.emplace_back(pair);
63 // if the cache grew too large, start replacing in a round-robin fashion
64 while (_pairs.size() > _max) {
65 _pairSet.erase(_pairs.front());
66 _pairs.pop_front();
67 }
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// If RooArgSets set1 and set2 or sets with similar contents have
72/// been seen by this cache manager before return `false` If not,
73/// return `true`. If sets have not been seen and doRefill is true,
74/// update cache reference to current input sets.
75
76bool RooNormSetCache::autoCache(const RooAbsArg* self, const RooArgSet* set1,
77 const RooArgSet* set2, const TNamed* set2RangeName, bool doRefill)
78{
79
80 // Automated cache management function - Returns `true` if cache is invalidated
81
82 // A - Check if set1/2 are in cache and range name is identical
83 if (set2RangeName == _set2RangeName && contains(set1,set2)) {
84 return false ;
85 }
86
87 // B - Check if dependents(set1/set2) are compatible with current cache
88
89 RooArgSet set1d;
90 RooArgSet set2d;
91 if (self) {
92 if(set1) self->getObservables(set1,set1d,false);
93 if(set2) self->getObservables(set2,set2d,false);
94 } else {
95 if(set1) set1->snapshot(set1d);
96 if(set2) set2->snapshot(set2d);
97 }
98
100
101 if ( getColonSeparatedNameString(set1d) == _name1
102 && getColonSeparatedNameString(set2d) == _name2
103 && _set2RangeName == set2RangeName) {
104 // Compatible - Add current set1/2 to cache
105 add(set1,set2);
106
107 return false;
108 }
109
110 // C - Reset cache and refill with current state
111 if (doRefill) {
112 clear();
113 add(set1,set2);
114 _name1 = getColonSeparatedNameString(set1d);
115 _name2 = getColonSeparatedNameString(set2d);
116 _set2RangeName = (TNamed*) set2RangeName;
117 }
118
119 return true;
120}
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
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, char delim=':')