146 """Draw a single channel mapping showing fibre positions and the SiPM sensor.
150 sGeo: ROOT.TGeoManager
151 The geometry manager for the detector setup.
153 Global channel identifier encoding plane type, SiPM unit, and channel index.
157 Saves a PDF file named 'scifi_mapping_channel_1_{channel}.pdf' with the plot.
162 plane_type = int(channel / 1e5) % 10
163 locChannel = channel % 1000000
164 fibreVol = sGeo.FindVolumeFast(
"FiberVol")
165 R = fibreVol.GetShape().GetDX()
174 if not fibresSiPM[locChannel]:
177 for fibre
in fibresSiPM[locChannel]:
179 fibre + 100000000 + 1000000 + 0 * 100000
181 else fibre + 100000000 + 1000000 + 1 * 100000
183 self.
scifi.GetPosition(globfiberID, AF, BF)
184 loc = self.
scifi.GetLocalPos(globfiberID, BF)
185 print(f
"Position for fibre {globfiberID} A: {BF.X()}, {BF.Y()}, {BF.Z()}")
186 fibre_positions.append((loc[0], loc[2]))
195 D = ymax - ymin + 3 * R
196 x0 = (xmax + xmin) / 2.0
197 _fig, ax = plt.subplots(figsize=(8, 8))
198 ax.set_xlim(x0 - D / 2, x0 + D / 2)
199 ax.set_ylim(ymin - 1.5 * R, ymax + 1.5 * R)
200 for i, (x, y)
in enumerate(fibre_positions):
201 ellipse = patches.Ellipse((x, y), width=2 * R, height=2 * R, color=
"orange", alpha=0.6)
202 ax.add_patch(ellipse)
203 self.
scifi.GetSiPMPosition(locChannel, AF, BF)
204 loc = self.
scifi.GetLocalPos(globfiberID, BF)
205 print(f
"SiPM position for channel {channel}: {loc[0]}, {loc[1]}, {loc[2]}")
206 rect = patches.Rectangle(
207 (loc[0] - DX, loc[2] - DZ),
217 ax.set_xlabel(
"X [cm]")
218 ax.set_ylabel(
"Z [cm]")
219 ax.set_title(f
"SiPM Mapping for Channel {channel}")
221 ax.set_aspect(
"equal")
222 plt.savefig(f
"scifi_mapping_channel_1_{channel}.pdf")
227 number_of_channels=20,
228 output_file="scifi_mapping_many_channels.pdf",
235 """Draw overlay plot of multiple channel mappings on a single figure.
239 number_of_channels : int, optional
240 Total number of channels to display (split evenly between U and V planes).
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.
249 figsize : tuple of float, optional
250 Figure size in inches. Default is (16, 16).
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.
260 Saves an overlay PDF plot with the specified filename.
264 fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
267 fibreVol = sGeo.FindVolumeFast(
"FiberVol")
268 R = fibreVol.GetShape().GetDX()
269 DX, DZ = 0.025, 0.135 / 2
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]
279 def get_surrounding_keys(d: dict, target_key, N: int):
280 """Return neighbors of `target_key` in value-sorted order.
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).
289 Mapping from keys to comparable values.
290 target_key : hashable
291 The key around which to collect neighbors.
293 Total number of neighbors to return (N//2 before, N//2 after).
298 List of up to N keys: first the keys before, then the keys after.
302 sorted_keys = sorted(d.keys(), key=
lambda k: d[k])
305 idx = sorted_keys.index(target_key)
307 raise KeyError(f
"Target key {target_key!r} not found in dictionary")
from None
311 start = max(0, idx - half)
313 start_after = idx + 1
314 end_after = idx + 1 + half
316 before = sorted_keys[start:end_before]
317 after = sorted_keys[start_after:end_after]
319 return before + [target_key] + after
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)
324 channels = channelsU + channelsV
325 n_chan = len(channels)
331 for chan
in channels:
332 isU = chan
in channelsU
334 locChan = chan % 1_000_000
340 fibreID + 100000000 + 1000000 + 0 * 100000
342 else fibreID + 100000000 + 1000000 + 1 * 100000
344 self.
scifi.GetPosition(globfiberID, AF, BF)
345 loc = self.
scifi.GetLocalPos(globfiberID, BF)
350 for x, y
in zip(xs, ys):
351 ell = patches.Ellipse(
363 self.
scifi.GetSiPMPosition(chan, BF, AF)
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(
376 facecolor=
"blue" if isU
else "red",
377 alpha=alpha_fibre * 0.8,
378 hatch=
"//" if isU
else "\\\\",
385 p0 = ax.transData.transform((rx - DX, ry))
386 p1 = ax.transData.transform((rx + DX, ry))
387 disp_width = abs(p1[0] - p0[0])
389 pts = disp_width * 72.0 / fig.dpi
390 fontsize = max(2, min(12, pts * 0.8))
393 text = f
"SiPM: {(chan // 1000) % 10} \n ch: {chan % 1000}"
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)
417 plt.savefig(output_file)
419 print(f
"Saved overlay plot of {n_chan} channels to {output_file}")
423 number_of_channels: int = 20,
424 real_event: bool =
False,
425 x_coords: list[float] |
None =
None,
426 output_file: str =
"scifi_channel_ribbons_XY.pdf",
427 figsize: tuple[int, int] = (16, 16),
430 """Plot SciFi channel ribbons and SiPM boxes in X–Y view.
432 Simplified X–Y view: one ribbon per channel (no per-fiber loops).
433 Ribbons span Y from -25 to +25 (length=50 cm), thickness equal
434 to channel height = 2*DZ, tilted +5° for U-plane channels, -5° for
435 V-plane. SiPM boxes sit on top at y=25→25+2*DZ.
439 number_of_channels : int
440 Total ribbons (split around center) per plane.
442 If True, highlight ribbons corresponding to given x_coords.
443 x_coords : list of float
444 If real_event is True, list of two x-coordinates [x_U, x_V] to highlight.
446 Path to save the PDF.
447 figsize : tuple of float
448 Figure size in inches.
450 Figure resolution in dots per inch.
453 Saves a PDF file with the specified filename containing the plot.
470 alpha_sipm, alpha_fibre = 0.75, 0.5
472 alpha_sipm, alpha_fibre = 0.1, 0.1
473 if x_coords
is None or len(x_coords) < 2:
474 raise ValueError(
"x_coords must provide at least two coordinates when real_event is True")
476 matches_U = [c
for c, x
in self.
sipm_pos_U.items()
if abs(x - x_coords[0]) < DX]
477 matches_V = [c
for c, x
in self.
sipm_pos_V.items()
if abs(x - x_coords[1]) < DX]
478 if not matches_U
or not matches_V:
479 raise ValueError(f
"x_coords {x_coords} do not match any SciFi channel within {DX} cm")
480 channels_x_U = channel_x_U = matches_U[0]
481 channels_x_V = channel_x_V = matches_V[0]
483 fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
486 centerU = next(c
for c, x
in self.
sipm_pos_U.items()
if abs(x) < DX)
487 centerV = next(c
for c, x
in self.
sipm_pos_V.items()
if abs(x) < DX)
489 def get_surrounding(d, target, N):
490 keys = sorted(d.keys(), key=
lambda k: d[k])
491 i = keys.index(target)
493 start = max(0, i - half)
494 end = min(len(keys), i + half + 1)
495 return keys[start:end]
498 chansU = get_surrounding(self.
sipm_pos_U, centerU, number_of_channels)
499 chansV = get_surrounding(self.
sipm_pos_V, centerV, number_of_channels)
501 chansU = get_surrounding(self.
sipm_pos_U, channels_x_U, number_of_channels)
502 chansV = get_surrounding(self.
sipm_pos_V, channels_x_V, number_of_channels)
503 all_chans = chansU + chansV
512 alpha = np.deg2rad(angle_U)
514 for chan
in all_chans:
517 angle = angle_U
if isU
else angle_V
519 alpha = np.deg2rad(angle)
522 shift = dy * np.tan(alpha)
527 (x0 + dx - shift, -dy),
528 (x0 - dx - shift, -dy),
532 col =
"blue" if isU
else "red"
534 ribbon = patches.Polygon(
543 y_top = L / 2
if isU
else L / 2 + DZ
545 box = patches.Rectangle(
552 hatch=
"//" if isU
else "\\\\",
556 if channel_x_U == chan
or channel_x_V == chan:
558 ribbon = patches.Polygon(
567 y_top = L / 2
if isU
else L / 2 + DZ
569 box = patches.Rectangle(
576 hatch=
"//" if isU
else "\\\\",
580 ribbon = patches.Polygon(
589 y_top = L / 2
if isU
else L / 2 + DZ
591 box = patches.Rectangle(
598 hatch=
"//" if isU
else "\\\\",
603 ax.set_aspect(
"equal")
605 shift = 3 * np.tan(alpha)
606 min_x = min([self.
sipm_pos_U[chan]
for chan
in chansU]) - shift
607 max_x = max(self.
sipm_pos_V[chan]
for chan
in chansV) + shift
608 ax.set_xlim(min_x, max_x)
611 min_x, max_x = -25, 25
613 ax.set_xlim(min_x, max_x)
615 ax.set_xlabel(
"X [cm]")
616 ax.set_ylabel(
"Y [cm]")
617 ax.set_title(
"SciFi channel ribbons & SiPM boxes (X–Y view)")
637 ax.legend(handles=legend_handles, title=
"Plane", loc=
"upper right")
639 plt.savefig(output_file)
641 print(f
"Saved channel ribbons + SiPM boxes to {output_file}")
646 number_of_channels=20,
647 output_file="scifi_combined_views.pdf",
652 """Draw combined Z–X and X–Y views of SciFi channel mappings side by side.
656 sGeo : ROOT.TGeoManager
657 The geometry manager for the detector setup.
658 number_of_channels : int, optional
659 Total number of channels to display (split evenly between U and V planes).
661 output_file : str, optional
662 Filename for the saved PDF plot. Default: 'scifi_combined_views.pdf'.
663 figsize : tuple of float, optional
664 Figure size in inches. Default is (18, 8).
666 Resolution of the figure in dots per inch. Default is 300.
667 alpha_fibre : float, optional
668 Transparency for fibre ellipses. Default is 0.4.
671 Saves a PDF file with the specified filename containing the combined views.
676 fig, (ax1, ax2) = plt.subplots(1, 2, figsize=figsize, dpi=dpi)
678 fibreVol = sGeo.FindVolumeFast(
"FiberVol")
679 R = fibreVol.GetShape().GetDX()
680 DX, DZ = 0.025, 0.135 / 2
682 center_U = [chan
for chan, x
in self.
sipm_pos_U.items()
if abs(x) <= DX - 0.001][0]
683 center_V = [chan
for chan, x
in self.
sipm_pos_V.items()
if abs(x) <= DX - 0.001][0]
685 def get_keys(d, target, N):
686 ks = sorted(d, key=
lambda k: d[k])
689 start = max(0, i - half)
690 end = min(len(ks), i + half + 1)
693 chansU = get_keys(self.
sipm_pos_U, center_U, number_of_channels)
694 chansV = get_keys(self.
sipm_pos_V, center_V, number_of_channels)
695 channels = chansU + chansV
696 n_chan = len(channels)
697 cmap = plt.get_cmap(
"tab20")
698 colors = [cmap(i / (n_chan - 1))
for i
in range(n_chan)]
702 for idx, chan
in enumerate(channels):
704 locChan = chan % 1000000
707 for fid
in fmap[locChan]:
708 gid = fid + int(1e8 + 1e6 + (0
if chan
in chansU
else 1) * 1e5)
709 self.
scifi.GetPosition(gid, AF, BF)
710 loc = self.
scifi.GetLocalPos(gid, BF)
713 for x, z
in zip(xs, zs):
714 ell = patches.Ellipse((x, z), 2 * R, 2 * R, color=
"orange", alpha=alpha_fibre)
716 self.
scifi.GetSiPMPosition(chan, BF, AF)
720 gidSiPM = locChan + int(1e8 + 1e6 + (0
if chan
in chansU
else 1) * 1e5)
721 loc = self.
scifi.GetLocalPos(gidSiPM, BF)
722 rx, rz = loc[0], loc[2]
723 rect = patches.Rectangle(
730 alpha=alpha_fibre * 0.8,
734 ax1.set_xlabel(
"X [cm]")
735 ax1.set_ylabel(
"Z [cm]")
736 ax1.set_aspect(
"equal")
738 ax1.set_title(
"Z–X: SciFi mappings")
745 centerU = [c
for c, x
in self.
sipm_pos_U.items()
if abs(x) < DX][0]
746 centerV = [c
for c, x
in self.
sipm_pos_V.items()
if abs(x) < DX][0]
747 chansU = get_keys(self.
sipm_pos_U, centerU, number_of_channels)
748 chansV = get_keys(self.
sipm_pos_V, centerV, number_of_channels)
749 all_ch = chansU + chansV
750 colorsU = [plt.get_cmap(
"Blues")(i / max(len(chansU) - 1, 1))
for i
in range(len(chansU))]
751 colorsV = [plt.get_cmap(
"Reds")(i / max(len(chansV) - 1, 1))
for i
in range(len(chansV))]
756 angle = angleU
if isU
else angleV
757 alpha_rad = np.deg2rad(angle)
760 shift = dy * np.tan(alpha_rad)
764 (x0 + dx - shift, -dy),
765 (x0 - dx - shift, -dy),
767 col = colorsU[chansU.index(chan)]
if isU
else colorsV[chansV.index(chan)]
768 poly = patches.Polygon(coords, closed=
True, facecolor=col, edgecolor=
"black", alpha=0.5)
771 box = patches.Rectangle(
781 ax2.set_xlabel(
"X [cm]")
782 ax2.set_ylabel(
"Y [cm]")
783 ax2.set_aspect(
"equal")
785 ax2.set_title(
"X–Y: Channel ribbons")
803 ax2.legend(handles=legend_handles, title=
"Plane", loc=
"upper right")
806 plt.savefig(output_file)