{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "59b2e230",
   "metadata": {},
   "source": [
    "# rf505_asciicfg\n",
    "Organization and simultaneous fits: reading and writing ASCII configuration files\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:32 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "02997120",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:19.701499Z",
     "iopub.status.busy": "2026-05-19T20:32:19.701382Z",
     "iopub.status.idle": "2026-05-19T20:32:20.688291Z",
     "shell.execute_reply": "2026-05-19T20:32:20.687832Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9788077f",
   "metadata": {},
   "source": [
    "Create pdf\n",
    "------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ddf87fd",
   "metadata": {},
   "source": [
    "Construct gauss(x,m,s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0cc2a332",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:20.689896Z",
     "iopub.status.busy": "2026-05-19T20:32:20.689764Z",
     "iopub.status.idle": "2026-05-19T20:32:20.874238Z",
     "shell.execute_reply": "2026-05-19T20:32:20.873784Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#0] WARNING:InputArguments -- The parameter 's' with range [-10, 10] of the RooGaussian 'g' exceeds the safe range of (0, inf). Advise to limit its range.\n"
     ]
    }
   ],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", -10, 10)\n",
    "m = ROOT.RooRealVar(\"m\", \"m\", 0, -10, 10)\n",
    "s = ROOT.RooRealVar(\"s\", \"s\", 1, -10, 10)\n",
    "gauss = ROOT.RooGaussian(\"g\", \"g\", x, m, s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d9dfd13",
   "metadata": {},
   "source": [
    "Construct poly(x,p0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7c154555",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:20.875921Z",
     "iopub.status.busy": "2026-05-19T20:32:20.875803Z",
     "iopub.status.idle": "2026-05-19T20:32:21.070061Z",
     "shell.execute_reply": "2026-05-19T20:32:21.069117Z"
    }
   },
   "outputs": [],
   "source": [
    "p0 = ROOT.RooRealVar(\"p0\", \"p0\", 0.01, 0.0, 1.0)\n",
    "poly = ROOT.RooPolynomial(\"p\", \"p\", x, [p0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20d1dae6",
   "metadata": {},
   "source": [
    "model = f*gauss(x) + (1-f)*poly(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6db97a25",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.071680Z",
     "iopub.status.busy": "2026-05-19T20:32:21.071525Z",
     "iopub.status.idle": "2026-05-19T20:32:21.184171Z",
     "shell.execute_reply": "2026-05-19T20:32:21.183248Z"
    }
   },
   "outputs": [],
   "source": [
    "f = ROOT.RooRealVar(\"f\", \"f\", 0.5, 0.0, 1.0)\n",
    "model = ROOT.RooAddPdf(\"model\", \"model\", [gauss, poly], [f])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5c052a4",
   "metadata": {},
   "source": [
    "Fit model to toy data\n",
    "-----------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "669c14c5",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.185735Z",
     "iopub.status.busy": "2026-05-19T20:32:21.185579Z",
     "iopub.status.idle": "2026-05-19T20:32:21.415836Z",
     "shell.execute_reply": "2026-05-19T20:32:21.414941Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data\n",
      "[#1] INFO:Fitting -- using generic CPU library compiled with no vectorizations\n",
      "[#1] INFO:Fitting -- Creation of NLL object took 778.604 μs\n",
      "[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n",
      "[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<cppyy.gbl.RooFitResult object at 0x(nil)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = model.generate({x}, 1000)\n",
    "model.fitTo(d, PrintLevel=-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a59a189e",
   "metadata": {},
   "source": [
    "Write parameters to ASCII file\n",
    "-----------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a966a00a",
   "metadata": {},
   "source": [
    "Obtain set of parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8244111f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.417432Z",
     "iopub.status.busy": "2026-05-19T20:32:21.417298Z",
     "iopub.status.idle": "2026-05-19T20:32:21.526274Z",
     "shell.execute_reply": "2026-05-19T20:32:21.525399Z"
    }
   },
   "outputs": [],
   "source": [
    "params = model.getParameters({x})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b57450f",
   "metadata": {},
   "source": [
    "Write parameters to file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5b8e6a08",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.527848Z",
     "iopub.status.busy": "2026-05-19T20:32:21.527710Z",
     "iopub.status.idle": "2026-05-19T20:32:21.635159Z",
     "shell.execute_reply": "2026-05-19T20:32:21.634624Z"
    }
   },
   "outputs": [],
   "source": [
    "params.writeToFile(\"rf505_asciicfg_example.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59db317d",
   "metadata": {},
   "source": [
    "Read parameters from ASCII file\n",
    "----------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12df3c40",
   "metadata": {},
   "source": [
    "Read parameters from file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e3d32f7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.644644Z",
     "iopub.status.busy": "2026-05-19T20:32:21.644482Z",
     "iopub.status.idle": "2026-05-19T20:32:21.757829Z",
     "shell.execute_reply": "2026-05-19T20:32:21.757347Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  1) 0x5639bafd8c20 RooRealVar::  f = 0.50733 +/- 0.020971  L(0 - 1)  \"f\"\n",
      "  2) 0x5639baeffbb0 RooRealVar::  m = 0.0064018 +/- 0.053686  L(-10 - 10)  \"m\"\n",
      "  3) 0x5639b84a8d60 RooRealVar:: p0 = 0.0073509 +/- 0.0078312  L(0 - 1)  \"p0\"\n",
      "  4) 0x5639baefdc50 RooRealVar::  s = 0.96516 +/- 0.047052  L(-10 - 10)  \"s\"\n"
     ]
    }
   ],
   "source": [
    "params.readFromFile(\"rf505_asciicfg_example.txt\")\n",
    "params.Print(\"v\")\n",
    "\n",
    "configFile = ROOT.gROOT.GetTutorialDir().Data() + \"/roofit/rf505_asciicfg.txt\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dfdbb50",
   "metadata": {},
   "source": [
    "Read parameters from section 'Section2' of file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "55e70be6",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.766273Z",
     "iopub.status.busy": "2026-05-19T20:32:21.766118Z",
     "iopub.status.idle": "2026-05-19T20:32:21.870670Z",
     "shell.execute_reply": "2026-05-19T20:32:21.870335Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#0] ERROR:InputArguments -- RooArgSet::readFromFile(parameters) error opening file /github/home/ROOT-CI/build/tutorials/roofit/rf505_asciicfg.txt\n",
      "  1) 0x5639bafd8c20 RooRealVar::  f = 0.50733 +/- 0.020971  L(0 - 1)  \"f\"\n",
      "  2) 0x5639baeffbb0 RooRealVar::  m = 0.0064018 +/- 0.053686  L(-10 - 10)  \"m\"\n",
      "  3) 0x5639b84a8d60 RooRealVar:: p0 = 0.0073509 +/- 0.0078312  L(0 - 1)  \"p0\"\n",
      "  4) 0x5639baefdc50 RooRealVar::  s = 0.96516 +/- 0.047052  L(-10 - 10)  \"s\"\n"
     ]
    }
   ],
   "source": [
    "params.readFromFile(configFile, \"\", \"Section2\")\n",
    "params.Print(\"v\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9e99091",
   "metadata": {},
   "source": [
    "Read parameters from section 'Section3' of file. Mark all\n",
    "variables that were processed with the \"READ\" attribute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9befa4b1",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.880723Z",
     "iopub.status.busy": "2026-05-19T20:32:21.880550Z",
     "iopub.status.idle": "2026-05-19T20:32:21.986168Z",
     "shell.execute_reply": "2026-05-19T20:32:21.985524Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#0] ERROR:InputArguments -- RooArgSet::readFromFile(parameters) error opening file /github/home/ROOT-CI/build/tutorials/roofit/rf505_asciicfg.txt\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "params.readFromFile(configFile, \"READ\", \"Section3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32bd04c5",
   "metadata": {},
   "source": [
    "Print the list of parameters that were not read from Section3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6bf2c0c0",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:21.988057Z",
     "iopub.status.busy": "2026-05-19T20:32:21.987929Z",
     "iopub.status.idle": "2026-05-19T20:32:22.123858Z",
     "shell.execute_reply": "2026-05-19T20:32:22.123203Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The following parameters of the were _not_ read from Section3:  { @0x5639bbb50aa0, @0x5639bbb50aa8, @0x5639bbb50ab0, @0x5639bbb50ab8 }\n"
     ]
    }
   ],
   "source": [
    "print(\"The following parameters of the were _not_ read from Section3: \", params.selectByAttrib(\"READ\", False))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82515bb7",
   "metadata": {},
   "source": [
    "Read parameters from section 'Section4' of file, contains\n",
    "'include file' statement of rf505_asciicfg_example.txt\n",
    "so that we effective read the same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "02086f4c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:22.125820Z",
     "iopub.status.busy": "2026-05-19T20:32:22.125681Z",
     "iopub.status.idle": "2026-05-19T20:32:22.230338Z",
     "shell.execute_reply": "2026-05-19T20:32:22.229737Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#0] ERROR:InputArguments -- RooArgSet::readFromFile(parameters) error opening file /github/home/ROOT-CI/build/tutorials/roofit/rf505_asciicfg.txt\n",
      "  1) 0x5639bafd8c20 RooRealVar::  f = 0.50733 +/- 0.020971  L(0 - 1)  \"f\"\n",
      "  2) 0x5639baeffbb0 RooRealVar::  m = 0.0064018 +/- 0.053686  L(-10 - 10)  \"m\"\n",
      "  3) 0x5639b84a8d60 RooRealVar:: p0 = 0.0073509 +/- 0.0078312  L(0 - 1)  \"p0\"\n",
      "  4) 0x5639baefdc50 RooRealVar::  s = 0.96516 +/- 0.047052  L(-10 - 10)  \"s\"\n"
     ]
    }
   ],
   "source": [
    "params.readFromFile(configFile, \"\", \"Section4\")\n",
    "params.Print(\"v\")"
   ]
  }
 ],
 "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
}
