Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooParametricStepFunction.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitBabar *
4 * @(#)root/roofit:$Id$
5 * Authors: *
6 * Aaron Roodman, Stanford Linear Accelerator Center, Stanford University *
7 * *
8 * Copyright (c) 2004, Stanford University. All rights reserved. *
9 *
10 * Redistribution and use in source and binary forms, *
11 * with or without modification, are permitted according to the terms *
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
13 *****************************************************************************/
14
15/** \class RooParametricStepFunction
16 \ingroup Roofit
17
18The Parametric Step Function PDF is a binned distribution whose parameters
19are the heights of each bin. This PDF was first used in BaBar's B0->pi0pi0
20paper BABAR Collaboration (B. Aubert et al.) Phys.Rev.Lett.91:241801,2003.
21
22This PDF may be used to describe oddly shaped distributions. It differs
23from a RooKeysPdf or a RooHistPdf in that a RooParametricStepFunction
24has free parameters. In particular, any statistical uncertainty in
25sample used to model this PDF may be understood with these free parameters;
26this is not possible with non-parametric PDFs.
27
28The RooParametricStepFunction has Nbins-1 free parameters. Note that
29the limits of the dependent variable must match the low and hi bin limits.
30
31Here is an example showing how to use the RooParametricStepFunction to fit toy
32data generated from a uniform distribution:
33
34~~~ {.cpp}
35// Define some constant parameters
36const int nBins = 10;
37const double xMin = 0.0;
38const double xMax = 10.0;
39const double binWidth = (xMax - xMin) / nBins;
40const std::size_t nEvents = 10000;
41
42// Fill the bin boundaries
43std::vector<double> binBoundaries(nBins + 1);
44
45for(int i = 0; i <= nBins; ++i) {
46 binBoundaries[i] = i * binWidth;
47}
48
49// The RooParametricStepFunction needs a TArrayD
50TArrayD binBoundariesTArr{int(binBoundaries.size()), binBoundaries.data()};
51
52RooRealVar x{"x", "x", xMin, xMax};
53
54// There are nBins-1 free coefficient parameters, whose sum must be <= 1.0.
55// We all set them to 0.1, such that the resulting step function pdf is
56// initially uniform.
57RooArgList coefList;
58for(int i = 0; i < nBins - 1; ++i) {
59 auto name = std::string("coef_") + std::to_string(i);
60 coefList.addOwned(std::make_unique<RooRealVar>(name.c_str(), name.c_str(), 0.1, 0.0, 1.0));
61}
62
63// Create the RooParametricStepFunction pdf
64RooParametricStepFunction pdf{"pdf", "pdf", x, coefList, binBoundariesTArr, int(nBins)};
65
66// Generate some toy data, following our uniform step function pdf
67std::unique_ptr<RooAbsData> data{pdf.generate(x, nEvents)};
68
69// Fit the step function to the toy data
70pdf.fitTo(*data);
71
72// Now we plot the data and the pdf, the latter should not be uniform
73// anymore because the coefficients were fit to the toy data
74RooPlot *xframe = x.frame(RooFit::Title("Fitting uniform toy data with a step function p.d.f."));
75data->plotOn(xframe);
76pdf.plotOn(xframe);
77xframe->Draw();
78~~~
79*/
80
81#include "Riostream.h"
82#include "TArrayD.h"
83#include <math.h>
84
86#include "RooRealVar.h"
87#include "RooArgList.h"
88
89#include "TError.h"
90
92
93////////////////////////////////////////////////////////////////////////////////
94/// Constructor
95
97 RooAbsReal& x, const RooArgList& coefList, TArrayD& limits, Int_t nBins) :
98 RooAbsPdf(name, title),
99 _x("x", "Dependent", this, x),
100 _coefList("coefList","List of coefficients",this),
101 _nBins(nBins)
102{
103
104 // Check lowest order
105 if (_nBins<0) {
106 std::cout << "RooParametricStepFunction::ctor(" << GetName()
107 << ") WARNING: nBins must be >=0, setting value to 0" << std::endl ;
108 _nBins=0 ;
109 }
110
111 for (auto *coef : coefList) {
112 if (!dynamic_cast<RooAbsReal*>(coef)) {
113 std::cout << "RooParametricStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
114 << " is not of type RooAbsReal" << std::endl ;
115 R__ASSERT(0) ;
116 }
117 _coefList.add(*coef) ;
118 }
119
120 // Bin limits
121 limits.Copy(_limits);
122
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Copy constructor
127
129 RooAbsPdf(other, name),
130 _x("x", this, other._x),
131 _coefList("coefList",this,other._coefList),
132 _nBins(other._nBins)
133{
134 (other._limits).Copy(_limits);
135}
136
137////////////////////////////////////////////////////////////////////////////////
138
139Int_t RooParametricStepFunction::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
140{
141 if (matchArgs(allVars, analVars, _x)) return 1;
142 return 0;
143}
144
145////////////////////////////////////////////////////////////////////////////////
146
147double RooParametricStepFunction::analyticalIntegral(Int_t code, const char* rangeName) const
148{
149 R__ASSERT(code==1) ;
150
151 // Case without range is trivial: p.d.f is by construction normalized
152 if (!rangeName) {
153 return 1.0 ;
154 }
155
156 // Case with ranges, calculate integral explicitly
157 double xmin = _x.min(rangeName) ;
158 double xmax = _x.max(rangeName) ;
159
160 double sum=0 ;
161 Int_t i ;
162 for (i=1 ; i<=_nBins ; i++) {
163 double binVal = (i<_nBins) ? (static_cast<RooRealVar*>(_coefList.at(i-1))->getVal()) : lastBinValue() ;
164 if (_limits[i-1]>=xmin && _limits[i]<=xmax) {
165 // Bin fully in the integration domain
166 sum += (_limits[i]-_limits[i-1])*binVal ;
167 } else if (_limits[i-1]<xmin && _limits[i]>xmax) {
168 // Domain is fully contained in this bin
169 sum += (xmax-xmin)*binVal ;
170 // Exit here, this is the last bin to be processed by construction
171 return sum ;
172 } else if (_limits[i-1]<xmin && _limits[i]<=xmax && _limits[i]>xmin) {
173 // Lower domain boundary is in bin
174 sum += (_limits[i]-xmin)*binVal ;
175 } else if (_limits[i-1]>=xmin && _limits[i]>xmax && _limits[i-1]<xmax) {
176 sum += (xmax-_limits[i-1])*binVal ;
177 // Upper domain boundary is in bin
178 // Exit here, this is the last bin to be processed by construction
179 return sum ;
180 }
181 }
182
183 return sum;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187
189{
190 double sum(0.);
191 double binSize(0.);
192 for (Int_t j=1;j<_nBins;j++){
193 RooRealVar* tmp = (RooRealVar*) _coefList.at(j-1);
194 binSize = _limits[j] - _limits[j-1];
195 sum = sum + tmp->getVal()*binSize;
196 }
197 binSize = _limits[_nBins] - _limits[_nBins-1];
198 return (1.0 - sum)/binSize;
199}
200
201////////////////////////////////////////////////////////////////////////////////
202
204{
205 double value(0.);
206 if (_x >= _limits[0] && _x < _limits[_nBins]){
207
208 for (Int_t i=1;i<=_nBins;i++){
209 if (_x < _limits[i]){
210 // in Bin i-1 (starting with Bin 0)
211 if (i<_nBins) {
212 // not in last Bin
213 RooRealVar* tmp = (RooRealVar*) _coefList.at(i-1);
214 value = tmp->getVal();
215 break;
216 } else {
217 // in last Bin
218 double sum(0.);
219 double binSize(0.);
220 for (Int_t j=1;j<_nBins;j++){
221 RooRealVar* tmp = (RooRealVar*) _coefList.at(j-1);
222 binSize = _limits[j] - _limits[j-1];
223 sum = sum + tmp->getVal()*binSize;
224 }
225 binSize = _limits[_nBins] - _limits[_nBins-1];
226 value = (1.0 - sum)/binSize;
227 if (value<=0.0){
228 value = 0.000001;
229 // cout << "RooParametricStepFunction: sum of values gt 1.0 -- beware!!" <<endl;
230 }
231 break;
232 }
233 }
234 }
235
236 }
237 return value;
238
239}
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Definition TError.h:117
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
bool matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
The Parametric Step Function PDF is a binned distribution whose parameters are the heights of each bi...
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:40
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
double min(const char *rname=nullptr) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
Array of doubles (64 bits per element).
Definition TArrayD.h:27
void Copy(TArrayD &array) const
Definition TArrayD.h:42
void Copy(TObject &named) const override
Copy this to obj.
Definition TNamed.cxx:94
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Double_t x[n]
Definition legend1.C:17
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345