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