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.08/04


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

In [2]:
%jsroot on


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("http://opendata.atlas.cern/release/samples/MC/mc_147770.Zee.root")
## f = ROOT.TFile.Open("mc_105986.ZZ.root")


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")


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 [6]:
hist = ROOT.TH1F("variable","Example plot: Number of leptons",4,0,4)


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 [7]:
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 [8]:
hist.Draw()

In [9]:
canvas.Draw()

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

In [13]:
hist.Draw()
canvas.Draw()