{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58c22f19",
   "metadata": {},
   "source": [
    "# hist002_TH1_fillrandom_userfunc\n",
    "Fill a 1D histogram from a user-defined parametric function.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Rene Brun, Giacomo Parolini  \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:11 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6d2a8397",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:43.132240Z",
     "iopub.status.busy": "2026-05-19T20:11:43.132123Z",
     "iopub.status.idle": "2026-05-19T20:11:44.104860Z",
     "shell.execute_reply": "2026-05-19T20:11:44.104456Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf8bb393",
   "metadata": {},
   "source": [
    "Create a user-defined formula.\n",
    "A function (any dimension) or a formula may reference an already defined formula"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9476fbf4",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:44.120429Z",
     "iopub.status.busy": "2026-05-19T20:11:44.120273Z",
     "iopub.status.idle": "2026-05-19T20:11:44.258814Z",
     "shell.execute_reply": "2026-05-19T20:11:44.258386Z"
    }
   },
   "outputs": [],
   "source": [
    "form1 = ROOT.TFormula(\"form1\", \"abs(sin(x)/x)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cb9574c",
   "metadata": {},
   "source": [
    "Create a 1D function using the formula defined above and the predefined \"gaus\" formula."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9b3745ca",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:44.275426Z",
     "iopub.status.busy": "2026-05-19T20:11:44.275271Z",
     "iopub.status.idle": "2026-05-19T20:11:44.405053Z",
     "shell.execute_reply": "2026-05-19T20:11:44.404400Z"
    }
   },
   "outputs": [],
   "source": [
    "rangeMin = 0.0\n",
    "rangeMax = 10.0\n",
    "sqroot = ROOT.TF1(\"sqroot\", \"x*gaus(0) + [3]*form1\", rangeMin, rangeMax)\n",
    "sqroot.SetLineColor(4)\n",
    "sqroot.SetLineWidth(6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec8a2852",
   "metadata": {},
   "source": [
    "Set parameters to the functions \"gaus\" and \"form1\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e5f0ae9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:44.407092Z",
     "iopub.status.busy": "2026-05-19T20:11:44.406959Z",
     "iopub.status.idle": "2026-05-19T20:11:44.519662Z",
     "shell.execute_reply": "2026-05-19T20:11:44.519006Z"
    }
   },
   "outputs": [],
   "source": [
    "gausScale = 10.0  # [0]\n",
    "gausMean = 4.0    # [1]\n",
    "gausVar = 1.0     # [2]\n",
    "form1Scale = 20.0 # [3]\n",
    "sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ad65472",
   "metadata": {},
   "source": [
    "Create a one dimensional histogram and fill it following the distribution in function sqroot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de9890b8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:44.534451Z",
     "iopub.status.busy": "2026-05-19T20:11:44.534308Z",
     "iopub.status.idle": "2026-05-19T20:11:44.659375Z",
     "shell.execute_reply": "2026-05-19T20:11:44.658740Z"
    }
   },
   "outputs": [],
   "source": [
    "h1d = ROOT.TH1D(\"h1d\", \"Test random numbers\", 200, rangeMin, rangeMax)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6a7f8e9",
   "metadata": {},
   "source": [
    "Use our user-defined function to fill the histogram with random values sampled from it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f8b7b7bd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:44.661508Z",
     "iopub.status.busy": "2026-05-19T20:11:44.661378Z",
     "iopub.status.idle": "2026-05-19T20:11:44.785194Z",
     "shell.execute_reply": "2026-05-19T20:11:44.783857Z"
    }
   },
   "outputs": [],
   "source": [
    "h1d.FillRandom(\"sqroot\", 10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f8fe442",
   "metadata": {},
   "source": [
    "Open a ROOT file and save the formula, function and histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "43674b3e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:44.799224Z",
     "iopub.status.busy": "2026-05-19T20:11:44.799081Z",
     "iopub.status.idle": "2026-05-19T20:11:45.009156Z",
     "shell.execute_reply": "2026-05-19T20:11:45.008523Z"
    }
   },
   "outputs": [],
   "source": [
    "with ROOT.TFile.Open(\"fillrandom_userfunc_py.root\", \"RECREATE\") as myFile:\n",
    "   myFile.WriteObject(form1, form1.GetName())\n",
    "   myFile.WriteObject(sqroot, sqroot.GetName())\n",
    "   myFile.WriteObject(h1d, h1d.GetName())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be204d96",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c319701f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:45.012695Z",
     "iopub.status.busy": "2026-05-19T20:11:45.012549Z",
     "iopub.status.idle": "2026-05-19T20:11:45.125526Z",
     "shell.execute_reply": "2026-05-19T20:11:45.125128Z"
    }
   },
   "outputs": [],
   "source": [
    "from ROOT import gROOT \n",
    "gROOT.GetListOfCanvases().Draw()"
   ]
  }
 ],
 "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
}
