Logo ROOT  
Reference Guide
rf901_numintconfig.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Numeric algorithm tuning: configuration and customization of how numeric (partial) integrals are executed
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11from __future__ import print_function
12import ROOT
13
14
15# Adjust global 1D integration precision
16# ----------------------------------------------------------------------------
17
18# Print current global default configuration for numeric integration
19# strategies
20ROOT.RooAbsReal.defaultIntegratorConfig().Print("v")
21
22# Example: Change global precision for 1D integrals from 1e-7 to 1e-6
23#
24# The relative epsilon (change as fraction of current best integral estimate) and
25# absolute epsilon (absolute change w.r.t last best integral estimate) can be specified
26# separately. For most pdf integrals the relative change criterium is the most important,
27# however for certain non-pdf functions that integrate out to zero a separate absolute
28# change criterium is necessary to declare convergence of the integral
29#
30# NB: ROOT.This change is for illustration only. In general the precision should be at least 1e-7
31# for normalization integrals for MINUIT to succeed.
32#
33ROOT.RooAbsReal.defaultIntegratorConfig().setEpsAbs(1e-6)
34ROOT.RooAbsReal.defaultIntegratorConfig().setEpsRel(1e-6)
35
36# N u m e r i c i n t e g r a t i o n o f l a n d a u p d f
37# ------------------------------------------------------------------
38
39# Construct pdf without support for analytical integrator for
40# demonstration purposes
41x = ROOT.RooRealVar("x", "x", -10, 10)
42landau = ROOT.RooLandau("landau", "landau", x, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(0.1))
43
44# Activate debug-level messages for topic integration to be able to follow
45# actions below
46ROOT.RooMsgService.instance().addStream(ROOT.RooFit.DEBUG, Topic=ROOT.RooFit.Integration)
47
48# Calculate integral over landau with default choice of numeric integrator
49intLandau = landau.createIntegral({x})
50val = intLandau.getVal()
51print(" [1] int_dx landau(x) = ", val) # setprecision(15)
52
53# Same with custom configuration
54# -----------------------------------------------------------
55
56# Construct a custom configuration which uses the adaptive Gauss-Kronrod technique
57# for closed 1D integrals
58customConfig = ROOT.RooNumIntConfig(ROOT.RooAbsReal.defaultIntegratorConfig())
59integratorGKNotExisting = customConfig.method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D")
60if integratorGKNotExisting:
61 print("WARNING: RooAdaptiveGaussKronrodIntegrator is not existing because ROOT is built without Mathmore support")
62
63# Calculate integral over landau with custom integral specification
64intLandau2 = landau.createIntegral({x}, NumIntConfig=customConfig)
65val2 = intLandau2.getVal()
66print(" [2] int_dx landau(x) = ", val2)
67
68# Adjusting default config for a specific pdf
69# -------------------------------------------------------------------------------------
70
71# Another possibility: associate custom numeric integration configuration
72# as default for object 'landau'
73landau.setIntegratorConfig(customConfig)
74
75# Calculate integral over landau custom numeric integrator specified as
76# object default
77intLandau3 = landau.createIntegral({x})
78val3 = intLandau3.getVal()
79print(" [3] int_dx landau(x) = ", val3)
80
81# Another possibility: Change global default for 1D numeric integration
82# strategy on finite domains
83if not integratorGKNotExisting:
84 ROOT.RooAbsReal.defaultIntegratorConfig().method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D")
85
86 # Adjusting parameters of a speficic technique
87 # ---------------------------------------------------------------------------------------
88
89 # Adjust maximum number of steps of ROOT.RooIntegrator1D in the global
90 # default configuration
91 ROOT.RooAbsReal.defaultIntegratorConfig().getConfigSection("RooIntegrator1D").setRealValue("maxSteps", 30)
92
93 # Example of how to change the parameters of a numeric integrator
94 # (Each config section is a ROOT.RooArgSet with ROOT.RooRealVars holding real-valued parameters
95 # and ROOT.RooCategories holding parameters with a finite set of options)
96 customConfig.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg", 50)
97 customConfig.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setCatLabel("method", "15Points")
98
99 # Example of how to print set of possible values for "method" category
100 customConfig.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").find("method").Print("v")
void Print(std::ostream &os, const OptionType &opt)