{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import uproot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Branches"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Open `ROOT` file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = uproot.open('file_with_tree.root')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List all objects on the root level of the `ROOT` file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access `TTree` with name `'Isr'`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['Isr']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List all branches"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(*f['Isr'].keys(), sep=', ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access a single branch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['Isr']['intNumberGoodPhotons']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve content of branch as array using `.array()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Awkward Array (Default)\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['Isr']['intNumberGoodPhotons'].array()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['Isr']['intNumberGoodPhotons'].array(library='np')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pandas\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['Isr']['intNumberGoodPhotons'].array(library='pd')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Histograms"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Open `ROOT` file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = uproot.open('file_with_histograms.root')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List all objects"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(*f.keys(), sep=', ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access `TH1D` histogram"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve bin contents"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].values()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get information about the axes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].axes[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Axis edges"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].axes[0].edges()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Axis widths"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].axes[0].widths()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert histogram to different libraries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].to_numpy()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boost Histogram\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].to_boost()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hist\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f['hMHadRec'].to_hist()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stack"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load histograms from file and scale according to simulated luminosity"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h_ee = uproot.open('file_with_histograms_1.root')['hMHadRec'].to_hist() * 2.4800064575645755\n",
"h_mumu = uproot.open('file_with_histograms_2.root')['hMHadRec'].to_hist() * 0.1320246"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create stack from list/iterator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from hist.stack import Stack\n",
"Stack.from_iter([h_ee, h_mumu])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create stack from dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s = Stack.from_dict({\n",
" 'ee': h_ee,\n",
" 'mumu': h_mumu\n",
"})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot stack unstacked"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot stack stacked"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.plot(stack=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot stack with custom colors and labels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from matplotlib import gridspec\n",
"\n",
"fig = plt.figure(figsize=(9, 6))\n",
"gs = gridspec.GridSpec(1, 1, figure=fig)\n",
"ax = fig.add_subplot(gs[0])\n",
"\n",
"s.plot(ax=ax, stack=True, histtype='fill', color=['red', 'green'], label=[r'$e^+e^-$', r'$\\mu^+\\mu^-$'])\n",
"\n",
"ax.legend()\n",
"\n",
"ax.set_xlabel(r'$\\sqrt{s^\\prime}$ / GeV')\n",
"ax.set_ylabel(r'Events / 5 MeV')\n",
"\n",
"ax.set_xlim(0, 2)\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Histogram arithmetics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Addition"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h_ee + h_mumu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subtraction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h_ee - h_mumu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiplication"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h_ee * h_mumu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Division"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h_mumu / h_ee"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(h_mumu / h_ee).plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Slicing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2 = uproot.open('file_with_histograms_3.root')['hTracksEoPVsMHad'].to_hist()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2[0:80,40:150]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Projection"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Project to the $y$ axis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2[::sum,:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Project to the $x$ axis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2[:,::sum]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Project only a slice to the $x$ axis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h2[:,10:50:sum]"
]
}
],
"metadata": {
"file_extension": ".py",
"kernelspec": {
"display_name": "Python 3.10.5 ('.venv': venv)",
"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.10.5"
},
"metadata": {
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
}
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"orig_nbformat": 2,
"pygments_lexer": "ipython3",
"version": 3,
"vscode": {
"interpreter": {
"hash": "cc78ec09620010df01e1f27913a91518ec8979d90c34155fa8818c96ba93e8ea"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}