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