PyROOT Basics

Let's import ROOT first and initialise interactive environment.

For non-interactive use, the following lines should be at the very top of the python file:

import ROOT
ROOT.gROOT.SetBatch(True)

Root Trees

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.

To get familiarised with the contents of a file we can get number of entries and the content of one event.

We can also scan through the tree and get a matrix of the quantities. We can pass the following arguments:

Useful TTree functions

Command Action
tree.Print() prints the content of the tree
tree.Scan() scans the rows and columns
tree.Draw("x") draw a branch of tree x
tree.Draw("x", "x > 0") draw x when x > 0
tree.Draw("x", "x > 0 && y > 0") draw x when both x > 0 and y > 0
tree.Draw("y", "", "same") uperimpose y on x
tree.Draw("y:x") make y vs x 2D scatter plot
tree.Draw("z:y:x") make z:y:x 3D plot
tree.Draw("sqrt(x*x + y*y)") plot calculated quantity
tree.Draw("x>>h1") dump a root branch to a histogram
more... TTree documentation

Drawing/Plotting Tree Content

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.

One can draw directly from a TTree. The available options are:

As we have seen ROOT makes assumptions how to make a histogram, mainly binning and axis ranges.

Let's create a histogram manually. Usually one uses TH1D which has double precision. Even if our quantities are not very precise, the precision matters if we have a lot of events (which is usually the case in HEP).

The options for TH1D are:

Often we want to do more complex data manipulations. In such case we loop the tree and fill the histogram(s) manually.

Notes:

If we want to save to a file, we can make a new file and write the histogram to it.

Useful TH1 functions

Command Action
hist.GetMean() mean of a histogram
hist.GetRMS() RMS of a histogram
hist.GetBinContent(i) content of a bin with index i
hist.GetBinError(i) uncertainty of a bin with index i
hist.GetMaximum() maximum value of the histogram
hist.GetMaximumBin() the bin with the maximum value
hist.Integral() integral of the histogram
hist.GetXaxis() x-axis
hist.GetYaxis() y-axis
more... TH1 documentation

Exercise 1: Histogram Drawing

Write a python macro ExerciseHist.py.

  1. Open the Zmumu.root file and load the tree called physics.
  2. Create two histograms with 40 bins ranging from 0 to 0.2 GeV to plot the muon masses.
  3. Fill the histograms with leading and subleading muon mass from branches lep1_m and lep2_m.
  4. Calculate the mean values and RMSs.
  5. Calculate the integrals.

Bonus questions:

  1. Are the integrals the same and why?
  2. Write the histogram to a file.

Graphs

Graphs are used to display value pairs, errors can be defined to be either symmetric or antisymmetric.

Useful TGraph functions

Command Action
TGraph(n) 1D graph without errors with n points
TGraphErrors(n) 1D graph with symmetrical errors with n points
TGraphAsymmErrors(n) 1D graph with asymmetrical errors with n points
graph.SetPoint(i, x, y) set a point with index i with coordinates x and y
graph.SetPointError(i, ex, ey) set symmetrical errors of a point with index i and values ex and ey
graph.SetPointError(i, exl, exh, eyl, exh) set asymmetrical errors of a point with index i and values exl, exh, eyl and eyh
graph.Fit("function") fit a function with name function
more... click class constructor above

Functions

Classes for 1 to 3 dimensional functions: TF1, TF2 and TF3.

One can use:

Functions can be used to fit a TGraph or a TH1.

Useful TF1 functions

Command Action
graph.SetParameter(i, value) set parameter with index i and value value
graph.GetParameter(i) get parameter with index i
graph.GetParError(i) get parameter error with index i
graph.SetLineColor(kRed) set line color (e.g. kRed)
more... TF1 documentation

Exercise 2: Graphs and Fits

Write a python macro ExerciseGraph.py.

  1. Create a graph with symmetric errors and 5 points.
  2. Set the following points: (1.0, 2.1), (2.0, 2.9), (3.0, 4.05), (4.0, 5.2), (5.0, 5.95)
  3. Set the errors on x to 0.0 and the errors on y to 0.1 (all at once).
  4. Draw the graph including the axes and error bars.
  5. Create a one dimensional function f(x) = ax + b and fit it to the graph.

Bonus questions:

  1. Programatically obtain the two parameters a and b and their estimated uncertainties.

Other Canvas Elements

One canvas can be reused to plot many items besides histograms, graphs and functions:

Simple objects listed above do not require the same argument.

Exercise 3: Canvas and Legends

Write a python macro ExerciseCanvas.py.

  1. Create two histograms with 50 bins ranging from -3 to 3 with two different names.
  2. Fill first histogram with Gaussian distribution with 10000 entries.
  3. Fill second histogram with a second order polynomial and 5000 entries (hint: hist2.FillRandom("pol2", 500)).
  4. Set the line color of the first histogram to kRed and second to kBlue.
  5. Draw both histograms on a canvas.
  6. Clone both histograms and normalise them (scale with inverse of the integral).
  7. Draw both histograms on a different canvas.
  8. Draw a legend on both canvases at position (0.16, 0.63, 0.45, 0.91); bonus: do it after you created both canvases.
  9. Save both canvases in a PDF; bonus: save them in the same file.

Ratio plots

Often we want to plot ratio between two distributions (e.g. data and Monte Carlo or how well fit matches the distribution).

But sometimes one ratio pad is not enough or we want to do it manually.

Unfortunately "ROOT plotting math" is quite complex. We will use a simple example to demonstrate the idea. Label sizes are relative to the height (and width) of the pad so we need to rescale the values to the size of the pad. As width is constant in our example we only take the fractions of the height and divide by it.