File indexing completed on 2025-08-06 08:17:19
0001 #include "RDH.h"
0002
0003 #include <stdexcept>
0004 #include <cstring>
0005 #include <iostream>
0006 #include <iomanip>
0007
0008 #if __cplusplus >= 202002L
0009 #include <format>
0010 #endif
0011
0012 using namespace mvtx;
0013
0014
0015
0016 RDHAny::RDHAny(int v)
0017 {
0018 if (v == 0)
0019 {
0020 *this = RDH{};
0021 }
0022 else if (v == 8)
0023 {
0024 *this = RDHv8{};
0025 }
0026 else
0027 {
0028 throw std::runtime_error(std::string("unsupported RDH version ") + std::to_string(v));
0029 }
0030 }
0031
0032
0033 void RDHAny::copyFrom(const void* rdh)
0034 {
0035 std::memcpy(this, rdh, sizeof(RDHAny));
0036 }
0037
0038 #if __cplusplus >= 202002L
0039
0040 void RDHUtils::printRDH(const RDHv8& rdh)
0041 {
0042
0043 std::cout << std::format(
0044 "FlxHdrCntr:{:d} flxId:{:d} pageSize:0x{:03x} gbtLink:0x{:02x} ",
0045 (rdh.flxHdrCntr), (rdh.flxId), (rdh.pageSize), (rdh.gbtLink)).c_str();
0046 std::cout << std::format(
0047 "FlxHdrSize:0x{:02x} FlxHdrVersion:0x{:02x} FlxHdrCode:0x{:02x}",
0048 (rdh.flxHdrSize), (rdh.flxHdrVersion), (rdh.flxHdrCode)).c_str();
0049 std::cout << std::endl;
0050 std::cout << std::format(
0051 "RDHversion:0x{:02x} RDHsize:0x{:02x} FEEID:0x{:04x} SrcID:0x{:02x} ",
0052 (rdh.rdhVersion), (rdh.rdhSize), (rdh.feeId), (rdh.sourceId)).c_str();
0053 std::cout << std::endl;
0054 std::cout << std::format(
0055 "detField:0x{:08x} bc:0x{:03x} orbit:0x{:010x}",
0056 (rdh.detectorField), (rdh.bc), (rdh.orbit)).c_str();
0057 std::cout << std::endl;
0058 std::cout << std::format(
0059 "trgType:0x{:08x} packetCounter:0x{:04x} stopBit:0x{:01x} priority:0x{:01x} RDHGBTCntr:0x{:04X} ",
0060 (rdh.trgType), (rdh.packetCounter), (rdh.stopBit), (rdh.priority), (rdh.rdhGBTcounter)).c_str();
0061 std::cout << std::endl;
0062 }
0063 #else
0064 void RDHUtils::printRDH(const RDHv8& )
0065 {
0066 std::cerr << "RDHUtils::printRDH(const RDHv8&) only implemented in c++20." << std::endl;
0067 }
0068 #endif
0069
0070
0071 void RDHUtils::printRDH(const void* rdhP)
0072 {
0073 int version = getVersion(rdhP);
0074 if(version==8){
0075 printRDH(*reinterpret_cast<const RDHv8*>(rdhP));
0076 }
0077 else{
0078 std::cerr << "Unexpected RDH version " << version << " from";
0079 dumpRDH(rdhP);
0080 throw std::runtime_error("invalid RDH provided");
0081 }
0082
0083 }
0084
0085
0086 #if __cplusplus >= 202002L
0087 void RDHUtils::dumpRDH(const void* rdhP)
0088 {
0089 const uint32_t* w32 = reinterpret_cast<const uint32_t*>(rdhP);
0090 for (int i = 0; i < 4; i++)
0091 {
0092 int l = 4 * i;
0093 std::cout << std::format(
0094 "[rdh{:d}] 0x{:08x} 0x{:08x} 0x{:08x} 0x{:08x}",
0095 i, w32[l + 3], w32[l + 2], w32[l + 1], w32[l]).c_str();
0096 std::cout << std::endl;
0097 }
0098 }
0099 #else
0100 void RDHUtils::dumpRDH(const void* )
0101 {
0102 std::cerr << "void RDHUtils::dumpRDH(const void*) only implemented in c++20." << std::endl;
0103 }
0104 #endif
0105
0106
0107 bool RDHUtils::checkRDH(const void* rdhP, bool verbose, bool checkZeros)
0108 {
0109 int version = getVersion(rdhP);
0110 bool ok = true;
0111
0112 if(version==8){
0113 ok = checkRDH(*reinterpret_cast<const RDHv8*>(rdhP), verbose, checkZeros);
0114 }
0115 else {
0116 ok = false;
0117 if (verbose)
0118 {
0119 std::cerr << "WARNING: "
0120 << "Unexpected RDH version " << version << " from" << std::endl;
0121 }
0122 }
0123 if (!ok && verbose) {
0124 dumpRDH(rdhP);
0125 }
0126 return ok;
0127 }
0128
0129
0130 bool RDHUtils::checkRDH(const RDHv8& rdh, bool verbose, bool checkZeros)
0131 {
0132
0133 bool ok = true;
0134 if (rdh.flxHdrSize != 0x20)
0135 {
0136 if (verbose)
0137 {
0138 std::cerr << "WARNING: "
0139 << "RDH FLX Header Size 0x20 is expected instead of "
0140 << int(rdh.flxHdrSize) << std::endl;
0141 }
0142 ok = false;
0143 }
0144 if (rdh.flxHdrVersion != 0x01)
0145 {
0146 if (verbose)
0147 {
0148 std::cerr << "WARNING: "
0149 << "RDH FLX Header version 0x01 is expected instead of "
0150 << int(rdh.flxHdrVersion) << std::endl;
0151 }
0152 ok = false;
0153 }
0154 if (rdh.flxHdrCode != 0xAB)
0155 {
0156 if (verbose)
0157 {
0158 std::cerr << "WARNING: "
0159 << "RDH FLX Header Code 0xAB is expected instead of "
0160 << int(rdh.flxHdrVersion) << std::endl;
0161 }
0162 ok = false;
0163 }
0164 if (rdh.rdhVersion != 8)
0165 {
0166 if (verbose) {
0167 std::cerr << "WARNING: "
0168 << "RDH version 7 is expected instead of "
0169 << int(rdh.rdhVersion) << std::endl;
0170 }
0171 ok = false;
0172 }
0173 if (rdh.rdhSize != 32)
0174 {
0175 if (verbose) {
0176 std::cerr << "WARNING: "
0177 << "RDH with header size of 32 B is expected instead of "
0178 << int(rdh.rdhSize) << std::endl;
0179 }
0180 ok = false;
0181 }
0182 if ((! rdh.packetCounter) && (rdh.stopBit))
0183 {
0184 if (verbose) {
0185 std::cerr << "WARNING: "
0186 << "RDH stopBit is not expected in packetCounter 0 "
0187 << int(rdh.packetCounter) << int(rdh.stopBit) << std::endl;
0188 }
0189 ok = false;
0190 }
0191 if (checkZeros && (rdh.zero0 || rdh.zero1 || rdh.zero2 || rdh.zero3 || rdh.zero4 || rdh.zero5))
0192 {
0193 if (verbose)
0194 {
0195 std::cerr << "WARNING: "
0196 << "Some reserved fields of RDH v7 are not empty"
0197 << std::endl;
0198 }
0199 ok = false;
0200 }
0201 if (!ok && verbose)
0202 {
0203 dumpRDH(rdh);
0204 }
0205 return ok;
0206 }