File indexing completed on 2026-01-09 09:26:48
0001 import pytest
0002 from pathlib import Path
0003
0004 from helpers import dd4hepEnabled
0005
0006 import acts.examples
0007 from acts.examples.odd import getOpenDataDetector
0008
0009
0010 def count_surfaces(geo):
0011 __tracebackhide__ = True
0012 nSurfaces = 0
0013
0014 def visit(srf):
0015 nonlocal nSurfaces
0016 nSurfaces += 1
0017
0018 geo.visitSurfaces(visit)
0019
0020 return nSurfaces
0021
0022
0023 def check_extra_odd(srf):
0024 if srf.geometryId.volume in [28, 30, 23, 25, 16, 18]:
0025 assert srf.geometryId.extra != 0
0026 return
0027
0028
0029 def test_generic_geometry():
0030 detector = acts.examples.GenericDetector()
0031 trackingGeometry = detector.trackingGeometry()
0032 contextDecorators = detector.contextDecorators()
0033 assert detector is not None
0034 assert trackingGeometry is not None
0035 assert contextDecorators is not None
0036
0037 assert count_surfaces(trackingGeometry) == 18728
0038
0039
0040 def test_telescope_geometry():
0041 n_surfaces = 10
0042
0043 config = acts.examples.TelescopeDetector.Config(
0044 bounds=[100, 100],
0045 positions=[10 * i for i in range(n_surfaces)],
0046 stereos=[0] * n_surfaces,
0047 binValue=0,
0048 )
0049 detector = acts.examples.TelescopeDetector(config)
0050 trackingGeometry = detector.trackingGeometry()
0051 contextDecorators = detector.contextDecorators()
0052
0053 assert detector is not None
0054 assert trackingGeometry is not None
0055 assert contextDecorators is not None
0056
0057 assert count_surfaces(trackingGeometry) == n_surfaces
0058
0059
0060 @pytest.mark.skipif(not dd4hepEnabled, reason="DD4hep is not set up")
0061 def test_odd():
0062 with getOpenDataDetector() as detector:
0063 trackingGeometry = detector.trackingGeometry()
0064
0065 trackingGeometry.visitSurfaces(check_extra_odd)
0066
0067 assert count_surfaces(trackingGeometry) == 18824
0068
0069
0070 import itertools
0071
0072
0073 def test_tgeo_config_triplet(monkeypatch):
0074
0075 from acts.examples.tgeo import TGeoDetector, Interval
0076
0077
0078 def eq(self, other):
0079 return self.lower == other.lower and self.upper == other.upper
0080
0081 monkeypatch.setattr(Interval, "__eq__", eq)
0082
0083 LayerTriplet = TGeoDetector.Config.LayerTriplet
0084 c = TGeoDetector.Config
0085
0086 def assert_combinations(value, _type):
0087 t = LayerTriplet(value)
0088 assert t.negative == value and t.central == value and t.positive == value
0089 assert isinstance(t, _type)
0090
0091 keys = ["negative", "central", "positive"]
0092
0093 combinations = (
0094 [(k,) for k in keys] + list(itertools.combinations(keys, 2)) + [keys]
0095 )
0096
0097 for c in combinations:
0098 d = {k: value for k in c}
0099
0100 t = LayerTriplet(**d)
0101 assert isinstance(t, _type)
0102 for k in c:
0103 assert getattr(t, k) == value
0104
0105 v = ["Some::SensorName"]
0106 assert_combinations(v, c.LayerTripletVectorString)
0107
0108 with pytest.raises(TypeError):
0109 LayerTriplet(["Some::SensorName", 848])
0110
0111 with pytest.raises(TypeError):
0112 LayerTriplet(("Some::SensorName", 848))
0113
0114 for v in (True, False):
0115 assert_combinations(v, c.LayerTripletBool)
0116
0117 assert_combinations("hallo", c.LayerTripletString)
0118
0119 assert_combinations(5.3, c.LayerTripletDouble)
0120
0121 assert_combinations(Interval(5.0, 9.0), c.LayerTripletInterval)
0122
0123 with pytest.raises(TypeError):
0124 LayerTriplet(("a", 9))
0125
0126 v = (4.4, 2.2)
0127 t = LayerTriplet(v)
0128 assert t.negative == Interval(*v)
0129 assert t.central == Interval(*v)
0130 assert t.positive == Interval(*v)
0131
0132
0133 def test_tgeo_config_volume(monkeypatch):
0134 from acts.examples.tgeo import TGeoDetector, Interval
0135
0136
0137 def eq(self, other):
0138 return self.lower == other.lower and self.upper == other.upper
0139
0140 monkeypatch.setattr(Interval, "__eq__", eq)
0141
0142 Volume = TGeoDetector.Config.Volume
0143
0144 v = Volume(name="blubb")
0145 assert v
0146
0147 for key in ("binToleranceR", "binToleranceZ", "binTolerancePhi"):
0148 v = Volume(**{key: Interval(4, 5)})
0149 assert getattr(v, key) == Interval(4, 5)
0150
0151 v = Volume(**{key: (4, 5)})
0152 assert getattr(v, key) == Interval(4, 5)
0153
0154 v = Volume(**{key: (None, 5)})
0155 assert getattr(v, key) == Interval(None, 5)
0156
0157 v = Volume(**{key: (4, None)})
0158 assert getattr(v, key) == Interval(4, None)
0159
0160
0161 def test_coordinate_converter(trk_geo):
0162 from acts.examples import json
0163
0164 digiCfg = acts.examples.DigitizationAlgorithm.Config(
0165 digitizationConfigs=acts.examples.json.readDigiConfigFromJson(
0166 str(
0167 Path(__file__).parent.parent.parent.parent
0168 / "Examples/Configs/generic-digi-smearing-config.json"
0169 )
0170 ),
0171 surfaceByIdentifier=trk_geo.geoIdSurfaceMap(),
0172 )
0173 converter = acts.examples.DigitizationCoordinatesConverter(digiCfg)
0174
0175 def test_surface(surface):
0176 gctx = acts.GeometryContext()
0177 geo_id = surface.geometryId.value
0178 geo_center = surface.center(gctx)
0179 x, y, z = geo_center[0], geo_center[1], geo_center[2]
0180
0181
0182 assert converter.globalToLocal(geo_id, x, y, z) == (0, 0)
0183 assert converter.localToGlobal(geo_id, 0, 0) == (x, y, z)
0184
0185
0186 global_shifted = converter.localToGlobal(geo_id, 5, 5)
0187 local_shifted = converter.globalToLocal(geo_id, *global_shifted)
0188 assert abs(local_shifted[0] - 5) / 5 < 1e-6
0189 assert abs(local_shifted[1] - 5) / 5 < 1e-6
0190
0191 trk_geo.visitSurfaces(test_surface)