CMS 3D CMS Logo

CSCDDUEventData.cc
Go to the documentation of this file.
1 /* CSCDDUEventData.cc
2  * Modified 4/21/03 to get rid of arrays and store all CSC data
3  * in vectors.
4  * A.Tumanov
5  */
6 
9 
10 #include <iostream>
11 #include <cstdio>
12 
14 
15 #ifdef LOCAL_UNPACK
16 bool CSCDDUEventData::debug = false;
17 uint32_t CSCDDUEventData::errMask = 0xFFFFFFFF;
18 #else
19 #include <atomic>
20 std::atomic<bool> CSCDDUEventData::debug{false};
21 std::atomic<uint32_t> CSCDDUEventData::errMask{0xFFFFFFFF};
22 #endif
23 
24 
25 
27 {
29 }
30 
31 
32 CSCDDUEventData::CSCDDUEventData(const uint16_t *buf, CSCDCCExaminer* examiner)
33 {
34  unpack_data(buf, examiner);
35 }
36 
38 {
39 }
40 
41 
42 void CSCDDUEventData::add(CSCEventData & cscData, int dmbId, int dduInput, unsigned format_version)
43 {
44  theDDUHeader.setDMBDAV(dduInput);
45  //@@ Tim: The following sets the word which is supposed to be CSCs in error, with bit 15 set for DMB Full
46  //@@ so I think sim should not set it at all
47  //@@ theDDUTrailer.setDMBDAV(dmbId);
48  theDDUHeader.setDMBDAV(dduInput);
49  cscData.dmbHeader()->setdmbID(dmbId);
51  theData.push_back(cscData);
52 }
53 
55 {
57 }
58 
59 void CSCDDUEventData::decodeStatus(int code) const
60 {
61  // JRG is Jason Gilmore
62  // JRG, low-order 16-bit status (most serious errors):
63  if((code&errMask)>0)
64  {
65  // JRG, low-order 16-bit status (most serious errors):
66  if((code&0x0000F000)>0)
67  {
68  if((0x00008000&code)>0)
69  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Critical Error, ** needs reset **";
70  if((0x00004000&code)>0)
71  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Single Error, bad event";
72  if((0x00002000&code)>0)
73  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Single Warning";
74  if((0x00001000&code)>0)
75  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Near Full Warning";
76  }
77  if((code&0x00000F00)>0)
78  {
79  if((0x00000800&code)>0)
80  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU 64-bit Alignment Error";
81  if((0x00000400&code)>0)
82  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Control DLL Error occured";
83  if((0x00000200&code)>0)
84  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU DMB Error occurred";
85  if((0x00000100&code)>0)
86  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Lost In Event Error";
87  }
88  if((code&0x000000F0)>0)
89  {
90  if((0x00000080&code)>0)
91  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Lost In Data Error occurred";
92  if((0x00000040&code)>0)
93  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Timeout Error";
94  if((0x00000020&code)>0)
95  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " TMB or ALCT CRC Error";
96  if((0x00000010&code)>0)
97  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Multiple Transmit Errors";
98  }
99  if((code&0x0000000F)>0)
100  {
101  if((0x00000008&code)>0)
102  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Sync Lost or FIFO Full Error";
103  if((0x00000004&code)>0)
104  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Fiber/FIFO Connection Error";
105  if((0x00000002&code)>0)
106  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU L1A Match Error";
107  if((0x00000001&code)>0)
108  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU DMB or CFEB CRC Error";
109  }
110  if((code&0xF0000000)>0)
111  {
112  // JRG, high-order 16-bit status (not-so-serious errors):
113  if((0x80000000&code)>0)
114  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU DMB LCT/DAV/Movlp Mismatch";
115  if((0x40000000&code)>0)
116  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU-CFEB L1 Mismatch";
117  if((0x20000000&code)>0)
118  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU saw no good DMB CRCs";
119  if((0x10000000&code)>0)
120  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU CFEB Count Error";
121  }
122  if((code&0x0F000000)>0)
123  {
124  if((0x08000000&code)>0)
125  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU FirstDat Error";
126  if((0x04000000&code)>0)
127  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU L1A-FIFO Full Error";
128  if((0x02000000&code)>0)
129  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Data Stuck in FIFO";
130  if((0x01000000&code)>0)
131  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU NoLiveFibers Error";
132  }
133  if((code&0x00F00000)>0)
134  {
135  if((0x00800000&code)>0)
136  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Spwd single-bit Warning";
137  if((0x00400000&code)>0)
138  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Input FPGA Error";
139  if((0x00200000&code)>0)
140  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU DAQ Stop bit set";
141  if((0x00100000&code)>0)
142  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU DAQ says Not Ready";
143  if((0x00300000&code)==0x00200000)
144  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU DAQ Applied Backpressure";
145  }
146  if((code&0x000F0000)>0)
147  {
148  if((0x00080000&code)>0)
149  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU TMB Error";
150  if((0x00040000&code)>0)
151  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU ALCT Error";
152  if((0x00020000&code)>0)
153  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Trigger Readout Wordcount Error";
154  if((0x00010000&code)>0)
155  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " DDU Trigger L1A Match Error";
156  }
157  }
158 }
159 
160 void CSCDDUEventData::unpack_data(const uint16_t *buf, CSCDCCExaminer* examiner)
161 {
162  // just to calculate length
163  const uint16_t * inputBuf = buf;
164  const uint16_t * inputBuf0 = buf;
165  theData.clear();
166  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << "CSCDDUEventData::unpack_data() is called";
167  if (debug)
168  for (int i=0;i<6;++i)
169  {
170  LogTrace ("CSCDDUEventData|CSCRawToDigi") << i << std::hex << buf[4*i+3] << buf[4*i+2]
171  << buf[4*i+1] << buf[4*i];
172  std::cout << i << " " << std::hex << buf[4*i+3] << " " << buf[4*i+2] << " "
173  << buf[4*i+1] << " " << buf[4*i] << std::endl;
174  }
175 
177 
178  if (debug) {
179  LogTrace ("CSCDDUEventData|CSCRawToDigi") << "size of ddu header in words = " << theDDUHeader.sizeInWords();
180  LogTrace ("CSCDDUEventData|CSCRawToDigi") << "sizeof(DDUHeader) = " << sizeof(theDDUHeader);
181  }
182  buf += theDDUHeader.sizeInWords();
183 
184 
185  // if (theDDUHeader.format_version() >= 0x6)
186  if (theDDUHeader.format_version() == 0x7)
187  {
188  theFormatVersion = 2013;
189  }
190  else if (theDDUHeader.format_version() <= 0x6)
191  {
192  theFormatVersion = 2005;
193  }
194  else // Add handling for any other format version
195  {
196  theFormatVersion = 2013;
197  }
198 
199 
200  // we really don't want to copy CSCEventData's while filling the vec
201  theData.clear();
202  theData.reserve(theDDUHeader.ncsc());
203 
204  if (examiner!= nullptr) { // Use selective unpacking mode
205 
206  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << "selective unpacking starting";
207 
208  // Find this DDU in examiner's DDUs list
209  DDUIdType dduID = theDDUHeader.source_id();
210 
211  std::map<DDUIdType,std::map<CSCIdType,const uint16_t*> > ddus = examiner->DMB_block();
212  std::map<DDUIdType,std::map<CSCIdType,const uint16_t*> >::iterator ddu_itr = ddus.find(dduID);
213  const uint16_t* dduBlock = (const uint16_t*)((examiner->DDU_block())[dduID]);
214  uint32_t dduBufSize = (examiner->DDU_size())[dduID];
215 
216  if (ddu_itr != ddus.end() && dduBufSize!=0 && dduBlock==inputBuf) {
217  std::map<CSCIdType,const uint16_t*> &cscs = ddu_itr->second;
218  std::map<CSCIdType,const uint16_t*>::iterator csc_itr;
219 
220  for (csc_itr=cscs.begin(); csc_itr != cscs.end(); ++csc_itr) {
221  short cscid = csc_itr->first;
222 
223  if(cscid != -1)
224  {
225  const uint16_t* pos = (const uint16_t*)csc_itr->second;
226 
227 
228  ExaminerStatusType errors = examiner->errorsForChamber(cscid);
229  if ((errors & examiner->getMask()) > 0 ) {
230  if (debug)
231  LogTrace ("CSCDDUEventData|CSCRawToDigi" )
232  << "skip unpacking of CSC " << cscid << " due format errors: 0x" << std::hex << errors << std::dec;
233  continue;
234  }
235 
236  theData.push_back(CSCEventData(pos, theFormatVersion));
237  }
238  }
239 
240  if (debug)
241  {
242  LogTrace ("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
243  }
244  // decode ddu tail
245  theDDUTrailer.setFromBuffer(inputBuf+dduBufSize);
246  // memcpy(&theDDUTrailer, dduBlock+(dduBufSize-theDDUTrailer.sizeInWords())*2, theDDUTrailer.sizeInWords()*2);
247  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
249  if ((errorstat&errMask) != 0)
250  {
251  if (theDDUTrailer.check())
252  {
253  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi")
254  << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
255  if (debug) decodeStatus(errorstat);
256  }
257  else
258  {
259  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi" )
260  << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
261  }
262  }
263 
264  if (debug) {
265  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " Final errorstat " << std::hex << errorstat << std::dec ;
266  }
267  // the trailer counts in 64-bit words
268 
269  // theSizeInWords = dduBufSize;
270  // buf=inputBuf+dduBufSize;
271 
272  }
273  theSizeInWords = dduBufSize+12;
274 
275  } else {
276 
277 
278  while( (((buf[0]&0xf000) == 0x9000)||((buf[0]&0xf000) == 0xa000))
279  && (buf[3] != 0x8000)) {
280  // ++i;
281  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << "unpack csc data loop started";
282  theData.push_back(CSCEventData(buf));
283  buf += (theData.back()).size();
284  if (debug) {
285  LogTrace ("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
286  }
287  }
288 
289  if (debug) {
290  LogTrace ("CSCDDUEventData|CSCRawToDigi") << "unpacking ddu trailer ";
291  LogTrace ("CSCDDUEventData|CSCRawToDigi") << std::hex << buf[3]<<" " << buf[2]
292  <<" " << buf[1]<<" " << buf[0];
293  }
294 
295  // decode ddu tail
297  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
299  if ((errorstat&errMask) != 0)
300  {
301  if (theDDUTrailer.check())
302  {
303  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi")
304  << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
305  if (debug) decodeStatus(errorstat);
306  }
307  else
308  {
309  if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi" )
310  << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
311  }
312  }
313 
314  if (debug)
315  LogTrace ("CSCDDUEventData|CSCRawToDigi") << " Final errorstat " << std::hex << errorstat << std::dec ;
316  // the trailer counts in 64-bit words
317  buf += theDDUTrailer.sizeInWords();
318 
319  theSizeInWords = buf - inputBuf;
320  }
321 
323 theDDUTrailer0 = inputBuf0[theSizeInWords-4];
324 }
325 
326 
328 {
329  // the trailer counts in 64-bit words
330  if (debug)
331  {
332  LogTrace ("CSCDDUEventData|CSCRawToDigi") << sizeInWords();
333  LogTrace ("CSCDDUEventData|CSCRawToDigi") << "wordcount = " << theDDUTrailer.wordcount()*4;
334  }
335 
336  return theDDUHeader.check() && theDDUTrailer.check();
337 }
338 
339 boost::dynamic_bitset<> CSCDDUEventData::pack()
340 {
341  boost::dynamic_bitset<> result = bitset_utilities::ushortToBitset( theDDUHeader.sizeInWords()*16,
342  theDDUHeader.data());
343  //std::cout <<"SANDRIK inside DDUEvdata check = ";
344  //theDDUHeader.check();
345  //std::cout <<std::endl;
346  //std::cout <<"printing out ddu header words"<<std::endl;
347  //for (unsigned int i=0;i<theDDUHeader.sizeInWords();i++) {
348  // unsigned short * buf = theDDUHeader.data();
349  // printf("%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i]);
350  // i+=3;
351  //}
352  //std::cout <<"printing out ddu header words via bitset"<<std::endl;
353  //bitset_utilities::printWords(result);
354 
355  for(unsigned int i = 0; i < theData.size(); ++i)
356  {
357  result = bitset_utilities::append(result,theData[i].pack());
358  }
359  theSizeInWords = result.size()/16 + theDDUTrailer.sizeInWords();
360  // 64-bit word count
362  boost::dynamic_bitset<> dduTrailer = bitset_utilities::ushortToBitset ( theDDUTrailer.sizeInWords()*16,
363  theDDUTrailer.data());
364  result = bitset_utilities::append(result,dduTrailer);
365 
366  return result;
367 }
368 
void setFromBuffer(uint16_t const *buf)
Definition: CSCDDUHeader.h:21
void setEventInformation(int bxnum, int lvl1num)
CSCDDUEventData(const CSCDDUHeader &)
uint16_t theFormatVersion
CSCDDUHeader header() const
bool check() const
Definition: CSCDDUHeader.cc:54
int format_version() const
Definition: CSCDDUHeader.h:27
void setDMBDAV(int dduInput)
Definition: CSCDDUHeader.cc:35
static unsigned sizeInWords()
Definition: CSCDDUTrailer.h:33
CSCDDUHeader theDDUHeader
int lvl1num() const
Definition: CSCDDUHeader.h:30
void setWordCount(unsigned wordcount)
Definition: CSCDDUTrailer.h:48
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
unsigned errorstat() const
Definition: CSCDDUTrailer.h:46
ExaminerStatusType errorsForChamber(CSCIdType chamber) const
int bxnum() const
Definition: CSCDDUHeader.h:29
std::map< DDUIdType, std::map< CSCIdType, const uint16_t * > > DMB_block(void) const
void unpack_data(const uint16_t *buf, CSCDCCExaminer *examiner=0)
a good test routine would be to unpack data, then pack it again.
unsigned short * data()
Definition: CSCDDUTrailer.h:42
bool check() const
Definition: CSCDDUTrailer.h:35
const std::vector< CSCEventData > & cscData() const
accessor to data
std::map< DDUIdType, uint32_t > DDU_size(void) const
#define LogTrace(id)
std::map< DDUIdType, const uint16_t * > DDU_block(void) const
static std::atomic< unsigned int > errMask
ExaminerMaskType getMask() const
uint16_t theDDUTrailer0
void setFromBuffer(uint16_t const *buf)
Definition: CSCDDUTrailer.h:29
uint32_t ExaminerStatusType
int size() const
static unsigned sizeInWords()
Definition: CSCDDUHeader.h:37
void add(CSCEventData &, int dmbId, int dduInput, unsigned int format_version=2005)
for making events. Sets the bxnum and lvl1num inside the chamber event
int16_t DDUIdType
std::vector< CSCEventData > theData
unsigned wordcount() const
Definition: CSCDDUTrailer.h:47
int ncsc() const
Definition: CSCDDUHeader.h:32
Definition: errors.py:1
int source_id() const
Definition: CSCDDUHeader.h:28
boost::dynamic_bitset ushortToBitset(const unsigned int numberOfBits, unsigned short *buf)
this method takes numberOfBits bits from unsigned short * array and returns them in the bitset obj...
bool check() const
void decodeStatus() const
CSCDDUTrailer theDDUTrailer
unsigned short * data()
Definition: CSCDDUHeader.h:50
const CSCDMBHeader * dmbHeader() const
the DAQ motherboard header. A good place for event and chamber info
Definition: CSCEventData.h:82
static std::atomic< bool > debug
void setdmbID(int newDMBID)
Definition: CSCDMBHeader.h:36
long unsigned int errorstat
trailer info
boost::dynamic_bitset pack()
Alex check this 16 or 64.
int sizeInWords() const