Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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\file RooNumRunningInt.cxx
14\class RooNumRunningInt
15\ingroup Roofitcore
16
17Implementation of RooAbsCachedReal that represents a running integral
18\f[ RI(f(x)) = \int_{xlow}^{x} f(x') dx' \f]
19that is calculated internally with a numeric technique: The input function
20is first sampled into a histogram, which is then numerically integrated.
21The output function is an interpolated version of the integrated histogram.
22The sampling density is controlled by the binning named "cache" in the observable x.
23The shape of the p.d.f is always calculated for the entire domain in x and
24cached in a histogram. The cache histogram is automatically recalculated
25when any of the parameters of the input p.d.f. has changed.
26**/
27
28#include "Riostream.h"
29
30#include "RooAbsPdf.h"
31#include "RooNumRunningInt.h"
32#include "RooAbsReal.h"
33#include "RooMsgService.h"
34#include "RooDataHist.h"
35#include "RooHistPdf.h"
36#include "RooRealVar.h"
37
38////////////////////////////////////////////////////////////////////////////////
39/// Construct running integral of function '_func' over x_print from
40/// the lower bound on _x to the present value of _x using a numeric
41/// sampling technique. The sampling frequency is controlled by the
42/// binning named 'bname' and a default second order interpolation
43/// is applied to smooth the histogram-based c.d.f.
44
45RooNumRunningInt::RooNumRunningInt(const char *name, const char *title, RooAbsReal& _func, RooRealVar& _x, const char* bname) :
47 func("func","func",this,_func),
48 x("x","x",this,_x),
49 _binningName(bname?bname:"cache")
50 {
52 }
53
54
55
56
57////////////////////////////////////////////////////////////////////////////////
58/// Copy constructor
59
62 func("func",this,other.func),
63 x("x",this,other.x),
64 _binningName(other._binningName)
65 {
66 }
67
68////////////////////////////////////////////////////////////////////////////////
69/// Return unique name for RooAbsCachedPdf cache components
70/// constructed from input function name
71
73{
74 static std::string ret;
75 ret = func.arg().GetName();
76 ret += "_NUMRUNINT";
77 return ret.c_str();
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Construct RunningIntegral CacheElement
82
84 : FuncCacheElem(self, nset),
86 _xx(*hist()->get()->find(self.x.arg().GetName()))
87{
88 // Instantiate temp arrays
89 _ax.resize(hist()->numEntries());
90 _ay.resize(hist()->numEntries());
91
92 // Copy X values from histo
93
94 for (int i=0 ; i<hist()->numEntries() ; i++) {
95 hist()->get(i) ;
96 _ax[i] = static_cast<RooRealVar *>(_xx.first())->getVal();
97 _ay[i] = -1 ;
98 }
99
100}
101
102
103////////////////////////////////////////////////////////////////////////////////
104/// Return all RooAbsArg components contained in cache element
105
107{
109 ret.add(FuncCacheElem::containedArgs(action)) ;
110 ret.add(*_self) ;
111 ret.add(_xx);
112 return ret ;
113}
114
115
116
117////////////////////////////////////////////////////////////////////////////////
118/// Calculate the numeric running integral and store
119/// the result in the cache histogram provided
120/// by RooAbsCachedPdf
121
123{
124 // Update contents of histogram
125 Int_t nbins = hist()->numEntries() ;
126
127 double xsave = _self->x ;
128
129 Int_t lastHi=0 ;
130 Int_t nInitRange=32 ;
131 for (int i=1 ; i<=nInitRange ; i++) {
132 Int_t hi = (i*nbins)/nInitRange -1 ;
133 Int_t lo = lastHi ;
134 addRange(lo,hi,nbins) ;
135 lastHi=hi ;
136 }
137
138 // Perform numeric integration
139 for (int i=1 ; i<nbins ; i++) {
140 _ay[i] += _ay[i-1] ;
141 }
142
143 // Normalize and transfer to cache histogram
144 double binv = (_self->x.max()-_self->x.min())/nbins ;
145 for (int i=0 ; i<nbins ; i++) {
146 hist()->get(i) ;
147 if (cdfmode) {
148 hist()->set(i, _ay[i]/_ay[nbins-1], 0.);
149 } else {
150 hist()->set(i, _ay[i]*binv, 0.);
151 }
152 }
153
154 if (cdfmode) {
155 func()->setCdfBoundaries(true) ;
156 }
157 _self->x = xsave ;
158}
159
160
161
162////////////////////////////////////////////////////////////////////////////////
163/// Fill all empty histogram bins in the range [ixlo,ixhi] where nbins is the
164/// total number of histogram bins. This method samples the mid-point of the
165/// range and if the mid-point value is within small tolerance of the interpolated
166/// mid-point value fills all remaining elements through linear interpolation.
167/// If the tolerance is exceeded, the algorithm is recursed on the two subranges
168/// [xlo,xmid] and [xmid,xhi]
169
171{
172 // Add first and last point, if not there already
173 if (_ay[ixlo]<0) {
174 addPoint(ixlo) ;
175 }
176 if (_ay[ixhi]<0) {
177 addPoint(ixhi) ;
178 }
179
180 // Terminate here if there is no gap
181 if (ixhi-ixlo==1) {
182 return ;
183 }
184
185 // If gap size is one, simply fill gap and return
186 if (ixhi-ixlo==2) {
187 addPoint(ixlo+1) ;
188 return ;
189 }
190
191 // Add mid-point
192 Int_t ixmid = (ixlo+ixhi)/2 ;
193 addPoint(ixmid) ;
194
195 // Calculate difference of mid-point w.r.t interpolated value
196 double yInt = _ay[ixlo] + (_ay[ixhi]-_ay[ixlo])*(ixmid-ixlo)/(ixhi-ixlo) ;
197
198 // If relative deviation is greater than tolerance divide and iterate
199 if (std::abs(yInt-_ay[ixmid])*(_ax[nbins-1]-_ax[0])>1e-6) {
200 addRange(ixlo,ixmid,nbins) ;
201 addRange(ixmid,ixhi,nbins) ;
202 } else {
203 for (Int_t j=ixlo+1 ; j<ixmid ; j++) {
204 _ay[j] = _ay[ixlo] + (_ay[ixmid]-_ay[ixlo])*(j-ixlo)/(ixmid-ixlo) ;
205 }
206 for (Int_t j=ixmid+1 ; j<ixhi ; j++) {
207 _ay[j] = _ay[ixmid] + (_ay[ixhi]-_ay[ixmid])*(j-ixmid)/(ixhi-ixmid) ;
208 }
209 }
210
211}
212
213
214////////////////////////////////////////////////////////////////////////////////
215/// Sample function at bin ix
216
218{
219 hist()->get(ix) ;
220 _self->x = static_cast<RooRealVar *>(_xx.first())->getVal();
221 _ay[ix] = _self->func.arg().getVal(_xx);
222}
223
224
225
226////////////////////////////////////////////////////////////////////////////////
227/// Fill the cache object by calling its calculate() method
228
230{
231 RICacheElem& riCache = static_cast<RICacheElem&>(cache) ;
232 riCache.calculate(false) ;
233}
234
235
236
237////////////////////////////////////////////////////////////////////////////////
238/// Return observable in nset to be cached by RooAbsCachedPdf
239/// this is always the x observable that is integrated
240
247
248
249
250////////////////////////////////////////////////////////////////////////////////
251/// Return the parameters of the cache created by RooAbsCachedPdf.
252/// These are always the input functions parameter, but never the
253/// integrated variable x.
254
256{
257 auto ret = func->getParameters(RooArgSet()) ;
258 ret->remove(x.arg(),true,true) ;
259 return ret;
260}
261
262
263////////////////////////////////////////////////////////////////////////////////
264/// Create custom cache element for running integral calculations
265
267{
268 return new RICacheElem(*const_cast<RooNumRunningInt*>(this),nset) ;
269}
270
271
272////////////////////////////////////////////////////////////////////////////////
273/// Dummy function that is never called
274
276{
277 std::cout << "RooNumRunningInt::evaluate(" << GetName() << ")" << std::endl ;
278 return 0 ;
279}
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
#define hi
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Abstract base class for functions that need or want to cache their evaluate() output in a RooHistFunc...
void setInterpolationOrder(Int_t order)
Set interpolation order of RooHistFunct representing cache histogram.
RooAbsArg * first() const
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:82
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...
RooArgList containedArgs(Action) override
Return all RooAbsArg components contained in cache element.
void addPoint(Int_t ix)
Sample function at bin ix.
void calculate(bool cdfmode)
Calculate the numeric running integral and store the result in the cache histogram provided by RooAbs...
RICacheElem(const RooNumRunningInt &ri, const RooArgSet *nset)
Construct RunningIntegral CacheElement.
Implementation of RooAbsCachedReal that represents a running integral.
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...
double evaluate() const override
Dummy function that is never called.
const char * inputBaseName() const override
Return unique name for RooAbsCachedPdf cache components constructed from input function name.
RooFit::OwningPtr< RooArgSet > actualParameters(const RooArgSet &nset) const override
Return the parameters of the cache created by RooAbsCachedPdf.
RooRealProxy func
Proxy to functions whose running integral is calculated.
FuncCacheElem * createCache(const RooArgSet *nset) const override
Create custom cache element for running integral calculations.
RooFit::OwningPtr< RooArgSet > actualObservables(const RooArgSet &nset) const override
Return observable in nset to be cached by RooAbsCachedPdf this is always the x observable that is int...
friend class RICacheElem
RooRealProxy x
Integrated observable.
void fillCacheObject(FuncCacheElem &cacheFunc) const override
Fill the cache object by calling its calculate() method.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const T & arg() const
Return reference to object held in proxy.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Double_t x[n]
Definition legend1.C:17
T * OwningPtr
An alias for raw pointers for indicating that the return type of a RooFit function is an owning point...
Definition Config.h:35