Logo ROOT   6.10/09
Reference Guide
RooCachedReal.cxx
Go to the documentation of this file.
1  /*****************************************************************************
2  * Project: RooFit *
3  * *
4  * Copyright (c) 2000-2005, Regents of the University of California *
5  * and Stanford University. All rights reserved. *
6  * *
7  * Redistribution and use in source and binary forms, *
8  * with or without modification, are permitted according to the terms *
9  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
10  *****************************************************************************/
11 
12 /**
13 \file RooCachedReal.cxx
14 \class RooCachedReal
15 \ingroup Roofitcore
16 
17 RooCachedReal is an implementation of RooAbsCachedReal that can cache
18 any external RooAbsReal input function provided in the constructor.
19 **/
20 
21 #include "Riostream.h"
22 
23 #include "RooAbsPdf.h"
24 #include "RooCachedReal.h"
25 #include "RooAbsReal.h"
26 #include "RooAbsCategory.h"
27 #include "RooMsgService.h"
28 #include "RooDataHist.h"
29 #include "RooHistPdf.h"
30 #include "RooChangeTracker.h"
31 
32 using namespace std;
33 
35  ;
36 
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Constructor taking name, title and function to be cached. To control
40 /// granularity of the binning of the cache histogram set the desired properties
41 /// in the binning named "cache" in the observables of the function
42 
43 RooCachedReal::RooCachedReal(const char *name, const char *title, RooAbsReal& _func) :
44  RooAbsCachedReal(name,title),
45  func("func","func",this,_func),
46  _useCdfBoundaries(kFALSE),
47  _cacheSource(kFALSE)
48  {
49  // Choose same expensive object cache as input function
51  }
52 
53 
54 
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Constructor taking name, title and function to be cached and
58 /// fixed choice of variable to cache. To control granularity of the
59 /// binning of the cache histogram set the desired properties in the
60 /// binning named "cache" in the observables of the function.
61 /// If the fixed set of cache observables does not match the observables
62 /// defined in the use context of the p.d.f the cache is still filled
63 /// completely. Ee.g. when it is specified to cache x and p and only x
64 /// is a observable in the given use context the cache histogram will
65 /// store sampled values for all values of observable x and parameter p.
66 /// In such a mode of operation the cache will also not be recalculated
67 /// if the observable p changes
68 
69 RooCachedReal::RooCachedReal(const char *name, const char *title, RooAbsReal& _func, const RooArgSet& cacheObs) :
70  RooAbsCachedReal(name,title),
71  func("func","func",this,_func),
72  _cacheObs("cacheObs","cacheObs",this,kFALSE,kFALSE),
75  {
76  _cacheObs.add(cacheObs) ;
77 
78  // Choose same expensive object cache as input function
80  }
81 
82 
83 
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Copy constructor
87 
88 RooCachedReal::RooCachedReal(const RooCachedReal& other, const char* name) :
89  RooAbsCachedReal(other,name),
90  func("func",this,other.func),
91  _cacheObs("cacheObs",this,other._cacheObs),
94  {
95  }
96 
97 
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// Destructor
101 
103 {
104 }
105 
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Interface function to create an internal cache object that represent
109 /// each cached function configuration. This interface allows to create and
110 /// return a class derived from RooAbsCachedReal::FuncCacheElem so that
111 /// a derived class fillCacheObject implementation can utilize extra functionality
112 /// defined in such a derived cache class
113 
115 {
117  if (_cacheSource) {
118  ret->setCacheSource(kTRUE) ;
119  }
120  return ret ;
121 }
122 
123 
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Update contents of cache histogram by resampling the input function
127 
129 {
130  unsigned nDim = cache.hist()->get()->getSize();
131  if (nDim>1) {
132  RooFIter iter = cache.hist()->get()->fwdIterator();
133  RooAbsArg* arg ;
134  unsigned nCat(0);
135  while((arg=iter.next())) if (dynamic_cast<RooAbsCategory*>(arg)) ++nCat;
136  if (nDim>nCat+1) {
137  coutP(Eval) << "RooCachedReal::fillCacheObject(" << GetName() << ") filling "
138  << nCat << " + " << nDim-nCat <<" dimensional cache (" << cache.hist()->numEntries() << " points)" <<endl;
139  }
140  }
141 
142  // Make deep clone of self and attach to dataset observables
143  if (!cache.sourceClone()) {
144  RooAbsArg* sourceClone = func.arg().cloneTree() ;
145  cache.setSourceClone((RooAbsReal*)sourceClone) ;
146  cache.sourceClone()->recursiveRedirectServers(*cache.hist()->get()) ;
148  }
149 
150  // Iterator over all bins of RooDataHist and fill weights
151  for (Int_t i=0 ; i<cache.hist()->numEntries() ; i++) {
152  const RooArgSet* obs = cache.hist()->get(i) ;
153  Double_t binVal = cache.sourceClone()->getVal(obs) ;
154  cache.hist()->set(binVal) ;
155  }
156 
157  // Delete source clone if we don't cache it
158  if (!cache.cacheSource()) {
159  cache.setSourceClone(0) ;
160  }
161 
163 
164 }
165 
166 
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// If this pdf is operated with a fixed set of observables, return
170 /// the subset of the fixed observables that are actual dependents
171 /// of the external input p.d.f. If this p.d.f is operated without
172 /// a fixed set of cache observables, return the actual observables
173 /// of the external input p.d.f given the choice of observables defined
174 /// in nset
175 
177 {
178  if (_cacheObs.getSize()>0) {
179  return func.arg().getObservables(_cacheObs) ;
180  }
181 
182  return func.arg().getObservables(nset) ;
183 }
184 
185 
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// If this p.d.f is operated with a fixed set of observables, return
189 /// all variables of the external input p.d.f that are not one of
190 /// the cache observables. If this p.d.f is operated in automatic mode,
191 /// return the parameters of the external input p.d.f
192 
194 {
195  if (_cacheObs.getSize()>0) {
196  return func.arg().getParameters(_cacheObs) ;
197  }
198  return func.arg().getParameters(nset) ;
199 }
200 
201 
203 {
204  if (operMode()==ADirty) {
206  }
207 }
208 
209 
210 
211 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual RooArgSet * actualParameters(const RooArgSet &nset) const
If this p.d.f is operated with a fixed set of observables, return all variables of the external input...
virtual RooAbsArg * cloneTree(const char *newname=0) const
Clone tree expression of objects.
Definition: RooAbsArg.cxx:2296
virtual const RooArgSet * get() const
Definition: RooDataHist.h:77
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Definition: RooAbsArg.h:194
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
void set(Double_t weight, Double_t wgtErr=-1)
Increment the weight of the bin enclosing the coordinates given by &#39;row&#39; by the specified amount...
int Int_t
Definition: RtypesCore.h:41
STL namespace.
#define coutP(a)
Definition: RooMsgService.h:32
virtual ~RooCachedReal()
Destructor.
Bool_t _useCdfBoundaries
Definition: RooCachedReal.h:68
RooChangeTracker * paramTracker()
RooRealProxy func
Definition: RooCachedReal.h:66
virtual FuncCacheElem * createCache(const RooArgSet *nset) const
Interface function to create an internal cache object that represent each cached function configurati...
Int_t getSize() const
RooArgSet parameters() const
RooAbsCachedReal is the abstract base class for functions that need or want to cache their evaluate()...
RooExpensiveObjectCache & expensiveObjectCache() const
Definition: RooAbsArg.cxx:2342
RooSetProxy _cacheObs
Definition: RooCachedReal.h:67
void setCdfBoundaries(Bool_t flag)
Definition: RooHistFunc.h:60
RooCachedReal is an implementation of RooAbsCachedReal that can cache any external RooAbsReal input f...
Definition: RooCachedReal.h:20
RooAbsArg * absArg() const
Definition: RooArgProxy.h:37
OperMode operMode() const
Definition: RooAbsArg.h:399
RooAbsArg * next()
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition: RooAbsArg.h:497
#define ClassImp(name)
Definition: Rtypes.h:336
virtual Int_t numEntries() const
Return the number of bins.
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooFIter fwdIterator() const
RooArgSet * getParameters(const RooAbsData *data, Bool_t stripDisconnected=kTRUE) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don&#39;t match any of...
Definition: RooAbsArg.cxx:560
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Bool_t _cacheSource
Definition: RooCachedReal.h:69
void setSourceClone(RooAbsReal *newSource)
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Change cache operation mode to given mode.
Definition: RooAbsArg.cxx:1754
virtual FuncCacheElem * createCache(const RooArgSet *nset) const
Interface function to create an internal cache object that represent each cached function configurati...
virtual RooArgSet * actualObservables(const RooArgSet &nset) const
If this pdf is operated with a fixed set of observables, return the subset of the fixed observables t...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1088
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Overloaded RooArgSet::add() method inserts &#39;var&#39; into set and registers &#39;var&#39; as server to owner with...
virtual void fillCacheObject(FuncCacheElem &cacheFunc) const
Update contents of cache histogram by resampling the input function.