Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooBinIntegrator.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
9 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
10 * *
11 * Copyright (c) 2000-2005, Regents of the University of California *
12 * and Stanford University. All rights reserved. *
13 * *
14 * Redistribution and use in source and binary forms, *
15 * with or without modification, are permitted according to the terms *
16 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
17 *****************************************************************************/
18
19/**
20\file RooBinIntegrator.cxx
21\class RooBinIntegrator
22\ingroup Roofitcore
23
24Computes the integral over a binned distribution by summing the bin contents of all bins.
25**/
26
27#include "RooBinIntegrator.h"
28
29#include "RooArgSet.h"
30#include "RooRealVar.h"
31#include "RooNumber.h"
32#include "RooNumIntConfig.h"
33#include "RooNumIntFactory.h"
34#include "RooMsgService.h"
35#include "RooRealBinding.h"
36
37#include <cassert>
38#include <memory>
39
40
41using std::endl, std::list;
42
43// Register this class with RooNumIntConfig
44
45////////////////////////////////////////////////////////////////////////////////
46/// Register RooBinIntegrator, is parameters and capabilities with RooNumIntFactory
47
48void RooBinIntegrator::registerIntegrator(RooNumIntFactory& fact)
49{
50 RooRealVar numBins("numBins","Number of bins in range",100) ;
51
52 std::string name = "RooBinIntegrator";
53
54 auto creator = [](const RooAbsFunc &function, const RooNumIntConfig &config) {
55 return std::make_unique<RooBinIntegrator>(function, config);
56 };
57
58 fact.registerPlugin(name, creator, {numBins},
59 /*canIntegrate1D=*/true,
60 /*canIntegrate2D=*/true,
61 /*canIntegrateND=*/true,
62 /*canIntegrateOpenEnded=*/false);
63
65}
66
67
68////////////////////////////////////////////////////////////////////////////////
69/// Construct integrator on given function binding binding
70
71RooBinIntegrator::RooBinIntegrator(const RooAbsFunc &function, int numBins)
72 : RooAbsIntegrator(function), _useIntegrandLimits(true)
73{
74 assert(_function && _function->isValid());
75
76 // Allocate coordinate buffer size after number of function dimensions
77 _x.resize(_function->getDimension());
78 _numBins = numBins;
79
80 _xmin.resize(_function->getDimension()) ;
81 _xmax.resize(_function->getDimension()) ;
82
83 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
84 _xmin[i]= _function->getMinLimit(i);
85 _xmax[i]= _function->getMaxLimit(i);
86 }
87 updateBinBoundaries(/*warnIfNoBinning=*/true);
88 checkLimits();
89
90}
91
92
93////////////////////////////////////////////////////////////////////////////////
94/// Retrieve the bin boundaries from the integrand. They can depend on the
95/// parameters, e.g. for a RooHistPdf whose observable is a shifted variable,
96/// so this is done for each integral.
97
98void RooBinIntegrator::updateBinBoundaries(bool warnIfNoBinning)
99{
100 _binb.resize(_function->getDimension());
101 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
102 std::unique_ptr<list<double>> tmp{ _function->binBoundaries(i) };
103 if (!tmp) {
104 if (warnIfNoBinning) {
105 oocoutW(nullptr,Integration) << "RooBinIntegrator::RooBinIntegrator WARNING: integrand provide no binning definition observable #"
106 << i << " substituting default binning of " << _numBins << " bins" << std::endl ;
107 }
108 tmp = std::make_unique<list<double>>( );
109 for (Int_t j=0 ; j<=_numBins ; j++) {
110 tmp->push_back(_xmin[i]+j*(_xmax[i]-_xmin[i])/_numBins) ;
111 }
112 }
113 std::vector<double> &binb = _binb[i];
114 binb.assign(tmp->begin(), tmp->end());
115
116 // The bin boundaries provided by the integrand only include the boundaries
117 // that lie strictly inside the integration range. To integrate correctly
118 // over partial bins at the edges of the range, the integration limits need
119 // to be added as the outermost boundaries (if not already present).
120 if (binb.empty() || binb.front() > _xmin[i]) {
121 binb.insert(binb.begin(), _xmin[i]);
122 }
123 if (binb.back() < _xmax[i]) {
124 binb.push_back(_xmax[i]);
125 }
126 }
127}
128
129
130////////////////////////////////////////////////////////////////////////////////
131/// Construct integrator on given function binding binding
132
133RooBinIntegrator::RooBinIntegrator(const RooAbsFunc& function, const RooNumIntConfig& config) :
134 RooBinIntegrator(function, static_cast<int>(config.getConfigSection("RooBinIntegrator").getRealValue("numBins")))
135{
136}
137
138
139////////////////////////////////////////////////////////////////////////////////
140/// Change our integration limits. Return true if the new limits are
141/// ok, or otherwise false. Always returns false and does nothing
142/// if this object was constructed to always use our integrand's limits.
143
144bool RooBinIntegrator::setLimits(double *xmin, double *xmax)
145{
146 if(_useIntegrandLimits) {
147 oocoutE(nullptr,Integration) << "RooBinIntegrator::setLimits: cannot override integrand's limits" << std::endl;
148 return false;
149 }
150 _xmin[0]= *xmin;
151 _xmax[0]= *xmax;
152 return checkLimits();
153}
154
155
156////////////////////////////////////////////////////////////////////////////////
157/// Check that our integration range is finite and otherwise return false.
158/// Update the limits from the integrand if requested.
159
160bool RooBinIntegrator::checkLimits() const
161{
162 if(_useIntegrandLimits) {
163 assert(nullptr != integrand() && integrand()->isValid());
164 _xmin.resize(_function->getDimension()) ;
165 _xmax.resize(_function->getDimension()) ;
166 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
167 _xmin[i]= integrand()->getMinLimit(i);
168 _xmax[i]= integrand()->getMaxLimit(i);
169 }
170 }
171 for (UInt_t i=0 ; i<_function->getDimension() ; i++) {
172 if (_xmax[i]<=_xmin[i]) {
173 oocoutE(nullptr,Integration) << "RooBinIntegrator::checkLimits: bad range with min >= max (_xmin = " << _xmin[i] << " _xmax = " << _xmax[i] << ")" << std::endl;
174 return false;
175 }
176 if (RooNumber::isInfinite(_xmin[i]) || RooNumber::isInfinite(_xmax[i])) {
177 return false ;
178 }
179 }
180
181 return true;
182}
183
184
185////////////////////////////////////////////////////////////////////////////////
186/// Calculate numeric integral at given set of function binding parameters,
187/// for any number of dimensions.
188double RooBinIntegrator::integral(const double *)
189{
190 assert(isValid());
191 if (_function->getDimension() < 1) return 0.;
192
193 updateBinBoundaries(/*warnIfNoBinning=*/false);
194
196
197 recursive_integration(0,1.,sum);
198
199 return sum.Sum();
200}
201
202/**
203 * @brief It performs recursively for loops to calculate N-dimensional integration
204 * @param d the current recursivity depth (dimension currently being for-looped)
205 * @param delta the (d-1)-dimensional bin width/area/volume/hypervolume...
206 * @param sum the resulting integral where to accumulate the integral, passed by reference
207 */
208void RooBinIntegrator::recursive_integration(const UInt_t d, const double delta, ROOT::Math::KahanSum<double>& sum) {
209 const std::vector<double>& binb = _binb[d];
210 const bool isLastDim = d+1 == _function->getDimension();
211 for (unsigned int ibin=0; ibin < binb.size() - 1; ++ibin) {
212 const double hi = binb[ibin + 1];
213 const double lo = binb[ibin];
214 const double mid = (hi+lo)/2.;
215 _x[d] = mid;
216 if (isLastDim) {
217 const double binInt = integrand(_x.data())*(hi-lo)*delta;
218 sum += binInt ;
219 }
220 else {
221 recursive_integration(d+1, (hi-lo)*delta, sum);
222 }
223 }
224}
225
226/// \endcond
#define d(i)
Definition RSha256.hxx:102
#define oocoutW(o, a)
#define oocoutE(o, a)
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
char name[80]
Definition TGX11.cxx:142
float xmin
#define hi
float xmax
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition Util.h:141
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition RooAbsFunc.h:27
Abstract interface for integrators of real-valued functions that implement the RooAbsFunc interface.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
Holds the configuration parameters of the various numeric integrators used by RooRealIntegral.
RooCategory & method1D()
static RooNumIntConfig & defaultConfig()
Return reference to instance of default numeric integrator configuration object.
Factory to instantiate numeric integrators from a given function binding and a given configuration.
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
Variable that can be changed from the outside.
Definition RooRealVar.h:37
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2335