{
"cells": [
{
"cell_type": "markdown",
"id": "cfd36a46",
"metadata": {},
"source": [
"# rf901_numintconfig\n",
"Numeric algorithm tuning: configuration and customization of how numeric (partial) integrals are executed\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Wouter Verkerke \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:17 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8f22ab47",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:05.847878Z",
"iopub.status.busy": "2024-03-19T19:18:05.847308Z",
"iopub.status.idle": "2024-03-19T19:18:05.899996Z",
"shell.execute_reply": "2024-03-19T19:18:05.882144Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"RooNumIntConfig.h\"\n",
"#include \"RooLandau.h\"\n",
"#include \"RooArgSet.h\"\n",
"#include \n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "0be56947",
"metadata": {},
"source": [
"Adjust global 1D integration precision\n",
"----------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "e00f327c",
"metadata": {},
"source": [
"Print current global default configuration for numeric integration strategies"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c063a6a3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:05.911552Z",
"iopub.status.busy": "2024-03-19T19:18:05.911172Z",
"iopub.status.idle": "2024-03-19T19:18:06.737353Z",
"shell.execute_reply": "2024-03-19T19:18:06.735084Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requested precision: 1e-07 absolute, 1e-07 relative\n",
"\n",
"1-D integration method: RooIntegrator1D (RooImproperIntegrator1D if open-ended)\n",
"2-D integration method: RooAdaptiveIntegratorND (N/A if open-ended)\n",
"N-D integration method: RooAdaptiveIntegratorND (N/A if open-ended)\n",
"\n",
"Available integration methods:\n",
"\n",
"*** RooBinIntegrator ***\n",
"Capabilities: [1-D] [2-D] [N-D] \n",
"Configuration: \n",
" 1) numBins = 100\n",
"\n",
"*** RooIntegrator1D ***\n",
"Capabilities: [1-D] \n",
"Configuration: \n",
" 1) sumRule = Trapezoid(idx = 0)\n",
"\n",
" 2) extrapolation = Wynn-Epsilon(idx = 1)\n",
"\n",
" 3) maxSteps = 20\n",
" 4) minSteps = 999\n",
" 5) fixSteps = 0\n",
"\n",
"*** RooIntegrator2D ***\n",
"Capabilities: [2-D] \n",
"Configuration: \n",
"(Depends on 'RooIntegrator1D')\n",
"\n",
"*** RooSegmentedIntegrator1D ***\n",
"Capabilities: [1-D] \n",
"Configuration: \n",
" 1) numSeg = 3\n",
"(Depends on 'RooIntegrator1D')\n",
"\n",
"*** RooSegmentedIntegrator2D ***\n",
"Capabilities: [2-D] \n",
"Configuration: \n",
"(Depends on 'RooSegmentedIntegrator1D')\n",
"\n",
"*** RooImproperIntegrator1D ***\n",
"Capabilities: [1-D] [OpenEnded] \n",
"Configuration: \n",
"(Depends on 'RooIntegrator1D')\n",
"\n",
"*** RooMCIntegrator ***\n",
"Capabilities: [1-D] [2-D] [N-D] \n",
"Configuration: \n",
" 1) samplingMode = Importance(idx = 0)\n",
"\n",
" 2) genType = QuasiRandom(idx = 0)\n",
"\n",
" 3) verbose = false(idx = 0)\n",
"\n",
" 4) alpha = 1.5\n",
" 5) nRefineIter = 5\n",
" 6) nRefinePerDim = 1000\n",
" 7) nIntPerDim = 5000\n",
"\n",
"*** RooAdaptiveIntegratorND ***\n",
"Capabilities: [2-D] [N-D] \n",
"Configuration: \n",
" 1) maxEval2D = 100000\n",
" 2) maxEval3D = 1e+06\n",
" 3) maxEvalND = 1e+07\n",
" 4) maxWarn = 5\n",
"\n",
"*** RooAdaptiveGaussKronrodIntegrator1D ***\n",
"Capabilities: [1-D] [OpenEnded] \n",
"Configuration: \n",
" 1) maxSeg = 100\n",
" 2) method = 21Points(idx = 2)\n",
"\n",
"\n",
"*** RooGaussKronrodIntegrator1D ***\n",
"Capabilities: [1-D] [OpenEnded] \n",
"Configuration: \n",
"\n"
]
}
],
"source": [
"RooAbsReal::defaultIntegratorConfig()->Print(\"v\");"
]
},
{
"cell_type": "markdown",
"id": "26349af4",
"metadata": {},
"source": [
"Example: Change global precision for 1D integrals from 1e-7 to 1e-6\n",
"\n",
"The relative epsilon (change as fraction of current best integral estimate) and\n",
"absolute epsilon (absolute change w.r.t last best integral estimate) can be specified\n",
"separately. For most pdf integrals the relative change criterium is the most important,\n",
"however for certain non-pdf functions that integrate out to zero a separate absolute\n",
"change criterium is necessary to declare convergence of the integral\n",
"\n",
"NB: This change is for illustration only. In general the precision should be at least 1e-7\n",
"for normalization integrals for MINUIT to succeed."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a311e929",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:06.742554Z",
"iopub.status.busy": "2024-03-19T19:18:06.742139Z",
"iopub.status.idle": "2024-03-19T19:18:06.962126Z",
"shell.execute_reply": "2024-03-19T19:18:06.961040Z"
}
},
"outputs": [],
"source": [
"RooAbsReal::defaultIntegratorConfig()->setEpsAbs(1e-6);\n",
"RooAbsReal::defaultIntegratorConfig()->setEpsRel(1e-6);"
]
},
{
"cell_type": "markdown",
"id": "8ef9c6fe",
"metadata": {},
"source": [
"Numeric integration of landau pdf\n",
"------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0bcd6e02",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:06.978101Z",
"iopub.status.busy": "2024-03-19T19:18:06.977714Z",
"iopub.status.idle": "2024-03-19T19:18:07.221062Z",
"shell.execute_reply": "2024-03-19T19:18:07.213221Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -10, 10);\n",
"RooLandau landau(\"landau\", \"landau\", x, 0.0, 0.1);"
]
},
{
"cell_type": "markdown",
"id": "3b8e5374",
"metadata": {},
"source": [
"Disable analytic integration from demonstration purposes"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0463fa3d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:07.242326Z",
"iopub.status.busy": "2024-03-19T19:18:07.241909Z",
"iopub.status.idle": "2024-03-19T19:18:07.551316Z",
"shell.execute_reply": "2024-03-19T19:18:07.500813Z"
}
},
"outputs": [],
"source": [
"landau.forceNumInt(true);"
]
},
{
"cell_type": "markdown",
"id": "491ee624",
"metadata": {},
"source": [
"Activate debug-level messages for topic integration to be able to follow actions below"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ef3e658f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:07.595712Z",
"iopub.status.busy": "2024-03-19T19:18:07.595312Z",
"iopub.status.idle": "2024-03-19T19:18:07.843115Z",
"shell.execute_reply": "2024-03-19T19:18:07.838548Z"
}
},
"outputs": [],
"source": [
"RooMsgService::instance().addStream(DEBUG, Topic(Integration));"
]
},
{
"cell_type": "markdown",
"id": "9702c544",
"metadata": {},
"source": [
"Calculate integral over landau with default choice of numeric integrator"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7e750063",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:07.852632Z",
"iopub.status.busy": "2024-03-19T19:18:07.852226Z",
"iopub.status.idle": "2024-03-19T19:18:08.234497Z",
"shell.execute_reply": "2024-03-19T19:18:08.229608Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#3] INFO:Integration -- RooRealIntegral::ctor(landau_Int[x]) Constructing integral of function landau over observables(x) with normalization () with range identifier \n",
"[#3] DEBUG:Integration -- landau: Adding observable x as shape dependent\n",
"[#3] DEBUG:Integration -- landau: Adding parameter 0 as value dependent\n",
"[#3] DEBUG:Integration -- landau: Adding parameter 0.1 as value dependent\n",
"[#3] INFO:Integration -- landau: Observable x is suitable for analytical integration (if supported by p.d.f)\n",
"[#3] INFO:Integration -- landau: Observables (x) are numerically integrated\n",
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(landau_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)\n",
" [1] int_dx landau(x) = 0.0989653362054419\n"
]
}
],
"source": [
"std::unique_ptr intLandau{landau.createIntegral(x)};\n",
"double val = intLandau->getVal();\n",
"cout << \" [1] int_dx landau(x) = \" << setprecision(15) << val << endl;"
]
},
{
"cell_type": "markdown",
"id": "8edb80e4",
"metadata": {},
"source": [
"Same with custom configuration\n",
"-----------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "c3037668",
"metadata": {},
"source": [
"Construct a custom configuration which uses the adaptive Gauss-Kronrod technique\n",
"for closed 1D integrals"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c6c3bd1c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:08.239717Z",
"iopub.status.busy": "2024-03-19T19:18:08.239354Z",
"iopub.status.idle": "2024-03-19T19:18:08.459639Z",
"shell.execute_reply": "2024-03-19T19:18:08.458545Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Unbalanced braces. This cell was not processed.\n"
]
}
],
"source": [
"RooNumIntConfig customConfig(*RooAbsReal::defaultIntegratorConfig());\n",
"#ifdef R__HAS_MATHMORE\n",
"customConfig.method1D().setLabel(\"RooAdaptiveGaussKronrodIntegrator1D\");\n",
"#else\n",
"Warning(\"rf901_numintconfig\",\"ROOT is built without Mathmore (GSL) support. Cannot use RooAdaptiveGaussKronrodIntegrator1D\");\n",
"#endif"
]
},
{
"cell_type": "markdown",
"id": "c7d80840",
"metadata": {},
"source": [
"Calculate integral over landau with custom integral specification"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "dff90861",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:08.468263Z",
"iopub.status.busy": "2024-03-19T19:18:08.467885Z",
"iopub.status.idle": "2024-03-19T19:18:09.302514Z",
"shell.execute_reply": "2024-03-19T19:18:09.290212Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-314, _ZN5cling7runtime8internal15DynamicExprInfoC2EPKcPPvb, _ZN12__cling_N5274val2E, _ZN12__cling_N52724__dynamic__cling_Un1Qu30E, _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb, _ZN12__cling_N52716__cling_Un1Qu326EPv, _GLOBAL__sub_I_cling_module_314, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, __cxx_global_var_initcling_module_314_.3, __cxx_global_var_initcling_module_314_.2, $.cling-module-314.__inits.0, _ZN12__cling_N52710intLandau2E, __cxx_global_var_initcling_module_314_ }) }\n",
"IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]!\n",
"You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*)\n",
"Maybe you need to load the corresponding shared library?\n",
"IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]!\n",
"You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler()\n",
"Maybe you need to load the corresponding shared library?\n"
]
}
],
"source": [
"std::unique_ptr intLandau2{landau.createIntegral(x, NumIntConfig(customConfig))};\n",
"double val2 = intLandau2->getVal();\n",
"cout << \" [2] int_dx landau(x) = \" << val2 << endl;"
]
},
{
"cell_type": "markdown",
"id": "5ef34c5c",
"metadata": {},
"source": [
"Adjusting default config for a specific pdf\n",
"-------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "f6158753",
"metadata": {},
"source": [
"Another possibility: associate custom numeric integration configuration as default for object 'landau'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "335485c7",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:09.326682Z",
"iopub.status.busy": "2024-03-19T19:18:09.326299Z",
"iopub.status.idle": "2024-03-19T19:18:09.546402Z",
"shell.execute_reply": "2024-03-19T19:18:09.545458Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-314 }) }\n",
"cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }\n"
]
}
],
"source": [
"landau.setIntegratorConfig(customConfig);"
]
},
{
"cell_type": "markdown",
"id": "39ded71d",
"metadata": {},
"source": [
"Calculate integral over landau custom numeric integrator specified as object default"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "daa6b3d3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:09.570856Z",
"iopub.status.busy": "2024-03-19T19:18:09.570463Z",
"iopub.status.idle": "2024-03-19T19:18:09.789956Z",
"shell.execute_reply": "2024-03-19T19:18:09.788840Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#3] INFO:Integration -- RooRealIntegral::ctor(landau_Int[x]) Constructing integral of function landau over observables(x) with normalization () with range identifier \n",
"[#3] DEBUG:Integration -- landau: Adding observable x as shape dependent\n",
"[#3] DEBUG:Integration -- landau: Adding parameter 0 as value dependent\n",
"[#3] DEBUG:Integration -- landau: Adding parameter 0.1 as value dependent\n",
"[#3] INFO:Integration -- landau: Observable x is suitable for analytical integration (if supported by p.d.f)\n",
"[#3] INFO:Integration -- landau: Observables (x) are numerically integrated\n",
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(landau_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)\n",
" [3] int_dx landau(x) = 0.0989653362054419\n"
]
}
],
"source": [
"std::unique_ptr intLandau3{landau.createIntegral(x)};\n",
"double val3 = intLandau3->getVal();\n",
"cout << \" [3] int_dx landau(x) = \" << val3 << endl;"
]
},
{
"cell_type": "markdown",
"id": "c5ad3ee9",
"metadata": {},
"source": [
"Another possibility: Change global default for 1D numeric integration strategy on finite domains"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "22b65b1c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:09.810260Z",
"iopub.status.busy": "2024-03-19T19:18:09.809867Z",
"iopub.status.idle": "2024-03-19T19:18:10.022659Z",
"shell.execute_reply": "2024-03-19T19:18:10.021191Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Unbalanced braces. This cell was not processed.\n"
]
}
],
"source": [
"#ifdef R__HAS_MATHMORE\n",
"RooAbsReal::defaultIntegratorConfig()->method1D().setLabel(\"RooAdaptiveGaussKronrodIntegrator1D\");"
]
},
{
"cell_type": "markdown",
"id": "8b12366d",
"metadata": {},
"source": [
"Adjusting parameters of a specific technique\n",
"---------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "3622c4b7",
"metadata": {},
"source": [
"Adjust maximum number of steps of RooIntegrator1D in the global default configuration"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "87a8ea20",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:10.071880Z",
"iopub.status.busy": "2024-03-19T19:18:10.071482Z",
"iopub.status.idle": "2024-03-19T19:18:10.309842Z",
"shell.execute_reply": "2024-03-19T19:18:10.308633Z"
}
},
"outputs": [],
"source": [
"RooAbsReal::defaultIntegratorConfig()->getConfigSection(\"RooIntegrator1D\").setRealValue(\"maxSteps\", 30);"
]
},
{
"cell_type": "markdown",
"id": "deb5e343",
"metadata": {},
"source": [
"Example of how to change the parameters of a numeric integrator\n",
"(Each config section is a RooArgSet with RooRealVars holding real-valued parameters\n",
"and RooCategories holding parameters with a finite set of options)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "f5a1811f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:10.320456Z",
"iopub.status.busy": "2024-03-19T19:18:10.320073Z",
"iopub.status.idle": "2024-03-19T19:18:10.533221Z",
"shell.execute_reply": "2024-03-19T19:18:10.532162Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE }) }\n"
]
}
],
"source": [
"customConfig.getConfigSection(\"RooAdaptiveGaussKronrodIntegrator1D\").setRealValue(\"maxSeg\", 50);\n",
"customConfig.getConfigSection(\"RooAdaptiveGaussKronrodIntegrator1D\").setCatLabel(\"method\", \"15Points\");"
]
},
{
"cell_type": "markdown",
"id": "15efea99",
"metadata": {},
"source": [
"Example of how to print set of possible values for \"method\" category"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "bca25470",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:18:10.549328Z",
"iopub.status.busy": "2024-03-19T19:18:10.548946Z",
"iopub.status.idle": "2024-03-19T19:18:10.813457Z",
"shell.execute_reply": "2024-03-19T19:18:10.798506Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_61:3:2: error: #endif without #if\n",
"#endif\n",
" ^\n"
]
}
],
"source": [
"customConfig.getConfigSection(\"RooAdaptiveGaussKronrodIntegrator1D\").find(\"method\")->Print(\"v\");\n",
"#endif"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}