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  constexpr int ADC_shift = 0;
33  constexpr int PXID_shift = ADC_shift + ADC_bits;
34  constexpr int DCOL_shift = PXID_shift + PXID_bits;
35  constexpr int ROC_shift = DCOL_shift + DCOL_bits;
36  constexpr int LINK_shift = ROC_shift + ROC_bits;
37 
38  constexpr PixelDataFormatter::Word32 LINK_mask = ~(~PixelDataFormatter::Word32(0) << LINK_bits);
39  constexpr PixelDataFormatter::Word32 ROC_mask = ~(~PixelDataFormatter::Word32(0) << ROC_bits);
40  constexpr PixelDataFormatter::Word32 DCOL_mask = ~(~PixelDataFormatter::Word32(0) << DCOL_bits);
41  constexpr PixelDataFormatter::Word32 PXID_mask = ~(~PixelDataFormatter::Word32(0) << PXID_bits);
42  constexpr PixelDataFormatter::Word32 ADC_mask = ~(~PixelDataFormatter::Word32(0) << ADC_bits);
43 
44 }
45 
47  : theDigiCounter(0), theWordCounter(0), theCablingTree(map), badPixelInfo(0), modulesToUnpack(0)
48 {
49  int s32 = sizeof(Word32);
50  int s64 = sizeof(Word64);
51  int s8 = sizeof(char);
52  if ( s8 != 1 || s32 != 4*s8 || s64 != 2*s32) {
53  LogError("UnexpectedSizes")
54  <<" unexpected sizes: "
55  <<" size of char is: " << s8
56  <<", size of Word32 is: " << s32
57  <<", size of Word64 is: " << s64
58  <<", send exception" ;
59  }
60  includeErrors = false;
61  useQualityInfo = false;
62  allDetDigis = 0;
63  hasDetDigis = 0;
64 }
65 
66 void PixelDataFormatter::setErrorStatus(bool ErrorStatus)
67 {
68  includeErrors = ErrorStatus;
70 }
71 
72 void PixelDataFormatter::setQualityStatus(bool QualityStatus, const SiPixelQuality* QualityInfo)
73 {
74  useQualityInfo = QualityStatus;
75  badPixelInfo = QualityInfo;
76 }
77 
78 void PixelDataFormatter::setModulesToUnpack(const std::set<unsigned int> * moduleIds)
79 {
80  modulesToUnpack = moduleIds;
81 }
82 
84 {
85  theFrameReverter = reverter;
86 }
87 
88 void PixelDataFormatter::interpretRawData(bool& errorsInEvent, int fedId, const FEDRawData& rawData, Collection & digis, Errors& errors)
89 {
90  using namespace sipixelobjects;
91 
92  int nWords = rawData.size()/sizeof(Word64);
93  if (nWords==0) return;
94 
95  SiPixelFrameConverter converter(theCablingTree, fedId);
96 
97  // check CRC bit
98  const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1);
99  if(!errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors)) return;
100 
101  // check headers
102  const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
103  bool moreHeaders = true;
104  while (moreHeaders) {
105  header++;
106  LogTrace("")<<"HEADER: " << print(*header);
107  bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors);
108  moreHeaders = headerStatus;
109  }
110 
111  // check trailers
112  bool moreTrailers = true;
113  trailer++;
114  while (moreTrailers) {
115  trailer--;
116  LogTrace("")<<"TRAILER: " << print(*trailer);
117  bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
118  moreTrailers = trailerStatus;
119  }
120 
121  // data words
122  theWordCounter += 2*(nWords-2);
123  LogTrace("")<<"data words: "<< (trailer-header-1);
124 
125  int link = -1;
126  int roc = -1;
127  PixelROC const * rocp=nullptr;
128  bool skipROC=false;
129  edm::DetSet<PixelDigi> * detDigis=nullptr;
130 
131  const Word32 * bw =(const Word32 *)(header+1);
132  const Word32 * ew =(const Word32 *)(trailer);
133  if ( *(ew-1) == 0 ) { ew--; theWordCounter--;}
134  for (auto word = bw; word < ew; ++word) {
135  LogTrace("")<<"DATA: " << print(*word);
136 
137  auto ww = *word;
138  //assert(ww==0);
139  if unlikely(ww==0) { theWordCounter--; continue;}
140  int nlink = (ww >> LINK_shift) & LINK_mask;
141  int nroc = (ww >> ROC_shift) & ROC_mask;
142 
143  if ( (nlink!=link) | (nroc!=roc) ) { // new roc
144  link = nlink; roc=nroc;
145  skipROC = likely(roc<25) ? false : !errorcheck.checkROC(errorsInEvent, fedId, &converter, ww, errors);
146  if (skipROC) continue;
147  rocp = converter.toRoc(link,roc);
148  if unlikely(!rocp) {
149  errorsInEvent = true;
150  errorcheck.conversionError(fedId, &converter, 2, ww, errors);
151  skipROC=true;
152  continue;
153  }
154  auto rawId = rocp->rawId();
155 
156  if (useQualityInfo&(nullptr!=badPixelInfo)) {
157  short rocInDet = (short) rocp->idInDetUnit();
158  skipROC = badPixelInfo->IsRocBad(rawId, rocInDet);
159  if (skipROC) continue;
160  }
161  skipROC= modulesToUnpack && ( modulesToUnpack->find(rawId) == modulesToUnpack->end());
162  if (skipROC) continue;
163 
164  detDigis = &digis.find_or_insert(rawId);
165  if ( (*detDigis).empty() ) (*detDigis).data.reserve(32); // avoid the first relocations
166  }
167  if unlikely(skipROC) continue;
168 
169 
170  int dcol = (ww >> DCOL_shift) & DCOL_mask;
171  int pxid = (ww >> PXID_shift) & PXID_mask;
172  int adc = (ww >> ADC_shift) & ADC_mask;
173 
174  LocalPixel::DcolPxid local = { dcol, pxid };
175  if unlikely(!local.valid()) {
176  LogDebug("PixelDataFormatter::interpretRawData")
177  << "status #3";
178  errorsInEvent = true;
179  errorcheck.conversionError(fedId, &converter, 3, ww, errors);
180  continue;
181  }
182 
183  GlobalPixel global = rocp->toGlobal( LocalPixel(local) );
184  (*detDigis).data.emplace_back(global.row, global.col, adc);
185 
186  LogTrace("") << (*detDigis).data.back();
187  }
188 
189 }
190 
191 void doVectorize(int const * __restrict__ w, int * __restrict__ row, int * __restrict__ col, int * __restrict__ valid, int N, PixelROC const * rocp) {
192  for (int i=0; i<N; ++i) {
193  auto ww = w[i];
194  int dcol = (ww >> DCOL_shift) & DCOL_mask;
195  int pxid = (ww >> PXID_shift) & PXID_mask;
196  // int adc = (ww >> ADC_shift) & ADC_mask;
197 
198  LocalPixel::DcolPxid local = { dcol, pxid };
199  valid[i] = local.valid();
200  GlobalPixel global = rocp->toGlobal( LocalPixel(local) );
201  row[i]=global.row; col[i]=global.col;
202 
203  }
204 
205 }
206 
207 
208 void PixelDataFormatter::formatRawData(unsigned int lvl1_ID, RawData & fedRawData, const Digis & digis)
209 {
210  std::map<int, vector<Word32> > words;
211 
212  // translate digis into 32-bit raw words and store in map indexed by Fed
213  for (Digis::const_iterator im = digis.begin(); im != digis.end(); im++) {
214  allDetDigis++;
215  cms_uint32_t rawId = im->first;
216  hasDetDigis++;
217  const DetDigis & detDigis = im->second;
218  for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); it++) {
219  theDigiCounter++;
220  const PixelDigi & digi = (*it);
221  int status = digi2word( rawId, digi, words);
222  if (status) {
223  LogError("FormatDataException")
224  <<" digi2word returns error #"<<status
225  <<" Ndigis: "<<theDigiCounter << endl
226  <<" detector: "<<rawId<< endl
227  << print(digi) <<endl;
228  } // if (status)
229  } // for (DetDigis
230  } // for (Digis
231  LogTrace(" allDetDigis/hasDetDigis : ") << allDetDigis<<"/"<<hasDetDigis;
232 
233  typedef std::map<int, vector<Word32> >::const_iterator RI;
234  for (RI feddata = words.begin(); feddata != words.end(); feddata++) {
235  int fedId = feddata->first;
236  // since raw words are written in the form of 64-bit packets
237  // add extra 32-bit word to make number of words even if necessary
238  if (words.find(fedId)->second.size() %2 != 0) words[fedId].push_back( Word32(0) );
239 
240  // size in Bytes; create output structure
241  int dataSize = words.find(fedId)->second.size() * sizeof(Word32);
242  int nHeaders = 1;
243  int nTrailers = 1;
244  dataSize += (nHeaders+nTrailers)*sizeof(Word64);
245  FEDRawData * rawData = new FEDRawData(dataSize);
246 
247  // get begining of data;
248  Word64 * word = reinterpret_cast<Word64* >(rawData->data());
249 
250  // write one header
251  FEDHeader::set( reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
252  word++;
253 
254  // write data
255  unsigned int nWord32InFed = words.find(fedId)->second.size();
256  for (unsigned int i=0; i < nWord32InFed; i+=2) {
257  *word = (Word64(words.find(fedId)->second[i]) << 32 ) | words.find(fedId)->second[i+1];
258  LogDebug("PixelDataFormatter") << print(*word);
259  word++;
260  }
261 
262  // write one trailer
263  FEDTrailer::set( reinterpret_cast<unsigned char*>(word), dataSize/sizeof(Word64), 0,0,0);
264  word++;
265 
266  // check memory
267  if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
268  string s = "** PROBLEM in PixelDataFormatter !!!";
269  throw cms::Exception(s);
270  } // if (word !=
271  fedRawData[fedId] = *rawData;
272  delete rawData;
273  } // for (RI feddata
274 }
275 
277  std::map<int, vector<Word32> > & words) const
278 {
279  LogDebug("PixelDataFormatter")
280 // <<" detId: " << detId
281  <<print(digi);
282 
283  DetectorIndex detector = {detId, digi.row(), digi.column()};
284  ElectronicIndex cabling;
285  int fedId = theFrameReverter->toCabling(cabling, detector);
286  if (fedId<0) return fedId;
287 
288  Word32 word =
289  (cabling.link << LINK_shift)
290  | (cabling.roc << ROC_shift)
291  | (cabling.dcol << DCOL_shift)
292  | (cabling.pxid << PXID_shift)
293  | (digi.adc() << ADC_shift);
294  words[fedId].push_back(word);
295  theWordCounter++;
296 
297  return 0;
298 }
299 
300 
301 // obsolete...
302 int PixelDataFormatter::word2digi(const int fedId, const SiPixelFrameConverter* converter,
303  const bool includeErrors, const bool useQuality, const Word32 & word, Digis & digis) const
304 {
305  // do not interpret false digis
306  if (word == 0 ) return 0;
307 
308  ElectronicIndex cabling;
309  cabling.dcol = (word >> DCOL_shift) & DCOL_mask;
310  cabling.pxid = (word >> PXID_shift) & PXID_mask;
311  cabling.link = (word >> LINK_shift) & LINK_mask;
312  cabling.roc = (word >> ROC_shift) & ROC_mask;
313  int adc = (word >> ADC_shift) & ADC_mask;
314 
315  if (debug) {
316  LocalPixel::DcolPxid pixel = {cabling.dcol,cabling.pxid};
317  LocalPixel local(pixel);
318  LogTrace("")<<" link: "<<cabling.link<<", roc: "<<cabling.roc
319  <<" rocRow: "<<local.rocRow()<<", rocCol:"<<local.rocCol()
320  <<" (dcol: "<<cabling.dcol<<", pxid:"<<cabling.pxid<<"), adc:"<<adc;
321  }
322 
323  if (!converter) return 0;
324 
325  DetectorIndex detIdx;
326  int status = converter->toDetector(cabling, detIdx);
327  if (status) return status;
328 
329  // exclude ROC(raw) based on bad ROC list bad in SiPixelQuality
330  // enable: process.siPixelDigis.UseQualityInfo = True
331  // 20-10-2010 A.Y.
332  if (useQuality&&badPixelInfo) {
333  CablingPathToDetUnit path = {static_cast<unsigned int>(fedId),
334  static_cast<unsigned int>(cabling.link),
335  static_cast<unsigned int>(cabling.roc)};
336  const PixelROC * roc = theCablingTree->findItem(path);
337  short rocInDet = (short) roc->idInDetUnit();
338  bool badROC = badPixelInfo->IsRocBad(detIdx.rawId, rocInDet);
339  if (badROC) return 0;
340  }
341 
342  if (modulesToUnpack && modulesToUnpack->find(detIdx.rawId) == modulesToUnpack->end()) return 0;
343 
344  digis[detIdx.rawId].emplace_back(detIdx.row, detIdx.col, adc);
345 
346  theDigiCounter++;
347 
348  if (debug) LogTrace("") << digis[detIdx.rawId].back();
349  return 0;
350 }
351 
353 {
354  ostringstream str;
355  str << " DIGI: row: " << digi.row() <<", col: " << digi.column() <<", adc: " << digi.adc();
356  return str.str();
357 }
358 
360 {
361  ostringstream str;
362  str <<"word64: " << reinterpret_cast<const bitset<64>&> (word);
363  return str.str();
364 }
365 
#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
const double w
Definition: UKUtility.cc:23
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:256
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)
std::string link(std::string &nm, std::string &ns)
Definition: hierarchy.cc:24
#define likely(x)
void doVectorize(int const *__restrict__ w, int *__restrict__ row, int *__restrict__ col, int *__restrict__ valid, int N, PixelROC const *rocp)
tuple path
else: Piece not in the list, fine.
bool checkCRC(bool &errorsInEvent, int fedId, const Word64 *trailer, Errors &errors)
Definition: ErrorChecker.cc:56
PixelDataFormatter(const SiPixelFedCabling *map)
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)
#define N
Definition: blowfish.cc:9
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
int col
Definition: cuy.py:1008
GlobalPixel toGlobal(const LocalPixel &loc) const
Definition: PixelROC.h:58
bool checkHeader(bool &errorsInEvent, int fedId, const Word64 *header, Errors &errors)
Definition: ErrorChecker.cc:69