Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*  
0002  *  \author A. Campbell - DESY
0003  */
0004 #ifndef HTBDAQ_DATA_STANDALONE
0005 #include "EventFilter/CastorRawToDigi/interface/CastorMergerData.h"
0006 #else
0007 #include "CastorMergerData.h"
0008 #endif
0009 #include <cstring>
0010 #include <cstdio>
0011 #include <algorithm>
0012 
0013 CastorMergerData::CastorMergerData() : m_formatVersion(-2), m_rawLength(0), m_rawConst(nullptr), m_ownData(nullptr) {}
0014 CastorMergerData::CastorMergerData(const unsigned short* data, int length) {
0015   adoptData(data, length);
0016   m_ownData = nullptr;
0017 }
0018 CastorMergerData::CastorMergerData(const CastorMergerData& hd)
0019     : m_formatVersion(hd.m_formatVersion), m_rawLength(hd.m_rawLength), m_rawConst(hd.m_rawConst), m_ownData(nullptr) {}
0020 
0021 CastorMergerData::CastorMergerData(int version_to_create) : m_formatVersion(version_to_create) {
0022   allocate(version_to_create);
0023 }
0024 
0025 void CastorMergerData::allocate(int version_to_create) {
0026   m_formatVersion = version_to_create;
0027   // the needed space is for the biggest possible event...
0028   const int needed = 0x200;
0029   // create a buffer big enough...
0030   m_ownData = new unsigned short[needed];
0031   m_rawLength = 0;
0032   m_rawConst = m_ownData;
0033 }
0034 
0035 CastorMergerData& CastorMergerData::operator=(const CastorMergerData& hd) {
0036   if (m_ownData == nullptr) {
0037     m_formatVersion = hd.m_formatVersion;
0038     m_rawLength = hd.m_rawLength;
0039     m_rawConst = hd.m_rawConst;
0040   }
0041   return (*this);
0042 }
0043 
0044 void CastorMergerData::adoptData(const unsigned short* data, int length) {
0045   m_rawLength = length;
0046   m_rawConst = data;
0047   if (m_rawLength < 5) {
0048     m_formatVersion = -2;  // invalid!
0049   } else {
0050     m_formatVersion = (m_rawConst[4] >> 12) & 0xF;
0051   }
0052 }
0053 
0054 // check :: not EE, length is reasonable, length matches wordcount
0055 //          length required for tp+daq is correct
0056 
0057 bool CastorMergerData::check() const {
0058   // length checks
0059   //  minimum length
0060   if (m_rawLength < 6 + 12)
0061     return false;
0062   //  matches wordcount
0063   if (m_rawLength != m_rawConst[m_rawLength - 3])
0064     return false;
0065 
0066   return true;
0067 }
0068 
0069 void CastorMergerData::unpack(unsigned char* tp_lengths, unsigned short* tp_samples) const {
0070   if (tp_lengths != nullptr)
0071     memset(tp_lengths, 0, 1);
0072 
0073   int tp_words_total, headerLen, trailerLen;
0074   determineSectionLengths(tp_words_total, headerLen, trailerLen);
0075 
0076   int wordPtr;
0077   const unsigned short* tpBase = m_rawConst + headerLen;
0078   // process the trigger primitive words
0079   if (tp_lengths != nullptr) {
0080     for (wordPtr = 0; wordPtr < tp_words_total; wordPtr++) {
0081       tp_samples[tp_lengths[0]] = tpBase[wordPtr];
0082       tp_lengths[0]++;
0083     }
0084   }
0085 }
0086 void CastorMergerData::determineSectionLengths(int& tpWords, int& headerWords, int& trailerWords) const {
0087   tpWords = m_rawConst[5] >> 8;  // should be 8 but could be up to 12
0088   headerWords = 8;
0089   trailerWords = 0;  // minimum, may be more...
0090 }
0091 
0092 void CastorMergerData::determineStaticLengths(int& headerWords, int& trailerWords) const {
0093   headerWords = 8;
0094   trailerWords = 0;  // minimum, may be more...
0095 }
0096 void CastorMergerData::pack(unsigned char* tp_lengths, unsigned short* tp_samples) {
0097   int tp_words_total = 0, headerLen, trailerLen;
0098   determineStaticLengths(headerLen, trailerLen);
0099 
0100   tp_words_total = 0;
0101   int isample;
0102 
0103   // trigger words
0104   unsigned short* ptr = m_ownData + headerLen;
0105   if (tp_samples != nullptr && tp_lengths != nullptr) {
0106     for (isample = 0; isample < tp_lengths[0] && isample < 12; isample++) {
0107       ptr[tp_words_total] = tp_samples[isample];
0108       tp_words_total++;
0109     }
0110   }
0111 
0112   m_ownData[5] = (tp_words_total << 8) | 0x1;
0113   unsigned short totalLen = headerLen + tp_words_total + trailerLen;
0114 
0115   m_rawLength = totalLen;
0116   m_ownData[totalLen - 2] = totalLen / 2;  // 32-bit words
0117   m_ownData[totalLen - 3] = totalLen;
0118   m_ownData[totalLen - 4] = tp_words_total;
0119 }
0120 
0121 void CastorMergerData::packHeaderTrailer(
0122     int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev) {
0123   m_ownData[0] = L1Anumber & 0xFF;
0124   m_ownData[1] = (L1Anumber & 0xFFFF00) >> 8;
0125 
0126   m_ownData[2] = 0x8000;  // Version is valid, no error bits - status bits need definition
0127   m_ownData[3] = ((orbitn & 0x1F) << 11) | (submodule & 0x7FF);
0128   m_ownData[4] = ((m_formatVersion & 0xF) << 12) | (bcn & 0xFFF);
0129   m_ownData[5] |= ((nps & 0xF) << 4) | 0x1;
0130   m_ownData[6] = ((firmwareRev & 0x70000) >> 3) | (firmwareRev & 0x1FFF);
0131   m_ownData[7] = (pipeline & 0xFF) | ((ndd & 0x1F) << 8);
0132   m_ownData[m_rawLength - 4] &= 0x7FF;
0133   m_ownData[m_rawLength - 4] |= (ndd & 0x1F) << 11;
0134 
0135   m_ownData[m_rawLength - 2] = m_rawLength / 2;  // 32-bit words
0136   m_ownData[m_rawLength - 1] = (L1Anumber & 0xFF) << 8;
0137 }
0138 
0139 unsigned int CastorMergerData::getOrbitNumber() const { return (m_rawConst[3] >> 11); }
0140 
0141 unsigned int CastorMergerData::getFirmwareRevision() const { return (m_rawConst[6]); }