-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_modulemap.py
385 lines (308 loc) · 17 KB
/
compute_modulemap.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import os
import sys
import math
import multiprocessing
import pickle
import numpy as np
from tqdm import tqdm
from DetectorGeometry import DetectorGeometry
from Module import Module
from Centroid import Centroid
import LSTMath as sdlmath
# See Constants.py for definitions.
from Constants import PTTHRESH
def get_straight_line_connections_parallel(ref_detid, return_dict={}):
return_dict[ref_detid] = get_straight_line_connections(ref_detid)
return True
def get_curved_line_connections_parallel(ref_detid, return_dict={}):
return_dict[ref_detid] = get_curved_line_connections(ref_detid)
return True
def get_straight_line_connections(ref_detid):
# reference module centroid
centroid = centroidDB.getCentroid(ref_detid)
# reference module phi position
refphi = math.atan2(centroid[1], centroid[0])
# reference module Module instance
ref_module = Module(ref_detid)
# reference module layer
ref_layer = ref_module.layer()
# reference subdet
ref_subdet = ref_module.subdet()
tar_detids_to_be_considered = []
if ref_subdet == 5:
tar_detids_to_be_considered += det_geom.getBarrelLayerDetIds(ref_layer + 1)
else:
tar_detids_to_be_considered += det_geom.getEndcapLayerDetIds(ref_layer + 1)
list_of_detids_etaphi_layer_tar = []
# for tar_detid in tqdm(tar_detids_to_be_considered, desc="looping over target detids"):
for tar_detid in tar_detids_to_be_considered:
if sdlmath.module_overlaps_in_eta_phi(
det_geom.getData()[ref_detid],
det_geom.getData()[tar_detid],
refphi,
0
):
list_of_detids_etaphi_layer_tar.append(tar_detid)
elif sdlmath.module_overlaps_in_eta_phi(
det_geom.getData()[ref_detid],
det_geom.getData()[tar_detid],
refphi,
10
):
list_of_detids_etaphi_layer_tar.append(tar_detid)
elif sdlmath.module_overlaps_in_eta_phi(
det_geom.getData()[ref_detid],
det_geom.getData()[tar_detid],
refphi,
-10
):
list_of_detids_etaphi_layer_tar.append(tar_detid)
# Consider barrel to endcap connections if the intersection area is > 0
if ref_subdet == 5:
list_of_barrel_endcap_connected_tar_detids = []
for zshift in [0, 10, -10]:
ref_polygon = sdlmath.get_etaphi_polygon(det_geom.getData()[ref_detid], refphi, zshift)
# Check whether there is still significant non-zero area
for tar_detid in list_of_detids_etaphi_layer_tar:
tar_polygon = sdlmath.get_etaphi_polygon(det_geom.getData()[tar_detid], refphi, zshift)
ref_polygon = ref_polygon.difference(tar_polygon)
# If area is "non-zero" then consider endcap
tar_detids_to_be_considered = []
if ref_polygon.area > 0.0001:
tar_detids_to_be_considered += det_geom.getEndcapLayerDetIds(1)
# for tar_detid in tqdm(tar_detids_to_be_considered, desc="looping over target detids"):
for tar_detid in tar_detids_to_be_considered:
# If the centroids are far away then don't consider (this was important to exclude incorrect matching when target module is pi away)
centroid_target = centroidDB.getCentroid(tar_detid)
tarphi = math.atan2(centroid_target[1], centroid_target[0])
if abs(sdlmath.Phi_mpi_pi(tarphi - refphi)) > math.pi / 2.:
continue
tar_polygon = sdlmath.get_etaphi_polygon(det_geom.getData()[tar_detid], refphi, zshift)
if ref_polygon.intersects(tar_polygon):
list_of_barrel_endcap_connected_tar_detids.append(tar_detid)
list_of_barrel_endcap_connected_tar_detids = list(set(list_of_barrel_endcap_connected_tar_detids))
list_of_detids_etaphi_layer_tar += list_of_barrel_endcap_connected_tar_detids
return list_of_detids_etaphi_layer_tar
def bounds_after_curved(ref_detid, doR=True):
# Obtaining positive particle helices from centroid and bounds of reference module
bounds = det_geom.getData()[ref_detid]
centroid = centroidDB.getCentroid(ref_detid)
charge = 1
next_layer_bound_points = []
theta = math.atan2(math.sqrt(centroid[0]**2 + centroid[1]**2), centroid[2])
refphi = math.atan2(centroid[1], centroid[0])
ref_layer = Module(ref_detid).layer()
ref_subdet = Module(ref_detid).subdet()
for bound in bounds:
helix_p10 = sdlmath.construct_helix_from_points(PTTHRESH, 0, 0, 10, bound[1], bound[2], bound[0], -charge)
helix_m10 = sdlmath.construct_helix_from_points(PTTHRESH, 0, 0, -10, bound[1], bound[2], bound[0], -charge)
helix_p10_pos = sdlmath.construct_helix_from_points(PTTHRESH, 0, 0, 10, bound[1], bound[2], bound[0], charge)
helix_m10_pos = sdlmath.construct_helix_from_points(PTTHRESH, 0, 0, -10, bound[1], bound[2], bound[0], charge)
# helix_p10 = sdlmath.construct_helix_from_points(1, 0, 0, 0, bound[1], bound[2], bound[0], -charge)
# helix_m10 = sdlmath.construct_helix_from_points(1, 0, 0, 0, bound[1], bound[2], bound[0], -charge)
# helix_p10_pos = sdlmath.construct_helix_from_points(1, 0, 0, 0, bound[1], bound[2], bound[0], charge)
# helix_m10_pos = sdlmath.construct_helix_from_points(1, 0, 0, 0, bound[1], bound[2], bound[0], charge)
bound_theta = math.atan2(math.sqrt(bound[1]**2 + bound[2]**2), bound[0])
bound_phi = math.atan2(bound[2], bound[1])
if ref_subdet == 5:
tar_layer_radius = det_geom.getBarrelLayerAverageRadius(ref_layer + 1)
tar_layer_z = det_geom.getEndcapLayerAverageAbsZ(1)
if ref_subdet == 5:
if doR:
tar_layer_radius = det_geom.getBarrelLayerAverageRadius(ref_layer + 1)
if bound_theta > theta:
if sdlmath.Phi_mpi_pi(bound_phi - refphi) > 0:
next_point = sdlmath.get_helix_point_from_radius(helix_p10, tar_layer_radius)
else:
next_point = sdlmath.get_helix_point_from_radius(helix_p10_pos, tar_layer_radius)
else:
if sdlmath.Phi_mpi_pi(bound_phi - refphi) > 0:
next_point = sdlmath.get_helix_point_from_radius(helix_m10, tar_layer_radius)
else:
next_point = sdlmath.get_helix_point_from_radius(helix_m10_pos, tar_layer_radius)
else:
tar_layer_z = det_geom.getEndcapLayerAverageAbsZ(1)
if bound_theta > theta:
if sdlmath.Phi_mpi_pi(bound_phi - refphi) > 0:
next_point = sdlmath.get_helix_point_from_z(helix_p10, math.copysign(tar_layer_z, helix_p10.lam()))
else:
next_point = sdlmath.get_helix_point_from_z(helix_p10_pos, math.copysign(tar_layer_z, helix_p10.lam()))
else:
if sdlmath.Phi_mpi_pi(bound_phi - refphi) > 0:
next_point = sdlmath.get_helix_point_from_z(helix_m10, math.copysign(tar_layer_z, helix_p10.lam()))
else:
next_point = sdlmath.get_helix_point_from_z(helix_m10_pos, math.copysign(tar_layer_z, helix_p10.lam()))
else:
tar_layer_z = det_geom.getEndcapLayerAverageAbsZ(ref_layer + 1)
if bound_theta > theta:
if sdlmath.Phi_mpi_pi(bound_phi - refphi) > 0:
next_point = sdlmath.get_helix_point_from_z(helix_p10, math.copysign(tar_layer_z, helix_p10.lam()))
else:
next_point = sdlmath.get_helix_point_from_z(helix_p10_pos, math.copysign(tar_layer_z, helix_p10.lam()))
else:
if sdlmath.Phi_mpi_pi(bound_phi - refphi) > 0:
next_point = sdlmath.get_helix_point_from_z(helix_m10, math.copysign(tar_layer_z, helix_m10.lam()))
else:
next_point = sdlmath.get_helix_point_from_z(helix_m10_pos, math.copysign(tar_layer_z, helix_m10.lam()))
next_layer_bound_points.append([next_point[2], next_point[0], next_point[1]])
return next_layer_bound_points
def get_curved_line_connections(ref_detid):
# reference module centroid
centroid = centroidDB.getCentroid(ref_detid)
# reference module phi position
refphi = math.atan2(centroid[1], centroid[0])
# reference module Module instance
ref_module = Module(ref_detid)
# reference module layer
ref_layer = ref_module.layer()
# reference subdet
ref_subdet = ref_module.subdet()
# Target IDs to be considered in the next layer
tar_detids_to_be_considered = []
if ref_subdet == 5:
tar_detids_to_be_considered += det_geom.getBarrelLayerDetIds(ref_layer + 1)
else:
tar_detids_to_be_considered += det_geom.getEndcapLayerDetIds(ref_layer + 1)
next_layer_bound_points = bounds_after_curved(ref_detid)
list_of_detids_etaphi_layer_tar = []
# for tar_detid in tqdm(tar_detids_to_be_considered, desc="looping over target detids"):
for tar_detid in tar_detids_to_be_considered:
if sdlmath.module_overlaps_in_eta_phi(
next_layer_bound_points,
det_geom.getData()[tar_detid],
refphi,
0
):
list_of_detids_etaphi_layer_tar.append(tar_detid)
# elif sdlmath.module_overlaps_in_eta_phi(
# next_layer_bound_points,
# det_geom.getData()[tar_detid],
# refphi,
# 10
# ):
# list_of_detids_etaphi_layer_tar.append(tar_detid)
# elif sdlmath.module_overlaps_in_eta_phi(
# next_layer_bound_points,
# det_geom.getData()[tar_detid],
# refphi,
# -10
# ):
# list_of_detids_etaphi_layer_tar.append(tar_detid)
# Consider barrel to endcap connections if the intersection area is > 0
if ref_subdet == 5:
list_of_barrel_endcap_connected_tar_detids = []
# for zshift in [0, 10, -10]:
for zshift in [0]:
ref_polygon = sdlmath.get_etaphi_polygon(next_layer_bound_points, refphi, zshift)
# Check whether there is still significant non-zero area
for tar_detid in list_of_detids_etaphi_layer_tar:
tar_polygon = sdlmath.get_etaphi_polygon(det_geom.getData()[tar_detid], refphi, zshift)
ref_polygon = ref_polygon.difference(tar_polygon)
# If area is "non-zero" then consider endcap
tar_detids_to_be_considered = []
if ref_polygon.area > 0.0001:
tar_detids_to_be_considered += det_geom.getEndcapLayerDetIds(1)
# for tar_detid in tqdm(tar_detids_to_be_considered, desc="looping over target detids"):
for tar_detid in tar_detids_to_be_considered:
# If the centroids are far away then don't consider (this was important to exclude incorrect matching when target module is pi away)
centroid_target = centroidDB.getCentroid(tar_detid)
tarphi = math.atan2(centroid_target[1], centroid_target[0])
if abs(sdlmath.Phi_mpi_pi(tarphi - refphi)) > math.pi / 2.:
continue
tar_polygon = sdlmath.get_etaphi_polygon(det_geom.getData()[tar_detid], refphi, zshift)
if ref_polygon.intersects(tar_polygon):
list_of_barrel_endcap_connected_tar_detids.append(tar_detid)
list_of_barrel_endcap_connected_tar_detids = list(set(list_of_barrel_endcap_connected_tar_detids))
list_of_detids_etaphi_layer_tar += list_of_barrel_endcap_connected_tar_detids
return list_of_detids_etaphi_layer_tar
def write_straight_line_connections(output="output/module_connection_tracing_straight.txt"):
return write_connections(docurved=False,output=output)
def write_curved_line_connections(output="output/module_connection_tracing_curved.txt"):
return write_connections(docurved=True,output=output)
def write_connections(docurved=False, output="output/module_connection_tracing.txt"):
list_of_detids_etaphi_layer_ref = det_geom.getDetIds(
lambda x:
((Module(x[0]).subdet() == 5 and Module(x[0]).isLower() == 1 and Module(x[0]).layer() != 6) or
(Module(x[0]).subdet() == 4 and Module(x[0]).isLower() == 1 and Module(x[0]).layer() != 5 and
not (Module(x[0]).ring() == 15 and Module(x[0]).layer() == 1) and
not (Module(x[0]).ring() == 15 and Module(x[0]).layer() == 2) and
not (Module(x[0]).ring() == 12 and Module(x[0]).layer() == 3) and
not (Module(x[0]).ring() == 12 and Module(x[0]).layer() == 4)
))
# and (Module(x[0]).layer() == 1 and Module(x[0]).rod() == 1 and Module(x[0]).isLower() == 1)
)
ref_detid = sorted(list(list_of_detids_etaphi_layer_ref))[0]
njobs = 32
manager = multiprocessing.Manager()
return_dict = manager.dict()
pool = multiprocessing.Pool(processes=njobs)
module_map = {}
for ref_detid in tqdm(list_of_detids_etaphi_layer_ref, desc="looping over ref detids"):
if docurved:
job = pool.apply_async(get_curved_line_connections_parallel, args=(ref_detid, return_dict))
# module_map[ref_detid] = get_curved_line_connections(ref_detid)
else:
job = pool.apply_async(get_straight_line_connections_parallel, args=(ref_detid, return_dict))
# module_map[ref_detid] = get_straight_line_connections(ref_detid)
pool.close()
pool.join()
module_map = dict(return_dict)
f = open(output, "w")
print("Writing module connections...")
for ref_detid in sorted(tqdm(module_map.keys())):
tar_detids = [str(x) for x in module_map[ref_detid]]
f.write("{} {} {}\n".format(ref_detid, len(tar_detids), " ".join(tar_detids)))
return module_map
def merge_maps(straight, curved, output_path="output/module_connection_tracing_merged.txt"):
# Initialize a dictionary to hold the merged connections
merged_connections = {}
# Merge the straight connections
for key, values in straight.items():
if key not in merged_connections:
merged_connections[key] = set()
merged_connections[key].update(values)
# Merge the curved connections
for key, values in curved.items():
if key not in merged_connections:
merged_connections[key] = set()
merged_connections[key].update(values)
# Write the merged connections to the output file
with open(output_path, "w") as file:
for reference in sorted(merged_connections.keys()):
uniquelist = list(merged_connections[reference])
nconn = len(uniquelist)
targets = [str(x) for x in uniquelist]
file.write("{} {} {}\n".format(reference, nconn, " ".join(targets)))
print(f"Merged map written to {output_path}")
if __name__ == "__main__":
# Default file paths
default_centroid_file = "output/sensor_centroids.txt"
default_geom_file = "output/sensor_corners.txt"
default_average_radius_file = "data/average_r_OT800_IT615.txt"
default_average_z_file = "data/average_z_OT800_IT615.txt"
# Check for help flag
if '-h' in sys.argv or '--help' in sys.argv:
print("\nUsage: python compute_modulemap.py [centroid_file] [geom_file] [average_radius_file] [average_z_file]")
print("\nOptions:")
print(f" centroid_file Path to the centroid file. Default is {default_centroid_file}")
print(f" geom_file Path to the geometry file. Default is {default_geom_file}")
print(f" average_radius_file Path to the average radius file. Default is {default_average_radius_file}")
print(f" average_z_file Path to the average z file. Default is {default_average_z_file}")
sys.exit()
# Determine file paths based on arguments provided
centroid_file = sys.argv[1] if len(sys.argv) > 1 else default_centroid_file
geom_file = sys.argv[2] if len(sys.argv) > 2 else default_geom_file
average_radius_file = sys.argv[3] if len(sys.argv) > 3 else default_average_radius_file
average_z_file = sys.argv[4] if len(sys.argv) > 4 else default_average_z_file
# Setting up detector geometry (centroids and boundaries)
centroidDB = Centroid(centroid_file)
dirpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
det_geom = DetectorGeometry(geom_file, average_radius_file, average_z_file)
det_geom.buildByLayer()
# Make output folder if it doesn't exist
os.makedirs(os.path.dirname("output/"), exist_ok=True)
# Generate straight and curved module maps
curved_map = write_curved_line_connections()
straight_map = write_straight_line_connections()
# Merge straight and curved module maps into one file
merge_maps(straight_map, curved_map)