36 def __init__(self, name, intree, outtree=None, mcBranchName=None) -> None:
40 super().
__init__(name, intree, mcBranchName=mcBranchName, outtree=outtree)
45 self.
MCadc = ROOT.std.vector(
"std::vector<float>")()
50 ubt_geo = global_variables.ShipGeo.UpstreamTagger
53 self.
_tile_sizes = {int(region): float(size)
for region, size
in ubt_geo.RegionTileSize.items()}
54 self.
_grid_x = (ubt_geo.TileGridOriginX, ubt_geo.TileGridEndX)
55 self.
_grid_y = (ubt_geo.TileGridOriginY, ubt_geo.TileGridEndY)
64 raise ValueError(f
"Invalid UBT tile-ID grid size: {self._grid_size}")
70 if multiple <= 0
or not math.isclose(tile_size, multiple * self.
_grid_size, rel_tol=0.0, abs_tol=1.0e-9):
72 f
"UBT tile size {tile_size} cm is not an integer multiple of the tile-ID base cell size"
73 f
" {self._grid_size} cm"
83 ubt_geo.ADCResponseMapFile, set(self.
_tile_sizes.values())
88 """Raise UBTGeometryMismatch if the UBT points of this geometry cannot be digitized.
90 The decision is taken from the simulated geometry, not from the
91 configuration, which configure replaces for old geometry files. A
92 geometry without any UBT region predates the scintillator UBT: its UBT
93 was a vacuum box, whose detector IDs are not region IDs. Otherwise
94 every region of the UBT map must be placed, with the same tile size.
97 raise RuntimeError(
"Load the geometry of the simulation (FAIRGeom) before digitizing the UBT")
100 nodes = assembly.GetNodes()
if assembly
else None
101 for node
in nodes
or []:
104 volumes[node.GetNumber()] = f
"{match.group(1)}cm"
107 "the geometry has no UBT regions: it predates the scintillator UBT (a vacuum box)"
109 expected = {int(region): f
"{round(float(size))}cm" for region, size
in ubt_geo.RegionTileSize.items()}
110 differing = sorted(r
for r
in expected.keys() | volumes.keys()
if expected.get(r) != volumes.get(r))
113 f
"{len(differing)} UBT regions of the geometry differ from the UBT map, e.g. {differing[:5]}"
124 """Load the ADC/MeV vs local position (mm) TH2D for each tile size (cm)."""
128 response_file = ROOT.TFile.Open(path)
129 except OSError
as error:
131 f
"Cannot open the UBT ADC response map file {path}; it is stored with Git LFS, so run `git lfs pull`"
132 " if it has not been fetched"
135 for tile_size
in tile_sizes:
136 name = f
"adc_per_mev_{round(10.0 * tile_size)}mm"
137 histogram = response_file.Get(name)
138 if not isinstance(histogram, ROOT.TH2D):
139 raise ValueError(f
"UBT ADC response map {path} has no TH2D '{name}'")
140 histogram.SetDirectory(ROOT.gROOT)
141 maps[tile_size] = histogram
142 response_file.Close()
155 coordinate: float, grid_origin: float, grid_end: float, tile_size: float, tile_count: int
157 """Return the globally aligned tile center containing coordinate."""
158 coordinate = min(max(coordinate, grid_origin), math.nextafter(grid_end, grid_origin))
162 tile_index = min(math.floor((coordinate - grid_origin) / tile_size), tile_count - 1)
163 return grid_origin + (tile_index + 0.5) * tile_size
172 def tile_id(self, position: ROOT.TVector3, tile_size: float) -> int:
173 """Return a stable unique ID for a constituent tile.
175 The tile is identified by its lower-left cell on the tile-ID grid, the
176 grid of the smallest tile side (2 cm) over the tile grid bounds, with
177 ``columns`` x ``rows`` cells:
179 tile ID = (tile side / grid size - 1) * columns * rows + row * columns + column
181 So each tile side has its own range of ``columns * rows`` IDs, and a
182 2 cm and a 4 cm tile with the same lower-left cell get different IDs.
183 For the default geometry (340 x 504 cm2, 170 x 252 cells), the range
184 of 2 cm tiles is [0, 42840) and that of 4 cm tiles [42840, 85680). The
185 stride follows the UBT box, so decoding an ID needs the grid of its
188 side_index, cell = divmod(tile_id, columns * rows)
189 row, column = divmod(cell, columns)
191 column = round((position.X() - tile_size / 2.0 - self.
_grid_x[0]) / self.
_grid_size)
192 row = round((position.Y() - tile_size / 2.0 - self.
_grid_y[0]) / self.
_grid_size)
193 if not (0 <= column < self.
_columns and 0 <= row < self.
_rows):
195 f
"UBT tile at ({position.X()}, {position.Y()}) cm with size {tile_size} cm "
196 f
"maps to cell ({column}, {row}), outside the {self._columns} x {self._rows} tile-ID grid; "
197 f
"x bounds={self._grid_x} cm, y bounds={self._grid_y} cm, base cell size={self._grid_size} cm"
201 def adc_signal(self, point, tile_center: ROOT.TVector3, tile_size: float) -> float:
202 """Return the (unrounded) ADC signal of a point from its energy and position in the tile."""
204 local_x_mm = 10.0 * (point.GetX() - tile_center.X())
205 local_y_mm = 10.0 * (point.GetY() - tile_center.Y())
209 x_axis = response_map.GetXaxis()
210 y_axis = response_map.GetYaxis()
211 x_bin = min(max(x_axis.FindFixBin(local_x_mm), 1), x_axis.GetNbins())
212 y_bin = min(max(y_axis.FindFixBin(local_y_mm), 1), y_axis.GetNbins())
213 adc_per_mev = response_map.GetBinContent(x_bin, y_bin)
214 deposited_energy_mev = 1000.0 * point.GetEnergyLoss()
215 return deposited_energy_mev * adc_per_mev
218 """Digitize UBT MC points into one hit per fired constituent tile.
220 An MC point covers a (track, region) pair and a region holds 25 or 100
221 tiles, so a tile can collect points of several tracks, e.g. a muon and
222 a delta ray. Their ADC signals, each taken at its own position in the
223 tile, are summed before rounding and before the trigger threshold is
224 applied. The hit time is that of the first point in the tile.
226 tiles: dict[int, _FiredTile] = {}
227 for index, aMCPoint
in enumerate(self.
intree.UpstreamTaggerPoint):
228 tile_size = self.
_tile_sizes[aMCPoint.GetDetectorID()]
230 tile_id = self.
tile_id(position, tile_size)
231 tile = tiles.get(tile_id)
233 tile = tiles[tile_id] =
_FiredTile(aMCPoint, position)
234 elif aMCPoint.GetTime() < tile.first.GetTime():
235 tile.first = aMCPoint
236 tile.points.append(index)
237 tile.signals.append(self.
adc_signal(aMCPoint, position, tile_size))
239 for tile_id, tile
in tiles.items():
240 adc_counts = max(0, round(sum(tile.signals)))
241 aHit = ROOT.UpstreamTaggerHit(tile.first, self.
intree.t0, tile.position, self.
_time_res)
242 aHit.SetTileID(tile_id)
243 aHit.SetADC(adc_counts)
245 self.
det.push_back(aHit)
246 if self.
MCdet is not None:
247 self.
MCdet.push_back(ROOT.std.vector(
"int")(tile.points))
248 if self.
MCadc is not None:
249 self.
MCadc.push_back(ROOT.std.vector(
"float")(tile.signals))