234 ) -> None:
235 """Draw overlay plot of multiple channel mappings on a single figure.
236
237 Parameters
238 ----------
239 number_of_channels : int, optional
240 Total number of channels to display (split evenly between U and V planes).
241 Default is 20.
242 sGeo : ROOT.TGeoManager
243 The geometry manager for the detector setup.
244 output_file : str, optional
245 Filename for the saved PDF plot. Default: 'scifi_mapping_all_channels.pdf'.
246 labeling : bool, optional
247 If True, annotate each SiPM rectangle with channel information.
248 Default is True.
249 figsize : tuple of float, optional
250 Figure size in inches. Default is (16, 16).
251 dpi : int, optional
252 Resolution of the figure in dots per inch. Default is 300.
253 cmap_name : str, optional
254 Matplotlib colormap name for differentiating channels. Default is 'tab20'.
255 alpha_fibre : float, optional
256 Transparency for fibre ellipses. Default is 0.4.
257
258 Side Effects
259 ------------
260 Saves an overlay PDF plot with the specified filename.
261
262 """
263
264 fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
265
266
267 fibreVol = sGeo.FindVolumeFast("FiberVol")
268 R = fibreVol.GetShape().GetDX()
269 DX, DZ = 0.025, 0.135 / 2
270
271
272
273
274
275
276 center_channel_U = [chan for chan, x_pos in self.sipm_pos_U.items() if abs(x_pos) <= DX - 0.001][0]
277 center_channel_V = [chan for chan, x_pos in self.sipm_pos_V.items() if abs(x_pos) <= DX - 0.001][0]
278
279 def get_surrounding_keys(d: dict, target_key, N: int):
280 """Return neighbors of `target_key` in value-sorted order.
281
282 Given a dict `d`, find `target_key` in the ordering of keys sorted by
283 their values, and return up to N//2 keys that come before it and N//2 keys
284 that come after it (excluding the target_key itself).
285
286 Parameters
287 ----------
288 d : dict
289 Mapping from keys to comparable values.
290 target_key : hashable
291 The key around which to collect neighbors.
292 N : int
293 Total number of neighbors to return (N//2 before, N//2 after).
294
295 Returns
296 -------
297 list
298 List of up to N keys: first the keys before, then the keys after.
299
300 """
301
302 sorted_keys = sorted(d.keys(), key=lambda k: d[k])
303
304 try:
305 idx = sorted_keys.index(target_key)
306 except ValueError:
307 raise KeyError(f"Target key {target_key!r} not found in dictionary") from None
308
309 half = N // 2
310
311 start = max(0, idx - half)
312 end_before = idx
313 start_after = idx + 1
314 end_after = idx + 1 + half
315
316 before = sorted_keys[start:end_before]
317 after = sorted_keys[start_after:end_after]
318
319 return before + [target_key] + after
320
321 channelsU = get_surrounding_keys(self.sipm_pos_U, center_channel_U, number_of_channels)
322 channelsV = get_surrounding_keys(self.sipm_pos_V, center_channel_V, number_of_channels)
323
324 channels = channelsU + channelsV
325 n_chan = len(channels)
326
327 AF = ROOT.TVector3()
328 BF = ROOT.TVector3()
329
330
331 for chan in channels:
332 isU = chan in channelsU
333
334 locChan = chan % 1_000_000
335
336
337 xs, ys = [], []
338 for fibreID in (self.fibre_to_simp_map_U if chan in channelsU else self.fibre_to_simp_map_V)[locChan]:
339 globfiberID = (
340 fibreID + 100000000 + 1000000 + 0 * 100000
341 if chan in channelsU
342 else fibreID + 100000000 + 1000000 + 1 * 100000
343 )
344 self.scifi.GetPosition(globfiberID, AF, BF)
345 loc = self.scifi.GetLocalPos(globfiberID, BF)
346 xs.append(loc[0])
347 ys.append(loc[2])
348
349
350 for x, y in zip(xs, ys):
351 ell = patches.Ellipse(
352 (x, y),
353 2 * R,
354 2 * R,
355 fill=False,
356 edgecolor="black",
357 linewidth=1,
358 alpha=alpha_fibre,
359 )
360 ax.add_patch(ell)
361
362
363 self.scifi.GetSiPMPosition(chan, BF, AF)
364
365
366
367 globSiPMID = locChan + 100000000 + 1000000 + (0 if isU else 1) * 100000
368 loc_siPM = self.scifi.GetLocalPos(globSiPMID, BF)
369 rx, ry = loc_siPM[0], loc_siPM[2]
370 rect = patches.Rectangle(
371 (rx - DX, ry - DZ),
372 2 * DX,
373 2 * DZ,
374 linewidth=1,
375 edgecolor="black",
376 facecolor="blue" if isU else "red",
377 alpha=alpha_fibre * 0.8,
378 hatch="//" if isU else "\\\\",
379 )
380 ax.add_patch(rect)
381
382 if labeling:
383
384
385 p0 = ax.transData.transform((rx - DX, ry))
386 p1 = ax.transData.transform((rx + DX, ry))
387 disp_width = abs(p1[0] - p0[0])
388
389 pts = disp_width * 72.0 / fig.dpi
390 fontsize = max(2, min(12, pts * 0.8))
391
392
393 text = f"SiPM: {(chan // 1000) % 10} \n ch: {chan % 1000}"
394 ax.text(
395 rx - DX / 2,
396 ry + DZ + R * 0.1,
397 text,
398 ha="left",
399 va="bottom",
400 fontsize=fontsize,
401 color="black",
402 clip_on=True,
403 )
404
405
406 ax.relim()
407 ax.autoscale_view()
408 ax.set_aspect("equal")
409 ax.tick_params(axis="both", which="major", labelsize=18)
410 ax.tick_params(axis="both", which="minor", labelsize=18)
411 ax.set_xlabel("X [cm]", fontsize=20)
412 ax.set_ylabel("Z [cm]", fontsize=20)
413 ax.set_title("SiPM-Channel Mappings Overlaid", fontsize=24)
414 ax.grid(True)
415
416 plt.tight_layout()
417 plt.savefig(output_file)
418 plt.close(fig)
419 print(f"Saved overlay plot of {n_chan} channels to {output_file}")
420