Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*  
0002  *  \author A. Campbell -- DESY
0003  */
0004 #include "EventFilter/CastorRawToDigi/interface/CastorCORData.h"
0005 #include "EventFilter/CastorRawToDigi/interface/CastorMergerData.h"
0006 #include "EventFilter/CastorRawToDigi/interface/CastorCTDCHeader.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include <cstring>
0009 #include <cstdint>
0010 
0011 const int CastorCTDCHeader::SPIGOT_COUNT = 2;  // COR spigots - does not include merger pay load
0012 
0013 CastorCTDCHeader::CastorCTDCHeader() {}
0014 
0015 unsigned int CastorCTDCHeader::getTotalLengthBytes() const {
0016   unsigned int totalSize = sizeof(CastorCTDCHeader);
0017   for (int i = 0; i < SPIGOT_COUNT + 1; i++)  // includes merger pay load
0018     totalSize += (spigotInfo[i] & 0x3FF) * 4;
0019   return totalSize;  // doesn't include the trailer
0020 }
0021 
0022 int CastorCTDCHeader::getSpigotData(int nspigot, CastorCORData& decodeTool, int validSize) const {
0023   const uint16_t* base = ((const uint16_t*)this) + sizeof(CastorCTDCHeader) / sizeof(uint16_t);
0024   int offset = 0, i, len = 0;
0025   for (i = 0; i <= nspigot; i++) {
0026     offset += len;
0027     len = (spigotInfo[i] & 0x3FF) * 2;
0028   }
0029   if ((offset + len + sizeof(CastorCTDCHeader) / sizeof(uint16_t)) < (validSize / sizeof(uint16_t))) {
0030     decodeTool.adoptData(base + offset, len);
0031     return 0;
0032   } else {
0033     return -1;
0034   }
0035 }
0036 
0037 void CastorCTDCHeader::clear() {
0038   commondataformat0 = 0;
0039   commondataformat1 = 0x50000000u;
0040   commondataformat2 = 0;
0041   commondataformat3 = 0;
0042   ctdch0 = 0x1;  // format version 1
0043   ctdch1 = 0;
0044   for (int i = 0; i < 3; i++)
0045     spigotInfo[i] = 0;
0046   spigotInfo[3] = 0x12345678;  // end DCC header pattern
0047 }
0048 
0049 void CastorCTDCHeader::setHeader(int sourceid, int bcn, int l1aN, int orbN) {
0050   commondataformat0 = 0x8 | ((sourceid & 0xFFF) << 8) | ((bcn & 0xFFF) << 20);
0051   commondataformat1 = 0x50000000u | (l1aN & 0xFFFFFF);
0052 }
0053 
0054 void CastorCTDCHeader::copySpigotData(unsigned int spigot_id,
0055                                       const CastorCORData& data,
0056                                       bool valid,
0057                                       unsigned char LRB_error_word) {
0058   if (spigot_id >= (unsigned int)SPIGOT_COUNT)
0059     return;
0060   // construct the spigot info
0061   spigotInfo[spigot_id] = (data.getRawLength() / 2) | 0xc000;
0062   if (valid)
0063     spigotInfo[spigot_id] |= 0x2000;
0064   spigotInfo[spigot_id] |= (LRB_error_word << 16) | ((data.getErrorsWord() & 0xFF) << 24);
0065   // status info...
0066   if (valid)
0067     ctdch0 |= (1 << (spigot_id + 14));
0068   // copy
0069   unsigned int lenSoFar = 0;
0070   for (unsigned int i = 0; i < spigot_id; i++)
0071     lenSoFar += getSpigotDataLength(i);
0072   unsigned short* startingPoint =
0073       ((unsigned short*)this) + sizeof(CastorCTDCHeader) / sizeof(unsigned short) + lenSoFar * 2;
0074   memcpy(startingPoint, data.getRawData(), sizeof(unsigned short) * data.getRawLength());
0075   // update the trailer...
0076   lenSoFar += data.getRawLength() / 2;  // 32-bit words
0077   uint32_t* trailer = ((uint32_t*)this) + sizeof(CastorCTDCHeader) / sizeof(uint32_t) + lenSoFar;
0078   int len64 = sizeof(CastorCTDCHeader) / 8 + lenSoFar / 2 + 1;
0079   trailer[1] = 0;
0080   trailer[0] = 0xA0000000u | len64;
0081 }
0082 
0083 void CastorCTDCHeader::copyMergerData(const CastorMergerData& data, bool valid) {
0084   unsigned int spigot_id = 2;
0085   // construct the spigot info
0086   spigotInfo[spigot_id] = (data.getRawLength() / 2) | 0xc000;  // Enabled & Present
0087   if (valid)
0088     spigotInfo[spigot_id] |= 0x2000;  // Valid
0089   spigotInfo[spigot_id] |= ((data.getErrorsWord() & 0xFF) << 24);
0090   // status info...
0091   if (valid)
0092     ctdch0 |= (1 << (spigot_id + 14));
0093   // copy
0094   unsigned int lenSoFar = 0;
0095   for (unsigned int i = 0; i < spigot_id; i++)
0096     lenSoFar += getSpigotDataLength(i);
0097   unsigned short* startingPoint =
0098       ((unsigned short*)this) + sizeof(CastorCTDCHeader) / sizeof(unsigned short) + lenSoFar * 2;
0099   memcpy(startingPoint, data.getRawData(), sizeof(unsigned short) * data.getRawLength());
0100   // update the trailer...
0101   lenSoFar += data.getRawLength() / 2;  // 32-bit words
0102   uint32_t* trailer = ((uint32_t*)this) + sizeof(CastorCTDCHeader) / sizeof(uint32_t) + lenSoFar;
0103   int len64 = sizeof(CastorCTDCHeader) / 8 + lenSoFar / 2 + 1;
0104   trailer[1] = 0;
0105   trailer[0] = 0xA0000000u | len64;
0106 }
0107 
0108 std::ostream& operator<<(std::ostream& s, const CastorCTDCHeader& head) {
0109   for (int i = 0; i < CastorCTDCHeader::SPIGOT_COUNT + 1; i++) {
0110     s << "Spigot " << i << " : " << head.getSpigotDataLength(i) << " bytes, ";
0111     if (head.getSpigotEnabled(i))
0112       s << "E";
0113     if (head.getSpigotPresent(i))
0114       s << "P";
0115     if (head.getSpigotValid(i))
0116       s << "V";
0117     s << ". Error codes: " << std::hex << int(head.getSpigotErrorBits(i)) << std::dec;
0118     s << std::endl;
0119   }
0120   return s;
0121 }