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