Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:21

0001 #ifndef EventFilter_CSCRawToDigi_CSCALCTTrailer_h
0002 #define EventFilter_CSCRawToDigi_CSCALCTTrailer_h
0003 
0004 /** documented in  flags
0005   http://www.phys.ufl.edu/~madorsky/alctv/alct2000_spec.PDF
0006 */
0007 
0008 #include <cstring>  // memcpy
0009 #ifndef LOCAL_UNPACK
0010 #include <atomic>
0011 #endif
0012 #include "DataFormats/CSCDigi/interface/CSCALCTStatusDigi.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 
0015 struct CSCALCTTrailer2006 {
0016   CSCALCTTrailer2006();
0017 
0018   void setFromBuffer(unsigned short const* buf) { memcpy(this, buf, sizeInWords() * 2); }
0019 
0020   void setSize(int size) { frameCount = size; }
0021   void setCRC(unsigned int crc) {
0022     crc0 = crc & 0x7FF;
0023     crc1 = (crc >> 11) & 0x7FF;
0024   }
0025   short unsigned int sizeInWords() const {  ///size of ALCT Header
0026     return 4;
0027   }
0028   unsigned crc0 : 11, zero_0 : 1, d_0 : 4;
0029   unsigned crc1 : 11, zero_1 : 1, d_1 : 4;
0030   unsigned e0dLine : 16;
0031   unsigned frameCount : 11, reserved_3 : 1, d_3 : 4;
0032 };
0033 
0034 struct CSCALCTTrailer2007 {
0035   CSCALCTTrailer2007();
0036 
0037   void setFromBuffer(unsigned short const* buf) { memcpy(this, buf, sizeInWords() * 2); }
0038 
0039   void setSize(int size) { frameCount = size; }
0040   void setCRC(unsigned int crc) {
0041     crc0 = crc & 0x7FF;
0042     crc1 = (crc >> 11) & 0x7FF;
0043   }
0044   short unsigned int sizeInWords() const {  ///size of ALCT Header
0045     return 4;
0046   }
0047   unsigned e0dLine : 16;
0048   unsigned crc0 : 11, zero_0 : 1, reserved_0 : 4;
0049   unsigned crc1 : 11, zero_1 : 1, reserved_1 : 4;
0050   unsigned frameCount : 11, reserved_3 : 1, reserved_4 : 4;
0051 };
0052 
0053 class CSCALCTTrailer {
0054 public:
0055   ///needed for packing
0056   CSCALCTTrailer(int size, int firmVersion);
0057   CSCALCTTrailer(const unsigned short* buf);
0058   CSCALCTTrailer(const CSCALCTStatusDigi& digi) { CSCALCTTrailer(digi.trailer()); }
0059 
0060   static void setDebug(bool debugValue) { debug = debugValue; }
0061 
0062   unsigned short* data() {
0063 #ifdef LOCAL_UNPACK
0064     switch (firmwareVersion) {
0065 #else
0066     switch (firmwareVersion.load()) {
0067 #endif
0068       case 2006:
0069         memcpy(theOriginalBuffer, &trailer2006, trailer2006.sizeInWords() * 2);
0070         break;
0071       case 2007:
0072         memcpy(theOriginalBuffer, &trailer2007, trailer2007.sizeInWords() * 2);
0073         break;
0074       default:
0075         edm::LogError("CSCALCTTrailer|CSCRawToDigi")
0076             << "couldn't access data: ALCT firmware version is bad/not defined!";
0077         break;
0078     }
0079     return theOriginalBuffer;
0080   }
0081 
0082   /// in 16-bit frames
0083   static int sizeInWords() { return 4; }
0084 
0085   void setCRC(unsigned int crc) {
0086 #ifdef LOCAL_UNPACK
0087     switch (firmwareVersion) {
0088 #else
0089     switch (firmwareVersion.load()) {
0090 #endif
0091       case 2006:
0092         trailer2006.setCRC(crc);
0093         break;
0094       case 2007:
0095         trailer2007.setCRC(crc);
0096         break;
0097       default:
0098         edm::LogError("CSCALCTTrailer|CSCRawToDigi") << "couldn't setCRC: ALCT firmware version is bad/not defined!";
0099         break;
0100     }
0101   }
0102 
0103   int getCRC() {
0104 #ifdef LOCAL_UNPACK
0105     switch (firmwareVersion) {
0106 #else
0107     switch (firmwareVersion.load()) {
0108 #endif
0109       case 2006:
0110         return ((trailer2006.crc1 & 0x7ff) << 11) | (trailer2006.crc0 & 0x7ff);
0111       case 2007:
0112         return ((trailer2007.crc1 & 0x7ff) << 11) | (trailer2007.crc0 & 0x7ff);
0113       default:
0114         edm::LogError("CSCALCTTrailer|CSCRawToDigi") << "couldn't getCRC: ALCT firmware version is bad/not defined!";
0115         return 0;
0116     }
0117   }
0118 
0119   bool check() const {
0120 #ifdef LOCAL_UNPACK
0121     switch (firmwareVersion) {
0122 #else
0123     switch (firmwareVersion.load()) {
0124 #endif
0125       case 2006:
0126         return (trailer2006.e0dLine & 0xfff) == 0xe0d;
0127       case 2007:
0128         return (trailer2007.e0dLine & 0xffff) == 0xde0d;
0129       default:
0130         edm::LogError("CSCALCTTrailer|CSCRawToDigi") << "couldn't check: ALCT firmware version is bad/not defined!";
0131         return false;
0132     }
0133   }
0134 
0135   int wordCount() const {
0136 #ifdef LOCAL_UNPACK
0137     switch (firmwareVersion) {
0138 #else
0139     switch (firmwareVersion.load()) {
0140 #endif
0141       case 2006:
0142         return trailer2006.frameCount;
0143       case 2007:
0144         return trailer2007.frameCount;
0145       default:
0146         edm::LogError("CSCALCTTrailer|CSCRawToDigi") << "couldn't wordCount: ALCT firmware version is bad/not defined!";
0147         return 0;
0148     }
0149   }
0150 
0151   unsigned alctCRCCheck() const {
0152 #ifdef LOCAL_UNPACK
0153     switch (firmwareVersion) {
0154 #else
0155     switch (firmwareVersion.load()) {
0156 #endif
0157       case 2006:
0158         return trailer2006.reserved_3;
0159       case 2007:
0160         return trailer2007.reserved_3;
0161       default:
0162         edm::LogError("CSCALCTTrailer|CSCRawToDigi") << "couldn't CRCcheck: ALCT firmware version is bad/not defined!";
0163         return 0;
0164     }
0165   }
0166 
0167   unsigned FrameCount() const { return wordCount(); }
0168 
0169   CSCALCTTrailer2006 alctTrailer2006() { return trailer2006; }
0170   CSCALCTTrailer2007 alctTrailer2007() { return trailer2007; }
0171 
0172 private:
0173 #ifdef LOCAL_UNPACK
0174   static bool debug;
0175   static unsigned short int firmwareVersion;
0176 #else
0177   static std::atomic<bool> debug;
0178   static std::atomic<unsigned short int> firmwareVersion;
0179 #endif
0180 
0181   CSCALCTTrailer2006 trailer2006;
0182   CSCALCTTrailer2007 trailer2007;
0183   unsigned short int theOriginalBuffer[4];
0184 };
0185 
0186 #endif