Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_detectors.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file test_detectors.py
1 import pytest
2 
3 from helpers import dd4hepEnabled
4 
5 from common import getOpenDataDetectorDirectory
6 from acts.examples.odd import getOpenDataDetector
7 
8 import acts.examples
9 
10 
11 def count_surfaces(geo):
12  __tracebackhide__ = True
13  nSurfaces = 0
14 
15  def visit(srf):
16  nonlocal nSurfaces
17  nSurfaces += 1
18 
19  geo.visitSurfaces(visit)
20 
21  return nSurfaces
22 
23 
24 def check_extra_odd(srf):
25  if srf.geometryId().volume() in [28, 30, 23, 25, 16, 18]:
26  assert srf.geometryId().extra() != 0
27  return
28 
29 
31  detector, geo, contextDecorators = acts.examples.GenericDetector.create()
32  assert detector is not None
33  assert geo is not None
34  assert contextDecorators is not None
35 
36  assert count_surfaces(geo) == 18728
37 
38 
40  n_surfaces = 10
41 
42  detector, geo, contextDecorators = acts.examples.TelescopeDetector.create(
43  bounds=[100, 100],
44  positions=[10 * i for i in range(n_surfaces)],
45  stereos=[0] * n_surfaces,
46  binValue=0,
47  )
48 
49  assert detector is not None
50  assert geo is not None
51  assert contextDecorators is not None
52 
53  assert count_surfaces(geo) == n_surfaces
54 
55 
56 @pytest.mark.skipif(not dd4hepEnabled, reason="DD4hep is not set up")
57 def test_odd():
58  config = acts.MaterialMapJsonConverter.Config()
59  matDeco = acts.JsonMaterialDecorator(
60  rConfig=config,
61  jFileName=str(
62  getOpenDataDetectorDirectory() / "config/odd-material-mapping-config.json"
63  ),
64  level=acts.logging.WARNING,
65  )
66 
67  detector, geo, _ = getOpenDataDetector(getOpenDataDetectorDirectory(), matDeco)
68 
69  geo.visitSurfaces(check_extra_odd)
70 
71  assert count_surfaces(geo) == 18824
72 
73 
75  detector, geo, deco = acts.examples.AlignedDetector.create()
76 
77  assert detector is not None
78  assert geo is not None
79  assert deco is not None
80 
81  assert count_surfaces(geo) == 18728
82 
83 
84 import itertools
85 
86 
87 def test_tgeo_config_triplet(monkeypatch):
88  from acts.examples import TGeoDetector, Interval
89 
90  # monkeypatch the comparison operator
91  def eq(self, other):
92  return self.lower == other.lower and self.upper == other.upper
93 
94  monkeypatch.setattr(Interval, "__eq__", eq)
95 
96  LayerTriplet = TGeoDetector.Config.LayerTriplet
97  c = TGeoDetector.Config
98 
99  def assert_combinations(value, _type):
100  t = LayerTriplet(value)
101  assert t.negative == value and t.central == value and t.positive == value
102  assert isinstance(t, _type)
103 
104  keys = ["negative", "central", "positive"]
105 
106  combinations = (
107  [(k,) for k in keys] + list(itertools.combinations(keys, 2)) + [keys]
108  )
109 
110  for c in combinations:
111  d = {k: value for k in c}
112 
113  t = LayerTriplet(**d)
114  assert isinstance(t, _type)
115  for k in c:
116  assert getattr(t, k) == value
117 
118  v = ["Some::SensorName"]
119  assert_combinations(v, c.LayerTripletVectorString)
120 
121  with pytest.raises(TypeError):
122  LayerTriplet(["Some::SensorName", 848])
123 
124  with pytest.raises(TypeError):
125  LayerTriplet(("Some::SensorName", 848))
126 
127  for v in (True, False):
128  assert_combinations(v, c.LayerTripletBool)
129 
130  assert_combinations("hallo", c.LayerTripletString)
131 
132  assert_combinations(5.3, c.LayerTripletDouble)
133 
134  assert_combinations(Interval(5.0, 9.0), c.LayerTripletInterval)
135 
136  with pytest.raises(TypeError):
137  LayerTriplet(("a", 9))
138 
139  v = (4.4, 2.2)
140  t = LayerTriplet(v)
141  assert t.negative == Interval(*v)
142  assert t.central == Interval(*v)
143  assert t.positive == Interval(*v)
144 
145 
146 def test_tgeo_config_volume(monkeypatch):
147  from acts.examples import TGeoDetector, Interval
148 
149  # monkeypatch the comparison operator
150  def eq(self, other):
151  return self.lower == other.lower and self.upper == other.upper
152 
153  monkeypatch.setattr(Interval, "__eq__", eq)
154 
155  Volume = TGeoDetector.Config.Volume
156 
157  v = Volume(name="blubb")
158  assert v
159 
160  for key in ("binToleranceR", "binToleranceZ", "binTolerancePhi"):
161  v = Volume(**{key: Interval(4, 5)})
162  assert getattr(v, key) == Interval(4, 5)
163 
164  v = Volume(**{key: (4, 5)})
165  assert getattr(v, key) == Interval(4, 5)
166 
167  v = Volume(**{key: (None, 5)})
168  assert getattr(v, key) == Interval(None, 5)
169 
170  v = Volume(**{key: (4, None)})
171  assert getattr(v, key) == Interval(4, None)