Make sure that the necessary pacakges are installed:
import sys
!{sys.executable} -m pip install matplotlib --user
!{sys.executable} -m pip install mplhep --user
!{sys.executable} -m pip install uproot --user
!{sys.executable} -m pip install scipy --user
We will need matplotlib, uproot, numpy , awkward array, mplhep and scipy
import matplotlib
import uproot
import numpy as np
import awkward as ak
import mplhep as hep
from scipy.optimize import curve_fit
Uproot converts the root TTree file into its own TTree object
tree=uproot.open("Zmumu.root")["physics"]
print(tree)
<TTree '' (15 branches) at 0x000106645f60>
The uproot TTree object can be converted into python data sciecne object such as an awkward array, a numpy array or standard python dictionary objects
branches=tree.arrays()
print(branches)
print(type(branches))
[{lep1_pt: 59.9, lep1_eta: -2.18, lep1_phi: -1.42, lep1_E: 268, ...}, ...] <class 'awkward.highlevel.Array'>
branches_dictionary=branches.tolist()
print(type(branches_dictionary))
print(type(branches_dictionary[0]))
<class 'list'> <class 'dict'>
print("Total event #: ", len(branches_dictionary))
Total event #: 2500000
print("Event 1 , lep_pt: ", branches_dictionary[1]["lep1_pt"])
Event 1 , lep_pt: 33.927207946777344
print("Event # 123: ")
print("-------")
for branch, value in branches_dictionary[123].items():
print("%s : %s"%(branch, value))
Event # 123: ------- lep1_pt : 36.613399505615234 lep1_eta : 1.7273433208465576 lep1_phi : 2.458820104598999 lep1_E : 106.24195861816406 lep1_m : 0.10270115733146667 lep2_pt : 24.32341766357422 lep2_eta : 0.9998672008514404 lep2_phi : -0.6162007451057434 lep2_E : 37.52934646606445 lep2_m : 0.10544638335704803 Z_pt : 12.449456214904785 Z_eta : 3.028292417526245 Z_phi : 2.3284811973571777 Z_E : 143.77130126953125 Z_m : 63.64608383178711
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
lep1_pt=branches["lep1_pt"]
print(type(lep1_pt))
<class 'awkward.highlevel.Array'>
fill, bins, edges=plt.hist(lep1_pt, bins=100, range=(0,150))
fig, ax = plt.subplots()
fill, bins, edges=plt.hist(lep1_pt, bins=100, range=(0,150))
ax.set_ylabel("Entries")
ax.set_xlabel("lepton1 PT(GeV)")
ax.set_title("Example 1d histogram")
plt.legend()
plt.show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
fig, ax = plt.subplots()
fill, bins, edges=plt.hist(lep1_pt, bins=100, range=(0,150), label="lepton 1: PT", color="g", log=True)
ax.set_ylabel("Entries")
ax.set_xlabel("lepton1 PT(GeV)")
ax.set_title("Example 1d histogram")
plt.legend()
plt.show()
mask=branches['lep1_pt']<40
print("mask: ", mask)
lep1_pt_filtered=lep1_pt[mask]
mask: [False, True, True, True, False, False, ..., False, False, True, True, True]
fill, bins, edges=plt.hist(lep1_pt_filtered, bins=100, range=(0,150), label="lepton 1: PT, PT<40GeV", color="r")
fig, ax = plt.subplots()
fill, bins, edges=plt.hist(lep1_pt_filtered, bins=100, range=(0,150), label="lepton 1: PT, PT<40GeV", color="r")
ax.set_ylabel("Entries")
ax.set_xlabel("lepton1 PT(GeV)")
ax.set_title("Example 1d histogram: Filtered")
plt.legend()
plt.show()
fig, ax = plt.subplots()
#Add the histograms here
fill, bins, edges=plt.hist(lep1_pt, bins=100, range=(0,150), label="lepton 1: PT", color="g", alpha=0.5)
fill, bins, edges=plt.hist(lep1_pt_filtered, bins=100, range=(0,150), label="lepton 1: PT, PT<40GeV", color="r", alpha=0.5)
ax.set_ylabel("Entries")
ax.set_xlabel("lepton1 PT(GeV)")
ax.set_title("Example 1d histogram: Filtered")
plt.legend()
plt.show()
rms = np.sqrt(np.mean(np.square(lep1_pt)))
mean = np.mean(lep1_pt)
integral = len(lep1_pt)
max_bin=np.argmax(lep1_pt)
print("mean: ", mean)
print("rms:", rms)
print("integral: ", integral)
print("max bin entry #: ", max_bin)
mean: 48.2660032 rms: 58.77952819136949 integral: 2500000 max bin entry #: 2481654
lep2_pt=branches['lep2_pt']
plt.hist2d(np.array(lep1_pt), np.array(lep2_pt), bins=[100, 100], range=[[0,150], [0, 150]])
(array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 1., 0., 0.], [0., 0., 0., ..., 4., 1., 0.], [0., 0., 0., ..., 3., 1., 0.]]), array([ 0. , 1.5, 3. , 4.5, 6. , 7.5, 9. , 10.5, 12. , 13.5, 15. , 16.5, 18. , 19.5, 21. , 22.5, 24. , 25.5, 27. , 28.5, 30. , 31.5, 33. , 34.5, 36. , 37.5, 39. , 40.5, 42. , 43.5, 45. , 46.5, 48. , 49.5, 51. , 52.5, 54. , 55.5, 57. , 58.5, 60. , 61.5, 63. , 64.5, 66. , 67.5, 69. , 70.5, 72. , 73.5, 75. , 76.5, 78. , 79.5, 81. , 82.5, 84. , 85.5, 87. , 88.5, 90. , 91.5, 93. , 94.5, 96. , 97.5, 99. , 100.5, 102. , 103.5, 105. , 106.5, 108. , 109.5, 111. , 112.5, 114. , 115.5, 117. , 118.5, 120. , 121.5, 123. , 124.5, 126. , 127.5, 129. , 130.5, 132. , 133.5, 135. , 136.5, 138. , 139.5, 141. , 142.5, 144. , 145.5, 147. , 148.5, 150. ]), array([ 0. , 1.5, 3. , 4.5, 6. , 7.5, 9. , 10.5, 12. , 13.5, 15. , 16.5, 18. , 19.5, 21. , 22.5, 24. , 25.5, 27. , 28.5, 30. , 31.5, 33. , 34.5, 36. , 37.5, 39. , 40.5, 42. , 43.5, 45. , 46.5, 48. , 49.5, 51. , 52.5, 54. , 55.5, 57. , 58.5, 60. , 61.5, 63. , 64.5, 66. , 67.5, 69. , 70.5, 72. , 73.5, 75. , 76.5, 78. , 79.5, 81. , 82.5, 84. , 85.5, 87. , 88.5, 90. , 91.5, 93. , 94.5, 96. , 97.5, 99. , 100.5, 102. , 103.5, 105. , 106.5, 108. , 109.5, 111. , 112.5, 114. , 115.5, 117. , 118.5, 120. , 121.5, 123. , 124.5, 126. , 127.5, 129. , 130.5, 132. , 133.5, 135. , 136.5, 138. , 139.5, 141. , 142.5, 144. , 145.5, 147. , 148.5, 150. ]), <matplotlib.collections.QuadMesh at 0x3fa348dc0>)
fig, ax= plt.subplots()
plt.hist2d(np.array(lep1_pt), np.array(lep2_pt), bins=[100, 100], range=[[0,150], [0, 150]])
plt.colorbar()
ax.set_ylabel("lepton 2 PT (GeV)")
ax.set_xlabel("lepton 1 PT (GeV)")
ax.set_title("Example 2d histogram: lep1 vs lep2")
plt.legend()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
<matplotlib.legend.Legend at 0x4ca4bb490>
plt.clf()
fig, ax= plt.subplots()
ax.set_title("Graph")
plt.plot([3,5,7.2], [2.1,2.9,3.5])
[<matplotlib.lines.Line2D at 0x3fa650310>]
<Figure size 640x480 with 0 Axes>
from scipy.optimize import curve_fit
Z_mass=branches["Z_m"]
fig, ax = plt.subplots()
counts, edges, _= plt.hist(Z_mass, bins=200, range=(50,200), label="Z mass")
from scipy.optimize import curve_fit
#create a gaussian function for fitting
def gauss(x, *p):
A, mu, sigma = p
return A*numpy.exp(-(x-mu)**2/(2.*sigma**2))
# Fit data xdata/ydata
counts, edges, _= plt.hist(Z_mass, bins=200, range=(50,200), label="Z mass")
bins=np.linspace(0,100, 200)
xdata=(edges[:-1]+edges[1:])/2
ydata=counts
from scipy.optimize import curve_fit
#create a gaussian function for fitting
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
# Get the fitted curve
optimizedParameters, pcov = curve_fit(gauss, xdata, ydata, p0=[2000000., 100., 1.]);
y_fit=gauss(xdata, *optimizedParameters)
#Drawing the fit curve over the original histogram
counts, edges, _= plt.hist(Z_mass, bins=200, range=(50,200), label="Z mass")
plt.plot(xdata, y_fit, label="fit");
counts, edges, _= plt.hist(Z_mass, bins=200, range=(50,200), label="Z mass")
plt.plot(xdata, y_fit, label="fit");
ax.set_xlabel("pt(GeV)", fontsize=15)
ax.set_ylabel("Events ", fontsize=15)
ax.legend()
plt.legend()
plt.show()
print('Fitted amp = ', optimizedParameters[0])
print('Fitted mean = ', optimizedParameters[1])
print('Fitted standard deviation = ', optimizedParameters[2])
Fitted amp = 224833.95003746863 Fitted mean = 90.75921756252049 Fitted standard deviation = 2.777861533290021
import mplhep as hep
fig, ax = plt.subplots()
fill, edges, _=plt.hist(lep1_pt, bins=100, range=(0,150), label="lepton 1: PT", color="g")
ax.set_ylabel("Entries")
ax.set_xlabel("lepton1 PT(GeV)")
ax.set_title("Example 1d histogram")
plt.legend()
plt.show()
import mplhep as hep
#hep.style.use(hep.style.CMS)
hep.style.use(hep.style.ATLAS)
fill, edges, bins=plt.hist(lep1_pt, bins=100, range=(0,150), label="lepton 1: PT", color="g", log=True)
plt.clf()
fig, ax = plt.subplots(figsize=(10, 5))
hep.histplot(
fill,
bins=edges,
histtype="step", #"fill"
color="w",
alpha=1,
edgecolor="r",
label="lep1 pt",
ax=ax,
)
ax.set_xlabel("pt(GeV)", fontsize=15)
ax.set_ylabel("Events ", fontsize=15)
ax.set_xlim(0, 150)
ax.legend()
hep.atlas.label()
#hep.atlas.label("Preliminary", data=True, lumi=50, year=2017)
fig.show()
/var/folders/50/rhj1rsj9317d9pfht5v6w4s80000gn/T/ipykernel_44684/3333250534.py:30: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backend, so cannot show the figure. fig.show()
<Figure size 800x600 with 0 Axes>
import numpy as np
fill, x_edges, y_edges, _=plt.hist2d(np.array(lep1_pt), np.array(lep2_pt), bins=[100, 100], range=[[0,150], [0, 150]])
plt.clf()
hep.style.use(hep.style.ATLAS)
hep.hist2dplot(fill, x_edges, y_edges)
hep.cms.label("Preliminary", data=True, lumi=50, year=2017)
(exptext: Custom Text(0.0, 1, 'CMS'), expsuffix: Custom Text(0.0, 1.005, 'Preliminary'))
Z_mass=branches["Z_m"]
counts, edges, _= plt.hist(Z_mass, bins=200, range=(50,200), label="Z mass")
xdata=(edges[:-1]+edges[1:])/2
ydata=counts
#create a gaussian function for fitting
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
# Get the fitted curve
optimizedParameters, pcov = curve_fit(gauss, xdata, ydata, p0=[2000000., 100., 1.]);
y_fit=gauss(xdata, *optimizedParameters)
plt.plot(xdata, y_fit, label="fit");
ax.set_xlabel("pt(GeV)", fontsize=15)
ax.set_ylabel("Events ", fontsize=15)
ax.legend()
plt.legend()
plt.show()
Z_mass=branches["Z_m"]
counts, edges, _= plt.hist(Z_mass, bins=200, range=(50,200), label="Z mass")
xdata=(edges[:-1]+edges[1:])/2
ydata=counts
#create a gaussian function for fitting
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
# Get the fitted curve
optimizedParameters, pcov = curve_fit(gauss, xdata, ydata, p0=[2000000., 100., 1.]);
y_fit=gauss(xdata, *optimizedParameters)
hep.style.use(hep.style.ATLAS)
plt.plot(xdata, y_fit, label="fit");
ax.legend()
hep.atlas.label("Work in Progress", data=True, lumi=50, year=2017)
plt.legend()
plt.show()
Last update: 06 March 2023