CMS 3D CMS Logo

CTPPSPixelDataFormatter.cc
Go to the documentation of this file.
2 
6 
8 
11 
12 #include <bitset>
13 #include <sstream>
14 #include <iostream>
15 
16 using namespace edm;
17 using namespace std;
18 
19 namespace {
20  constexpr int m_LINK_bits = 6;
21  constexpr int m_ROC_bits = 5;
22  constexpr int m_DCOL_bits = 5;
23  constexpr int m_PXID_bits = 8;
24  constexpr int m_COL_bits = 6;
25  constexpr int m_ROW_bits = 7;
26  constexpr int m_ADC_bits = 8;
27  constexpr int min_Dcol = 0;
28  constexpr int max_Dcol = 25;
29  constexpr int min_Pixid = 2;
30  constexpr int max_Pixid = 161;
31  constexpr int min_COL = 0;
32  constexpr int max_COL = 51;
33  constexpr int min_ROW = 0;
34  constexpr int max_ROW = 79;
35  constexpr int maxRocIndex = 3;
36  constexpr int maxLinkIndex = 49;
37 
38 } // namespace
39 
40 CTPPSPixelDataFormatter::CTPPSPixelDataFormatter(std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo> const& mapping)
41  : m_WordCounter(0), m_Mapping(mapping) {
42  int s32 = sizeof(Word32);
43  int s64 = sizeof(Word64);
44  int s8 = sizeof(char);
45  if (s8 != 1 || s32 != 4 * s8 || s64 != 2 * s32) {
46  LogError("UnexpectedSizes") << " unexpected sizes: "
47  << " size of char is: " << s8 << ", size of Word32 is: " << s32
48  << ", size of Word64 is: " << s64 << ", send exception";
49  }
50 
51  m_ADC_shift = 0;
52  m_PXID_shift = m_ADC_shift + m_ADC_bits;
53  m_DCOL_shift = m_PXID_shift + m_PXID_bits;
54  //Run3 shifts
55  m_ROW_shift = m_ADC_shift + m_ADC_bits;
56  m_COL_shift = m_ROW_shift + m_ROW_bits;
57 
58  m_ROC_shift = m_DCOL_shift + m_DCOL_bits;
59 
60  m_LINK_shift = m_ROC_shift + m_ROC_bits;
61  m_LINK_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_LINK_bits);
62  m_ROC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROC_bits);
63 
64  m_DCOL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_DCOL_bits);
65  m_PXID_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_PXID_bits);
66  //Run3 masks
67  m_COL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_COL_bits);
68  m_ROW_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROW_bits);
69 
70  m_ADC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ADC_bits);
71 }
72 
74  m_IncludeErrors = errorStatus;
76 }
77 
79  const bool& isRun3, bool& errorsInEvent, int fedId, const FEDRawData& rawData, Collection& digis, Errors& errors) {
80  int nWords = rawData.size() / sizeof(Word64);
81  if (nWords == 0)
82  return;
83 
85  const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
86  if (!m_ErrorCheck.checkCRC(errorsInEvent, fedId, trailer, errors))
87  return;
88 
90  const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
91  header--;
92  bool moreHeaders = true;
93  while (moreHeaders) {
94  header++;
95  LogTrace("") << "HEADER: " << print(*header);
96  bool headerStatus = m_ErrorCheck.checkHeader(errorsInEvent, fedId, header, errors);
97  moreHeaders = headerStatus;
98  }
99 
101  bool moreTrailers = true;
102  trailer++;
103  while (moreTrailers) {
104  trailer--;
105  LogTrace("") << "TRAILER: " << print(*trailer);
106  bool trailerStatus = m_ErrorCheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
107  moreTrailers = trailerStatus;
108  }
109 
111  m_WordCounter += 2 * (nWords - 2);
112  LogTrace("") << "data words: " << (trailer - header - 1);
113 
114  int link = -1;
115  int roc = -1;
116 
117  bool skipROC = false;
118 
119  edm::DetSet<CTPPSPixelDigi>* detDigis = nullptr;
120 
121  const Word32* bw = (const Word32*)(header + 1);
122  const Word32* ew = (const Word32*)(trailer);
123  if (*(ew - 1) == 0) {
124  ew--;
125  m_WordCounter--;
126  }
127 
128  for (auto word = bw; word < ew; ++word) {
129  LogTrace("") << "DATA: " << print(*word);
130  auto ww = *word;
131  if UNLIKELY (ww == 0) {
132  m_WordCounter--;
133  continue;
134  }
135 
136  int nlink = (ww >> m_LINK_shift) & m_LINK_mask;
137  int nroc = (ww >> m_ROC_shift) & m_ROC_mask;
138  int FMC = 0;
139  uint32_t iD = RPixErrorChecker::dummyDetId; //0xFFFFFFFF; //dummyDetId
140  int convroc = nroc - 1;
141 
142  CTPPSPixelROC rocp;
143  CTPPSPixelFramePosition fPos(fedId, FMC, nlink, convroc);
144  std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo>::const_iterator mit;
145  mit = m_Mapping.find(fPos);
146  if (mit != m_Mapping.end()) {
147  CTPPSPixelROCInfo rocInfo = (*mit).second;
148  iD = rocInfo.iD;
149  rocp.setParameters(iD, rocInfo.roc, convroc);
150  }
151 
152  if ((nlink != link) | (nroc != roc)) { // new roc
153  link = nlink;
154  roc = nroc;
155 
156  if ((roc - 1) < maxRocIndex) {
157  skipROC = false;
158  } else {
159  // using dummy detId - recovering of FED channel foreseen in DQM
161  skipROC = !m_ErrorCheck.checkROC(errorsInEvent, fedId, iD, ww, errors);
162  }
163  if (skipROC)
164  continue;
165 
166  if (mit == m_Mapping.end()) {
167  if (nlink >= maxLinkIndex) {
169  edm::LogError("CTPPSPixelDataFormatter") << " Invalid linkId ";
170  } else if ((nroc - 1) >= maxRocIndex) {
172  edm::LogError("CTPPSPixelDataFormatter")
173  << " Invalid ROC Id " << convroc << " in nlink " << nlink << " of FED " << fedId << " in DetId " << iD;
174  } else {
176  edm::LogError("CTPPSPixelDataFormatter") << " Error unknown ";
177  }
178  skipROC = true; // skipping roc due to mapping errors
179  continue;
180  }
181  if (rocp.rawId() == 0) {
182  skipROC = true;
183  continue;
184  }
185 
186  auto rawId = rocp.rawId();
187 
188  detDigis = &digis.find_or_insert(rawId);
189  if ((*detDigis).empty())
190  (*detDigis).data.reserve(32); // avoid the first relocations
191  }
192 
193  if (skipROC || rocp.rawId() == 0)
194  continue;
195 
196  int adc = (ww >> m_ADC_shift) & m_ADC_mask;
197 
198  int dcol = (ww >> m_DCOL_shift) & m_DCOL_mask;
199  int pxid = (ww >> m_PXID_shift) & m_PXID_mask;
200  int col = (ww >> m_COL_shift) & m_COL_mask;
201  int row = (ww >> m_ROW_shift) & m_ROW_mask;
202 
203  if (!isRun3 && (dcol < min_Dcol || dcol > max_Dcol || pxid < min_Pixid || pxid > max_Pixid)) {
204  edm::LogError("CTPPSPixelDataFormatter")
205  << " unphysical dcol and/or pxid "
206  << "fedId=" << fedId << " nllink=" << nlink << " convroc=" << convroc << " adc=" << adc << " dcol=" << dcol
207  << " pxid=" << pxid << " detId=" << iD;
208 
210 
211  continue;
212  }
213  if (isRun3 && (col < min_COL || col > max_COL || row < min_ROW || row > max_ROW)) {
214  edm::LogError("CTPPSPixelDataFormatter") << " unphysical col and/or row "
215  << "fedId=" << fedId << " nllink=" << nlink << " convroc=" << convroc
216  << " adc=" << adc << " col=" << col << " row=" << row << " detId=" << iD;
218 
219  continue;
220  }
221 
222  std::pair<int, int> rocPixel;
223  std::pair<int, int> modPixel;
224 
225  if (isRun3) {
226  rocPixel = std::make_pair(row, col);
227  modPixel = rocp.toGlobal(rocPixel);
228  } else {
229  rocPixel = std::make_pair(dcol, pxid);
230  modPixel = rocp.toGlobalfromDcol(rocPixel);
231  }
232 
233  CTPPSPixelDigi testdigi(modPixel.first, modPixel.second, adc);
234 
235  if (detDigis)
236  (*detDigis).data.emplace_back(modPixel.first, modPixel.second, adc);
237  }
238 }
239 
241  unsigned int lvl1_ID,
243  const Digis& digis,
244  std::vector<PPSPixelIndex> iDdet2fed) {
245  std::map<int, vector<Word32> > words;
246  // translate digis into 32-bit raw words and store in map indexed by Fed
247  m_allDetDigis = 0;
248  m_hasDetDigis = 0;
249  for (auto const& im : digis) {
250  m_allDetDigis++;
251  cms_uint32_t rawId = im.first;
252 
253  const DetDigis& detDigis = im.second;
254  for (auto const& it : detDigis) {
255  int nroc = 999, nlink = 999;
256  int rocPixelRow = -1, rocPixelColumn = -1, rocID = -1;
257  int modulePixelColumn = it.column();
258  int modulePixelRow = it.row();
259 
260  m_Indices.transformToROC(modulePixelColumn, modulePixelRow, rocID, rocPixelColumn, rocPixelRow);
261  const int dcol = m_Indices.DColumn(rocPixelColumn);
262  const int pxid = 2 * (rpixValues::ROCSizeInX - rocPixelRow) + (rocPixelColumn % 2);
263 
264  unsigned int urocID = rocID;
265  PPSPixelIndex myTest = {rawId, urocID, 0, 0, 0};
266  // the range has always at most one element
267  auto range = std::equal_range(iDdet2fed.begin(), iDdet2fed.end(), myTest, compare);
268  if (range.first != range.second) {
269  auto i = range.first - iDdet2fed.begin();
270  nlink = iDdet2fed.at(i).fedch;
271  nroc = iDdet2fed.at(i).rocch + 1;
272 
273  pps::pixel::ElectronicIndex cabling = {nlink, nroc, dcol, pxid};
274  if (isRun3) {
275  cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
276  (rocPixelColumn << m_COL_shift) | (rocPixelRow << m_ROW_shift) |
277  (it.adc() << m_ADC_shift);
278 
279  words[iDdet2fed.at(i).fedid].push_back(word);
280  } else {
281  cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
282  (cabling.dcol << m_DCOL_shift) | (cabling.pxid << m_PXID_shift) |
283  (it.adc() << m_ADC_shift);
284 
285  words[iDdet2fed.at(i).fedid].push_back(word);
286  }
287  m_WordCounter++;
288  m_hasDetDigis++;
289 
290  } // range
291  } // for DetDigis
292  } // for Digis
293 
294  LogTrace(" allDetDigis/hasDetDigis : ") << m_allDetDigis << "/" << m_hasDetDigis;
295  for (auto const& feddata : words) {
296  int fedId = feddata.first;
297 
298  // since raw words are written in the form of 64-bit packets
299  // add extra 32-bit word to make number of words even if necessary
300  if (words.find(fedId)->second.size() % 2 != 0)
301  words[fedId].emplace_back(0);
302 
303  // size in Bytes; create output structure
304  size_t dataSize = words.find(fedId)->second.size() * sizeof(Word32);
305  int nHeaders = 1;
306  int nTrailers = 1;
307  dataSize += (nHeaders + nTrailers) * sizeof(Word64);
308 
309  FEDRawData rawData{dataSize};
310 
311  // get begining of data;
312  Word64* word = reinterpret_cast<Word64*>(rawData.data());
313 
314  // write one header
315  FEDHeader::set(reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
316  word++;
317 
318  // write data
319  unsigned int nWord32InFed = words.find(fedId)->second.size();
320  for (unsigned int i = 0; i < nWord32InFed; i += 2) {
321  *word = (Word64(words.find(fedId)->second[i]) << 32) | words.find(fedId)->second[i + 1];
322  LogDebug("CTPPSPixelDataFormatter") << print(*word);
323  word++;
324  }
325 
326  // write one trailer
327  FEDTrailer::set(reinterpret_cast<unsigned char*>(word), dataSize / sizeof(Word64), 0, 0, 0);
328  word++;
329 
330  // check memory
331  if (word != reinterpret_cast<Word64*>(rawData.data() + dataSize)) {
332  //if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
333  string s = "** PROBLEM in CTPPSPixelDataFormatter !!!";
334  LogError("CTPPSPixelDataFormatter") << "** PROBLEM in CTPPSPixelDataFormatter!!!";
335  throw cms::Exception(s);
336  } // if (word !=
338  } // for (RI feddata
339 }
340 
342  std::ostringstream str;
343  str << "word64: " << reinterpret_cast<const std::bitset<64>&>(word);
344  return str.str();
345 }
void setErrorStatus(bool theErrorStatus)
void interpretRawData(const bool &isRun3, bool &errorsInEvent, int fedId, const FEDRawData &data, Collection &digis, Errors &errors)
Contains mappind data related to a ROC.
bool checkHeader(bool &errorsInEvent, int fedId, const Word64 *header, Errors &errors) const
bool checkCRC(bool &errorsInEvent, int fedId, const Word64 *trailer, Errors &errors) const
std::string print(const Word64 &word) const
void setParameters(uint32_t du, int idInDU, int idLk)
Definition: CTPPSPixelROC.h:29
std::map< uint32_t, DetErrors > Errors
bool checkTrailer(bool &errorsInEvent, int fedId, unsigned int nWords, const Word64 *trailer, Errors &errors) const
std::unordered_map< int, FEDRawData > RawData
static bool compare(const PPSPixelIndex &a, const PPSPixelIndex &b)
const std::map< CTPPSPixelFramePosition, CTPPSPixelROCInfo > & m_Mapping
Log< level::Error, false > LogError
bool checkROC(bool &errorsInEvent, int fedId, uint32_t iD, const Word32 &errorWord, Errors &errors) const
#define LogTrace(id)
reference find_or_insert(det_id_type id)
Definition: DetSetVector.h:234
uint32_t iD
the symbolic id
uint64_t word
static void set(unsigned char *trailer, uint32_t lenght, uint16_t crc, uint8_t evt_stat, uint8_t tts, bool moreTrailers=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:31
std::unordered_map< cms_uint32_t, DetDigis > Digis
int transformToROC(const int col, const int row, int &rocId, int &colROC, int &rowROC) const
uint32_t rawId() const
return the DetUnit to which this ROC belongs to.
Definition: CTPPSPixelROC.h:36
unsigned int cms_uint32_t
Definition: typedefs.h:15
void formatRawData(const bool &isRun3, unsigned int lvl1_ID, RawData &fedRawData, const Digis &digis, std::vector< PPSPixelIndex > v_iDdet2fed)
void setErrorStatus(bool errorStatus)
std::vector< CTPPSPixelDigi > DetDigis
void conversionError(int fedId, uint32_t iD, const State &state, const Word32 &errorWord, Errors &errors) const
static constexpr Word32 dummyDetId
collection_type data
Definition: DetSet.h:80
HLT enums.
col
Definition: cuy.py:1009
Definition: errors.py:1
static void set(unsigned char *header, uint8_t triggerType, uint32_t lvl1ID, uint16_t bxID, uint16_t sourceID, uint8_t version=0, bool moreHeaders=false)
Set all fields in the header.
Definition: FEDHeader.cc:25
#define UNLIKELY(x)
Definition: Likely.h:21
std::pair< int, int > toGlobalfromDcol(const std::pair< int, int > &rocPixel) const
Definition: CTPPSPixelROC.h:71
#define str(s)
static int DColumn(const int colROC)
uint16_t *__restrict__ uint16_t const *__restrict__ adc
constexpr int ROCSizeInX
#define LogDebug(id)
CTPPSPixelDataFormatter(std::map< CTPPSPixelFramePosition, CTPPSPixelROCInfo > const &mapping)
std::pair< int, int > toGlobal(const std::pair< int, int > &rocPixel) const
Definition: CTPPSPixelROC.h:57