-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_depos_packets_toroot.py
192 lines (155 loc) · 7.63 KB
/
export_depos_packets_toroot.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os, argparse
from array import array
import ROOT, h5py
from matplotlib import pyplot as plt
import numpy as np
from tqdm import tqdm
from larpixsoft.detector import set_detector_properties
from larpixsoft.geometry import get_geom_map
from larpixsoft.funcs import get_events_no_cuts
def main(INPUT_FILES, OUTPUT_NAME, PLOT):
detector = set_detector_properties('data/detector/ndlar-module.yaml', \
'data/pixel_layout/multi_tile_layout-3.0.40.yaml', \
pedestal=74)
geometry = get_geom_map('data/pixel_layout/multi_tile_layout-3.0.40.yaml')
ROOT.gROOT.ProcessLine('#include<vector>')
f_ROOT = ROOT.TFile.Open(OUTPUT_NAME if OUTPUT_NAME != '' else 'out.root', "RECREATE")
t = ROOT.TTree("ND_depos_packets", "nddepospackets")
depos = ROOT.vector("std::vector<double>")()
t.Branch("nd_depos", depos)
packets = ROOT.vector("std::vector<double>")()
t.Branch("nd_packets", packets)
vertex_info = ROOT.vector("double")(4)
t.Branch("vertex", vertex_info)
eventID = array('i', [0])
t.Branch("eventID", eventID, 'eventID/I')
n_passed, num = 0, 0
n_adc_failed, n_assns_failed = 0, 0
for input_file in INPUT_FILES:
f = h5py.File(input_file, 'r')
vertices = { vertex['eventID'] : \
(vertex['z_vert']/10.0, vertex['y_vert']/10.0, vertex['x_vert']/10.0, 0.0) \
for vertex in f['vertices'] }
data_packets, tracks = get_events_no_cuts(f['packets'], f['mc_packets_assn'], f['tracks'], geometry, detector)
for i, (event_data_packets, event_tracks) in enumerate(tqdm(zip(data_packets, tracks))):
depos.clear()
packets.clear()
for i in range(vertex_info.size()):
vertex_info[i] = -9999.0
eventID[0] = -1
ids = { track.eventid for track in event_tracks }
if len(ids) > 1:
print("ids = {}".format(ids))
raise Exception("Packets should be for a single event")
id = ids.pop()
eventID[0] = id
vertex = vertices[id]
# Write vertex info
vertex_info[0] = vertex[0] # x
vertex_info[1] = vertex[1] # y
vertex_info[2] = vertex[2] # z
vertex_info[3] = vertex[3] # t
# Write depos
total_e = 0
for track in event_tracks:
depo = ROOT.vector("double")(13)
depo[0] = track.trackid
depo[1] = track.pdg
depo[2] = track.x_start
depo[3] = track.x_end
depo[4] = track.y_start
depo[5] = track.y_end
depo[6] = track.z_start
depo[7] = track.z_end
depo[8] = track.t_start
depo[9] = track.t_end
depo[10] = track.electrons
depo[11] = track.dE
depo[12] = 1.0 if track.active_volume else -1.0
depos.push_back(depo)
# Wrtie packets
for p in event_data_packets:
packet = ROOT.vector("double")(7)
packet[0] = p.x + p.anode.tpc_x
packet[1] = p.y + p.anode.tpc_y
packet[2] = p.z_global()
packet[3] = p.t()
packet[4] = p.ADC
packet[5] = p.z() # nd drift length
packet[6] = p.x # nd module x for knowledge of when approacing edge of module
packets.push_back(packet)
if PLOT:
vertex_x, vertex_y, vertex_z = [vertex[0]], [vertex[1]], [vertex[2]]
depo_x, depo_y, depo_z = [], [], []
for track in event_tracks:
depo_x.append(track.x_start)
depo_y.append(track.y_start)
depo_z.append(track.z_start)
packet_x, packet_y, packet_z = [], [], []
for p in event_data_packets:
packet_x.append(p.x + p.anode.tpc_x)
packet_y.append(p.y + p.anode.tpc_y)
packet_z.append(p.z_global())
print("Packets:", len(packet_z), "Depos:",len(depo_z))
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1, projection='3d')
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
ax1.scatter(vertex_z, vertex_x, vertex_y, label='vertex', marker='x', s=40)
ax1.scatter(packet_z, packet_x, packet_y, label='packet', marker='o', color='g')
xlims = (413.72, 916.68)
ylims = (-148.613, 155.387)
zlims = (-356.7, 356.7)
ax1.set_xlabel('Z')
ax1.set_ylabel('X')
ax1.set_zlabel('Y')
ax1.set_xlim(zlims[0] - 50, zlims[1] + 50)
ax1.set_ylim(xlims[0] - 50, xlims[1] + 50)
ax1.set_zlim(ylims[0] - 50, ylims[1] + 50)
ax1.legend(loc="lower left")
ax2.scatter(vertex_z, vertex_x, vertex_y, label='vertex', marker='x', s=40)
ax2.scatter(depo_z, depo_x, depo_y, label='depo', marker='o', color='r')
ax2.set_xlabel('Z')
ax2.set_ylabel('X')
ax2.set_zlabel('Y')
ax2.set_xlim(zlims[0] - 50, zlims[1] + 50)
ax2.set_ylim(xlims[0] - 50, xlims[1] + 50)
ax2.set_zlim(ylims[0] - 50, ylims[1] + 50)
ax2.legend(loc="lower left")
lines = [
((xlims[0], xlims[1]), (zlims[0],) * 2, (ylims[0],) * 2), # left low /
((xlims[0],) * 2, (zlims[0], zlims[1]), (ylims[0],) * 2), # low front -
((xlims[0],) * 2, (zlims[0],) * 2, (ylims[0], ylims[1])), # left front |
((xlims[0], xlims[1]), (zlims[0],) * 2, (ylims[1],) * 2), # left up /
((xlims[0],) * 2, (zlims[0], zlims[1]), (ylims[1],) * 2), # up front -
((xlims[1],) * 2, (zlims[0], zlims[1]), (ylims[1],) * 2), # up back -
((xlims[1],) * 2, (zlims[0],) * 2, (ylims[1], ylims[0])), # left back |
((xlims[0],) * 2, (zlims[1],) * 2, (ylims[0], ylims[1])), # right front |
((xlims[0], xlims[1]), (zlims[1],) * 2, (ylims[0],) * 2), # right low /
((xlims[1],) * 2, (zlims[1],) * 2, (ylims[0], ylims[1])), # right back |
((xlims[1],) * 2, (zlims[1], zlims[0]), (ylims[0],) * 2), # low back -
((xlims[0], xlims[1]), (zlims[1],) * 2, (ylims[1],) * 2) # right up /
]
for line in lines:
# Need to swap x and z from how we did it earlier because other coords are in
# edep-sim convention and these are in ND convention
ax1.plot(
line[1], line[0], zs=line[2], color='black', label='_', linestyle='dashed'
)
ax2.plot(
line[1], line[0], zs=line[2], color='black', label='_', linestyle='dashed'
)
fig.tight_layout()
plt.show()
t.Fill()
f_ROOT.Write()
f_ROOT.Close()
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("input_files", nargs='+')
parser.add_argument("-o", type=str, default='', help="output root file name")
parser.add_argument("--plot", action='store_true')
args = parser.parse_args()
return (args.input_files, args.o, args.plot)
if __name__ == '__main__':
arguments = parse_arguments()
main(*arguments)