Belle II Software  release-08-01-10
seqroot_input.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # Test SeqRootInputModule. Input data is the same as used by chain_input.py
12 # (both files)
13 
14 import os
15 import basf2
16 from ROOT import Belle2
17 import gzip
18 import glob
19 import struct
20 from b2test_utils import clean_working_directory, safe_process, skip_test_if_light
21 
22 skip_test_if_light() # cannot simulate events in a light release
23 
24 basf2.conditions.disable_globaltag_replay()
25 basf2.set_random_seed("something important")
26 # simplify logging output to just the type and the message
27 basf2.logging.enable_summary(False)
28 for level in basf2.LogLevel.values.values():
29  basf2.logging.set_info(level, basf2.LogInfo.LEVEL | basf2.LogInfo.MESSAGE)
30 
31 
32 class TestModule(basf2.Module):
33 
34  """Test to read relations in the input files."""
35 
36  def event(self):
37  """reimplementation of Module::event().
38 
39  prints PXD true and simhit indices, using relations
40  """
41 
42  truehits = Belle2.PyStoreArray('PXDTrueHits')
43  for truehit in truehits:
44  relations = truehit.getRelationsTo("PXDSimHits")
45  print("truehit %d => hits " % truehit.getArrayIndex(), end="")
46  for hit in relations:
47  print(hit.getArrayIndex(), end=", ")
48  print()
49 
50 
51 # ============================================================================
52 # Now lets create the necessary modules to perform a simulation
53 
54 filename = basf2.find_file('framework/tests/seqroot_input.sroot')
55 
56 main = basf2.Path()
57 seqinput = main.add_module("SeqRootInput", logLevel=basf2.LogLevel.WARNING)
58 main.add_module("EventInfoPrinter")
59 main.add_module("PrintCollections", printForEvent=0)
60 main.add_module(TestModule())
61 
62 with clean_working_directory():
63  content = open(filename, "rb").read()
64  first_record = struct.unpack("@i", content[:4])[0]
65  second_record = struct.unpack("@i", content[first_record:first_record + 4])[0] + first_record
66  # write the file twice, we want to check that we will read the sroot and the
67  # sroot-1. But we need to omit the first record in the second one
68  open("01-ok.sroot", "wb").write(content)
69  open("01-ok.sroot-1", "wb").write(content[first_record:])
70  # write the first record + 1 byte which will cause an invalid read on the
71  # record size
72  open("02-1+1b.sroot", "wb").write(content[:first_record + 1])
73  # now just write the first record + 5 bytes: this will be enough for the
74  # next record size but will not contain the record
75  open("03-1+5b.sroot", "wb").write(content[:first_record + 5])
76  # write the second record + 1 byte which will cause an invalid read on the
77  # record size
78  open("04-2+1b.sroot", "wb").write(content[:second_record + 1])
79  # now just write the second record + 5 bytes: this will be enough for the
80  # next record size but will not contain the record
81  open("05-2+5b.sroot", "wb").write(content[:second_record + 5])
82  gzip.open("05-ok.sroot.gz", "wb").write(content)
83  gcontent = open("05-ok.sroot.gz", "rb").read()
84  gzip.open("05-ok.sroot-1.gz", "wb").write(content[first_record:])
85  gzip.open("06-1+1b.sroot.gz", "wb").write(content[:first_record + 1])
86  gzip.open("07-1+5b.sroot.gz", "wb").write(content[:first_record + 5])
87  # write the uncompressed file with compression suffix to trigger
88  # decompression error
89  open("08-nogz.sroot.gz", "wb").write(content)
90  # only write half the compressed file, this will give an decompression error
91  open("09-cut.sroot.gz", "wb").write(gcontent[:len(gcontent) // 2])
92  # and one where the record size is extremely large
93  open("10-max.sroot", "wb").write(struct.pack("@i", 0x7fffffff))
94 
95  # FIXME: this does not work on GitLab runner.
96  # and one without permission
97  # open("11-no-permission.sroot", "wb").write(content)
98  # os.chmod("11-no-permission.sroot", 0)
99 
100  files = sorted(glob.glob("*.sroot") + glob.glob("*.sroot.gz")) + \
101  ["nosuchfile.sroot", "/nosuchdir/file.sroot"]
102 
103  for filename in files:
104  print("Trying", filename, "...")
105  seqinput.param("inputFileName", filename)
106  safe_process(main)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72