The aim of this notebook is to show how to open a 2D ROOT histogram from a Geant4 simulation saved in a ROOT file.
Let's start by importing the required modules
import uproot
import numpy as np
import matplotlib.pyplot as plt
We can see that there are two Ntuples (two matrices) outData
and crossPos
. Just for informations, there are two different ntuples because they have a different number of rows.
Furthermore there are two Histograms: crossingPos
and crossingWeigh
, being respectively the raw histogram of particles crossing at a given distance and the weighted one based on their total energy.
with uproot.open("../CERN2022-build/out_data/tbeamdata0000.root") as f:
for k in f:
print(k)
try:
for kk in f[k]:
print(kk)
except:
pass
outData;1 <TBranch 'NEvent' at 0x7f0f52e52fd0> <TBranch 'Tracker_NHit_X_0' at 0x7f0f52e53340> <TBranch 'Tracker_NHit_Y_0' at 0x7f0f52e53f10> <TBranch 'Tracker_NHit_X_1' at 0x7f0f52e54f40> <TBranch 'Tracker_NHit_Y_1' at 0x7f0f52e54dc0> <TBranch 'Tracker_X_0' at 0x7f0f52e90f10> <TBranch 'Tracker_Y_0' at 0x7f0f52e90070> <TBranch 'Tracker_X_1' at 0x7f0f52e97b20> <TBranch 'Tracker_Y_1' at 0x7f0f52e99310> <TBranch 'GammaCal_EDep_CC' at 0x7f0f52e99be0> <TBranch 'GammaCal_EDep_TL' at 0x7f0f52ead4f0> <TBranch 'GammaCal_EDep_TR' at 0x7f0f52eaddc0> <TBranch 'GammaCal_EDep_BL' at 0x7f0f52e9e940> <TBranch 'GammaCal_EDep_BR' at 0x7f0f52e9e070> <TBranch 'GammaCal_EDep_CL' at 0x7f0f52e628b0> <TBranch 'GammaCal_EDep_CR' at 0x7f0f52e67e50> <TBranch 'CrystalA_EDep' at 0x7f0f52e67580> <TBranch 'CrystaB_EDep' at 0x7f0f52e3e3a0> <TBranch 'CrystalC_EDep' at 0x7f0f52e3ec70> crossPos;1 <TBranch 'EvID' at 0x7f0f52e43be0> <TBranch 'Xcrossing' at 0x7f0f52e49580> <TBranch 'Ycrossing' at 0x7f0f52e49e50> <TBranch 'PDGEncoding' at 0x7f0f52e5f760> <TBranch 'ParticleID' at 0x7f0f52e5ffa0> <TBranch 'totEnergy' at 0x7f0f52e73940> crossingPos;1 crossingWeigh;1
This is my colormap for logz histograms, for coloring the zeros as ones.
import matplotlib as mpl
import copy
my_cmap = copy.copy(mpl.cm.jet) # copy the default cmap
my_cmap.set_bad(my_cmap(0))
Let's have a look in more detail to one histogram, and extract relevant informations
with uproot.open("../CERN2022-build/out_data/tbeamdata0000.root")["crossingWeigh"] as f:
a = f.all_members
print(a.keys()) # Print what's inside
# Directly access the bins and height matrix
b = f.to_numpy()
for i in range(len(b)):
print(b[i].shape)
# Extract bins and height matrix
hh = b[0]
binsx = b[1]
binsy = b[2]
bincx = binsx[:-1] + (binsx[1] - binsx[0])/2
bincy = binsy[:-1] + (binsy[1] - binsy[0])/2
dict_keys(['@fUniqueID', '@fBits', 'fName', 'fTitle', 'fLineColor', 'fLineStyle', 'fLineWidth', 'fFillColor', 'fFillStyle', 'fMarkerColor', 'fMarkerStyle', 'fMarkerSize', 'fNcells', 'fXaxis', 'fYaxis', 'fZaxis', 'fBarOffset', 'fBarWidth', 'fEntries', 'fTsumw', 'fTsumw2', 'fTsumwx', 'fTsumwx2', 'fMaximum', 'fMinimum', 'fNormFactor', 'fContour', 'fSumw2', 'fOption', 'fFunctions', 'fScalefactor', 'fTsumwy', 'fTsumwy2', 'fTsumwxy', 'fN']) (300, 300) (301,) (301,)
A simple example of how to plot the istogram
# %matplotlib widget
%matplotlib inline
fig, ax = plt.subplots()
prof = ax.imshow(hh.T, cmap = my_cmap, origin = "lower",
extent = (binsx.min(), binsx.max(), binsy.min(), binsy.max()))
ax.set_xlabel("X [cm]", fontsize = 14)
ax.set_ylabel("Y [cm]", fontsize = 14)
fig.colorbar(prof, ax = ax)
plt.show()