Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:14:47

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::checkCRC() const {
0063   // check DAQ error flags
0064   if (daqErrorFlags != 0)
0065     return false;
0066 
0067   // return true if CRC not present
0068   if (!isCRCPresent())
0069     return true;
0070 
0071   // compare CRC
0072   word crc_fin = 0xffff;
0073 
0074   for (int i = 11; i >= 1; i--)
0075     crc_fin = calculateCRC(crc_fin, data[i]);
0076 
0077   return (crc_fin == data[0]);
0078 }
0079 
0080 VFATFrame::word VFATFrame::calculateCRC(VFATFrame::word crc_in, VFATFrame::word dato) {
0081   word v = 0x0001;
0082   word mask = 0x0001;
0083   bool d = false;
0084   word crc_temp = crc_in;
0085   unsigned char datalen = 16;
0086 
0087   for (int i = 0; i < datalen; i++) {
0088     if (dato & v)
0089       d = true;
0090     else
0091       d = false;
0092 
0093     if ((crc_temp & mask) ^ d)
0094       crc_temp = crc_temp >> 1 ^ 0x8408;
0095     else
0096       crc_temp = crc_temp >> 1;
0097 
0098     v <<= 1;
0099   }
0100 
0101   return crc_temp;
0102 }
0103 
0104 void VFATFrame::Print(bool binary) const {
0105   if (binary) {
0106     for (int i = 0; i < 12; i++) {
0107       const word &w = data[11 - i];
0108       word mask = (1 << 15);
0109       for (int j = 0; j < 16; j++) {
0110         if (w & mask)
0111           printf("1");
0112         else
0113           printf("0");
0114         mask = (mask >> 1);
0115         if ((j + 1) % 4 == 0)
0116           printf("|");
0117       }
0118       printf("\n");
0119     }
0120   } else {
0121     printf("ID = %03x, BC = %04u, EC = %03u, flags = %2u, CRC = %04x ",
0122            getChipID(),
0123            getBC(),
0124            getEC(),
0125            getFlags(),
0126            getCRC());
0127 
0128     if (checkCRC())
0129       printf("(  OK), footprint ");
0130     else
0131       printf("(FAIL), footprint ");
0132 
0133     if (checkFootprint())
0134       printf("  OK");
0135     else
0136       printf("FAIL");
0137 
0138     printf(", frame = %04x|%04x|%04x|", data[11], data[10], data[9]);
0139     for (int i = 8; i > 0; i--)
0140       printf("%04x", data[i]);
0141     printf("|%04x", data[0]);
0142 
0143     printf(", presFl=%x", presenceFlags);
0144     printf(", daqErrFl=%x", daqErrorFlags);
0145 
0146     printf("\n");
0147   }
0148 }