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