# Rf 9 0 1_Numintconfig¶

Numeric algorithm tuning: configuration and customization of how numeric (partial) integrals are executed

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, January 19, 2022 at 10:39 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "RooNumIntConfig.h"
#include "RooLandau.h"
#include "RooArgSet.h"
#include <iomanip>

In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;


## Adjust global 1d integration precision¶

Print current global default configuration for numeric integration strategies

In [3]:
RooAbsReal::defaultIntegratorConfig()->Print("v");

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University

Requested precision: 1e-07 absolute, 1e-07 relative

1-D integration method: RooIntegrator1D (RooImproperIntegrator1D if open-ended)
2-D integration method: RooAdaptiveIntegratorND (N/A if open-ended)
N-D integration method: RooAdaptiveIntegratorND (N/A if open-ended)

Available integration methods:

*** RooBinIntegrator ***
Capabilities: [1-D] [2-D] [N-D]
Configuration:
1)  numBins = 100

*** RooIntegrator1D ***
Capabilities: [1-D]
Configuration:
1)        sumRule = Trapezoid(idx = 0)

2)  extrapolation = Wynn-Epsilon(idx = 1)

3)       maxSteps = 20
4)       minSteps = 999
5)       fixSteps = 0

*** RooIntegrator2D ***
Capabilities: [2-D]
Configuration:
(Depends on 'RooIntegrator1D')

*** RooSegmentedIntegrator1D ***
Capabilities: [1-D]
Configuration:
1)  numSeg = 3
(Depends on 'RooIntegrator1D')

*** RooSegmentedIntegrator2D ***
Capabilities: [2-D]
Configuration:
(Depends on 'RooSegmentedIntegrator1D')

*** RooImproperIntegrator1D ***
Capabilities: [1-D] [OpenEnded]
Configuration:
(Depends on 'RooIntegrator1D')

*** RooMCIntegrator ***
Capabilities: [1-D] [2-D] [N-D]
Configuration:
1)   samplingMode = Importance(idx = 0)

2)        genType = QuasiRandom(idx = 0)

3)        verbose = false(idx = 0)

4)          alpha = 1.5
5)    nRefineIter = 5
6)  nRefinePerDim = 1000
7)     nIntPerDim = 5000

Capabilities: [2-D] [N-D]
Configuration:
1)  maxEval2D = 100000
2)  maxEval3D = 1e+06
3)  maxEvalND = 1e+07
4)    maxWarn = 5

Capabilities: [1-D] [OpenEnded]
Configuration:
1)  maxSeg = 100
2)  method = 21Points(idx = 2)

*** RooGaussKronrodIntegrator1D ***
Capabilities: [1-D] [OpenEnded]
Configuration:



Example: change global precision for 1d integrals from 1e-7 to 1e-6

The relative epsilon (change as fraction of current best integral estimate) and absolute epsilon (absolute change w.r.t last best integral estimate) can be specified separately. For most pdf integrals the relative change criterium is the most important, however for certain non-pdf functions that integrate out to zero a separate absolute change criterium is necessary to declare convergence of the integral

NB: This change is for illustration only. In general the precision should be at least 1e-7 for normalization integrals for MINUIT to succeed.

In [4]:
RooAbsReal::defaultIntegratorConfig()->setEpsAbs(1e-6);
RooAbsReal::defaultIntegratorConfig()->setEpsRel(1e-6);


## Numeric integration of landau pdf¶

Construct pdf without support for analytical integrator for demonstration purposes

In [5]:
RooRealVar x("x", "x", -10, 10);
RooLandau landau("landau", "landau", x, RooConst(0), RooConst(0.1));


Activate debug-level messages for topic integration to be able to follow actions below

In [6]:
RooMsgService::instance().addStream(DEBUG, Topic(Integration));


Calculate integral over landau with default choice of numeric integrator

In [7]:
RooAbsReal *intLandau = landau.createIntegral(x);
Double_t val = intLandau->getVal();
cout << " [1] int_dx landau(x) = " << setprecision(15) << val << endl;

[#3] INFO:Integration -- RooRealIntegral::ctor(landau_Int[x]) Constructing integral of function landau over observables(x) with normalization () with range identifier <none>
[#3] DEBUG:Integration -- landau: Adding observable x of server x as shape dependent
[#3] INFO:Integration -- landau: Observable x is suitable for analytical integration (if supported by p.d.f)
[#3] INFO:Integration -- landau: Observables (x) are numerically integrated
[#1] INFO:NumericIntegration -- RooRealIntegral::init(landau_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
[1] int_dx landau(x) = 0.0989653362054419


## Same with custom configuration¶

Construct a custom configuration which uses the adaptive gauss-kronrod technique for closed 1D integrals

In [8]:
RooNumIntConfig customConfig(*RooAbsReal::defaultIntegratorConfig());
#ifdef R__HAS_MATHMORE
#else
Warning("rf901_numintconfig","ROOT is built without Mathmore (GSL) support. Cannot use RooAdaptiveGaussKronrodIntegrator1D");
#endif

Unbalanced braces. This cell was not processed.


Calculate integral over landau with custom integral specification

In [9]:
RooAbsReal *intLandau2 = landau.createIntegral(x, NumIntConfig(customConfig));
Double_t val2 = intLandau2->getVal();
cout << " [2] int_dx landau(x) = " << val2 << endl;

input_line_60:2:109: error: use of undeclared identifier 'customConfig'
(((*(class RooLandau*)0x7fb6978fc410)).createIntegral(((*(class RooRealVar*)0x7fb6978fc038)), NumIntConfig(customConfig)))
^
Error in <HandleInterpreterException>: Error evaluating expression (((*(class RooLandau*)0x7fb6978fc410)).createIntegral(((*(class RooRealVar*)0x7fb6978fc038)), NumIntConfig(customConfig)))
Execution of your code was aborted.


## Adjusting default config for a specific pdf¶

Another possibility: associate custom numeric integration configuration as default for object 'landau'

In [10]:
landau.setIntegratorConfig(customConfig);

input_line_62:2:61: error: use of undeclared identifier 'customConfig'
(((*(class RooLandau*)0x7fb6978fc410)).setIntegratorConfig(customConfig))
^
Error in <HandleInterpreterException>: Error evaluating expression (((*(class RooLandau*)0x7fb6978fc410)).setIntegratorConfig(customConfig))
Execution of your code was aborted.


Calculate integral over landau custom numeric integrator specified as object default

In [11]:
RooAbsReal *intLandau3 = landau.createIntegral(x);
Double_t val3 = intLandau3->getVal();
cout << " [3] int_dx landau(x) = " << val3 << endl;

[#3] INFO:Integration -- RooRealIntegral::ctor(landau_Int[x]) Constructing integral of function landau over observables(x) with normalization () with range identifier <none>
[#3] DEBUG:Integration -- landau: Adding observable x of server x as shape dependent
[#3] INFO:Integration -- landau: Observable x is suitable for analytical integration (if supported by p.d.f)
[#3] INFO:Integration -- landau: Observables (x) are numerically integrated
[#1] INFO:NumericIntegration -- RooRealIntegral::init(landau_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
[3] int_dx landau(x) = 0.0989653362054419


Another possibility: change global default for 1d numeric integration strategy on finite domains

In [12]:
#ifdef R__HAS_MATHMORE

Unbalanced braces. This cell was not processed.


## Adjusting parameters of a specific technique¶

Adjust maximum number of steps of roointegrator1d in the global default configuration

In [13]:
RooAbsReal::defaultIntegratorConfig()->getConfigSection("RooIntegrator1D").setRealValue("maxSteps", 30);


Example of how to change the parameters of a numeric integrator (Each config section is a RooArgSet with RooRealVars holding real-valued parameters and RooCategories holding parameters with a finite set of options)

In [14]:
customConfig.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg", 50);

input_line_67:2:3: error: use of undeclared identifier 'customConfig'
^
Error in <HandleInterpreterException>: Error evaluating expression (customConfig.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg", 50))
Execution of your code was aborted.


Example of how to print set of possible values for "method" category

In [15]:
customConfig.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").find("method")->Print("v");
#endif

input_line_68:3:2: error: #endif without #if
#endif
^