Belle II Software  release-05-01-25
test_skims.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """Test skims by running them all as a single combined steering file."""
5 
6 from importlib import import_module
7 from glob import glob
8 
9 from b2test_utils import clean_working_directory, require_file
10 import basf2 as b2
11 import modularAnalysis as ma
12 from skimExpertFunctions import CombinedSkim
13 from skim.registry import Registry
14 from reconstruction import add_skim_software_trigger
15 
16 __authors__ = ["Phil Grace"]
17 
18 # NOTE: Another way this test could have been written is to run the standalone steering
19 # file for every skim:
20 #
21 # for SkimName in Registry.names:
22 # b2.b2test_utils.check_error_free(
23 # "b2skim-run", f"Skim {SkimName}", "skim", toolopts=[SkimName, "-n", "10"]
24 # )
25 #
26 # I think what is written below is preferable, as it makes the overall running time of
27 # this test smaller.
28 
29 
30 def get_skim_object(SkimName):
31  """Get an instance of the skim class for the given skim.
32 
33  This is achieved by importing the module listed alongside the skim name in the
34  `skim.registry.Registry`.
35 
36  Parameters:
37  SkimName (str): Name of the skim to be found.
38 
39  Returns:
40  SkimObject: The an instance of the requested skim object.
41  """
42  ModuleName = Registry.get_skim_module(SkimName)
43  SkimModule = import_module(f"skim.{ModuleName}")
44  SkimClass = getattr(SkimModule, SkimName)
45  return SkimClass()
46 
47 
48 def main():
49  # Prepend the GT containing, by definition, the most up-to-date HLT skim menu
50  b2.conditions.prepend_globaltag(b2.conditions.default_globaltags[0])
51 
52  path = b2.Path()
53  mdst_files = glob(f'{b2.find_file("mdst/tests")}/mdst-v*.root')
54  mdst_files.sort(reverse=True)
55  ma.inputMdstList("default", require_file(mdst_files[0]), path=path)
56 
57  # Recompute the HLT skim decision to avoid crashes due to missing HLT skims
58  add_skim_software_trigger(path)
59 
60  SkimObjects = [get_skim_object(skim) for skim in Registry.names]
61  skim = CombinedSkim(*[skim for skim in SkimObjects if not isinstance(skim, CombinedSkim)])
62  skim(path)
63 
64  b2.process(path, max_event=10)
65 
66 
67 if __name__ == "__main__":
68  with clean_working_directory():
69  main()
skim
Definition: __init__.py:1
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
skim.registry
Definition: registry.py:1