ROOT  6.06/09
Reference Guide
RooNumRunningInt.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 //
14 // BEGIN_HTML
15 // Class RooNumRunningInt is an implementation of RooAbsCachedReal that represents a running integral
16 // <pre>
17 // RI(f(x)) = Int[x_lo,x] f(x') dx'
18 // </pre>
19 // that is calculated internally with a numeric technique: The input function
20 // is first sampled into a histogram, which is then numerically integrated.
21 // The output function is an interpolated version of the integrated histogram.
22 // The sampling density is controlled by the binning named "cache" in the observable x.
23 // The shape of the p.d.f is always calculated for the entire domain in x and
24 // cached in a histogram. The cache histogram is automatically recalculated
25 // when any of the parameters of the input p.d.f. has changed.
26 // END_HTML
27 //
28 
29 #include "Riostream.h"
30 
31 #include "RooAbsPdf.h"
32 #include "RooNumRunningInt.h"
33 #include "RooAbsReal.h"
34 #include "RooMsgService.h"
35 #include "RooDataHist.h"
36 #include "RooHistPdf.h"
37 #include "RooRealVar.h"
38 
39 using namespace std;
40 
42  ;
43 
44 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Construct running integral of function '_func' over x_print from
48 /// the lower bound on _x to the present value of _x using a numeric
49 /// sampling technique. The sampling frequency is controlled by the
50 /// binning named 'bname' and a default second order interpolation
51 /// is applied to smooth the histogram-based c.d.f.
52 
53 RooNumRunningInt::RooNumRunningInt(const char *name, const char *title, RooAbsReal& _func, RooRealVar& _x, const char* bname) :
54  RooAbsCachedReal(name,title),
55  func("func","func",this,_func),
56  x("x","x",this,_x),
57  _binningName(bname?bname:"cache")
58  {
60  }
61 
62 
63 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Copy constructor
67 
69  RooAbsCachedReal(other,name),
70  func("func",this,other.func),
71  x("x",this,other.x),
72  _binningName(other._binningName)
73  {
74  }
75 
76 
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Destructor
80 
82 {
83 }
84 
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Return unique name for RooAbsCachedPdf cache components
88 /// constructed from input function name
89 
90 const char* RooNumRunningInt::inputBaseName() const
91 {
92  static string ret ;
93  ret = func.arg().GetName() ;
94  ret += "_NUMRUNINT" ;
95  return ret.c_str() ;
96 } ;
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Construct RunningIntegral CacheElement
102 
104  FuncCacheElem(self,nset), _self(&const_cast<RooNumRunningInt&>(self))
105 {
106  // Instantiate temp arrays
107  _ax = new Double_t[hist()->numEntries()] ;
108  _ay = new Double_t[hist()->numEntries()] ;
109 
110  // Copy X values from histo
111  _xx = (RooRealVar*) hist()->get()->find(self.x.arg().GetName()) ;
112  for (int i=0 ; i<hist()->numEntries() ; i++) {
113  hist()->get(i) ;
114  _ax[i] = _xx->getVal() ;
115  _ay[i] = -1 ;
116  }
117 
118 }
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Destructor
123 
125 {
126  // Delete temp arrays
127  delete[] _ax ;
128  delete[] _ay ;
129 }
130 
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Return all RooAbsArg components contained in cache element
134 
136 {
137  RooArgList ret ;
138  ret.add(FuncCacheElem::containedArgs(action)) ;
139  ret.add(*_self) ;
140  ret.add(*_xx) ;
141  return ret ;
142 }
143 
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Calculate the numeric running integral and store
148 /// the result in the cache histogram provided
149 /// by RooAbsCachedPdf
150 
152 {
153  // Update contents of histogram
154  Int_t nbins = hist()->numEntries() ;
155 
156  Double_t xsave = _self->x ;
157 
158  Int_t lastHi=0 ;
159  Int_t nInitRange=32 ;
160  for (int i=1 ; i<=nInitRange ; i++) {
161  Int_t hi = (i*nbins)/nInitRange -1 ;
162  Int_t lo = lastHi ;
163  addRange(lo,hi,nbins) ;
164  lastHi=hi ;
165  }
166 
167  // Perform numeric integration
168  for (int i=1 ; i<nbins ; i++) {
169  _ay[i] += _ay[i-1] ;
170  }
171 
172  // Normalize and transfer to cache histogram
173  Double_t binv = (_self->x.max()-_self->x.min())/nbins ;
174  for (int i=0 ; i<nbins ; i++) {
175  hist()->get(i) ;
176  if (cdfmode) {
177  hist()->set(_ay[i]/_ay[nbins-1]) ;
178  } else {
179  hist()->set(_ay[i]*binv) ;
180  }
181  }
182 
183  if (cdfmode) {
184  func()->setCdfBoundaries(kTRUE) ;
185  }
186  _self->x = xsave ;
187 }
188 
189 
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Fill all empty histogram bins in the range [ixlo,ixhi] where nbins is the
193 /// total number of histogram bins. This method samples the mid-point of the
194 /// range and if the mid-point value is within small tolerance of the interpolated
195 /// mid-point value fills all remaining elements through linear interpolation.
196 /// If the tolerance is exceeded, the algorithm is recursed on the two subranges
197 /// [xlo,xmid] and [xmid,xhi]
198 
200 {
201  // Add first and last point, if not there already
202  if (_ay[ixlo]<0) {
203  addPoint(ixlo) ;
204  }
205  if (_ay[ixhi]<0) {
206  addPoint(ixhi) ;
207  }
208 
209  // Terminate here if there is no gap
210  if (ixhi-ixlo==1) {
211  return ;
212  }
213 
214  // If gap size is one, simply fill gap and return
215  if (ixhi-ixlo==2) {
216  addPoint(ixlo+1) ;
217  return ;
218  }
219 
220  // Add mid-point
221  Int_t ixmid = (ixlo+ixhi)/2 ;
222  addPoint(ixmid) ;
223 
224  // Calculate difference of mid-point w.r.t interpolated value
225  Double_t yInt = _ay[ixlo] + (_ay[ixhi]-_ay[ixlo])*(ixmid-ixlo)/(ixhi-ixlo) ;
226 
227  // If relative deviation is greater than tolerance divide and iterate
228  if (fabs(yInt-_ay[ixmid])*(_ax[nbins-1]-_ax[0])>1e-6) {
229  addRange(ixlo,ixmid,nbins) ;
230  addRange(ixmid,ixhi,nbins) ;
231  } else {
232  for (Int_t j=ixlo+1 ; j<ixmid ; j++) {
233  _ay[j] = _ay[ixlo] + (_ay[ixmid]-_ay[ixlo])*(j-ixlo)/(ixmid-ixlo) ;
234  }
235  for (Int_t j=ixmid+1 ; j<ixhi ; j++) {
236  _ay[j] = _ay[ixmid] + (_ay[ixhi]-_ay[ixmid])*(j-ixmid)/(ixhi-ixmid) ;
237  }
238  }
239 
240 }
241 
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Sample function at bin ix
245 
247 {
248  hist()->get(ix) ;
249  _self->x = _xx->getVal() ;
250  _ay[ix] = _self->func.arg().getVal(*_xx) ;
251 
252 }
253 
254 
255 
256 ////////////////////////////////////////////////////////////////////////////////
257 /// Fill the cache object by calling its calculate() method
258 
260 {
261  RICacheElem& riCache = static_cast<RICacheElem&>(cache) ;
262  riCache.calculate(kFALSE) ;
263 }
264 
265 
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Return observable in nset to be cached by RooAbsCachedPdf
269 /// this is always the x observable that is integrated
270 
272 {
273  RooArgSet* ret = new RooArgSet ;
274  ret->add(x.arg()) ;
275  return ret ;
276 }
277 
278 
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// Return the parameters of the cache created by RooAbsCachedPdf.
282 /// These are always the input functions parameter, but never the
283 /// integrated variable x.
284 
286 {
288  ret->remove(x.arg(),kTRUE,kTRUE) ;
289  return ret ;
290 }
291 
292 
293 ////////////////////////////////////////////////////////////////////////////////
294 /// Create custom cache element for running integral calculations
295 
297 {
298  return new RICacheElem(*const_cast<RooNumRunningInt*>(this),nset) ;
299 }
300 
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// Dummy function that is never called
304 
306 {
307  cout << "RooNumRunningInt::evaluate(" << GetName() << ")" << endl ;
308  return 0 ;
309 }
310 
311 
void setInterpolationOrder(Int_t order)
Set interpolation order of RooHistFunct representing cache histogram.
RooNumRunningInt(const char *name, const char *title, RooAbsReal &_func, RooRealVar &_x, const char *binningName="cache")
Construct running integral of function '_func' over x_print from the lower bound on _x to the present...
virtual RooArgSet * actualObservables(const RooArgSet &nset) const
Return observable in nset to be cached by RooAbsCachedPdf this is always the x observable that is int...
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
void addRange(Int_t ixlo, Int_t ixhi, Int_t nbins)
Fill all empty histogram bins in the range [ixlo,ixhi] where nbins is the total number of histogram b...
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
int nbins[3]
STL namespace.
ClassImp(RooNumRunningInt)
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
Double_t x[n]
Definition: legend1.C:17
virtual FuncCacheElem * createCache(const RooArgSet *nset) const
Create custom cache element for running integral calculations.
friend class RICacheElem
friend class RooArgSet
Definition: RooAbsArg.h:469
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
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't match any of...
Definition: RooAbsArg.cxx:559
virtual ~RooNumRunningInt()
Destructor.
virtual void fillCacheObject(FuncCacheElem &cacheFunc) const
Fill the cache object by calling its calculate() method.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
RooRealProxy func
return
Definition: TBase64.cxx:62
void addPoint(Int_t ix)
Sample function at bin ix.
virtual RooArgList containedArgs(Action)
Return list of contained RooAbsArg objects.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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
void calculate(Bool_t cdfmode)
Calculate the numeric running integral and store the result in the cache histogram provided by RooAbs...
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual const char * inputBaseName() const
Return unique name for RooAbsCachedPdf cache components constructed from input function name...
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
virtual RooArgSet * actualParameters(const RooArgSet &nset) const
Return the parameters of the cache created by RooAbsCachedPdf.
RICacheElem(const RooNumRunningInt &ri, const RooArgSet *nset)
Construct RunningIntegral CacheElement.
float type_of_call hi(const int &, const int &)
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual RooArgList containedArgs(Action)
Return all RooAbsArg components contained in cache element.
virtual Double_t evaluate() const
Dummy function that is never called.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448