Logo 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 * *
10 * Copyright (c) 2009, NIKHEF. All rights reserved. *
11 *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
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 "RooRealVar.h"
37#include "RooArgList.h"
38#include "RooMsgService.h"
39#include "RooMath.h"
40
41using namespace std;
42
44
45////////////////////////////////////////////////////////////////////////////////
46/// Constructor
47
49{
50 _interpolate = false ;
51}
52
53////////////////////////////////////////////////////////////////////////////////
54/// Constructor
55
56RooStepFunction::RooStepFunction(const char* name, const char* title,
57 RooAbsReal& x, const RooArgList& coefList, const RooArgList& boundaryList, bool interpolate) :
58 RooAbsReal(name, title),
59 _x("x", "Dependent", this, x),
60 _coefList("coefList","List of coefficients",this),
61 _boundaryList("boundaryList","List of boundaries",this),
62 _interpolate(interpolate)
63{
64 for (auto *coef : coefList) {
65 if (!dynamic_cast<RooAbsReal*>(coef)) {
66 cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
67 << " is not of type RooAbsReal" << endl ;
68 assert(0) ;
69 }
70 _coefList.add(*coef) ;
71 }
72
73 for (auto *boundary : boundaryList) {
74 if (!dynamic_cast<RooAbsReal*>(boundary)) {
75 cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: boundary " << boundary->GetName()
76 << " is not of type RooAbsReal" << endl ;
77 assert(0) ;
78 }
79 _boundaryList.add(*boundary) ;
80 }
81
83 coutE(InputArguments) << "RooStepFunction::ctor(" << GetName() << ") ERROR: Number of boundaries must be number of coefficients plus 1" << endl ;
84 throw string("RooStepFunction::ctor() ERROR: Number of boundaries must be number of coefficients plus 1") ;
85 }
86
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Copy constructor
91
93 RooAbsReal(other, name),
94 _x("x", this, other._x),
95 _coefList("coefList",this,other._coefList),
96 _boundaryList("boundaryList",this,other._boundaryList),
97 _interpolate(other._interpolate)
98{
99}
100
101
102////////////////////////////////////////////////////////////////////////////////
103/// Transfer contents to vector for use below
104
106{
107 vector<double> b(_boundaryList.getSize()) ;
108 vector<double> c(_coefList.getSize()+3) ;
109 Int_t nb(0) ;
110 for (auto * boundary : static_range_cast<RooAbsReal*>(_boundaryList)) {
111 b[nb++] = boundary->getVal() ;
112 }
113
114 // Return zero if outside any boundaries
115 if ((_x<b[0]) || (_x>b[nb-1])) return 0 ;
116
117 if (!_interpolate) {
118
119 // No interpolation -- Return values bin-by-bin
120 for (Int_t i=0;i<nb-1;i++){
121 if (_x>b[i]&&_x<=b[i+1]) {
122 return ((RooAbsReal*)_coefList.at(i))->getVal() ;
123 }
124 }
125 return 0 ;
126
127 } else {
128
129 // Interpolation
130
131 // Make array of (b[0],bin centers,b[last])
132 c[0] = b[0] ; c[nb] = b[nb-1] ;
133 for (Int_t i=0 ; i<nb-1 ; i++) {
134 c[i+1] = (b[i]+b[i+1])/2 ;
135 }
136
137 // Make array of (0,coefficient values,0)
138 Int_t nc(0) ;
139 vector<double> y(_coefList.size()+3) ;
140 y[nc++] = 0 ;
141 for(auto * coef : static_range_cast<RooAbsReal*>(_coefList)) {
142 y[nc++] = coef->getVal() ;
143 }
144 y[nc++] = 0 ;
145
146 for (Int_t i=0;i<nc-1;i++){
147 if (_x>c[i]&&_x<=c[i+1]) {
148 double xx[2] ; xx[0]=c[i] ; xx[1]=c[i+1] ;
149 double yy[2] ; yy[0]=y[i] ; yy[1]=y[i+1] ;
150 return RooMath::interpolate(xx,yy,2,_x) ;
151 }
152 }
153 return 0;
154 }
155}
#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.
Storage_t::size_type size() const
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:62
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.
RooStepFunction()
Constructor.
RooListProxy _coefList
RooListProxy _boundaryList
double evaluate() const override
Transfer contents to vector for use below.
RooRealProxy _x
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