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