27def main(filename, nrecords, nskip, channel_map, print_headers, print_adc_stats, check_timestamps, det):
28
29 h5_file = HDF5RawDataFile(filename)
30
31 records = h5_file.get_all_record_ids()
32
33 if nskip > len(records):
34 print(f'Requested records to skip {nskip} is greater than number of records {len(records)}. Exiting...')
35 return
36 if nrecords>0:
37 if (nskip+nrecords)>len(records):
38 nrecords=-1
39 else:
40 nrecords=nskip+nrecords
41
42 records_to_process = []
43 if nrecords==-1:
44 records_to_process = records[nskip:]
45 else:
46 records_to_process = records[nskip:nrecords]
47 print(f'Will process {len(records_to_process)} of {len(records)} records.')
48
49
50 ch_map = None
51 if channel_map is not None:
52 ch_map = detchannelmaps.make_tpc_map(channel_map)
53 offline_ch_num_dict = {}
54 offline_ch_plane_dict = {}
55
56 for r in records_to_process:
57
58 print(f'Processing (Record Number,Sequence Number)=({r[0],r[1]})')
59 wib_geo_ids = h5_file.get_geo_ids_for_subdetector(r,detdataformats.DetID.string_to_subdetector(det))
60
61 for gid in wib_geo_ids:
62
63 det_link = 0xffff & (gid >> 48);
64 det_slot = 0xffff & (gid >> 32);
65 det_crate = 0xffff & (gid >> 16);
66 det_id = 0xffff & gid;
67 subdet = detdataformats.DetID.Subdetector(det_id)
68 det_name = detdataformats.DetID.subdetector_to_string(subdet)
69
70 if not quiet:
71 print(f'\tProcessing subdetector {det_name}, crate {det_crate}, slot {det_slot}, link {det_link}')
72
73 frag = h5_file.get_frag(r,gid)
74 frag_hdr = frag.get_header()
75 frag_type = frag.get_fragment_type()
76 frag_ts = frag.get_trigger_timestamp()
77 if(frag_type!=daqdataformats.FragmentType.kWIB):
78 print('\tNot WIB fragment type {frag_type}. Continue.')
79 continue
80
81 print(f'\tTrigger timestamp for fragment is {frag_ts}')
82
83 n_frames = get_n_frames(frag);
84 print(f'\tFound {n_frames} WIB2 Frames.')
85
86 wf = fddetdataformats.WIB2Frame(frag.get_data())
87
88
89 if print_headers:
90 print('\n\t==== WIB HEADER (First Frame) ====')
91 print_header(wf,prefix='\t\t')
92
93
94 if(offline_ch_num_dict.get(gid) is None):
95 if channel_map is None:
96 offline_ch_num_dict[gid] = np.arange(256)
97 offline_ch_plane_dict[gid] = np.full(256,9999)
98 else:
99 wh = wf.get_header()
100 offline_ch_num_dict[gid] = np.array([ch_map.get_offline_channel_from_crate_slot_fiber_chan(wh.crate, wh.slot, wh.link, c) for c in range(256)])
101 offline_ch_plane_dict[gid] = np.array([ ch_map.get_plane_from_offline_channel(uc) for uc in offline_ch_num_dict[gid] ])
102
103
104
105 if check_timestamps:
106 timestamps = np_array_timestamp(frag)
107 timestamps_diff = np.diff(timestamps)
108 timestamps_diff_vals, timestamps_diff_counts = np.unique(timestamps_diff, return_counts=True)
109
110 if(n_frames>0):
111 print('\n\t==== TIMESTAMP CHECK ====')
112 print(f'\t\tTimestamps (First, Last, Min, Max): ({timestamps[0]},{timestamps[-1]},{np.min(timestamps)},{np.max(timestamps)})')
113 print(f'\t\tTimestamp diffs: {timestamps_diff_vals}')
114 print(f'\t\tTimestamp diff counts: {timestamps_diff_counts}')
115 print(f'\t\tAverage diff: {np.mean(timestamps_diff)}')
116
117 if print_adc_stats:
118
119
120 adcs = np_array_adc(frag)
121 adcs_rms = np.std(adcs,axis=0)
122 adcs_ped = np.mean(adcs,axis=0)
123
124 print('\n\t====WIB DATA====')
125
126 for ch,rms in enumerate(adcs_rms):
127 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}')
128
129 print("\n")
130
131
132 if check_timestamps:
133 timestamps_frame0 = np.array([ fddetdataformats.WIB2Frame(h5_file.get_frag(r,gid).get_data()).get_timestamp() for gid in wib_geo_ids ])
134 timestamps_frame0_diff = timestamps_frame0 - timestamps_frame0[0]
135
136 print('\n\t==== TIMESTAMP ACROSS WIBS CHECK ====')
137 print(f'\t\tTimestamp diff relative to first WIB',timestamps_frame0_diff)
138
139
140
141 print(f'Processed all requested records')
142
int main(int argc, char **argv)