SND@LHC Software
Loading...
Searching...
No Matches
test_shipGeoConfig.py
Go to the documentation of this file.
1#!/usr/bin/env python
2import unittest
3import shipunit as u
4import os
5from ShipGeoConfig import AttrDict, ConfigRegistry
6
7
8class TestSingleConfig(unittest.TestCase):
9 def setUp(self):
10 self.key = "bb"
11 with ConfigRegistry.register_config("bb") as c:
12 c.length = 10
13 c.width = 20
14
15 def test_len(self):
16 assert len(ConfigRegistry.keys()) == 1, list(ConfigRegistry.keys())
17
18 def test_key(self):
19 assert self.key in ConfigRegistry.keys()
20
21 def test_value(self):
22 assert ConfigRegistry[self.key].length == 10
23
24 def tearDown(self):
25 ConfigRegistry.clean()
26
27
28class TestInheritance(unittest.TestCase):
29 def setUp(self):
30 with ConfigRegistry.register_config("bb") as c:
31 c.length = 10
32 c.width = 20
33 c.muShield = AttrDict(z=10)
34 with ConfigRegistry.register_config("cc", base="bb") as c:
35 c.height = 30
36 c.volume = c.length * c.height * c.width
37
38 def test_len(self):
39 assert len(ConfigRegistry.keys()) == 2
40
41 def test_key(self):
42 assert "bb" in ConfigRegistry.keys()
43 assert "cc" in ConfigRegistry.keys()
44
45 def test_value(self):
46 assert ConfigRegistry["bb"].length == 10
47 assert ConfigRegistry["cc"].length == 10
48 assert ConfigRegistry["cc"].muShield.z == 10
49 assert ConfigRegistry["bb"].muShield.z == 10
50
51 def test_latest(self):
52 c = ConfigRegistry.get_latest_config()
53 self.assertTrue(c is not None)
54 self.assertEqual(c.height, 30)
55
56 def tearDown(self):
57 ConfigRegistry.clean()
58
59
60class TestStringSingleConfig(unittest.TestCase):
61 def setUp(self):
62 self.key = "basic"
63 config = """
64import shipunit as u
65from ShipGeoConfig import AttrDict, ConfigRegistry
66
67with ConfigRegistry.register_config("basic") as c:
68 c.vetoStation = AttrDict(z=-2390.*u.cm)
69 c.TrackStation1 = AttrDict(z=1510.*u.cm)
70 c.TrackStation2 = AttrDict(z=1710.*u.cm)
71 c.TrackStation3 = AttrDict(z=2150.*u.cm)
72 c.TrackStation4 = AttrDict(z=2370.*u.cm)
73
74 c.z = c.TrackStation2.z + 0.5 * (c.TrackStation3.z - c.TrackStation2.z)
75
76 c.Bfield = AttrDict(z=c.z)
77 c.Bfield.max = 1.5*u.kilogauss # was 1.15 in EOI
78
79 # target absorber muon shield setup
80 c.decayVolume = AttrDict(z=0*u.cm)
81 c.decayVolume.length = 50*u.m
82
83 c.muShield = AttrDict(z=0*u.cm)
84 c.muShield.dZ1 = 2.5*u.m
85 c.muShield.dZ2 = 3.5*u.m
86 c.muShield.dZ3 = 3.0*u.m
87 c.muShield.dZ4 = 3.0*u.m
88 c.muShield.dZ5 = 2.5*u.m
89 c.muShield.dZ6 = 2.5*u.m
90 c.muShield.LE = 5*u.m
91"""
92 ConfigRegistry.loadpys(config)
93
94 def test_len(self):
95 assert len(ConfigRegistry.keys()) == 1, list(ConfigRegistry.keys())
96
97 def test_key(self):
98 assert self.key in ConfigRegistry.keys()
99
100 def test_value(self):
101 assert ConfigRegistry[self.key].Bfield.max == 1.5*u.kilogauss
102
103 def tearDown(self):
104 ConfigRegistry.clean()
105
106
107class TestStringConditionalConfig(unittest.TestCase):
108 def setUp(self):
109 self.key = "basic"
110 self.config = """
111import shipunit as u
112from ShipGeoConfig import AttrDict, ConfigRegistry
113
114with ConfigRegistry.register_config("basic") as c:
115 c.vetoStation = AttrDict(z=-2390.*u.cm)
116 c.TrackStation1 = AttrDict(z=1510.*u.cm)
117 c.TrackStation2 = AttrDict(z=1710.*u.cm)
118 c.TrackStation3 = AttrDict(z=2150.*u.cm)
119 c.TrackStation4 = AttrDict(z=2370.*u.cm)
120
121 c.z = c.TrackStation2.z + 0.5 * (c.TrackStation3.z - c.TrackStation2.z)
122
123 c.Bfield = AttrDict(z=c.z)
124 c.Bfield.max = 1.5*u.kilogauss # was 1.15 in EOI
125
126 # target absorber muon shield setup
127 c.decayVolume = AttrDict(z=0*u.cm)
128 c.decayVolume.length = 50*u.m
129
130 if MU_SHIELD_ENABLED:
131 c.muShield = AttrDict(z=0*u.cm)
132 c.muShield.dZ1 = 2.5*u.m
133 c.muShield.dZ2 = 3.5*u.m
134 c.muShield.dZ3 = 3.0*u.m
135 c.muShield.dZ4 = 3.0*u.m
136 c.muShield.dZ5 = 2.5*u.m
137 c.muShield.dZ6 = 2.5*u.m
138 c.muShield.LE = 5*u.m
139"""
140
141 def test_true(self):
142 c = ConfigRegistry.loadpys(self.config, MU_SHIELD_ENABLED=True)
143 self.assertTrue("muShield" in c)
144 assert self.key in ConfigRegistry.keys()
145 assert len(ConfigRegistry.keys()) == 1, list(ConfigRegistry.keys())
146 assert ConfigRegistry[self.key].Bfield.max == 1.5*u.kilogauss
147 assert ConfigRegistry[self.key].muShield.dZ1 == 2.5*u.m
148
149 def test_false(self):
150 ConfigRegistry.loadpys(self.config, MU_SHIELD_ENABLED=False)
151 assert self.key in ConfigRegistry.keys()
152 assert len(ConfigRegistry.keys()) == 1, list(ConfigRegistry.keys())
153 assert ConfigRegistry[self.key].Bfield.max == 1.5*u.kilogauss
154 self.assertTrue("muShield" not in ConfigRegistry[self.key])
155
156 def tearDown(self):
157 ConfigRegistry.clean()
158
159
160class TestStringNewLine(unittest.TestCase):
161 def setUp(self):
162 self.key = "basic"
163 self.config = """import shipunit as u\r\nfrom ShipGeoConfig import AttrDict, ConfigRegistry\r\nwith ConfigRegistry.register_config("basic") as c:\r
164 c.vetoStation = AttrDict(z=-2390.*u.cm)\r\n"""
165 self.filename = "x.py"
166 fh = open(self.filename, "w")
167 fh.write(self.config)
168 fh.close()
169
170 def test_true(self):
171 c = ConfigRegistry.loadpys(self.config)
172 self.assertTrue("vetoStation" in c)
173 assert ConfigRegistry[self.key].vetoStation.z == -2390*u.cm
174
175 def test_readDOS(self):
176 c = ConfigRegistry.loadpy(self.filename, Yheight = 1)
177 self.assertTrue("vetoStation" in c)
178 assert ConfigRegistry[self.key].vetoStation.z == -2390*u.cm
179
180 def tearDown(self):
181 ConfigRegistry.clean()
182 os.remove(self.filename)
183
184if __name__ == '__main__':
185 unittest.main()