DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
binary_dump.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3import argparse
4import datetime
5import h5py
6import struct
7from hdf5_dump import print_header, print_fragment_header
8
9
11 frag_magic_word = bytes.fromhex("22221111")
12 trh_magic_word = bytes.fromhex("44443333")
13 trh_loc = []
14 frag_loc = []
15 with open(fname, "rb") as f:
16 iloc = 0
17 nrec = 0
18 while (byte := f.read(4)):
19 if byte == trh_magic_word:
20 trh_loc.append(iloc)
21 frag_loc.append([])
22 nrec += 1
23 if byte == frag_magic_word:
24 frag_loc[nrec-1].append(iloc)
25 iloc += 4
26 print(80*'=')
27 print(19*'=', " Location of records in the binary file ", 19*'=')
28 print(80*'=')
29 for i in range(len(trh_loc)):
30 if i >= g_n_request:
31 break
32 print("trigger {:<8} header - {:<16}".format(i+1, trh_loc[i]),
33 "fragments: ", frag_loc[i])
34 return (trh_loc, frag_loc)
35
36
37def parse_binary_file(fname, k_n_request, k_print_out, clock_speed_hz,
38 k_list_components):
39 frag_magic_word = bytes.fromhex("22221111")
40 trh_magic_word = bytes.fromhex("44443333")
41 tsl_magic_word = bytes.fromhex("66665555")
42 with open(fname, "rb") as f:
43 bytesbuffer = bytearray()
44 ntrh = 0
45 nfrag = 0
46 record_type = ""
47 while (byte := f.read(4)):
48 if record_type == "":
49 if byte == trh_magic_word:
50 record_type = "TriggerRecord"
51 if byte == tsl_magic_word:
52 record_type = "TimeSlice"
53 if byte == trh_magic_word or byte == tsl_magic_word:
54 nfrag = 0
55 if ntrh !=0 and k_print_out in ['both', 'fragment']:
56 print(31*"-", "Fragment Header ", 31*"-")
57 print_fragment_header(bytesbuffer, clock_speed_hz)
58 bytesbuffer = bytearray()
59 ntrh += 1
60 if ntrh > k_n_request and k_n_request != 0: break
61 if byte == frag_magic_word:
62 if nfrag == 0 and ntrh != 0 and k_print_out in ['both',
63 'header']:
64 print(24*"=", "TriggerRecord/TimeSlice Header", 24*"=")
65 print_header(bytesbuffer, record_type, clock_speed_hz,
66 k_list_components)
67 if nfrag != 0 and k_print_out in ['both', 'fragment']:
68 print(31*"-", "Fragment Header ", 31*"-")
69 print_fragment_header(bytesbuffer, clock_speed_hz)
70 bytesbuffer = bytearray()
71 nfrag += 1
72 bytesbuffer.extend(byte)
73 return
74
75
77 parser = argparse.ArgumentParser(
78 description='Python script to parse binary files converted from HDF5.')
79
80 parser.add_argument('-f', '--file-name',
81 help='path to binary file',
82 required=True)
83
84 parser.add_argument('-p', '--print-out',
85 choices=['header', 'fragment', 'both'],
86 default='both',
87 help='select which part of data to display')
88
89 parser.add_argument('-l', '--list-components',
90 help='''list components in trigger record header, used
91 with "--print-out header" or "--print-out both", not
92 applicable to TimeSlice data''', action='store_true')
93
94
95 parser.add_argument('-d', '--debug',
96 help='print locations of headers in the file',
97 action='store_true')
98
99 parser.add_argument('-n', '--num-of-records', type=int,
100 help='specify number of records to be parsed',
101 default=0)
102
103 parser.add_argument('-s', '--speed-of-clock', type=float,
104 help='''specify clock speed in Hz, default is
105 62500000.0 (62.5MHz)''',
106 default=62500000.0)
107
108 parser.add_argument('-v', '--version', action='version',
109 version='%(prog)s 1.0')
110 return parser.parse_args()
111
112
113def main():
114 args = parse_args()
115
116 bfile = args.file_name
117 print("Reading file", bfile)
118
119 parse_binary_file(bfile, args.num_of_records, args.print_out,
120 args.speed_of_clock, args.list_components)
121 if args.debug:
123 return
124
125
126if __name__ == "__main__":
127 main()
get_record_locations_from_binary_file(fname)
parse_binary_file(fname, k_n_request, k_print_out, clock_speed_hz, k_list_components)