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 "Riostream.h"
34
35
36using std::endl, std::ostream;
37
38
39
40////////////////////////////////////////////////////////////////////////////////
41/// Default constructor
42
47
48
49////////////////////////////////////////////////////////////////////////////////
50/// Construct binning with 'nBins' bins and with a range
51/// parameterized by external RooAbsReals xloIn and xhiIn.
52
55 _xlo(&xloIn),
56 _xhi(&xhiIn),
57 _nbins(nBins)
58{
59}
60
61
62
63////////////////////////////////////////////////////////////////////////////////
64/// Destructor
65
67{
68 if (_array) delete[] _array ;
69 if (_lp) delete _lp ;
70}
71
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// Copy constructor
76
78{
79
80 if (other._lp) {
81 _xlo = static_cast<RooAbsReal*>(other._lp->at(0)) ;
82 _xhi = static_cast<RooAbsReal*>(other._lp->at(1)) ;
83
84 } else {
85
86 _xlo = other._xlo ;
87 _xhi = other._xhi ;
88 }
89
90 _nbins = other._nbins ;
91 _lp = nullptr ;
92
93}
94
95
96
97////////////////////////////////////////////////////////////////////////////////
98/// Hook function called by RooAbsRealLValue when this binning
99/// is inserted as binning for into given owner. Create
100/// list proxy registered with owner that will track and implement
101/// server directs to external RooAbsReals of this binning
102
104{
105 _owner = &owner ;
106
107 // If list proxy already exists update pointers from proxy
108 if (_lp) {
109 _xlo = xlo() ;
110 _xhi = xhi() ;
111 delete _lp ;
112 }
113
114 // If list proxy does not exist, create it now
115 _lp = new RooListProxy(Form("range::%s",GetName()),"lp",&owner,false,true) ;
116 _lp->add(*_xlo) ;
117 _lp->add(*_xhi) ;
118 _xlo = nullptr ;
119 _xhi = nullptr ;
120
121
122}
123
124
125////////////////////////////////////////////////////////////////////////////////
126/// Hook function called by RooAbsRealLValue when this binning
127/// is removed as binning for into given owner. Delete list
128/// proxy that was inserted in owner
129
131{
132 _owner = nullptr ;
133
134 // Remove list proxy from owner
135 if (_lp) {
136 _xlo = xlo() ;
137 _xhi = xhi() ;
138 delete _lp ;
139 _lp = nullptr ;
140 }
141}
142
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Adjust range by adjusting values of external RooAbsReal values
147/// Only functional when external representations are lvalues
148
150{
151 if (newxlo>newxhi) {
152 coutE(InputArguments) << "RooParamBinning::setRange: ERROR low bound > high bound" << std::endl ;
153 return ;
154 }
155
156 RooAbsRealLValue* xlolv = dynamic_cast<RooAbsRealLValue*>(xlo()) ;
157 if (xlolv) {
158 xlolv->setVal(newxlo) ;
159 } else {
160 coutW(InputArguments) << "RooParamBinning::setRange: WARNING lower bound not represented by lvalue, cannot set lower bound value through setRange()" << std::endl ;
161 }
162
163 RooAbsRealLValue* xhilv = dynamic_cast<RooAbsRealLValue*>(xhi()) ;
164 if (xhilv) {
165 xhilv->setVal(newxhi) ;
166 } else {
167 coutW(InputArguments) << "RooParamBinning::setRange: WARNING upper bound not represented by lvalue, cannot set upper bound value through setRange()" << std::endl ;
168 }
169
170}
171
172
173
174////////////////////////////////////////////////////////////////////////////////
175/// Return the fit bin index for the current value
176
177void RooParamBinning::binNumbers(double const * x, int * bins, std::size_t n, int coef) const
178{
179 const double xloVal = xlo()->getVal();
180 const double xhiVal = xhi()->getVal();
181 const double oneOverW = 1./averageBinWidth();
182
183 for(std::size_t i = 0; i < n; ++i) {
184 bins[i] += coef * (x[i] >= xhiVal ? _nbins - 1 : std::max(0, int((x[i] - xloVal)*oneOverW)));
185 }
186}
187
188
189
190////////////////////////////////////////////////////////////////////////////////
191/// Return the central value of the 'i'-th fit bin
192
194{
195 if (i<0 || i>=_nbins) {
196 coutE(InputArguments) << "RooParamBinning::binCenter ERROR: bin index " << i
197 << " is out of range (0," << _nbins-1 << ")" << std::endl ;
198 return 0 ;
199 }
200
201 return xlo()->getVal() + (i + 0.5)*averageBinWidth() ;
202}
203
204
205
206
207////////////////////////////////////////////////////////////////////////////////
208/// Return average bin width
209
210double RooParamBinning::binWidth(Int_t /*bin*/) const
211{
212 return (xhi()->getVal()-xlo()->getVal())/_nbins ;
213}
214
215
216
217////////////////////////////////////////////////////////////////////////////////
218/// Return the low edge of the 'i'-th fit bin
219
221{
222 if (i<0 || i>=_nbins) {
223 coutE(InputArguments) << "RooParamBinning::binLow ERROR: bin index " << i
224 << " is out of range (0," << _nbins-1 << ")" << std::endl ;
225 return 0 ;
226 }
227
228 return xlo()->getVal() + i*binWidth(i) ;
229}
230
231
232
233////////////////////////////////////////////////////////////////////////////////
234/// Return the high edge of the 'i'-th fit bin
235
237{
238 if (i<0 || i>=_nbins) {
239 coutE(InputArguments) << "RooParamBinning::fitBinHigh ERROR: bin index " << i
240 << " is out of range (0," << _nbins-1 << ")" << std::endl ;
241 return 0 ;
242 }
243
244 return xlo()->getVal() + (i + 1)*binWidth(i) ;
245}
246
247
248
249////////////////////////////////////////////////////////////////////////////////
250/// Return array of bin boundaries
251
253{
254 if (_array) delete[] _array ;
255 _array = new double[_nbins+1] ;
256
257 Int_t i ;
258 for (i=0 ; i<=_nbins ; i++) {
259 _array[i] = xlo()->getVal() + i*binWidth(i) ;
260 }
261 return _array ;
262}
263
264
265
266////////////////////////////////////////////////////////////////////////////////
267/// Print details of binning
268
269void RooParamBinning::printMultiline(ostream &os, Int_t /*content*/, bool /*verbose*/, TString indent) const
270{
271 os << indent << "_xlo = " << _xlo << std::endl ;
272 os << indent << "_xhi = " << _xhi << std::endl ;
273 if (_lp) {
274 os << indent << "xlo() = " << xlo() << std::endl ;
275 os << indent << "xhi() = " << xhi() << std::endl ;
276 }
277 if (xlo()) {
278 xlo()->Print("t") ;
279 }
280 if (xhi()) {
281 xhi()->Print("t") ;
282 }
283}
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:145
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
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:138
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16