File indexing completed on 2025-08-06 08:17:19
0001
0002
0003
0004
0005 #ifndef MVTXDECODER_RDH_H
0006 #define MVTXDECODER_RDH_H
0007
0008 #include <cstdint>
0009 #include <type_traits>
0010
0011 namespace mvtx {
0012
0013 struct RDHv8 {
0014 #pragma GCC diagnostic push
0015 #pragma GCC diagnostic ignored "-Wpedantic"
0016 union {
0017
0018 uint64_t word0 = 0x0;
0019 struct {
0020 uint64_t flxHdrCntr : 32;
0021 uint64_t zero0 : 32;
0022 };
0023 };
0024 union {
0025 uint64_t word1 = 0x0;
0026 struct {
0027 uint64_t zero1 : 64;
0028 };
0029 };
0030 union{
0031 uint64_t word2 = 0x0;
0032 struct {
0033 uint64_t zero2 : 56;
0034 uint64_t flxId : 8;
0035 };
0036 };
0037 union {
0038 uint64_t word3 = 0xab01200000000000;
0039 struct {
0040 uint64_t zero3 : 8;
0041 uint64_t pageSize : 12;
0042 uint64_t zero4 : 12;
0043 uint64_t gbtLink : 5;
0044 uint64_t zero5: 3;
0045 uint64_t flxHdrSize: 8;
0046 uint64_t flxHdrVersion : 8;
0047 uint64_t flxHdrCode : 8;
0048 };
0049 };
0050
0051 union{
0052 struct __attribute__((packed)) {
0053 uint64_t word4 = 0x000000ffff2008;
0054 uint16_t word5 = 0x0;
0055 };
0056 struct __attribute__((packed)) {
0057 uint64_t rdhVersion : 8;
0058 uint64_t rdhSize : 8;
0059 uint64_t feeId : 16;
0060 uint64_t sourceId: 8;
0061 uint64_t detectorField : 32;
0062 uint64_t zero6 : 8;
0063 };
0064 };
0065 union {
0066 struct __attribute__((packed)) {
0067 uint64_t word6 = 0x0;
0068 uint16_t word7 = 0x0;
0069 };
0070 struct __attribute__((packed)) {
0071 uint64_t bc : 12;
0072 uint64_t zero7 : 20;
0073 uint64_t orbit : 40;
0074 uint64_t zero8 : 8;
0075 };
0076 };
0077 union {
0078 struct __attribute__((packed)) {
0079 uint64_t word8 = 0x0;
0080 uint32_t word9 = 0x0;
0081 };
0082 struct __attribute__((packed)) {
0083 uint64_t trgType : 32;
0084 uint64_t packetCounter : 16;
0085 uint64_t stopBit : 8;
0086 uint64_t priority : 8;
0087 uint64_t zero9 : 16;
0088 uint64_t rdhGBTcounter : 16;
0089 };
0090 };
0091 };
0092 #pragma GCC diagnostic pop
0093
0094 struct RDHAny {
0095
0096 uint64_t word0 = 0x0;
0097 uint64_t word1 = 0x0;
0098 uint64_t word2 = 0x0;
0099 uint64_t word3 = 0x0;
0100 uint64_t word4 = 0x0;
0101 uint64_t word5 = 0x0;
0102 uint64_t word6 = 0x0;
0103 uint64_t word7 = 0x0;
0104
0105 RDHAny(int v = 0);
0106
0107 template <typename H>
0108 RDHAny(const H& rdh);
0109
0110 template <typename H>
0111 RDHAny& operator=(const H& rdh);
0112
0113
0114 using RDHv8 = mvtx::RDHv8;
0115
0116
0117 template <typename rdh>
0118 static constexpr void sanityCheckStrict()
0119 {
0120 static_assert(std::is_same<rdh, RDHv8>::value,
0121 "not an RDH");
0122 }
0123
0124
0125 template <typename rdh>
0126 static constexpr void sanityCheckLoose()
0127 {
0128 static_assert(std::is_same<rdh, RDHv8>::value || std::is_same<rdh, RDHAny>::value,
0129 "not an RDH or RDHAny");
0130 }
0131
0132 template <typename H>
0133 static const void* voidify(const H& rdh)
0134 {
0135 sanityCheckLoose<H>();
0136 return reinterpret_cast<const void*>(&rdh);
0137 }
0138
0139 template <typename H>
0140 static void* voidify(H& rdh)
0141 {
0142 sanityCheckLoose<H>();
0143 return reinterpret_cast<void*>(&rdh);
0144 }
0145
0146 const void* voidify() const { return voidify(*this); }
0147 void* voidify() { return voidify(*this); }
0148
0149 template <typename H>
0150 H* as_ptr()
0151 {
0152 sanityCheckLoose<H>();
0153 return reinterpret_cast<H*>(this);
0154 }
0155
0156 template <typename H>
0157 H& as_ref()
0158 {
0159 sanityCheckLoose<H>();
0160 return reinterpret_cast<H&>(this);
0161 }
0162
0163 protected:
0164 void copyFrom(const void* rdh);
0165 };
0166
0167 using RDH = RDHv8;
0168
0169 struct RDHUtils {
0170
0171
0172 #define TOREF(DST, SRC) *reinterpret_cast<DST*>(SRC)
0173
0174 #define TOCREF(DST, SRC) *reinterpret_cast<const DST*>(SRC)
0175
0176 using RDHDef = mvtx::RDH;
0177 using RDHAny = mvtx::RDHAny;
0178
0179
0180 template <typename H>
0181 static constexpr int getVersion()
0182 {
0183 RDHAny::sanityCheckStrict<H>();
0184 if (std::is_same<H, RDHv8>::value) {
0185 return 8;
0186 }
0187 return -1;
0188 }
0189
0190
0191 template <typename H>
0192 static uint8_t getVersion(const H& rdh)
0193 {
0194 return rdh.rdhVersion;
0195 }
0196 static uint8_t getVersion(const RDHAny& rdh) { return getVersion(rdh.voidify()); }
0197 static uint8_t getVersion(const void* rdhP) { return getVersion(TOCREF(RDHDef, rdhP)); }
0198
0199
0200 static void printRDH(const RDHv8& rdh);
0201 static void printRDH(const RDHAny& rdh) { printRDH(rdh.voidify()); }
0202 static void printRDH(const void* rdhP);
0203
0204
0205 static bool checkRDH(const RDHv8& rdh, bool verbose = true, bool checkZeros = false);
0206 static bool checkRDH(const RDHAny& rdh, bool verbose = true, bool checkZeros = false)
0207 {
0208 return checkRDH(rdh.voidify(), verbose, checkZeros);
0209 }
0210 static bool checkRDH(const void* rdhP, bool verbose = true, bool checkZeros = false);
0211
0212
0213 template <typename H>
0214 static void dumpRDH(const H& rdh)
0215 {
0216 dumpRDH(reinterpret_cast<const void*>(&rdh));
0217 }
0218 static void dumpRDH(const void* rdhP);
0219 };
0220
0221
0222
0223 template <typename H>
0224 inline RDHAny::RDHAny(const H& rdh)
0225 {
0226 sanityCheckLoose<H>();
0227 copyFrom(&rdh);
0228 }
0229
0230
0231
0232 template <typename H>
0233 inline RDHAny& RDHAny::operator=(const H& rdh)
0234 {
0235 sanityCheckLoose<H>();
0236 if (this != voidify(rdh)) {
0237 copyFrom(&rdh);
0238 }
0239 return *this;
0240 }
0241
0242 }
0243
0244 #endif