{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "13e22b2f",
   "metadata": {},
   "source": [
    "# hist001_TH1_fillrandom\n",
    "Fill a 1D histogram with random values using predefined functions.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** 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:07 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f303ce0",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:07:42.995534Z",
     "iopub.status.busy": "2026-05-19T20:07:42.995418Z",
     "iopub.status.idle": "2026-05-19T20:07:43.940342Z",
     "shell.execute_reply": "2026-05-19T20:07:43.939810Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33ef0070",
   "metadata": {},
   "source": [
    "Create a one dimensional histogram and fill it with a gaussian distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72d97840",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:07:43.942130Z",
     "iopub.status.busy": "2026-05-19T20:07:43.941985Z",
     "iopub.status.idle": "2026-05-19T20:07:44.073812Z",
     "shell.execute_reply": "2026-05-19T20:07:44.073353Z"
    }
   },
   "outputs": [],
   "source": [
    "h1d = ROOT.TH1D(\"h1d\", \"Test random numbers\", nbinsx = 200, xlow = 0.0, xup = 10.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09ae310e",
   "metadata": {},
   "source": [
    "\"gaus\" is a predefined ROOT function. Here we are filling the histogram with\n",
    "10000 values sampled from that distribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1e8afcd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:07:44.075937Z",
     "iopub.status.busy": "2026-05-19T20:07:44.075810Z",
     "iopub.status.idle": "2026-05-19T20:07:44.272585Z",
     "shell.execute_reply": "2026-05-19T20:07:44.272060Z"
    }
   },
   "outputs": [],
   "source": [
    "h1d.FillRandom(\"gaus\", 10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b72096c",
   "metadata": {},
   "source": [
    "Open a ROOT file and save the histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d9964cf9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:07:44.274356Z",
     "iopub.status.busy": "2026-05-19T20:07:44.274229Z",
     "iopub.status.idle": "2026-05-19T20:07:44.433187Z",
     "shell.execute_reply": "2026-05-19T20:07:44.432631Z"
    }
   },
   "outputs": [],
   "source": [
    "with ROOT.TFile.Open(\"fillrandom_py.root\", \"RECREATE\") as myfile:\n",
    "   myfile.WriteObject(h1d, h1d.GetName())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9aaf0e1",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fd994b45",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:07:44.435363Z",
     "iopub.status.busy": "2026-05-19T20:07:44.435236Z",
     "iopub.status.idle": "2026-05-19T20:07:44.542830Z",
     "shell.execute_reply": "2026-05-19T20:07:44.542266Z"
    }
   },
   "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
}
