CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripFEDBufferComponents.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <ostream>
3 #include <sstream>
4 #include <cstring>
7 
8 namespace sistrip {
9 
10  void printHexValue(const uint8_t value, std::ostream& os)
11  {
12  const std::ios_base::fmtflags originalFormatFlags = os.flags();
13  os << std::hex << std::setfill('0') << std::setw(2);
14  os << uint16_t(value);
15  os.flags(originalFormatFlags);
16  }
17 
18  void printHexWord(const uint8_t* pointer, const size_t lengthInBytes, std::ostream& os)
19  {
20  size_t i = lengthInBytes-1;
21  do{
22  printHexValue(pointer[i],os);
23  if (i != 0) os << " ";
24  } while (i-- != 0);
25  }
26 
27  void printHex(const void* pointer, const size_t lengthInBytes, std::ostream& os)
28  {
29  const uint8_t* bytePointer = reinterpret_cast<const uint8_t*>(pointer);
30  //if there is one 64 bit word or less, print it out
31  if (lengthInBytes <= 8) {
32  printHexWord(bytePointer,lengthInBytes,os);
33  }
34  //otherwise, print word numbers etc
35  else {
36  //header
37  os << "word\tbyte\t \t\tbyte" << std::endl;;
38  const size_t words = lengthInBytes/8;
39  const size_t extraBytes = lengthInBytes - 8*words;
40  //print full words
41  for (size_t w = 0; w < words; w++) {
42  const size_t startByte = w*8;
43  os << w << '\t' << startByte+8 << '\t';
44  printHexWord(bytePointer+startByte,8,os);
45  os << "\t\t" << startByte << std::endl;
46  }
47  //print part word, if any
48  if (extraBytes) {
49  const size_t startByte = words*8;
50  os << words << '\t' << startByte+8 << '\t';
51  //padding
52  size_t p = 8;
53  while (p-- > extraBytes) {
54  os << "00 ";
55  }
56  printHexWord(bytePointer+startByte,extraBytes,os);
57  os << "\t\t" << startByte << std::endl;
58  }
59  os << std::endl;
60  }
61  }
62 
63 
64  uint16_t calculateFEDBufferCRC(const uint8_t* buffer, const size_t lengthInBytes)
65  {
66  uint16_t crc = 0xFFFF;
67  for (size_t i = 0; i < lengthInBytes-8; i++) {
68  crc = evf::compute_crc_8bit(crc,buffer[i^7]);
69  }
70  for (size_t i=lengthInBytes-8; i<lengthInBytes; i++) {
71  uint8_t byte;
72  //set CRC bytes to zero since these were not set when CRC was calculated
73  if (i==lengthInBytes-4 || i==lengthInBytes-3)
74  byte = 0x00;
75  else
76  byte = buffer[i^7];
77  crc = evf::compute_crc_8bit(crc,byte);
78  }
79  return crc;
80  }
81 
82 
83  std::ostream& operator<<(std::ostream& os, const FEDBufferFormat& value)
84  {
85  switch (value) {
87  os << "Old VME";
88  break;
90  os << "Old S-Link";
91  break;
92  case BUFFER_FORMAT_NEW:
93  os << "New";
94  break;
96  os << "Invalid";
97  break;
98  default:
99  os << "Unrecognized";
100  os << " (";
101  printHexValue(value,os);
102  os << ")";
103  break;
104  }
105  return os;
106  }
107 
108  std::ostream& operator<<(std::ostream& os, const FEDHeaderType& value)
109  {
110  switch (value) {
112  os << "Full debug";
113  break;
115  os << "APV error";
116  break;
117  case HEADER_TYPE_NONE:
118  os << "None";
119  break;
120  case HEADER_TYPE_INVALID:
121  os << "Invalid";
122  break;
123  default:
124  os << "Unrecognized";
125  os << " (";
126  printHexValue(value,os);
127  os << ")";
128  break;
129  }
130  return os;
131  }
132 
133  std::ostream& operator<<(std::ostream& os, const FEDReadoutMode& value)
134  {
135  switch (value) {
136  case READOUT_MODE_SCOPE:
137  os << "Scope mode";
138  break;
140  os << "Virgin raw";
141  break;
143  os << "Processed raw";
144  break;
146  os << "Zero suppressed";
147  break;
149  os << "Zero suppressed lite";
150  break;
151  case READOUT_MODE_SPY:
152  os << "Spy channel";
153  break;
155  os << "Invalid";
156  break;
157  default:
158  os << "Unrecognized";
159  os << " (";
160  printHexValue(value,os);
161  os << ")";
162  break;
163  }
164  return os;
165  }
166 
167  std::ostream& operator<<(std::ostream& os, const FEDDataType& value)
168  {
169  switch (value) {
170  case DATA_TYPE_REAL:
171  os << "Real data";
172  break;
173  case DATA_TYPE_FAKE:
174  os << "Fake data";
175  break;
176  default:
177  os << "Unrecognized";
178  os << " (";
179  printHexValue(value,os);
180  os << ")";
181  break;
182  }
183  return os;
184  }
185 
186  std::ostream& operator<<(std::ostream& os, const FEDDAQEventType& value)
187  {
188  switch (value) {
190  os << "Physics trigger";
191  break;
193  os << "Calibration trigger";
194  break;
195  case DAQ_EVENT_TYPE_TEST:
196  os << "Test trigger";
197  break;
199  os << "Technical trigger";
200  break;
202  os << "Simulated event";
203  break;
205  os << "Traced event";
206  break;
208  os << "Error";
209  break;
211  os << "Unknown";
212  break;
213  default:
214  os << "Unrecognized";
215  os << " (";
216  printHexValue(value,os);
217  os << ")";
218  break;
219  }
220  return os;
221  }
222 
223  std::ostream& operator<<(std::ostream& os, const FEDTTSBits& value)
224  {
225  switch (value) {
226  case TTS_DISCONNECTED0:
227  os << "Disconected 0";
228  break;
229  case TTS_WARN_OVERFLOW:
230  os << "Warning overflow";
231  break;
232  case TTS_OUT_OF_SYNC:
233  os << "Out of sync";
234  break;
235  case TTS_BUSY:
236  os << "Busy";
237  break;
238  case TTS_READY:
239  os << "Ready";
240  break;
241  case TTS_ERROR:
242  os << "Error";
243  break;
244  case TTS_INVALID:
245  os << "Invalid";
246  break;
247  case TTS_DISCONNECTED1:
248  os << "Disconected 1";
249  break;
250  default:
251  os << "Unrecognized";
252  os << " (";
253  printHexValue(value,os);
254  os << ")";
255  break;
256  }
257  return os;
258  }
259 
260  std::ostream& operator<<(std::ostream& os, const FEDBufferState& value)
261  {
262  switch (value) {
263  case BUFFER_STATE_UNSET:
264  os << "Unset";
265  break;
266  case BUFFER_STATE_EMPTY:
267  os << "Empty";
268  break;
270  os << "Partial Full";
271  break;
272  case BUFFER_STATE_FULL:
273  os << "Full";
274  break;
275  default:
276  os << "Unrecognized";
277  os << " (";
278  printHexValue(value,os);
279  os << ")";
280  break;
281  }
282  return os;
283  }
284 
285  std::ostream& operator<<(std::ostream& os, const FEDChannelStatus& value)
286  {
287  if (!(value&CHANNEL_STATUS_LOCKED)) os << "Unlocked ";
288  if (!(value&CHANNEL_STATUS_IN_SYNC)) os << "Out-of-sync ";
289  if (!(value&CHANNEL_STATUS_APV1_ADDRESS_GOOD)) os << "APV 1 bad address ";
290  if (!(value&CHANNEL_STATUS_APV1_NO_ERROR_BIT)) os << "APV 1 error ";
291  if (!(value&CHANNEL_STATUS_APV0_ADDRESS_GOOD)) os << "APV 0 bad address ";
292  if (!(value&CHANNEL_STATUS_APV0_NO_ERROR_BIT)) os << "APV 0 error ";
293  if (value == CHANNEL_STATUS_NO_PROBLEMS) os << "No errors";
294  return os;
295  }
296 
297  FEDBufferFormat fedBufferFormatFromString(const std::string& bufferFormatString)
298  {
299  if ( (bufferFormatString == "OLD_VME") ||
300  (bufferFormatString == "BUFFER_FORMAT_OLD_VME") ||
301  (bufferFormatString == "Old VME") ) {
302  return BUFFER_FORMAT_OLD_VME;
303  }
304  if ( (bufferFormatString == "OLD_SLINK") ||
305  (bufferFormatString == "BUFFER_FORMAT_OLD_SLINK") ||
306  (bufferFormatString == "Old S-Link") ) {
308  }
309  if ( (bufferFormatString == "NEW") ||
310  (bufferFormatString == "BUFFER_FORMAT_NEW") ||
311  (bufferFormatString == "New") ) {
312  return BUFFER_FORMAT_NEW;
313  }
314  //if it was none of the above then return invalid
315  return BUFFER_FORMAT_INVALID;
316  }
317 
318  FEDHeaderType fedHeaderTypeFromString(const std::string& headerTypeString)
319  {
320  if ( (headerTypeString == "FULL_DEBUG") ||
321  (headerTypeString == "HEADER_TYPE_FULL_DEBUG") ||
322  (headerTypeString == "Full debug") ) {
323  return HEADER_TYPE_FULL_DEBUG;
324  }
325  if ( (headerTypeString == "APV_ERROR") ||
326  (headerTypeString == "HEADER_TYPE_APV_ERROR") ||
327  (headerTypeString == "APV error") ) {
328  return HEADER_TYPE_APV_ERROR;
329  }
330  if ( (headerTypeString == "None") ||
331  (headerTypeString == "none") ) {
332  return HEADER_TYPE_NONE;
333  }
334  //if it was none of the above then return invalid
335  return HEADER_TYPE_INVALID;
336  }
337 
338  FEDReadoutMode fedReadoutModeFromString(const std::string& readoutModeString)
339  {
340  if ( (readoutModeString == "READOUT_MODE_SCOPE") ||
341  (readoutModeString == "SCOPE") ||
342  (readoutModeString == "SCOPE_MODE") ||
343  (readoutModeString == "Scope mode") ) {
344  return READOUT_MODE_SCOPE;
345  }
346  if ( (readoutModeString == "READOUT_MODE_VIRGIN_RAW") ||
347  (readoutModeString == "VIRGIN_RAW") ||
348  (readoutModeString == "Virgin raw") ) {
350  }
351  if ( (readoutModeString == "READOUT_MODE_PROC_RAW") ||
352  (readoutModeString == "PROC_RAW") ||
353  (readoutModeString == "PROCESSED_RAW") ||
354  (readoutModeString == "Processed raw") ) {
355  return READOUT_MODE_PROC_RAW;
356  }
357  if ( (readoutModeString == "READOUT_MODE_ZERO_SUPPRESSED") ||
358  (readoutModeString == "ZERO_SUPPRESSED") ||
359  (readoutModeString == "Zero suppressed") ) {
361  }
362  if ( (readoutModeString == "READOUT_MODE_ZERO_SUPPRESSED_LITE") ||
363  (readoutModeString == "ZERO_SUPPRESSED_LITE") ||
364  (readoutModeString == "Zero suppressed lite") ) {
366  }
367  if ( (readoutModeString == "READOUT_MODE_SPY") ||
368  (readoutModeString == "SPY") ||
369  (readoutModeString == "Spy channel") ) {
370  return READOUT_MODE_SPY;
371  }
372  //if it was none of the above then return invalid
373  return READOUT_MODE_INVALID;
374  }
375 
376  FEDDataType fedDataTypeFromString(const std::string& dataTypeString)
377  {
378  if ( (dataTypeString == "REAL") ||
379  (dataTypeString == "DATA_TYPE_REAL") ||
380  (dataTypeString == "Real data") ) {
381  return DATA_TYPE_REAL;
382  }
383  if ( (dataTypeString == "FAKE") ||
384  (dataTypeString == "DATA_TYPE_FAKE") ||
385  (dataTypeString == "Fake data") ) {
386  return DATA_TYPE_FAKE;
387  }
388  //if it was none of the above then throw an exception (there is no invalid value for the data type since it is represented as a single bit in the buffer)
389  std::ostringstream ss;
390  ss << "Trying to convert to a FEDDataType from an invalid string: " << dataTypeString;
391  throw cms::Exception("FEDDataType") << ss.str();
392  }
393 
394  FEDDAQEventType fedDAQEventTypeFromString(const std::string& daqEventTypeString)
395  {
396  if ( (daqEventTypeString == "PHYSICS") ||
397  (daqEventTypeString == "DAQ_EVENT_TYPE_PHYSICS") ||
398  (daqEventTypeString == "Physics trigger") ) {
399  return DAQ_EVENT_TYPE_PHYSICS;
400  }
401  if ( (daqEventTypeString == "CALIBRATION") ||
402  (daqEventTypeString == "DAQ_EVENT_TYPE_CALIBRATION") ||
403  (daqEventTypeString == "Calibration trigger") ) {
405  }
406  if ( (daqEventTypeString == "TEST") ||
407  (daqEventTypeString == "DAQ_EVENT_TYPE_TEST") ||
408  (daqEventTypeString == "Test trigger") ) {
409  return DAQ_EVENT_TYPE_TEST;
410  }
411  if ( (daqEventTypeString == "TECHNICAL") ||
412  (daqEventTypeString == "DAQ_EVENT_TYPE_TECHNICAL") ||
413  (daqEventTypeString == "Technical trigger") ) {
415  }
416  if ( (daqEventTypeString == "SIMULATED") ||
417  (daqEventTypeString == "DAQ_EVENT_TYPE_SIMULATED") ||
418  (daqEventTypeString == "Simulated trigger") ) {
420  }
421  if ( (daqEventTypeString == "TRACED") ||
422  (daqEventTypeString == "DAQ_EVENT_TYPE_TRACED") ||
423  (daqEventTypeString == "Traced event") ) {
424  return DAQ_EVENT_TYPE_TRACED;
425  }
426  if ( (daqEventTypeString == "ERROR") ||
427  (daqEventTypeString == "DAQ_EVENT_TYPE_ERROR") ||
428  (daqEventTypeString == "Error") ) {
429  return DAQ_EVENT_TYPE_ERROR;
430  }
431  //if it was none of the above then return invalid
432  return DAQ_EVENT_TYPE_INVALID;
433  }
434 
435 
436 
437 
438  void FEDStatusRegister::printFlags(std::ostream& os) const
439  {
440  if (slinkFullFlag()) os << "SLINK_FULL ";
441  if (trackerHeaderMonitorDataReadyFlag()) os << "HEADER_MONITOR_READY ";
442  if (qdrMemoryFullFlag()) os << "QDR_FULL ";
443  if (qdrMemoryPartialFullFlag()) os << "QDR_PARTIAL_FULL ";
444  if (qdrMemoryEmptyFlag()) os << "QDR_EMPTY ";
445  if (l1aBxFIFOFullFlag()) os << "L1A_FULL ";
446  if (l1aBxFIFOPartialFullFlag()) os << "L1A_PARTIAL_FULL ";
447  if (l1aBxFIFOEmptyFlag()) os << "L1A_EMPTY ";
448  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
449  if (feDataMissingFlag(iFE)) os << "FEUNIT" << uint16_t(iFE) << "MISSING ";
450  }
451  }
452 
454  {
455  uint8_t result(0x00);
456  if (qdrMemoryFullFlag()) result |= BUFFER_STATE_FULL;
458  if (qdrMemoryEmptyFlag()) result |= BUFFER_STATE_EMPTY;
459  return FEDBufferState(result);
460  }
461 
463  {
464  uint8_t result(0x00);
465  if (l1aBxFIFOFullFlag()) result |= BUFFER_STATE_FULL;
467  if (l1aBxFIFOEmptyFlag()) result |= BUFFER_STATE_EMPTY;
468  return FEDBufferState(result);
469  }
470 
471  void FEDStatusRegister::setBit(const uint8_t num, const bool bitSet)
472  {
473  const uint16_t mask = (0x0001 << num);
474  if (bitSet) data_ |= mask;
475  else data_ &= (~mask);
476  }
477 
479  {
480  switch (state) {
481  case BUFFER_STATE_FULL:
483  case BUFFER_STATE_EMPTY:
484  case BUFFER_STATE_UNSET:
485  break;
486  default:
487  std::ostringstream ss;
488  ss << "Invalid buffer state: ";
489  printHex(&state,1,ss);
490  throw cms::Exception("FEDBuffer") << ss.str();
491  }
495  return *this;
496  }
497 
499  {
500  switch (state) {
501  case BUFFER_STATE_FULL:
503  case BUFFER_STATE_EMPTY:
504  case BUFFER_STATE_UNSET:
505  break;
506  default:
507  std::ostringstream ss;
508  ss << "Invalid buffer state: ";
509  printHex(&state,1,ss);
510  throw cms::Exception("FEDBuffer") << ss.str();
511  }
515  return *this;
516  }
517 
518 
519 
520 
521  void FEDBackendStatusRegister::printFlags(std::ostream& os) const
522  {
523  if (internalFreezeFlag()) os << "INTERNAL_FREEZE ";
524  if (slinkDownFlag()) os << "SLINK_DOWN ";
525  if (slinkFullFlag()) os << "SLINK_FULL ";
526  if (backpressureFlag()) os << "BACKPRESSURE ";
527  if (ttcReadyFlag()) os << "TTC_READY ";
528  if (trackerHeaderMonitorDataReadyFlag()) os << "HEADER_MONITOR_READY ";
530  printFlagsForBuffer(frameAddressFIFOState(),"FRAME_ADDRESS",os);
531  printFlagsForBuffer(totalLengthFIFOState(),"TOTAL_LENGTH",os);
532  printFlagsForBuffer(trackerHeaderFIFOState(),"TRACKER_HEADER",os);
533  printFlagsForBuffer(l1aBxFIFOState(),"L1ABX",os);
536  }
537 
538  void FEDBackendStatusRegister::printFlagsForBuffer(const FEDBufferState bufferState, const std::string name, std::ostream& os) const
539  {
540  if (bufferState&BUFFER_STATE_EMPTY) os << name << "_EMPTY ";
541  if (bufferState&BUFFER_STATE_PARTIAL_FULL) os << name << "_PARTIAL_FULL ";
542  if (bufferState&BUFFER_STATE_FULL) os << name << "_FULL ";
543  if (bufferState == BUFFER_STATE_UNSET) os << name << "_UNSET ";
544  }
545 
546  FEDBufferState FEDBackendStatusRegister::getBufferState(const uint8_t bufferPosition) const
547  {
548  uint8_t result = 0x00;
549  if (getBit(bufferPosition+STATE_OFFSET_EMPTY)) result |= BUFFER_STATE_EMPTY;
550  if (getBit(bufferPosition+STATE_OFFSET_PARTIAL_FULL)) result |= BUFFER_STATE_PARTIAL_FULL;
551  if (getBit(bufferPosition+STATE_OFFSET_FULL)) result |= BUFFER_STATE_FULL;
552  return FEDBufferState(result);
553  }
554 
555  void FEDBackendStatusRegister::setBufferSate(const uint8_t bufferPosition, const FEDBufferState state)
556  {
557  switch (state) {
558  case BUFFER_STATE_FULL:
560  case BUFFER_STATE_EMPTY:
561  case BUFFER_STATE_UNSET:
562  break;
563  default:
564  std::ostringstream ss;
565  ss << "Invalid buffer state: ";
566  printHex(&state,1,ss);
567  throw cms::Exception("FEDBuffer") << ss.str();
568  }
569  setBit(bufferPosition+STATE_OFFSET_EMPTY, state&BUFFER_STATE_EMPTY);
571  setBit(bufferPosition+STATE_OFFSET_FULL, state&BUFFER_STATE_FULL);
572  }
573 
574  void FEDBackendStatusRegister::setBit(const uint8_t num, const bool bitSet)
575  {
576  const uint32_t mask = (0x00000001 << num);
577  if (bitSet) data_ |= mask;
578  else data_ &= (~mask);
579  }
580 
582  const FEDBufferState frameAddressFIFOBufferState,
583  const FEDBufferState totalLengthFIFOBufferState,
584  const FEDBufferState trackerHeaderFIFOBufferState,
585  const FEDBufferState l1aBxFIFOBufferState,
586  const FEDBufferState feEventLengthFIFOBufferState,
587  const FEDBufferState feFPGABufferState,
588  const bool backpressure, const bool slinkFull,
589  const bool slinkDown, const bool internalFreeze,
590  const bool trackerHeaderMonitorDataReady, const bool ttcReady)
591  : data_(0)
592  {
593  setInternalFreezeFlag(internalFreeze);
594  setSLinkDownFlag(slinkDown);
595  setSLinkFullFlag(slinkFull);
596  setBackpressureFlag(backpressure);
597  setTTCReadyFlag(ttcReady);
598  setTrackerHeaderMonitorDataReadyFlag(trackerHeaderMonitorDataReady);
599  setQDRMemoryState(qdrMemoryBufferState);
600  setFrameAddressFIFOState(frameAddressFIFOBufferState);
601  setTotalLengthFIFOState(totalLengthFIFOBufferState);
602  setTrackerHeaderFIFOState(trackerHeaderFIFOBufferState);
603  setL1ABXFIFOState(l1aBxFIFOBufferState);
604  setFEEventLengthFIFOState(feEventLengthFIFOBufferState);
605  setFEFPGABufferState(feFPGABufferState);
606  }
607 
608 
609 
610 
611  TrackerSpecialHeader::TrackerSpecialHeader(const uint8_t* headerPointer)
612  {
613  //the buffer format byte is one of the valid values if we assume the buffer is not swapped
614  const bool validFormatByteWhenNotWordSwapped = ( (headerPointer[BUFFERFORMAT] == BUFFER_FORMAT_CODE_NEW) ||
615  (headerPointer[BUFFERFORMAT] == BUFFER_FORMAT_CODE_OLD) );
616  //the buffer format byte is the old value if we assume the buffer is swapped
617  const bool validFormatByteWhenWordSwapped = (headerPointer[BUFFERFORMAT^4] == BUFFER_FORMAT_CODE_OLD);
618  //if the buffer format byte is valid if the buffer is not swapped or it is never valid
619  if (validFormatByteWhenNotWordSwapped || (!validFormatByteWhenNotWordSwapped && !validFormatByteWhenWordSwapped) ) {
620  memcpy(specialHeader_,headerPointer,8);
621  wordSwapped_ = false;
622  } else {
623  memcpy(specialHeader_,headerPointer+4,4);
624  memcpy(specialHeader_+4,headerPointer,4);
625  wordSwapped_ = true;
626  }
627  }
628 
630  {
634  else return BUFFER_FORMAT_OLD_SLINK;
635  }
636  else return BUFFER_FORMAT_INVALID;
637  }
638 
640  {
645  else return HEADER_TYPE_INVALID;
646  }
647 
649  {
650  const uint8_t eventTypeNibble = trackerEventTypeNibble();
651  //if it is scope mode then return as is (it cannot be fake data)
652  if (eventTypeNibble == READOUT_MODE_SCOPE) return FEDReadoutMode(eventTypeNibble);
653  //if not then ignore the last bit which indicates if it is real or fake
654  else {
655  const uint8_t mode = (eventTypeNibble & 0xE);
656  switch(mode) {
661  case READOUT_MODE_SPY:
662  return FEDReadoutMode(mode);
663  default:
664  return READOUT_MODE_INVALID;
665  }
666  }
667  }
668 
670  {
671  const uint8_t eventTypeNibble = trackerEventTypeNibble();
672  //if it is scope mode then it is always real
673  if (eventTypeNibble == READOUT_MODE_SCOPE) return DATA_TYPE_REAL;
674  //in other modes it is the lowest order bit of event type nibble
675  else return FEDDataType(eventTypeNibble & 0x1);
676  }
677 
679  {
680  //check if order in buffer is different
681  if ( ( (bufferFormat()==BUFFER_FORMAT_OLD_VME) && (newBufferFormat!=BUFFER_FORMAT_OLD_VME) ) ||
682  ( (bufferFormat()!=BUFFER_FORMAT_OLD_VME) && (newBufferFormat==BUFFER_FORMAT_OLD_VME) ) ) {
684  }
685  //set appropriate code
686  setBufferFormatByte(newBufferFormat);
687  return *this;
688  }
689 
691  {
692  switch (newBufferFormat) {
696  break;
697  case BUFFER_FORMAT_NEW:
699  break;
700  default:
701  std::ostringstream ss;
702  ss << "Invalid buffer format: ";
703  printHex(&newBufferFormat,1,ss);
704  throw cms::Exception("FEDBuffer") << ss.str();
705  }
706  }
707 
709  {
710  switch(headerType) {
713  case HEADER_TYPE_NONE:
714  setHeaderTypeNibble(headerType);
715  return *this;
716  default:
717  std::ostringstream ss;
718  ss << "Invalid header type: ";
719  printHex(&headerType,1,ss);
720  throw cms::Exception("FEDBuffer") << ss.str();
721  }
722  }
723 
725  {
726  switch(readoutMode) {
727  case READOUT_MODE_SCOPE:
728  //scope mode is always real
729  setReadoutModeBits(readoutMode);
730  setDataTypeBit(true);
735  case READOUT_MODE_SPY:
736  setReadoutModeBits(readoutMode);
737  break;
738  default:
739  std::ostringstream ss;
740  ss << "Invalid readout mode: ";
741  printHex(&readoutMode,1,ss);
742  throw cms::Exception("FEDBuffer") << ss.str();
743  }
744  return *this;
745  }
746 
748  {
749  //if mode is scope then this bit can't be changed
750  if (readoutMode() == READOUT_MODE_SCOPE) return *this;
751  switch (dataType) {
752  case DATA_TYPE_REAL:
753  case DATA_TYPE_FAKE:
754  setDataTypeBit(dataType);
755  return *this;
756  default:
757  std::ostringstream ss;
758  ss << "Invalid data type: ";
759  printHex(&dataType,1,ss);
760  throw cms::Exception("FEDBuffer") << ss.str();
761  }
762  }
763 
765  {
766  const uint8_t mask = 0x1 << internalFEUnitNum;
767  const uint8_t result = ( (apvAddressErrorRegister() & (~mask)) | (error?mask:0x00) );
769  return *this;
770  }
771 
772  TrackerSpecialHeader& TrackerSpecialHeader::setFEEnableForFEUnit(const uint8_t internalFEUnitNum, const bool enabled)
773  {
774  const uint8_t mask = 0x1 << internalFEUnitNum;
775  const uint8_t result = ( (feEnableRegister() & (~mask)) | (enabled?mask:0x00) );
776  setFEEnableRegister(result);
777  return *this;
778  }
779 
780  TrackerSpecialHeader& TrackerSpecialHeader::setFEOverflowForFEUnit(const uint8_t internalFEUnitNum, const bool overflow)
781  {
782  const uint8_t mask = 0x1 << internalFEUnitNum;
783  const uint8_t result = ( (feOverflowRegister() & (~mask)) | (overflow?mask:0x00) );
784  setFEEnableRegister(result);
785  return *this;
786  }
787 
789  const FEDHeaderType headerType, const FEDDataType dataType,
790  const uint8_t address, const uint8_t addressErrorRegister,
791  const uint8_t feEnableRegister, const uint8_t feOverflowRegister,
792  const FEDStatusRegister fedStatusRegister)
793  {
794  memset(specialHeader_,0x00,8);
795  //determine if order is swapped in real buffer
796  wordSwapped_ = (bufferFormat == BUFFER_FORMAT_OLD_VME);
797  //set fields
798  setBufferFormatByte(bufferFormat);
799  setReadoutMode(readoutMode);
800  setHeaderType(headerType);
801  setDataType(dataType);
802  setAPVEAddress(address);
803  setAPVEAddressErrorRegister(addressErrorRegister);
804  setFEEnableRegister(feEnableRegister);
805  setFEOverflowRegister(feOverflowRegister);
806  setFEDStatusRegister(fedStatusRegister);
807  }
808 
809 
810 
811 
813  {
814  switch(eventTypeNibble()) {
817  case DAQ_EVENT_TYPE_TEST:
823  default:
824  return DAQ_EVENT_TYPE_INVALID;
825  }
826  }
827 
829  {
830  header_[7] = ((header_[7] & 0xF0) | evtType);
831  return *this;
832  }
833 
834  FEDDAQHeader& FEDDAQHeader::setL1ID(const uint32_t l1ID)
835  {
836  header_[4] = (l1ID & 0x000000FF);
837  header_[5] = ( (l1ID & 0x0000FF00) >> 8);
838  header_[6] = ( (l1ID & 0x00FF0000) >> 16);
839  return *this;
840  }
841 
842  FEDDAQHeader& FEDDAQHeader::setBXID(const uint16_t bxID)
843  {
844  header_[3] = ( (bxID & 0x0FF0) >> 4);
845  header_[2] = ( (header_[2] & 0x0F) | ( (bxID & 0x000F) << 4) );
846  return *this;
847  }
848 
849  FEDDAQHeader& FEDDAQHeader::setSourceID(const uint16_t sourceID)
850  {
851  header_[2] = ( (header_[2] & 0xF0) | ( (sourceID & 0x0F00) >> 8) );
852  header_[1] = (sourceID & 0x00FF);
853  return *this;
854  }
855 
856  FEDDAQHeader::FEDDAQHeader(const uint32_t l1ID, const uint16_t bxID, const uint16_t sourceID, const FEDDAQEventType evtType)
857  {
858  //clear everything (FOV,H,x,$ all set to 0)
859  memset(header_,0x0,8);
860  //set the BoE nibble to indicate this is the last fragment
861  header_[7] = 0x50;
862  //set variable fields vith values supplied
863  setEventType(evtType);
864  setL1ID(l1ID);
865  setBXID(bxID);
866  setSourceID(sourceID);
867  }
868 
869 
870 
871 
873  {
874  switch(ttsNibble()) {
875  case TTS_DISCONNECTED0:
876  case TTS_WARN_OVERFLOW:
877  case TTS_OUT_OF_SYNC:
878  case TTS_BUSY:
879  case TTS_READY:
880  case TTS_ERROR:
881  case TTS_DISCONNECTED1:
882  return FEDTTSBits(ttsNibble());
883  default:
884  return TTS_INVALID;
885  }
886  }
887 
888  FEDDAQTrailer::FEDDAQTrailer(const uint32_t eventLengthIn64BitWords, const uint16_t crc, const FEDTTSBits ttsBits,
889  const bool slinkTransmissionError, const bool badFEDID, const bool slinkCRCError,
890  const uint8_t eventStatusNibble)
891  {
892  //clear everything (T,x,$ all set to 0)
893  memset(trailer_,0x0,8);
894  //set the EoE nibble to indicate this is the last fragment
895  trailer_[7] = 0xA0;
896  //set variable fields vith values supplied
897  setEventLengthIn64BitWords(eventLengthIn64BitWords);
898  setEventStatusNibble(eventStatusNibble);
899  setTTSBits(ttsBits);
900  setCRC(crc);
901  setSLinkTransmissionErrorBit(slinkTransmissionError);
902  setBadSourceIDBit(badFEDID);
903  setSLinkCRCErrorBit(slinkCRCError);
904  }
905 
906  FEDDAQTrailer& FEDDAQTrailer::setEventLengthIn64BitWords(const uint32_t eventLengthIn64BitWords)
907  {
908  trailer_[4] = (eventLengthIn64BitWords & 0x000000FF);
909  trailer_[5] = ( (eventLengthIn64BitWords & 0x0000FF00) >> 8);
910  trailer_[6] = ( (eventLengthIn64BitWords & 0x00FF0000) >> 16);
911  return *this;
912  }
913 
915  {
916  trailer_[2] = (crc & 0x00FF);
917  trailer_[3] = ( (crc >> 8) & 0x00FF );
918  return *this;
919  }
920 
922  {
923  if (bitSet) trailer_[1] |= 0x80;
924  else trailer_[1] &= (~0x80);
925  return *this;
926  }
927 
929  {
930  if (bitSet) trailer_[1] |= 0x40;
931  else trailer_[1] &= (~0x40);
932  return *this;
933  }
934 
936  {
937  if (bitSet) trailer_[0] |= 0x04;
938  else trailer_[0] &= (~0x40);
939  return *this;
940  }
941 
942  FEDDAQTrailer& FEDDAQTrailer::setEventStatusNibble(const uint8_t eventStatusNibble)
943  {
944  trailer_[1] = ( (trailer_[1] & 0xF0) | (eventStatusNibble & 0x0F) );
945  return *this;
946  }
947 
949  {
950  trailer_[0] = ( (trailer_[0] & 0x0F) | (ttsBits & 0xF0) );
951  return *this;
952  }
953 
954 
955 
956 
958  {
959  }
960 
962  {
964  }
965 
966  void FEDAPVErrorHeader::print(std::ostream& os) const
967  {
969  }
970 
972  {
973  return new FEDAPVErrorHeader(*this);
974  }
975 
976  bool FEDAPVErrorHeader::checkStatusBits(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
977  {
978  //3 bytes per FE unit, channel order is reversed in FE unit data, 2 bits per channel
979  const uint16_t bitNumber = (internalFEDChannelNum/FEDCH_PER_FEUNIT)*24 + (FEDCH_PER_FEUNIT-1-(internalFEDChannelNum%FEDCH_PER_FEUNIT))*2 + apvNum;
980  //bit high means no error
981  return (header_[bitNumber/8] & (0x01<<(bitNumber%8)) );
982  }
983 
985  {
986  return (checkStatusBits(internalFEDChannelNum,0) && checkStatusBits(internalFEDChannelNum,1));
987  }
988 
989  const uint8_t* FEDAPVErrorHeader::data() const
990  {
991  return header_;
992  }
993 
994  FEDAPVErrorHeader::FEDAPVErrorHeader(const std::vector<bool> apvsGood)
995  {
997  for (uint8_t iCh = 0; iCh < FEDCH_PER_FED; iCh++) {
998  setAPVStatusBit(iCh,0,apvsGood[iCh*2]);
999  setAPVStatusBit(iCh,1,apvsGood[iCh*2+1]);
1000  }
1001  }
1002 
1003  FEDAPVErrorHeader& FEDAPVErrorHeader::setAPVStatusBit(const uint8_t internalFEDChannelNum, const uint8_t apvNum, const bool apvGood)
1004  {
1005  //3 bytes per FE unit, channel order is reversed in FE unit data, 2 bits per channel
1006  const uint16_t bitNumber = (internalFEDChannelNum/FEDCH_PER_FED)*24 + (FEDCH_PER_FED-1-(internalFEDChannelNum%FEDCH_PER_FED))*2+apvNum;
1007  const uint8_t byteNumber = bitNumber/8;
1008  const uint8_t bitInByte = bitNumber%8;
1009  const uint8_t mask = (0x01 << bitInByte);
1010  header_[byteNumber] = ( (header_[byteNumber] & (~mask)) | (apvGood?mask:0x00) );
1011  return *this;
1012  }
1013 
1015  {
1016  //if channel is unlocked then set both APV bits bad
1017  if ( (!(status & CHANNEL_STATUS_LOCKED)) || (!(status & CHANNEL_STATUS_IN_SYNC)) ) {
1018  setAPVStatusBit(internalFEDChannelNum,0,false);
1019  setAPVStatusBit(internalFEDChannelNum,1,false);
1020  return;
1021  } else {
1022  if ( (status & CHANNEL_STATUS_APV0_ADDRESS_GOOD) && (status & CHANNEL_STATUS_APV0_NO_ERROR_BIT) ) {
1023  setAPVStatusBit(internalFEDChannelNum,0,true);
1024  } else {
1025  setAPVStatusBit(internalFEDChannelNum,0,false);
1026  }
1027  if ( (status & CHANNEL_STATUS_APV1_ADDRESS_GOOD) && (status & CHANNEL_STATUS_APV1_NO_ERROR_BIT) ) {
1028  setAPVStatusBit(internalFEDChannelNum,1,true);
1029  } else {
1030  setAPVStatusBit(internalFEDChannelNum,1,false);
1031  }
1032  }
1033  }
1034 
1035  //These methods do nothing as the values in question are in present in the APV Error header.
1036  //The methods exist so that users of the base class can set the values without caring which type of header they have and so if they are needed.
1037  void FEDAPVErrorHeader::setFEUnitMajorityAddress(const uint8_t internalFEUnitNum, const uint8_t address)
1038  {
1039  return;
1040  }
1042  {
1043  return;
1044  }
1045  void FEDAPVErrorHeader::setFEUnitLength(const uint8_t internalFEUnitNum, const uint16_t length)
1046  {
1047  return;
1048  }
1049 
1050 
1052  {
1053  }
1054 
1056  {
1058  }
1059 
1060  void FEDFullDebugHeader::print(std::ostream& os) const
1061  {
1063  }
1064 
1066  {
1067  return new FEDFullDebugHeader(*this);
1068  }
1069 
1070  bool FEDFullDebugHeader::checkStatusBits(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
1071  {
1072  return ( !unlockedFromBit(internalFEDChannelNum) &&
1073  !outOfSyncFromBit(internalFEDChannelNum) &&
1074  !apvError(internalFEDChannelNum,apvNum) &&
1075  !apvAddressError(internalFEDChannelNum,apvNum) );
1076  }
1077 
1079  {
1080  //return ( !unlockedFromBit(internalFEDChannelNum) &&
1081  // !outOfSyncFromBit(internalFEDChannelNum) &&
1082  // !apvErrorFromBit(internalFEDChannelNum,0) &&
1083  // !apvAddressErrorFromBit(internalFEDChannelNum,0) &&
1084  // !apvErrorFromBit(internalFEDChannelNum,1) &&
1085  // !apvAddressErrorFromBit(internalFEDChannelNum,1) );
1086  return (getChannelStatus(internalFEDChannelNum) == CHANNEL_STATUS_NO_PROBLEMS);
1087  }
1088 
1090  {
1091  const uint8_t* pFEWord = feWord(internalFEDChannelNum/FEDCH_PER_FEUNIT);
1092  const uint8_t feUnitChanNum = internalFEDChannelNum % FEDCH_PER_FEUNIT;
1093  const uint8_t startByteInFEWord = (FEDCH_PER_FEUNIT-1 - feUnitChanNum) * 6 / 8;
1094  switch ( (FEDCH_PER_FEUNIT-1-feUnitChanNum) % 4 ) {
1095  case 0:
1096  return FEDChannelStatus( pFEWord[startByteInFEWord] & 0x3F );
1097  case 1:
1098  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xC0) >> 6) | ((pFEWord[startByteInFEWord+1] & 0x0F) << 2) );
1099  case 2:
1100  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xF0) >> 4) | ((pFEWord[startByteInFEWord+1] & 0x03) << 4) );
1101  case 3:
1102  return FEDChannelStatus( (pFEWord[startByteInFEWord] & 0xFC) >> 2 );
1103  //stop compiler warning
1104  default:
1105  return FEDChannelStatus(0);
1106  }
1107  /*const uint8_t feUnitChanNum = internalFEDChannelNum / FEDCH_PER_FEUNIT;
1108  const uint8_t* pFEWord = feWord(feUnitChanNum);
1109  const uint8_t startByteInFEWord = feUnitChanNum * 3 / 4;
1110  //const uint8_t shift = ( 6 - ((feUnitChanNum-1)%4) );
1111  //const uint16_t mask = ( 0x003F << shift );
1112  //uint8_t result = ( (pFEWord[startByteInFEWord] & (mask&0x00FF)) >> shift );
1113  //result |= ( (pFEWord[startByteInFEWord+1] & (mask>>8)) << (8-shift) );
1114  switch (feUnitChanNum % 4) {
1115  case 0:
1116  return FEDChannelStatus( pFEWord[startByteInFEWord] & 0x3F );
1117  case 1:
1118  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xC0) >> 6) | ((pFEWord[startByteInFEWord+1] & 0x0F) << 2) );
1119  case 2:
1120  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xF0) >> 4) | ((pFEWord[startByteInFEWord+1] & 0x03) << 4) );
1121  case 3:
1122  return FEDChannelStatus( (pFEWord[startByteInFEWord] & 0xFC) >> 2 );
1123  //stop compiler warning
1124  default:
1125  return FEDChannelStatus(0);
1126  }*/
1127  }
1128 
1129  const uint8_t* FEDFullDebugHeader::data() const
1130  {
1131  return header_;
1132  }
1133 
1134  FEDFullDebugHeader::FEDFullDebugHeader(const std::vector<uint16_t> feUnitLengths, const std::vector<uint8_t> feMajorityAddresses,
1135  const std::vector<FEDChannelStatus> channelStatus, const FEDBackendStatusRegister beStatusRegister,
1136  const uint32_t daqRegister, const uint32_t daqRegister2)
1137  {
1139  setBEStatusRegister(beStatusRegister);
1140  setDAQRegister(daqRegister);
1141  setDAQRegister2(daqRegister2);
1142  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1143  setFEUnitLength(iFE,feUnitLengths[iFE]);
1144  setFEUnitMajorityAddress(iFE,feMajorityAddresses[iFE]);
1145  }
1146  for (uint8_t iCh = 0; iCh < FEDCH_PER_FED; iCh++) {
1147  setChannelStatus(iCh,channelStatus[iCh]);
1148  }
1149  }
1150 
1152  {
1153  setUnlocked(internalFEDChannelNum, !(status&CHANNEL_STATUS_LOCKED) );
1154  setOutOfSync(internalFEDChannelNum, !(status&CHANNEL_STATUS_IN_SYNC) );
1155  setAPVAddressError(internalFEDChannelNum,1, !(status&CHANNEL_STATUS_APV1_ADDRESS_GOOD) );
1156  setAPVAddressError(internalFEDChannelNum,0, !(status&CHANNEL_STATUS_APV0_ADDRESS_GOOD) );
1157  setAPVError(internalFEDChannelNum,1, !(status&CHANNEL_STATUS_APV1_NO_ERROR_BIT) );
1158  setAPVError(internalFEDChannelNum,0, !(status&CHANNEL_STATUS_APV0_NO_ERROR_BIT) );
1159  }
1160 
1161  void FEDFullDebugHeader::setFEUnitMajorityAddress(const uint8_t internalFEUnitNum, const uint8_t address)
1162  {
1163  feWord(internalFEUnitNum)[9] = address;
1164  }
1165 
1167  {
1168  set32BitWordAt(feWord(0)+10,beStatusRegister);
1169  }
1170 
1171  void FEDFullDebugHeader::setDAQRegister(const uint32_t daqRegister)
1172  {
1173  set32BitWordAt(feWord(7)+10,daqRegister);
1174  }
1175 
1176  void FEDFullDebugHeader::setDAQRegister2(const uint32_t daqRegister2)
1177  {
1178  set32BitWordAt(feWord(6)+10,daqRegister2);
1179  }
1180 
1181  void FEDFullDebugHeader::setFEUnitLength(const uint8_t internalFEUnitNum, const uint16_t length)
1182  {
1183  feWord(internalFEUnitNum)[15] = ( (length & 0xFF00) >> 8);
1184  feWord(internalFEUnitNum)[14] = (length & 0x00FF);
1185  }
1186 
1187  void FEDFullDebugHeader::setBit(const uint8_t internalFEDChannelNum, const uint8_t bit, const bool value)
1188  {
1189  const uint8_t bitInFeWord = (FEDCH_PER_FEUNIT-1 - (internalFEDChannelNum%FEDCH_PER_FEUNIT)) * 6 + bit;
1190  uint8_t& byte = *(feWord(internalFEDChannelNum / FEDCH_PER_FEUNIT)+(bitInFeWord/8));
1191  const uint8_t mask = (0x1 << bitInFeWord%8);
1192  byte = ( (byte & (~mask)) | (value?mask:0x0) );
1193  }
1194 
1196  {
1197  }
1198 
1199 
1200 
1201 
1202  FEDBufferBase::FEDBufferBase(const uint8_t* fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat)
1203  : channels_(FEDCH_PER_FED,FEDChannel(NULL,0,0)),
1204  originalBuffer_(fedBuffer),
1205  bufferSize_(fedBufferSize)
1206  {
1207  init(fedBuffer,fedBufferSize,allowUnrecognizedFormat);
1208  }
1209 
1210  FEDBufferBase::FEDBufferBase(const uint8_t* fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat, const bool fillChannelVector)
1211  : originalBuffer_(fedBuffer),
1212  bufferSize_(fedBufferSize)
1213  {
1214  init(fedBuffer,fedBufferSize,allowUnrecognizedFormat);
1215  if (fillChannelVector) channels_.assign(FEDCH_PER_FED,FEDChannel(NULL,0,0));
1216  }
1217 
1218  void FEDBufferBase::init(const uint8_t* fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat)
1219  {
1220  //min buffer length. DAQ header, DAQ trailer, tracker special header.
1221  static const size_t MIN_BUFFER_SIZE = 8+8+8;
1222  //check size is non zero and data pointer is not NULL
1223  if (!originalBuffer_) throw cms::Exception("FEDBuffer") << "Buffer pointer is NULL.";
1224  if (bufferSize_ < MIN_BUFFER_SIZE) {
1225  std::ostringstream ss;
1226  ss << "Buffer is too small. "
1227  << "Min size is " << MIN_BUFFER_SIZE << ". "
1228  << "Buffer size is " << bufferSize_ << ". ";
1229  throw cms::Exception("FEDBuffer") << ss.str();
1230  }
1231 
1232  //construct tracker special header using second 64 bit word
1234 
1235  //check the buffer format
1237  if (bufferFormat == BUFFER_FORMAT_INVALID && !allowUnrecognizedFormat) {
1238  std::ostringstream ss;
1239  ss << "Buffer format not recognized. "
1240  << "Tracker special header: " << specialHeader_;
1241  throw cms::Exception("FEDBuffer") << ss.str();
1242  }
1243  //swap the buffer words so that the whole buffer is in slink ordering
1244  if ( (bufferFormat == BUFFER_FORMAT_OLD_VME) || (bufferFormat == BUFFER_FORMAT_NEW) ) {
1245  uint8_t* newBuffer = new uint8_t[bufferSize_];
1246  const uint32_t* originalU32 = reinterpret_cast<const uint32_t*>(originalBuffer_);
1247  const size_t sizeU32 = bufferSize_/4;
1248  uint32_t* newU32 = reinterpret_cast<uint32_t*>(newBuffer);
1249  if (bufferFormat == BUFFER_FORMAT_OLD_VME) {
1250  //swap whole buffer
1251  for (size_t i = 0; i < sizeU32; i+=2) {
1252  newU32[i] = originalU32[i+1];
1253  newU32[i+1] = originalU32[i];
1254  }
1255  }
1256  if (bufferFormat == BUFFER_FORMAT_NEW) {
1257  //copy DAQ header
1258  memcpy(newU32,originalU32,8);
1259  //copy DAQ trailer
1260  memcpy(newU32+sizeU32-2,originalU32+sizeU32-2,8);
1261  //swap the payload
1262  for (size_t i = 2; i < sizeU32-2; i+=2) {
1263  newU32[i] = originalU32[i+1];
1264  newU32[i+1] = originalU32[i];
1265  }
1266  }
1267  orderedBuffer_ = newBuffer;
1268  } //if ( (bufferFormat == BUFFER_FORMAT_OLD_VME) || (bufferFormat == BUFFER_FORMAT_NEW) )
1269  else {
1271  }
1272 
1273  //construct header object at begining of buffer
1275  //construct trailer object using last 64 bit word of buffer
1277  }
1278 
1280  {
1281  //if the buffer was coppied and swapped then delete the copy
1283  }
1284 
1285  void FEDBufferBase::print(std::ostream& os) const
1286  {
1287  os << "buffer format: " << bufferFormat() << std::endl;
1288  os << "Buffer size: " << bufferSize() << " bytes" << std::endl;
1289  os << "Event length from DAQ trailer: " << daqEventLengthInBytes() << " bytes" << std::endl;
1290  os << "Source ID: " << daqSourceID() << std::endl;
1291  os << "Header type: " << headerType() << std::endl;
1292  os << "Readout mode: " << readoutMode() << std::endl;
1293  os << "Data type: " << dataType() << std::endl;
1294  os << "DAQ event type: " << daqEventType() << std::endl;
1295  os << "TTS state: " << daqTTSState() << std::endl;
1296  os << "L1 ID: " << daqLvl1ID() << std::endl;
1297  os << "BX ID: " << daqBXID() << std::endl;
1298  os << "FED status register flags: "; fedStatusRegister().printFlags(os); os << std::endl;
1299  os << "APVe Address: " << uint16_t(apveAddress()) << std::endl;
1300  os << "Enabled FE units: " << uint16_t(nFEUnitsEnabled()) << std::endl;
1301  }
1302 
1304  {
1305  uint8_t result = 0;
1306  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1307  if (feEnabled(iFE)) result++;
1308  }
1309  return result;
1310  }
1311 
1313  {
1314  return ( (daqSourceID() >= FED_ID_MIN) &&
1315  (daqSourceID() <= FED_ID_MAX) );
1316  }
1317 
1319  {
1320  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1321  if (!feEnabled(iFE)) continue;
1322  if (majorityAddressErrorForFEUnit(iFE)) return false;
1323  }
1324  return true;
1325  }
1326 
1328  {
1329  const uint8_t feUnit = internalFEDChannelNum/FEDCH_PER_FEUNIT;
1330  return ( !majorityAddressErrorForFEUnit(feUnit) && feEnabled(feUnit) && !feOverflow(feUnit) );
1331  }
1332 
1334  {
1336  }
1337 
1338  std::string FEDBufferBase::checkSummary() const
1339  {
1340  std::ostringstream summary;
1341  summary << "Check buffer type valid: " << ( checkBufferFormat() ? "passed" : "FAILED" ) << std::endl;
1342  summary << "Check header format valid: " << ( checkHeaderType() ? "passed" : "FAILED" ) << std::endl;
1343  summary << "Check readout mode valid: " << ( checkReadoutMode() ? "passed" : "FAILED" ) << std::endl;
1344  //summary << "Check APVe address valid: " << ( checkAPVEAddressValid() ? "passed" : "FAILED" ) << std::endl;
1345  summary << "Check FE unit majority addresses: " << ( checkMajorityAddresses() ? "passed" : "FAILED" ) << std::endl;
1346  if (!checkMajorityAddresses()) {
1347  summary << "FEs with majority address error: ";
1348  unsigned int badFEs = 0;
1349  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1350  if (!feEnabled(iFE)) continue;
1351  if (majorityAddressErrorForFEUnit(iFE)) {
1352  summary << uint16_t(iFE) << " ";
1353  badFEs++;
1354  }
1355  }
1356  summary << std::endl;
1357  summary << "Number of FE Units with bad addresses: " << badFEs << std::endl;
1358  }
1359  summary << "Check for FE unit buffer overflows: " << ( checkNoFEOverflows() ? "passed" : "FAILED" ) << std::endl;
1360  if (!checkNoFEOverflows()) {
1361  summary << "FEs which overflowed: ";
1362  unsigned int badFEs = 0;
1363  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1364  if (feOverflow(iFE)) {
1365  summary << uint16_t(iFE) << " ";
1366  badFEs++;
1367  }
1368  }
1369  summary << std::endl;
1370  summary << "Number of FE Units which overflowed: " << badFEs << std::endl;
1371  }
1372  summary << "Check for S-Link CRC errors: " << ( checkNoSlinkCRCError() ? "passed" : "FAILED" ) << std::endl;
1373  summary << "Check for S-Link transmission error: " << ( checkNoSLinkTransmissionError() ? "passed" : "FAILED" ) << std::endl;
1374  summary << "Check CRC: " << ( checkCRC() ? "passed" : "FAILED" ) << std::endl;
1375  summary << "Check source ID is FED ID: " << ( checkSourceIDs() ? "passed" : "FAILED" ) << std::endl;
1376  summary << "Check for unexpected source ID at FRL: " << ( checkNoUnexpectedSourceID() ? "passed" : "FAILED" ) << std::endl;
1377  summary << "Check there are no extra headers or trailers: " << ( checkNoExtraHeadersOrTrailers() ? "passed" : "FAILED" ) << std::endl;
1378  summary << "Check length from trailer: " << ( checkLengthFromTrailer() ? "passed" : "FAILED" ) << std::endl;
1379  return summary.str();
1380  }
1381 
1382 
1383 
1384 
1385  uint16_t FEDChannel::cmMedian(const uint8_t apvIndex) const
1386  {
1388  std::ostringstream ss;
1389  ss << "Request for CM median from channel with non-ZS packet code. "
1390  << "Packet code is " << uint16_t(packetCode()) << "."
1391  << std::endl;
1392  throw cms::Exception("FEDBuffer") << ss.str();
1393  }
1394  if (apvIndex > 1) {
1395  std::ostringstream ss;
1396  ss << "Channel APV index out of range when requesting CM median for APV. "
1397  << "Channel APV index is " << uint16_t(apvIndex) << "."
1398  << std::endl;
1399  throw cms::Exception("FEDBuffer") << ss.str();
1400  }
1401  uint16_t result = 0;
1402  //CM median is 10 bits with lowest order byte first. First APV CM median starts in 4th byte of channel data
1403  result |= data_[(offset_+3+2*apvIndex)^7];
1404  result |= ( ((data_[(offset_+4+2*apvIndex)^7]) << 8) & 0x300 );
1405  return result;
1406  }
1407 
1408 }
FEDBackendStatusRegister & setFEFPGABufferState(const FEDBufferState state)
FEDBackendStatusRegister & setQDRMemoryState(const FEDBufferState state)
FEDBackendStatusRegister & setL1ABXFIFOState(const FEDBufferState state)
bool feDataMissingFlag(const uint8_t internalFEUnitNum) const
virtual void setDAQRegister(const uint32_t daqRegister)
int i
Definition: DBlmapReader.cc:9
virtual void print(std::ostream &os) const
TrackerSpecialHeader & setDataType(const FEDDataType dataType)
static const size_t APV_ERROR_HEADER_SIZE_IN_BYTES
void printHexValue(const uint8_t value, std::ostream &os)
void printFlags(std::ostream &os) const
void setBit(const uint8_t num, const bool bitSet)
char * address
Definition: mlp_lapack.h:14
static const uint16_t FED_ID_MIN
void setOutOfSync(const uint8_t internalFEDChannelNum, const bool value)
virtual void setFEUnitLength(const uint8_t internalFEUnitNum, const uint16_t length)
void setQDRMemoryEmptyFlag(const bool bitSet)
virtual std::string checkSummary() const
FEDBackendStatusRegister & setFrameAddressFIFOState(const FEDBufferState state)
TrackerSpecialHeader & setFEOverflowRegister(const uint8_t feOverflowRegister)
bool unlockedFromBit(const uint8_t internalFEDChannelNum) const
virtual bool checkStatusBits(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
FEDBackendStatusRegister & setTrackerHeaderFIFOState(const FEDBufferState state)
FEDFullDebugHeader(const uint8_t *headerBuffer)
virtual bool checkChannelStatusBits(const uint8_t internalFEDChannelNum) const
FEDDAQTrailer & setSLinkCRCErrorBit(const bool bitSet)
FEDDAQTrailer & setTTSBits(const FEDTTSBits ttsBits)
const uint8_t * feWord(const uint8_t internalFEUnitNum) const
virtual void setBEStatusRegister(const FEDBackendStatusRegister beStatusRegister)
FEDReadoutMode readoutMode() const
FEDDataType fedDataTypeFromString(const std::string &dataTypeString)
bool getBit(const uint8_t num) const
void setAPVError(const uint8_t internalFEDChannelNum, const uint8_t apvNum, const bool value)
virtual void setFEUnitMajorityAddress(const uint8_t internalFEUnitNum, const uint8_t address)
uint8_t internalFEDChannelNum(const uint8_t internalFEUnitNum, const uint8_t internalFEUnitChannelNum)
TrackerSpecialHeader & setAPVEAddress(const uint8_t address)
FEDDAQTrailer & setSLinkTransmissionErrorBit(const bool bitSet)
#define NULL
Definition: scimark2.h:8
FEDHeaderType fedHeaderTypeFromString(const std::string &headerTypeString)
void printFlags(std::ostream &os) const
TrackerSpecialHeader & setBufferFormat(const FEDBufferFormat newBufferFormat)
uint16_t calculateFEDBufferCRC(const uint8_t *buffer, const size_t lengthInBytes)
std::ostream & operator<<(std::ostream &os, const FEDBufferFormat &value)
virtual bool checkStatusBits(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
bool apvError(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
FEDAPVErrorHeader(const uint8_t *headerBuffer)
FEDBufferFormat fedBufferFormatFromString(const std::string &bufferFormatString)
void setBit(const uint8_t num, const bool bitSet)
FEDDAQEventType daqEventType() const
TrackerSpecialHeader & setHeaderType(const FEDHeaderType headerType)
FEDReadoutMode fedReadoutModeFromString(const std::string &readoutModeString)
unsigned char byte
FEDBackendStatusRegister(const uint32_t backendStatusRegister)
uint16_t cmMedian(const uint8_t apvIndex) const
virtual void setFEUnitLength(const uint8_t internalFEUnitNum, const uint16_t length)
virtual void print(std::ostream &os) const
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED
virtual FEDFullDebugHeader * clone() const
virtual void setBEStatusRegister(const FEDBackendStatusRegister beStatusRegister)
FEDBackendStatusRegister & setTotalLengthFIFOState(const FEDBufferState state)
TrackerSpecialHeader & setFEEnableForFEUnit(const uint8_t internalFEUnitNum, const bool enabled)
FEDDAQTrailer & setEventLengthIn64BitWords(const uint32_t eventLengthIn64BitWords)
FEDDAQTrailer & setEventStatusNibble(const uint8_t eventStatusNibble)
bool majorityAddressErrorForFEUnit(const uint8_t internalFEUnitNum) const
void setUnlocked(const uint8_t internalFEDChannelNum, const bool value)
FEDBackendStatusRegister & setTTCReadyFlag(const bool bitSet)
FEDDAQTrailer & setCRC(const uint16_t crc)
TrackerSpecialHeader & setAPVEAddressErrorRegister(const uint8_t addressErrorRegister)
TrackerSpecialHeader & setFEDStatusRegister(const FEDStatusRegister fedStatusRegister)
TrackerSpecialHeader & setAPVAddressErrorForFEUnit(const uint8_t internalFEUnitNum, const bool error)
FEDStatusRegister & setL1ABXFIFOBufferState(const FEDBufferState state)
FEDAPVErrorHeader & setAPVStatusBit(const uint8_t internalFEDChannelNum, const uint8_t apvNum, const bool apvGood)
static const uint16_t FEUNITS_PER_FED
FEDBufferState qdrMemoryState() const
tuple result
Definition: query.py:137
void setQDRMemoryFullFlag(const bool bitSet)
bool feEnabled(const uint8_t internalFEUnitNum) const
virtual void setDAQRegister2(const uint32_t daqRegister2)
void setL1ABXFIFOFullFlag(const bool bitSet)
static const uint8_t BUFFER_FORMAT_CODE_NEW
FEDBufferBase(const uint8_t *fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat=false)
FEDHeaderType headerType() const
void setAPVAddressError(const uint8_t internalFEDChannelNum, const uint8_t apvNum, const bool value)
void printFlagsForBuffer(const FEDBufferState bufferState, const std::string name, std::ostream &os) const
virtual void setFEUnitMajorityAddress(const uint8_t internalFEUnitNum, const uint8_t address)
void setL1ABXFIFOPartialFullFlag(const bool bitSet)
FEDBufferState l1aBxFIFOState() const
void setHeaderTypeNibble(const uint8_t value)
FEDDAQEventType fedDAQEventTypeFromString(const std::string &daqEventTypeString)
std::vector< FEDChannel > channels_
void setBufferFormatByte(const FEDBufferFormat newBufferFormat)
FEDBufferState getBufferState(const uint8_t bufferPosition) const
FEDDAQTrailer & setBadSourceIDBit(const bool bitSet)
static const size_t FULL_DEBUG_HEADER_SIZE_IN_BYTES
void printHex(const void *pointer, const size_t length, std::ostream &os)
static const uint16_t FEDCH_PER_FEUNIT
FEDBackendStatusRegister & setTrackerHeaderMonitorDataReadyFlag(const bool bitSet)
void setReadoutModeBits(const uint8_t value)
bool apvAddressError(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
virtual void setChannelStatus(const uint8_t internalFEDChannelNum, const FEDChannelStatus status)
FEDDAQHeader & setEventType(const FEDDAQEventType evtType)
FEDDAQHeader & setL1ID(const uint32_t l1ID)
long long int num
Definition: procUtils.cc:71
unsigned short compute_crc_8bit(unsigned short crc, unsigned char data)
Definition: CRC16.h:79
FEDBackendStatusRegister & setSLinkFullFlag(const bool bitSet)
FEDDAQHeader & setSourceID(const uint16_t sourceID)
char state
Definition: procUtils.cc:75
void setBufferSate(const uint8_t bufferPosition, const FEDBufferState state)
virtual void setChannelStatus(const uint8_t internalFEDChannelNum, const FEDChannelStatus status)
void init(const uint8_t *fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat)
uint8_t header_[FULL_DEBUG_HEADER_SIZE_IN_BYTES]
TrackerSpecialHeader specialHeader_
virtual const uint8_t * data() const
FEDChannelStatus getChannelStatus(const uint8_t internalFEDChannelNum) const
virtual void print(std::ostream &os) const
static const uint16_t FEDCH_PER_FED
TrackerSpecialHeader & setFEOverflowForFEUnit(const uint8_t internalFEUnitNum, const bool overflow)
virtual bool channelGood(const uint8_t internalFEDChannelNum) const
FEDBackendStatusRegister & setFEEventLengthFIFOState(const FEDBufferState state)
virtual const uint8_t * data() const
void printHexWord(const uint8_t *pointer, const size_t lengthInBytes, std::ostream &os)
static const uint16_t FED_ID_MAX
FEDDAQHeader & setBXID(const uint16_t bxID)
static const uint8_t BUFFER_FORMAT_CODE_OLD
void setBit(const uint8_t internalFEDChannelNum, const uint8_t bit, const bool value)
tuple status
Definition: ntuplemaker.py:245
FEDStatusRegister & setQDRMemoryBufferState(const FEDBufferState state)
virtual FEDAPVErrorHeader * clone() const
FEDDAQEventType eventType() const
static void set32BitWordAt(uint8_t *startOfWord, const uint32_t value)
uint8_t header_[APV_ERROR_HEADER_SIZE_IN_BYTES]
FEDBackendStatusRegister & setInternalFreezeFlag(const bool bitSet)
void setL1ABXFIFOEmptyFlag(const bool bitSet)
virtual bool checkChannelStatusBits(const uint8_t internalFEDChannelNum) const
FEDBackendStatusRegister & setBackpressureFlag(const bool bitSet)
TrackerSpecialHeader & setReadoutMode(const FEDReadoutMode readoutMode)
bool outOfSyncFromBit(const uint8_t internalFEDChannelNum) const
bool feOverflow(const uint8_t internalFEUnitNum) const
FEDStatusRegister fedStatusRegister() const
void setQDRMemoryPartialFullFlag(const bool bitSet)
FEDBackendStatusRegister & setSLinkDownFlag(const bool bitSet)
T w() const
FEDBufferFormat bufferFormat() const
TrackerSpecialHeader & setFEEnableRegister(const uint8_t feEnableRegister)