Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:45:16

0001 /****************************************************************************
0002 *
0003 * This is a part of the TOTEM testbeam/monitoring software.
0004 * This is a part of the TOTEM offline software.
0005 * Authors: 
0006 *  Jan Kašpar (jan.kaspar@gmail.com) 
0007 *  Leszek Grzanka
0008 *
0009 ****************************************************************************/
0010 
0011 #include "EventFilter/CTPPSRawToDigi/interface/VFATFrame.h"
0012 
0013 #include <cstdio>
0014 #include <cstring>
0015 
0016 VFATFrame::VFATFrame(const VFATFrame::word *_data)
0017     : presenceFlags(15),   // by default BC, EC, ID and CRC are present
0018       daqErrorFlags(0),    // by default, no DAQ error
0019       numberOfClusters(0)  // no clusters by default
0020 {
0021   if (_data)
0022     setData(_data);
0023   else
0024     memset(data, 0, 12 * sizeof(word));
0025 }
0026 
0027 void VFATFrame::setData(const VFATFrame::word *_data) { memcpy(data, _data, 24); }
0028 
0029 std::vector<unsigned char> VFATFrame::getActiveChannels() const {
0030   std::vector<unsigned char> channels;
0031 
0032   for (int i = 0; i < 8; i++) {
0033     // quick check
0034     if (!data[1 + i])
0035       continue;
0036 
0037     // go throug bits
0038     word mask;
0039     char offset;
0040     for (mask = 1 << 15, offset = 15; mask; mask >>= 1, offset--) {
0041       if (data[1 + i] & mask)
0042         channels.push_back(i * 16 + offset);
0043     }
0044   }
0045 
0046   return channels;
0047 }
0048 
0049 bool VFATFrame::checkFootprint() const {
0050   if (isIDPresent() && (data[9] & 0xF000) != 0xE000)
0051     return false;
0052 
0053   if (isECPresent() && (data[10] & 0xF000) != 0xC000)
0054     return false;
0055 
0056   if (isBCPresent() && (data[11] & 0xF000) != 0xA000)
0057     return false;
0058 
0059   return true;
0060 }
0061 
0062 bool VFATFrame::checkFootprintT2() const {
0063   if (isIDPresent() && (data[2] & 0xF000) != 0xE000)
0064     return false;
0065 
0066   if (isECPresent() && (data[1] & 0xF000) != 0xC000)
0067     return false;
0068 
0069   if (isBCPresent() && (data[0] & 0xF000) != 0xA000)
0070     return false;
0071 
0072   return true;
0073 }
0074 
0075 bool VFATFrame::checkCRC() const {
0076   // check DAQ error flags
0077   if (daqErrorFlags != 0)
0078     return false;
0079 
0080   // return true if CRC not present
0081   if (!isCRCPresent())
0082     return true;
0083 
0084   // compare CRC
0085   word crc_fin = 0xffff;
0086 
0087   for (int i = 11; i >= 1; i--)
0088     crc_fin = calculateCRC(crc_fin, data[i]);
0089 
0090   return (crc_fin == data[0]);
0091 }
0092 
0093 bool VFATFrame::checkCRCT2() const {
0094   // return true if CRC not present
0095   if (!isCRCPresent())
0096     return true;
0097 
0098   // compare CRC
0099   word crc_fin = 0xffff;
0100 
0101   for (int i = 0; i < 11; i++)
0102     crc_fin = calculateCRC(crc_fin, data[i]);
0103 
0104   return (crc_fin == data[11]);
0105 }
0106 
0107 VFATFrame::word VFATFrame::calculateCRC(VFATFrame::word crc_in, VFATFrame::word dato) {
0108   word v = 0x0001;
0109   word mask = 0x0001;
0110   bool d = false;
0111   word crc_temp = crc_in;
0112   unsigned char datalen = 16;
0113 
0114   for (int i = 0; i < datalen; i++) {
0115     if (dato & v)
0116       d = true;
0117     else
0118       d = false;
0119 
0120     if ((crc_temp & mask) ^ d)
0121       crc_temp = crc_temp >> 1 ^ 0x8408;
0122     else
0123       crc_temp = crc_temp >> 1;
0124 
0125     v <<= 1;
0126   }
0127 
0128   return crc_temp;
0129 }
0130 
0131 void VFATFrame::Print(bool binary) const {
0132   if (binary) {
0133     for (int i = 0; i < 12; i++) {
0134       const word &w = data[11 - i];
0135       word mask = (1 << 15);
0136       for (int j = 0; j < 16; j++) {
0137         if (w & mask)
0138           printf("1");
0139         else
0140           printf("0");
0141         mask = (mask >> 1);
0142         if ((j + 1) % 4 == 0)
0143           printf("|");
0144       }
0145       printf("\n");
0146     }
0147   } else {
0148     printf("ID = %03x, BC = %04u, EC = %03u, flags = %2u, CRC = %04x ",
0149            getChipID(),
0150            getBC(),
0151            getEC(),
0152            getFlags(),
0153            getCRC());
0154 
0155     if (checkCRC())
0156       printf("(  OK), footprint ");
0157     else
0158       printf("(FAIL), footprint ");
0159 
0160     if (checkFootprint())
0161       printf("  OK");
0162     else
0163       printf("FAIL");
0164 
0165     printf(", frame = %04x|%04x|%04x|", data[11], data[10], data[9]);
0166     for (int i = 8; i > 0; i--)
0167       printf("%04x", data[i]);
0168     printf("|%04x", data[0]);
0169 
0170     printf(", presFl=%x", presenceFlags);
0171     printf(", daqErrFl=%x", daqErrorFlags);
0172 
0173     printf("\n");
0174   }
0175 }
0176 
0177 void VFATFrame::PrintT2(bool binary) const {
0178   if (binary) {
0179     for (int i = 0; i < 12; i++) {
0180       const word &w = data[i];
0181       word mask = (1 << 15);
0182       for (int j = 0; j < 16; j++) {
0183         if (w & mask)
0184           printf("1");
0185         else
0186           printf("0");
0187         mask = (mask >> 1);
0188         if ((j + 1) % 4 == 0)
0189           printf("|");
0190       }
0191       printf("\n");
0192     }
0193   } else {
0194     // print right CRC
0195     word crc_fin = 0xffff;
0196 
0197     for (int i = 0; i < 11; i++)
0198       crc_fin = calculateCRC(crc_fin, data[i]);
0199 
0200     printf("CRC = %04x ", getCRCT2());
0201 
0202     if (checkCRCT2())
0203       printf("(  OK), footprint ");
0204     else
0205       printf("(FAIL, right = %04x), footprint ", crc_fin);
0206 
0207     if (checkFootprintT2())
0208       printf("  OK");
0209     else
0210       printf("FAIL");
0211 
0212     printf("Frame = %04x|%04x|%04x|", data[0], data[1], data[2]);
0213     for (int i = 3; i < 11; i++)
0214       printf("%04x", data[i]);
0215     printf("|%04x", data[11]);
0216 
0217     printf("\n");
0218   }
0219 }