Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22Computes the integral over a binned distribution by summing the bin contents of all bins.
23**/
24
25#include "RooBinIntegrator.h"
26
27#include "RooArgSet.h"
28#include "RooRealVar.h"
29#include "RooNumber.h"
30#include "RooNumIntConfig.h"
31#include "RooNumIntFactory.h"
32#include "RooMsgService.h"
33#include "RooRealBinding.h"
34
35#include "TClass.h"
36#include "Math/Util.h"
37
38#include <cassert>
39#include <memory>
40
41
42
43using 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
57 std::string name = "RooBinIntegrator";
58
59 auto creator = [](const RooAbsFunc &function, const RooNumIntConfig &config) {
60 return std::make_unique<RooBinIntegrator>(function, config);
61 };
62
63 fact.registerPlugin(name, creator, {numBins},
64 /*canIntegrate1D=*/true,
65 /*canIntegrate2D=*/true,
66 /*canIntegrateND=*/true,
67 /*canIntegrateOpenEnded=*/false);
68
70}
71
72
73////////////////////////////////////////////////////////////////////////////////
74/// Construct integrator on given function binding binding
75
76RooBinIntegrator::RooBinIntegrator(const RooAbsFunc& function, int numBins):
77 RooAbsIntegrator(function)
78{
80 assert(_function && _function->isValid());
81
82 // Allocate coordinate buffer size after number of function dimensions
83 _x.resize(_function->getDimension());
84 _numBins = numBins;
85
86 _xmin.resize(_function->getDimension()) ;
87 _xmax.resize(_function->getDimension()) ;
88
89 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
92
93 // Retrieve bin configuration from integrand
94 std::unique_ptr<list<double>> tmp{ _function->binBoundaries(i) };
95 if (!tmp) {
96 oocoutW(nullptr,Integration) << "RooBinIntegrator::RooBinIntegrator WARNING: integrand provide no binning definition observable #"
97 << i << " substituting default binning of " << _numBins << " bins" << endl ;
98 tmp = std::make_unique<list<double>>( );
99 for (Int_t j=0 ; j<=_numBins ; j++) {
100 tmp->push_back(_xmin[i]+j*(_xmax[i]-_xmin[i])/_numBins) ;
101 }
102 }
103 _binb.emplace_back(tmp->begin(), tmp->end());
104
105 }
106 checkLimits();
107
108}
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// Construct integrator on given function binding binding
113
115 RooBinIntegrator(function, static_cast<int>(config.getConfigSection("RooBinIntegrator").getRealValue("numBins")))
116{
117}
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Destructor
122
124{
125}
126
127
128////////////////////////////////////////////////////////////////////////////////
129/// Change our integration limits. Return true if the new limits are
130/// ok, or otherwise false. Always returns false and does nothing
131/// if this object was constructed to always use our integrand's limits.
132
134{
136 oocoutE(nullptr,Integration) << "RooBinIntegrator::setLimits: cannot override integrand's limits" << endl;
137 return false;
138 }
139 _xmin[0]= *xmin;
140 _xmax[0]= *xmax;
141 return checkLimits();
142}
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Check that our integration range is finite and otherwise return false.
147/// Update the limits from the integrand if requested.
148
150{
152 assert(nullptr != integrand() && integrand()->isValid());
153 _xmin.resize(_function->getDimension()) ;
154 _xmax.resize(_function->getDimension()) ;
155 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
156 _xmin[i]= integrand()->getMinLimit(i);
157 _xmax[i]= integrand()->getMaxLimit(i);
158 }
159 }
160 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
161 if (_xmax[i]<=_xmin[i]) {
162 oocoutE(nullptr,Integration) << "RooBinIntegrator::checkLimits: bad range with min >= max (_xmin = " << _xmin[i] << " _xmax = " << _xmax[i] << ")" << endl;
163 return false;
164 }
166 return false ;
167 }
168 }
169
170 return true;
171}
172
173
174////////////////////////////////////////////////////////////////////////////////
175/// Calculate numeric integral at given set of function binding parameters.
176double RooBinIntegrator::integral(const double *)
177{
178 assert(isValid());
179
181
182 if (_function->getDimension() == 1) {
183 const std::vector<double>& binb = _binb[0];
184
185 for (unsigned int ibin=0; ibin < binb.size() - 1; ++ibin) {
186 const double xhi = binb[ibin + 1];
187 const double xlo = binb[ibin];
188 const double xcenter = (xhi+xlo)/2.;
189 const double binInt = integrand(xvec(xcenter))*(xhi-xlo) ;
190 sum += binInt ;
191 }
192 } else if (_function->getDimension() == 2) {
193 const std::vector<double>& binbx = _binb[0];
194 const std::vector<double>& binby = _binb[1];
195
196 for (unsigned int ibin1=0; ibin1 < binbx.size() - 1; ++ibin1) {
197 const double x1hi = binbx[ibin1 + 1];
198 const double x1lo = binbx[ibin1];
199 double x1center = (x1hi+x1lo)/2 ;
200
201 for (unsigned int ibin2=0; ibin2 < binby.size() - 1; ++ibin2) {
202 const double x2hi = binby[ibin2 + 1];
203 const double x2lo = binby[ibin2];
204 const double x2center = (x2hi+x2lo)/2.;
205
206 const double binInt = integrand(xvec(x1center,x2center))*(x1hi-x1lo)*(x2hi-x2lo) ;
207 sum += binInt ;
208 }
209 }
210 } else if (_function->getDimension() == 3) {
211 const std::vector<double>& binbx = _binb[0];
212 const std::vector<double>& binby = _binb[1];
213 const std::vector<double>& binbz = _binb[2];
214
215 for (unsigned int ibin1=0; ibin1 < binbx.size() - 1; ++ibin1) {
216 const double x1hi = binbx[ibin1 + 1];
217 const double x1lo = binbx[ibin1];
218 double x1center = (x1hi+x1lo)/2 ;
219
220 for (unsigned int ibin2=0; ibin2 < binby.size() - 1; ++ibin2) {
221 const double x2hi = binby[ibin2 + 1];
222 const double x2lo = binby[ibin2];
223 const double x2center = (x2hi+x2lo)/2.;
224
225 for (unsigned int ibin3=0; ibin3 < binbz.size() - 1; ++ibin3) {
226 const double x3hi = binbz[ibin3 + 1];
227 const double x3lo = binbz[ibin3];
228 const double x3center = (x3hi+x3lo)/2.;
229
230 const double binInt = integrand(xvec(x1center,x2center,x3center))*(x1hi-x1lo)*(x2hi-x2lo)*(x3hi-x3lo);
231 sum += binInt ;
232 }
233 }
234 }
235 }
236
237 return sum.Sum();
238}
239
240
#define oocoutW(o, a)
#define oocoutE(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition Util.h:122
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition RooAbsFunc.h:27
bool isValid() const
Definition RooAbsFunc.h:37
virtual double getMaxLimit(UInt_t dimension) const =0
virtual double getMinLimit(UInt_t dimension) const =0
UInt_t getDimension() const
Definition RooAbsFunc.h:33
virtual std::list< double > * binBoundaries(Int_t) const
Definition RooAbsFunc.h:69
Abstract interface for integrators of real-valued functions that implement the RooAbsFunc interface.
bool isValid() const
Is integrator in valid state.
const RooAbsFunc * _function
Pointer to function binding of integrand.
const RooAbsFunc * integrand() const
Return integrand function binding.
Computes the integral over a binned distribution by summing the bin contents of all bins.
std::vector< double > _xmax
! Upper integration bound
static void registerIntegrator(RooNumIntFactory &fact)
Register RooBinIntegrator, is parameters and capabilities with RooNumIntFactory.
Int_t _numBins
! Size of integration range
bool setLimits(double *xmin, double *xmax) override
Change our integration limits.
std::vector< std::vector< double > > _binb
! list of bin boundaries
std::vector< double > _xmin
! Lower integration bound
RooBinIntegrator(const RooAbsFunc &function, int numBins=100)
Construct integrator on given function binding binding.
double * xvec(double xx)
std::vector< double > _x
! do not persist
bool _useIntegrandLimits
If true limits of function binding are ued.
bool checkLimits() const override
Check that our integration range is finite and otherwise return false.
double integral(const double *yvec=nullptr) override
Calculate numeric integral at given set of function binding parameters.
~RooBinIntegrator() override
Destructor.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
RooNumIntConfig holds the configuration parameters of the various numeric integrators used by RooReal...
RooCategory & method1D()
static RooNumIntConfig & defaultConfig()
Return reference to instance of default numeric integrator configuration object.
RooNumIntFactory is a factory to instantiate numeric integrators from a given function binding and a ...
bool registerPlugin(std::string const &name, Creator const &creator, const RooArgSet &defConfig, bool canIntegrate1D, bool canIntegrate2D, bool canIntegrateND, bool canIntegrateOpenEnded, const char *depName="")
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...
static constexpr int isInfinite(double x)
Return true if x is infinite by RooNumber internal specification.
Definition RooNumber.h:27
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:37
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345