{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2682c6cc",
   "metadata": {},
   "source": [
    "# rf902_numgenconfig\n",
    "Numeric algorithm tuning: configuration and customization of how MC sampling algorithms\n",
    "on specific pdfs are executed\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:**  Clemens Lange, Wouter Verkerke (C++ version)  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:35 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1764d14c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:32.605691Z",
     "iopub.status.busy": "2026-05-19T20:35:32.605540Z",
     "iopub.status.idle": "2026-05-19T20:35:33.618873Z",
     "shell.execute_reply": "2026-05-19T20:35:33.608408Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe263109",
   "metadata": {},
   "source": [
    "Adjust global MC sampling strategy\n",
    "------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee7cc084",
   "metadata": {},
   "source": [
    "Example pdf for use below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c5b2dc0b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:33.620513Z",
     "iopub.status.busy": "2026-05-19T20:35:33.620378Z",
     "iopub.status.idle": "2026-05-19T20:35:33.861981Z",
     "shell.execute_reply": "2026-05-19T20:35:33.861351Z"
    }
   },
   "outputs": [],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", 0, 10)\n",
    "model = ROOT.RooChebychev(\"model\", \"model\", x, [0.0, 0.5, -0.1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e0bfbec",
   "metadata": {},
   "source": [
    "Change global strategy for 1D sampling problems without conditional observable\n",
    "(1st kFALSE) and without discrete observable (2nd kFALSE) from ROOT.RooFoamGenerator,\n",
    "( an interface to the ROOT.TFoam MC generator with adaptive subdivisioning strategy ) to ROOT.RooAcceptReject,\n",
    "a plain accept/reject sampling algorithm [ ROOT.RooFit default before\n",
    "ROOT 5.23/04 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a38b8732",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:33.864302Z",
     "iopub.status.busy": "2026-05-19T20:35:33.864168Z",
     "iopub.status.idle": "2026-05-19T20:35:34.003120Z",
     "shell.execute_reply": "2026-05-19T20:35:33.996646Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#0] ERROR:InputArguments -- Trying to set invalid state label 'RooAcceptReject' for category method1D\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ROOT.RooAbsPdf.defaultGeneratorConfig().method1D(False, False).setLabel(\"RooAcceptReject\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3763f47f",
   "metadata": {},
   "source": [
    "Generate 10Kevt using ROOT.RooAcceptReject"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3f3978cd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:34.005389Z",
     "iopub.status.busy": "2026-05-19T20:35:34.005212Z",
     "iopub.status.idle": "2026-05-19T20:35:34.174437Z",
     "shell.execute_reply": "2026-05-19T20:35:34.173966Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " --- RooGenContext --- \n",
      "Using PDF RooChebychev::model[ x=x coefList=(0,0.5,-0.1) ]\n",
      "Use PDF generator for ()\n",
      "Use MC sampling generator RooFoamGenerator for (x)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RooDataSet::modelData[x] = 10000 entries\n"
     ]
    }
   ],
   "source": [
    "data_ar = model.generate({x}, 10000, Verbose=True)\n",
    "data_ar.Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ab39429",
   "metadata": {},
   "source": [
    "Adjusting default config for a specific pdf\n",
    "-------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48cad991",
   "metadata": {},
   "source": [
    "Another possibility: associate custom MC sampling configuration as default for object 'model'\n",
    "The kTRUE argument will install a clone of the default configuration as specialized configuration\n",
    "for self model if none existed so far"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b0232ccc",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:34.183187Z",
     "iopub.status.busy": "2026-05-19T20:35:34.183048Z",
     "iopub.status.idle": "2026-05-19T20:35:34.292535Z",
     "shell.execute_reply": "2026-05-19T20:35:34.291526Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.specialGeneratorConfig(True).method1D(False, False).setLabel(\"RooFoamGenerator\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3eb07d54",
   "metadata": {},
   "source": [
    "Adjusting parameters of a specific technique\n",
    "---------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5561842a",
   "metadata": {},
   "source": [
    "Adjust maximum number of steps of ROOT.RooIntegrator1D in the global\n",
    "default configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "239028d9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:34.294216Z",
     "iopub.status.busy": "2026-05-19T20:35:34.294087Z",
     "iopub.status.idle": "2026-05-19T20:35:34.410169Z",
     "shell.execute_reply": "2026-05-19T20:35:34.409121Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ROOT.RooAbsPdf.defaultGeneratorConfig().getConfigSection(\"RooAcceptReject\").setRealValue(\"nTrial1D\", 2000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba9b6ba7",
   "metadata": {},
   "source": [
    "Example of how to change the parameters of a numeric integrator\n",
    "(Each config section is a ROOT.RooArgSet with ROOT.RooRealVars holding real-valued parameters\n",
    " and ROOT.RooCategories holding parameters with a finite set of options)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6bd6e1f1",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:34.411861Z",
     "iopub.status.busy": "2026-05-19T20:35:34.411733Z",
     "iopub.status.idle": "2026-05-19T20:35:34.520996Z",
     "shell.execute_reply": "2026-05-19T20:35:34.520392Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.specialGeneratorConfig().getConfigSection(\"RooFoamGenerator\").setRealValue(\"chatLevel\", 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fa9cd8d",
   "metadata": {},
   "source": [
    "Generate 10Kevt using ROOT.RooFoamGenerator (FOAM verbosity increased\n",
    "with above chatLevel adjustment for illustration purposes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "87ffec7e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:35:34.522993Z",
     "iopub.status.busy": "2026-05-19T20:35:34.522866Z",
     "iopub.status.idle": "2026-05-19T20:35:34.635212Z",
     "shell.execute_reply": "2026-05-19T20:35:34.634349Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n",
      "F                                                                              F\n",
      "F                   ****************************************                   F\n",
      "F                    ******      TFoam::Initialize    ******                   F\n",
      "F                   ****************************************                   F\n",
      "F                                                      TFOAM                   F\n",
      "F    Version =           1.02M   =                   Release date:  2005.04.10 F\n",
      "F       kDim =          1 =  Dimension of the hyper-cubical space              F\n",
      "F     nCells =         30 =  Requested number of Cells (half of them active)   F\n",
      "F     nSampl =        200 =  No of MC events in exploration of a cell          F\n",
      "F       nBin =          8 =  No of bins in histograms, MC exploration of cell  F\n",
      "F   EvPerBin =         25 =  Maximum No effective_events/bin, MC exploration   F\n",
      "F   OptDrive =          2 =  Type of Driver   =1,2 for Sigma,WtMax             F\n",
      "F     OptRej =          1 =  MC rejection on/off for OptRej=0,1                F\n",
      "F   MaxWtRej =             1.1   =       Maximum wt in rejection for wt=1 evts F\n",
      "F                                                                              F\n",
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n",
      "11\n",
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n",
      "F                                                                              F\n",
      "F                    ***  TFoam::Initialize FINISHED!!!  ***                   F\n",
      "F     nCalls =       5800 =            Total number of function calls          F\n",
      "F     XPrime =      0.10992972   =     Primary total integral                  F\n",
      "F     XDiver =     0.010000374   =     Driver  total integral                  F\n",
      "F   mcResult =     0.099929343   =     Estimate of the true MC Integral        F\n",
      "F                                                                              F\n",
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n",
      " --- RooGenContext --- \n",
      "Using PDF RooChebychev::model[ x=x coefList=(0,0.5,-0.1) ]\n",
      "Use PDF generator for ()\n",
      "Use MC sampling generator RooFoamGenerator for (x)\n",
      "RooDataSet::modelData[x] = 10000 entries\n"
     ]
    }
   ],
   "source": [
    "data_foam = model.generate({x}, 10000, Verbose=True)\n",
    "data_foam.Print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
