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 "tof_typedefs.h"
24#include "packets/monitoring.h"
25#include "packets/tof_packet.h"
26#include "events/tof_event_header.hpp"
27#include "calibration.h"
28#include "version.h"
29
30class RBCalibration;
31
32#define NCHN 9
33#define NWORDS 1024
34#define N_LTBS 25
35#define N_CHN_PER_LTB 16
36
37struct RBEventHeader;
38struct RBEvent;
40struct TofHit;
41
42/*********************************************************/
43
45static const f32 C_LIGHT_PADDLE = 15.4;
46
47/*********************************************************/
48
49static const u8 EVENTSTATUS_UNKNOWN = 0;
50static const u8 EVENTSTATUS_CRC32WRONG = 10;
51static const u8 EVENTSTATUS_TAILWRONG = 11;
52static const u8 EVENTSTATUS_CHIDWRONG = 12;
53static const u8 EVENTSTATUS_CELLSYNCERR = 13;
54static const u8 EVENTSTATUS_CHNSYNCERR = 14;
55static const u8 EVENTSTATUS_CELLANDCHNSYNCERR = 15;
56static const u8 EVENTSTATUS_ANYDATAMANGLING = 16;
57static const u8 EVENTSTATUS_INCOMPLETEREADOUT = 21;
58static const u8 EVENTSTATUS_INCOMPATIBLEDATA = 22;
59static const u8 EVENTSTATUS_EVENTTIMEOUT = 23;
60static const u8 EVENTSTATUS_GOODNOCRCORERRBITCHECK = 39;
61static const u8 EVENTSTATUS_GOODNOCRCCHECK = 40;
62static const u8 EVENTSTATUS_GOODNOERRBITCHECK = 41;
63static const u8 EVENTSTATUS_PERFECT = 42;
64
71enum class EventStatus : u8 {
72 Unknown = EVENTSTATUS_UNKNOWN,
73 Crc32Wrong = EVENTSTATUS_CRC32WRONG,
74 TailWrong = EVENTSTATUS_TAILWRONG,
75 ChannelIDWrong = EVENTSTATUS_CHIDWRONG,
76 CellSyncErrors = EVENTSTATUS_CELLSYNCERR,
77 ChnSyncErrors = EVENTSTATUS_CHNSYNCERR,
78 CellAndChnSyncErrors = EVENTSTATUS_CELLANDCHNSYNCERR,
79 AnyDataMangling = EVENTSTATUS_ANYDATAMANGLING,
80 IncompatibleData = EVENTSTATUS_INCOMPATIBLEDATA,
81 EventTimeOut = EVENTSTATUS_EVENTTIMEOUT,
82 GoodNoCRCOrErrBitCheck = EVENTSTATUS_GOODNOCRCORERRBITCHECK,
85 GoodNoCRCCheck = EVENTSTATUS_GOODNOCRCCHECK,
88 GoodNoErrBitCheck = EVENTSTATUS_GOODNOERRBITCHECK,
89 IncompleteReadout = EVENTSTATUS_INCOMPLETEREADOUT,
90 Perfect = EVENTSTATUS_PERFECT,
91
92};
93
94std::ostream& operator<<(std::ostream& os, const EventStatus& status);
95
96template <>
97struct std::formatter<EventStatus> : std::formatter<std::string> {
98 // Parse format specifiers (default implementation)
99 constexpr auto parse(std::format_parse_context& ctx) {
100 return ctx.begin();
101 }
102
103 auto format(const EventStatus& status, auto& ctx) {
104 std::ostringstream oss;
105 oss << status; // Use the << operator to convert enum to string
106 return std::format_to(ctx.out(), "{}", oss.str());
107 }
108};
109
110
111/*********************************************************/
112
113static const u8 TRIGGERTYPE_UNKNOWN = 0;
114static const u8 TRIGGERTYPE_GAPS = 4;
115static const u8 TRIGGERTYPE_ANY = 1;
116static const u8 TRIGGERTYPE_TRACK = 2;
117static const u8 TRIGGERTYPE_TRACKCENTRAL = 3;
118static const u8 TRIGGERTYPE_POISSON = 100;
119static const u8 TRIGGERTYPE_FORCED = 101;
120
121
122/************************************
123 *
124 * GAPS Trigger types/sources. Description
125 * can be found elsewhere. More than oen
126 * of them can be active at the same time
127 *
128 */
129enum class TriggerType : u8 {
130 Unknown = TRIGGERTYPE_UNKNOWN,
132 Gaps = TRIGGERTYPE_GAPS,
133 Any = TRIGGERTYPE_ANY,
134 Track = TRIGGERTYPE_TRACK,
135 TrackCentral = TRIGGERTYPE_TRACKCENTRAL,
137 Poisson = TRIGGERTYPE_POISSON,
138 Forced = TRIGGERTYPE_FORCED,
139};
140
141std::ostream& operator<<(std::ostream& os, const TriggerType& t_type);
142
143/*********************************************************/
144
145static const u8 LTBTHRESHOLD_NOHIT = 0;
146static const u8 LTBTHRESHOLD_HIT = 1;
147static const u8 LTBTHRESHOLD_BETA = 2;
148static const u8 LTBTHRESHOLD_VETO = 3;
149static const u8 LTBTHRESHOLD_UNKNOWN = 255;
150
151enum class LTBThreshold : u8 {
152 NoHit = LTBTHRESHOLD_NOHIT,
154 Hit = LTBTHRESHOLD_HIT,
156 Beta = LTBTHRESHOLD_BETA,
158 Veto = LTBTHRESHOLD_VETO,
161 Unknown = LTBTHRESHOLD_UNKNOWN,
162};
163
164std::ostream& operator<<(std::ostream& os, const LTBThreshold& thresh);
165
166/*********************************************************/
167
176 static const u16 HEAD = 0xAAAA;
177 static const u16 TAIL = 0x5555;
178 static const u16 SIZE = 30; // size in bytes with HEAD and TAIL
179
180 u8 rb_id ;
181 u32 event_id ;
182 u8 status_byte ;
183 u16 channel_mask ;
184 u16 stop_cell ;
185 u16 ch9_amp ;
186 u16 ch9_freq ;
187 u16 ch9_phase ;
188 u16 fpga_temp ;
189 u32 timestamp32 ;
190 u16 timestamp16 ;
191
193
194 static RBEventHeader from_bytestream(const Vec<u8> &bytestream,
195 u64 &pos);
196
197 Vec<u8> get_channels() const;
198 u8 get_nchan() const;
199 Vec<u8> get_active_data_channels() const;
200 bool has_ch9() const;
201 u8 get_n_datachan() const;
202 f32 get_fpga_temp() const;
203 bool is_event_fragment() const;
204 bool drs_lost_trigger() const;
205 bool lost_lock() const;
206 bool lost_lock_last_sec() const;
207 bool is_locked() const;
208 bool is_locked_last_sec() const;
209
210 std::array<f32, 3> get_sine_fit() const;
211
213 u64 get_timestamp48() const;
214
216 std::string to_string() const;
217};
218
227struct RBEvent {
228 static const u16 HEAD = 0xAAAA;
229 static const u16 TAIL = 0x5555;
230
231 // data type will be an enum
232 u8 data_type;
233 EventStatus status;
234 RBEventHeader header;
235 Vec<Vec<u16>> adc;
236 Vec<TofHit> hits;
237
238 RBEvent();
239
240 const Vec<u16>& get_channel_by_label(u8 channel) const;
241 const Vec<u16>& get_channel_by_id(u8 channel) const;
242
243 const Vec<u16>& get_channel_adc(u8 channel) const;
244
246 static f32 calc_baseline(const Vec<f32> &volts, usize min_bin, usize max_bin);
247
248 static RBEvent from_bytestream(const Vec<u8> &bytestream,
249 u64 &pos);
250
251 std::string to_string() const;
252
253 private:
254
259 bool channel_check(u8 channel) const;
260 Vec<u16> _empty_channel = Vec<u16>();
261};
262
270
271 static const u16 HEAD = 0xAAAA;
272 static const u16 TAIL = 0x5555;
273 static const usize SIZE = 15; // bytes
274
275 u32 event_id ;
276 u8 ltb_hit_index;
277 u8 ltb_id ;
278 u8 ltb_dsi ;
279 u8 ltb_j ;
280 u8 ltb_ch ;
281 u8 rb_id ;
282 u8 rb_ch ;
283
284 static RBMissingHit from_bytestream(const Vec<u8> &bytestream,
285 u64 &pos);
286};
287
288/*********************************************************/
289
290static const u8 EVENT_QUALITY_UNKNOWN = 0;
291static const u8 EVENT_QUALITY_SILVER = 10;
292static const u8 EVENT_QUALITY_GOLD = 20;
293static const u8 EVENT_QUALITY_DIAMOND = 30;
294static const u8 EVENT_QUALITY_FOURLEAFCLOVER = 40;
295
296
303enum class EventQuality : u8 {
304 Unknown = EVENT_QUALITY_UNKNOWN,
305 Silver = EVENT_QUALITY_SILVER,
306 Gold = EVENT_QUALITY_GOLD,
307 Diamond = EVENT_QUALITY_DIAMOND,
312 FourLeafClover = EVENT_QUALITY_FOURLEAFCLOVER
313};
314
315std::ostream& operator<<(std::ostream& os, const EventQuality& qual);
316
317/*********************************************************/
318
319static const u8 COMPRESSION_LEVEL_UNKNOWN = 0;
320static const u8 COMPRESSION_LEVEL_NONE = 10;
321
322enum class CompressionLevel : u8 {
323 Unknown = COMPRESSION_LEVEL_UNKNOWN,
324 None = COMPRESSION_LEVEL_NONE,
325};
326
327std::ostream& operator<<(std::ostream& os, const CompressionLevel& level);
328
329
350 static const u16 HEAD = 0xAAAA;
352 static const u16 TAIL = 0x5555;
354 static const usize SIZE = 45; // size in bytes
356 EventStatus event_status;
358 u32 event_id ;
368 u32 crc ;
369 u16 trigger_source ;
370 u32 dsi_j_mask ;
371 Vec<u16> channel_mask ;
372 u64 mtb_link_mask ;
373
375
379
382
383 Vec<u8> get_rb_link_ids() const;
384
397 Vec<std::tuple<u8, u8, u8, LTBThreshold>> get_trigger_hits() const;
398
399
401 Vec<TriggerType> get_trigger_sources() const;
413 static MasterTriggerEvent from_bytestream(const Vec<u8> &bytestream,
414 u64 &pos);
416 std::string to_string() const;
417
418};
419
420
434struct TofEvent {
435 static const u16 HEAD = 0xAAAA;
436 static const u16 TAIL = 0x5555;
437
438 EventStatus status;
439 TofEventHeader header;
440 MasterTriggerEvent mt_event;
441
442
445 Vec<RBEvent> rb_events;
449 Vec<RBMissingHit> missing_hits;
450
451 TofEvent();
452
464 static TofEvent from_bytestream(const Vec<u8> &bytestream,
465 u64 &pos);
466
478 static TofEvent from_tofpacket(const TofPacket &packet);
479
480 static u32 get_n_rbmissinghits(u32 mask);
481 static u32 get_n_rbevents(u32 mask);
482
484 std::string to_string() const;
485
489 const RBEvent& get_rbevent(u8 board_id) const;
490
494 Vec<u8> get_rbids() const;
495
496 private:
501 bool passed_consistency_check();
502
505 RBEvent _empty_event = RBEvent();
506};
507
508/***********************************************
509 * Reconstructed waveform peak information
510 *
511 * There should be one TofHit per reconstructed
512 * peak
513 *
514 *
515 */
516struct TofHit {
517 static const u16 HEAD = 0xF0F0;
518 static const u16 TAIL = 0xF0F;
519
520 u8 paddle_id;
521 // deprecated
522 bool broken;
523
524 // new variables for V1
525 Gaps::ProtocolVersion version;
526 f32 baseline_a;
527 f32 baseline_a_rms;
528 f32 baseline_b;
529 f32 baseline_b_rms;
530 f32 phase;
531
532 u32 timestamp32;
533 u16 timestamp16;
534 // don't serialize
535 f32 paddle_len;
536
537 u8 ctr_etx;
538 u16 tail = 0xF0F;
539
540 f32 get_time_a() const;
541 f32 get_time_b() const;
542 f32 get_peak_a() const;
543 f32 get_peak_b() const;
544 f32 get_charge_a() const;
545 f32 get_charge_b() const;
546 f32 get_charge_min_i() const;
547 f32 get_x_pos() const;
548 f32 get_t_avg() const;
549 f32 get_t0() const;
550 f64 get_timestamp48() const;
551
554 void set_paddle_len(f32 paddle_len);
555
556 static TofHit from_bytestream(const Vec<u8> &bytestream,
557 u64 &pos);
558
559 // easier print out
560 std::string to_string() const;
561
562 private:
563 // we keep this private, since
564 // the user should use the getters
565 // to get the values converted
566 // back to f32
567 // deprecated, but kept for compatibility
568 u16 time_a;
569 u16 time_b;
570 u16 peak_a;
571 u16 peak_b;
572 u16 charge_a;
573 u16 charge_b;
574 u16 charge_min_i;
575 u16 x_pos;
576 u16 t_average;
577
578 f32 time_a_f32;
579 f32 time_b_f32;
580 f32 peak_a_f32;
581 f32 peak_b_f32;
582 f32 charge_a_f32;
583 f32 charge_b_f32;
584};
585
586/************************
587 * A part of a TofEvent
588 * - a single waveform
589 *
590 * That is a waveform for
591 * a specific channel for a
592 * specific id.
593 *
594 * Each paddle has 2 waveforms
595 *
596 *
597 */
599 static const u16 HEAD = 0xAAAA;
600 static const u16 TAIL = 0x5555;
601
602 u32 event_id ;
603 u8 rb_id ;
604 u8 rb_channel;
605 u16 stop_cell ;
606 Vec<u16> adc ;
607
608 static RBWaveform from_bytestream(const Vec<u8> &bytestream,
609 u64 &pos);
610
611 std::string to_string() const;
612};
613
614
622 static const u16 HEAD = 0xAAAA;
623 static const u16 TAIL = 0x5555;
624
625 Gaps::ProtocolVersion version ;
626 EventStatus status ;
627 u8 quality ;
628 u16 trigger_sources ;
633 u32 event_id ;
634 // flight computer event variable packet
635 u8 n_hits_umb ;
636 u8 n_hits_cbe ;
637 u8 n_hits_cor ;
638 f32 tot_edep_umb ;
639 f32 tot_edep_cbe ;
640 f32 tot_edep_cor ;
641
642 u32 timestamp32 ;
643 u16 timestamp16 ;
648 u32 dsi_j_mask ;
649 Vec<u16> channel_mask ;
650 u64 mtb_link_mask ;
651 Vec<TofHit> hits ;
652
653 static TofEventSummary from_bytestream(const Vec<u8> &stream,
654 u64 &pos);
655 // combined timestamp
656 u64 get_timestamp48() const;
657
658 Vec<u8> get_rb_link_ids() const;
659
672 Vec<std::tuple<u8, u8, u8, LTBThreshold>> get_trigger_hits() const;
673
674
676 Vec<TriggerType> get_trigger_sources() const;
677
678 std::string to_string() const;
679};
680
681std::ostream& operator<<(std::ostream& os, const TofHit& pad);
682
683std::ostream& operator<<(std::ostream& os, const MasterTriggerEvent& mt);
684
685std::ostream& operator<<(std::ostream& os, const TofEvent& et);
686
687std::ostream& operator<<(std::ostream& os, const RBEvent& re);
688
689std::ostream& operator<<(std::ostream& os, const RBEventHeader& rh);
690
691std::ostream& operator<<(std::ostream& os, const RBWaveform& rh);
692
693std::ostream& operator<<(std::ostream& os, const TofEventSummary& tes);
694
695#endif
‍**
Definition caraspace.hpp:8
Definition events.h:348
static const u16 TAIL
end struct marker
Definition events.h:352
static const u16 HEAD
begin struct marker
Definition events.h:350
u32 event_id
event_id as assigned by the MasterTriggerBoard
Definition events.h:358
u32 tiu_timestamp
Tracker (?) timestamp.
Definition events.h:362
u64 get_timestamp_abs48() const
Get absolute timestamp as sent by the GPS.
u64 get_timestamp_gps48() const
Vec< TriggerType > get_trigger_sources() const
Get the trigger sources from trigger source byte.
u32 timestamp
MTB timestamp.
Definition events.h:360
static MasterTriggerEvent from_bytestream(const Vec< u8 > &bytestream, u64 &pos)
u32 tiu_gps32
GAPS GPS clock value (slow)
Definition events.h:364
std::string to_string() const
String representation of the struct.
u32 crc
triggered paddles as seen by the MTB
Definition events.h:368
u32 tiu_gps16
GAPS GPS clock value (fast)
Definition events.h:366
static const usize SIZE
the struct has a fixed size of SIZE
Definition events.h:354
Vec< std::tuple< u8, u8, u8, LTBThreshold > > get_trigger_hits() const
Definition calibration.h:33
Definition events.h:175
std::string to_string() const
string representation for printing
u64 get_timestamp48() const
the combined timestamp
Definition events.h:227
static f32 calc_baseline(const Vec< f32 > &volts, usize min_bin, usize max_bin)
Get the baseline for a single channel.
Definition events.h:269
Definition events.h:598
Definition tof_event_header.hpp:6
Definition events.h:621
Vec< std::tuple< u8, u8, u8, LTBThreshold > > get_trigger_hits() const
u16 primary_charge
reconstructed primary charge
Definition events.h:647
Vec< TriggerType > get_trigger_sources() const
Get the trigger sources from trigger source byte.
u8 n_trigger_paddles
Definition events.h:632
u16 primary_beta
reconstructed primary beta
Definition events.h:645
Definition events.h:434
static TofEvent from_tofpacket(const TofPacket &packet)
Vec< RBEvent > rb_events
Definition events.h:445
const RBEvent & get_rbevent(u8 board_id) const
Vec< u8 > get_rbids() const
Vec< RBMissingHit > missing_hits
Definition events.h:449
static TofEvent from_bytestream(const Vec< u8 > &bytestream, u64 &pos)
std::string to_string() const
string representation for printing
Definition events.h:516
void set_paddle_len(f32 paddle_len)
Definition tof_packet.h:81