# A simple introductional notebook to HEP analysis in python¶

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.

# Simple pyROOT notebook example

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.

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.

In [1]:
import ROOT

Welcome to JupyROOT 6.18/04


In order to activate the interactive visualisation of the histogram that is later created we can use the JSROOT magic

(if gives yo an error, don't worry, it maybe that your ROOT is simply too old)

In [2]:
%jsroot on


### The following analysis is searching for events where Z bosons decay to two leptons of same flavour and opposite charge (to be seen for example in the Feynman diagram).¶

Next we have to open the data that we want to analyze. As described above the data is stored in a *.root file.

In [3]:
f = ROOT.TFile.Open("https://atlas-opendata.web.cern.ch/atlas-opendata/samples/2020/1largeRjet1lep/MC/mc_361106.Zee.1largeRjet1lep.root") ## 13 TeV sample
##f = ROOT.TFile.Open("http://opendata.atlas.cern/release/samples/MC/mc_105987.WZ.root") ## 8 TeV sample
##f = ROOT.TFile.Open("/home/student/datasets/MC/mc_105987.WZ.root") ## local file exampl


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.

In [4]:
canvas = ROOT.TCanvas("Canvas","a first way to plot a variable",800,600)


The next step is to define a tree named tree to get the data out of the *.root file.

In [5]:
tree = f.Get("mini")

In [6]:
tree.GetEntries()

Out[6]:
53653

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.

In [7]:
hist = ROOT.TH1F("variable","Example plot: Number of leptons; Number of leptons ; Events ",5,-0.5,4.5)


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!.

In [8]:
for event in tree:
hist.Fill(tree.lep_n)

print("Done!")

Done!


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.

In [9]:
hist.SetFillColor(2)
hist.Draw()

In [10]:
canvas.Draw()


The next cell will rescale the histogram to one (1). This will allow to see proportions in the histogram itself. This is called normalisation

In [11]:
scale = hist.Integral()
hist.Scale(1/scale)
hist.SetFillColor(2)

In [12]:
hist.Draw()
canvas.Draw("hist")


Done!