Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooParamBinning.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. 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\file RooParamBinning.cxx
19\class RooParamBinning
20\ingroup Roofitcore
21
22Implementation of RooAbsBinning that constructs
23a binning with a range definition that depends on external RooAbsReal objects.
24The external RooAbsReal definitions are explicitly allowed to depend on other
25observables and parameters, and make it possible to define non-rectangular
26range definitions in RooFit. Objects of class RooParamBinning are made
27by the RooRealVar::setRange() that takes RooAbsReal references as arguments
28**/
29
30#include "RooParamBinning.h"
31#include "RooMsgService.h"
32
33#include <ostream>
34
35using std::endl, std::ostream;
36
37
38
39////////////////////////////////////////////////////////////////////////////////
40/// Default constructor
41
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Construct binning with 'nBins' bins and with a range
50/// parameterized by external RooAbsReals xloIn and xhiIn.
51
54 _xlo(&xloIn),
55 _xhi(&xhiIn),
56 _nbins(nBins)
57{
58}
59
60
61
62////////////////////////////////////////////////////////////////////////////////
63/// Destructor
64
66{
67 if (_array) delete[] _array ;
68 if (_lp) delete _lp ;
69}
70
71
72
73////////////////////////////////////////////////////////////////////////////////
74/// Copy constructor
75
77{
78
79 if (other._lp) {
80 _xlo = static_cast<RooAbsReal*>(other._lp->at(0)) ;
81 _xhi = static_cast<RooAbsReal*>(other._lp->at(1)) ;
82
83 } else {
84
85 _xlo = other._xlo ;
86 _xhi = other._xhi ;
87 }
88
89 _nbins = other._nbins ;
90 _lp = nullptr ;
91
92}
93
94
95
96////////////////////////////////////////////////////////////////////////////////
97/// Hook function called by RooAbsRealLValue when this binning
98/// is inserted as binning for into given owner. Create
99/// list proxy registered with owner that will track and implement
100/// server directs to external RooAbsReals of this binning
101
103{
104 _owner = &owner ;
105
106 // If list proxy already exists update pointers from proxy
107 if (_lp) {
108 _xlo = xlo() ;
109 _xhi = xhi() ;
110 delete _lp ;
111 }
112
113 // If list proxy does not exist, create it now
114 _lp = new RooListProxy(Form("range::%s",GetName()),"lp",&owner,false,true) ;
115 _lp->add(*_xlo) ;
116 _lp->add(*_xhi) ;
117 _xlo = nullptr ;
118 _xhi = nullptr ;
119
120
121}
122
123
124////////////////////////////////////////////////////////////////////////////////
125/// Hook function called by RooAbsRealLValue when this binning
126/// is removed as binning for into given owner. Delete list
127/// proxy that was inserted in owner
128
130{
131 _owner = nullptr ;
132
133 // Remove list proxy from owner
134 if (_lp) {
135 _xlo = xlo() ;
136 _xhi = xhi() ;
137 delete _lp ;
138 _lp = nullptr ;
139 }
140}
141
142
143
144////////////////////////////////////////////////////////////////////////////////
145/// Adjust range by adjusting values of external RooAbsReal values
146/// Only functional when external representations are lvalues
147
149{
150 if (newxlo>newxhi) {
151 coutE(InputArguments) << "RooParamBinning::setRange: ERROR low bound > high bound" << std::endl ;
152 return ;
153 }
154
155 RooAbsRealLValue* xlolv = dynamic_cast<RooAbsRealLValue*>(xlo()) ;
156 if (xlolv) {
157 xlolv->setVal(newxlo) ;
158 } else {
159 coutW(InputArguments) << "RooParamBinning::setRange: WARNING lower bound not represented by lvalue, cannot set lower bound value through setRange()" << std::endl ;
160 }
161
162 RooAbsRealLValue* xhilv = dynamic_cast<RooAbsRealLValue*>(xhi()) ;
163 if (xhilv) {
164 xhilv->setVal(newxhi) ;
165 } else {
166 coutW(InputArguments) << "RooParamBinning::setRange: WARNING upper bound not represented by lvalue, cannot set upper bound value through setRange()" << std::endl ;
167 }
168
169}
170
171
172
173////////////////////////////////////////////////////////////////////////////////
174/// Return the fit bin index for the current value
175
176void RooParamBinning::binNumbers(double const * x, int * bins, std::size_t n, int coef) const
177{
178 const double xloVal = xlo()->getVal();
179 const double xhiVal = xhi()->getVal();
180 const double oneOverW = 1./averageBinWidth();
181
182 for(std::size_t i = 0; i < n; ++i) {
183 bins[i] += coef * (x[i] >= xhiVal ? _nbins - 1 : std::max(0, int((x[i] - xloVal)*oneOverW)));
184 }
185}
186
187
188
189////////////////////////////////////////////////////////////////////////////////
190/// Return the central value of the 'i'-th fit bin
191
193{
194 if (i<0 || i>=_nbins) {
195 coutE(InputArguments) << "RooParamBinning::binCenter ERROR: bin index " << i
196 << " is out of range (0," << _nbins-1 << ")" << std::endl ;
197 return 0 ;
198 }
199
200 return xlo()->getVal() + (i + 0.5)*averageBinWidth() ;
201}
202
203
204
205
206////////////////////////////////////////////////////////////////////////////////
207/// Return average bin width
208
209double RooParamBinning::binWidth(Int_t /*bin*/) const
210{
211 return (xhi()->getVal()-xlo()->getVal())/_nbins ;
212}
213
214
215
216////////////////////////////////////////////////////////////////////////////////
217/// Return the low edge of the 'i'-th fit bin
218
220{
221 if (i<0 || i>=_nbins) {
222 coutE(InputArguments) << "RooParamBinning::binLow ERROR: bin index " << i
223 << " is out of range (0," << _nbins-1 << ")" << std::endl ;
224 return 0 ;
225 }
226
227 return xlo()->getVal() + i*binWidth(i) ;
228}
229
230
231
232////////////////////////////////////////////////////////////////////////////////
233/// Return the high edge of the 'i'-th fit bin
234
236{
237 if (i<0 || i>=_nbins) {
238 coutE(InputArguments) << "RooParamBinning::fitBinHigh ERROR: bin index " << i
239 << " is out of range (0," << _nbins-1 << ")" << std::endl ;
240 return 0 ;
241 }
242
243 return xlo()->getVal() + (i + 1)*binWidth(i) ;
244}
245
246
247
248////////////////////////////////////////////////////////////////////////////////
249/// Return array of bin boundaries
250
252{
253 if (_array) delete[] _array ;
254 _array = new double[_nbins+1] ;
255
256 Int_t i ;
257 for (i=0 ; i<=_nbins ; i++) {
258 _array[i] = xlo()->getVal() + i*binWidth(i) ;
259 }
260 return _array ;
261}
262
263
264
265////////////////////////////////////////////////////////////////////////////////
266/// Print details of binning
267
268void RooParamBinning::printMultiline(ostream &os, Int_t /*content*/, bool /*verbose*/, TString indent) const
269{
270 os << indent << "_xlo = " << _xlo << std::endl ;
271 os << indent << "_xhi = " << _xhi << std::endl ;
272 if (_lp) {
273 os << indent << "xlo() = " << xlo() << std::endl ;
274 os << indent << "xhi() = " << xhi() << std::endl ;
275 }
276 if (xlo()) {
277 xlo()->Print("t") ;
278 }
279 if (xhi()) {
280 xhi()->Print("t") ;
281 }
282}
RooCollectionProxy< RooArgList > RooListProxy
Definition RooAbsArg.h:51
#define coutW(a)
#define coutE(a)
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2570
void Print(Option_t *options=nullptr) const override
Print the object to the defaultPrintStream().
Definition RooAbsArg.h:238
Abstract base class for RooRealVar binning definitions.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
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...
Implementation of RooAbsBinning that constructs a binning with a range definition that depends on ext...
RooParamBinning(const char *name=nullptr)
Default constructor.
RooAbsReal * xhi() const
RooListProxy * _lp
RooAbsArg * _owner
~RooParamBinning() override
Destructor.
double * _array
! do not persist
RooAbsReal * _xhi
!
void insertHook(RooAbsRealLValue &) const override
Hook function called by RooAbsRealLValue when this binning is inserted as binning for into given owne...
double * array() const override
Return array of bin boundaries.
double binCenter(Int_t bin) const override
Return the central value of the 'i'-th fit bin.
double binHigh(Int_t bin) const override
Return the high edge of the 'i'-th fit bin.
double binLow(Int_t bin) const override
Return the low edge of the 'i'-th fit bin.
void binNumbers(double const *x, int *bins, std::size_t n, int coef) const override
Return the fit bin index for the current value.
double binWidth(Int_t bin) const override
Return average bin width.
double averageBinWidth() const override
RooAbsReal * _xlo
!
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Print details of binning.
void removeHook(RooAbsRealLValue &) const override
Hook function called by RooAbsRealLValue when this binning is removed as binning for into given owner...
void setRange(double xlo, double xhi) override
Adjust range by adjusting values of external RooAbsReal values Only functional when external represen...
RooAbsReal * xlo() const
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Basic string class.
Definition TString.h:137
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16