Belle II Software light-2509-fornax
test_std_photons.py
1#!/usr/bin/env python3
2
3
10
11import inspect
12import unittest
13
14from basf2 import create_path
15import stdPhotons
16
17
18class TestStdPhotons(unittest.TestCase):
19 """Test case for standard photon lists"""
20
21 def _check_list(self, listtype=None, std_function=stdPhotons.stdPhotons, expected_lists=["all"]):
22 """check that a given listtype function works"""
23 testpath = create_path()
24 if (std_function is stdPhotons.stdPhotons) and (listtype is not None):
25 std_function(listtype, path=testpath)
26 else:
27 std_function(path=testpath)
28
29 # verify that we load only the list-creating modules
30 self.assertEqual(
31 len(testpath.modules()), len(set(expected_lists)) + 1 if listtype == 'all' else len(set(expected_lists)) + 3,
32 f"List {listtype} doesn't work with function {std_function.__name__}")
33 self.assertTrue(all((module.type() == "ParticleLoader") or (module.type() == "ParticleListManipulator")
34 or (module.type() == "ParticleSelector")
35 for module in testpath.modules()))
36
37 #
38 built_list = []
39 for module in testpath.modules():
40 for param in module.available_params():
41 if param.name == 'decayStrings':
42 name = param.values[0].split(':')[1]
43 built_list.append(name)
44 if param.name == 'outputListName':
45 name = str(param.values).split(':')[1]
46 built_list.append(name)
47
48 # we have the particle lists we expect
49 for a, b in zip(built_list, expected_lists):
50 self.assertEqual(a, b, f"Loaded list \'{a}\' instead of \'{b}\' with function {std_function.__name__}")
51
53 """Check that the builder function raises a ValueError for a non-existing list name."""
54 # \cond false positive doxygen warning
55 self.assertRaises(ValueError, self._check_list, "flibble")
56 # \endcond
57
59 """
60 Check that the default list type is one of the lists in the cases that are checked for in :func:`stdPhotons.stdPhotons`.
61
62 This test relies on ``ValueError`` being raised for nonsense list types, which is tested by
63 :func:`test_nonsense_list`. However, :func:`test_nonsense_list` doesn't ensure that the default list works, so
64 for that this test is needed.
65 """
66 test_path = create_path()
67 try:
68 stdPhotons.stdPhotons(path=test_path)
69 except ValueError:
70 stdPhotons_signature = inspect.signature(stdPhotons.stdPhotons)
71 default_listtype = stdPhotons_signature.parameters["listtype"].default
72 self.fail(f"stdPhotons default listtype {default_listtype} is not in set of allowed list names.")
73
75 """Check that the default list type (loose) works."""
76 # basically a duplicate of test_loose_list
77 self._check_list(expected_lists=["cdc", "cdc", "loose"])
78
79 def test_all_list(self):
80 """check that the builder function works with the all list"""
81 self._check_list("all", expected_lists=["all"])
82
83 def test_cdc_list(self):
84 """check that the builder function works with the cdc list"""
85 self._check_list("cdc", expected_lists=["cdc"])
86
87 def test_loose_list(self):
88 """check that the builder function works with the loose list"""
89 self._check_list("loose", expected_lists=["cdc", "cdc", "loose"])
90
92 """check that the builder function works with the default (loose) list"""
93 self._check_list(expected_lists=["cdc", "cdc", "loose"])
94
95 def test_tight_list(self):
96 """check that the builder function works with the tight list"""
97 self._check_list("tight", expected_lists=["cdc", "cdc", "loose", "tight"])
98
100 """check that the builder function works with the pi0eff60_May2020 list"""
101 self._check_list("pi0eff60_May2020", expected_lists=["pi0eff60_May2020"])
102
104 """check that the builder function works with the pi0eff50_May2020 list"""
105 self._check_list("pi0eff50_May2020", expected_lists=["pi0eff50_May2020"])
106
108 """check that the builder function works with the pi0eff40_May2020 list"""
109 self._check_list("pi0eff40_May2020", expected_lists=["pi0eff40_May2020"])
110
112 """check that the builder function works with the pi0eff30_May2020 list"""
113 self._check_list("pi0eff30_May2020", expected_lists=["pi0eff30_May2020"])
114
116 """check that the builder function works with the pi0eff20_May2020 list"""
117 self._check_list("pi0eff20_May2020", expected_lists=["pi0eff20_May2020"])
118
120 """check that the builder function works with the pi0eff10_May2020 list"""
121 self._check_list("pi0eff10_May2020", expected_lists=["pi0eff10_May2020"])
122
123 def test_skim(self):
124 """check that the builder function works with the skim list"""
125 self._check_list("skim", std_function=stdPhotons.loadStdSkimPhoton, expected_lists=["cdc", "cdc", "loose", "skim"])
126
127 def test_belle(self):
128 """check that the builder function works with the belle list"""
129 self._check_list("goodBelle", std_function=stdPhotons.loadStdGoodBellePhoton, expected_lists=["goodBelle"])
130
131
132if __name__ == '__main__':
133 unittest.main()
_check_list(self, listtype=None, std_function=stdPhotons.stdPhotons, expected_lists=["all"])
stdPhotons(listtype='loose', path=None, beamBackgroundMVAWeight="", fakePhotonMVAWeight="", biasCorrectionTable="")
Definition stdPhotons.py:20