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
22Class RooParamBinning is an implementation 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 namespace std;
37
39;
40
41
42////////////////////////////////////////////////////////////////////////////////
43/// Default constructor
44/// cout << "RooParamBinning(" << this << ") default ctor" << endl ;
45
47 RooAbsBinning(name), _xlo(0), _xhi(0), _nbins(100), _binw(0), _lp(0), _owner(0)
48{
49 _array = 0 ;
50}
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Construct binning with 'nBins' bins and with a range
55/// parameterized by external RooAbsReals xloIn and xhiIn.
56
59 _array(0),
60 _xlo(&xloIn),
61 _xhi(&xhiIn),
62 _nbins(nBins),
63 _binw(0),
64 _lp(0),
65 _owner(0)
66{
67}
68
69
70
71////////////////////////////////////////////////////////////////////////////////
72/// Destructor
73
75{
76 if (_array) delete[] _array ;
77 if (_lp) delete _lp ;
78}
79
80
81
82////////////////////////////////////////////////////////////////////////////////
83/// Copy constructor
84/// cout << "RooParamBinning::cctor(" << this << ") orig = " << &other << endl ;
85
87 RooAbsBinning(name), _binw(0), _owner(0)
88{
89 _array = 0 ;
90
91 if (other._lp) {
92// cout << "RooParamBinning::cctor(this = " << this << ") taking addresses from orig ListProxy" << endl ;
93 _xlo = (RooAbsReal*) other._lp->at(0) ;
94 _xhi = (RooAbsReal*) other._lp->at(1) ;
95
96 } else {
97
98// cout << "RooParamBinning::cctor(this = " << this << ") taking addresses from orig pointers " << other._xlo << " " << other._xhi << endl ;
99
100 _xlo = other._xlo ;
101 _xhi = other._xhi ;
102 }
103
104 _nbins = other._nbins ;
105 _lp = 0 ;
106
107 //cout << "RooParamBinning::cctor(this = " << this << " xlo = " << &_xlo << " xhi = " << &_xhi << " _lp = " << _lp << " owner = " << _owner << ")" << endl ;
108}
109
110
111
112////////////////////////////////////////////////////////////////////////////////
113/// Hook function called by RooAbsRealLValue when this binning
114/// is inserted as binning for into given owner. Create
115/// list proxy registered with owner that will track and implement
116/// server directs to external RooAbsReals of this binning
117
119{
120 _owner = &owner ;
121
122 // If list proxy already exists update pointers from proxy
123// cout << "RooParamBinning::insertHook(" << this << "," << GetName() << ") _lp at beginning = " << _lp << endl ;
124 if (_lp) {
125// cout << "updating raw pointers from list proxy contents" << endl ;
126 _xlo = xlo() ;
127 _xhi = xhi() ;
128 delete _lp ;
129 }
130// cout << "_xlo = " << _xlo << " _xhi = " << _xhi << endl ;
131
132 // If list proxy does not exist, create it now
133 _lp = new RooListProxy(Form("range::%s",GetName()),"lp",&owner,false,true) ;
134 _lp->add(*_xlo) ;
135 _lp->add(*_xhi) ;
136 _xlo = 0 ;
137 _xhi = 0 ;
138
139
140}
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Hook function called by RooAbsRealLValue when this binning
145/// is removed as binning for into given owner. Delete list
146/// proxy that was inserted in owner
147
149{
150 _owner = 0 ;
151
152 // Remove list proxy from owner
153 if (_lp) {
154 _xlo = xlo() ;
155 _xhi = xhi() ;
156 delete _lp ;
157 _lp = 0 ;
158 }
159}
160
161
162
163////////////////////////////////////////////////////////////////////////////////
164/// Adjust range by adjusting values of external RooAbsReal values
165/// Only functional when external representations are lvalues
166
167void RooParamBinning::setRange(double newxlo, double newxhi)
168{
169 if (newxlo>newxhi) {
170 coutE(InputArguments) << "RooParamBinning::setRange: ERROR low bound > high bound" << endl ;
171 return ;
172 }
173
174 RooAbsRealLValue* xlolv = dynamic_cast<RooAbsRealLValue*>(xlo()) ;
175 if (xlolv) {
176 xlolv->setVal(newxlo) ;
177 } else {
178 coutW(InputArguments) << "RooParamBinning::setRange: WARNING lower bound not represented by lvalue, cannot set lower bound value through setRange()" << endl ;
179 }
180
181 RooAbsRealLValue* xhilv = dynamic_cast<RooAbsRealLValue*>(xhi()) ;
182 if (xhilv) {
183 xhilv->setVal(newxhi) ;
184 } else {
185 coutW(InputArguments) << "RooParamBinning::setRange: WARNING upper bound not represented by lvalue, cannot set upper bound value through setRange()" << endl ;
186 }
187
188}
189
190
191
192////////////////////////////////////////////////////////////////////////////////
193/// Return the fit bin index for the current value
194
195void RooParamBinning::binNumbers(double const * x, int * bins, std::size_t n, int coef) const
196{
197 const double xloVal = xlo()->getVal();
198 const double xhiVal = xhi()->getVal();
199 const double oneOverW = 1./averageBinWidth();
200
201 for(std::size_t i = 0; i < n; ++i) {
202 bins[i] += coef * (x[i] >= xhiVal ? _nbins - 1 : std::max(0, int((x[i] - xloVal)*oneOverW)));
203 }
204}
205
206
207
208////////////////////////////////////////////////////////////////////////////////
209/// Return the central value of the 'i'-th fit bin
210
212{
213 if (i<0 || i>=_nbins) {
214 coutE(InputArguments) << "RooParamBinning::binCenter ERROR: bin index " << i
215 << " is out of range (0," << _nbins-1 << ")" << endl ;
216 return 0 ;
217 }
218
219 return xlo()->getVal() + (i + 0.5)*averageBinWidth() ;
220}
221
222
223
224
225////////////////////////////////////////////////////////////////////////////////
226/// Return average bin width
227
228double RooParamBinning::binWidth(Int_t /*bin*/) const
229{
230 return (xhi()->getVal()-xlo()->getVal())/_nbins ;
231}
232
233
234
235////////////////////////////////////////////////////////////////////////////////
236/// Return the low edge of the 'i'-th fit bin
237
239{
240 if (i<0 || i>=_nbins) {
241 coutE(InputArguments) << "RooParamBinning::binLow ERROR: bin index " << i
242 << " is out of range (0," << _nbins-1 << ")" << endl ;
243 return 0 ;
244 }
245
246 return xlo()->getVal() + i*binWidth(i) ;
247}
248
249
250
251////////////////////////////////////////////////////////////////////////////////
252/// Return the high edge of the 'i'-th fit bin
253
255{
256 if (i<0 || i>=_nbins) {
257 coutE(InputArguments) << "RooParamBinning::fitBinHigh ERROR: bin index " << i
258 << " is out of range (0," << _nbins-1 << ")" << endl ;
259 return 0 ;
260 }
261
262 return xlo()->getVal() + (i + 1)*binWidth(i) ;
263}
264
265
266
267////////////////////////////////////////////////////////////////////////////////
268/// Return array of bin boundaries
269
271{
272 if (_array) delete[] _array ;
273 _array = new double[_nbins+1] ;
274
275 Int_t i ;
276 for (i=0 ; i<=_nbins ; i++) {
277 _array[i] = xlo()->getVal() + i*binWidth(i) ;
278 }
279 return _array ;
280}
281
282
283
284////////////////////////////////////////////////////////////////////////////////
285/// Print details of binning
286
287void RooParamBinning::printMultiline(ostream &os, Int_t /*content*/, bool /*verbose*/, TString indent) const
288{
289 os << indent << "_xlo = " << _xlo << endl ;
290 os << indent << "_xhi = " << _xhi << endl ;
291 if (_lp) {
292 os << indent << "xlo() = " << xlo() << endl ;
293 os << indent << "xhi() = " << xhi() << endl ;
294 }
295 if (xlo()) {
296 xlo()->Print("t") ;
297 }
298 if (xhi()) {
299 xhi()->Print("t") ;
300 }
301}
302
303
RooCollectionProxy< RooArgList > RooListProxy
Definition RooAbsArg.h:52
#define coutW(a)
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
void Print(Option_t *options=nullptr) const override
Print the object to the defaultPrintStream().
Definition RooAbsArg.h:318
RooAbsBinning is the abstract base class for RooRealVar binning definitions.
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual void setVal(double value)=0
Set the current value of the object. Needs to be overridden by implementations.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
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...
Class RooParamBinning is an implementation of RooAbsBinning that constructs a binning with a range de...
RooParamBinning(const char *name=nullptr)
Default constructor cout << "RooParamBinning(" << this << ") default ctor" << endl ;.
RooAbsReal * xhi() const
RooListProxy * _lp
RooAbsArg * _owner
~RooParamBinning() override
Destructor.
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
do not persist
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:47
Basic string class.
Definition TString.h:139
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16