{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19fd0f90",
   "metadata": {},
   "source": [
    "# hist002_TH1_fillrandom_userfunc_uhi\n",
    "Fill a 1D histogram from a user-defined parametric function.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Rene Brun, Giacomo Parolini, Nursena Bitirgen  \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": "553e597e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:45.073749Z",
     "iopub.status.busy": "2026-05-19T20:11:45.073598Z",
     "iopub.status.idle": "2026-05-19T20:11:46.211054Z",
     "shell.execute_reply": "2026-05-19T20:11:46.210448Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09034711",
   "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": "dc07168f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:46.215232Z",
     "iopub.status.busy": "2026-05-19T20:11:46.215020Z",
     "iopub.status.idle": "2026-05-19T20:11:46.350273Z",
     "shell.execute_reply": "2026-05-19T20:11:46.349552Z"
    }
   },
   "outputs": [],
   "source": [
    "form1 = ROOT.TFormula(\"form1\", \"abs(sin(x)/x)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "967a3f5f",
   "metadata": {},
   "source": [
    "Create a 1D function using the formula defined above and the predefined \"gaus\" formula."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3d5e65cb",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:46.353466Z",
     "iopub.status.busy": "2026-05-19T20:11:46.353327Z",
     "iopub.status.idle": "2026-05-19T20:11:46.488120Z",
     "shell.execute_reply": "2026-05-19T20:11:46.487499Z"
    }
   },
   "outputs": [],
   "source": [
    "rangeMin = 0.0\n",
    "rangeMax = 10.0\n",
    "sqroot = ROOT.TF1(\"sqroot\", \"x*gaus(0) + [3]*form1\", rangeMin, rangeMax)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d434225",
   "metadata": {},
   "source": [
    "Set parameters to the functions \"gaus\" and \"form1\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e4f31fbd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:46.490092Z",
     "iopub.status.busy": "2026-05-19T20:11:46.489963Z",
     "iopub.status.idle": "2026-05-19T20:11:46.602812Z",
     "shell.execute_reply": "2026-05-19T20:11:46.602111Z"
    }
   },
   "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": "283e8613",
   "metadata": {},
   "source": [
    "Create a one dimensional histogram and fill it following the distribution in function sqroot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3e0e928e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:46.604492Z",
     "iopub.status.busy": "2026-05-19T20:11:46.604363Z",
     "iopub.status.idle": "2026-05-19T20:11:46.729129Z",
     "shell.execute_reply": "2026-05-19T20:11:46.728378Z"
    }
   },
   "outputs": [],
   "source": [
    "h1d = ROOT.TH1D(\"h1d\", \"Test random numbers\", 200, rangeMin, rangeMax)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d683047",
   "metadata": {},
   "source": [
    "Use our user-defined function to fill the histogram with random values sampled from it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "21dc5d97",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:46.731151Z",
     "iopub.status.busy": "2026-05-19T20:11:46.731020Z",
     "iopub.status.idle": "2026-05-19T20:11:46.862721Z",
     "shell.execute_reply": "2026-05-19T20:11:46.857278Z"
    }
   },
   "outputs": [],
   "source": [
    "h1d.Fill(np.array([sqroot.GetRandom() for _ in range(10000)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efee8466",
   "metadata": {},
   "source": [
    "Open a ROOT file and save the formula, function and histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3d309075",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:46.871219Z",
     "iopub.status.busy": "2026-05-19T20:11:46.871083Z",
     "iopub.status.idle": "2026-05-19T20:11:47.079529Z",
     "shell.execute_reply": "2026-05-19T20:11:47.079043Z"
    }
   },
   "outputs": [],
   "source": [
    "with ROOT.TFile.Open(\"fillrandom_userfunc_py_uhi.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": "37a25a89",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0698167f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:11:47.081109Z",
     "iopub.status.busy": "2026-05-19T20:11:47.080988Z",
     "iopub.status.idle": "2026-05-19T20:11:47.188352Z",
     "shell.execute_reply": "2026-05-19T20:11:47.187890Z"
    }
   },
   "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
}
