DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
hdf5_wib2_to_binary.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Created on: 21/02/2023 15:28
4
5Author: Shyam Bhuller
6
7Description: Python script to write WIB2 frames from DUNE-DAQ HDF5 files to binary files.
8"""
9
10import argparse
11
12from hdf5libs import HDF5RawDataFile
13import daqdataformats
14import fddetdataformats
15
16from rich import print
17
18
19def Debug(x):
20 """ print if we are in debug mode
21
22 Args:
23 x (any): thing to prints
24 """
25 if debug:
26 print(x)
27
28
29def main(args):
30 h5file = HDF5RawDataFile(args.file_name)
31
32 total_records = len(h5file.get_all_record_ids())
33 if args.n_records == -1:
34 args.n_records = total_records
35 if args.n_records > total_records:
36 raise Exception(f"Number of specified records is greater than the total {total}")
37
38 n_links = len([f for f in h5file.get_fragment_dataset_paths(1) if f.split("_")[-1] == "WIB"]) # exclude other fragments in the record that are not WIB frames
39 if args.link >= n_links:
40 raise Exception(f"Link number out of range.")
41
42 out_name = f"wib_link_{args.link}.bin"
43 with open(out_name, "wb") as bf:
44 total_frames = 0
45 # loop over all triggers
46 for i in range(args.n_records):
47 header = h5file.get_record_header_dataset_path(i+1) # trigger number starts at 1
48 Debug(header)
49
50 fragments = h5file.get_fragment_dataset_paths(i+1)
51 fragments = [f for f in fragments if f.split("_")[-1] == "WIB"] # exclude other fragments in the record that are not WIB frames
52
53 Debug(f"loading fragment: {fragments[args.link]}")
54 f = h5file.get_frag(fragments[args.link])
55
56 WIB2Frame_size = fddetdataformats.WIB2Frame.sizeof()
57
58 n_frames = (f.get_size() - f.get_header().sizeof()) // WIB2Frame_size # calculate the number of wib frames per fragment
59 for j in range(n_frames):
60 Debug(f.get_fragment_type())
61 Debug(f.get_element_id())
62
63 data = fddetdataformats.WIB2Frame(f.get_data(j * WIB2Frame_size)) # unpack fragment to WIB2Frame
64
65 Debug(f"{data.sizeof()=}")
66
67 bf.write(bytearray(data.get_bytes())) # write binary data to the file
68 total_frames += j
69 print(f"writing {total_frames} WIB2 frames to binary file.", "\r")
70 print(f"wrote {args.n_records} fragments from wib link {args.link} to file {out_name}.")
71 return
72
73if __name__ == "__main__":
74 parser = argparse.ArgumentParser(description = 'Python script to write WIB2 frames from DUNE-DAQ HDF5 files to binary files.')
75 parser.add_argument(dest = "file_name", help = 'path to HDF5 file')
76 parser.add_argument("-l", "--link", dest = "link", type = int, help = "link number to conver to binary", required = True)
77 parser.add_argument('-n', '--num-of-records', dest = "n_records", type = int, help = 'specify number of records to be parsed, -1 will parse all records', default = 0, required = True)
78 parser.add_argument("--debug", dest = "debug", action = "store_true", help = "Debugging information")
79 args = parser.parse_args()
80 debug = args.debug
81 Debug(vars(args))
82 main(args)