This repository was archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.py
More file actions
169 lines (132 loc) · 6.42 KB
/
Copy pathdisplay.py
File metadata and controls
169 lines (132 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import uproot as up
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm, colors
import argparse
from os.path import join
CellWidthX = 2.5
CellWidthY = 2.5
LayerThick = 4
nCellX = 21
nCellY = 21
nLayer = 11
def read_file(fname: str, tree: str, event_index: int, staggered: bool):
with up.open(fname) as f:
tree = f[tree]
EventID = tree['EventNumber'].array(library='np')
try:
event = np.where(EventID == event_index)[0][0]
except:
print('Event ID does not exist in the ROOT file!')
raise
print(f'Entry ID: {event}')
print(f'Event ID: {event_index}')
xtemp = tree['Hit_X'].array(library='np')[event]
ytemp = tree['Hit_Y'].array(library='np')[event]
x = np.zeros_like(xtemp)
y = np.zeros_like(ytemp)
z = np.round(tree['Hit_Z'].array(library='np')[event] / LayerThick).astype(int)
energy = tree['Hit_Energy'].array(library='np')[event]
assert len(x) == len(y) == len(z) == len(energy)
if staggered:
for i in np.arange(len(z)):
if z[i] % 2 == 0:
x[i] = np.round(xtemp[i] / CellWidthX - 0.25).astype(int)
y[i] = np.round(ytemp[i] / CellWidthY - 0.25).astype(int)
else:
x[i] = np.round(xtemp[i] / CellWidthX + 0.25).astype(int)
y[i] = np.round(ytemp[i] / CellWidthY + 0.25).astype(int)
else:
x[i] = np.round(xtemp[i] / CellWidthX).astype(int)
y[i] = np.round(ytemp[i] / CellWidthY).astype(int)
znew, ynew, xnew, enew = (np.array(a) for a in zip(*sorted(zip(z, y, x, energy), reverse=True)))
return xnew, ynew, znew, enew
def plot(fname: str, tree: str, run_index: int, event_index: int, title: str, staggered: bool):
x, y, z, energy = read_file(fname, tree, event_index, staggered)
energy_norm = energy / np.max(energy)
nhits = len(x)
WidthX = (nCellX + 0.5 * staggered) * CellWidthX
WidthY = (nCellY + 0.5 * staggered) * CellWidthY
ratioX = WidthX / (LayerThick * (nLayer + 1))
ratioY = 1
ratioZ = WidthY / (LayerThick * (nLayer + 1))
fig = plt.figure()
spec = fig.add_gridspec(2, 2)
ax_xz = fig.add_subplot(spec[0, 1], projection='3d')
plt.gca().set_box_aspect((ratioX, ratioY, ratioZ))
ax_xy = fig.add_subplot(spec[1, 1], projection='3d')
plt.gca().set_box_aspect((ratioX, ratioY, ratioZ))
ax = fig.add_subplot(spec[:, 0], projection='3d')
plt.gca().set_box_aspect((ratioX, ratioY, ratioZ))
cmap = cm.OrRd
for i in np.arange(nhits):
if z[i] % 2 == 0:
xnew = np.arange(x[i] - 1, x[i] + 1) + 0.75 * staggered
ynew = np.arange(y[i] - 1, y[i] + 1) + 0.75 * staggered
else:
xnew = np.arange(x[i] - 1, x[i] + 1) + 0.25 * staggered
ynew = np.arange(y[i] - 1, y[i] + 1) + 0.25 * staggered
xnew, ynew = np.meshgrid(xnew, ynew)
znew = z[i] * np.ones(xnew.shape)
enew = energy_norm[i] * np.ones(xnew.shape)
for axis in (ax, ax_xz, ax_xy):
axis.plot_surface(xnew, znew, ynew, cmap=cmap, facecolors=cmap(enew), edgecolor='k', alpha=0.8, lw=0.05, rstride=1, cstride=1, antialiased=False)
ax.computed_zorder = False
for axis in (ax, ax_xz, ax_xy):
axis.set_xlim(-0.5 * nCellX, 0.5 * nCellY)
axis.set_ylim(0, nLayer)
axis.set_zlim(-0.5 * nCellY, 0.5 * nCellY)
axis.set_aspect(aspect='equalxz')
axis.grid(False)
axis.invert_xaxis()
if axis == ax:
axis.set_xticks(np.linspace(-10, 10, 5), CellWidthX * np.linspace(-10, 10, 5))
axis.set_yticks(np.linspace(0, nLayer + 1, 5))
axis.set_zticks(np.linspace(-10, 10, 5), CellWidthY * np.linspace(-10, 10, 5))
axis.set_xlabel("X [cm]", size='x-large')
axis.set_ylabel("Z [layer]", size='x-large')
axis.set_zlabel("Y [cm]", size='x-large')
m = plt.cm.ScalarMappable(cmap=cmap)
m.set_array(energy)
plt.colorbar(m, pad=0.2, ax=plt.gca(), orientation='horizontal').set_label(label="Hit Energy [MeV]", size='large')
else:
axis.set_xticks([])
axis.set_yticks([])
axis.set_zticks([])
ax_xz.set_xlabel("X", size='large', labelpad=-10)
ax_xz.set_ylabel("Z", size='large', labelpad=-10)
ax_xy.set_xlabel("X", size='large', labelpad=-10)
ax_xy.set_zlabel("Y", size='large', labelpad=-10)
fig.suptitle(title, size='xx-large')
ax.text2D(0.05, 0.95, f'Run ID: {run_index}\nEvent ID: {event_index}', transform=ax.transAxes)
ax.text2D(0.8, 0.95, r'$E_\mathrm{total} =$' + f'{np.sum(energy):.3f} MeV\n' + r'$E_\mathrm{max} =$' + f'{np.max(energy):.3f} MeV', transform=ax.transAxes)
ax.view_init(elev=20, azim=-35, roll=0)
ax_xz.view_init(elev=90, azim=0, roll=0)
ax_xy.view_init(elev=0, azim=-90, roll=0)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", type=str, default='', required=True, help="Input ROOT file")
parser.add_argument("-t", "--tree", type=str, default='dp', help="Input tree name (default: dp)")
parser.add_argument("-g", "--staggered", type=int, default=1, choices=[0, 1], help="Staggered structure")
parser.add_argument("-i", "--title", type=str, default='', help="Title of display figure")
parser.add_argument("-r", "--run", type=int, default=0, help="Run ID of the event to be displayed")
parser.add_argument("-e", "--event", type=int, default=0, help="Event ID")
parser.add_argument("-d", "--dir", type=str, default=None, help="Directory to save the plot")
parser.add_argument("-o", "--output", type=str, default=None, help="Output file name")
parser.add_argument("-s", "--show", type=int, default=1, choices=[0, 1], help="Instantly display or not")
args = parser.parse_args()
filename = args.file
tree = args.tree
staggered = args.staggered
title = args.title
run_index = args.run
event_index = args.event
save_dir = args.dir
output = args.output
show = args.show
plot(filename, tree, run_index, event_index, title, staggered)
if save_dir and output:
plt.savefig(join(save_dir, output), bbox_inches='tight')
print("Figure ", join(save_dir, output), " successfully created!")
if show:
plt.show()