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