Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 07:52:37

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