Belle II Software  release-08-01-10
prune_datastore.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2
12 from ROOT import Belle2
13 from b2test_utils import clean_working_directory, safe_process
14 
15 
16 class TestModule(basf2.Module):
17  """Test if the DataStore contains the expected content."""
18 
19  def __init__(self, is_inverted):
20  """Create a new instance. If is_inverted is True we check of absence of content"""
21  super().__init__()
22 
23  self._is_inverted_is_inverted = is_inverted
24 
25  def event(self):
26  """reimplementation of Module::event().
27 
28  Checks for the amount of PXD Clusters and PXD Digits after
29  the prune module was run
30  """
31 
32  PXDClusters = Belle2.PyStoreArray('PXDClusters')
33  PXDDigits = Belle2.PyStoreArray('PXDDigits')
34 
35  # PXDClusters are in our keep list, should still be there
36  if self._is_inverted_is_inverted:
37  assert PXDClusters.getEntries() == 0
38  # while the PXDDigits should be empty
39  assert PXDDigits.getEntries() > 0
40  else:
41  assert PXDClusters.getEntries() > 0
42  # while the PXDDigits should be empty
43  assert PXDDigits.getEntries() == 0
44 
45  # ensure the eventmetadata has been kept, which is implicitly done by
46  # PruneDataStore
47  evtmetadata = Belle2.PyStoreObj('EventMetaData')
48  assert evtmetadata
49 
50 
51 basf2.set_log_level(basf2.LogLevel.ERROR)
52 basf2.conditions.disable_globaltag_replay()
53 basf2.set_random_seed("something important")
54 # make sure FATAL messages don't have the function signature as this makes
55 # problems with clang printing namespaces differently
56 basf2.logging.set_info(basf2.LogLevel.FATAL, basf2.logging.get_info(basf2.LogLevel.ERROR))
57 # find file to read
58 input_file = basf2.find_file('framework/tests/root_input.root')
59 
60 with clean_working_directory():
61  main = basf2.Path()
62 
63  main.add_module('RootInput', inputFileName=input_file)
64  main.add_module('EventInfoPrinter')
65  main.add_module('PrintCollections')
66  main.add_module('PruneDataStore', matchEntries=['PXDClusters.*'])
67  main.add_module('PrintCollections')
68  main.add_module(TestModule(False))
69 
70  # ensure the pruned datastore is still write-able to disk
71  main.add_module('RootOutput', outputFileName='prune_datastore_output_test.root', updateFileCatalog=False)
72 
73  # Process events
74  basf2.process(main)
75 
76  # now test if the negated logic works, too
77  main = basf2.Path()
78 
79  main.add_module('RootInput', inputFileName=input_file)
80  main.add_module('EventInfoPrinter')
81  main.add_module('PrintCollections')
82  main.add_module('PruneDataStore', matchEntries=['PXDClusters.*'], keepMatchedEntries=False)
83  main.add_module('PrintCollections')
84  main.add_module(TestModule(True))
85 
86  # ensure the pruned datastore is still write-able to disk
87  main.add_module('RootOutput', outputFileName='prune_datastore_output_test.root', updateFileCatalog=False)
88 
89  # Process events
90  basf2.process(main)
91 
92  # now test if a regex which cannot be compiled is properly reported
93  main = basf2.Path()
94 
95  main.add_module('RootInput', inputFileName=input_file)
96  main.add_module('EventInfoPrinter')
97  main.add_module('PrintCollections')
98  main.add_module('PruneDataStore', matchEntries=['[a-b][a'], keepMatchedEntries=False)
99  main.add_module('PrintCollections')
100 
101  # Process events
102  assert(not safe_process(main) == 0)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def __init__(self, is_inverted)
_is_inverted
variable to remember if we test for existence or absence