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