{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A simple introductional notebook to HEP analysis in python\n", "

In this notebook you can find an easy set of commands that show the basic computing techniques commonly used in high energy physics (HEP) analyzes. It also shows how to create a histogram, fill it and draw it. Moreover it is an introduction to [ROOT](https://root.cern.ch/) too. At the end you get a plot with the number of leptons.

\n", "\n", "\n", "

Simple pyROOT notebook example

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The library used is ROOT - a scientific software framework that provides all the functionalities needed to deal with big data processing, statistical analysis, visualisation and storage.\n", "\n", "First of all ROOT is imported to read the files in the _.root_ data format. A _.root_ file consists of a tree having branches and leaves. At this point you could also import further programs that contain other formulas that you maybe use more often. But here we don't import other programs to keep it simple." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.18/04\n" ] } ], "source": [ "import ROOT" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to activate the interactive visualisation of the histogram that is later created we can use the JSROOT magic \n", "\n", "*(if gives yo an error, don't worry, it maybe that your ROOT is simply too old)*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%jsroot on" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The following analysis is searching for events where [Z bosons](https://en.wikipedia.org/wiki/W_and_Z_bosons) decay to two leptons of same flavour and opposite charge (to be seen for example in the [Feynman diagram](https://en.wikipedia.org/wiki/Feynman_diagram))." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we have to open the data that we want to analyze. As described above the data is stored in a _*.root_ file." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "f = ROOT.TFile.Open(\"https://atlas-opendata.web.cern.ch/atlas-opendata/samples/2020/1largeRjet1lep/MC/mc_361106.Zee.1largeRjet1lep.root\") ## 13 TeV sample\n", "##f = ROOT.TFile.Open(\"http://opendata.atlas.cern/release/samples/MC/mc_105987.WZ.root\") ## 8 TeV sample\n", "##f = ROOT.TFile.Open(\"/home/student/datasets/MC/mc_105987.WZ.root\") ## local file exampl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After the data is opened we create a canvas on which we can draw a histogram. If we do not have a canvas we cannot see our histogram at the end. Its name is _Canvas_ and its header is _a first way to plot a variable_. The two following arguments define the width and the height of the canvas." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "canvas = ROOT.TCanvas(\"Canvas\",\"a first way to plot a variable\",800,600)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next step is to define a tree named _tree_ to get the data out of the _*.root_ file." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "tree = f.Get(\"mini\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "53653" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tree.GetEntries()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we define a histogram that will later be placed on this canvas. Its name is _variable_ and the header of the histogram is _Example plot: Number of leptons_. The three following arguments indicate that this histogram contains 4 so called bins which have a range from 0 to 4." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "hist = ROOT.TH1F(\"variable\",\"Example plot: Number of leptons; Number of leptons ; Events \",5,-0.5,4.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following lines are a loop that goes over the data that is stored in the tree and fills the histogram _h_ that we already defined. In this first notebook we don't do any cuts to keep it simple. Accordingly the loop fills the histogram for each event stored in the tree. After the program has looped over all the data it prints the word __Done!__." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done!\n" ] } ], "source": [ "for event in tree:\n", " hist.Fill(tree.lep_n)\n", " \n", "print(\"Done!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After filling the histogram we want to see the results of the analysis. First we draw the histogram on the canvas and then the canvas on which the histogram lies." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "hist.SetFillColor(2)\n", "hist.Draw()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "canvas.Draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next cell will rescale the histogram to one (1).\n", "This will allow to see proportions in the histogram itself.\n", "**This is called normalisation**" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "scale = hist.Integral()\n", "hist.Scale(1/scale)\n", "hist.SetFillColor(2)\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "hist.Draw()\n", "canvas.Draw(\"hist\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Done!**" ] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 2 }