DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
tpcdecoder.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3from hdf5libs import HDF5RawDataFile
4import h5py
5
6import daqdataformats
7import detdataformats
8import detchannelmaps
9
12
13import click
14import time
15import numpy as np
16
17
18@click.command()
19@click.argument('filename', type=click.Path(exists=True))
20@click.option('--nrecords', '-n', default=-1, help='How many Trigger Records to process (default: all)')
21@click.option('--nskip', default=0, help='How many Trigger Records to skip (default: 0)')
22@click.option('--print-adc-stats', is_flag=True, help="Print ADC Pedestals/RMS")
23@click.option('--print-wvfm-samples', default=0, help='How many samples in each waveform to print.')
24@click.option('--det', multiple=True, default=['VD_TopTPC','VD_BottomTPC'], help='Subdetector string (default: VD_TopTPC,VD_BottomTPC)')
25@click.option('--channel-map', default=None, help="Channel map to load (default: None)")
26
27def main(filename, nrecords, nskip, print_adc_stats, print_wvfm_samples, det, channel_map):
28
29 #get the file
30 h5_file = HDF5RawDataFile(filename)
31
32 #get the run number and operational environment out of the file attributes
33 with h5py.File(h5_file.get_file_name(), 'r') as f:
34 run_number = f.attrs["run_number"]
35 op_env = f.attrs["operational_environment"]
36 print(f'Processing file {h5_file.get_file_name()}. run_number={run_number}, operational_environemnt={op_env}')
37
38 #fill in appropriate channel name if we can tell from operational_environment
39 openv_channel_map=None
40 if op_env=="np04hd":
41 openv_channel_map="PD2HDTPCChannelMap"
42 elif op_env=="np04hdcoldbox":
43 openv_channel_map="HDColdboxTPCChannelMap"
44 elif op_env=="iceberghd" or op_env=="iceberg" or op_env=="icebergvd":
45 openv_channel_map="ICEBERGChannelMap"
46 elif op_env=="np02vd":
47 openv_channel_map="PD2VDTPCChannelMap"
48 else:
49 print(f'Unknown operational_environment ({op_env}). Will use specified channel-map {channel_map}')
50 openv_channel_map=channel_map
51
52 if openv_channel_map!=channel_map and channel_map is not None:
53 print(f'Operational environment {op_env} suggests channel map {openv_channel_map}, not {channel_map}.')
54 print(f'Please correct (or do not specify channel map and use operational environment to select). Exiting...')
55 return
56
57 channel_map=openv_channel_map
58 print(f'Using channel_map={channel_map}')
59
60 #get list of records in the file
61 records = h5_file.get_all_record_ids()
62
63 #pick which records to process based on cmdline inputs
64 if nskip > len(records):
65 print(f'Requested records to skip {nskip} is greater than number of records {len(records)}. Exiting...')
66 return
67 if nrecords>0:
68 if (nskip+nrecords)>len(records):
69 nrecords=-1
70 else:
71 nrecords=nskip+nrecords
72 records_to_process = []
73 if nrecords==-1:
74 records_to_process = records[nskip:]
75 else:
76 records_to_process = records[nskip:nrecords]
77 print(f'Will process {len(records_to_process)} of {len(records)} records.')
78
79 #loop over the records
80 for r in records_to_process:
81
82 #get the record index and print it
83 record_index = RecordDataBase(run=run_number,trigger=r[0],sequence=r[1])
84 print(f'Processing ',record_index)
85
86 #get the trigger record header data and print it
87 trh = h5_file.get_trh(record_index.trigger,record_index.sequence)
88 n_frags = len(h5_file.get_fragment_dataset_paths(record_index.trigger,record_index.sequence))
89 trh_data = rawdatautils.unpack.utils.TriggerRecordHeaderUnpacker().get_trh_data(trh,n_frags)[0]
90 print(trh_data)
91
92 # Process each detector type in the list
93 for det_type in det:
94
95 #get all geo_ids for this type
96 geo_ids = h5_file.get_geo_ids_for_subdetector(r,detdataformats.DetID.string_to_subdetector(det_type))
97
98 #loop through geo_ids
99 for gid in geo_ids:
100
101 #print basic information from geo id
102 det_stream = 0xffff & (gid >> 48);
103 det_slot = 0xffff & (gid >> 32);
104 det_crate = 0xffff & (gid >> 16);
105 det_id = 0xffff & gid;
106 subdet = detdataformats.DetID.Subdetector(det_id)
107 det_name = detdataformats.DetID.subdetector_to_string(subdet)
108 print(f'\tProcessing subdetector {det_name}, '
109 f'crate {det_crate}, '
110 f'slot {det_slot}, '
111 f'stream {det_stream}')
112
113 #get the fragment
114 frag = h5_file.get_frag(r,gid)
115
117 frag_header = unpacker.get_frh_data(frag)[0]
118 print('\t',frag_header)
119
120 #get fragment type and pick the correct unpacker
121 frag_type = frag.get_fragment_type()
122 if frag_type==daqdataformats.FragmentType.kWIBEth:
123 unpacker = rawdatautils.unpack.utils.WIBEthUnpacker(channel_map=channel_map,ana_data_prescale=1,wvfm_data_prescale=1)
124 elif frag_type==daqdataformats.FragmentType.kTDEEth:
125 unpacker = rawdatautils.unpack.utils.TDEEthUnpacker(channel_map=channel_map,ana_data_prescale=1,wvfm_data_prescale=1)
126 else:
127 print(f'\tUnknown fragment type {frag_type}. Continuing...')
128 continue
129
130 n_frames = unpacker.get_n_obj(frag)
131 print(f'Found {n_frames} frames in this fragment.')
132 if n_frames==0:
133 continue
134
135 daq_header_data = unpacker.get_daq_header_data(frag)
136 det_header_data = unpacker.get_det_header_data(frag)
137
138 for i_deth, deth in enumerate(det_header_data):
139 print(f'\t',daq_header_data[i_deth])
140 print(f'\t',deth)
141
142 det_ana_data, det_wvfm_data = unpacker.get_det_data_all(frag)
143
144 if print_adc_stats:
145 for det_ana in det_ana_data:
146# print(f'\t\tChannel {det_ana.channel} adc stats:')
147 print('\t\t',det_ana)
148
149 if print_wvfm_samples:
150 for det_wvfm in det_wvfm_data:
151 print(f'\t\tChannel {det_wvfm.channel} waveform:')
152 for i_sample in range(print_wvfm_samples):
153 ts_str = np.format_float_positional(det_wvfm.timestamps[i_sample])
154 print(f'\t\t\t {i_sample:>5}: ts={ts_str:<25} val={det_wvfm.adcs[i_sample]}')
155
156
157 #end record loop
158
159 print(f'Processed all requested records')
160
161if __name__ == '__main__':
162 main()
main(filename, nrecords, nskip, print_adc_stats, print_wvfm_samples, det, channel_map)
Definition tpcdecoder.py:27