File indexing completed on 2024-09-07 04:36:08
0001 #include "EventFilter/CTPPSRawToDigi/interface/CTPPSPixelDataFormatter.h"
0002
0003 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0004 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0005 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0006
0007 #include "CondFormats/PPSObjects/interface/CTPPSPixelROC.h" //KS
0008
0009 #include "FWCore/Utilities/interface/Exception.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011
0012 #include <bitset>
0013 #include <sstream>
0014 #include <iostream>
0015
0016 using namespace edm;
0017 using namespace std;
0018
0019 namespace {
0020 constexpr int m_LINK_bits = 6;
0021 constexpr int m_ROC_bits = 5;
0022 constexpr int m_DCOL_bits = 5;
0023 constexpr int m_PXID_bits = 8;
0024 constexpr int m_COL_bits = 6;
0025 constexpr int m_ROW_bits = 7;
0026 constexpr int m_ADC_bits = 8;
0027 constexpr int min_Dcol = 0;
0028 constexpr int max_Dcol = 25;
0029 constexpr int min_Pixid = 2;
0030 constexpr int max_Pixid = 161;
0031 constexpr int min_COL = 0;
0032 constexpr int max_COL = 51;
0033 constexpr int min_ROW = 0;
0034 constexpr int max_ROW = 79;
0035 constexpr int maxRocIndex = 3;
0036 constexpr int maxLinkIndex = 49;
0037
0038 }
0039
0040 CTPPSPixelDataFormatter::CTPPSPixelDataFormatter(std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo> const& mapping,
0041 CTPPSRawToDigiErrorSummary& eSummary)
0042 : m_WordCounter(0),
0043 m_Mapping(mapping),
0044 m_ErrorSummary(eSummary)
0045
0046 {
0047 int s32 = sizeof(Word32);
0048 int s64 = sizeof(Word64);
0049 int s8 = sizeof(char);
0050 if (s8 != 1 || s32 != 4 * s8 || s64 != 2 * s32) {
0051 LogError("UnexpectedSizes") << " unexpected sizes: "
0052 << " size of char is: " << s8 << ", size of Word32 is: " << s32
0053 << ", size of Word64 is: " << s64 << ", send exception";
0054 }
0055
0056 m_ADC_shift = 0;
0057 m_PXID_shift = m_ADC_shift + m_ADC_bits;
0058 m_DCOL_shift = m_PXID_shift + m_PXID_bits;
0059
0060 m_ROW_shift = m_ADC_shift + m_ADC_bits;
0061 m_COL_shift = m_ROW_shift + m_ROW_bits;
0062
0063 m_ROC_shift = m_DCOL_shift + m_DCOL_bits;
0064
0065 m_LINK_shift = m_ROC_shift + m_ROC_bits;
0066 m_LINK_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_LINK_bits);
0067 m_ROC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROC_bits);
0068
0069 m_DCOL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_DCOL_bits);
0070 m_PXID_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_PXID_bits);
0071
0072 m_COL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_COL_bits);
0073 m_ROW_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROW_bits);
0074
0075 m_ADC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ADC_bits);
0076 }
0077
0078 void CTPPSPixelDataFormatter::setErrorStatus(bool errorStatus) {
0079 m_IncludeErrors = errorStatus;
0080 m_ErrorCheck.setErrorStatus(m_IncludeErrors);
0081 }
0082
0083 void CTPPSPixelDataFormatter::interpretRawData(
0084 const bool& isRun3, bool& errorsInEvent, int fedId, const FEDRawData& rawData, Collection& digis, Errors& errors) {
0085 int nWords = rawData.size() / sizeof(Word64);
0086 if (nWords == 0)
0087 return;
0088
0089
0090 const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
0091 if (!m_ErrorCheck.checkCRC(errorsInEvent, fedId, trailer, errors))
0092 return;
0093
0094
0095 const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
0096 header--;
0097 bool moreHeaders = true;
0098 while (moreHeaders) {
0099 header++;
0100 LogTrace("") << "HEADER: " << print(*header);
0101 bool headerStatus = m_ErrorCheck.checkHeader(errorsInEvent, fedId, header, errors);
0102 moreHeaders = headerStatus;
0103 }
0104
0105
0106 bool moreTrailers = true;
0107 trailer++;
0108 while (moreTrailers) {
0109 trailer--;
0110 LogTrace("") << "TRAILER: " << print(*trailer);
0111 bool trailerStatus = m_ErrorCheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
0112 moreTrailers = trailerStatus;
0113 }
0114
0115
0116 m_WordCounter += 2 * (nWords - 2);
0117 LogTrace("") << "data words: " << (trailer - header - 1);
0118
0119 int link = -1;
0120 int roc = -1;
0121
0122 bool skipROC = false;
0123
0124 edm::DetSet<CTPPSPixelDigi>* detDigis = nullptr;
0125
0126 const Word32* bw = (const Word32*)(header + 1);
0127 const Word32* ew = (const Word32*)(trailer);
0128 if (*(ew - 1) == 0) {
0129 ew--;
0130 m_WordCounter--;
0131 }
0132
0133 for (auto word = bw; word < ew; ++word) {
0134 LogTrace("") << "DATA: " << print(*word);
0135 auto ww = *word;
0136 if UNLIKELY (ww == 0) {
0137 m_WordCounter--;
0138 continue;
0139 }
0140
0141 int nlink = (ww >> m_LINK_shift) & m_LINK_mask;
0142 int nroc = (ww >> m_ROC_shift) & m_ROC_mask;
0143 int FMC = 0;
0144 uint32_t iD = RPixErrorChecker::dummyDetId;
0145 int convroc = nroc - 1;
0146
0147 CTPPSPixelROC rocp;
0148 CTPPSPixelFramePosition fPos(fedId, FMC, nlink, convroc);
0149 std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo>::const_iterator mit;
0150 mit = m_Mapping.find(fPos);
0151 if (mit != m_Mapping.end()) {
0152 CTPPSPixelROCInfo rocInfo = (*mit).second;
0153 iD = rocInfo.iD;
0154 rocp.setParameters(iD, rocInfo.roc, convroc);
0155 }
0156
0157 if ((nlink != link) | (nroc != roc)) {
0158 link = nlink;
0159 roc = nroc;
0160
0161 if ((roc - 1) < maxRocIndex) {
0162 skipROC = false;
0163 } else {
0164
0165 iD = RPixErrorChecker::dummyDetId;
0166 skipROC = !m_ErrorCheck.checkROC(errorsInEvent, fedId, iD, ww, errors);
0167 }
0168 if (skipROC)
0169 continue;
0170
0171 if (mit == m_Mapping.end()) {
0172 if (nlink >= maxLinkIndex) {
0173 m_ErrorCheck.conversionError(fedId, iD, InvalidLinkId, ww, errors);
0174
0175 m_ErrorSummary.add("Invalid linkId", "");
0176 } else if ((nroc - 1) >= maxRocIndex) {
0177 m_ErrorCheck.conversionError(fedId, iD, InvalidROCId, ww, errors);
0178 m_ErrorSummary.add("Invalid ROC",
0179 fmt::format("Id {0}, in link {1}, of FED {2} in DetId {3}", convroc, nlink, fedId, iD));
0180
0181 } else {
0182 m_ErrorCheck.conversionError(fedId, iD, Unknown, ww, errors);
0183 m_ErrorSummary.add("Error unknown");
0184 }
0185 skipROC = true;
0186 continue;
0187 }
0188 if (rocp.rawId() == 0) {
0189 skipROC = true;
0190 continue;
0191 }
0192
0193 auto rawId = rocp.rawId();
0194
0195 detDigis = &digis.find_or_insert(rawId);
0196 if ((*detDigis).empty())
0197 (*detDigis).data.reserve(32);
0198 }
0199
0200 if (skipROC || rocp.rawId() == 0)
0201 continue;
0202
0203 int adc = (ww >> m_ADC_shift) & m_ADC_mask;
0204
0205 int dcol = (ww >> m_DCOL_shift) & m_DCOL_mask;
0206 int pxid = (ww >> m_PXID_shift) & m_PXID_mask;
0207 int col = (ww >> m_COL_shift) & m_COL_mask;
0208 int row = (ww >> m_ROW_shift) & m_ROW_mask;
0209
0210 if (!isRun3 && (dcol < min_Dcol || dcol > max_Dcol || pxid < min_Pixid || pxid > max_Pixid)) {
0211 m_ErrorSummary.add(
0212 "unphysical dcol and/or pxid",
0213 fmt::format("fedId= {0}, nllink= {1}, convroc= {2}, adc= {3}, dcol= {4}, pxid= {5}, detId= {6}",
0214 fedId,
0215 nlink,
0216 convroc,
0217 adc,
0218 dcol,
0219 pxid,
0220 iD));
0221
0222 m_ErrorCheck.conversionError(fedId, iD, InvalidPixelId, ww, errors);
0223
0224 continue;
0225 }
0226 if (isRun3 && (col < min_COL || col > max_COL || row < min_ROW || row > max_ROW)) {
0227 m_ErrorSummary.add("unphysical col and/or row",
0228 fmt::format("fedId= {0}, nllink= {1}, convroc= {2}, adc= {3}, col= {4}, row= {5}, detId= {6}",
0229 fedId,
0230 nlink,
0231 convroc,
0232 adc,
0233 col,
0234 row,
0235 iD));
0236
0237 m_ErrorCheck.conversionError(fedId, iD, InvalidPixelId, ww, errors);
0238
0239 continue;
0240 }
0241
0242 std::pair<int, int> rocPixel;
0243 std::pair<int, int> modPixel;
0244
0245 if (isRun3) {
0246 rocPixel = std::make_pair(row, col);
0247 modPixel = rocp.toGlobal(rocPixel);
0248 } else {
0249 rocPixel = std::make_pair(dcol, pxid);
0250 modPixel = rocp.toGlobalfromDcol(rocPixel);
0251 }
0252
0253 CTPPSPixelDigi testdigi(modPixel.first, modPixel.second, adc);
0254
0255 if (detDigis)
0256 (*detDigis).data.emplace_back(modPixel.first, modPixel.second, adc);
0257 }
0258 }
0259
0260 void CTPPSPixelDataFormatter::formatRawData(const bool& isRun3,
0261 unsigned int lvl1_ID,
0262 RawData& fedRawData,
0263 const Digis& digis,
0264 std::vector<PPSPixelIndex> iDdet2fed) {
0265 std::map<int, vector<Word32> > words;
0266
0267 m_allDetDigis = 0;
0268 m_hasDetDigis = 0;
0269 for (auto const& im : digis) {
0270 m_allDetDigis++;
0271 cms_uint32_t rawId = im.first;
0272
0273 const DetDigis& detDigis = im.second;
0274 for (auto const& it : detDigis) {
0275 int nroc = 999, nlink = 999;
0276 int rocPixelRow = -1, rocPixelColumn = -1, rocID = -1;
0277 int modulePixelColumn = it.column();
0278 int modulePixelRow = it.row();
0279
0280 m_Indices.transformToROC(modulePixelColumn, modulePixelRow, rocID, rocPixelColumn, rocPixelRow);
0281 const int dcol = m_Indices.DColumn(rocPixelColumn);
0282 const int pxid = 2 * (rpixValues::ROCSizeInX - rocPixelRow) + (rocPixelColumn % 2);
0283
0284 unsigned int urocID = rocID;
0285 PPSPixelIndex myTest = {rawId, urocID, 0, 0, 0};
0286
0287 auto range = std::equal_range(iDdet2fed.begin(), iDdet2fed.end(), myTest, compare);
0288 if (range.first != range.second) {
0289 auto i = range.first - iDdet2fed.begin();
0290 nlink = iDdet2fed.at(i).fedch;
0291 nroc = iDdet2fed.at(i).rocch + 1;
0292
0293 pps::pixel::ElectronicIndex cabling = {nlink, nroc, dcol, pxid};
0294 if (isRun3) {
0295 cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
0296 (rocPixelColumn << m_COL_shift) | (rocPixelRow << m_ROW_shift) |
0297 (it.adc() << m_ADC_shift);
0298
0299 words[iDdet2fed.at(i).fedid].push_back(word);
0300 } else {
0301 cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
0302 (cabling.dcol << m_DCOL_shift) | (cabling.pxid << m_PXID_shift) |
0303 (it.adc() << m_ADC_shift);
0304
0305 words[iDdet2fed.at(i).fedid].push_back(word);
0306 }
0307 m_WordCounter++;
0308 m_hasDetDigis++;
0309
0310 }
0311 }
0312 }
0313
0314 LogTrace(" allDetDigis/hasDetDigis : ") << m_allDetDigis << "/" << m_hasDetDigis;
0315 for (auto const& feddata : words) {
0316 int fedId = feddata.first;
0317
0318
0319
0320 if (words.find(fedId)->second.size() % 2 != 0)
0321 words[fedId].emplace_back(0);
0322
0323
0324 size_t dataSize = words.find(fedId)->second.size() * sizeof(Word32);
0325 int nHeaders = 1;
0326 int nTrailers = 1;
0327 dataSize += (nHeaders + nTrailers) * sizeof(Word64);
0328
0329 FEDRawData rawData{dataSize};
0330
0331
0332 Word64* word = reinterpret_cast<Word64*>(rawData.data());
0333
0334
0335 FEDHeader::set(reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
0336 word++;
0337
0338
0339 unsigned int nWord32InFed = words.find(fedId)->second.size();
0340 for (unsigned int i = 0; i < nWord32InFed; i += 2) {
0341 *word = (Word64(words.find(fedId)->second[i]) << 32) | words.find(fedId)->second[i + 1];
0342 LogDebug("CTPPSPixelDataFormatter") << print(*word);
0343 word++;
0344 }
0345
0346
0347 FEDTrailer::set(reinterpret_cast<unsigned char*>(word), dataSize / sizeof(Word64), 0, 0, 0);
0348 word++;
0349
0350
0351 if (word != reinterpret_cast<Word64*>(rawData.data() + dataSize)) {
0352
0353 string s = "** PROBLEM in CTPPSPixelDataFormatter !!!";
0354 LogError("CTPPSPixelDataFormatter") << "** PROBLEM in CTPPSPixelDataFormatter!!!";
0355 throw cms::Exception(s);
0356 }
0357 fedRawData[fedId] = rawData;
0358 }
0359 }
0360
0361 std::string CTPPSPixelDataFormatter::print(const Word64& word) const {
0362 std::ostringstream str;
0363 str << "word64: " << reinterpret_cast<const std::bitset<64>&>(word);
0364 return str.str();
0365 }