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