Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-10 23:12:03

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 }  // namespace
0039 
0040 CTPPSPixelDataFormatter::CTPPSPixelDataFormatter(std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo> const& mapping)
0041     : m_WordCounter(0), m_Mapping(mapping) {
0042   int s32 = sizeof(Word32);
0043   int s64 = sizeof(Word64);
0044   int s8 = sizeof(char);
0045   if (s8 != 1 || s32 != 4 * s8 || s64 != 2 * s32) {
0046     LogError("UnexpectedSizes") << " unexpected sizes: "
0047                                 << "  size of char is: " << s8 << ", size of Word32 is: " << s32
0048                                 << ", size of Word64 is: " << s64 << ", send exception";
0049   }
0050 
0051   m_ADC_shift = 0;
0052   m_PXID_shift = m_ADC_shift + m_ADC_bits;
0053   m_DCOL_shift = m_PXID_shift + m_PXID_bits;
0054   //Run3 shifts
0055   m_ROW_shift = m_ADC_shift + m_ADC_bits;
0056   m_COL_shift = m_ROW_shift + m_ROW_bits;
0057 
0058   m_ROC_shift = m_DCOL_shift + m_DCOL_bits;
0059 
0060   m_LINK_shift = m_ROC_shift + m_ROC_bits;
0061   m_LINK_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_LINK_bits);
0062   m_ROC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROC_bits);
0063 
0064   m_DCOL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_DCOL_bits);
0065   m_PXID_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_PXID_bits);
0066   //Run3 masks
0067   m_COL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_COL_bits);
0068   m_ROW_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROW_bits);
0069 
0070   m_ADC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ADC_bits);
0071 }
0072 
0073 void CTPPSPixelDataFormatter::setErrorStatus(bool errorStatus) {
0074   m_IncludeErrors = errorStatus;
0075   m_ErrorCheck.setErrorStatus(m_IncludeErrors);
0076 }
0077 
0078 void CTPPSPixelDataFormatter::interpretRawData(
0079     const bool& isRun3, bool& errorsInEvent, int fedId, const FEDRawData& rawData, Collection& digis, Errors& errors) {
0080   int nWords = rawData.size() / sizeof(Word64);
0081   if (nWords == 0)
0082     return;
0083 
0084   /// check CRC bit
0085   const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
0086   if (!m_ErrorCheck.checkCRC(errorsInEvent, fedId, trailer, errors))
0087     return;
0088 
0089   /// check headers
0090   const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
0091   header--;
0092   bool moreHeaders = true;
0093   while (moreHeaders) {
0094     header++;
0095     LogTrace("") << "HEADER:  " << print(*header);
0096     bool headerStatus = m_ErrorCheck.checkHeader(errorsInEvent, fedId, header, errors);
0097     moreHeaders = headerStatus;
0098   }
0099 
0100   /// check trailers
0101   bool moreTrailers = true;
0102   trailer++;
0103   while (moreTrailers) {
0104     trailer--;
0105     LogTrace("") << "TRAILER: " << print(*trailer);
0106     bool trailerStatus = m_ErrorCheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
0107     moreTrailers = trailerStatus;
0108   }
0109 
0110   /// data words
0111   m_WordCounter += 2 * (nWords - 2);
0112   LogTrace("") << "data words: " << (trailer - header - 1);
0113 
0114   int link = -1;
0115   int roc = -1;
0116 
0117   bool skipROC = false;
0118 
0119   edm::DetSet<CTPPSPixelDigi>* detDigis = nullptr;
0120 
0121   const Word32* bw = (const Word32*)(header + 1);
0122   const Word32* ew = (const Word32*)(trailer);
0123   if (*(ew - 1) == 0) {
0124     ew--;
0125     m_WordCounter--;
0126   }
0127   for (auto word = bw; word < ew; ++word) {
0128     LogTrace("") << "DATA: " << print(*word);
0129 
0130     auto ww = *word;
0131     if UNLIKELY (ww == 0) {
0132       m_WordCounter--;
0133       continue;
0134     }
0135     int nlink = (ww >> m_LINK_shift) & m_LINK_mask;
0136     int nroc = (ww >> m_ROC_shift) & m_ROC_mask;
0137     int FMC = 0;
0138     uint32_t iD = RPixErrorChecker::dummyDetId;  //0xFFFFFFFF; //dummyDetId
0139     int convroc = nroc - 1;
0140     CTPPSPixelFramePosition fPos(fedId, FMC, nlink, convroc);
0141 
0142     std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo>::const_iterator mit;
0143     mit = m_Mapping.find(fPos);
0144 
0145     if (mit == m_Mapping.end()) {
0146       if (nlink >= maxLinkIndex) {
0147         m_ErrorCheck.conversionError(fedId, iD, InvalidLinkId, ww, errors);
0148       } else if ((nroc - 1) >= maxRocIndex) {
0149         m_ErrorCheck.conversionError(fedId, iD, InvalidROCId, ww, errors);
0150       } else {
0151         m_ErrorCheck.conversionError(fedId, iD, Unknown, ww, errors);
0152       }
0153       continue;  //skip word
0154     }
0155 
0156     CTPPSPixelROCInfo rocInfo = (*mit).second;
0157     iD = rocInfo.iD;
0158     CTPPSPixelROC rocp(iD, rocInfo.roc, convroc);
0159 
0160     if ((nlink != link) | (nroc != roc)) {  // new roc
0161       link = nlink;
0162       roc = nroc;
0163 
0164       skipROC = LIKELY((roc - 1) < maxRocIndex) ? false : !m_ErrorCheck.checkROC(errorsInEvent, fedId, iD, ww, errors);
0165       if (skipROC)
0166         continue;
0167 
0168       auto rawId = rocp.rawId();
0169 
0170       detDigis = &digis.find_or_insert(rawId);
0171       if ((*detDigis).empty())
0172         (*detDigis).data.reserve(32);  // avoid the first relocations
0173     }
0174 
0175     int adc = (ww >> m_ADC_shift) & m_ADC_mask;
0176 
0177     int dcol = (ww >> m_DCOL_shift) & m_DCOL_mask;
0178     int pxid = (ww >> m_PXID_shift) & m_PXID_mask;
0179     int col = (ww >> m_COL_shift) & m_COL_mask;
0180     int row = (ww >> m_ROW_shift) & m_ROW_mask;
0181 
0182     if (!isRun3 && (dcol < min_Dcol || dcol > max_Dcol || pxid < min_Pixid || pxid > max_Pixid)) {
0183       edm::LogError("CTPPSPixelDataFormatter")
0184           << " unphysical dcol and/or pxid "
0185           << " nllink=" << nlink << " nroc=" << nroc << " adc=" << adc << " dcol=" << dcol << " pxid=" << pxid;
0186 
0187       m_ErrorCheck.conversionError(fedId, iD, InvalidPixelId, ww, errors);
0188 
0189       continue;
0190     }
0191     if (isRun3 && (col < min_COL || col > max_COL || row < min_ROW || row > max_ROW)) {
0192       edm::LogError("CTPPSPixelDataFormatter")
0193           << " unphysical col and/or row "
0194           << " nllink=" << nlink << " nroc=" << nroc << " adc=" << adc << " col=" << col << " row=" << row;
0195 
0196       m_ErrorCheck.conversionError(fedId, iD, InvalidPixelId, ww, errors);
0197 
0198       continue;
0199     }
0200 
0201     std::pair<int, int> rocPixel;
0202     std::pair<int, int> modPixel;
0203 
0204     if (isRun3) {
0205       rocPixel = std::make_pair(row, col);
0206       modPixel = rocp.toGlobal(rocPixel);
0207     } else {
0208       rocPixel = std::make_pair(dcol, pxid);
0209       modPixel = rocp.toGlobalfromDcol(rocPixel);
0210     }
0211 
0212     CTPPSPixelDigi testdigi(modPixel.first, modPixel.second, adc);
0213 
0214     if (detDigis)
0215       (*detDigis).data.emplace_back(modPixel.first, modPixel.second, adc);
0216   }
0217 }
0218 
0219 void CTPPSPixelDataFormatter::formatRawData(const bool& isRun3,
0220                                             unsigned int lvl1_ID,
0221                                             RawData& fedRawData,
0222                                             const Digis& digis,
0223                                             std::vector<PPSPixelIndex> iDdet2fed) {
0224   std::map<int, vector<Word32> > words;
0225   // translate digis into 32-bit raw words and store in map indexed by Fed
0226   m_allDetDigis = 0;
0227   m_hasDetDigis = 0;
0228   for (auto const& im : digis) {
0229     m_allDetDigis++;
0230     cms_uint32_t rawId = im.first;
0231 
0232     const DetDigis& detDigis = im.second;
0233     for (auto const& it : detDigis) {
0234       int nroc = 999, nlink = 999;
0235       int rocPixelRow = -1, rocPixelColumn = -1, rocID = -1;
0236       int modulePixelColumn = it.column();
0237       int modulePixelRow = it.row();
0238 
0239       m_Indices.transformToROC(modulePixelColumn, modulePixelRow, rocID, rocPixelColumn, rocPixelRow);
0240       const int dcol = m_Indices.DColumn(rocPixelColumn);
0241       const int pxid = 2 * (rpixValues::ROCSizeInX - rocPixelRow) + (rocPixelColumn % 2);
0242 
0243       unsigned int urocID = rocID;
0244       PPSPixelIndex myTest = {rawId, urocID, 0, 0, 0};
0245       // the range has always at most one element
0246       auto range = std::equal_range(iDdet2fed.begin(), iDdet2fed.end(), myTest, compare);
0247       if (range.first != range.second) {
0248         auto i = range.first - iDdet2fed.begin();
0249         nlink = iDdet2fed.at(i).fedch;
0250         nroc = iDdet2fed.at(i).rocch + 1;
0251 
0252         pps::pixel::ElectronicIndex cabling = {nlink, nroc, dcol, pxid};
0253         if (isRun3) {
0254           cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
0255                               (rocPixelColumn << m_COL_shift) | (rocPixelRow << m_ROW_shift) |
0256                               (it.adc() << m_ADC_shift);
0257 
0258           words[iDdet2fed.at(i).fedid].push_back(word);
0259         } else {
0260           cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
0261                               (cabling.dcol << m_DCOL_shift) | (cabling.pxid << m_PXID_shift) |
0262                               (it.adc() << m_ADC_shift);
0263 
0264           words[iDdet2fed.at(i).fedid].push_back(word);
0265         }
0266         m_WordCounter++;
0267         m_hasDetDigis++;
0268 
0269       }  // range
0270     }    // for DetDigis
0271   }      // for Digis
0272 
0273   LogTrace(" allDetDigis/hasDetDigis : ") << m_allDetDigis << "/" << m_hasDetDigis;
0274   for (auto const& feddata : words) {
0275     int fedId = feddata.first;
0276 
0277     // since raw words are written in the form of 64-bit packets
0278     // add extra 32-bit word to make number of words even if necessary
0279     if (words.find(fedId)->second.size() % 2 != 0)
0280       words[fedId].emplace_back(0);
0281 
0282     // size in Bytes; create output structure
0283     size_t dataSize = words.find(fedId)->second.size() * sizeof(Word32);
0284     int nHeaders = 1;
0285     int nTrailers = 1;
0286     dataSize += (nHeaders + nTrailers) * sizeof(Word64);
0287 
0288     FEDRawData rawData{dataSize};
0289 
0290     // get begining of data;
0291     Word64* word = reinterpret_cast<Word64*>(rawData.data());
0292 
0293     // write one header
0294     FEDHeader::set(reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
0295     word++;
0296 
0297     // write data
0298     unsigned int nWord32InFed = words.find(fedId)->second.size();
0299     for (unsigned int i = 0; i < nWord32InFed; i += 2) {
0300       *word = (Word64(words.find(fedId)->second[i]) << 32) | words.find(fedId)->second[i + 1];
0301       LogDebug("CTPPSPixelDataFormatter") << print(*word);
0302       word++;
0303     }
0304 
0305     // write one trailer
0306     FEDTrailer::set(reinterpret_cast<unsigned char*>(word), dataSize / sizeof(Word64), 0, 0, 0);
0307     word++;
0308 
0309     // check memory
0310     if (word != reinterpret_cast<Word64*>(rawData.data() + dataSize)) {
0311       //if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
0312       string s = "** PROBLEM in CTPPSPixelDataFormatter !!!";
0313       LogError("CTPPSPixelDataFormatter") << "** PROBLEM in CTPPSPixelDataFormatter!!!";
0314       throw cms::Exception(s);
0315     }  // if (word !=
0316     fedRawData[fedId] = rawData;
0317   }  // for (RI feddata
0318 }
0319 
0320 std::string CTPPSPixelDataFormatter::print(const Word64& word) const {
0321   std::ostringstream str;
0322   str << "word64:  " << reinterpret_cast<const std::bitset<64>&>(word);
0323   return str.str();
0324 }