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 
26 const int LINK_bits = 6;
27 const int ROC_bits = 5;
28 const int DCOL_bits = 5;
29 const int PXID_bits = 8;
30 const int ADC_bits = 8;
31 
32 const int ADC_shift = 0;
37 
43 
45 
46 
48  : theDigiCounter(0), theWordCounter(0), theCablingTree(map), badPixelInfo(0)
49 {
50  int s32 = sizeof(Word32);
51  int s64 = sizeof(Word64);
52  int s8 = sizeof(char);
53  if ( s8 != 1 || s32 != 4*s8 || s64 != 2*s32) {
54  LogError("**PixelDataFormatter**")
55  <<" unexpected sizes: "
56  <<" size of char is: " << s8
57  <<", size of Word32 is: " << s32
58  <<", size of Word64 is: " << s64
59  <<", send exception" ;
60  }
61  includeErrors = false;
62  useQualityInfo = false;
63  allDetDigis = 0;
64  hasDetDigis = 0;
65 }
66 
67 void PixelDataFormatter::setErrorStatus(bool ErrorStatus)
68 {
69  includeErrors = ErrorStatus;
71 }
72 
73 void PixelDataFormatter::setQualityStatus(bool QualityStatus, const SiPixelQuality* QualityInfo)
74 {
75  useQualityInfo = QualityStatus;
76  badPixelInfo = QualityInfo;
77 }
78 
79 void PixelDataFormatter::interpretRawData(bool& errorsInEvent, int fedId, const FEDRawData& rawData, Digis& digis, Errors& errors)
80 {
82  int nWords = rawData.size()/sizeof(Word64);
83  if (nWords==0) return;
84 
85  SiPixelFrameConverter * converter = (theCablingTree) ?
86  new SiPixelFrameConverter(theCablingTree, fedId) : 0;
87 
88  // check CRC bit
89  const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1);
90  bool CRC_OK = errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors);
91 
92  if(CRC_OK) {
93  // check headers
94  const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
95  bool moreHeaders = true;
96  while (moreHeaders) {
97  header++;
98  if (debug) LogTrace("")<<"HEADER: " << print(*header);
99  bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors);
100  moreHeaders = headerStatus;
101  }
102 
103  // check trailers
104  bool moreTrailers = true;
105  trailer++;
106  while (moreTrailers) {
107  trailer--;
108  if (debug) LogTrace("")<<"TRAILER: " << print(*trailer);
109  bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
110  moreTrailers = trailerStatus;
111  }
112 
113  // data words
114  theWordCounter += 2*(nWords-2);
115  if (debug) LogTrace("")<<"data words: "<< (trailer-header-1);
116  for (const Word64* word = header+1; word != trailer; word++) {
117  if (debug) LogTrace("")<<"DATA: " << print(*word);
118  Word32 w1 = *word & WORD32_mask;
119  Word32 w2 = *word >> 32 & WORD32_mask;
120  if (w1==0) theWordCounter--;
121  if (w2==0) theWordCounter--;
122 
123  // check status of word...
124  bool notErrorROC1 = errorcheck.checkROC(errorsInEvent, fedId, converter, w1, errors);
125  if (notErrorROC1) {
126  int status1 = word2digi(fedId, converter, includeErrors, useQualityInfo, w1, digis);
127  if (status1) {
128  LogDebug("PixelDataFormatter::interpretRawData")
129  << "status #" <<status1<<" returned for word1";
130  errorsInEvent = true;
131  errorcheck.conversionError(fedId, converter, status1, w1, errors);
132  }
133  }
134  bool notErrorROC2 = errorcheck.checkROC(errorsInEvent, fedId, converter, w2, errors);
135  if (notErrorROC2) {
136  int status2 = word2digi(fedId, converter, includeErrors, useQualityInfo, w2, digis);
137  if (status2) {
138  LogDebug("PixelDataFormatter::interpretRawData")
139  << "status #" <<status2<<" returned for word2";
140  errorsInEvent = true;
141  errorcheck.conversionError(fedId, converter, status2, w2, errors);
142  }
143  }
144  }
145  } // end if(CRC_OK)
146  delete converter;
147 }
148 
149 
150 FEDRawData * PixelDataFormatter::formatData(unsigned int lvl1_ID, int fedId, const Digis & digis)
151 {
152  vector<Word32> words;
153 
154  SiPixelFrameConverter converter(theCablingTree, fedId);
155  for (Digis::const_iterator im = digis.begin(); im != digis.end(); im++) {
156  allDetDigis++;
157  uint32_t rawId = im->first;
158  if ( !converter.hasDetUnit(rawId) ) continue;
159  hasDetDigis++;
160  const DetDigis & detDigis = im->second;
161  for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); it++) {
162  theDigiCounter++;
163  const PixelDigi & digi = (*it);
164  int status = digi2word( &converter, rawId, digi, words);
165  if (status) {
166  LogError("PixelDataFormatter::formatData exception")
167  <<" digi2word returns error #"<<status
168  <<" Ndigis: "<<theDigiCounter << endl
169  <<" detector: "<<rawId<< endl
170  << print(digi) <<endl;
171  }
172  }
173  }
174  LogTrace(" allDetDigis/hasDetDigis : ") << allDetDigis<<"/"<<hasDetDigis;
175 
176  //
177  // since digis are written in the form of 64-bit packets
178  // add extra 32-bit word to make number of digis even
179  //
180  if (words.size() %2 != 0) words.push_back( Word32(0) );
181 
182 
183  //
184  // size in Bytes; create output structure
185  //
186  int dataSize = words.size() * sizeof(Word32);
187  int nHeaders = 1;
188  int nTrailers = 1;
189  dataSize += (nHeaders+nTrailers)*sizeof(Word64);
190  FEDRawData * rawData = new FEDRawData(dataSize);
191 
192  //
193  // get begining of data;
194  Word64 * word = reinterpret_cast<Word64* >(rawData->data());
195 
196  //
197  // write one header
198  FEDHeader::set( reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId);
199  word++;
200 
201  //
202  // write data
203  for (unsigned int i=0; i < words.size(); i+=2) {
204  *word = (Word64(words[i]) << 32 ) | words[i+1];
205  LogDebug("PixelDataFormatter") << print(*word);
206  word++;
207  }
208 
209  // write one trailer
210  FEDTrailer::set( reinterpret_cast<unsigned char*>(word), dataSize/sizeof(Word64), 0,0,0);
211  word++;
212 
213  //
214  // check memory
215  //
216  if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
217  string s = "** PROBLEM in PixelDataFormatter !!!";
218  throw cms::Exception(s);
219  }
220 
221  return rawData;
222 }
223 
225  uint32_t detId, const PixelDigi& digi, std::vector<Word32> & words) const
226 {
227  LogDebug("PixelDataFormatter")
228 // <<" detId: " << detId
229  <<print(digi);
230 
231  DetectorIndex detector = {detId, digi.row(), digi.column()};
232  ElectronicIndex cabling;
233  int status = converter->toCabling(cabling, detector);
234  if (status) return status;
235 
236  Word32 word =
237  (cabling.link << LINK_shift)
238  | (cabling.roc << ROC_shift)
239  | (cabling.dcol << DCOL_shift)
240  | (cabling.pxid << PXID_shift)
241  | (digi.adc() << ADC_shift);
242  words.push_back(word);
243  theWordCounter++;
244  return 0;
245 }
246 
247 
248 int PixelDataFormatter::word2digi(const int fedId, const SiPixelFrameConverter* converter,
249  const bool includeErrors, const bool useQuality, const Word32 & word, Digis & digis) const
250 {
251  // do not interpret false digis
252  if (word == 0 ) return 0;
253 
254  ElectronicIndex cabling;
255  cabling.dcol = (word >> DCOL_shift) & DCOL_mask;
256  cabling.pxid = (word >> PXID_shift) & PXID_mask;
257  cabling.link = (word >> LINK_shift) & LINK_mask;
258  cabling.roc = (word >> ROC_shift) & ROC_mask;
259  int adc = (word >> ADC_shift) & ADC_mask;
260 
261  if (debug) {
262  LocalPixel::DcolPxid pixel = {cabling.dcol,cabling.pxid};
263  LocalPixel local(pixel);
264  LogTrace("")<<" link: "<<cabling.link<<", roc: "<<cabling.roc
265  <<" rocRow: "<<local.rocRow()<<", rocCol:"<<local.rocCol()
266  <<" (dcol: "<<cabling.dcol<<", pxid:"<<cabling.pxid<<"), adc:"<<adc;
267  }
268 
269  if (!converter) return 0;
270 
271  DetectorIndex detIdx;
272  int status = converter->toDetector(cabling, detIdx);
273  if (status) return status;
274 
275  // exclude ROC(raw) based on bad ROC list bad in SiPixelQuality
276  // enable: process.siPixelDigis.UseQualityInfo = True
277  // 20-10-2010 A.Y.
278  if (useQuality&&badPixelInfo) {
279  CablingPathToDetUnit path = {fedId, cabling.link, cabling.roc};
280  const PixelROC * roc = theCablingTree->findItem(path);
281  short rocInDet = (short) roc->idInDetUnit();
282  bool badROC = badPixelInfo->IsRocBad(detIdx.rawId, rocInDet);
283  if (badROC) return 0;
284  }
285 
286  PixelDigi pd(detIdx.row, detIdx.col, adc);
287  digis[detIdx.rawId].push_back(pd);
288 
289  theDigiCounter++;
290 
291  if (debug) LogTrace("") << print(pd);
292  return 0;
293 }
294 
295 std::string PixelDataFormatter::print(const PixelDigi & digi) const
296 {
297  ostringstream str;
298  str << " DIGI: row: " << digi.row() <<", col: " << digi.column() <<", adc: " << digi.adc();
299  return str.str();
300 }
301 
302 std::string PixelDataFormatter::print(const Word64 & word) const
303 {
304  ostringstream str;
305  //str <<"word64: " << *reinterpret_cast<const bitset<64>*> (&word);
306  str <<"word64: " << reinterpret_cast<const bitset<64>&> (word);
307  return str.str();
308 }
309 
int adc(sample_type sample)
get the ADC sample (12 bits)
#define LogDebug(id)
const int DCOL_bits
Definition: ErrorChecker.cc:23
int row() const
Definition: PixelDigi.h:35
int i
Definition: DBlmapReader.cc:9
common ppss p3p6s2 common epss epspn46 common const1 w2
Definition: inclppp.h:1
bool checkTrailer(bool &errorsInEvent, int fedId, int nWords, const Word64 *trailer, Errors &errors)
Definition: ErrorChecker.cc:85
virtual const sipixelobjects::PixelROC * findItem(const sipixelobjects::CablingPathToDetUnit &) const =0
const int DCOL_shift
Definition: ErrorChecker.cc:30
static MessageDrop * instance()
Definition: MessageDrop.cc:61
std::map< uint32_t, DetErrors > Errors
static bool debugEnabled
Definition: MessageDrop.h:97
void interpretRawData(bool &errorsInEvent, int fedId, const FEDRawData &data, Digis &digis, Errors &errors)
int digi2word(const SiPixelFrameConverter *converter, uint32_t detId, const PixelDigi &digi, std::vector< Word32 > &words) const
const ErrorChecker::Word32 DCOL_mask
Definition: ErrorChecker.cc:40
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:44
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
identify pixel inside single ROC
Definition: LocalPixel.h:7
const int PXID_shift
Definition: ErrorChecker.cc:29
dictionary map
Definition: Association.py:160
bool checkROC(bool &errorsInEvent, int fedId, const SiPixelFrameConverter *converter, Word32 &errorWord, Errors &errors)
int path() const
Definition: HLTadd.h:3
const PixelDataFormatter::Word32 ADC_mask
bool checkCRC(bool &errorsInEvent, int fedId, const Word64 *trailer, Errors &errors)
Definition: ErrorChecker.cc:54
PixelDataFormatter(const SiPixelFedCabling *map)
const int LINK_shift
Definition: ErrorChecker.cc:32
const ErrorChecker::Word32 ROC_mask
Definition: ErrorChecker.cc:39
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:42
const int ADC_shift
Definition: ErrorChecker.cc:28
unsigned int idInDetUnit() const
id of this ROC in DetUnit etermined by token path
Definition: PixelROC.h:43
std::string print(const PixelDigi &digi) const
unsigned short adc() const
Definition: PixelDigi.h:38
const int ROC_bits
Definition: ErrorChecker.cc:22
const PixelDataFormatter::Word64 WORD32_mask
void setQualityStatus(bool QualityStatus, const SiPixelQuality *QualityInfo)
const ErrorChecker::Word32 PXID_mask
Definition: ErrorChecker.cc:41
const SiPixelQuality * badPixelInfo
void setErrorStatus(bool ErrorStatus)
Definition: ErrorChecker.cc:49
#define LogTrace(id)
double collumn and pixel ID in double collumn representation
Definition: LocalPixel.h:22
const int ADC_bits
Definition: ErrorChecker.cc:25
const int LINK_bits
Definition: ErrorChecker.cc:21
int toCabling(sipixelobjects::ElectronicIndex &cabling, const sipixelobjects::DetectorIndex &detector) const
void conversionError(int fedId, const SiPixelFrameConverter *converter, int status, Word32 &errorWord, Errors &errors)
const int ROC_shift
Definition: ErrorChecker.cc:31
std::vector< PixelDigi > DetDigis
FEDRawData * formatData(unsigned int lvl1_ID, int fedId, const Digis &digis)
bool hasDetUnit(uint32_t radId) const
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
std::map< uint32_t, DetDigis > Digis
const ErrorChecker::Word32 LINK_mask
Definition: ErrorChecker.cc:38
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
string s
Definition: asciidump.py:422
const SiPixelFedCabling * theCablingTree
tuple status
Definition: ntuplemaker.py:245
int column() const
Definition: PixelDigi.h:36
const int PXID_bits
Definition: ErrorChecker.cc:24
bool checkHeader(bool &errorsInEvent, int fedId, const Word64 *header, Errors &errors)
Definition: ErrorChecker.cc:67