gaps-online-software 0.10
online software for the TOF system for the GAPS experiment
Loading...
Searching...
No Matches
events.h
1#ifndef TOFEVENTS_H_INCLUDED
2#define TOFEVENTS_H_INCLUDED
3
19#include <tuple>
20#include <array>
21#include <format>
22
23#include "result/result.h"
24
25#include "tof_typedefs.h"
26#include "packets/monitoring.h"
27#include "packets/tof_packet.h"
28#include "events/tof_event_header.hpp"
29#include "calibration.h"
30#include "version.h"
31#include "errors.hpp"
32#ifdef BUILD_CXXDB
33#include "database.h"
34#endif
35
36namespace r = result;
37namespace g = Gaps;
38
39class RBCalibration;
40
41#define NCHN 9
42#define NWORDS 1024
43#define N_LTBS 25
44#define N_CHN_PER_LTB 16
45
46
47/*********************************************************/
48
50static const f32 C_LIGHT_PADDLE = 15.4;
51
52/*********************************************************/
53
54static const u8 EVENTSTATUS_UNKNOWN = 0;
55static const u8 EVENTSTATUS_CRC32WRONG = 10;
56static const u8 EVENTSTATUS_TAILWRONG = 11;
57static const u8 EVENTSTATUS_CHIDWRONG = 12;
58static const u8 EVENTSTATUS_CELLSYNCERR = 13;
59static const u8 EVENTSTATUS_CHNSYNCERR = 14;
60static const u8 EVENTSTATUS_CELLANDCHNSYNCERR = 15;
61static const u8 EVENTSTATUS_ANYDATAMANGLING = 16;
62static const u8 EVENTSTATUS_INCOMPLETEREADOUT = 21;
63static const u8 EVENTSTATUS_INCOMPATIBLEDATA = 22;
64static const u8 EVENTSTATUS_EVENTTIMEOUT = 23;
65static const u8 EVENTSTATUS_GOODNOCRCORERRBITCHECK = 39;
66static const u8 EVENTSTATUS_GOODNOCRCCHECK = 40;
67static const u8 EVENTSTATUS_GOODNOERRBITCHECK = 41;
68static const u8 EVENTSTATUS_PERFECT = 42;
69
76enum class EventStatus : u8 {
77 Unknown = EVENTSTATUS_UNKNOWN,
78 Crc32Wrong = EVENTSTATUS_CRC32WRONG,
79 TailWrong = EVENTSTATUS_TAILWRONG,
80 ChannelIDWrong = EVENTSTATUS_CHIDWRONG,
81 CellSyncErrors = EVENTSTATUS_CELLSYNCERR,
82 ChnSyncErrors = EVENTSTATUS_CHNSYNCERR,
83 CellAndChnSyncErrors = EVENTSTATUS_CELLANDCHNSYNCERR,
84 AnyDataMangling = EVENTSTATUS_ANYDATAMANGLING,
85 IncompatibleData = EVENTSTATUS_INCOMPATIBLEDATA,
86 EventTimeOut = EVENTSTATUS_EVENTTIMEOUT,
87 GoodNoCRCOrErrBitCheck = EVENTSTATUS_GOODNOCRCORERRBITCHECK,
90 GoodNoCRCCheck = EVENTSTATUS_GOODNOCRCCHECK,
93 GoodNoErrBitCheck = EVENTSTATUS_GOODNOERRBITCHECK,
94 IncompleteReadout = EVENTSTATUS_INCOMPLETEREADOUT,
95 Perfect = EVENTSTATUS_PERFECT,
96
97};
98
99std::ostream& operator<<(std::ostream& os, const EventStatus& status);
100
101template <>
102struct std::formatter<EventStatus> : std::formatter<std::string> {
103 // Parse format specifiers (default implementation)
104 constexpr auto parse(std::format_parse_context& ctx) {
105 return ctx.begin();
106 }
107
108 auto format(const EventStatus& status, auto& ctx) {
109 std::ostringstream oss;
110 oss << status; // Use the << operator to convert enum to string
111 return std::format_to(ctx.out(), "{}", oss.str());
112 }
113};
114
115/*********************************************************/
116
117static const u8 TRIGGERTYPE_UNKNOWN = 0;
118static const u8 TRIGGERTYPE_ANY = 1;
119static const u8 TRIGGERTYPE_TRACK = 2;
120static const u8 TRIGGERTYPE_TRACKCENTRAL = 3;
121static const u8 TRIGGERTYPE_GAPS = 4;
122static const u8 TRIGGERTYPE_POISSON = 100;
123static const u8 TRIGGERTYPE_FORCED = 101;
124
125
126/************************************
127 *
128 * GAPS Trigger types/sources. Description
129 * can be found elsewhere. More than oen
130 * of them can be active at the same time
131 *
132 */
133enum class TriggerType : u8 {
134 Unknown = TRIGGERTYPE_UNKNOWN,
136 Gaps = TRIGGERTYPE_GAPS,
137 Any = TRIGGERTYPE_ANY,
138 Track = TRIGGERTYPE_TRACK,
139 TrackCentral = TRIGGERTYPE_TRACKCENTRAL,
141 Poisson = TRIGGERTYPE_POISSON,
142 Forced = TRIGGERTYPE_FORCED,
143};
144
145std::ostream& operator<<(std::ostream& os, const TriggerType& t_type);
146
147/*********************************************************/
148
149static const u8 LTBTHRESHOLD_NOHIT = 0;
150static const u8 LTBTHRESHOLD_HIT = 1;
151static const u8 LTBTHRESHOLD_BETA = 2;
152static const u8 LTBTHRESHOLD_VETO = 3;
153static const u8 LTBTHRESHOLD_UNKNOWN = 255;
154
155enum class LTBThreshold : u8 {
156 NoHit = LTBTHRESHOLD_NOHIT,
158 Hit = LTBTHRESHOLD_HIT,
160 Beta = LTBTHRESHOLD_BETA,
162 Veto = LTBTHRESHOLD_VETO,
165 Unknown = LTBTHRESHOLD_UNKNOWN,
166};
167
168std::ostream& operator<<(std::ostream& os, const LTBThreshold& thresh);
169
170/*********************************************************/
171
180 static constexpr u16 HEAD = 0xAAAA;
181 static constexpr u16 TAIL = 0x5555;
182 static constexpr u16 SIZE = 30; // size in bytes with HEAD and TAIL
183
184 u8 rb_id = 0;
185 u32 event_id = 0;
186 u8 status_byte = 0;
187 u16 channel_mask = 0;
188 u16 stop_cell = 0;
189 u16 ch9_amp = 0;
190 u16 ch9_freq = 0;
191 u16 ch9_phase = 0;
192 u16 fpga_temp = 0;
193 u32 timestamp32 = 0;
194 u16 timestamp16 = 0;
195
197
198 static auto from_bytestream(const Vec<u8> &bytestream, u64 &pos)
199 -> r::Result<RBEventHeader, g::IOError>;
200
201 auto get_channels() const -> Vec<u8>;
202 auto get_nchan() const -> u8;
203 auto get_active_data_channels() const -> Vec<u8>;
204 auto has_ch9() const -> bool;
205 auto get_n_datachan() const -> u8;
206 auto get_fpga_temp() const -> f32;
207 auto is_event_fragment() const -> bool;
208 auto drs_lost_trigger() const -> bool;
209 auto lost_lock() const -> bool;
210 auto lost_lock_last_sec() const -> bool;
211 auto is_locked() const -> bool;
212 auto is_locked_last_sec() const -> bool;
213 auto get_sine_fit() const -> std::array<f32,3>;
215 auto get_timestamp48() const -> u64;
217 auto to_string() const -> std::string;
218};
219
220/***********************************************
221 * Reconstructed waveform peak information
222 *
223 * There should be one TofHit per reconstructed
224 * peak
225 *
226 *
227 */
228struct TofHit {
229 static constexpr u16 HEAD = 0xF0F0;
230 static constexpr u16 TAIL = 0xF0F;
231
232 u8 paddle_id;
233 // deprecated
234 bool broken;
235
236 // new variables for V1
237 Gaps::ProtocolVersion version;
238 f32 baseline_a;
239 f32 baseline_a_rms;
240 f32 baseline_b;
241 f32 baseline_b_rms;
242 f32 phase;
243
244 // event wide calculated time
245 f32 event_t0 = 0;
246
247 u32 timestamp32;
248 u16 timestamp16;
249 // don't serialize
250 f32 paddle_len = 0;
251 f32 coax_cbl_time = 0;
252 f32 hart_cbl_time = 0;
253
254 u8 ctr_etx;
255 u16 tail = 0xF0F;
256
257 auto get_time_a() const -> f32;
258 auto get_time_b() const -> f32;
259 auto get_peak_a() const -> f32;
260 auto get_peak_b() const -> f32;
261 auto get_charge_a() const -> f32;
262 auto get_charge_b() const -> f32;
263 auto get_charge_min_i() const -> f32;
264 auto get_x_pos() const -> f32;
265 auto get_t_avg() const -> f32;
268 auto get_t0_relative() const -> f32;
269 auto get_timestamp48() const -> f64;
270
273 void set_paddle_len(f32 paddle_len);
274
275 #if BUILD_CXXDB
276 auto set_paddle(const Gaps::TofPaddle& paddle) -> void;
277 auto get_phase_delay() const -> f32;
278 auto get_cable_delay() const -> f32;
279 auto get_t0() const -> f32;
280 auto get_edep() const -> f32;
281 #endif
282
283 static auto from_bytestream(const Vec<u8> &bytestream, u64 &pos)
284 -> TofHit;
285
286 // easier print out
287 auto to_string() const -> std::string;
288
289 private:
290 // we keep this private, since
291 // the user should use the getters
292 // to get the values converted
293 // back to f32
294 // deprecated, but kept for compatibility
295 u16 time_a;
296 u16 time_b;
297 u16 peak_a;
298 u16 peak_b;
299 u16 charge_a;
300 u16 charge_b;
301 u16 charge_min_i;
302 u16 x_pos;
303 u16 t_average;
304
305 f32 time_a_f32 = 0;
306 f32 time_b_f32 = 0;
307 f32 peak_a_f32 = 0;
308 f32 peak_b_f32 = 0;
309 f32 charge_a_f32 = 0;
310 f32 charge_b_f32 = 0;
311};
312
321struct RBEvent {
322 static constexpr u16 HEAD = 0xAAAA;
323 static constexpr u16 TAIL = 0x5555;
324
325 // data type will be an enum
326 u8 data_type = 0;
327 EventStatus status = EventStatus::Unknown;
328 RBEventHeader header = RBEventHeader();
329 Vec<Vec<u16>> adc = Vec<Vec<u16>>();
330 Vec<TofHit> hits = Vec<TofHit>();
331
332 RBEvent();
333
334 auto get_channel_by_label(u8 channel) const -> const Vec<u16>&;
335 auto get_channel_by_id(u8 channel) const -> const Vec<u16>&;
336
337 auto get_channel_adc(u8 channel) const -> const Vec<u16>&;
338
340 static auto calc_baseline(const Vec<f32> &volts, usize min_bin, usize max_bin) -> f32;
341
342 static auto from_bytestream(const Vec<u8> &bytestream, u64 &pos)
343 -> RBEvent;
344
345 auto to_string() const -> std::string;
346
347 private:
348
353 auto channel_check(u8 channel) const -> bool;
354 Vec<u16> _empty_channel = Vec<u16>();
355};
356
364 [[deprecated("Unused/not useful deemed feature")]]
365
366 static constexpr u16 HEAD = 0xAAAA;
367 static constexpr u16 TAIL = 0x5555;
368 static constexpr usize SIZE = 15; // bytes
369
370 u32 event_id ;
371 u8 ltb_hit_index;
372 u8 ltb_id ;
373 u8 ltb_dsi ;
374 u8 ltb_j ;
375 u8 ltb_ch ;
376 u8 rb_id ;
377 u8 rb_ch ;
378
379 static RBMissingHit from_bytestream(const Vec<u8> &bytestream,
380 u64 &pos);
381};
382
383/*********************************************************/
384
385static const u8 EVENT_QUALITY_UNKNOWN = 0;
386static const u8 EVENT_QUALITY_SILVER = 10;
387static const u8 EVENT_QUALITY_GOLD = 20;
388static const u8 EVENT_QUALITY_DIAMOND = 30;
389static const u8 EVENT_QUALITY_FOURLEAFCLOVER = 40;
390
391
398enum class EventQuality : u8 {
399 Unknown = EVENT_QUALITY_UNKNOWN,
400 Silver = EVENT_QUALITY_SILVER,
401 Gold = EVENT_QUALITY_GOLD,
402 Diamond = EVENT_QUALITY_DIAMOND,
407 FourLeafClover = EVENT_QUALITY_FOURLEAFCLOVER
408};
409
410std::ostream& operator<<(std::ostream& os, const EventQuality& qual);
411
412/*********************************************************/
413
414static const u8 COMPRESSION_LEVEL_UNKNOWN = 0;
415static const u8 COMPRESSION_LEVEL_NONE = 10;
416
417enum class CompressionLevel : u8 {
418 Unknown = COMPRESSION_LEVEL_UNKNOWN,
419 None = COMPRESSION_LEVEL_NONE,
420};
421
422std::ostream& operator<<(std::ostream& os, const CompressionLevel& level);
423
424
445 static constexpr u16 HEAD = 0xAAAA;
447 static constexpr u16 TAIL = 0x5555;
449 static constexpr usize SIZE = 0; // size in bytes
451 EventStatus event_status = EventStatus::Unknown;
453 u32 event_id = 0;
455 u32 timestamp = 0;
457 u32 tiu_timestamp = 0;
459 u32 tiu_gps32 = 0;
461 u32 tiu_gps16 = 0;
463 u32 crc = 0;
464 u16 trigger_source = 0;
465 u32 dsi_j_mask = 0;
466 Vec<u16> channel_mask = Vec<u16>();
467 u64 mtb_link_mask = 0;
468
470
473 [[deprecated("The format of the gps timestamp changed and it is only 32 bits as of now")]]
474 auto get_timestamp_gps48() const -> u64;
476 auto get_timestamp_gps() const -> u32;
479 auto get_timestamp_abs48() const -> u64;
480 auto get_rb_link_ids() const -> Vec<u8>;
493 auto get_trigger_hits() const
494 -> Vec<std::tuple<u8, u8, u8, LTBThreshold>>;
495
497 auto get_trigger_sources() const -> Vec<TriggerType>;
509 static auto from_bytestream(const Vec<u8> &bytestream, u64 &pos)
512 auto to_string() const -> std::string;
513};
514
528struct TofEvent {
529 static constexpr u16 HEAD = 0xAAAA;
530 static constexpr u16 TAIL = 0x5555;
531
532 EventStatus status;
533 TofEventHeader header;
534 MasterTriggerEvent mt_event;
535
538 Vec<RBEvent> rb_events;
542 Vec<RBMissingHit> missing_hits;
543
544 TofEvent();
545
557 static auto from_bytestream(const Vec<u8> &bytestream, u64 &pos)
558 -> r::Result<TofEvent, g::IOError>;
559
571 static auto from_tofpacket(const TofPacket &packet) -> TofEvent;
572
573 #ifdef BUILD_CXXDB
576 auto set_paddlemap(const Gaps::TofPaddleMap&) -> void;
577 #endif
578
579 [[deprecated("RBMissingHit is deprecated")]]
580 static auto get_n_rbmissinghits(u32 mask) -> u32;
581 static auto get_n_rbevents(u32 mask) -> u32;
583 auto get_hits() const -> Vec<TofHit>;
586 auto normalize_hit_times() -> void;
587
589 auto to_string() const -> std::string;
590
594 auto get_rbevent(u8 board_id) const -> const RBEvent&;
595
599 auto get_rbids() const -> Vec<u8>;
600
601 private:
606 auto passed_consistency_check() -> bool;
607
610 RBEvent _empty_event = RBEvent();
611};
612
613
614/************************
615 * A part of a TofEvent
616 * - a single waveform
617 *
618 * That is a waveform for
619 * a specific channel for a
620 * specific id.
621 *
622 * Each paddle has 2 waveforms
623 *
624 *
625 */
627 static constexpr u16 HEAD = 0xAAAA;
628 static constexpr u16 TAIL = 0x5555;
629
630 u32 event_id ;
631 u8 rb_id ;
632 u8 rb_channel;
633 u16 stop_cell ;
634 Vec<u16> adc ;
635
636 static auto from_bytestream(const Vec<u8> &bytestream, u64 &pos) -> RBWaveform;
637 auto to_string() const -> std::string;
638};
639
640
648 static constexpr u16 HEAD = 0xAAAA;
649 static constexpr u16 TAIL = 0x5555;
650
651 Gaps::ProtocolVersion version ;
652 EventStatus status ;
653 u8 quality ;
654 u16 trigger_sources ;
659 u32 event_id ;
660 u16 run_id ;
661 u32 timestamp32 ;
662 u16 timestamp16 ;
663 // deprecated, won't get serialized
664 u16 primary_beta ;
665 u16 primary_charge ;
666 u16 drs_dead_lost_hits;
667 u32 dsi_j_mask ;
668 Vec<u16> channel_mask ;
669 u64 mtb_link_mask ;
670 Vec<TofHit> hits ;
671
672 // flight computer event variable packet
673 u8 n_hits_umb ;
674 u8 n_hits_cbe ;
675 u8 n_hits_cor ;
676 f32 tot_edep_umb ;
677 f32 tot_edep_cbe ;
678 f32 tot_edep_cor ;
679
680 static auto from_tofpacket(const TofPacket &packet)
681 -> r::Result<TofEventSummary, g::IOError>;
682 static auto from_bytestream(const Vec<u8> &stream, u64 &pos) -> r::Result<TofEventSummary, g::IOError> ;
683
684 #ifdef BUILD_CXXDB
687 auto set_paddlemap(const Gaps::TofPaddleMap&) -> void;
690 auto normalize_hit_times() -> void;
691 #endif
692
693 // combined timestamp
694 auto get_timestamp48() const -> u64;
695
696 auto get_rb_link_ids() const -> Vec<u8>;
697
710 auto get_trigger_hits() const -> Vec<std::tuple<u8, u8, u8, LTBThreshold>>;
712 auto get_trigger_sources() const -> Vec<TriggerType>;
713
714 auto to_string() const -> std::string;
715};
716
717std::ostream& operator<<(std::ostream& os, const TofHit& pad);
718
719std::ostream& operator<<(std::ostream& os, const MasterTriggerEvent& mt);
720
721std::ostream& operator<<(std::ostream& os, const TofEvent& et);
722
723std::ostream& operator<<(std::ostream& os, const RBEvent& re);
724
725std::ostream& operator<<(std::ostream& os, const RBEventHeader& rh);
726
727std::ostream& operator<<(std::ostream& os, const RBWaveform& rh);
728
729std::ostream& operator<<(std::ostream& os, const TofEventSummary& tes);
730
731#endif
Definition calibration.h:105
std::map< u8, TofPaddle > TofPaddleMap
A map of paddle id -> TofPaddle.
Definition database.h:62
Definition database.h:18
Definition events.h:443
auto get_timestamp_gps48() const -> u64
Definition calibration.h:34
Definition events.h:179
auto to_string() const -> std::string
string representation for printing
auto get_timestamp48() const -> u64
the combined timestamp
Definition events.h:321
static auto calc_baseline(const Vec< f32 > &volts, usize min_bin, usize max_bin) -> f32
Get the baseline for a single channel.
Definition events.h:363
Definition events.h:626
Definition tof_event_header.hpp:12
Definition events.h:647
u8 n_trigger_paddles
Definition events.h:658
Definition events.h:528
static auto from_bytestream(const Vec< u8 > &bytestream, u64 &pos) -> r::Result< TofEvent, g::IOError >
auto get_hits() const -> Vec< TofHit >
Get all hits from all rb_events.
Vec< RBEvent > rb_events
Definition events.h:538
Vec< RBMissingHit > missing_hits
Definition events.h:542
static auto from_tofpacket(const TofPacket &packet) -> TofEvent
Definition events.h:228
auto get_t0_relative() const -> f32
Definition tof_packet.h:83