Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "EventFilter/CSCRawToDigi/interface/CSCRPCData.h"
0002 #include "DataFormats/CSCDigi/interface/CSCRPCDigi.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 
0005 #include <string>
0006 #include <cstdio>
0007 #include <strings.h>  // for bzero
0008 #include <cstring>
0009 
0010 /** data format is
0011   RPC0  Tbin 0  Pads[7:0]
0012   RPC0  BXN     Pads[15:8]
0013   RPC0  Tbin 1  Pads[7:0]
0014 ...
0015   RPC0  Tbin 6  Pads[7:0]
0016   RPC3  BXN     Pads[15:8]
0017 */
0018 
0019 #ifdef LOCAL_UNPACK
0020 bool CSCRPCData::debug = false;
0021 #else
0022 std::atomic<bool> CSCRPCData::debug{false};
0023 #endif
0024 
0025 CSCRPCData::CSCRPCData(int ntbins) : ntbins_(ntbins), size_(0) {
0026   theData[0] = 0x6b04;
0027   for (int i = 1; i < 257; ++i) {
0028     // data format is bits 12-14 are RPC number, 0 to 3
0029     int rpc = (i - 1) / 14;
0030     theData[i] = rpc << 12;
0031 
0032     // bits 8-11 of the first word of the pair is time bin
0033     int tbin = ((i - 1) % 14) / 2;
0034     theData[i] |= tbin << 8;
0035   }
0036   theData[257] = 0x6e04;
0037 }
0038 
0039 CSCRPCData::CSCRPCData(const unsigned short* buf, int length) : size_(length) {
0040   //size_ = ntbins_*2*4+2;
0041   // header & trailer word, + 4 RPCs per time bin, 2 lines per RPC
0042   ntbins_ = (size_ - 2) / 8;
0043   memcpy(theData, buf, size_ * 2);
0044 }
0045 
0046 void CSCRPCData::Print() const {
0047   LogTrace("CSCRPCData|CSCRawToDigi") << "CSCRPCData.Print";
0048   for (int line = 0; line < ((size_)); ++line) {
0049     LogTrace("CSCRPCData|CSCRawToDigi") << std::hex << theData[line];
0050   }
0051 
0052   for (int linePair = 0; linePair < ((size_ - 2) / 2); ++linePair) {
0053     // skip header word
0054     int pos = linePair * 2 + 1;
0055     // make the two pad words into one and see if it's empty
0056     //int pad = theData[pos] & 0xff + ((theData[pos+1] & 0x3f) << 8);
0057 
0058     int bxnnew = ((theData[pos + 1] >> 8) & 0x7);
0059 
0060     int rpc = (theData[pos] >> 12) & 0x7;
0061     int tbin = (theData[pos] >> 8) & 0xf;
0062     int bxn = bxnnew;
0063 
0064     LogTrace("CSCRPCData|CSCRawToDigi") << " RPC=" << rpc << " Tbin=" << tbin << " BXN=" << bxn;
0065   }
0066 }
0067 
0068 std::vector<int> CSCRPCData::BXN() const {
0069   std::vector<int> result;
0070   for (int linePair = 0; linePair < ((size_ - 2) / 2); ++linePair) {
0071     // skip header word
0072     int pos = linePair * 2 + 1;
0073     /// make the two pad words into one and see if it's empty
0074     //int pad = theData[pos] & 0xff + ((theData[pos+1] & 0x3f) << 8);
0075 
0076     int bxnnew = ((theData[pos + 1] >> 8) & 0x7);
0077     //int bxnnew = (((theData[pos+1] >> 8)  & 0x3 )<<2) | ((theData[pos+1]>>6)&0x3) ;
0078 
0079     int rpc = (theData[pos] >> 12) & 0x7;
0080     //int tbin = (theData[pos]   >> 8)  & 0xf;
0081     //int bxn  = bxnnew;
0082     result.push_back(bxnnew);
0083     result.push_back(rpc);
0084   }
0085   return result;
0086 }
0087 
0088 std::vector<CSCRPCDigi> CSCRPCData::digis() const {
0089   std::vector<CSCRPCDigi> result;
0090   int bxnold = 0;
0091   int bxnnew = 0;
0092   //int bxnewGreg;
0093   for (int linePair = 0; linePair < ((size_ - 2) / 2); ++linePair) {
0094     // skip header word
0095     int pos = linePair * 2 + 1;
0096     //  LogTrace("RPC") << "+++ CSCRPCData " << std::hex << theData[pos]
0097     //           << " " << theData[pos+1];
0098     if (debug)
0099       LogTrace("CSCRPCData|CSCRawToDigi") << "+++ CSCRPCData " << std::hex << theData[pos] << " " << theData[pos + 1];
0100     // make the two pad words into one and see if it's empty
0101     int pad = (theData[pos] & 0xff) + ((theData[pos + 1] & 0xff) << 8);
0102 
0103     //bxnnew = (((theData[pos+1] >> 8)  & 0x3 )<<2) | ((theData[pos+1]>>6)&0x3) ;
0104     bxnnew = ((theData[pos + 1] >> 8) & 0x7);
0105     //LogTrace("RPC") << "               " << "bxnnew" << " " << bxnnew;
0106     //LogTrace("RPC") << "               " << "bxnnewGreg" << " " << bxnewGreg;
0107     if (linePair == 0)
0108       bxnold = bxnnew;
0109     if (bxnnew - bxnold > 1)
0110       LogTrace("CSCRPCData|CSCRawToDigi")
0111           << "+++ CSCRPCData warning: RPC BXN is incrementing by more than 1 clock cycle";
0112     bxnold = bxnnew;
0113 
0114     if (pad != 0) {
0115       if (debug)
0116         LogTrace("CSCRPCData|CSCRawToDigi")
0117             << "+++ CSCRPCData Found a PAD =" << std::hex << pad << " " << theData[pos] << " + " << theData[pos + 1];
0118       int rpc = (theData[pos] >> 12) & 0x7;
0119       int tbin = (theData[pos] >> 8) & 0xf;
0120       int bxn = bxnnew;
0121       //LogTrace("RPC") << " rpc: " << rpc << " bxn: " << bxn << " tbin: " << tbin;
0122       for (int i = 0; i < 16; ++i) {
0123         // if the bit is set, make a digi
0124         if ((pad >> i) & 1) {
0125           result.push_back(CSCRPCDigi(rpc, i, bxn, tbin));
0126           //LogTrace("RPC") << "digi-->" << " rpc: " << rpc << " i: " << i << " bxn: " << bxn << " tbin: " << tbin;
0127         }
0128       }
0129     }
0130   }
0131   return result;
0132 }