{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "20a3bd77",
   "metadata": {},
   "source": [
    "# hist015_TH1_read_and_draw_uhi\n",
    "A Simple histogram drawing example.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Wim Lavrijsen, 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:12 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2bc17b71",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:15.538221Z",
     "iopub.status.busy": "2026-05-19T20:12:15.538090Z",
     "iopub.status.idle": "2026-05-19T20:12:16.967370Z",
     "shell.execute_reply": "2026-05-19T20:12:16.967029Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 1400x1200 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import mplhep as hep\n",
    "import numpy as np\n",
    "import ROOT\n",
    "from ROOT import TFile, gROOT\n",
    "\n",
    "mpl_fig = plt.figure(figsize=(14, 12))\n",
    "gs = mpl_fig.add_gridspec(3, 2, height_ratios=[1.5, 1.5, 1.5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "526b9c58",
   "metadata": {},
   "source": [
    "We connect the ROOT file generated in the hsimple.py tutorial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0e8766c3",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:16.989486Z",
     "iopub.status.busy": "2026-05-19T20:12:16.989252Z",
     "iopub.status.idle": "2026-05-19T20:12:17.140591Z",
     "shell.execute_reply": "2026-05-19T20:12:17.139500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TFile**\t\tpy-hsimple.root\tDemo ROOT file with histograms\n",
      " TFile*\t\tpy-hsimple.root\tDemo ROOT file with histograms\n",
      "  KEY: TH1F\thpx;1\tThis is the px distribution\n",
      "  KEY: TH2F\thpxpy;1\tpy vs px\n",
      "  KEY: TProfile\thprof;1\tProfile of pz versus px\n",
      "  KEY: TNtuple\tntuple;1\tDemo ntuple\n"
     ]
    }
   ],
   "source": [
    "example = TFile(\"py-hsimple.root\")\n",
    "example.ls()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "665111b4",
   "metadata": {},
   "source": [
    "Draw histogram hpx in first pad."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a2352ce1",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:17.148330Z",
     "iopub.status.busy": "2026-05-19T20:12:17.148189Z",
     "iopub.status.idle": "2026-05-19T20:12:17.271531Z",
     "shell.execute_reply": "2026-05-19T20:12:17.270548Z"
    }
   },
   "outputs": [],
   "source": [
    "hpx = gROOT.FindObject(\"hpx\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "347f21ca",
   "metadata": {},
   "source": [
    "Get the statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "73948387",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:17.273201Z",
     "iopub.status.busy": "2026-05-19T20:12:17.273066Z",
     "iopub.status.idle": "2026-05-19T20:12:17.421044Z",
     "shell.execute_reply": "2026-05-19T20:12:17.420525Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.65, 0.95, 'Entries = 25000\\nMean = -0.00817\\nStd Dev = 0.998')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entries = int(hpx.GetEntries())\n",
    "mean = hpx.GetMean()\n",
    "std_dev = hpx.GetStdDev()\n",
    "info_text = f\"Entries = {entries}\\nMean = {mean:.5f}\\nStd Dev = {std_dev:.3f}\"\n",
    "\n",
    "ax1 = mpl_fig.add_subplot(gs[0:2, 0])\n",
    "ax1.set_facecolor(\"#FFFDD0C8\")\n",
    "hep.histplot(hpx, ax=ax1, histtype=\"fill\", color=\"#EEAC91\", alpha=0.5, edgecolor=\"blue\", linewidth=1.5)\n",
    "ax1.text(\n",
    "    0.65,\n",
    "    0.95,\n",
    "    info_text,\n",
    "    transform=ax1.transAxes,\n",
    "    verticalalignment=\"top\",\n",
    "    fontsize=10,\n",
    "    bbox=dict(boxstyle=\"round,pad=0.3\", facecolor=\"white\", alpha=0.7),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56c72f59",
   "metadata": {},
   "source": [
    "Draw hpx as an interactive lego."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2b2f4f5a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:17.423140Z",
     "iopub.status.busy": "2026-05-19T20:12:17.423002Z",
     "iopub.status.idle": "2026-05-19T20:12:17.549469Z",
     "shell.execute_reply": "2026-05-19T20:12:17.548407Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.55, 0.85, 'Entries = 25000\\nMean = -0.00817\\nStd Dev = 0.998')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax2 = mpl_fig.add_subplot(gs[0:2, 1], projection=\"3d\")\n",
    "ax2.set_facecolor(\"#FFFDD0C8\")\n",
    "x = np.array([hpx.GetBinCenter(i) for i in range(1, hpx.GetNbinsX() + 1)])\n",
    "y = np.zeros_like(x)\n",
    "z = np.zeros_like(x)\n",
    "dz = hpx.values()\n",
    "dx = np.full_like(x, hpx.GetBinWidth(1) * 0.9)\n",
    "dy = np.full_like(x, 0.2)\n",
    "ax2.bar3d(x, y, z, dx, dy, dz, color=\"#EEAC91\", edgecolor=\"darkblue\", alpha=0.85)\n",
    "ax2.text2D(\n",
    "    0.55,\n",
    "    0.85,\n",
    "    info_text,\n",
    "    transform=ax2.transAxes,\n",
    "    fontsize=10,\n",
    "    bbox=dict(boxstyle=\"round,pad=0.3\", facecolor=\"white\", alpha=0.7),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c274602d",
   "metadata": {},
   "source": [
    "Draw hpx with its errors and a marker."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "603d678d",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:17.551180Z",
     "iopub.status.busy": "2026-05-19T20:12:17.551042Z",
     "iopub.status.idle": "2026-05-19T20:12:18.229266Z",
     "shell.execute_reply": "2026-05-19T20:12:18.228876Z"
    }
   },
   "outputs": [],
   "source": [
    "ax3 = mpl_fig.add_subplot(gs[2, :])\n",
    "ax3.set_facecolor(\"#FFFDD0C8\")\n",
    "hep.histplot(hpx, ax=ax3, histtype=\"errorbar\", color=\"darkblue\")\n",
    "ax3.grid(True)\n",
    "ax3.text(\n",
    "    0.90,\n",
    "    0.95,\n",
    "    info_text,\n",
    "    transform=ax3.transAxes,\n",
    "    verticalalignment=\"top\",\n",
    "    fontsize=10,\n",
    "    bbox=dict(boxstyle=\"round,pad=0.3\", facecolor=\"white\", alpha=0.7),\n",
    ")\n",
    "\n",
    "mpl_fig.suptitle(\"Drawing options for one dimensional histograms\", fontsize=14, fontweight=\"bold\")\n",
    "plt.show()\n",
    "\n",
    "if example.IsOpen():\n",
    "    example.Close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2789abb",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d61cead8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:12:18.231344Z",
     "iopub.status.busy": "2026-05-19T20:12:18.231141Z",
     "iopub.status.idle": "2026-05-19T20:12:18.353777Z",
     "shell.execute_reply": "2026-05-19T20:12:18.352771Z"
    }
   },
   "outputs": [],
   "source": [
    "%jsroot on\n",
    "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
}
