149def extract_rous_and_planes(files: list[str], channel_map: 'detchannelmaps._daq_detchannelmaps_py.TPCChannelMap', planes_to_filter: set[int]) -> (list[TPStreamFile], ROUPlaneData):
150 """
151 This function goes over the provided TPStream files.
152 It extracts the readout units used to generate the data in the files.
153 It also extracts unfiltered planes for each readout unit.
154 """
155 logging.info("Extracting ROUs and planes from files")
156 all_tpstream_files = []
157 rou_plane_data = ROUPlaneData()
158
159 valid_subdetectors = {
160 int(detdataformats.DetID.Subdetector.kHD_TPC),
161 int(detdataformats.DetID.Subdetector.kVD_BottomTPC),
162 int(detdataformats.DetID.Subdetector.kVD_TopTPC),
163 int(detdataformats.DetID.Subdetector.kNDLAr_TPC)
164 }
165
166 for tpstream_file in files:
167 logging.debug("Processing file: %s", tpstream_file)
168 loaded_file = HDF5RawDataFile(tpstream_file)
169
170 if not loaded_file.is_timeslice_type:
171 logging.critical("File %s is not a TP Stream file!", tpstream_file)
172 sys.exit(1)
173
174 all_record_ids = loaded_file.get_all_record_ids()
175 if not all_record_ids:
176 logging.critical("File %s does not have valid records!", tpstream_file)
177 sys.exit(1)
178
179
180 first_record = True
181 for a_record in tqdm(all_record_ids, desc="Processing records"):
182
183
184 source_ids = loaded_file.get_source_ids_for_fragment_type(a_record, "Trigger_Primitive")
185 if len(source_ids) == 0:
186 logging.critical("File %s does not have valid SourceIDs!", tpstream_file)
187 sys.exit(1)
188 logging.debug("SIDs: %s", source_ids)
189
190 for i, sid in enumerate(source_ids):
191 frag = loaded_file.get_frag(a_record, sid)
192
193 if frag.get_data_size() < 1:
194 logging.critical("File %s has an empty fragment!", tpstream_file)
195 sys.exit(1)
196 tp = trgdataformats.TriggerPrimitive(frag.get_data(0))
197
198 if first_record == True:
199 all_tpstream_files.append(TPStreamFile(tpstream_file, tp.time_start, 0))
200 logging.debug("First time start: %s", tp.time_start)
201 first_record = False
202
203
204 subdet = tp.detid
205 if subdet not in valid_subdetectors:
206 logging.debug("Subdetector %s is not in the map of valid subdetectors!", detdataformats.DetID.subdetector_to_string( detdataformats.DetID.Subdetector( subdet ) ) )
207 continue
208
209 plane = channel_map.get_plane_from_offline_channel(tp.channel)
210 if plane not in planes_to_filter:
211 rou = channel_map.get_element_name_from_offline_channel(tp.channel)
212 rou_plane_data.add_value(rou, plane)
213 logging.debug("Extracted rou: %s for plane: %s", rou, plane)
214 else:
215 logging.debug("Plane %s filtered", plane)
216 cleanup()
217 del frag, tp
218 cleanup()
219 del loaded_file, a_record, source_ids
220
221
222 logging.info("Extracted ROUs and planes: %s", rou_plane_data.data)
223
224
225 if not rou_plane_data.data:
226 logging.critical("No valid data was extracted!")
227 sys.exit(1)
228
229 return all_tpstream_files, rou_plane_data
230