Belle II Software  release-05-01-25
test_std_charged.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import unittest
5 from basf2 import create_path
6 import stdCharged
7 
8 
9 _base_functions = [
10  stdCharged.stdPi,
11  stdCharged.stdK,
12  stdCharged.stdPr,
13  stdCharged.stdE,
14  stdCharged.stdMu
15 ]
16 
17 
18 class TestStdCharged(unittest.TestCase):
19  """Test case for charged standard particle lists"""
20 
21  def _check_list_name(self, target, functionname, particleloader):
22  """
23  Check that the list name we expect is the one that was
24  actually added to the ParticleLoader
25  """
26  for param in particleloader.available_params():
27  if param.name == 'decayStringsWithCuts':
28  name = param.values[0][0].split(':')[1]
29  self.assertTrue(
30  name == target,
31  "Name: \"%s\" added by function %s, expecting \"%s\""
32  % (name, functionname, target))
33 
34  def _check_listtype_exists(self, listtype, functions=_base_functions):
35  """check that a given listtype function works"""
36  for f in functions:
37  testpath = create_path()
38  f(listtype, path=testpath)
39  self.assertEqual(
40  len(testpath.modules()), 1,
41  "List %s doesn't work with function %s" % (listtype, f.__name__))
42  self.assertTrue(any(module.type() == "ParticleLoader" for module in testpath.modules()))
43 
44  # now we're certain that the loader is the only module on the path,
45  # so check it was added with the ParticleList name we expect
46  loader = testpath.modules()[0]
47  self._check_list_name(listtype, f.__name__, loader)
48 
49  def _check_function_call(self, functions=_base_functions, expectedlist=stdCharged._defaultlist):
50  """check that a function works (i.e. adds a particle loader)"""
51  for f in functions:
52  testpath = create_path()
53  f(path=testpath)
54  self.assertEqual(
55  len(testpath.modules()), 1,
56  "Function %s doesn't work" % f.__name__)
57  self.assertTrue(any(module.type() == "ParticleLoader" for module in testpath.modules()))
58  loader = testpath.modules()[0]
59  self._check_list_name(expectedlist, f.__name__, loader)
60 
61  def _check_listtype_does_not_exist(self, listtype, functions=_base_functions):
62  """check that a given listtype function doesn't do anything"""
63  for f in functions:
64  testpath = create_path()
65  f(listtype, path=testpath)
66  self.assertEqual(
67  len(testpath.modules()), 0,
68  "List %s works with function %s" % (listtype, f.__name__))
69  self.assertFalse(any(module.type() == "ParticleLoader" for module in testpath.modules()))
70 
72  """check that the builder functions fail with a nonexisting list"""
73  self._check_listtype_does_not_exist("flibble")
74 
75  def test_all_list(self):
76  """check that the builder functions all work with the all list"""
77  self._check_listtype_exists("all")
78 
79  def test_good_list(self):
80  """check that the builder functions all work with the good list"""
81  self._check_listtype_exists("good")
82  # all functions may be called with no arguments: makes a "good" list
83  self._check_function_call(_base_functions)
84 
85  def test_higheff(self):
86  """check that the builder functions all work with the higheff list"""
87  self._check_listtype_exists("higheff")
88 
89  def test_loose(self):
90  """check that the builder functions all work with the loose list"""
91  self._check_listtype_exists("loose")
92 
94  """check that the builder functions all work with the percentile eff lists"""
95  for function in _base_functions:
96  eff_exists = 0
97  for ename in stdCharged._effnames:
98  cut = stdCharged._stdChargedEffCuts(stdCharged._chargednames[_base_functions.index(function)],
99  ename)
100  if 0.0 < cut < 1.0:
101  self._check_listtype_exists(ename, [function])
102  eff_exists += 1
103  else:
104  self._check_listtype_does_not_exist(ename, [function])
105  self.assertTrue(
106  eff_exists,
107  "Function: \"%s\" has no valid list based on efficiency percentile."
108  % (function.__name__))
109 
111  """check that the builder functions work with the mostLikely lists"""
112  nLists = 5 # Number of ParticleLoader's expected
113  testpath = create_path()
114  stdCharged.stdMostLikely(path=testpath)
115  self.assertEqual(
116  len(testpath.modules()), nLists,
117  "There should be %i fillParticleList calls" % nLists)
118  self.assertTrue(any(module.type() == "ParticleLoader" for module in testpath.modules()))
119  for module in testpath.modules():
120  self._check_list_name('mostlikely', 'stdMostLikely', module)
121 
122 if __name__ == '__main__':
123  unittest.main()
test_std_charged.TestStdCharged.test_mostLikely_lists
def test_mostLikely_lists(self)
Definition: test_std_charged.py:110
test_std_charged.TestStdCharged._check_list_name
def _check_list_name(self, target, functionname, particleloader)
Definition: test_std_charged.py:21
test_std_charged.TestStdCharged.test_nonesense_list
def test_nonesense_list(self)
Definition: test_std_charged.py:71
stdCharged._stdChargedEffCuts
def _stdChargedEffCuts(particletype, listtype)
Definition: stdCharged.py:17
test_std_charged.TestStdCharged.test_percentile_eff
def test_percentile_eff(self)
Definition: test_std_charged.py:93
test_std_charged.TestStdCharged.test_higheff
def test_higheff(self)
Definition: test_std_charged.py:85
stdCharged.stdMostLikely
def stdMostLikely(pidPriors=None, suffix='', path=None)
Definition: stdCharged.py:165
test_std_charged.TestStdCharged.test_good_list
def test_good_list(self)
Definition: test_std_charged.py:79
test_std_charged.TestStdCharged
Definition: test_std_charged.py:18
test_std_charged.TestStdCharged._check_listtype_does_not_exist
def _check_listtype_does_not_exist(self, listtype, functions=_base_functions)
Definition: test_std_charged.py:61
test_std_charged.TestStdCharged._check_function_call
def _check_function_call(self, functions=_base_functions, expectedlist=stdCharged._defaultlist)
Definition: test_std_charged.py:49
test_std_charged.TestStdCharged.test_loose
def test_loose(self)
Definition: test_std_charged.py:89
test_std_charged.TestStdCharged._check_listtype_exists
def _check_listtype_exists(self, listtype, functions=_base_functions)
Definition: test_std_charged.py:34
test_std_charged.TestStdCharged.test_all_list
def test_all_list(self)
Definition: test_std_charged.py:75