30def main(filenames, nrecords, nskip, channel_map, print_headers, print_adc_stats, check_timestamps, det, quiet):
31
32 for filename in filenames:
33
34 h5_file = HDF5RawDataFile(filename)
35
36 records = h5_file.get_all_record_ids()
37
38 if nskip > len(records):
39 print(f'Requested records to skip {nskip} is greater than number of records {len(records)}. Exiting...')
40 return
41 if nrecords>0:
42 if (nskip+nrecords)>len(records):
43 nrecords=-1
44 else:
45 nrecords=nskip+nrecords
46
47 records_to_process = []
48 if nrecords==-1:
49 records_to_process = records[nskip:]
50 else:
51 records_to_process = records[nskip:nrecords]
52 print(f'Will process {len(records_to_process)} of {len(records)} records.')
53
54
55 ch_map = None
56 if channel_map is not None:
57 ch_map = detchannelmaps.make_tpc_map(channel_map)
58 offline_ch_num_dict = {}
59 offline_ch_plane_dict = {}
60
61 for r in records_to_process:
62
63 if not quiet:
64 print(f'Processing (Record Number,Sequence Number)=({r[0],r[1]})')
65
66 wib_geo_ids = h5_file.get_geo_ids_for_subdetector(r,detdataformats.DetID.string_to_subdetector(det))
67
68 timestamps_frame0 = []
69 for gid in wib_geo_ids:
70
71 det_link = 0xffff & (gid >> 48);
72 det_slot = 0xffff & (gid >> 32);
73 det_crate = 0xffff & (gid >> 16);
74 det_id = 0xffff & gid;
75 subdet = detdataformats.DetID.Subdetector(det_id)
76 det_name = detdataformats.DetID.subdetector_to_string(subdet)
77
78 if not quiet:
79 print(f'\tProcessing subdetector {det_name}, crate {det_crate}, slot {det_slot}, link {det_link}')
80
81 frag = h5_file.get_frag(r,gid)
82 frag_hdr = frag.get_header()
83 frag_type = frag.get_fragment_type()
84 frag_type_name = "Unknown"
85 frag_ts = frag.get_trigger_timestamp()
86
87 unpacker = None
88 utils = None
89 FrameType = None
90 if(frag_type==daqdataformats.FragmentType.kWIB):
91 frag_type_name="WIB"
92 unpacker = wib2_unpack
93 utils = wib2_utils
94 FrameType = fddetdataformats.WIB2Frame
95 elif(frag_type==daqdataformats.FragmentType.kWIBEth):
96 frag_type_name="WIBEth"
97 unpacker = wibeth_unpack
98 utils = wibeth_utils
99 FrameType = fddetdataformats.WIBEthFrame
100
101 if not quiet:
102 print(f'\tFragment type is {frag_type} ({frag_type_name})')
103 print(f'\tTrigger timestamp for fragment is {frag_ts}')
104
105 if unpacker is None:
106 print('\tUnrecognized WIB fragment type. Continue.')
107 continue
108
109 n_frames = unpacker.get_n_frames(frag)
110 if not quiet:
111 print(f'\tFound {n_frames} {frag_type_name} frames.')
112
113 wf = FrameType(frag.get_data())
114
115
116 if print_headers:
117 print('\n\t==== WIB HEADER (First Frame) ====')
118 utils.print_header(wf,"\t\t")
119
120
121 if(offline_ch_num_dict.get(gid) is None):
122 if channel_map is None:
123 offline_ch_num_dict[gid] = np.arange(64)
124 offline_ch_plane_dict[gid] = np.full(64,9999)
125 else:
126 crate, slot, stream, nchans = None, None, None, None
127 if frag_type==daqdataformats.FragmentType.kWIBEth:
128 dh = wf.get_daqheader()
129 offline_ch_num_dict[gid] = np.array([ch_map.get_offline_channel_from_det_crate_slot_stream_chan(dh.det_id, dh.crate_id, dh.slot_id, dh.stream_id, c) for c in range(64)])
130 else:
131 wh = wf.get_header()
132 offline_ch_num_dict[gid] = np.array([ch_map.get_offline_channel_from_det_crate_slot_stream_chan(wh.detector_id, wh.crate, wh.slot, wh.link, c) for c in range(256)])
133 offline_ch_plane_dict[gid] = np.array([ ch_map.get_plane_from_offline_channel(uc) for uc in offline_ch_num_dict[gid] ])
134
135
136
137 if check_timestamps:
138 timestamps = unpacker.np_array_timestamp(frag)
139 timestamps_diff = np.diff(timestamps)
140 timestamps_diff_vals, timestamps_diff_counts = np.unique(timestamps_diff, return_counts=True)
141
142 if(n_frames>0):
143 timestamps_frame0.append(timestamps[0])
144 if not quiet or len(timestamps_diff_counts)>1:
145 print('\n\t==== TIMESTAMP CHECK ====')
146 print(f'\t\tTimestamps (First, Last, Min, Max): ({timestamps[0]},{timestamps[-1]},{np.min(timestamps)},{np.max(timestamps)})')
147 print(f'\t\tTimestamp diffs: {timestamps_diff_vals}')
148 print(f'\t\tTimestamp diff counts: {timestamps_diff_counts}')
149 print(f'\t\tAverage diff: {np.mean(timestamps_diff)}')
150 else:
151 timestamps_frame0.append(0)
152
153
154
155 if print_adc_stats:
156
157
158 adcs = unpacker.np_array_adc(frag)
159 adcs_rms = np.std(adcs,axis=0)
160 adcs_ped = np.mean(adcs,axis=0)
161
162 print('\n\t====WIB DATA====')
163
164 for ch,rms in enumerate(adcs_rms):
165 print(f'\t\tch {offline_ch_num_dict[gid][ch]} (plane {offline_ch_plane_dict[gid][ch]}): ped = {adcs_ped[ch]:.2f}, rms = {adcs_rms[ch]:.4f}')
166
167
168
169
170 if check_timestamps:
171 timestamps_frame0 = np.array(timestamps_frame0)
172 timestamps_frame0_diff = timestamps_frame0 - timestamps_frame0[0]
173
174 if not quiet or np.any(timestamps_frame0_diff):
175 print('\n\t==== TIMESTAMP ACROSS WIBS CHECK ====')
176 print(f'\t\tTimestamp diff relative to first WIB',timestamps_frame0_diff)
177
178
179
180
181 print(f'Processed all requested records')
182
183
int main(int argc, char **argv)