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
117  UNLIKELY(ww == 0) {
118  m_WordCounter--;
119  continue;
120  }
121  int nlink = (ww >> m_LINK_shift) & m_LINK_mask;
122  int nroc = (ww >> m_ROC_shift) & m_ROC_mask;
123 
124  int FMC = 0;
125  uint32_t iD = RPixErrorChecker::dummyDetId; //0xFFFFFFFF; //dummyDetId
126  int convroc = nroc - 1;
127  CTPPSPixelFramePosition fPos(fedId, FMC, nlink, convroc);
128  std::map<CTPPSPixelFramePosition, CTPPSPixelROCInfo>::const_iterator mit;
129  mit = m_Mapping.find(fPos);
130 
131  if (mit == m_Mapping.end()) {
132  if (nlink >= maxLinkIndex) {
134  } else if ((nroc - 1) >= maxRocIndex) {
136  } else {
138  }
139  continue; //skip word
140  }
141 
142  CTPPSPixelROCInfo rocInfo = (*mit).second;
143  iD = rocInfo.iD;
144  CTPPSPixelROC rocp(iD, rocInfo.roc, convroc);
145 
146  if ((nlink != link) | (nroc != roc)) { // new roc
147  link = nlink;
148  roc = nroc;
149 
150  skipROC = LIKELY((roc - 1) < maxRocIndex) ? false : !m_ErrorCheck.checkROC(errorsInEvent, fedId, iD, ww, errors);
151  if (skipROC)
152  continue;
153 
154  auto rawId = rocp.rawId();
155 
156  detDigis = &digis.find_or_insert(rawId);
157  if ((*detDigis).empty())
158  (*detDigis).data.reserve(32); // avoid the first relocations
159  }
160 
161  int adc = (ww >> m_ADC_shift) & m_ADC_mask;
162 
163  int dcol = (ww >> m_DCOL_shift) & m_DCOL_mask;
164  int pxid = (ww >> m_PXID_shift) & m_PXID_mask;
165 
166  if (dcol < min_Dcol || dcol > max_Dcol || pxid < min_Pixid || pxid > max_Pixid) {
167  edm::LogError("CTPPSPixelDataFormatter")
168  << " unphysical dcol and/or pxid "
169  << " nllink=" << nlink << " nroc=" << nroc << " adc=" << adc << " dcol=" << dcol << " pxid=" << pxid;
170 
172 
173  continue;
174  }
175 
176  std::pair<int, int> rocPixel;
177  std::pair<int, int> modPixel;
178 
179  rocPixel = std::make_pair(dcol, pxid);
180 
181  modPixel = rocp.toGlobalfromDcol(rocPixel);
182 
183  CTPPSPixelDigi testdigi(modPixel.first, modPixel.second, adc);
184 
185  if (detDigis)
186  (*detDigis).data.emplace_back(modPixel.first, modPixel.second, adc);
187  }
188 }
189 
190 void CTPPSPixelDataFormatter::formatRawData(unsigned int lvl1_ID,
192  const Digis& digis,
193  std::vector<PPSPixelIndex> iDdet2fed) {
194  std::map<int, vector<Word32> > words;
195  // translate digis into 32-bit raw words and store in map indexed by Fed
196  m_allDetDigis = 0;
197  m_hasDetDigis = 0;
198  for (auto const& im : digis) {
199  m_allDetDigis++;
200  cms_uint32_t rawId = im.first;
201 
202  const DetDigis& detDigis = im.second;
203  for (auto const& it : detDigis) {
204  int nroc = 999, nlink = 999;
205  int rocPixelRow = -1, rocPixelColumn = -1, rocID = -1;
206  int modulePixelColumn = it.column();
207  int modulePixelRow = it.row();
208 
209  m_Indices.transformToROC(modulePixelColumn, modulePixelRow, rocID, rocPixelColumn, rocPixelRow);
210  const int dcol = m_Indices.DColumn(rocPixelColumn);
211  const int pxid = 2 * (ROCSizeInX - rocPixelRow) + (rocPixelColumn % 2);
212 
213  unsigned int urocID = rocID;
214  PPSPixelIndex myTest = {rawId, urocID, 0, 0, 0};
215  // the range has always at most one element
216  auto range = std::equal_range(iDdet2fed.begin(), iDdet2fed.end(), myTest, compare);
217  if (range.first != range.second) {
218  auto i = range.first - iDdet2fed.begin();
219  nlink = iDdet2fed.at(i).fedch;
220  nroc = iDdet2fed.at(i).rocch + 1;
221 
222  pps::pixel::ElectronicIndex cabling = {nlink, nroc, dcol, pxid};
223 
224  cms_uint32_t word = (cabling.link << m_LINK_shift) | (cabling.roc << m_ROC_shift) |
225  (cabling.dcol << m_DCOL_shift) | (cabling.pxid << m_PXID_shift) | (it.adc() << m_ADC_shift);
226 
227  words[iDdet2fed.at(i).fedid].push_back(word);
228  m_WordCounter++;
229  m_hasDetDigis++;
230 
231  } // range
232  } // for DetDigis
233  } // for Digis
234 
235  LogTrace(" allDetDigis/hasDetDigis : ") << m_allDetDigis << "/" << m_hasDetDigis;
236  for (auto const& feddata : words) {
237  int fedId = feddata.first;
238 
239  // since raw words are written in the form of 64-bit packets
240  // add extra 32-bit word to make number of words even if necessary
241  if (words.find(fedId)->second.size() % 2 != 0)
242  words[fedId].push_back(Word32(0));
243 
244  // size in Bytes; create output structure
245  size_t dataSize = words.find(fedId)->second.size() * sizeof(Word32);
246  int nHeaders = 1;
247  int nTrailers = 1;
248  dataSize += (nHeaders + nTrailers) * sizeof(Word64);
249 
250  FEDRawData rawData{dataSize};
251 
252  // get begining of data;
253  Word64* word = reinterpret_cast<Word64*>(rawData.data());
254 
255  // write one header
256  FEDHeader::set(reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
257  word++;
258 
259  // write data
260  unsigned int nWord32InFed = words.find(fedId)->second.size();
261  for (unsigned int i = 0; i < nWord32InFed; i += 2) {
262  *word = (Word64(words.find(fedId)->second[i]) << 32) | words.find(fedId)->second[i + 1];
263  LogDebug("CTPPSPixelDataFormatter") << print(*word);
264  word++;
265  }
266 
267  // write one trailer
268  FEDTrailer::set(reinterpret_cast<unsigned char*>(word), dataSize / sizeof(Word64), 0, 0, 0);
269  word++;
270 
271  // check memory
272  if (word != reinterpret_cast<Word64*>(rawData.data() + dataSize)) {
273  //if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
274  string s = "** PROBLEM in CTPPSPixelDataFormatter !!!";
275  LogError("CTPPSPixelDataFormatter") << "** PROBLEM in CTPPSPixelDataFormatter!!!";
276  throw cms::Exception(s);
277  } // if (word !=
279  } // for (RI feddata
280 }
281 
283  std::ostringstream str;
284  str << "word64: " << reinterpret_cast<const std::bitset<64>&>(word);
285  return str.str();
286 }
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
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
ecalLiteDTU::adc
constexpr int adc(sample_type sample)
get the ADC sample (12 bits)
Definition: EcalLiteDTUSample.h:12
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:140
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:51
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:30
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:223
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:72
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:246
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:47
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:282
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:224
FEDHeader.h
CTPPSPixelDataFormatter::formatRawData
void formatRawData(unsigned int lvl1_ID, RawData &fedRawData, const Digis &digis, std::vector< PPSPixelIndex > v_iDdet2fed)
Definition: CTPPSPixelDataFormatter.cc:190
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