File indexing completed on 2024-04-06 12:10:56
0001 #include "EventFilter/Phase2TrackerRawToDigi/interface/Phase2TrackerFEDHeader.h"
0002 #include "EventFilter/Phase2TrackerRawToDigi/interface/utils.h"
0003 #include "FWCore/Utilities/interface/Exception.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005
0006 namespace Phase2Tracker {
0007
0008 Phase2TrackerFEDHeader::Phase2TrackerFEDHeader(const uint8_t* headerPointer) : trackerHeader_(headerPointer) {
0009 header_first_word_ = read64(0, trackerHeader_);
0010 header_second_word_ = read64(8, trackerHeader_);
0011
0012 init();
0013 }
0014
0015 void Phase2TrackerFEDHeader::init() {
0016 dataFormatVersion_ = dataFormatVersion();
0017 debugMode_ = debugMode();
0018
0019
0020
0021 eventType_ = eventType();
0022 readoutMode_ = readoutMode();
0023 conditionData_ = conditionData();
0024 dataType_ = dataType();
0025
0026 glibStatusCode_ = glibStatusCode();
0027
0028 numberOfCBC_ = numberOfCBC();
0029 pointerToData_ = pointerToData();
0030
0031 LogTrace("Phase2TrackerFEDBuffer") << "[Phase2Tracker::Phase2TrackerFEDHeader::" << __func__ << "]: \n"
0032 << " Tracker Header contents:\n"
0033 << " -- Data Format Version : " << uint32_t(dataFormatVersion_) << "\n"
0034 << " -- Debug Level : " << debugMode_ << "\n"
0035 << " -- Operating Mode : " << readoutMode_ << "\n"
0036 << " -- Condition Data : " << (conditionData_ ? "Present" : "Absent")
0037 << "\n"
0038 << " -- Data Type : " << (dataType_ ? "Real" : "Fake") << "\n"
0039 << " -- Glib Stat registers : " << std::hex << std::setw(16) << glibStatusCode_
0040 << "\n"
0041 << " -- connected CBC : " << std::dec << numberOfCBC_ << "\n";
0042 }
0043
0044 uint8_t Phase2TrackerFEDHeader::dataFormatVersion() const {
0045 uint8_t Version = static_cast<uint8_t>(extract64(VERSION_M, VERSION_S, header_first_word_));
0046 if (Version != 1) {
0047 std::ostringstream ss;
0048 ss << "[Phase2Tracker::Phase2TrackerFEDHeader::" << __func__ << "] ";
0049 ss << "Invalid Data Format Version in Traker Header : ";
0050 printHex(&header_first_word_, 1, ss);
0051 throw cms::Exception("Phase2TrackerFEDBuffer") << ss.str();
0052 }
0053 return Version;
0054 }
0055
0056 READ_MODE Phase2TrackerFEDHeader::debugMode() const {
0057
0058 uint8_t mode = static_cast<uint8_t>(extract64(HEADER_FORMAT_M, HEADER_FORMAT_S, header_first_word_));
0059
0060 switch (mode) {
0061 case SUMMARY:
0062 return READ_MODE(SUMMARY);
0063 case FULL_DEBUG:
0064 return READ_MODE(FULL_DEBUG);
0065 case CBC_ERROR:
0066 return READ_MODE(CBC_ERROR);
0067 default:
0068 std::ostringstream ss;
0069 ss << "[Phase2Tracker::Phase2TrackerFEDHeader::" << __func__ << "] ";
0070 ss << "Invalid Header Format in Traker Header : ";
0071 printHex(&header_first_word_, 1, ss);
0072 throw cms::Exception("Phase2TrackerFEDBuffer") << ss.str();
0073 }
0074
0075 return READ_MODE(READ_MODE_INVALID);
0076 }
0077
0078 uint8_t Phase2TrackerFEDHeader::eventType() const {
0079 return static_cast<uint8_t>(extract64(EVENT_TYPE_M, EVENT_TYPE_S, header_first_word_));
0080 }
0081
0082
0083 FEDReadoutMode Phase2TrackerFEDHeader::readoutMode() const {
0084
0085 uint8_t mode = static_cast<uint8_t>(eventType_ >> 2) & 0x3;
0086
0087 switch (mode) {
0088 case 2:
0089 return FEDReadoutMode(READOUT_MODE_PROC_RAW);
0090 case 1:
0091 return FEDReadoutMode(READOUT_MODE_ZERO_SUPPRESSED);
0092 default:
0093 std::ostringstream ss;
0094 ss << "[Phase2Tracker::Phase2TrackerFEDHeader::" << __func__ << "] ";
0095 ss << "Invalid Readout Mode in Traker Header : ";
0096 printHex(&header_first_word_, 1, ss);
0097 throw cms::Exception("Phase2TrackerFEDBuffer") << ss.str();
0098 }
0099 }
0100
0101 uint8_t Phase2TrackerFEDHeader::conditionData() const { return static_cast<uint8_t>(eventType_ >> 1) & 0x1; }
0102
0103 uint8_t Phase2TrackerFEDHeader::dataType() const { return static_cast<uint8_t>(eventType_) & 0x1; }
0104
0105 uint64_t Phase2TrackerFEDHeader::glibStatusCode() const {
0106 return extract64(GLIB_STATUS_M, GLIB_STATUS_S, header_first_word_);
0107 }
0108
0109 std::vector<bool> Phase2TrackerFEDHeader::frontendStatus() const {
0110 uint16_t FE_status = static_cast<uint16_t>(extract64(FRONTEND_STAT_M, FRONTEND_STAT_S, header_first_word_));
0111 std::vector<bool> status(16, false);
0112 for (int i = 0; i < 16; i++) {
0113 status[i] = (FE_status >> i) & 0x1;
0114 }
0115 return status;
0116 }
0117
0118 uint16_t Phase2TrackerFEDHeader::numberOfCBC() const {
0119 if (debugMode_ != SUMMARY) {
0120 return static_cast<uint16_t>(extract64(CBC_NUMBER_M, CBC_NUMBER_S, header_second_word_));
0121 } else {
0122 return 0;
0123 }
0124 }
0125
0126
0127 std::vector<uint8_t> Phase2TrackerFEDHeader::CBCStatus() const {
0128
0129 int offset = 8;
0130 uint64_t data64 = header_second_word_;
0131
0132 uint16_t cbc_num = numberOfCBC();
0133
0134 int status_size = 0;
0135 if (debugMode_ == FULL_DEBUG) {
0136 status_size = 8;
0137 } else if (debugMode_ == CBC_ERROR) {
0138 status_size = 1;
0139 }
0140
0141 std::vector<uint8_t> cbc_status;
0142 if (status_size == 8) {
0143 int num_bytes = cbc_num;
0144 int current_byte = 5;
0145 while (num_bytes > 0) {
0146 cbc_status.push_back(static_cast<uint8_t>((data64 >> current_byte * 8) & 0xFF));
0147 if (current_byte == 0) {
0148 current_byte = 8;
0149 offset += 8;
0150 data64 = read64(offset, trackerHeader_);
0151 }
0152 current_byte--;
0153 num_bytes--;
0154 }
0155 } else if (status_size == 1) {
0156 int current_bit = 47;
0157 int num_bits = cbc_num;
0158 while (num_bits > 0) {
0159 cbc_status.push_back(static_cast<uint8_t>((data64 >> current_bit) & 0x1));
0160 if (current_bit == 0) {
0161 current_bit = 64;
0162 offset += 8;
0163 data64 = read64(offset, trackerHeader_);
0164 }
0165 current_bit--;
0166 num_bits--;
0167 }
0168 }
0169
0170 return cbc_status;
0171 }
0172
0173 const uint8_t* Phase2TrackerFEDHeader::pointerToData() {
0174 int status_size = 0;
0175 int cbc_num = numberOfCBC();
0176
0177 if (debugMode_ == FULL_DEBUG) {
0178 status_size = 8;
0179 } else if (debugMode_ == CBC_ERROR) {
0180 status_size = 1;
0181 }
0182
0183 int num_add_words64 = (cbc_num * status_size - 48 + 64 - 1) / 64;
0184
0185 trackerHeaderSize_ = (2 + num_add_words64) * 8;
0186 return &trackerHeader_[trackerHeaderSize_];
0187 }
0188
0189 }