CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PixelDataFormatter.cc
Go to the documentation of this file.
2 
6 
8 
12 
14 
17 
18 #include <bitset>
19 #include <sstream>
20 #include <iostream>
21 
22 using namespace std;
23 using namespace edm;
24 using namespace sipixelobjects;
25 namespace {
26  constexpr int LINK_bits = 6;
27  constexpr int ROC_bits = 5;
28  constexpr int DCOL_bits = 5;
29  constexpr int PXID_bits = 8;
30  constexpr int ADC_bits = 8;
31 
32  // For phase1
33  constexpr int LINK_bits1 = 7;
34  constexpr int ROC_bits1 = 4;
35 
36  // Moved to the header file, keep commented out unti the final version is done/
37  // constexpr int ADC_shift = 0;
38  // constexpr int PXID_shift = ADC_shift + ADC_bits;
39  // constexpr int DCOL_shift = PXID_shift + PXID_bits;
40  // constexpr int ROC_shift = DCOL_shift + DCOL_bits;
41  // constexpr int LINK_shift = ROC_shift + ROC_bits;
42  // constexpr PixelDataFormatter::Word32 LINK_mask = ~(~PixelDataFormatter::Word32(0) << LINK_bits);
43  // constexpr PixelDataFormatter::Word32 ROC_mask = ~(~PixelDataFormatter::Word32(0) << ROC_bits);
44  // constexpr PixelDataFormatter::Word32 DCOL_mask = ~(~PixelDataFormatter::Word32(0) << DCOL_bits);
45  // constexpr PixelDataFormatter::Word32 PXID_mask = ~(~PixelDataFormatter::Word32(0) << PXID_bits);
46  // constexpr PixelDataFormatter::Word32 ADC_mask = ~(~PixelDataFormatter::Word32(0) << ADC_bits);
47 
48 }
49 
51  : theDigiCounter(0), theWordCounter(0), theCablingTree(map), badPixelInfo(0), modulesToUnpack(0)
52 {
53  int s32 = sizeof(Word32);
54  int s64 = sizeof(Word64);
55  int s8 = sizeof(char);
56  if ( s8 != 1 || s32 != 4*s8 || s64 != 2*s32) {
57  LogError("UnexpectedSizes")
58  <<" unexpected sizes: "
59  <<" size of char is: " << s8
60  <<", size of Word32 is: " << s32
61  <<", size of Word64 is: " << s64
62  <<", send exception" ;
63  }
64  includeErrors = false;
65  useQualityInfo = false;
66  allDetDigis = 0;
67  hasDetDigis = 0;
68 
69  ADC_shift = 0;
70  PXID_shift = ADC_shift + ADC_bits;
71  DCOL_shift = PXID_shift + PXID_bits;
72  ROC_shift = DCOL_shift + DCOL_bits;
73 
74  if(phase1) { // for phase 1
75  LINK_shift = ROC_shift + ROC_bits1;
76  LINK_mask = ~(~PixelDataFormatter::Word32(0) << LINK_bits1);
77  ROC_mask = ~(~PixelDataFormatter::Word32(0) << ROC_bits1);
78  maxROCIndex=8;
79  } else { // for phase 0
80  LINK_shift = ROC_shift + ROC_bits;
81  LINK_mask = ~(~PixelDataFormatter::Word32(0) << LINK_bits);
82  ROC_mask = ~(~PixelDataFormatter::Word32(0) << ROC_bits);
83  maxROCIndex=25;
84  }
85 
86 
87  DCOL_mask = ~(~PixelDataFormatter::Word32(0) << DCOL_bits);
88  PXID_mask = ~(~PixelDataFormatter::Word32(0) << PXID_bits);
89  ADC_mask = ~(~PixelDataFormatter::Word32(0) << ADC_bits);
90 
91 }
92 
93 void PixelDataFormatter::setErrorStatus(bool ErrorStatus)
94 {
95  includeErrors = ErrorStatus;
97 }
98 
99 void PixelDataFormatter::setQualityStatus(bool QualityStatus, const SiPixelQuality* QualityInfo)
100 {
101  useQualityInfo = QualityStatus;
102  badPixelInfo = QualityInfo;
103 }
104 
105 void PixelDataFormatter::setModulesToUnpack(const std::set<unsigned int> * moduleIds)
106 {
107  modulesToUnpack = moduleIds;
108 }
109 
111 {
112  theFrameReverter = reverter;
113 }
114 
115 void PixelDataFormatter::interpretRawData(bool& errorsInEvent, int fedId, const FEDRawData& rawData, Collection & digis, Errors& errors)
116 {
117  using namespace sipixelobjects;
118 
119  int nWords = rawData.size()/sizeof(Word64);
120  if (nWords==0) return;
121 
122  SiPixelFrameConverter converter(theCablingTree, fedId);
123 
124  // check CRC bit
125  const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1);
126  if(!errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors)) return;
127 
128  // check headers
129  const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
130  bool moreHeaders = true;
131  while (moreHeaders) {
132  header++;
133  LogTrace("")<<"HEADER: " << print(*header);
134  bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors);
135  moreHeaders = headerStatus;
136  }
137 
138  // check trailers
139  bool moreTrailers = true;
140  trailer++;
141  while (moreTrailers) {
142  trailer--;
143  LogTrace("")<<"TRAILER: " << print(*trailer);
144  bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
145  moreTrailers = trailerStatus;
146  }
147 
148  // data words
149  theWordCounter += 2*(nWords-2);
150  LogTrace("")<<"data words: "<< (trailer-header-1);
151 
152  int link = -1;
153  int roc = -1;
154  PixelROC const * rocp=nullptr;
155  bool skipROC=false;
156  edm::DetSet<PixelDigi> * detDigis=nullptr;
157 
158  const Word32 * bw =(const Word32 *)(header+1);
159  const Word32 * ew =(const Word32 *)(trailer);
160  if ( *(ew-1) == 0 ) { ew--; theWordCounter--;}
161  for (auto word = bw; word < ew; ++word) {
162  LogTrace("")<<"DATA: " << print(*word);
163 
164  auto ww = *word;
165  //assert(ww==0);
166  if unlikely(ww==0) { theWordCounter--; continue;}
167  int nlink = (ww >> LINK_shift) & LINK_mask;
168  int nroc = (ww >> ROC_shift) & ROC_mask;
169 
170  if ( (nlink!=link) | (nroc!=roc) ) { // new roc
171  link = nlink; roc=nroc;
172  skipROC = likely(roc<maxROCIndex) ? false : !errorcheck.checkROC(errorsInEvent, fedId, &converter, ww, errors);
173  if (skipROC) continue;
174  rocp = converter.toRoc(link,roc);
175  if unlikely(!rocp) {
176  errorsInEvent = true;
177  errorcheck.conversionError(fedId, &converter, 2, ww, errors);
178  skipROC=true;
179  continue;
180  }
181  auto rawId = rocp->rawId();
182 
183  if (useQualityInfo&(nullptr!=badPixelInfo)) {
184  short rocInDet = (short) rocp->idInDetUnit();
185  skipROC = badPixelInfo->IsRocBad(rawId, rocInDet);
186  if (skipROC) continue;
187  }
188  skipROC= modulesToUnpack && ( modulesToUnpack->find(rawId) == modulesToUnpack->end());
189  if (skipROC) continue;
190 
191  detDigis = &digis.find_or_insert(rawId);
192  if ( (*detDigis).empty() ) (*detDigis).data.reserve(32); // avoid the first relocations
193  }
194  if unlikely(skipROC) continue;
195 
196 
197  int dcol = (ww >> DCOL_shift) & DCOL_mask;
198  int pxid = (ww >> PXID_shift) & PXID_mask;
199  int adc = (ww >> ADC_shift) & ADC_mask;
200 
201  LocalPixel::DcolPxid local = { dcol, pxid };
202  if unlikely(!local.valid()) {
203  LogDebug("PixelDataFormatter::interpretRawData")
204  << "status #3";
205  errorsInEvent = true;
206  errorcheck.conversionError(fedId, &converter, 3, ww, errors);
207  continue;
208  }
209 
210  GlobalPixel global = rocp->toGlobal( LocalPixel(local) );
211  (*detDigis).data.emplace_back(global.row, global.col, adc);
212 
213  LogTrace("") << (*detDigis).data.back();
214  }
215 
216 }
217 
218 // I do not know what this was for or if it is needed? d.k. 10.14
219 // Keep it commented out until we are sure that it is not needed.
220 // void doVectorize(int const * __restrict__ w, int * __restrict__ row, int * __restrict__ col, int * __restrict__ valid, int N, PixelROC const * rocp) {
221 // for (int i=0; i<N; ++i) {
222 // auto ww = w[i];
223 // int dcol = (ww >> DCOL_shift) & DCOL_mask;
224 // int pxid = (ww >> PXID_shift) & PXID_mask;
225 // // int adc = (ww >> ADC_shift) & ADC_mask;
226 
227 // LocalPixel::DcolPxid local = { dcol, pxid };
228 // valid[i] = local.valid();
229 // GlobalPixel global = rocp->toGlobal( LocalPixel(local) );
230 // row[i]=global.row; col[i]=global.col;
231 
232 // }
233 
234 // }
235 
236 
237 void PixelDataFormatter::formatRawData(unsigned int lvl1_ID, RawData & fedRawData, const Digis & digis)
238 {
239  std::map<int, vector<Word32> > words;
240 
241  // translate digis into 32-bit raw words and store in map indexed by Fed
242  for (Digis::const_iterator im = digis.begin(); im != digis.end(); im++) {
243  allDetDigis++;
244  cms_uint32_t rawId = im->first;
245  hasDetDigis++;
246  const DetDigis & detDigis = im->second;
247  for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); it++) {
248  theDigiCounter++;
249  const PixelDigi & digi = (*it);
250  int status = digi2word( rawId, digi, words);
251  if (status) {
252  LogError("FormatDataException")
253  <<" digi2word returns error #"<<status
254  <<" Ndigis: "<<theDigiCounter << endl
255  <<" detector: "<<rawId<< endl
256  << print(digi) <<endl;
257  } // if (status)
258  } // for (DetDigis
259  } // for (Digis
260  LogTrace(" allDetDigis/hasDetDigis : ") << allDetDigis<<"/"<<hasDetDigis;
261 
262  typedef std::map<int, vector<Word32> >::const_iterator RI;
263  for (RI feddata = words.begin(); feddata != words.end(); feddata++) {
264  int fedId = feddata->first;
265  // since raw words are written in the form of 64-bit packets
266  // add extra 32-bit word to make number of words even if necessary
267  if (words.find(fedId)->second.size() %2 != 0) words[fedId].push_back( Word32(0) );
268 
269  // size in Bytes; create output structure
270  int dataSize = words.find(fedId)->second.size() * sizeof(Word32);
271  int nHeaders = 1;
272  int nTrailers = 1;
273  dataSize += (nHeaders+nTrailers)*sizeof(Word64);
274  FEDRawData * rawData = new FEDRawData(dataSize);
275 
276  // get begining of data;
277  Word64 * word = reinterpret_cast<Word64* >(rawData->data());
278 
279  // write one header
280  FEDHeader::set( reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
281  word++;
282 
283  // write data
284  unsigned int nWord32InFed = words.find(fedId)->second.size();
285  for (unsigned int i=0; i < nWord32InFed; i+=2) {
286  *word = (Word64(words.find(fedId)->second[i]) << 32 ) | words.find(fedId)->second[i+1];
287  LogDebug("PixelDataFormatter") << print(*word);
288  word++;
289  }
290 
291  // write one trailer
292  FEDTrailer::set( reinterpret_cast<unsigned char*>(word), dataSize/sizeof(Word64), 0,0,0);
293  word++;
294 
295  // check memory
296  if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
297  string s = "** PROBLEM in PixelDataFormatter !!!";
298  throw cms::Exception(s);
299  } // if (word !=
300  fedRawData[fedId] = *rawData;
301  delete rawData;
302  } // for (RI feddata
303 }
304 
306  std::map<int, vector<Word32> > & words) const
307 {
308  LogDebug("PixelDataFormatter")
309 // <<" detId: " << detId
310  <<print(digi);
311 
312  DetectorIndex detector = {detId, digi.row(), digi.column()};
313  ElectronicIndex cabling;
314  int fedId = theFrameReverter->toCabling(cabling, detector);
315  if (fedId<0) return fedId;
316 
317  Word32 word =
318  (cabling.link << LINK_shift)
319  | (cabling.roc << ROC_shift)
320  | (cabling.dcol << DCOL_shift)
321  | (cabling.pxid << PXID_shift)
322  | (digi.adc() << ADC_shift);
323  words[fedId].push_back(word);
324  theWordCounter++;
325 
326  return 0;
327 }
328 
329 
330 // obsolete...
332  const bool includeErrors, const bool useQuality, const Word32 & word, Digis & digis) const
333 {
334  // do not interpret false digis
335  if (word == 0 ) return 0;
336 
337  ElectronicIndex cabling;
338  cabling.dcol = (word >> DCOL_shift) & DCOL_mask;
339  cabling.pxid = (word >> PXID_shift) & PXID_mask;
340  cabling.link = (word >> LINK_shift) & LINK_mask;
341  cabling.roc = (word >> ROC_shift) & ROC_mask;
342  int adc = (word >> ADC_shift) & ADC_mask;
343 
344  if (debug) {
345  LocalPixel::DcolPxid pixel = {cabling.dcol,cabling.pxid};
346  LocalPixel local(pixel);
347  LogTrace("")<<" link: "<<cabling.link<<", roc: "<<cabling.roc
348  <<" rocRow: "<<local.rocRow()<<", rocCol:"<<local.rocCol()
349  <<" (dcol: "<<cabling.dcol<<", pxid:"<<cabling.pxid<<"), adc:"<<adc;
350  }
351 
352  if (!converter) return 0;
353 
354  DetectorIndex detIdx;
355  int status = converter->toDetector(cabling, detIdx);
356  if (status) return status;
357 
358  // exclude ROC(raw) based on bad ROC list bad in SiPixelQuality
359  // enable: process.siPixelDigis.UseQualityInfo = True
360  // 20-10-2010 A.Y.
361  if (useQuality&&badPixelInfo) {
362  CablingPathToDetUnit path = {static_cast<unsigned int>(fedId),
363  static_cast<unsigned int>(cabling.link),
364  static_cast<unsigned int>(cabling.roc)};
365  const PixelROC * roc = theCablingTree->findItem(path);
366  short rocInDet = (short) roc->idInDetUnit();
367  bool badROC = badPixelInfo->IsRocBad(detIdx.rawId, rocInDet);
368  if (badROC) return 0;
369  }
370 
371  if (modulesToUnpack && modulesToUnpack->find(detIdx.rawId) == modulesToUnpack->end()) return 0;
372 
373  digis[detIdx.rawId].emplace_back(detIdx.row, detIdx.col, adc);
374 
375  theDigiCounter++;
376 
377  if (debug) LogTrace("") << digis[detIdx.rawId].back();
378  return 0;
379 }
380 
382 {
383  ostringstream str;
384  str << " DIGI: row: " << digi.row() <<", col: " << digi.column() <<", adc: " << digi.adc();
385  return str.str();
386 }
387 
389 {
390  ostringstream str;
391  str <<"word64: " << reinterpret_cast<const bitset<64>&> (word);
392  return str.str();
393 }
394 
#define LogDebug(id)
int adc(sample_type sample)
get the ADC sample (12 bits)
int row() const
Definition: PixelDigi.h:56
int i
Definition: DBlmapReader.cc:9
std::map< cms_uint32_t, DetDigis > Digis
bool checkTrailer(bool &errorsInEvent, int fedId, int nWords, const Word64 *trailer, Errors &errors)
Definition: ErrorChecker.cc:87
virtual const sipixelobjects::PixelROC * findItem(const sipixelobjects::CablingPathToDetUnit &) const =0
void passFrameReverter(const SiPixelFrameReverter *reverter)
void setErrorStatus(bool ErrorStatus)
bool IsRocBad(const uint32_t &detid, const short &rocNb) const
static void set(unsigned char *trailer, int evt_lgth, int crc, int evt_stat, int tts, bool T=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:42
#define constexpr
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
identify pixel inside single ROC
Definition: LocalPixel.h:7
int toCabling(sipixelobjects::ElectronicIndex &cabling, const sipixelobjects::DetectorIndex &detector) const
reference find_or_insert(det_id_type id)
Definition: DetSetVector.h:254
bool checkROC(bool &errorsInEvent, int fedId, const SiPixelFrameConverter *converter, Word32 &errorWord, Errors &errors)
global coordinates (row and column in DetUnit, as in PixelDigi)
Definition: GlobalPixel.h:6
#define unlikely(x)
#define likely(x)
bool checkCRC(bool &errorsInEvent, int fedId, const Word64 *trailer, Errors &errors)
Definition: ErrorChecker.cc:56
static void set(unsigned char *header, int evt_ty, int lvl1_ID, int bx_ID, int source_ID, int version=0, bool H=false)
Set all fields in the header.
Definition: FEDHeader.cc:40
unsigned int idInDetUnit() const
id of this ROC in DetUnit etermined by token path
Definition: PixelROC.h:39
std::string print(const PixelDigi &digi) const
unsigned short adc() const
Definition: PixelDigi.h:59
void formatRawData(unsigned int lvl1_ID, RawData &fedRawData, const Digis &digis)
void setQualityStatus(bool QualityStatus, const SiPixelQuality *QualityInfo)
const std::set< unsigned int > * modulesToUnpack
const SiPixelQuality * badPixelInfo
void setErrorStatus(bool ErrorStatus)
Definition: ErrorChecker.cc:51
unsigned int cms_uint32_t
Definition: typedefs.h:15
#define LogTrace(id)
double collumn and pixel ID in double collumn representation
Definition: LocalPixel.h:22
std::map< cms_uint32_t, DetErrors > Errors
void interpretRawData(bool &errorsInEvent, int fedId, const FEDRawData &data, Collection &digis, Errors &errors)
void setModulesToUnpack(const std::set< unsigned int > *moduleIds)
const SiPixelFrameReverter * theFrameReverter
void conversionError(int fedId, const SiPixelFrameConverter *converter, int status, Word32 &errorWord, Errors &errors)
std::map< int, FEDRawData > RawData
std::vector< PixelDigi > DetDigis
int digi2word(cms_uint32_t detId, const PixelDigi &digi, std::map< int, std::vector< Word32 > > &words) const
collection_type data
Definition: DetSet.h:78
sipixelobjects::PixelROC const * toRoc(int link, int roc) const
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
int toDetector(const sipixelobjects::ElectronicIndex &cabling, sipixelobjects::DetectorIndex &detector) const
int word2digi(const int fedId, const SiPixelFrameConverter *converter, const bool includeError, const bool useQuality, const Word32 &word, Digis &digis) const
tuple status
Definition: ntuplemaker.py:245
SiPixelFedCabling const * theCablingTree
int column() const
Definition: PixelDigi.h:57
PixelDataFormatter(const SiPixelFedCabling *map, bool phase1=false)
bool checkHeader(bool &errorsInEvent, int fedId, const Word64 *header, Errors &errors)
Definition: ErrorChecker.cc:69