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