Belle II Software development
test_std_hyperons.py
1#!/usr/bin/env python3
2
3
10
11import unittest
12from basf2 import create_path
13from stdHyperons import stdXi, stdXi0, stdOmega, goodXi, goodXi0, goodOmega
14from b2bii import setB2BII, unsetB2BII
15
16
17class TestStdHyperons(unittest.TestCase):
18 """Test case for standard hyperon lists"""
19
20 def remove_spaces(self, s):
21 """Remove all spaces within a string"""
22 return "".join(s.split(' '))
23
25 self,
26 std_function,
27 expected_lists=[]):
28 """
29 Check the given particle lists are created.
30 The std_function ONLY takes one path argument.
31 """
32 testpath = create_path()
33 std_function(path=testpath)
34
35 built_lists = []
36 for module in testpath.modules():
37 for param in module.available_params():
38 if module.type() == 'ParticleLoader' and param.name == 'decayStringsWithCuts':
39 listname = param.values[0][0].split('->')[0]
40 elif module.type() == 'ParticleListManipulator' and param.name == 'outputListName':
41 listname = str(param.values).split(' -> ')[0]
42 elif module.type() == 'ParticleCombiner' and param.name == 'decayString':
43 listname = param.values.split(' -> ')[0]
44 else:
45 continue
46 listname = self.remove_spaces(listname)
47 if listname not in built_lists:
48 built_lists.append(listname)
49 expected_lists = [self.remove_spaces(listname) for listname in expected_lists]
50
51 for expected in expected_lists:
52 if expected not in built_lists:
53 return False
54 return True
55
56 def test_stdXi(self):
57 """Check stdXi"""
58 for fitter in ['KFit', 'TreeFit']:
59 self.assertTrue(self._check_list(lambda path: stdXi(fitter=fitter, path=path), expected_lists=['Xi-:std']))
60 # Also serves as a test for _check_list
61 self.assertFalse(
62 self._check_list(
63 lambda path: stdXi(
64 fitter='TreeFit',
65 path=path),
66 expected_lists=['Xi-:good']))
67 # Allow spaces in particle list
68 self.assertTrue(
69 self._check_list(
70 lambda path: stdXi(
71 fitter='TreeFit',
72 path=path),
73 expected_lists=['Xi- : std']))
74
76 """Check stdXi for B2BII settings"""
77 setB2BII()
78 for fitter in ['KFit', 'TreeFit']:
79 self.assertTrue(self._check_list(lambda path: stdXi(fitter=fitter, path=path), expected_lists=['Xi-:std']))
80 unsetB2BII()
81
82 def test_stdXi0(self):
83 """Check stdXi0"""
84 for gamma_efficiency in ['eff20', 'eff30', 'eff40', 'eff50', 'eff60']:
85 self.assertTrue(
86 self._check_list(
87 lambda path: stdXi0(
88 gammatype=gamma_efficiency,
89 path=path),
90 expected_lists=['Xi0:std']))
91
93 """Check stdXi0 for B2BII settings"""
94 setB2BII()
95 for gamma_efficiency in ['eff20', 'eff30', 'eff40', 'eff50', 'eff60']:
96 self.assertTrue(
97 self._check_list(
98 lambda path: stdXi0(
99 gammatype=gamma_efficiency,
100 path=path),
101 expected_lists=['Xi0:std']))
102 unsetB2BII()
103
104 def test_stdOmega(self):
105 """Check stdOmega"""
106 for fitter in ['KFit', 'TreeFit']:
107 self.assertTrue(
108 self._check_list(
109 lambda path: stdOmega(
110 fitter=fitter,
111 path=path),
112 expected_lists=['Omega-:std']))
113
115 """Check stdOmega for B2BII settings"""
116 setB2BII()
117 for fitter in ['KFit', 'TreeFit']:
118 self.assertTrue(
119 self._check_list(
120 lambda path: stdOmega(
121 fitter=fitter,
122 path=path),
123 expected_lists=['Omega-:std']))
124 unsetB2BII()
125
126 def test_goodXi(self):
127 """Check goodXi lists: veryloose, loose, tight"""
128 for xitype in ['veryloose', 'loose', 'tight']:
129 def create_list(path):
130 goodXi(xitype, path)
131 self.assertTrue(self._check_list(create_list, expected_lists=['Xi-:std', f'Xi-:{xitype}']))
132 # Should be no 'Xi' list. Make sure we did not make typos.
133 self.assertFalse(self._check_list(create_list, expected_lists=[f'Xi:{xitype}']))
134
136 """Check goodXi lists: veryloose, loose, tight. Test behavior if std list was already added before."""
137 for xitype in ['veryloose', 'loose', 'tight']:
138 def create_list(path):
139 stdXi(path=path)
140 goodXi(xitype, path)
141 self.assertTrue(self._check_list(create_list, expected_lists=['Xi-:std', f'Xi-:{xitype}']))
142 # Should be no 'Xi' list. Make sure we did not make typos.
143 self.assertFalse(self._check_list(create_list, expected_lists=[f'Xi:{xitype}']))
144
145 def test_goodXi0(self):
146 """Check goodXi0 lists: veryloose, loose, tight"""
147 for xitype in ['veryloose', 'loose', 'tight']:
148 def create_list(path):
149 goodXi0(xitype, path)
150 self.assertTrue(self._check_list(create_list, expected_lists=['Xi0:std', f'Xi0:{xitype}']),
151 f"xitype = {xitype}")
152 self.assertFalse(self._check_list(create_list, expected_lists=[f'Xi:{xitype}']))
153
155 """Check goodXi0 lists: veryloose, loose, tight. Test behavior if std list was already added before."""
156 for xitype in ['veryloose', 'loose', 'tight']:
157 def create_list(path):
158 stdXi0(path=path)
159 goodXi0(xitype, path)
160 self.assertTrue(self._check_list(create_list, expected_lists=['Xi0:std', f'Xi0:{xitype}']),
161 f"xitype = {xitype}")
162 self.assertFalse(self._check_list(create_list, expected_lists=[f'Xi:{xitype}']))
163
164 def test_goodOmega(self):
165 """Check goodOmega lists: veryloose, loose, tight"""
166 for omegatype in ['veryloose', 'loose', 'tight']:
167 def create_list(path):
168 goodOmega(omegatype, path)
169 self.assertTrue(self._check_list(create_list, expected_lists=['Omega-:std', f'Omega-:{omegatype}']))
170 self.assertFalse(self._check_list(create_list, expected_lists=[f'Omega:{omegatype}']))
171
173 """Check goodOmega lists: veryloose, loose, tight. Test behavior if std list was already added before."""
174 for omegatype in ['veryloose', 'loose', 'tight']:
175 def create_list(path):
176 stdOmega(path=path)
177 goodOmega(omegatype, path)
178 self.assertTrue(self._check_list(create_list, expected_lists=['Omega-:std', f'Omega-:{omegatype}']))
179 self.assertFalse(self._check_list(create_list, expected_lists=[f'Omega:{omegatype}']))
180
181
182if __name__ == '__main__':
183 unittest.main()
def _check_list(self, std_function, expected_lists=[])