ROOT   Reference Guide
RooStepFunction.cxx
Go to the documentation of this file.
1
2/*****************************************************************************
3 * Project: RooFit *
4 * Package: RooFitBabar *
5 * @(#)root/roofit:$Id$
6 * Author: *
7 * Tristan du Pree, Nikhef, Amsterdam, tdupree@nikhef.nl *
8 * Wouter Verkerke, Nikhef, Amsterdam, verkerke@nikhef.nl
9 * *
11 *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
15 *****************************************************************************/
16
17/** \class RooStepFunction
18 \ingroup Roofit
19
20The Step Function is a binned function whose parameters
21are the heights of each bin.
22
23This function may be used to describe oddly shaped distributions. A RooStepFunction
24has free parameters. In particular, any statistical uncertainty
25used to model this efficiency may be understood with these free parameters.
26
27Note that in contrast to RooParametricStepFunction, a RooStepFunction is NOT a PDF,
28but a not-normalized function (RooAbsReal)
29**/
30
31#include "Riostream.h"
32#include "TArrayD.h"
33#include <math.h>
34
35#include "RooStepFunction.h"
36#include "RooAbsReal.h"
37#include "RooRealVar.h"
38#include "RooArgList.h"
39#include "RooMsgService.h"
40#include "RooMath.h"
41
42using namespace std;
43
45
46////////////////////////////////////////////////////////////////////////////////
47/// Constructor
48
50{
51 _interpolate = false ;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor
56
57RooStepFunction::RooStepFunction(const char* name, const char* title,
58 RooAbsReal& x, const RooArgList& coefList, const RooArgList& boundaryList, bool interpolate) :
59 RooAbsReal(name, title),
60 _x("x", "Dependent", this, x),
61 _coefList("coefList","List of coefficients",this),
62 _boundaryList("boundaryList","List of boundaries",this),
63 _interpolate(interpolate)
64{
65 for (auto *coef : coefList) {
66 if (!dynamic_cast<RooAbsReal*>(coef)) {
67 cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
68 << " is not of type RooAbsReal" << endl ;
69 assert(0) ;
70 }
72 }
73
74 for (auto *boundary : boundaryList) {
75 if (!dynamic_cast<RooAbsReal*>(boundary)) {
76 cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: boundary " << boundary->GetName()
77 << " is not of type RooAbsReal" << endl ;
78 assert(0) ;
79 }
81 }
82
84 coutE(InputArguments) << "RooStepFunction::ctor(" << GetName() << ") ERROR: Number of boundaries must be number of coefficients plus 1" << endl ;
85 throw string("RooStepFunction::ctor() ERROR: Number of boundaries must be number of coefficients plus 1") ;
86 }
87
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Copy constructor
92
94 RooAbsReal(other, name),
95 _x("x", this, other._x),
96 _coefList("coefList",this,other._coefList),
97 _boundaryList("boundaryList",this,other._boundaryList),
98 _interpolate(other._interpolate)
99{
100}
101
102
103////////////////////////////////////////////////////////////////////////////////
104/// Transfer contents to vector for use below
105
107{
108 vector<double> b(_boundaryList.getSize()) ;
109 vector<double> c(_coefList.getSize()+3) ;
110 Int_t nb(0) ;
111 _boundIter->Reset() ;
112 RooAbsReal* boundary ;
113 while ((boundary=(RooAbsReal*)_boundIter->Next())) {
114 b[nb++] = boundary->getVal() ;
115 }
116
117 // Return zero if outside any boundaries
118 if ((_x<b[0]) || (_x>b[nb-1])) return 0 ;
119
120 if (!_interpolate) {
121
122 // No interpolation -- Return values bin-by-bin
123 for (Int_t i=0;i<nb-1;i++){
124 if (_x>b[i]&&_x<=b[i+1]) {
125 return ((RooAbsReal*)_coefList.at(i))->getVal() ;
126 }
127 }
128 return 0 ;
129
130 } else {
131
132 // Interpolation
133
134 // Make array of (b[0],bin centers,b[last])
135 c[0] = b[0] ; c[nb] = b[nb-1] ;
136 for (Int_t i=0 ; i<nb-1 ; i++) {
137 c[i+1] = (b[i]+b[i+1])/2 ;
138 }
139
140 // Make array of (0,coefficient values,0)
141 Int_t nc(0) ;
142 _coefIter->Reset() ;
143 RooAbsReal* coef ;
144 vector<double> y(_coefList.getSize()+3) ;
145 y[nc++] = 0 ;
146 while ((coef=(RooAbsReal*)_coefIter->Next())) {
147 y[nc++] = coef->getVal() ;
148 }
149 y[nc++] = 0 ;
150
151 for (Int_t i=0;i<nc-1;i++){
152 if (_x>c[i]&&_x<=c[i+1]) {
153 double xx[2] ; xx[0]=c[i] ; xx[1]=c[i+1] ;
154 double yy[2] ; yy[0]=y[i] ; yy[1]=y[i+1] ;
155 return RooMath::interpolate(xx,yy,2,_x) ;
156 }
157 }
158 return 0;
159 }
160}
#define c(i)
Definition: RSha256.hxx:101
#define coutE(a)
Definition: RooMsgService.h:37
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
char name[80]
Definition: TGX11.cxx:110
Int_t getSize() const
Return the number of elements in the collection.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:64
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:94
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
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...
static double interpolate(double yArr[], Int_t nOrder, double x)
Definition: RooMath.cxx:79
The Step Function is a binned function whose parameters are the heights of each bin.
TIterator * _boundIter
do not persist
RooStepFunction()
Constructor.
RooListProxy _coefList
TIterator * _coefIter
RooListProxy _boundaryList
double evaluate() const override
Transfer contents to vector for use below.
RooRealProxy _x
virtual void Reset()=0
virtual TObject * Next()=0
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
@ InputArguments
Definition: RooGlobalFunc.h:64