{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# PyROOT Exercise 1: Histogram Drawing" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Write a python macro `ExerciseHist.py`.\n", "\n", "1. Open the `Zmumu.root` file and load the tree called `physics`.\n", "2. Create two histograms with 40 bins ranging from 0 to 0.2 GeV to plot the muon masses.\n", "3. Fill the histograms with leading and subleading muon mass from branches `lep1_m` and `lep2_m`.\n", "4. Calculate the mean values and RMSs.\n", "5. Calculate the integrals.\n", "\n", "Bonus questions:\n", "1. Are the integrals the same and why?\n", "2. Write the histogram to a file." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Solution" ] }, { "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": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Leading muon mass: mean=0.105476; rms=0.007434\n", "Subleading muon mass: mean=0.105479; rms=0.005120\n", "Leading muon mass integral: 2495706.0\n", "Subleading muon mass integral: 2496792.0\n" ] }, { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ROOT import gStyle, TCanvas, TFile, TH1D\n", "\n", "gStyle.SetOptStat(0)\n", "\n", "file = TFile(\"Zmumu.root\")\n", "tree = file.Get(\"physics\")\n", "\n", "canvas = TCanvas(\"canvas\", \"\", 800, 600)\n", "\n", "hist_1 = TH1D(\"hist_1\", \"Leading lepton mass\", 40, 0, 0.2)\n", "hist_2 = TH1D(\"hist_2\", \"Subleading lepton mass\", 40, 0, 0.2)\n", "tree.Draw(\"lep1_m >> hist_1\", \"\", \"PLC\")\n", "tree.Draw(\"lep2_m >> hist_2\", \"\", \"PLC same\")\n", "canvas.Draw()\n", "\n", "print(f\"Leading muon mass: mean={hist_1.GetMean():.6f}; rms={hist_1.GetRMS():.6f}\")\n", "print(f\"Subleading muon mass: mean={hist_2.GetMean():.6f}; rms={hist_2.GetRMS():.6f}\")\n", "\n", "print(f\"Leading muon mass integral: {hist_1.Integral()}\")\n", "print(f\"Subleading muon mass integral: {hist_2.Integral()}\")" ] } ], "metadata": { "kernelspec": { "display_name": "LCG 103", "language": "python", "name": "lcg_103" }, "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.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 2 }