ROOT Histograms in Python

ROOT is a data analysis framework developed by CERN that is well-suited for the analysis of certain large scientific data sets such as particle collision events and astronomical data.

One useful ROOT feature are the histograms. A simple program that histograms 100,000 Gaussian random numbers is shown here:


from __future__ import division, print_function
from ROOT import gRandom, TCanvas, TH1F

c1 = TCanvas('c1', 'Example', 200, 10, 700, 500)
hpx = TH1F('hpx', 'px', 100, -4, 4)

for i in xrange(100000):
    px = gRandom.Gaus()
    hpx.Fill(px)

hpx.Draw()
c1.Update()

# Save the plot as an image
c1.Print("myhistogram.eps")

It’s output is shown here:

A second program is given on page~\pageref{root-histograms} that shows how the ROOT TCanvas can be split into regions to show multiple plots on the same canvas. It’s output is shown here:

We can also split ROOT’s TCanvas into regions to show multiple plots on a single canvas. A simple program that does that is shown here:


from __future__ import division, print_function
from ROOT import gRandom, TCanvas, TH1F

# Create a canvas
c1 = TCanvas('c1', 'My Basic Histograms', 200, 10, 700, 500)

# Divide the canvas into two plot areas
c1.Divide(1,2)

# Create a histograms
hist1 = TH1F('hist1', 'First', 100, -4, 4)
hist2 = TH1F('hist2', 'Second', 100, -4, 4)

for i in xrange(100000):
    px = gRandom.Gaus()
    hist1.Fill(px)

for i in xrange(100000):
    px = gRandom.Gaus()
    hist2.Fill(px)

# Go to plot area 1
c1.cd(1)

# Plot the first histogram
hist1.Draw()

# Go to plot area 2
c1.cd(2)

# Plot the second histogram
hist2.Draw()

c1.Update()

# Save the plot as an image
c1.Print("myhistogram.eps")

It’s output is shown here:

ROOT’s TLorentzVector Class in Python

ROOT is a data analysis framework developed by CERN that is well-suited for the analysis of certain large scientific data sets such as particle collision events and astronomical data.

One useful part of ROOT is their TLorentzVector class—objects that store and can work with relativistic four-vectors such as the space-time 4-vector or the energy-momentum 4-vector.

Below is a basic Python (2.7) program exploring the TLorentzVector class.


#
# This file explores the use of ROOT's TLorentzVector class, which is useful
# for working with energy-momentum 4-vectors.
#

from __future__ import division, print_function
from ROOT import TLorentzVector

# Create the energy-momentum vector for a photon with energy 5 GeV
photon = TLorentzVector(0, 0, 5.0, 5.0)

# Create the 4-vector for a proton at rest
proton = TLorentzVector(0, 0, 0, 0.938)

# Create vectors for two pi+ pions and one pi- pion
pip1, pip2, pim = TLorentzVector(), TLorentzVector(), TLorentzVector()

# Set the vector components for the pions
pip1.SetPxPyPzE(-0.226178, -0.198456, 1.946048, 1.974144)
pip2.SetPxPyPzE(0.554803, -0.301158, 1.301439, 1.453219)
pim.SetPxPyPzE(-0.07765, 0.072333, 1.372624, 1.38382)

# Print the magnitudes of each 4-vector
print("The magnitude of the photon 4-vector is", photon.Mag())
print("The magnitude of the proton 4-vector is", proton.Mag())
print("The magnitude of the pip1 4-vector is", pip1.Mag())
print("The magnitude of the pip2 4-vector is", pip2.Mag())
print("The magnitude of the pim 4-vector is", pim.Mag())

# Create a new 4-vector by adding/subtracting the others
diff = photon + proton - ( pip1 + pip2 + pim )

diffMass = diff.Mag()
print("The invariant mass of the missing particle is", diffMass)