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 << "PreMix raw";
156  break;
158  os << "Invalid";
159  break;
160  default:
161  os << "Unrecognized";
162  os << " (";
163  printHexValue(value,os);
164  os << ")";
165  break;
166  }
167  return os;
168  }
169 
170  std::ostream& operator<<(std::ostream& os, const FEDDataType& value)
171  {
172  switch (value) {
173  case DATA_TYPE_REAL:
174  os << "Real data";
175  break;
176  case DATA_TYPE_FAKE:
177  os << "Fake data";
178  break;
179  default:
180  os << "Unrecognized";
181  os << " (";
182  printHexValue(value,os);
183  os << ")";
184  break;
185  }
186  return os;
187  }
188 
189  std::ostream& operator<<(std::ostream& os, const FEDDAQEventType& value)
190  {
191  switch (value) {
193  os << "Physics trigger";
194  break;
196  os << "Calibration trigger";
197  break;
198  case DAQ_EVENT_TYPE_TEST:
199  os << "Test trigger";
200  break;
202  os << "Technical trigger";
203  break;
205  os << "Simulated event";
206  break;
208  os << "Traced event";
209  break;
211  os << "Error";
212  break;
214  os << "Unknown";
215  break;
216  default:
217  os << "Unrecognized";
218  os << " (";
219  printHexValue(value,os);
220  os << ")";
221  break;
222  }
223  return os;
224  }
225 
226  std::ostream& operator<<(std::ostream& os, const FEDTTSBits& value)
227  {
228  switch (value) {
229  case TTS_DISCONNECTED0:
230  os << "Disconected 0";
231  break;
232  case TTS_WARN_OVERFLOW:
233  os << "Warning overflow";
234  break;
235  case TTS_OUT_OF_SYNC:
236  os << "Out of sync";
237  break;
238  case TTS_BUSY:
239  os << "Busy";
240  break;
241  case TTS_READY:
242  os << "Ready";
243  break;
244  case TTS_ERROR:
245  os << "Error";
246  break;
247  case TTS_INVALID:
248  os << "Invalid";
249  break;
250  case TTS_DISCONNECTED1:
251  os << "Disconected 1";
252  break;
253  default:
254  os << "Unrecognized";
255  os << " (";
256  printHexValue(value,os);
257  os << ")";
258  break;
259  }
260  return os;
261  }
262 
263  std::ostream& operator<<(std::ostream& os, const FEDBufferState& value)
264  {
265  switch (value) {
266  case BUFFER_STATE_UNSET:
267  os << "Unset";
268  break;
269  case BUFFER_STATE_EMPTY:
270  os << "Empty";
271  break;
273  os << "Partial Full";
274  break;
275  case BUFFER_STATE_FULL:
276  os << "Full";
277  break;
278  default:
279  os << "Unrecognized";
280  os << " (";
281  printHexValue(value,os);
282  os << ")";
283  break;
284  }
285  return os;
286  }
287 
288  std::ostream& operator<<(std::ostream& os, const FEDChannelStatus& value)
289  {
290  if (!(value&CHANNEL_STATUS_LOCKED)) os << "Unlocked ";
291  if (!(value&CHANNEL_STATUS_IN_SYNC)) os << "Out-of-sync ";
292  if (!(value&CHANNEL_STATUS_APV1_ADDRESS_GOOD)) os << "APV 1 bad address ";
293  if (!(value&CHANNEL_STATUS_APV1_NO_ERROR_BIT)) os << "APV 1 error ";
294  if (!(value&CHANNEL_STATUS_APV0_ADDRESS_GOOD)) os << "APV 0 bad address ";
295  if (!(value&CHANNEL_STATUS_APV0_NO_ERROR_BIT)) os << "APV 0 error ";
296  if (value == CHANNEL_STATUS_NO_PROBLEMS) os << "No errors";
297  return os;
298  }
299 
301  {
302  if ( (bufferFormatString == "OLD_VME") ||
303  (bufferFormatString == "BUFFER_FORMAT_OLD_VME") ||
304  (bufferFormatString == "Old VME") ) {
305  return BUFFER_FORMAT_OLD_VME;
306  }
307  if ( (bufferFormatString == "OLD_SLINK") ||
308  (bufferFormatString == "BUFFER_FORMAT_OLD_SLINK") ||
309  (bufferFormatString == "Old S-Link") ) {
311  }
312  if ( (bufferFormatString == "NEW") ||
313  (bufferFormatString == "BUFFER_FORMAT_NEW") ||
314  (bufferFormatString == "New") ) {
315  return BUFFER_FORMAT_NEW;
316  }
317  //if it was none of the above then return invalid
318  return BUFFER_FORMAT_INVALID;
319  }
320 
322  {
323  if ( (headerTypeString == "FULL_DEBUG") ||
324  (headerTypeString == "HEADER_TYPE_FULL_DEBUG") ||
325  (headerTypeString == "Full debug") ) {
326  return HEADER_TYPE_FULL_DEBUG;
327  }
328  if ( (headerTypeString == "APV_ERROR") ||
329  (headerTypeString == "HEADER_TYPE_APV_ERROR") ||
330  (headerTypeString == "APV error") ) {
331  return HEADER_TYPE_APV_ERROR;
332  }
333  if ( (headerTypeString == "None") ||
334  (headerTypeString == "none") ) {
335  return HEADER_TYPE_NONE;
336  }
337  //if it was none of the above then return invalid
338  return HEADER_TYPE_INVALID;
339  }
340 
342  {
343  if ( (readoutModeString == "READOUT_MODE_SCOPE") ||
344  (readoutModeString == "SCOPE") ||
345  (readoutModeString == "SCOPE_MODE") ||
346  (readoutModeString == "Scope mode") ) {
347  return READOUT_MODE_SCOPE;
348  }
349  if ( (readoutModeString == "READOUT_MODE_VIRGIN_RAW") ||
350  (readoutModeString == "VIRGIN_RAW") ||
351  (readoutModeString == "Virgin raw") ) {
353  }
354  if ( (readoutModeString == "READOUT_MODE_PROC_RAW") ||
355  (readoutModeString == "PROC_RAW") ||
356  (readoutModeString == "PROCESSED_RAW") ||
357  (readoutModeString == "Processed raw") ) {
358  return READOUT_MODE_PROC_RAW;
359  }
360  if ( (readoutModeString == "READOUT_MODE_ZERO_SUPPRESSED") ||
361  (readoutModeString == "ZERO_SUPPRESSED") ||
362  (readoutModeString == "Zero suppressed") ) {
364  }
365  if ( (readoutModeString == "READOUT_MODE_ZERO_SUPPRESSED_LITE") ||
366  (readoutModeString == "ZERO_SUPPRESSED_LITE") ||
367  (readoutModeString == "Zero suppressed lite") ) {
369  }
370  if ( (readoutModeString == "READOUT_MODE_PREMIX_RAW") ||
371  (readoutModeString == "PREMIX_RAW") ||
372  (readoutModeString == "PreMix Raw") ) {
374  }
375  if ( (readoutModeString == "READOUT_MODE_SPY") ||
376  (readoutModeString == "SPY") ||
377  (readoutModeString == "Spy channel") ) {
378  return READOUT_MODE_SPY;
379  }
380  //if it was none of the above then return invalid
381  return READOUT_MODE_INVALID;
382  }
383 
385  {
386  if ( (dataTypeString == "REAL") ||
387  (dataTypeString == "DATA_TYPE_REAL") ||
388  (dataTypeString == "Real data") ) {
389  return DATA_TYPE_REAL;
390  }
391  if ( (dataTypeString == "FAKE") ||
392  (dataTypeString == "DATA_TYPE_FAKE") ||
393  (dataTypeString == "Fake data") ) {
394  return DATA_TYPE_FAKE;
395  }
396  //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)
397  std::ostringstream ss;
398  ss << "Trying to convert to a FEDDataType from an invalid string: " << dataTypeString;
399  throw cms::Exception("FEDDataType") << ss.str();
400  }
401 
403  {
404  if ( (daqEventTypeString == "PHYSICS") ||
405  (daqEventTypeString == "DAQ_EVENT_TYPE_PHYSICS") ||
406  (daqEventTypeString == "Physics trigger") ) {
407  return DAQ_EVENT_TYPE_PHYSICS;
408  }
409  if ( (daqEventTypeString == "CALIBRATION") ||
410  (daqEventTypeString == "DAQ_EVENT_TYPE_CALIBRATION") ||
411  (daqEventTypeString == "Calibration trigger") ) {
413  }
414  if ( (daqEventTypeString == "TEST") ||
415  (daqEventTypeString == "DAQ_EVENT_TYPE_TEST") ||
416  (daqEventTypeString == "Test trigger") ) {
417  return DAQ_EVENT_TYPE_TEST;
418  }
419  if ( (daqEventTypeString == "TECHNICAL") ||
420  (daqEventTypeString == "DAQ_EVENT_TYPE_TECHNICAL") ||
421  (daqEventTypeString == "Technical trigger") ) {
423  }
424  if ( (daqEventTypeString == "SIMULATED") ||
425  (daqEventTypeString == "DAQ_EVENT_TYPE_SIMULATED") ||
426  (daqEventTypeString == "Simulated trigger") ) {
428  }
429  if ( (daqEventTypeString == "TRACED") ||
430  (daqEventTypeString == "DAQ_EVENT_TYPE_TRACED") ||
431  (daqEventTypeString == "Traced event") ) {
432  return DAQ_EVENT_TYPE_TRACED;
433  }
434  if ( (daqEventTypeString == "ERROR") ||
435  (daqEventTypeString == "DAQ_EVENT_TYPE_ERROR") ||
436  (daqEventTypeString == "Error") ) {
437  return DAQ_EVENT_TYPE_ERROR;
438  }
439  //if it was none of the above then return invalid
440  return DAQ_EVENT_TYPE_INVALID;
441  }
442 
443 
444 
445 
446  void FEDStatusRegister::printFlags(std::ostream& os) const
447  {
448  if (slinkFullFlag()) os << "SLINK_FULL ";
449  if (trackerHeaderMonitorDataReadyFlag()) os << "HEADER_MONITOR_READY ";
450  if (qdrMemoryFullFlag()) os << "QDR_FULL ";
451  if (qdrMemoryPartialFullFlag()) os << "QDR_PARTIAL_FULL ";
452  if (qdrMemoryEmptyFlag()) os << "QDR_EMPTY ";
453  if (l1aBxFIFOFullFlag()) os << "L1A_FULL ";
454  if (l1aBxFIFOPartialFullFlag()) os << "L1A_PARTIAL_FULL ";
455  if (l1aBxFIFOEmptyFlag()) os << "L1A_EMPTY ";
456  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
457  if (feDataMissingFlag(iFE)) os << "FEUNIT" << uint16_t(iFE) << "MISSING ";
458  }
459  }
460 
462  {
463  uint8_t result(0x00);
464  if (qdrMemoryFullFlag()) result |= BUFFER_STATE_FULL;
466  if (qdrMemoryEmptyFlag()) result |= BUFFER_STATE_EMPTY;
467  return FEDBufferState(result);
468  }
469 
471  {
472  uint8_t result(0x00);
473  if (l1aBxFIFOFullFlag()) result |= BUFFER_STATE_FULL;
475  if (l1aBxFIFOEmptyFlag()) result |= BUFFER_STATE_EMPTY;
476  return FEDBufferState(result);
477  }
478 
479  void FEDStatusRegister::setBit(const uint8_t num, const bool bitSet)
480  {
481  const uint16_t mask = (0x0001 << num);
482  if (bitSet) data_ |= mask;
483  else data_ &= (~mask);
484  }
485 
487  {
488  switch (state) {
489  case BUFFER_STATE_FULL:
491  case BUFFER_STATE_EMPTY:
492  case BUFFER_STATE_UNSET:
493  break;
494  default:
495  std::ostringstream ss;
496  ss << "Invalid buffer state: ";
497  printHex(&state,1,ss);
498  throw cms::Exception("FEDBuffer") << ss.str();
499  }
503  return *this;
504  }
505 
507  {
508  switch (state) {
509  case BUFFER_STATE_FULL:
511  case BUFFER_STATE_EMPTY:
512  case BUFFER_STATE_UNSET:
513  break;
514  default:
515  std::ostringstream ss;
516  ss << "Invalid buffer state: ";
517  printHex(&state,1,ss);
518  throw cms::Exception("FEDBuffer") << ss.str();
519  }
523  return *this;
524  }
525 
526 
527 
528 
529  void FEDBackendStatusRegister::printFlags(std::ostream& os) const
530  {
531  if (internalFreezeFlag()) os << "INTERNAL_FREEZE ";
532  if (slinkDownFlag()) os << "SLINK_DOWN ";
533  if (slinkFullFlag()) os << "SLINK_FULL ";
534  if (backpressureFlag()) os << "BACKPRESSURE ";
535  if (ttcReadyFlag()) os << "TTC_READY ";
536  if (trackerHeaderMonitorDataReadyFlag()) os << "HEADER_MONITOR_READY ";
538  printFlagsForBuffer(frameAddressFIFOState(),"FRAME_ADDRESS",os);
539  printFlagsForBuffer(totalLengthFIFOState(),"TOTAL_LENGTH",os);
540  printFlagsForBuffer(trackerHeaderFIFOState(),"TRACKER_HEADER",os);
541  printFlagsForBuffer(l1aBxFIFOState(),"L1ABX",os);
544  }
545 
546  void FEDBackendStatusRegister::printFlagsForBuffer(const FEDBufferState bufferState, const std::string name, std::ostream& os) const
547  {
548  if (bufferState&BUFFER_STATE_EMPTY) os << name << "_EMPTY ";
549  if (bufferState&BUFFER_STATE_PARTIAL_FULL) os << name << "_PARTIAL_FULL ";
550  if (bufferState&BUFFER_STATE_FULL) os << name << "_FULL ";
551  if (bufferState == BUFFER_STATE_UNSET) os << name << "_UNSET ";
552  }
553 
554  FEDBufferState FEDBackendStatusRegister::getBufferState(const uint8_t bufferPosition) const
555  {
556  uint8_t result = 0x00;
557  if (getBit(bufferPosition+STATE_OFFSET_EMPTY)) result |= BUFFER_STATE_EMPTY;
558  if (getBit(bufferPosition+STATE_OFFSET_PARTIAL_FULL)) result |= BUFFER_STATE_PARTIAL_FULL;
559  if (getBit(bufferPosition+STATE_OFFSET_FULL)) result |= BUFFER_STATE_FULL;
560  return FEDBufferState(result);
561  }
562 
563  void FEDBackendStatusRegister::setBufferSate(const uint8_t bufferPosition, const FEDBufferState state)
564  {
565  switch (state) {
566  case BUFFER_STATE_FULL:
568  case BUFFER_STATE_EMPTY:
569  case BUFFER_STATE_UNSET:
570  break;
571  default:
572  std::ostringstream ss;
573  ss << "Invalid buffer state: ";
574  printHex(&state,1,ss);
575  throw cms::Exception("FEDBuffer") << ss.str();
576  }
577  setBit(bufferPosition+STATE_OFFSET_EMPTY, state&BUFFER_STATE_EMPTY);
579  setBit(bufferPosition+STATE_OFFSET_FULL, state&BUFFER_STATE_FULL);
580  }
581 
582  void FEDBackendStatusRegister::setBit(const uint8_t num, const bool bitSet)
583  {
584  const uint32_t mask = (0x00000001 << num);
585  if (bitSet) data_ |= mask;
586  else data_ &= (~mask);
587  }
588 
590  const FEDBufferState frameAddressFIFOBufferState,
591  const FEDBufferState totalLengthFIFOBufferState,
592  const FEDBufferState trackerHeaderFIFOBufferState,
593  const FEDBufferState l1aBxFIFOBufferState,
594  const FEDBufferState feEventLengthFIFOBufferState,
595  const FEDBufferState feFPGABufferState,
596  const bool backpressure, const bool slinkFull,
597  const bool slinkDown, const bool internalFreeze,
598  const bool trackerHeaderMonitorDataReady, const bool ttcReady)
599  : data_(0)
600  {
601  setInternalFreezeFlag(internalFreeze);
602  setSLinkDownFlag(slinkDown);
603  setSLinkFullFlag(slinkFull);
604  setBackpressureFlag(backpressure);
605  setTTCReadyFlag(ttcReady);
606  setTrackerHeaderMonitorDataReadyFlag(trackerHeaderMonitorDataReady);
607  setQDRMemoryState(qdrMemoryBufferState);
608  setFrameAddressFIFOState(frameAddressFIFOBufferState);
609  setTotalLengthFIFOState(totalLengthFIFOBufferState);
610  setTrackerHeaderFIFOState(trackerHeaderFIFOBufferState);
611  setL1ABXFIFOState(l1aBxFIFOBufferState);
612  setFEEventLengthFIFOState(feEventLengthFIFOBufferState);
613  setFEFPGABufferState(feFPGABufferState);
614  }
615 
616 
617 
618 
619  TrackerSpecialHeader::TrackerSpecialHeader(const uint8_t* headerPointer)
620  {
621  //the buffer format byte is one of the valid values if we assume the buffer is not swapped
622  const bool validFormatByteWhenNotWordSwapped = ( (headerPointer[BUFFERFORMAT] == BUFFER_FORMAT_CODE_NEW) ||
623  (headerPointer[BUFFERFORMAT] == BUFFER_FORMAT_CODE_OLD) );
624  //the buffer format byte is the old value if we assume the buffer is swapped
625  const bool validFormatByteWhenWordSwapped = (headerPointer[BUFFERFORMAT^4] == BUFFER_FORMAT_CODE_OLD);
626  //if the buffer format byte is valid if the buffer is not swapped or it is never valid
627  if (validFormatByteWhenNotWordSwapped || (!validFormatByteWhenNotWordSwapped && !validFormatByteWhenWordSwapped) ) {
628  memcpy(specialHeader_,headerPointer,8);
629  wordSwapped_ = false;
630  } else {
631  memcpy(specialHeader_,headerPointer+4,4);
632  memcpy(specialHeader_+4,headerPointer,4);
633  wordSwapped_ = true;
634  }
635  }
636 
638  {
642  else return BUFFER_FORMAT_OLD_SLINK;
643  }
644  else return BUFFER_FORMAT_INVALID;
645  }
646 
648  {
653  else return HEADER_TYPE_INVALID;
654  }
655 
657  {
658  const uint8_t eventTypeNibble = trackerEventTypeNibble();
659  //if it is scope mode then return as is (it cannot be fake data)
660  if (eventTypeNibble == READOUT_MODE_SCOPE) return FEDReadoutMode(eventTypeNibble);
661  //if it is premix then return as is: stripping last bit would make it spy data !
662  if (eventTypeNibble == READOUT_MODE_PREMIX_RAW) return FEDReadoutMode(eventTypeNibble);
663  //if not then ignore the last bit which indicates if it is real or fake
664  else {
665  const uint8_t mode = (eventTypeNibble & 0xE);
666  switch(mode) {
671  case READOUT_MODE_SPY:
672  return FEDReadoutMode(mode);
673  default:
674  return READOUT_MODE_INVALID;
675  }
676  }
677  }
678 
680  {
681  const uint8_t eventTypeNibble = trackerEventTypeNibble();
682  //if it is scope mode then it is always real
683  if (eventTypeNibble == READOUT_MODE_SCOPE) return DATA_TYPE_REAL;
684  //in other modes it is the lowest order bit of event type nibble
685  else return FEDDataType(eventTypeNibble & 0x1);
686  }
687 
689  {
690  //check if order in buffer is different
691  if ( ( (bufferFormat()==BUFFER_FORMAT_OLD_VME) && (newBufferFormat!=BUFFER_FORMAT_OLD_VME) ) ||
692  ( (bufferFormat()!=BUFFER_FORMAT_OLD_VME) && (newBufferFormat==BUFFER_FORMAT_OLD_VME) ) ) {
694  }
695  //set appropriate code
696  setBufferFormatByte(newBufferFormat);
697  return *this;
698  }
699 
701  {
702  switch (newBufferFormat) {
706  break;
707  case BUFFER_FORMAT_NEW:
709  break;
710  default:
711  std::ostringstream ss;
712  ss << "Invalid buffer format: ";
713  printHex(&newBufferFormat,1,ss);
714  throw cms::Exception("FEDBuffer") << ss.str();
715  }
716  }
717 
719  {
720  switch(headerType) {
723  case HEADER_TYPE_NONE:
724  setHeaderTypeNibble(headerType);
725  return *this;
726  default:
727  std::ostringstream ss;
728  ss << "Invalid header type: ";
729  printHex(&headerType,1,ss);
730  throw cms::Exception("FEDBuffer") << ss.str();
731  }
732  }
733 
735  {
736  switch(readoutMode) {
737  case READOUT_MODE_SCOPE:
738  //scope mode is always real
739  setReadoutModeBits(readoutMode);
740  setDataTypeBit(true);
745  case READOUT_MODE_SPY:
746  setReadoutModeBits(readoutMode);
747  break;
749  //special mode for simulation
750  setReadoutModeBits(readoutMode);
751  setDataTypeBit(true);
752  break;
753  default:
754  std::ostringstream ss;
755  ss << "Invalid readout mode: ";
756  printHex(&readoutMode,1,ss);
757  throw cms::Exception("FEDBuffer") << ss.str();
758  }
759  return *this;
760  }
761 
763  {
764  //if mode is scope then this bit can't be changed
765  if (readoutMode() == READOUT_MODE_SCOPE) return *this;
766  switch (dataType) {
767  case DATA_TYPE_REAL:
768  case DATA_TYPE_FAKE:
769  setDataTypeBit(dataType);
770  return *this;
771  default:
772  std::ostringstream ss;
773  ss << "Invalid data type: ";
774  printHex(&dataType,1,ss);
775  throw cms::Exception("FEDBuffer") << ss.str();
776  }
777  }
778 
780  {
781  const uint8_t mask = 0x1 << internalFEUnitNum;
782  const uint8_t result = ( (apvAddressErrorRegister() & (~mask)) | (error?mask:0x00) );
784  return *this;
785  }
786 
787  TrackerSpecialHeader& TrackerSpecialHeader::setFEEnableForFEUnit(const uint8_t internalFEUnitNum, const bool enabled)
788  {
789  const uint8_t mask = 0x1 << internalFEUnitNum;
790  const uint8_t result = ( (feEnableRegister() & (~mask)) | (enabled?mask:0x00) );
791  setFEEnableRegister(result);
792  return *this;
793  }
794 
795  TrackerSpecialHeader& TrackerSpecialHeader::setFEOverflowForFEUnit(const uint8_t internalFEUnitNum, const bool overflow)
796  {
797  const uint8_t mask = 0x1 << internalFEUnitNum;
798  const uint8_t result = ( (feOverflowRegister() & (~mask)) | (overflow?mask:0x00) );
799  setFEEnableRegister(result);
800  return *this;
801  }
802 
804  const FEDHeaderType headerType, const FEDDataType dataType,
805  const uint8_t address, const uint8_t addressErrorRegister,
806  const uint8_t feEnableRegister, const uint8_t feOverflowRegister,
807  const FEDStatusRegister fedStatusRegister)
808  {
809  memset(specialHeader_,0x00,8);
810  //determine if order is swapped in real buffer
811  wordSwapped_ = (bufferFormat == BUFFER_FORMAT_OLD_VME);
812  //set fields
813  setBufferFormatByte(bufferFormat);
814  setReadoutMode(readoutMode);
815  setHeaderType(headerType);
816  setDataType(dataType);
817  setAPVEAddress(address);
818  setAPVEAddressErrorRegister(addressErrorRegister);
819  setFEEnableRegister(feEnableRegister);
820  setFEOverflowRegister(feOverflowRegister);
821  setFEDStatusRegister(fedStatusRegister);
822  }
823 
824 
825 
826 
828  {
829  switch(eventTypeNibble()) {
832  case DAQ_EVENT_TYPE_TEST:
838  default:
839  return DAQ_EVENT_TYPE_INVALID;
840  }
841  }
842 
844  {
845  header_[7] = ((header_[7] & 0xF0) | evtType);
846  return *this;
847  }
848 
849  FEDDAQHeader& FEDDAQHeader::setL1ID(const uint32_t l1ID)
850  {
851  header_[4] = (l1ID & 0x000000FF);
852  header_[5] = ( (l1ID & 0x0000FF00) >> 8);
853  header_[6] = ( (l1ID & 0x00FF0000) >> 16);
854  return *this;
855  }
856 
857  FEDDAQHeader& FEDDAQHeader::setBXID(const uint16_t bxID)
858  {
859  header_[3] = ( (bxID & 0x0FF0) >> 4);
860  header_[2] = ( (header_[2] & 0x0F) | ( (bxID & 0x000F) << 4) );
861  return *this;
862  }
863 
864  FEDDAQHeader& FEDDAQHeader::setSourceID(const uint16_t sourceID)
865  {
866  header_[2] = ( (header_[2] & 0xF0) | ( (sourceID & 0x0F00) >> 8) );
867  header_[1] = (sourceID & 0x00FF);
868  return *this;
869  }
870 
871  FEDDAQHeader::FEDDAQHeader(const uint32_t l1ID, const uint16_t bxID, const uint16_t sourceID, const FEDDAQEventType evtType)
872  {
873  //clear everything (FOV,H,x,$ all set to 0)
874  memset(header_,0x0,8);
875  //set the BoE nibble to indicate this is the last fragment
876  header_[7] = 0x50;
877  //set variable fields vith values supplied
878  setEventType(evtType);
879  setL1ID(l1ID);
880  setBXID(bxID);
881  setSourceID(sourceID);
882  }
883 
884 
885 
886 
888  {
889  switch(ttsNibble()) {
890  case TTS_DISCONNECTED0:
891  case TTS_WARN_OVERFLOW:
892  case TTS_OUT_OF_SYNC:
893  case TTS_BUSY:
894  case TTS_READY:
895  case TTS_ERROR:
896  case TTS_DISCONNECTED1:
897  return FEDTTSBits(ttsNibble());
898  default:
899  return TTS_INVALID;
900  }
901  }
902 
903  FEDDAQTrailer::FEDDAQTrailer(const uint32_t eventLengthIn64BitWords, const uint16_t crc, const FEDTTSBits ttsBits,
904  const bool slinkTransmissionError, const bool badFEDID, const bool slinkCRCError,
905  const uint8_t eventStatusNibble)
906  {
907  //clear everything (T,x,$ all set to 0)
908  memset(trailer_,0x0,8);
909  //set the EoE nibble to indicate this is the last fragment
910  trailer_[7] = 0xA0;
911  //set variable fields vith values supplied
912  setEventLengthIn64BitWords(eventLengthIn64BitWords);
913  setEventStatusNibble(eventStatusNibble);
914  setTTSBits(ttsBits);
915  setCRC(crc);
916  setSLinkTransmissionErrorBit(slinkTransmissionError);
917  setBadSourceIDBit(badFEDID);
918  setSLinkCRCErrorBit(slinkCRCError);
919  }
920 
921  FEDDAQTrailer& FEDDAQTrailer::setEventLengthIn64BitWords(const uint32_t eventLengthIn64BitWords)
922  {
923  trailer_[4] = (eventLengthIn64BitWords & 0x000000FF);
924  trailer_[5] = ( (eventLengthIn64BitWords & 0x0000FF00) >> 8);
925  trailer_[6] = ( (eventLengthIn64BitWords & 0x00FF0000) >> 16);
926  return *this;
927  }
928 
930  {
931  trailer_[2] = (crc & 0x00FF);
932  trailer_[3] = ( (crc >> 8) & 0x00FF );
933  return *this;
934  }
935 
937  {
938  if (bitSet) trailer_[1] |= 0x80;
939  else trailer_[1] &= (~0x80);
940  return *this;
941  }
942 
944  {
945  if (bitSet) trailer_[1] |= 0x40;
946  else trailer_[1] &= (~0x40);
947  return *this;
948  }
949 
951  {
952  if (bitSet) trailer_[0] |= 0x04;
953  else trailer_[0] &= (~0x40);
954  return *this;
955  }
956 
957  FEDDAQTrailer& FEDDAQTrailer::setEventStatusNibble(const uint8_t eventStatusNibble)
958  {
959  trailer_[1] = ( (trailer_[1] & 0xF0) | (eventStatusNibble & 0x0F) );
960  return *this;
961  }
962 
964  {
965  trailer_[0] = ( (trailer_[0] & 0x0F) | (ttsBits & 0xF0) );
966  return *this;
967  }
968 
969 
970 
971 
973  {
974  }
975 
977  {
979  }
980 
981  void FEDAPVErrorHeader::print(std::ostream& os) const
982  {
984  }
985 
987  {
988  return new FEDAPVErrorHeader(*this);
989  }
990 
991  bool FEDAPVErrorHeader::checkStatusBits(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
992  {
993  //3 bytes per FE unit, channel order is reversed in FE unit data, 2 bits per channel
994  const uint16_t bitNumber = (internalFEDChannelNum/FEDCH_PER_FEUNIT)*24 + (FEDCH_PER_FEUNIT-1-(internalFEDChannelNum%FEDCH_PER_FEUNIT))*2 + apvNum;
995  //bit high means no error
996  return (header_[bitNumber/8] & (0x01<<(bitNumber%8)) );
997  }
998 
1000  {
1001  return (checkStatusBits(internalFEDChannelNum,0) && checkStatusBits(internalFEDChannelNum,1));
1002  }
1003 
1004  const uint8_t* FEDAPVErrorHeader::data() const
1005  {
1006  return header_;
1007  }
1008 
1009  FEDAPVErrorHeader::FEDAPVErrorHeader(const std::vector<bool>& apvsGood)
1010  {
1012  for (uint8_t iCh = 0; iCh < FEDCH_PER_FED; iCh++) {
1013  setAPVStatusBit(iCh,0,apvsGood[iCh*2]);
1014  setAPVStatusBit(iCh,1,apvsGood[iCh*2+1]);
1015  }
1016  }
1017 
1018  FEDAPVErrorHeader& FEDAPVErrorHeader::setAPVStatusBit(const uint8_t internalFEDChannelNum, const uint8_t apvNum, const bool apvGood)
1019  {
1020  //3 bytes per FE unit, channel order is reversed in FE unit data, 2 bits per channel
1021  const uint16_t bitNumber = (internalFEDChannelNum/FEDCH_PER_FED)*24 + (FEDCH_PER_FED-1-(internalFEDChannelNum%FEDCH_PER_FED))*2+apvNum;
1022  const uint8_t byteNumber = bitNumber/8;
1023  const uint8_t bitInByte = bitNumber%8;
1024  const uint8_t mask = (0x01 << bitInByte);
1025  header_[byteNumber] = ( (header_[byteNumber] & (~mask)) | (apvGood?mask:0x00) );
1026  return *this;
1027  }
1028 
1030  {
1031  //if channel is unlocked then set both APV bits bad
1032  if ( (!(status & CHANNEL_STATUS_LOCKED)) || (!(status & CHANNEL_STATUS_IN_SYNC)) ) {
1033  setAPVStatusBit(internalFEDChannelNum,0,false);
1034  setAPVStatusBit(internalFEDChannelNum,1,false);
1035  return;
1036  } else {
1037  if ( (status & CHANNEL_STATUS_APV0_ADDRESS_GOOD) && (status & CHANNEL_STATUS_APV0_NO_ERROR_BIT) ) {
1038  setAPVStatusBit(internalFEDChannelNum,0,true);
1039  } else {
1040  setAPVStatusBit(internalFEDChannelNum,0,false);
1041  }
1042  if ( (status & CHANNEL_STATUS_APV1_ADDRESS_GOOD) && (status & CHANNEL_STATUS_APV1_NO_ERROR_BIT) ) {
1043  setAPVStatusBit(internalFEDChannelNum,1,true);
1044  } else {
1045  setAPVStatusBit(internalFEDChannelNum,1,false);
1046  }
1047  }
1048  }
1049 
1050  //These methods do nothing as the values in question are in present in the APV Error header.
1051  //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.
1052  void FEDAPVErrorHeader::setFEUnitMajorityAddress(const uint8_t internalFEUnitNum, const uint8_t address)
1053  {
1054  return;
1055  }
1057  {
1058  return;
1059  }
1060  void FEDAPVErrorHeader::setFEUnitLength(const uint8_t internalFEUnitNum, const uint16_t length)
1061  {
1062  return;
1063  }
1064 
1065 
1067  {
1068  }
1069 
1071  {
1073  }
1074 
1075  void FEDFullDebugHeader::print(std::ostream& os) const
1076  {
1078  }
1079 
1081  {
1082  return new FEDFullDebugHeader(*this);
1083  }
1084 
1085  bool FEDFullDebugHeader::checkStatusBits(const uint8_t internalFEDChannelNum, const uint8_t apvNum) const
1086  {
1087  return ( !unlockedFromBit(internalFEDChannelNum) &&
1088  !outOfSyncFromBit(internalFEDChannelNum) &&
1089  !apvError(internalFEDChannelNum,apvNum) &&
1090  !apvAddressError(internalFEDChannelNum,apvNum) );
1091  }
1092 
1094  {
1095  //return ( !unlockedFromBit(internalFEDChannelNum) &&
1096  // !outOfSyncFromBit(internalFEDChannelNum) &&
1097  // !apvErrorFromBit(internalFEDChannelNum,0) &&
1098  // !apvAddressErrorFromBit(internalFEDChannelNum,0) &&
1099  // !apvErrorFromBit(internalFEDChannelNum,1) &&
1100  // !apvAddressErrorFromBit(internalFEDChannelNum,1) );
1101  return (getChannelStatus(internalFEDChannelNum) == CHANNEL_STATUS_NO_PROBLEMS);
1102  }
1103 
1105  {
1106  const uint8_t* pFEWord = feWord(internalFEDChannelNum/FEDCH_PER_FEUNIT);
1107  const uint8_t feUnitChanNum = internalFEDChannelNum % FEDCH_PER_FEUNIT;
1108  const uint8_t startByteInFEWord = (FEDCH_PER_FEUNIT-1 - feUnitChanNum) * 6 / 8;
1109  switch ( (FEDCH_PER_FEUNIT-1-feUnitChanNum) % 4 ) {
1110  case 0:
1111  return FEDChannelStatus( pFEWord[startByteInFEWord] & 0x3F );
1112  case 1:
1113  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xC0) >> 6) | ((pFEWord[startByteInFEWord+1] & 0x0F) << 2) );
1114  case 2:
1115  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xF0) >> 4) | ((pFEWord[startByteInFEWord+1] & 0x03) << 4) );
1116  case 3:
1117  return FEDChannelStatus( (pFEWord[startByteInFEWord] & 0xFC) >> 2 );
1118  //stop compiler warning
1119  default:
1120  return FEDChannelStatus(0);
1121  }
1122  /*const uint8_t feUnitChanNum = internalFEDChannelNum / FEDCH_PER_FEUNIT;
1123  const uint8_t* pFEWord = feWord(feUnitChanNum);
1124  const uint8_t startByteInFEWord = feUnitChanNum * 3 / 4;
1125  //const uint8_t shift = ( 6 - ((feUnitChanNum-1)%4) );
1126  //const uint16_t mask = ( 0x003F << shift );
1127  //uint8_t result = ( (pFEWord[startByteInFEWord] & (mask&0x00FF)) >> shift );
1128  //result |= ( (pFEWord[startByteInFEWord+1] & (mask>>8)) << (8-shift) );
1129  switch (feUnitChanNum % 4) {
1130  case 0:
1131  return FEDChannelStatus( pFEWord[startByteInFEWord] & 0x3F );
1132  case 1:
1133  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xC0) >> 6) | ((pFEWord[startByteInFEWord+1] & 0x0F) << 2) );
1134  case 2:
1135  return FEDChannelStatus( ((pFEWord[startByteInFEWord] & 0xF0) >> 4) | ((pFEWord[startByteInFEWord+1] & 0x03) << 4) );
1136  case 3:
1137  return FEDChannelStatus( (pFEWord[startByteInFEWord] & 0xFC) >> 2 );
1138  //stop compiler warning
1139  default:
1140  return FEDChannelStatus(0);
1141  }*/
1142  }
1143 
1144  const uint8_t* FEDFullDebugHeader::data() const
1145  {
1146  return header_;
1147  }
1148 
1149  FEDFullDebugHeader::FEDFullDebugHeader(const std::vector<uint16_t>& feUnitLengths, const std::vector<uint8_t>& feMajorityAddresses,
1150  const std::vector<FEDChannelStatus>& channelStatus, const FEDBackendStatusRegister beStatusRegister,
1151  const uint32_t daqRegister, const uint32_t daqRegister2)
1152  {
1154  setBEStatusRegister(beStatusRegister);
1155  setDAQRegister(daqRegister);
1156  setDAQRegister2(daqRegister2);
1157  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1158  setFEUnitLength(iFE,feUnitLengths[iFE]);
1159  setFEUnitMajorityAddress(iFE,feMajorityAddresses[iFE]);
1160  }
1161  for (uint8_t iCh = 0; iCh < FEDCH_PER_FED; iCh++) {
1162  setChannelStatus(iCh,channelStatus[iCh]);
1163  }
1164  }
1165 
1167  {
1168  setUnlocked(internalFEDChannelNum, !(status&CHANNEL_STATUS_LOCKED) );
1169  setOutOfSync(internalFEDChannelNum, !(status&CHANNEL_STATUS_IN_SYNC) );
1170  setAPVAddressError(internalFEDChannelNum,1, !(status&CHANNEL_STATUS_APV1_ADDRESS_GOOD) );
1171  setAPVAddressError(internalFEDChannelNum,0, !(status&CHANNEL_STATUS_APV0_ADDRESS_GOOD) );
1172  setAPVError(internalFEDChannelNum,1, !(status&CHANNEL_STATUS_APV1_NO_ERROR_BIT) );
1173  setAPVError(internalFEDChannelNum,0, !(status&CHANNEL_STATUS_APV0_NO_ERROR_BIT) );
1174  }
1175 
1176  void FEDFullDebugHeader::setFEUnitMajorityAddress(const uint8_t internalFEUnitNum, const uint8_t address)
1177  {
1178  feWord(internalFEUnitNum)[9] = address;
1179  }
1180 
1182  {
1183  set32BitWordAt(feWord(0)+10,beStatusRegister);
1184  }
1185 
1186  void FEDFullDebugHeader::setDAQRegister(const uint32_t daqRegister)
1187  {
1188  set32BitWordAt(feWord(7)+10,daqRegister);
1189  }
1190 
1191  void FEDFullDebugHeader::setDAQRegister2(const uint32_t daqRegister2)
1192  {
1193  set32BitWordAt(feWord(6)+10,daqRegister2);
1194  }
1195 
1196  void FEDFullDebugHeader::setFEUnitLength(const uint8_t internalFEUnitNum, const uint16_t length)
1197  {
1198  feWord(internalFEUnitNum)[15] = ( (length & 0xFF00) >> 8);
1199  feWord(internalFEUnitNum)[14] = (length & 0x00FF);
1200  }
1201 
1202  void FEDFullDebugHeader::setBit(const uint8_t internalFEDChannelNum, const uint8_t bit, const bool value)
1203  {
1204  const uint8_t bitInFeWord = (FEDCH_PER_FEUNIT-1 - (internalFEDChannelNum%FEDCH_PER_FEUNIT)) * 6 + bit;
1205  uint8_t& byte = *(feWord(internalFEDChannelNum / FEDCH_PER_FEUNIT)+(bitInFeWord/8));
1206  const uint8_t mask = (0x1 << bitInFeWord%8);
1207  byte = ( (byte & (~mask)) | (value?mask:0x0) );
1208  }
1209 
1211  {
1212  }
1213 
1214 
1215 
1216 
1217  FEDBufferBase::FEDBufferBase(const uint8_t* fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat)
1218  : channels_(FEDCH_PER_FED,FEDChannel(NULL,0,0)),
1219  originalBuffer_(fedBuffer),
1220  bufferSize_(fedBufferSize)
1221  {
1222  init(fedBuffer,fedBufferSize,allowUnrecognizedFormat);
1223  }
1224 
1225  FEDBufferBase::FEDBufferBase(const uint8_t* fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat, const bool fillChannelVector)
1226  : originalBuffer_(fedBuffer),
1227  bufferSize_(fedBufferSize)
1228  {
1229  init(fedBuffer,fedBufferSize,allowUnrecognizedFormat);
1230  if (fillChannelVector) channels_.assign(FEDCH_PER_FED,FEDChannel(NULL,0,0));
1231  }
1232 
1233  void FEDBufferBase::init(const uint8_t* fedBuffer, const size_t fedBufferSize, const bool allowUnrecognizedFormat)
1234  {
1235  //min buffer length. DAQ header, DAQ trailer, tracker special header.
1236  static const size_t MIN_BUFFER_SIZE = 8+8+8;
1237  //check size is non zero and data pointer is not NULL
1238  if (!originalBuffer_) throw cms::Exception("FEDBuffer") << "Buffer pointer is NULL.";
1239  if (bufferSize_ < MIN_BUFFER_SIZE) {
1240  std::ostringstream ss;
1241  ss << "Buffer is too small. "
1242  << "Min size is " << MIN_BUFFER_SIZE << ". "
1243  << "Buffer size is " << bufferSize_ << ". ";
1244  throw cms::Exception("FEDBuffer") << ss.str();
1245  }
1246 
1247  //construct tracker special header using second 64 bit word
1249 
1250  //check the buffer format
1252  if (bufferFormat == BUFFER_FORMAT_INVALID && !allowUnrecognizedFormat) {
1253  std::ostringstream ss;
1254  ss << "Buffer format not recognized. "
1255  << "Tracker special header: " << specialHeader_;
1256  throw cms::Exception("FEDBuffer") << ss.str();
1257  }
1258  //swap the buffer words so that the whole buffer is in slink ordering
1259  if ( (bufferFormat == BUFFER_FORMAT_OLD_VME) || (bufferFormat == BUFFER_FORMAT_NEW) ) {
1260  uint8_t* newBuffer = new uint8_t[bufferSize_];
1261  const uint32_t* originalU32 = reinterpret_cast<const uint32_t*>(originalBuffer_);
1262  const size_t sizeU32 = bufferSize_/4;
1263  uint32_t* newU32 = reinterpret_cast<uint32_t*>(newBuffer);
1264  if (bufferFormat == BUFFER_FORMAT_OLD_VME) {
1265  //swap whole buffer
1266  for (size_t i = 0; i < sizeU32; i+=2) {
1267  newU32[i] = originalU32[i+1];
1268  newU32[i+1] = originalU32[i];
1269  }
1270  }
1271  if (bufferFormat == BUFFER_FORMAT_NEW) {
1272  //copy DAQ header
1273  memcpy(newU32,originalU32,8);
1274  //copy DAQ trailer
1275  memcpy(newU32+sizeU32-2,originalU32+sizeU32-2,8);
1276  //swap the payload
1277  for (size_t i = 2; i < sizeU32-2; i+=2) {
1278  newU32[i] = originalU32[i+1];
1279  newU32[i+1] = originalU32[i];
1280  }
1281  }
1282  orderedBuffer_ = newBuffer;
1283  } //if ( (bufferFormat == BUFFER_FORMAT_OLD_VME) || (bufferFormat == BUFFER_FORMAT_NEW) )
1284  else {
1286  }
1287 
1288  //construct header object at begining of buffer
1290  //construct trailer object using last 64 bit word of buffer
1292  }
1293 
1295  {
1296  //if the buffer was coppied and swapped then delete the copy
1298  }
1299 
1300  void FEDBufferBase::print(std::ostream& os) const
1301  {
1302  os << "buffer format: " << bufferFormat() << std::endl;
1303  os << "Buffer size: " << bufferSize() << " bytes" << std::endl;
1304  os << "Event length from DAQ trailer: " << daqEventLengthInBytes() << " bytes" << std::endl;
1305  os << "Source ID: " << daqSourceID() << std::endl;
1306  os << "Header type: " << headerType() << std::endl;
1307  os << "Readout mode: " << readoutMode() << std::endl;
1308  os << "Data type: " << dataType() << std::endl;
1309  os << "DAQ event type: " << daqEventType() << std::endl;
1310  os << "TTS state: " << daqTTSState() << std::endl;
1311  os << "L1 ID: " << daqLvl1ID() << std::endl;
1312  os << "BX ID: " << daqBXID() << std::endl;
1313  os << "FED status register flags: "; fedStatusRegister().printFlags(os); os << std::endl;
1314  os << "APVe Address: " << uint16_t(apveAddress()) << std::endl;
1315  os << "Enabled FE units: " << uint16_t(nFEUnitsEnabled()) << std::endl;
1316  }
1317 
1319  {
1320  uint8_t result = 0;
1321  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1322  if (feEnabled(iFE)) result++;
1323  }
1324  return result;
1325  }
1326 
1328  {
1329  return ( (daqSourceID() >= FED_ID_MIN) &&
1330  (daqSourceID() <= FED_ID_MAX) );
1331  }
1332 
1334  {
1335  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1336  if (!feEnabled(iFE)) continue;
1337  if (majorityAddressErrorForFEUnit(iFE)) return false;
1338  }
1339  return true;
1340  }
1341 
1343  {
1344  const uint8_t feUnit = internalFEDChannelNum/FEDCH_PER_FEUNIT;
1345  return ( !majorityAddressErrorForFEUnit(feUnit) && feEnabled(feUnit) && !feOverflow(feUnit) );
1346  }
1347 
1349  {
1351  }
1352 
1354  {
1355  std::ostringstream summary;
1356  summary << "Check buffer type valid: " << ( checkBufferFormat() ? "passed" : "FAILED" ) << std::endl;
1357  summary << "Check header format valid: " << ( checkHeaderType() ? "passed" : "FAILED" ) << std::endl;
1358  summary << "Check readout mode valid: " << ( checkReadoutMode() ? "passed" : "FAILED" ) << std::endl;
1359  //summary << "Check APVe address valid: " << ( checkAPVEAddressValid() ? "passed" : "FAILED" ) << std::endl;
1360  summary << "Check FE unit majority addresses: " << ( checkMajorityAddresses() ? "passed" : "FAILED" ) << std::endl;
1361  if (!checkMajorityAddresses()) {
1362  summary << "FEs with majority address error: ";
1363  unsigned int badFEs = 0;
1364  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1365  if (!feEnabled(iFE)) continue;
1366  if (majorityAddressErrorForFEUnit(iFE)) {
1367  summary << uint16_t(iFE) << " ";
1368  badFEs++;
1369  }
1370  }
1371  summary << std::endl;
1372  summary << "Number of FE Units with bad addresses: " << badFEs << std::endl;
1373  }
1374  summary << "Check for FE unit buffer overflows: " << ( checkNoFEOverflows() ? "passed" : "FAILED" ) << std::endl;
1375  if (!checkNoFEOverflows()) {
1376  summary << "FEs which overflowed: ";
1377  unsigned int badFEs = 0;
1378  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
1379  if (feOverflow(iFE)) {
1380  summary << uint16_t(iFE) << " ";
1381  badFEs++;
1382  }
1383  }
1384  summary << std::endl;
1385  summary << "Number of FE Units which overflowed: " << badFEs << std::endl;
1386  }
1387  summary << "Check for S-Link CRC errors: " << ( checkNoSlinkCRCError() ? "passed" : "FAILED" ) << std::endl;
1388  summary << "Check for S-Link transmission error: " << ( checkNoSLinkTransmissionError() ? "passed" : "FAILED" ) << std::endl;
1389  summary << "Check CRC: " << ( checkCRC() ? "passed" : "FAILED" ) << std::endl;
1390  summary << "Check source ID is FED ID: " << ( checkSourceIDs() ? "passed" : "FAILED" ) << std::endl;
1391  summary << "Check for unexpected source ID at FRL: " << ( checkNoUnexpectedSourceID() ? "passed" : "FAILED" ) << std::endl;
1392  summary << "Check there are no extra headers or trailers: " << ( checkNoExtraHeadersOrTrailers() ? "passed" : "FAILED" ) << std::endl;
1393  summary << "Check length from trailer: " << ( checkLengthFromTrailer() ? "passed" : "FAILED" ) << std::endl;
1394  return summary.str();
1395  }
1396 
1397 
1398 
1399 
1400  uint16_t FEDChannel::cmMedian(const uint8_t apvIndex) const
1401  {
1403  std::ostringstream ss;
1404  ss << "Request for CM median from channel with non-ZS packet code. "
1405  << "Packet code is " << uint16_t(packetCode()) << "."
1406  << std::endl;
1407  throw cms::Exception("FEDBuffer") << ss.str();
1408  }
1409  if (apvIndex > 1) {
1410  std::ostringstream ss;
1411  ss << "Channel APV index out of range when requesting CM median for APV. "
1412  << "Channel APV index is " << uint16_t(apvIndex) << "."
1413  << std::endl;
1414  throw cms::Exception("FEDBuffer") << ss.str();
1415  }
1416  uint16_t result = 0;
1417  //CM median is 10 bits with lowest order byte first. First APV CM median starts in 4th byte of channel data
1418  result |= data_[(offset_+3+2*apvIndex)^7];
1419  result |= ( ((data_[(offset_+4+2*apvIndex)^7]) << 8) & 0x300 );
1420  return result;
1421  }
1422 
1423 }
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)
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)
const double w
Definition: UKUtility.cc:23
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)
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)
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)
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)
FEDBufferFormat bufferFormat() const
TrackerSpecialHeader & setFEEnableRegister(const uint8_t feEnableRegister)