5 Test HepMC reader and writer by reading in a hepmc file and writing out one
6 again and checking if they match.
14 import modularAnalysis
as ma
15 from tempfile
import TemporaryDirectory
18 input_filename = b2.find_file(
"generators/tests/event.hepmc")
19 if len(input_filename) == 0:
20 sys.stderr.write(
"TEST SKIPPED: input file " + input_filename +
" not found.")
23 with TemporaryDirectory()
as tmp:
25 output_filename =
"event.hepmc.out"
28 path = ma.create_path()
29 path.add_module(
"HepMCInput", inputFileList=[input_filename], expNum=0, runNum=0)
30 path.add_module(
"HepMCOutput", OutputFilename=output_filename)
35 Check only lines that contain numbers
37 HepMC lines look like this:
38 Letter Number Number Number Number ...
41 E: event, V: vertex, P: particle
43 return li.split()[0]
in [
"E",
"V",
"P"]
52 return [float(t)
for t
in tokens[start_index:]]
54 def get_nonempty_lines(f):
55 return [li
for li
in f
if not li.isspace()]
57 with open(input_filename)
as inputfile, open(output_filename)
as outputfile:
58 lines_input = get_nonempty_lines(inputfile)
59 lines_output = get_nonempty_lines(outputfile)
60 for line_input, line_output
in zip(lines_input, lines_output):
61 if valid_line(line_input):
62 assert valid_line(line_output)
63 numbers_input = parse_line(line_input)
64 numbers_output = parse_line(line_output)
65 for number_input, number_output
in zip(numbers_input, numbers_output):
66 assert math.isclose(number_input, number_output, rel_tol=1e-5)