Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <RooStepFunction.h>
32
33#include <RooArgList.h>
34#include <RooCurve.h>
35#include <RooMsgService.h>
36#include <RooMath.h>
37#include <RooRealVar.h>
38
40
41////////////////////////////////////////////////////////////////////////////////
42/// Constructor
43
44RooStepFunction::RooStepFunction(const char* name, const char* title,
45 RooAbsReal& x, const RooArgList& coefList, const RooArgList& boundaryList, bool interpolate) :
46 RooAbsReal(name, title),
47 _x("x", "Dependent", this, x),
48 _coefList("coefList","List of coefficients",this),
49 _boundaryList("boundaryList","List of boundaries",this),
50 _interpolate(interpolate)
51{
52 for (auto *coef : coefList) {
53 if (!dynamic_cast<RooAbsReal*>(coef)) {
54 std::cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
55 << " is not of type RooAbsReal" << std::endl ;
56 assert(0) ;
57 }
58 _coefList.add(*coef) ;
59 }
60
61 for (auto *boundary : boundaryList) {
62 if (!dynamic_cast<RooAbsReal*>(boundary)) {
63 std::cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: boundary " << boundary->GetName()
64 << " is not of type RooAbsReal" << std::endl;
65 assert(0) ;
66 }
67 _boundaryList.add(*boundary) ;
68 }
69
70 if (_boundaryList.size()!=_coefList.size()+1) {
71 coutE(InputArguments) << "RooStepFunction::ctor(" << GetName() << ") ERROR: Number of boundaries must be number of coefficients plus 1" << std::endl ;
72 throw std::invalid_argument("RooStepFunction::ctor() ERROR: Number of boundaries must be number of coefficients plus 1") ;
73 }
74
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Copy constructor
79
81 RooAbsReal(other, name),
82 _x("x", this, other._x),
83 _coefList("coefList",this,other._coefList),
84 _boundaryList("boundaryList",this,other._boundaryList),
85 _interpolate(other._interpolate)
86{
87}
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// Transfer contents to std::vector for use below
92
94{
95 std::vector<double> b(_boundaryList.size()) ;
96 std::vector<double> c(_coefList.size()+3) ;
97 Int_t nb(0) ;
98 for (auto * boundary : static_range_cast<RooAbsReal*>(_boundaryList)) {
99 b[nb++] = boundary->getVal() ;
100 }
101
102 // Return zero if outside any boundaries
103 if ((_x<b[0]) || (_x>b[nb-1])) return 0 ;
104
105 if (!_interpolate) {
106
107 // No interpolation -- Return values bin-by-bin
108 for (Int_t i=0;i<nb-1;i++){
109 if (_x>b[i]&&_x<=b[i+1]) {
110 return ((RooAbsReal*)_coefList.at(i))->getVal() ;
111 }
112 }
113 return 0 ;
114
115 } else {
116
117 // Interpolation
118
119 // Make array of (b[0],bin centers,b[last])
120 c[0] = b[0] ; c[nb] = b[nb-1] ;
121 for (Int_t i=0 ; i<nb-1 ; i++) {
122 c[i+1] = (b[i]+b[i+1])/2 ;
123 }
124
125 // Make array of (0,coefficient values,0)
126 Int_t nc(0) ;
127 std::vector<double> y(_coefList.size()+3) ;
128 y[nc++] = 0 ;
129 for(auto * coef : static_range_cast<RooAbsReal*>(_coefList)) {
130 y[nc++] = coef->getVal() ;
131 }
132 y[nc++] = 0 ;
133
134 for (Int_t i=0;i<nc-1;i++){
135 if (_x>c[i]&&_x<=c[i+1]) {
136 double xx[2] ; xx[0]=c[i] ; xx[1]=c[i+1] ;
137 double yy[2] ; yy[0]=y[i] ; yy[1]=y[i+1] ;
138 return RooMath::interpolate(xx,yy,2,_x) ;
139 }
140 }
141 return 0;
142 }
143}
144
145
146std::list<double> *RooStepFunction::plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const
147{
148 if (obs.namePtr() != _x->namePtr()) {
149 return nullptr;
150 }
151
152 // Retrieve position of all bin boundaries
153 std::vector<double> boundaries;
155 for (auto *boundary : static_range_cast<RooAbsReal *>(_boundaryList)) {
156 boundaries.push_back(boundary->getVal());
157 }
158
159 // Use the helper function from RooCurve to make sure to get sampling hints
160 // that work with the RooFitPlotting.
162}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
char name[80]
Definition TGX11.cxx:110
const TNamed * namePtr() const
De-duplicated pointer to this object's name.
Definition RooAbsArg.h:563
Storage_t::size_type size() const
void reserve(Storage_t::size_type count)
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
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 std::list< double > * plotSamplingHintForBinBoundaries(std::span< const double > boundaries, double xlo, double xhi)
Returns sampling hints for a histogram with given boundaries.
Definition RooCurve.cxx:897
static double interpolate(double yArr[], Int_t nOrder, double x)
Definition RooMath.cxx:78
The Step Function is a binned function whose parameters are the heights of each bin.
const RooArgList & boundaries()
RooListProxy _coefList
RooListProxy _boundaryList
double evaluate() const override
Transfer contents to std::vector for use below.
std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
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