DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dataclasses.py
Go to the documentation of this file.
1from dataclasses import dataclass, field
2import typing
3from datetime import datetime
4import pytz
5import numpy as np
6
7import daqdataformats
8import detdataformats
9import fddetdataformats
10import trgdataformats
11import detchannelmaps
12
14 return dts*16 //1e9
15
16def dts_to_datetime(dts_timestamp):
17 return datetime.fromtimestamp(dts_to_seconds(dts_timestamp), tz=pytz.timezone("UTC"))
18
19
20
22 # Find indices where the value changes compared to the previous value, adjusting the first index to start from 0
23 change_locations = np.insert(np.where(arr[1:] != arr[:-1])[0], 0, -1) + 1
24 # Check if the array is not empty
25 if len(arr) > 0:
26 # Return locations of changes, values at these locations, and the length of the array
27 return change_locations, arr[change_locations], len(arr)
28 else:
29 # Return empty results for an empty array
30 return [], [], 0
31
32def desparsify_array_diff_locs_and_vals(change_locations, change_values, arr_size):
33 # Create an empty array of the original size
34 reconstructed_arr = np.empty(arr_size, dtype=np.uint)
35 # Loop through each change location
36 for i in range(len(change_locations)):
37 # Apply the change value from the current change location to the end or next change location
38 if (i + 1) == len(change_locations):
39 reconstructed_arr[change_locations[i]:] = change_values[i]
40 else:
41 reconstructed_arr[change_locations[i]:change_locations[i + 1]] = change_values[i]
42 return reconstructed_arr
43
45 # Store the first value of the array for later reconstruction
46 arr_first = arr[0]
47 # Compute the difference of consecutive elements
48 arr_diff = np.diff(arr)
49 # Use sparsify function to find locations and values of changes in the diff array
50 arr_diff_locs, arr_diff_vals, _ = sparsify_array_diff_locs_and_vals(arr_diff)
51 # Return the first value, change locations, change values, and array size for reconstruction
52 return arr_first, arr_diff_locs, arr_diff_vals, len(arr)
53
54def desparsify_array_diff_of_diff_locs_and_vals(arr_first, change_locations, change_values, arr_size):
55 # Reconstruct the differential array from sparse representation
56 arr_diff = desparsify_array_diff_locs_and_vals(change_locations, change_values, arr_size - 1)
57 # Reconstruct the original array by cumulatively summing the differences and adding the first value
58 arr = np.concatenate((np.array([0], dtype=np.uint), arr_diff)).cumsum() + arr_first
59 return arr
60
61@dataclass(order=True)
63 run: int
64 trigger: int
65 sequence: int
66
67 @classmethod
68 def index_names(cls):
69 return [ "run","trigger","sequence" ]
70
71 def index_values(self):
72 return [ self.run, self.trigger, self.sequence ]
73
74@dataclass(order=True)
76 src_id: int
77 subsystem: int
78 subsystem_str: str
79 version: int
80
81@dataclass(order=True)
83 src_id: int
84
85 @classmethod
86 def index_names(cls):
87 return [ "run","trigger","sequence","src_id" ]
88
89 def index_values(self):
90 return [ self.runrun, self.triggertrigger, self.sequence, self.src_id ]
91
92
93@dataclass(order=True)
95
96 trigger_timestamp_dts: int
97 n_fragments: int
98 n_requested_components: int
99 error_bits: int
100 trigger_type: int
101 max_sequence_number: int
102 total_size_bytes: int
103 trigger_time : datetime = field(init=False)
104 trigger_type_bits: list[int] = field(init=False)
105
106 def __post_init__(self):
107 self.trigger_time = dts_to_datetime(self.trigger_timestamp_dts)
108 self.trigger_type_bits = [ trgdataformats.TriggerCandidateData.Type(i) for i in range(64) if (self.trigger_type & (1<<i))!=0 ]
109
110
111@dataclass(order=True)
113
114 trigger_timestamp_dts: int
115 window_begin_dts: int
116 window_end_dts: int
117 det_id: int
118 error_bits: int
119 fragment_type: int
120 total_size_bytes: int
121 data_size_bytes: int
122 trigger_time : datetime = field(init=False)
123 window_begin_time : datetime = field(init=False)
124 window_end_time : datetime = field(init=False)
125
126 def __post_init__(self):
127 self.trigger_time = dts_to_datetime(self.trigger_timestamp_dts)
128 self.window_begin_time = dts_to_datetime(self.window_begin_dts)
129 self.window_end_time = dts_to_datetime(self.window_end_dts)
130
131@dataclass(order=True)
133
134 n_obj: int
135 version: int
136
137@dataclass(order=True)
139
140 time_start: int
141 samples_to_peak: int
142 samples_over_threshold: int
143 channel: int
144 plane: int
145 element: int
146 adc_integral: int
147 adc_peak: int
148 detid: int
149 flag: int
150 id_ta: int
151
152@dataclass(order=True)
154
155 time_start: int
156 time_end: int
157 time_peak: int
158 time_activity: int
159 channel_start: int
160 channel_end: int
161 channel_peak: int
162 plane: int
163 element: int
164 adc_integral: int
165 adc_peak: int
166 detid: int
167 ta_type: int
168 algorithm: int
169 n_tps: int
170 id: int
171 id_tc: int
172
173@dataclass(order=True)
175
176 time_start: int
177 time_end: int
178 time_candidate: int
179 detid: int
180 tc_type: int
181 algorithm: int
182 n_tas: int
183 id: int
184
185@dataclass(order=True)
187
188 n_obj: int
189 daq_header_version: int
190 det_data_version: int
191 det_id: int
192 crate_id: int
193 slot_id: int
194 stream_id: int
195 timestamp_first_dts: int
196 timestamp_first_time: datetime = field(init=False)
197
198 def __post_init__(self):
199 self.timestamp_first_time = dts_to_datetime(self.timestamp_first_dts)
200
201@dataclass(order=True)
203
204 #first frame only
205 femb_id: int
206 colddata_id: int
207 version: int
208
209 #_idx arrays contain indices where value has changed from previous
210 #_vals arrays contain the values at those indices
211 pulser_vals: np.ndarray
212 pulser_idx: np.ndarray
213 calibration_vals: np.ndarray
214 calibration_idx: np.ndarray
215 ready_vals: np.ndarray
216 ready_idx: np.ndarray
217 context_vals: np.ndarray
218 context_idx: np.ndarray
219
220 wib_sync_vals: np.ndarray
221 wib_sync_idx: np.ndarray
222 femb_sync_vals: np.ndarray
223 femb_sync_idx: np.ndarray
224
225 cd_vals: np.ndarray
226 cd_idx: np.ndarray
227 crc_err_vals: np.ndarray
228 crc_err_idx: np.ndarray
229 link_valid_vals: np.ndarray
230 link_valid_idx: np.ndarray
231 lol_vals: np.ndarray
232 lol_idx: np.ndarray
233
234 #these take differences between successive values,
235 #and then, as above, look for differences in those differences
236 #store first value so the full array can be reconstructed
237 colddata_timestamp_0_diff_vals: np.ndarray
238 colddata_timestamp_0_diff_idx: np.ndarray
239 colddata_timestamp_0_first: int
240
241 colddata_timestamp_1_diff_vals: np.ndarray
242 colddata_timestamp_1_diff_idx: np.ndarray
243 colddata_timestamp_1_first: int
244
245 timestamp_dts_diff_vals: np.ndarray
246 timestamp_dts_diff_idx: np.ndarray
247 timestamp_dts_first: int
248
249 n_frames: int
250 n_channels: int
251 sampling_period: int
252
253@dataclass(order=True)
255
256 channel: int
257 plane: int
258 element: int
259 wib_chan: int
260
261 @classmethod
262 def index_names(cls):
263 return [ "run","trigger","sequence","src_id","channel" ]
264
265 def index_values(self):
266 return [ self.runrunrun, self.triggertriggertrigger, self.sequencesequence, self.src_id, self.channel ]
267
268@dataclass(order=True)
270
271 adc_mean: float
272 adc_rms: float
273 adc_max: int
274 adc_min: int
275 adc_median: float
276
277@dataclass(order=True)
279
280 timestamps: np.ndarray
281 adcs: np.ndarray
282 fft_mag: np.ndarray
283
284@dataclass(order=True)
286
287 #first frame only
288 channel_id: int
289 tde_header: int
290 version: int
291
292 #_idx arrays contain indices where value has changed from previous
293 #_vals arrays contain the values at those indices
294 errors_vals: np.ndarray
295 errors_idx: np.ndarray
296
297 #these take differences between successive values,
298 #and then, as above, look for differences in those differences
299 #store first value so the full array can be reconstructed
300 timestamp_dts_diff_vals: np.ndarray[int, np.float128]
301 timestamp_dts_diff_idx: np.ndarray
302 timestamp_dts_first: int
303
304 tai_time_diff_vals: np.ndarray[int, np.float128]
305 tai_time_diff_idx: np.ndarray
306 tai_time_first: int
307
308 n_frames: int
309 n_channels: int
310 sampling_period: int
311
312@dataclass(order=True)
314
315 channel: int
316 plane: int
317 element: int
318 tde_chan: int
319
320 @classmethod
321 def index_names(cls):
322 return [ "run","trigger","sequence","src_id","channel" ]
323
324 def index_values(self):
325 return [ self.runrunrun, self.triggertriggertrigger, self.sequencesequence, self.src_id, self.channel ]
326
327@dataclass(order=True)
329
330 adc_mean: float
331 adc_rms: float
332 adc_max: int
333 adc_min: int
334 adc_median: float
335
336@dataclass(order=True)
338
339 timestamps: np.ndarray[int, np.float128]
340 adcs: np.ndarray
341 fft_mag: np.ndarray
342
343@dataclass(order=True)
345
346 n_channels: int
347 sampling_period: int
348 ts_diffs_vals: np.ndarray
349 ts_diffs_counts: np.ndarray
350
351@dataclass(order=True)
353
354 channel: int
355 daphne_chan: int
356
357 @classmethod
358 def index_names(cls):
359 return [ "run","trigger","sequence","src_id","channel" ]
360
361 def index_values(self):
362 return [ self.runrunrun, self.triggertriggertrigger, self.sequencesequence, self.src_id, self.channel ]
363
364
365@dataclass(order=True)
367
368 adc_mean: float
369 adc_rms: float
370 adc_max: int
371 adc_min: int
372 adc_median: float
373
374@dataclass(order=True)
376
377 timestamps: np.ndarray
378 adcs: np.ndarray
379 fft_mag: np.ndarray
380
381
382@dataclass(order=True)
384
385 timestamp_dts: int
386 trigger_sample_value: int
387 threshold: float
388 baseline: float
389 adc_mean: float
390 adc_rms: float
391 adc_max: int
392 adc_min: int
393 adc_median: float
394 timestamp_max_dts: int
395 timestamp_min_dts: int
396
397@dataclass(order=True)
399
400 timestamp_dts: int
401 timestamps: np.ndarray
402 adcs: np.ndarray
desparsify_array_diff_locs_and_vals(change_locations, change_values, arr_size)
desparsify_array_diff_of_diff_locs_and_vals(arr_first, change_locations, change_values, arr_size)
sparsify_array_diff_locs_and_vals(arr)
Sparsification and desparsifications for arrays.