Logo ROOT   6.12/07
Reference Guide
RooBinIntegrator.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 RooBinIntegrator.cxx
19 \class RooBinIntegrator
20 \ingroup Roofitcore
21 
22 RooBinIntegrator implements an adaptive one-dimensional
23 numerical integration algorithm.
24 **/
25 
26 
27 #include "RooFit.h"
28 #include "Riostream.h"
29 
30 #include "TClass.h"
31 #include "RooBinIntegrator.h"
32 #include "RooArgSet.h"
33 #include "RooRealVar.h"
34 #include "RooNumber.h"
35 #include "RooIntegratorBinding.h"
36 #include "RooNumIntConfig.h"
37 #include "RooNumIntFactory.h"
38 #include "RooMsgService.h"
39 
40 #include <assert.h>
41 
42 
43 
44 using namespace std;
45 
47 ;
48 
49 // Register this class with RooNumIntConfig
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Register RooBinIntegrator, is parameters and capabilities with RooNumIntFactory
53 
55 {
56  RooRealVar numBins("numBins","Number of bins in range",100) ;
58  fact.storeProtoIntegrator(proto,RooArgSet(numBins)) ;
60 }
61 
62 
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Default constructor
66 
67 RooBinIntegrator::RooBinIntegrator() : _numBins(0), _useIntegrandLimits(kFALSE), _x(0)
68 {
69 }
70 
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Construct integrator on given function binding binding
74 
77 {
79  assert(0 != integrand() && integrand()->isValid());
80 
81  // Allocate coordinate buffer size after number of function dimensions
82  _x = new Double_t[_function->getDimension()] ;
83  _numBins = 100 ;
84 
85  _xmin.resize(_function->getDimension()) ;
86  _xmax.resize(_function->getDimension()) ;
87 
88  for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
89  _xmin[i]= integrand()->getMinLimit(i);
90  _xmax[i]= integrand()->getMaxLimit(i);
91 
92  // Retrieve bin configuration from integrand
93  list<Double_t>* tmp = integrand()->binBoundaries(i) ;
94  if (!tmp) {
95  oocoutW((TObject*)0,Integration) << "RooBinIntegrator::RooBinIntegrator WARNING: integrand provide no binning definition observable #"
96  << i << " substituting default binning of " << _numBins << " bins" << endl ;
97  tmp = new list<Double_t> ;
98  for (Int_t j=0 ; j<=_numBins ; j++) {
99  tmp->push_back(_xmin[i]+j*(_xmax[i]-_xmin[i])/_numBins) ;
100  }
101  }
102  _binb.push_back(tmp) ;
103  }
104  checkLimits();
105 
106 }
107 
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Construct integrator on given function binding binding
111 
114 {
115  const RooArgSet& configSet = config.getConfigSection(IsA()->GetName()) ;
117  _numBins = (Int_t) configSet.getRealValue("numBins") ;
118  assert(0 != integrand() && integrand()->isValid());
119 
120  // Allocate coordinate buffer size after number of function dimensions
121  _x = new Double_t[_function->getDimension()] ;
122 
123  for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
124  _xmin.push_back(integrand()->getMinLimit(i));
125  _xmax.push_back(integrand()->getMaxLimit(i));
126 
127  // Retrieve bin configuration from integrand
128  list<Double_t>* tmp = integrand()->binBoundaries(i) ;
129  if (!tmp) {
130  oocoutW((TObject*)0,Integration) << "RooBinIntegrator::RooBinIntegrator WARNING: integrand provide no binning definition observable #"
131  << i << " substituting default binning of " << _numBins << " bins" << endl ;
132  tmp = new list<Double_t> ;
133  for (Int_t j=0 ; j<=_numBins ; j++) {
134  tmp->push_back(_xmin[i]+j*(_xmax[i]-_xmin[i])/_numBins) ;
135  }
136  }
137  _binb.push_back(tmp) ;
138  }
139 
140  checkLimits();
141 }
142 
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Clone integrator with new function binding and configuration. Needed by RooNumIntFactory
146 
148 {
149  return new RooBinIntegrator(function,config) ;
150 }
151 
152 
153 
154 
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Destructor
158 
160 {
161  if(_x) delete[] _x;
162  for (vector<list<Double_t>*>::iterator iter = _binb.begin() ; iter!=_binb.end() ; ++iter) {
163  delete (*iter) ;
164  }
165 
166 }
167 
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Change our integration limits. Return kTRUE if the new limits are
171 /// ok, or otherwise kFALSE. Always returns kFALSE and does nothing
172 /// if this object was constructed to always use our integrand's limits.
173 
175 {
176  if(_useIntegrandLimits) {
177  oocoutE((TObject*)0,Integration) << "RooBinIntegrator::setLimits: cannot override integrand's limits" << endl;
178  return kFALSE;
179  }
180  _xmin[0]= *xmin;
181  _xmax[0]= *xmax;
182  return checkLimits();
183 }
184 
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Check that our integration range is finite and otherwise return kFALSE.
188 /// Update the limits from the integrand if requested.
189 
191 {
192  if(_useIntegrandLimits) {
193  assert(0 != integrand() && integrand()->isValid());
194  _xmin.resize(_function->getDimension()) ;
195  _xmax.resize(_function->getDimension()) ;
196  for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
197  _xmin[i]= integrand()->getMinLimit(i);
198  _xmax[i]= integrand()->getMaxLimit(i);
199  }
200  }
201  for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
202  if (_xmax[i]<=_xmin[i]) {
203  oocoutE((TObject*)0,Integration) << "RooBinIntegrator::checkLimits: bad range with min >= max (_xmin = " << _xmin[i] << " _xmax = " << _xmax[i] << ")" << endl;
204  return kFALSE;
205  }
207  return kFALSE ;
208  }
209  }
210 
211  return kTRUE;
212 }
213 
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Calculate numeric integral at given set of function binding parameters
217 
219 {
220  assert(isValid());
221 
222  double sum = 0. ;
223 
224  if (_function->getDimension()==1) {
225  list<Double_t>::iterator iter = _binb[0]->begin() ;
226  Double_t xlo = *iter ; iter++ ;
227  for (; iter!=_binb[0]->end() ; ++iter) {
228  Double_t xhi = *iter ;
229  Double_t xcenter = (xhi+xlo)/2 ;
230  Double_t binInt = integrand(xvec(xcenter))*(xhi-xlo) ;
231  sum += binInt ;
232  //cout << "RBI::integral over " << _function->getName() << " 1D binInt[" << xcenter << "] = " << binInt << " running sum = " << sum << endl ;
233  xlo=xhi ;
234  }
235  }
236 
237  if (_function->getDimension()==2) {
238 
239  list<Double_t>::iterator iter1 = _binb[0]->begin() ;
240 
241  Double_t x1lo = *iter1 ; iter1++ ;
242  for (; iter1!=_binb[0]->end() ; ++iter1) {
243 
244  Double_t x1hi = *iter1 ;
245  Double_t x1center = (x1hi+x1lo)/2 ;
246 
247  list<Double_t>::iterator iter2 = _binb[1]->begin() ;
248  Double_t x2lo = *iter2 ; iter2++ ;
249  for (; iter2!=_binb[1]->end() ; ++iter2) {
250 
251  Double_t x2hi = *iter2 ;
252  Double_t x2center = (x2hi+x2lo)/2 ;
253 
254  Double_t binInt = integrand(xvec(x1center,x2center))*(x1hi-x1lo)*(x2hi-x2lo) ;
255  //cout << "RBI::integral 2D binInt[" << x1center << "," << x2center << "] = " << binInt << " binv = " << (x1hi-x1lo) << "*" << (x2hi-x2lo) << endl ;
256  sum += binInt ;
257  x2lo=x2hi ;
258  }
259  x1lo=x1hi ;
260  }
261  }
262 
263  if (_function->getDimension()==3) {
264 
265  list<Double_t>::iterator iter1 = _binb[0]->begin() ;
266 
267  Double_t x1lo = *iter1 ; iter1++ ;
268  for (; iter1!=_binb[0]->end() ; ++iter1) {
269 
270  Double_t x1hi = *iter1 ;
271  Double_t x1center = (x1hi+x1lo)/2 ;
272 
273  list<Double_t>::iterator iter2 = _binb[1]->begin() ;
274  Double_t x2lo = *iter2 ; iter2++ ;
275  for (; iter2!=_binb[1]->end() ; ++iter2) {
276 
277  Double_t x2hi = *iter2 ;
278  Double_t x2center = (x2hi+x2lo)/2 ;
279 
280  list<Double_t>::iterator iter3 = _binb[2]->begin() ;
281  Double_t x3lo = *iter3 ; iter3++ ;
282  for (; iter3!=_binb[2]->end() ; ++iter3) {
283 
284  Double_t x3hi = *iter3 ;
285  Double_t x3center = (x3hi+x3lo)/2 ;
286 
287  Double_t binInt = integrand(xvec(x1center,x2center,x3center))*(x1hi-x1lo)*(x2hi-x2lo)*(x3hi-x3lo) ;
288  //cout << "RBI::integral 3D binInt[" << x1center << "," << x2center << "," << x3center << "] = " << binInt << endl ;
289  sum += binInt ;
290 
291  x3lo=x3hi ;
292  }
293  x2lo=x2hi ;
294  }
295  x1lo=x1hi ;
296  }
297  }
298 
299  return sum;
300 }
301 
302 
static RooNumIntConfig & defaultConfig()
Return reference to instance of default numeric integrator configuration object.
RooAbsIntegrator is the abstract interface for integrators of real-valued functions that implement th...
static long int sum(long int i)
Definition: Factory.cxx:2173
float xmin
Definition: THbookFile.cxx:93
RooNumIntConfig holds the configuration parameters of the various numeric integrators used by RooReal...
RooBinIntegrator implements an adaptive one-dimensional numerical integration algorithm.
virtual ~RooBinIntegrator()
Destructor.
virtual Bool_t checkLimits() const
Check that our integration range is finite and otherwise return kFALSE.
RooNumIntFactory is a factory to instantiate numeric integrators from a given function binding and a ...
virtual RooAbsIntegrator * clone(const RooAbsFunc &function, const RooNumIntConfig &config) const
Clone integrator with new function binding and configuration. Needed by RooNumIntFactory.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const RooArgSet & getConfigSection(const char *name) const
Retrieve configuration information specific to integrator with given name.
static Int_t isInfinite(Double_t x)
Return true if x is infinite by RooNumBer internal specification.
Definition: RooNumber.cxx:58
STL namespace.
Bool_t isValid() const
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)
Set value by specifying the name of the desired state If printError is set, a message will be printed...
RooCategory & method1D()
std::vector< std::list< Double_t > * > _binb
Upper integration bound.
#define oocoutE(o, a)
Definition: RooMsgService.h:47
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:146
Bool_t _useIntegrandLimits
Size of integration range.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
UInt_t getDimension() const
Definition: RooAbsFunc.h:29
unsigned int UInt_t
Definition: RtypesCore.h:42
const RooAbsFunc * _function
virtual Double_t getMinLimit(UInt_t dimension) const =0
virtual std::list< Double_t > * binBoundaries(Int_t) const
Definition: RooAbsFunc.h:64
float xmax
Definition: THbookFile.cxx:93
Int_t _numBins
list of bin boundaries
static void registerIntegrator(RooNumIntFactory &fact)
Register RooBinIntegrator, is parameters and capabilities with RooNumIntFactory.
const Bool_t kFALSE
Definition: RtypesCore.h:88
const RooAbsFunc * integrand() const
#define ClassImp(name)
Definition: Rtypes.h:359
Double_t * xvec(Double_t &xx)
virtual Double_t getMaxLimit(UInt_t dimension) const =0
double Double_t
Definition: RtypesCore.h:55
std::vector< Double_t > _xmax
Lower integration bound.
Bool_t setLimits(Double_t *xmin, Double_t *xmax)
Change our integration limits.
std::vector< Double_t > _xmin
#define oocoutW(o, a)
Definition: RooMsgService.h:46
Mother of all ROOT objects.
Definition: TObject.h:37
Double_t getRealValue(const char *name, Double_t defVal=0, Bool_t verbose=kFALSE) const
Get value of a RooAbsReal stored in set with given name.
Definition: RooArgSet.cxx:528
RooBinIntegrator()
Default constructor.
const char * proto
Definition: civetweb.c:11652
virtual Double_t integral(const Double_t *yvec=0)
Calculate numeric integral at given set of function binding parameters.
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
const Bool_t kTRUE
Definition: RtypesCore.h:87
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition: RooAbsFunc.h:23
Bool_t storeProtoIntegrator(RooAbsIntegrator *proto, const RooArgSet &defConfig, const char *depName="")
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...