{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# PyROOT Basics" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Let's import ROOT first and initialise interactive environment." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.28/00\n" ] } ], "source": [ "import ROOT\n", "%jsroot on" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For non-interactive use, the following lines should be at the very top of the python file:\n", "```\n", "import ROOT\n", "ROOT.gROOT.SetBatch(True)\n", "```" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Root Trees" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "A ROOT file with simulated Z->μμ events is prepared for you. Let's open it and get a `TTree` with the physics content out of it." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from ROOT import TFile\n", "file = TFile(\"Zmumu.root\")\n", "tree = file.Get(\"physics\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "To get familiarised with the contents of a file we can get number of entries and the content of one event." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Entries: 2500000\n", "======> EVENT:123\n", " lep1_pt = 36.6134\n", " lep1_eta = 1.72734\n", " lep1_phi = 2.45882\n", " lep1_E = 106.242\n", " lep1_m = 0.102701\n", " lep2_pt = 24.3234\n", " lep2_eta = 0.999867\n", " lep2_phi = -0.616201\n", " lep2_E = 37.5293\n", " lep2_m = 0.105446\n", " Z_pt = 12.4495\n", " Z_eta = 3.02829\n", " Z_phi = 2.32848\n", " Z_E = 143.771\n", " Z_m = 63.6461\n" ] } ], "source": [ "print(f\"Entries: {tree.GetEntries()}\")\n", "tree.Show(123)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can also scan through the tree and get a matrix of the quantities. We can pass the following arguments:\n", "- variables to list (e.g. `lep1_pt:lep2_pt:Z_pt`)\n", "- selection to apply (e.g. `Z_m > 200`)\n", "- options (let's skip this for now)\n", "- important for python: number of entries and first entry (otherwise the whole tree will get printed)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "************************************************\n", "* Row * lep1_pt * lep2_pt * Z_pt *\n", "************************************************\n", "* 1000 * 28.552259 * 24.640493 * 29.956617 *\n", "* 1001 * 46.691467 * 32.588851 * 20.080736 *\n", "* 1002 * 51.384120 * 23.717432 * 30.551261 *\n", "* 1003 * 51.648632 * 42.388435 * 93.866516 *\n", "* 1004 * 43.037010 * 40.113140 * 3.7478344 *\n", "* 1005 * 46.186592 * 43.645080 * 9.5110168 *\n", "* 1006 * 51.771820 * 35.814361 * 16.38661 *\n", "* 1007 * 30.069040 * 20.602933 * 9.9353952 *\n", "* 1008 * 42.196514 * 37.382263 * 5.1595315 *\n", "* 1009 * 51.599880 * 11.514470 * 46.104538 *\n", "************************************************\n" ] } ], "source": [ "tree.Scan(\"lep1_pt:lep2_pt:Z_pt\", \"\", \"\", 10, 1000)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "************************************************\n", "* Row * lep1_pt * lep2_pt * Z_pt *\n", "************************************************\n", "* 1213 * 139.37292 * 87.473121 * 66.915741 *\n", "* 1341 * 116.42005 * 39.788391 * 81.070587 *\n", "* 1844 * 85.993904 * 64.176918 * 28.775011 *\n", "************************************************\n", "==> 3 selected entries\n" ] } ], "source": [ "tree.Scan(\"lep1_pt:lep2_pt:Z_pt\", \"Z_m > 200\", \"\", 1000, 1000)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Useful `TTree` functions\n", "\n", "| Command | Action |\n", "| :------------ | :------- |\n", "| `tree.Print()` | prints the content of the tree |\n", "| `tree.Scan()` | scans the rows and columns |\n", "| `tree.Draw(\"x\")` | draw a branch of tree `x` |\n", "| `tree.Draw(\"x\", \"x > 0\")` | draw `x` when `x > 0` |\n", "| `tree.Draw(\"x\", \"x > 0 && y > 0\")` | draw `x` when both `x > 0` and `y > 0` |\n", "| `tree.Draw(\"y\", \"\", \"same\")` | uperimpose `y` on `x` |\n", "| `tree.Draw(\"y:x\")` | make `y` vs `x` 2D scatter plot |\n", "| `tree.Draw(\"z:y:x\")` | make `z:y:x` 3D plot |\n", "| `tree.Draw(\"sqrt(x*x + y*y)\")` | plot calculated quantity |\n", "| `tree.Draw(\"x>>h1\")` | dump a root branch to a histogram |\n", "| more... | [TTree documentation](https://root.cern.ch/doc/master/classTTree.html) |" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Drawing/Plotting Tree Content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's plot a few variables directly. Note that when using Jupyter I have to create a canvas to plot on first. This is not needed when running interactively." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from ROOT import TCanvas\n", "canvas = TCanvas(\"canvas\", \"\", 800, 450)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "One can draw directly from a `TTree`. The available options are:\n", "- the quantity to draw (e.g. `lep1_pt`)\n", "- selection to apply (e.g. `lep1_pt < 1000`)\n", "- drawing/plotting options" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "