ROOT Basics

Import Required Modules and Turn on Javascript

In [2]:
from ROOT import TFile, TCanvas, TH1F

%jsroot on
Welcome to JupyROOT 6.07/07

Open a Data File

In [3]:
file = TFile("opendata/data/Dimuons.root","READ")

Declare a Histogram

Histogram is a Graphical Way to Visualize Data

Divide Data into Equal Intervals (Bins) and Count How Many Values in Each Bin

Example Histogram of Distribution of Muon Charges

In [21]:
histogram = TH1F("Muon1_Charge","Muon1_Charge", 50, -2, 2)

declaration format: TH1F(name, title, number of bins, min value, max value)

ROOT Exercise 1:

Declare another histogram for x-component of muon momentum called Muon1_Px

In [36]:
histogram2 = TH1F("Muon1_Px","Muon1_Px", 100, -60, 60)

Loop Over Events in Data File

Fill the histogram with muon charge values (+1 or -1)

In [33]:
for dimuon in file.Dimuons:
    
    histogram.Fill(dimuon.Muon1_Charge)       

Draw a Histogram on a Canvas

In [29]:
Canvas = TCanvas()

histogram.Draw()

Canvas.Draw()

ROOT Exercise #2:

Plot another histogram (for example Muon1_Px)

Note: Same canvas can be used after clearing it with Canvas.Clear()

In [38]:
for dimuon in file.Dimuons:
    histogram2.Fill(dimuon.Muon1_Px)
In [39]:
Canvas.Clear()
histogram2.Draw()
Canvas.Draw()
In [ ]: