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_ADC_bits = 8;
25  constexpr int min_Dcol = 0;
26  constexpr int max_Dcol = 25;
27  constexpr int min_Pixid = 2;
28  constexpr int max_Pixid = 161;
29  constexpr int maxRocIndex = 3;
30  constexpr int maxLinkIndex = 13;
31 } // namespace
32 
33 CTPPSPixelDataFormatter::CTPPSPixelDataFormatter(std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo> const& mapping)
34  : m_WordCounter(0), m_Mapping(mapping) {
35  int s32 = sizeof(Word32);
36  int s64 = sizeof(Word64);
37  int s8 = sizeof(char);
38  if (s8 != 1 || s32 != 4 * s8 || s64 != 2 * s32) {
39  LogError("UnexpectedSizes") << " unexpected sizes: "
40  << " size of char is: " << s8 << ", size of Word32 is: " << s32
41  << ", size of Word64 is: " << s64 << ", send exception";
42  }
43 
44  m_ADC_shift = 0;
45  m_PXID_shift = m_ADC_shift + m_ADC_bits;
46  m_DCOL_shift = m_PXID_shift + m_PXID_bits;
47  m_ROC_shift = m_DCOL_shift + m_DCOL_bits;
48 
49  m_LINK_shift = m_ROC_shift + m_ROC_bits;
50  m_LINK_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_LINK_bits);
51  m_ROC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ROC_bits);
52 
53  m_DCOL_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_DCOL_bits);
54  m_PXID_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_PXID_bits);
55  m_ADC_mask = ~(~CTPPSPixelDataFormatter::Word32(0) << m_ADC_bits);
56 }
57 
59  m_IncludeErrors = errorStatus;
61 }
62 
64  bool& errorsInEvent, int fedId, const FEDRawData& rawData, Collection& digis, Errors& errors) {
65  int nWords = rawData.size() / sizeof(Word64);
66  if (nWords == 0)
67  return;
68 
70  const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
71  if (!m_ErrorCheck.checkCRC(errorsInEvent, fedId, trailer, errors))
72  return;
73 
75  const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
76  header--;
77  bool moreHeaders = true;
78  while (moreHeaders) {
79  header++;
80  LogTrace("") << "HEADER: " << print(*header);
81  bool headerStatus = m_ErrorCheck.checkHeader(errorsInEvent, fedId, header, errors);
82  moreHeaders = headerStatus;
83  }
84 
86  bool moreTrailers = true;
87  trailer++;
88  while (moreTrailers) {
89  trailer--;
90  LogTrace("") << "TRAILER: " << print(*trailer);
91  bool trailerStatus = m_ErrorCheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
92  moreTrailers = trailerStatus;
93  }
94 
96  m_WordCounter += 2 * (nWords - 2);
97  LogTrace("") << "data words: " << (trailer - header - 1);
98 
99  int link = -1;
100  int roc = -1;
101 
102  bool skipROC = false;
103 
104  edm::DetSet<CTPPSPixelDigi>* detDigis = nullptr;
105 
106  const Word32* bw = (const Word32*)(header + 1);
107  const Word32* ew = (const Word32*)(trailer);
108  if (*(ew - 1) == 0) {
109  ew--;
110  m_WordCounter--;
111  }
112  for (auto word = bw; word < ew; ++word) {
113  LogTrace("") << "DATA: " << print(*word);
114 
115  auto ww = *word;
116  if UNLIKELY (ww == 0) {
117  m_WordCounter--;
118  continue;
119  }
120  int nlink = (ww >> m_LINK_shift) & m_LINK_mask;
121  int nroc = (ww >> m_ROC_shift) & m_ROC_mask;
122 
123  int FMC = 0;
124  uint32_t iD = RPixErrorChecker::dummyDetId; //0xFFFFFFFF; //dummyDetId
125  int convroc = nroc - 1;
126  CTPPSPixelFramePosition fPos(fedId, FMC, nlink, convroc);
127  std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo>::const_iterator mit;
128  mit = m_Mapping.find(fPos);
129 
130  if (mit == m_Mapping.end()) {
131  if (nlink >= maxLinkIndex) {
133  } else if ((nroc - 1) >= maxRocIndex) {
135  } else {
137  }
138  continue; //skip word
139  }
140 
141  CTPPSPixelROCInfo rocInfo = (*mit).second;
142  iD = rocInfo.iD;
143  CTPPSPixelROC rocp(iD, rocInfo.roc, convroc);
144 
145  if ((nlink != link) | (nroc != roc)) { // new roc
146  link = nlink;
147  roc = nroc;
148 
149  skipROC = LIKELY((roc - 1) < maxRocIndex) ? false : !m_ErrorCheck.checkROC(errorsInEvent, fedId, iD, ww, errors);
150  if (skipROC)
151  continue;
152 
153  auto rawId = rocp.rawId();
154 
155  detDigis = &digis.find_or_insert(rawId);
156  if ((*detDigis).empty())
157  (*detDigis).data.reserve(32); // avoid the first relocations
158  }
159 
160  int adc = (ww >> m_ADC_shift) & m_ADC_mask;
161 
162  int dcol = (ww >> m_DCOL_shift) & m_DCOL_mask;
163  int pxid = (ww >> m_PXID_shift) & m_PXID_mask;
164 
165  if (dcol < min_Dcol || dcol > max_Dcol || pxid < min_Pixid || pxid > max_Pixid) {
166  edm::LogError("CTPPSPixelDataFormatter")
167  << " unphysical dcol and/or pxid "
168  << " nllink=" << nlink << " nroc=" << nroc << " adc=" << adc << " dcol=" << dcol << " pxid=" << pxid;
169 
171 
172  continue;
173  }
174 
175  std::pair<int, int> rocPixel;
176  std::pair<int, int> modPixel;
177 
178  rocPixel = std::make_pair(dcol, pxid);
179 
180  modPixel = rocp.toGlobalfromDcol(rocPixel);
181 
182  CTPPSPixelDigi testdigi(modPixel.first, modPixel.second, adc);
183 
184  if (detDigis)
185  (*detDigis).data.emplace_back(modPixel.first, modPixel.second, adc);
186  }
187 }
188 
189 void CTPPSPixelDataFormatter::formatRawData(unsigned int lvl1_ID,
191  const Digis& digis,
192  std::vector<PPSPixelIndex> iDdet2fed) {
193  std::map<int, vector<Word32> > words;
194  // translate digis into 32-bit raw words and store in map indexed by Fed
195  m_allDetDigis = 0;
196  m_hasDetDigis = 0;
197  for (auto const& im : digis) {
198  m_allDetDigis++;
199  cms_uint32_t rawId = im.first;
200 
201  const DetDigis& detDigis = im.second;
202  for (auto const& it : detDigis) {
203  int nroc = 999, nlink = 999;
204  int rocPixelRow = -1, rocPixelColumn = -1, rocID = -1;
205  int modulePixelColumn = it.column();
206  int modulePixelRow = it.row();
207 
208  m_Indices.transformToROC(modulePixelColumn, modulePixelRow, rocID, rocPixelColumn, rocPixelRow);
209  const int dcol = m_Indices.DColumn(rocPixelColumn);
210  const int pxid = 2 * (ROCSizeInX - rocPixelRow) + (rocPixelColumn % 2);
211 
212  unsigned int urocID = rocID;
213  PPSPixelIndex myTest = {rawId, urocID, 0, 0, 0};
214  // the range has always at most one element
215  auto range = std::equal_range(iDdet2fed.begin(), iDdet2fed.end(), myTest, compare);
216  if (range.first != range.second) {
217  auto i = range.first - iDdet2fed.begin();
218  nlink = iDdet2fed.at(i).fedch;
219  nroc = iDdet2fed.at(i).rocch + 1;
220 
221  pps::pixel::ElectronicIndex cabling = {nlink, nroc, dcol, pxid};
222 
223  cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
224  (cabling.dcol << m_DCOL_shift) | (cabling.pxid << m_PXID_shift) | (it.adc() << m_ADC_shift);
225 
226  words[iDdet2fed.at(i).fedid].push_back(word);
227  m_WordCounter++;
228  m_hasDetDigis++;
229 
230  } // range
231  } // for DetDigis
232  } // for Digis
233 
234  LogTrace(" allDetDigis/hasDetDigis : ") << m_allDetDigis << "/" << m_hasDetDigis;
235  for (auto const& feddata : words) {
236  int fedId = feddata.first;
237 
238  // since raw words are written in the form of 64-bit packets
239  // add extra 32-bit word to make number of words even if necessary
240  if (words.find(fedId)->second.size() % 2 != 0)
241  words[fedId].emplace_back(0);
242 
243  // size in Bytes; create output structure
244  size_t dataSize = words.find(fedId)->second.size() * sizeof(Word32);
245  int nHeaders = 1;
246  int nTrailers = 1;
247  dataSize += (nHeaders + nTrailers) * sizeof(Word64);
248 
249  FEDRawData rawData{dataSize};
250 
251  // get begining of data;
252  Word64* word = reinterpret_cast<Word64*>(rawData.data());
253 
254  // write one header
255  FEDHeader::set(reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
256  word++;
257 
258  // write data
259  unsigned int nWord32InFed = words.find(fedId)->second.size();
260  for (unsigned int i = 0; i < nWord32InFed; i += 2) {
261  *word = (Word64(words.find(fedId)->second[i]) << 32) | words.find(fedId)->second[i + 1];
262  LogDebug("CTPPSPixelDataFormatter") << print(*word);
263  word++;
264  }
265 
266  // write one trailer
267  FEDTrailer::set(reinterpret_cast<unsigned char*>(word), dataSize / sizeof(Word64), 0, 0, 0);
268  word++;
269 
270  // check memory
271  if (word != reinterpret_cast<Word64*>(rawData.data() + dataSize)) {
272  //if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
273  string s = "** PROBLEM in CTPPSPixelDataFormatter !!!";
274  LogError("CTPPSPixelDataFormatter") << "** PROBLEM in CTPPSPixelDataFormatter!!!";
275  throw cms::Exception(s);
276  } // if (word !=
278  } // for (RI feddata
279 }
280 
282  std::ostringstream str;
283  str << "word64: " << reinterpret_cast<const std::bitset<64>&>(word);
284  return str.str();
285 }
edm::DetSetVector
Definition: DetSetVector.h:61
cms_uint32_t
unsigned int cms_uint32_t
Definition: typedefs.h:15
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
mps_fire.i
i
Definition: mps_fire.py:428
pps::pixel::ElectronicIndex::pxid
int pxid
Definition: ElectronicIndex.h:9
MessageLogger.h
CTPPSPixelDataFormatter::nWords
int nWords() const
Definition: CTPPSPixelDataFormatter.h:72
funct::false
false
Definition: Factorize.h:29
RPixErrorChecker::dummyDetId
static constexpr Word32 dummyDetId
Definition: RPixErrorChecker.h:39
MainPageGenerator.link
link
Definition: MainPageGenerator.py:271
CTPPSPixelROC::toGlobalfromDcol
std::pair< int, int > toGlobalfromDcol(const std::pair< int, int > &rocPixel) const
Definition: CTPPSPixelROC.h:65
CTPPSPixelDataFormatter::m_ErrorCheck
RPixErrorChecker m_ErrorCheck
Definition: CTPPSPixelDataFormatter.h:99
edm::DetSet
Definition: DetSet.h:23
edm
HLT enums.
Definition: AlignableModifier.h:19
gpuClustering::adc
uint16_t *__restrict__ uint16_t const *__restrict__ adc
Definition: gpuClusterChargeCut.h:20
CTPPSPixelDataFormatter::m_DCOL_shift
int m_DCOL_shift
Definition: CTPPSPixelDataFormatter.h:101
CTPPSPixelDataFormatter::CTPPSPixelDataFormatter
CTPPSPixelDataFormatter(std::map< CTPPSPixelFramePosition, CTPPSPixelROCInfo > const &mapping)
Definition: CTPPSPixelDataFormatter.cc:33
CTPPSPixelDataFormatter::setErrorStatus
void setErrorStatus(bool theErrorStatus)
Definition: CTPPSPixelDataFormatter.cc:58
l1tstage2_dqm_sourceclient-live_cfg.rawData
rawData
Definition: l1tstage2_dqm_sourceclient-live_cfg.py:162
CTPPSPixelIndices::DColumn
static int DColumn(const int colROC)
Definition: CTPPSPixelIndices.h:293
CTPPSPixelFramePosition
Definition: CTPPSPixelFramePosition.h:27
CTPPSPixelROCInfo::iD
uint32_t iD
the symbolic id
Definition: CTPPSPixelDAQMapping.h:31
reco::Unknown
Definition: MuonSimInfo.h:32
pps::pixel::ElectronicIndex::dcol
int dcol
Definition: ElectronicIndex.h:8
CTPPSPixelDataFormatter.h
FEDRawData.h
RPixErrorChecker::setErrorStatus
void setErrorStatus(bool errorStatus)
Definition: RPixErrorChecker.cc:14
FEDRawData
Definition: FEDRawData.h:19
word
uint64_t word
Definition: CTPPSTotemDataFormatter.cc:29
CTPPSPixelDataFormatter::Errors
std::map< uint32_t, DetErrors > Errors
Definition: CTPPSPixelDataFormatter.h:61
CTPPSPixelDataFormatter::m_DCOL_mask
Word32 m_DCOL_mask
Definition: CTPPSPixelDataFormatter.h:102
UNLIKELY
#define UNLIKELY(x)
Definition: Likely.h:21
pps::pixel::ElectronicIndex
Definition: ElectronicIndex.h:5
alignCSCRings.s
s
Definition: alignCSCRings.py:92
RPixErrorChecker::conversionError
void conversionError(int fedId, uint32_t iD, const State &state, const Word32 &errorWord, Errors &errors) const
Definition: RPixErrorChecker.cc:134
l1t_dqm_sourceclient-live_cfg.fedRawData
fedRawData
Definition: l1t_dqm_sourceclient-live_cfg.py:188
errors
Definition: errors.py:1
CTPPSPixelDataFormatter::m_Indices
CTPPSPixelIndices m_Indices
Definition: CTPPSPixelDataFormatter.h:113
InvalidPixelId
Definition: RPixErrorChecker.h:15
InvalidLinkId
Definition: RPixErrorChecker.h:15
CTPPSPixelIndices::transformToROC
int transformToROC(const int col, const int row, int &rocId, int &colROC, int &rowROC) const
Definition: CTPPSPixelIndices.h:195
CTPPSPixelROCInfo::roc
unsigned int roc
Definition: CTPPSPixelDAQMapping.h:33
str
#define str(s)
Definition: TestProcessor.cc:52
CTPPSPixelDataFormatter::m_ROC_shift
int m_ROC_shift
Definition: CTPPSPixelDataFormatter.h:101
CTPPSPixelDataFormatter::m_WordCounter
int m_WordCounter
Definition: CTPPSPixelDataFormatter.h:96
CTPPSPixelDataFormatter::PPSPixelIndex
Definition: CTPPSPixelDataFormatter.h:78
CTPPSPixelDataFormatter::RawData
std::unordered_map< int, FEDRawData > RawData
Definition: CTPPSPixelDataFormatter.h:57
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
RPixErrorChecker::checkHeader
bool checkHeader(bool &errorsInEvent, int fedId, const Word64 *header, Errors &errors) const
Definition: RPixErrorChecker.cc:29
CTPPSPixelDigi
Definition: CTPPSPixelDigi.h:10
CTPPSPixelDataFormatter::compare
static bool compare(const PPSPixelIndex &a, const PPSPixelIndex &b)
Definition: CTPPSPixelDataFormatter.h:91
CTPPSPixelDataFormatter::m_Mapping
const std::map< CTPPSPixelFramePosition, CTPPSPixelROCInfo > & m_Mapping
Definition: CTPPSPixelDataFormatter.h:108
CTPPSPixelDataFormatter::m_ROC_mask
Word32 m_ROC_mask
Definition: CTPPSPixelDataFormatter.h:102
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
pps::pixel::ElectronicIndex::link
int link
Definition: ElectronicIndex.h:6
CTPPSPixelROC::rawId
uint32_t rawId() const
return the DetUnit to which this ROC belongs to.
Definition: CTPPSPixelROC.h:30
InvalidROCId
Definition: RPixErrorChecker.h:15
CTPPSPixelROC.h
CTPPSPixelDataFormatter::m_PXID_shift
int m_PXID_shift
Definition: CTPPSPixelDataFormatter.h:101
CTPPSPixelDataFormatter::Digis
std::unordered_map< cms_uint32_t, DetDigis > Digis
Definition: CTPPSPixelDataFormatter.h:66
l1tstage2_dqm_sourceclient-live_cfg.fedId
fedId
Definition: l1tstage2_dqm_sourceclient-live_cfg.py:88
CTPPSPixelDataFormatter::m_ADC_mask
Word32 m_ADC_mask
Definition: CTPPSPixelDataFormatter.h:102
FEDTrailer::set
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
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
CTPPSPixelDataFormatter::m_LINK_shift
int m_LINK_shift
Definition: CTPPSPixelDataFormatter.h:101
CTPPSPixelROC
Definition: CTPPSPixelROC.h:18
CTPPSPixelDataFormatter
Definition: CTPPSPixelDataFormatter.h:53
RPixErrorChecker::checkROC
bool checkROC(bool &errorsInEvent, int fedId, uint32_t iD, const Word32 &errorWord, Errors &errors) const
Definition: RPixErrorChecker.cc:68
std
Definition: JetResolutionObject.h:76
RPixErrorChecker::checkCRC
bool checkCRC(bool &errorsInEvent, int fedId, const Word64 *trailer, Errors &errors) const
Definition: RPixErrorChecker.cc:16
CTPPSPixelDataFormatter::m_IncludeErrors
bool m_IncludeErrors
Definition: CTPPSPixelDataFormatter.h:98
LIKELY
#define LIKELY(x)
Definition: Likely.h:20
Exception
Definition: hltDiff.cc:245
pps::pixel::ElectronicIndex::roc
int roc
Definition: ElectronicIndex.h:7
FEDHeader::set
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
CTPPSPixelDataFormatter::m_LINK_mask
Word32 m_LINK_mask
Definition: CTPPSPixelDataFormatter.h:102
CTPPSPixelDataFormatter::m_ADC_shift
int m_ADC_shift
Definition: CTPPSPixelDataFormatter.h:101
PixelMapPlotter.roc
roc
Definition: PixelMapPlotter.py:498
edm::DetSet::data
collection_type data
Definition: DetSet.h:80
Exception.h
RPixErrorChecker::checkTrailer
bool checkTrailer(bool &errorsInEvent, int fedId, unsigned int nWords, const Word64 *trailer, Errors &errors) const
Definition: RPixErrorChecker.cc:45
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
CTPPSPixelDataFormatter::DetDigis
std::vector< CTPPSPixelDigi > DetDigis
Definition: CTPPSPixelDataFormatter.h:58
edm::DetSetVector::find_or_insert
reference find_or_insert(det_id_type id)
Definition: DetSetVector.h:234
CTPPSPixelDataFormatter::m_hasDetDigis
int m_hasDetDigis
Definition: CTPPSPixelDataFormatter.h:112
CTPPSPixelDataFormatter::m_allDetDigis
int m_allDetDigis
Definition: CTPPSPixelDataFormatter.h:111
CTPPSPixelDataFormatter::print
std::string print(const Word64 &word) const
Definition: CTPPSPixelDataFormatter.cc:281
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:234
FEDHeader.h
CTPPSPixelDataFormatter::formatRawData
void formatRawData(unsigned int lvl1_ID, RawData &fedRawData, const Digis &digis, std::vector< PPSPixelIndex > v_iDdet2fed)
Definition: CTPPSPixelDataFormatter.cc:189
CTPPSPixelDataFormatter::interpretRawData
void interpretRawData(bool &errorsInEvent, int fedId, const FEDRawData &data, Collection &digis, Errors &errors)
Definition: CTPPSPixelDataFormatter.cc:63
taus_updatedMVAIds_cff.mapping
mapping
Definition: taus_updatedMVAIds_cff.py:29
CTPPSPixelROCInfo
Contains mappind data related to a ROC.
Definition: CTPPSPixelDAQMapping.h:23
CTPPSPixelDataFormatter::Word32
uint32_t Word32
Definition: CTPPSPixelDataFormatter.h:63
CTPPSPixelDataFormatter::Word64
uint64_t Word64
Definition: CTPPSPixelDataFormatter.h:64
CTPPSPixelDataFormatter::m_PXID_mask
Word32 m_PXID_mask
Definition: CTPPSPixelDataFormatter.h:102
FEDTrailer.h