ROOT  6.06/09
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 //////////////////////////////////////////////////////////////////////////////
18 //
19 // The Step Function is a binned function whose parameters
20 // are the heights of each bin.
21 //
22 // This function may be used to describe oddly shaped distributions. A RooStepFunction
23 // has free parameters. In particular, any statistical uncertainty
24 // used to model this efficiency may be understood with these free parameters.
25 //
26 // Note that in contrast to RooParametricStepFunction, a RooStepFunction is NOT a PDF,
27 // but a not-normalized function (RooAbsReal)
28 //
29 
30 #include "RooFit.h"
31 
32 #include "Riostream.h"
33 #include "TArrayD.h"
34 #include <math.h>
35 
36 #include "RooStepFunction.h"
37 #include "RooAbsReal.h"
38 #include "RooRealVar.h"
39 #include "RooArgList.h"
40 #include "RooMsgService.h"
41 #include "RooMath.h"
42 
43 using namespace std;
44 
46  ;
47 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Constructor
51 
53 {
54  _coefIter = _coefList.createIterator() ;
55  _boundIter = _boundaryList.createIterator() ;
56  _interpolate = kFALSE ;
57 }
58 
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Constructor
63 
64 RooStepFunction::RooStepFunction(const char* name, const char* title,
65  RooAbsReal& x, const RooArgList& coefList, const RooArgList& boundaryList, Bool_t interpolate) :
66  RooAbsReal(name, title),
67  _x("x", "Dependent", this, x),
68  _coefList("coefList","List of coefficients",this),
69  _boundaryList("boundaryList","List of boundaries",this),
70  _interpolate(interpolate)
71 {
73  TIterator* coefIter = coefList.createIterator() ;
74  RooAbsArg* coef ;
75  while((coef = (RooAbsArg*)coefIter->Next())) {
76  if (!dynamic_cast<RooAbsReal*>(coef)) {
77  cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
78  << " is not of type RooAbsReal" << endl ;
79  assert(0) ;
80  }
81  _coefList.add(*coef) ;
82  }
83  delete coefIter ;
84 
86  TIterator* boundaryIter = boundaryList.createIterator() ;
87  RooAbsArg* boundary ;
88  while((boundary = (RooAbsArg*)boundaryIter->Next())) {
89  if (!dynamic_cast<RooAbsReal*>(boundary)) {
90  cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: boundary " << boundary->GetName()
91  << " is not of type RooAbsReal" << endl ;
92  assert(0) ;
93  }
94  _boundaryList.add(*boundary) ;
95  }
96 
98  coutE(InputArguments) << "RooStepFunction::ctor(" << GetName() << ") ERROR: Number of boundaries must be number of coefficients plus 1" << endl ;
99  throw string("RooStepFunction::ctor() ERROR: Number of boundaries must be number of coefficients plus 1") ;
100  }
101 
102 }
103 
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Copy constructor
108 
110  RooAbsReal(other, name),
111  _x("x", this, other._x),
112  _coefList("coefList",this,other._coefList),
113  _boundaryList("boundaryList",this,other._boundaryList),
114  _interpolate(other._interpolate)
115 {
118 }
119 
120 
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Destructor
124 
126 {
127  delete _coefIter ;
128  delete _boundIter ;
129 }
130 
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Transfer contents to vector for use below
135 
137 {
138  vector<double> b(_boundaryList.getSize()) ;
139  vector<double> c(_coefList.getSize()+3) ;
140  Int_t nb(0) ;
141  _boundIter->Reset() ;
142  RooAbsReal* boundary ;
143  while ((boundary=(RooAbsReal*)_boundIter->Next())) {
144  b[nb++] = boundary->getVal() ;
145  }
146 
147  // Return zero if outside any boundaries
148  if ((_x<b[0]) || (_x>b[nb-1])) return 0 ;
149 
150  if (!_interpolate) {
151 
152  // No interpolation -- Return values bin-by-bin
153  for (Int_t i=0;i<nb-1;i++){
154  if (_x>b[i]&&_x<=b[i+1]) {
155  return ((RooAbsReal*)_coefList.at(i))->getVal() ;
156  }
157  }
158  return 0 ;
159 
160  } else {
161 
162  // Interpolation
163 
164  // Make array of (b[0],bin centers,b[last])
165  c[0] = b[0] ; c[nb] = b[nb-1] ;
166  for (Int_t i=0 ; i<nb-1 ; i++) {
167  c[i+1] = (b[i]+b[i+1])/2 ;
168  }
169 
170  // Make array of (0,coefficient values,0)
171  Int_t nc(0) ;
172  _coefIter->Reset() ;
173  RooAbsReal* coef ;
174  vector<double> y(_coefList.getSize()+3) ;
175  y[nc++] = 0 ;
176  while ((coef=(RooAbsReal*)_coefIter->Next())) {
177  y[nc++] = coef->getVal() ;
178  }
179  y[nc++] = 0 ;
180 
181  for (Int_t i=0;i<nc-1;i++){
182  if (_x>c[i]&&_x<=c[i+1]) {
183  Double_t xx[2] ; xx[0]=c[i] ; xx[1]=c[i+1] ;
184  Double_t yy[2] ; yy[0]=y[i] ; yy[1]=y[i+1] ;
185  return RooMath::interpolate(xx,yy,2,_x) ;
186  }
187  }
188  return 0;
189  }
190 }
191 
static Double_t interpolate(Double_t yArr[], Int_t nOrder, Double_t x)
Definition: RooMath.cxx:609
ClassImp(RooStepFunction)
#define coutE(a)
Definition: RooMsgService.h:35
virtual void Reset()=0
#define assert(cond)
Definition: unittest.h:542
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooListProxy _coefList
const Bool_t kFALSE
Definition: Rtypes.h:92
RooListProxy _boundaryList
STL namespace.
Double_t evaluate() const
Transfer contents to vector for use below.
Iterator abstract base class.
Definition: TIterator.h:32
TIterator * _boundIter
do not persist
Double_t x[n]
Definition: legend1.C:17
TIterator * createIterator(Bool_t dir=kIterForward) const
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
RooRealProxy _x
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
void interpolate(const ROOT::Math::Interpolator &itp, bool drawSame=false)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooStepFunction()
Constructor.
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
Double_t y[n]
Definition: legend1.C:17
#define name(a, b)
Definition: linkTestLib0.cpp:5
TIterator * _coefIter
virtual ~RooStepFunction()
Destructor.
virtual TObject * Next()=0
Int_t getSize() const
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66