6 from b2test_utils
import clean_working_directory, safe_process, show_only_errors
7 from ROOT
import Belle2
12 def check_mc_events(outfile, expect):
13 """Check if the number of MC events reported by the file metadata is what whe expect"""
15 metadata = subprocess.check_output([
"b2file-metadata-show",
"--json", outfile])
16 mcEvents = json.loads(metadata)[
"mcEvents"]
17 except Exception
as e:
18 basf2.B2ERROR(f
"Cannot determine number of MC events: {e}")
21 if mcEvents != expect:
22 basf2.B2ERROR(f
"{outfile} MCEvents not correct: got {mcEvents}, expected {expect}")
25 basf2.B2INFO(f
"{outfile}: MCEvents=={expect} Ok")
29 def run_rootio(outfile, infiles, expect, *, N=0, **input_kwargs):
31 Run RootInput/RootOutput and check that the number of events is what we want
34 outfile (str): filename for the output
35 infiles (list): list of input filenames
36 expect (int): numer of MC events we expect in the final file
37 N (int): pass to process() as second argument to limit the number of events
39 All other arguments are passed to RootInput as module parameters
43 path = basf2.create_path()
44 path.add_module(
"RootInput", inputFileNames=infiles, **input_kwargs)
45 path.add_module(
"RootOutput", outputFileName=outfile)
48 basf2.logging.zero_counters()
49 with show_only_errors():
52 result = check_mc_events(outfile, expect)
53 failures += 0
if result
else 1
58 basf2.logging.enable_summary(
False)
62 with clean_working_directory():
67 for i
in range(1, 16, 7):
68 all_files.append(f
"events-{i:03d}.root")
69 event_counts.append(i)
70 generate = basf2.create_path()
71 generate.add_module(
"EventInfoSetter", evtNumList=i)
72 generate.add_module(
"RootOutput", outputFileName=all_files[-1])
73 with show_only_errors():
74 safe_process(generate)
77 for name, total, filenames, sequence
in [
78 (
"single", event_counts[-1], all_files[-1:], [
"1:3"]),
79 (
"total", sum(event_counts), all_files, [
"0",
"1:2",
"2-"]),
82 run_rootio(f
"{name}.root", filenames, total)
84 run_rootio(f
"{name}-skip1.root", filenames, 0, skipNEvents=1)
85 run_rootio(f
"{name}-skipTo.root", filenames, 0, skipToEvent=[0, 0, 1])
86 run_rootio(f
"{name}-sequence.root", filenames, 0, entrySequences=sequence)
89 for N
in [1, total-1, total, 100000]:
91 run_rootio(f
"{name}-basf2-n-{N}.root", filenames, 0
if N < total
else total)
93 run_rootio(f
"{name}-proc-{N}.root", filenames, 0
if N < total
else total, N=N)
97 run_rootio(f
"duplicate.root", all_files*2, 0)
100 basf2.B2ERROR(f
"{failures} tests failed ...")