Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-21 02:12:28

0001 // Code to unpack the "RPC Data Record"
0002 
0003 #include "EventFilter/L1TRawToDigi/plugins/UnpackerFactory.h"
0004 
0005 #include "EMTFCollections.h"
0006 #include "EMTFUnpackerTools.h"
0007 
0008 // This is the "header" - no EMTFBlockRPC.h file is needed
0009 namespace l1t {
0010   namespace stage2 {
0011     namespace emtf {
0012 
0013       class RPCBlockUnpacker : public Unpacker {  // "RPCBlockUnpacker" inherits from "Unpacker"
0014       public:
0015         virtual int checkFormat(const Block& block);
0016         // virtual bool checkFormat() override; // Return "false" if block format does not match expected format
0017         bool unpack(const Block& block,
0018                     UnpackerCollections* coll) override;  // Apparently it's always good to use override in C++
0019         // virtual bool packBlock(const Block& block, UnpackerCollections *coll) override;
0020       };
0021 
0022       // class RPCBlockPacker : public Packer { // "RPCBlockPacker" inherits from "Packer"
0023       // public:
0024       //    virtual bool unpack(const Block& block, UnpackerCollections *coll) override; // Apparently it's always good to use override in C++
0025       // };
0026 
0027     }  // namespace emtf
0028   }    // namespace stage2
0029 }  // namespace l1t
0030 
0031 namespace l1t {
0032   namespace stage2 {
0033     namespace emtf {
0034 
0035       int RPCBlockUnpacker::checkFormat(const Block& block) {
0036         auto payload = block.payload();
0037         int errors = 0;
0038 
0039         // Check the number of 16-bit words
0040         if (payload.size() != 4) {
0041           errors += 1;
0042           edm::LogError("L1T|EMTF") << "Payload size in 'RPC Data Record' is different than expected";
0043         }
0044 
0045         // Check that each word is 16 bits
0046         for (unsigned int i = 0; i < 4; i++) {
0047           if (GetHexBits(payload[i], 16, 31) != 0) {
0048             errors += 1;
0049             edm::LogError("L1T|EMTF") << "Payload[" << i << "] has more than 16 bits in 'RPC Data Record'";
0050           }
0051         }
0052 
0053         uint16_t RPCa = payload[0];
0054         uint16_t RPCb = payload[1];
0055         uint16_t RPCc = payload[2];
0056         uint16_t RPCd = payload[3];
0057 
0058         // Check Format
0059         if (GetHexBits(RPCa, 15, 15) != 0) {
0060           errors += 1;
0061           edm::LogError("L1T|EMTF") << "Format identifier bits in RPCa are incorrect";
0062         }
0063         if (GetHexBits(RPCb, 15, 15) != 0) {
0064           errors += 1;
0065           edm::LogError("L1T|EMTF") << "Format identifier bits in RPCb are incorrect";
0066         }
0067         if (GetHexBits(RPCc, 15, 15) != 1) {
0068           errors += 1;
0069           edm::LogError("L1T|EMTF") << "Format identifier bits in RPCc are incorrect";
0070         }
0071         if (GetHexBits(RPCd, 15, 15) != 0) {
0072           errors += 1;
0073           edm::LogError("L1T|EMTF") << "Format identifier bits in RPCd are incorrect";
0074         }
0075 
0076         return errors;
0077       }
0078 
0079       // Converts station, ring, sector, subsector, neighbor, and segment from the RPC output
0080       void convert_RPC_location(int& station,
0081                                 int& ring,
0082                                 int& sector,
0083                                 int& subsector,
0084                                 int& neighbor,
0085                                 int& segment,
0086                                 const int evt_sector,
0087                                 const int frame,
0088                                 const int word,
0089                                 const int link) {
0090         station = -99;
0091         ring = -99;
0092         sector = -99;
0093         subsector = -99;
0094         neighbor = -99;
0095         segment = -99;
0096 
0097         // "link" is the "link index" field (0 - 6) in the EMTF DAQ document, not "link number" (1 - 7)
0098         // Neighbor indicated by link == 0
0099         sector = (link != 0 ? evt_sector : (evt_sector == 1 ? 6 : evt_sector - 1));
0100         subsector = (link != 0 ? link : 6);
0101         neighbor = (link == 0 ? 1 : 0);
0102         segment = (word % 2);
0103 
0104         if (frame == 0) {
0105           station = (word < 2 ? 1 : 2);
0106           ring = 2;
0107         } else if (frame == 1) {
0108           station = 3;
0109           ring = (word < 2 ? 2 : 3);
0110         } else if (frame == 2) {
0111           station = 4;
0112           ring = (word < 2 ? 2 : 3);
0113         }
0114       }  // End function: void convert_RPC_location()
0115 
0116       bool RPCBlockUnpacker::unpack(const Block& block, UnpackerCollections* coll) {
0117         // std::cout << "Inside EMTFBlockRPC.cc: unpack" << std::endl;
0118 
0119         // Get the payload for this block, made up of 16-bit words (0xffff)
0120         // Format defined in MTF7Payload::getBlock() in src/Block.cc
0121         // payload[0] = bits 0-15, payload[1] = 16-31, payload[3] = 32-47, etc.
0122         auto payload = block.payload();
0123 
0124         // Run 3 has a different EMTF DAQ output format since August 26th
0125         // Computed as (Year - 2000)*2^9 + Month*2^5 + Day (see Block.cc and EMTFBlockTrailers.cc)
0126         bool run3_DAQ_format =
0127             (getAlgoVersion() >=
0128              11546);  // Firmware from 26.08.22 which enabled new Run 3 DAQ format for RPCs - EY 13.09.22
0129 
0130         int nTPs = run3_DAQ_format ? 2 : 1;
0131 
0132         // Check Format of Payload
0133         l1t::emtf::RPC RPC_;
0134         for (int err = 0; err < checkFormat(block); err++)
0135           RPC_.add_format_error();
0136 
0137         // Assign payload to 16-bit words
0138         uint16_t RPCa = payload[0];
0139         uint16_t RPCb = payload[1];
0140         uint16_t RPCc = payload[2];
0141         uint16_t RPCd = payload[3];
0142 
0143         // If there are 2 TPs in the block we fill them 1 by 1
0144         for (int i = 1; i <= nTPs; i++) {
0145           // res is a pointer to a collection of EMTFDaqOut class objects
0146           // There is one EMTFDaqOut for each MTF7 (60 deg. sector) in the event
0147           EMTFDaqOutCollection* res;
0148           res = static_cast<EMTFCollections*>(coll)->getEMTFDaqOuts();
0149           int iOut = res->size() - 1;
0150 
0151           EMTFHitCollection* res_hit;
0152           res_hit = static_cast<EMTFCollections*>(coll)->getEMTFHits();
0153           EMTFHit Hit_;
0154 
0155           CPPFDigiCollection* res_CPPF;
0156           res_CPPF = static_cast<EMTFCollections*>(coll)->getEMTFCPPFs();
0157 
0158           ////////////////////////////
0159           // Unpack the RPC Data Record
0160           ////////////////////////////
0161 
0162           if (run3_DAQ_format) {  // Run 3 DAQ format has 2 TPs per block
0163             if (i == 1) {
0164               RPC_.set_phi(GetHexBits(RPCa, 0, 10));
0165               RPC_.set_word(GetHexBits(RPCa, 11, 12));
0166               RPC_.set_frame(GetHexBits(RPCa, 13, 14));
0167 
0168               RPC_.set_tbin(GetHexBits(RPCb, 0, 2));
0169               RPC_.set_vp(GetHexBits(RPCb, 3, 3));
0170               RPC_.set_theta(GetHexBits(RPCb, 4, 8));
0171               RPC_.set_bc0(GetHexBits(RPCb, 9, 9));
0172               RPC_.set_link(GetHexBits(RPCb, 12, 14));  // Link index (0 - 6); link number runs 1 - 7
0173             } else if (i == 2) {
0174               RPC_.set_phi(GetHexBits(RPCc, 0, 10));
0175               RPC_.set_word(GetHexBits(RPCc, 11, 12));
0176               RPC_.set_frame(GetHexBits(RPCc, 13, 14));
0177 
0178               RPC_.set_tbin(GetHexBits(RPCd, 0, 2));
0179               RPC_.set_vp(GetHexBits(RPCd, 3, 3));
0180               RPC_.set_theta(GetHexBits(RPCd, 4, 8));
0181               RPC_.set_bc0(GetHexBits(RPCd, 9, 9));
0182               RPC_.set_link(GetHexBits(RPCd, 12, 14));  // Link index (0 - 6); link number runs 1 - 7
0183             }
0184           } else {  // Run 2 DAQ format
0185             RPC_.set_phi(GetHexBits(RPCa, 0, 10));
0186 
0187             RPC_.set_theta(GetHexBits(RPCb, 0, 4));
0188             RPC_.set_word(GetHexBits(RPCb, 8, 9));
0189             RPC_.set_frame(GetHexBits(RPCb, 10, 11));
0190             RPC_.set_link(GetHexBits(RPCb, 12, 14));  // Link index (0 - 6); link number runs 1 - 7
0191 
0192             RPC_.set_rpc_bxn(GetHexBits(RPCc, 0, 11));
0193             RPC_.set_bc0(GetHexBits(RPCc, 14, 14));
0194 
0195             RPC_.set_tbin(GetHexBits(RPCd, 0, 2));
0196             RPC_.set_vp(GetHexBits(RPCd, 3, 3));
0197 
0198             // RPC_.set_dataword            ( uint64_t dataword);
0199           }
0200 
0201           // Convert specially-encoded RPC quantities
0202           int _station, _ring, _sector, _subsector, _neighbor, _segment;
0203           convert_RPC_location(_station,
0204                                _ring,
0205                                _sector,
0206                                _subsector,
0207                                _neighbor,
0208                                _segment,
0209                                (res->at(iOut)).PtrEventHeader()->Sector(),
0210                                RPC_.Frame(),
0211                                RPC_.Word(),
0212                                RPC_.Link());
0213 
0214           // Rotate by 20 deg to match RPC convention in CMSSW
0215           int _sector_rpc = (_subsector < 5) ? _sector : (_sector % 6) + 1;
0216           // Rotate by 2 to match RPC convention in CMSSW (RPCDetId.h)
0217           int _subsector_rpc = ((_subsector + 1) % 6) + 1;
0218           // Define chamber number
0219           int _chamber = (_sector_rpc - 1) * 6 + _subsector_rpc;
0220           // Define CSC-like subsector
0221           int _subsector_csc = (_station != 1) ? 0 : ((_chamber % 6 > 2) ? 1 : 2);
0222 
0223           Hit_.set_station(_station);
0224           Hit_.set_ring(_ring);
0225           Hit_.set_sector(_sector);
0226           Hit_.set_subsector(_subsector_csc);
0227           Hit_.set_sector_RPC(_sector_rpc);
0228           Hit_.set_subsector_RPC(_subsector_rpc);
0229           Hit_.set_chamber(_chamber);
0230           Hit_.set_neighbor(_neighbor);
0231           Hit_.set_pc_segment(_segment);
0232           Hit_.set_fs_segment(_segment);
0233           Hit_.set_bt_segment(_segment);
0234 
0235           // Fill the EMTFHit
0236           ImportRPC(Hit_, RPC_, (res->at(iOut)).PtrEventHeader()->Endcap(), (res->at(iOut)).PtrEventHeader()->Sector());
0237 
0238           // Set the stub number for this hit
0239           // Each chamber can send up to 2 stubs per BX
0240           // Also count stubs in corresponding CSC chamber; RPC hit counting is on top of LCT counting
0241           Hit_.set_stub_num(0);
0242           // See if matching hit is already in event record
0243           bool exact_duplicate = false;
0244           for (auto const& iHit : *res_hit) {
0245             if (Hit_.BX() == iHit.BX() && Hit_.Endcap() == iHit.Endcap() && Hit_.Station() == iHit.Station() &&
0246                 Hit_.Chamber() == iHit.Chamber()) {
0247               if ((iHit.Is_CSC() == 1 && iHit.Ring() == 2) ||
0248                   (iHit.Is_RPC() == 1)) {  // RPC rings 2 and 3 both map to CSC ring 2
0249                 if (Hit_.Neighbor() == iHit.Neighbor()) {
0250                   Hit_.set_stub_num(Hit_.Stub_num() + 1);
0251                   if (iHit.Is_RPC() == 1 && iHit.Ring() == Hit_.Ring() && iHit.Theta_fp() == Hit_.Theta_fp() &&
0252                       iHit.Phi_fp() == Hit_.Phi_fp()) {
0253                     exact_duplicate = true;
0254                   }
0255                 }
0256               }
0257             }
0258           }  // End loop: for (auto const & iHit : *res_hit)
0259 
0260           // Reject TPs with out-of-range BX values. This needs to be adjusted if we increase l1a_window parameter in EMTF config - EY 03.08.2022
0261           if (Hit_.BX() > 3 or Hit_.BX() < -3) {
0262             edm::LogWarning("L1T|EMTF") << "EMTF unpacked CPPF digis with out-of-range BX! BX " << Hit_.BX()
0263                                         << ", endcap " << Hit_.Endcap() << ", station " << Hit_.Station() << ", sector "
0264                                         << Hit_.Sector() << ", neighbor " << Hit_.Neighbor() << ", ring " << Hit_.Ring()
0265                                         << ", chamber " << Hit_.Chamber() << ", theta " << Hit_.Theta_fp() / 4
0266                                         << ", phi " << Hit_.Phi_fp() / 4 << std::endl;
0267             return true;
0268           }
0269 
0270           if (exact_duplicate)
0271             edm::LogWarning("L1T|EMTF") << "EMTF unpacked duplicate CPPF digis: BX " << Hit_.BX() << ", endcap "
0272                                         << Hit_.Endcap() << ", station " << Hit_.Station() << ", sector "
0273                                         << Hit_.Sector() << ", neighbor " << Hit_.Neighbor() << ", ring " << Hit_.Ring()
0274                                         << ", chamber " << Hit_.Chamber() << ", theta " << Hit_.Theta_fp() / 4
0275                                         << ", phi " << Hit_.Phi_fp() / 4 << std::endl;
0276 
0277           (res->at(iOut)).push_RPC(RPC_);
0278           if (!exact_duplicate)
0279             res_hit->push_back(Hit_);
0280           if (!exact_duplicate)
0281             res_CPPF->push_back(Hit_.CreateCPPFDigi());
0282         }
0283 
0284         // Finished with unpacking one RPC Data Record
0285         return true;
0286 
0287       }  // End bool RPCBlockUnpacker::unpack
0288 
0289       // bool RPCBlockPacker::pack(const Block& block, UnpackerCollections *coll) {
0290       //    std::cout << "Inside RPCBlockPacker::pack" << std::endl;
0291       //    return true;
0292       // } // End bool RPCBlockPacker::pack
0293 
0294     }  // End namespace emtf
0295   }    // End namespace stage2
0296 }  // End namespace l1t
0297 
0298 DEFINE_L1T_UNPACKER(l1t::stage2::emtf::RPCBlockUnpacker);
0299 // DEFINE_L1T_PACKER(l1t::stage2::RPCBlockPacker);