File indexing completed on 2025-01-30 09:14:56
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 def test_aligned_detector():
0071 detector = acts.examples.AlignedDetector()
0072 trackingGeometry = detector.trackingGeometry()
0073 decorators = detector.contextDecorators()
0074
0075 assert detector is not None
0076 assert trackingGeometry is not None
0077 assert decorators is not None
0078
0079 assert count_surfaces(trackingGeometry) == 18728
0080
0081
0082 import itertools
0083
0084
0085 def test_tgeo_config_triplet(monkeypatch):
0086 from acts.examples import TGeoDetector, Interval
0087
0088
0089 def eq(self, other):
0090 return self.lower == other.lower and self.upper == other.upper
0091
0092 monkeypatch.setattr(Interval, "__eq__", eq)
0093
0094 LayerTriplet = TGeoDetector.Config.LayerTriplet
0095 c = TGeoDetector.Config
0096
0097 def assert_combinations(value, _type):
0098 t = LayerTriplet(value)
0099 assert t.negative == value and t.central == value and t.positive == value
0100 assert isinstance(t, _type)
0101
0102 keys = ["negative", "central", "positive"]
0103
0104 combinations = (
0105 [(k,) for k in keys] + list(itertools.combinations(keys, 2)) + [keys]
0106 )
0107
0108 for c in combinations:
0109 d = {k: value for k in c}
0110
0111 t = LayerTriplet(**d)
0112 assert isinstance(t, _type)
0113 for k in c:
0114 assert getattr(t, k) == value
0115
0116 v = ["Some::SensorName"]
0117 assert_combinations(v, c.LayerTripletVectorString)
0118
0119 with pytest.raises(TypeError):
0120 LayerTriplet(["Some::SensorName", 848])
0121
0122 with pytest.raises(TypeError):
0123 LayerTriplet(("Some::SensorName", 848))
0124
0125 for v in (True, False):
0126 assert_combinations(v, c.LayerTripletBool)
0127
0128 assert_combinations("hallo", c.LayerTripletString)
0129
0130 assert_combinations(5.3, c.LayerTripletDouble)
0131
0132 assert_combinations(Interval(5.0, 9.0), c.LayerTripletInterval)
0133
0134 with pytest.raises(TypeError):
0135 LayerTriplet(("a", 9))
0136
0137 v = (4.4, 2.2)
0138 t = LayerTriplet(v)
0139 assert t.negative == Interval(*v)
0140 assert t.central == Interval(*v)
0141 assert t.positive == Interval(*v)
0142
0143
0144 def test_tgeo_config_volume(monkeypatch):
0145 from acts.examples import TGeoDetector, Interval
0146
0147
0148 def eq(self, other):
0149 return self.lower == other.lower and self.upper == other.upper
0150
0151 monkeypatch.setattr(Interval, "__eq__", eq)
0152
0153 Volume = TGeoDetector.Config.Volume
0154
0155 v = Volume(name="blubb")
0156 assert v
0157
0158 for key in ("binToleranceR", "binToleranceZ", "binTolerancePhi"):
0159 v = Volume(**{key: Interval(4, 5)})
0160 assert getattr(v, key) == Interval(4, 5)
0161
0162 v = Volume(**{key: (4, 5)})
0163 assert getattr(v, key) == Interval(4, 5)
0164
0165 v = Volume(**{key: (None, 5)})
0166 assert getattr(v, key) == Interval(None, 5)
0167
0168 v = Volume(**{key: (4, None)})
0169 assert getattr(v, key) == Interval(4, None)
0170
0171
0172 def test_coordinate_converter(trk_geo):
0173 digiCfg = acts.examples.DigitizationAlgorithm.Config(
0174 digitizationConfigs=acts.examples.readDigiConfigFromJson(
0175 str(
0176 Path(__file__).parent.parent.parent.parent
0177 / "Examples/Algorithms/Digitization/share/default-smearing-config-generic.json"
0178 )
0179 ),
0180 surfaceByIdentifier=trk_geo.geoIdSurfaceMap(),
0181 )
0182 converter = acts.examples.DigitizationCoordinatesConverter(digiCfg)
0183
0184 def test_surface(surface):
0185 gctx = acts.GeometryContext()
0186 geo_id = surface.geometryId().value()
0187 geo_center = surface.center(gctx)
0188 x, y, z = geo_center[0], geo_center[1], geo_center[2]
0189
0190
0191 assert converter.globalToLocal(geo_id, x, y, z) == (0, 0)
0192 assert converter.localToGlobal(geo_id, 0, 0) == (x, y, z)
0193
0194
0195 global_shifted = converter.localToGlobal(geo_id, 5, 5)
0196 local_shifted = converter.globalToLocal(geo_id, *global_shifted)
0197 assert abs(local_shifted[0] - 5) / 5 < 1e-6
0198 assert abs(local_shifted[1] - 5) / 5 < 1e-6
0199
0200 trk_geo.visitSurfaces(test_surface)