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