Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // Code to unpack the AMC13 header, "AMC data header", and "Event Record Header"
0002 
0003 #include "EventFilter/L1TRawToDigi/plugins/UnpackerFactory.h"
0004 
0005 #include "EMTFCollections.h"
0006 #include "EMTFUnpackerTools.h"
0007 
0008 // This is the "header" - no EMTFBlockHeaders.h file is needed
0009 namespace l1t {
0010   namespace stage2 {
0011     namespace emtf {
0012 
0013       class HeadersBlockUnpacker : public Unpacker {  // "HeadersBlockUnpacker" inherits from "Unpacker"
0014       public:
0015         virtual int checkFormat(const Block& block);
0016         bool unpack(const Block& block,
0017                     UnpackerCollections* coll) override;  // Apparently it's always good to use override in C++
0018         // virtual bool packBlock(const Block& block, UnpackerCollections *coll) override;
0019       };
0020 
0021       // class HeadersBlockPacker : public Packer { // "HeadersBlockPacker" inherits from "Packer"
0022       // public:
0023       //    virtual bool unpack(const Block& block, UnpackerCollections *coll) override; // Apparently it's always good to use override in C++
0024       // };
0025 
0026     }  // namespace emtf
0027   }    // namespace stage2
0028 }  // namespace l1t
0029 
0030 namespace l1t {
0031   namespace stage2 {
0032     namespace emtf {
0033 
0034       int HeadersBlockUnpacker::checkFormat(const Block& block) {
0035         auto payload = block.payload();
0036         int errors = 0;
0037 
0038         //Check the number of 16-bit words
0039         if (payload.size() != 12) {
0040           errors += 1;
0041           edm::LogError("L1T|EMTF") << "Payload size in 'AMC Data Header' is different than expected";
0042         }
0043 
0044         //Check that each word is 16 bits
0045         if (GetHexBits(payload[0], 16, 31) != 0) {
0046           errors += 1;
0047           edm::LogError("L1T|EMTF") << "Payload[0] has more than 16 bits in 'AMC Data Header'";
0048         }
0049         if (GetHexBits(payload[1], 16, 31) != 0) {
0050           errors += 1;
0051           edm::LogError("L1T|EMTF") << "Payload[1] has more than 16 bits in 'AMC Data Header'";
0052         }
0053         if (GetHexBits(payload[2], 16, 31) != 0) {
0054           errors += 1;
0055           edm::LogError("L1T|EMTF") << "Payload[2] has more than 16 bits in 'AMC Data Header'";
0056         }
0057         if (GetHexBits(payload[3], 16, 31) != 0) {
0058           errors += 1;
0059           edm::LogError("L1T|EMTF") << "Payload[3] has more than 16 bits in 'AMC Data Header'";
0060         }
0061         if (GetHexBits(payload[4], 16, 31) != 0) {
0062           errors += 1;
0063           edm::LogError("L1T|EMTF") << "Payload[4] has more than 16 bits in 'AMC Data Header'";
0064         }
0065         if (GetHexBits(payload[5], 16, 31) != 0) {
0066           errors += 1;
0067           edm::LogError("L1T|EMTF") << "Payload[5] has more than 16 bits in 'AMC Data Header'";
0068         }
0069         if (GetHexBits(payload[6], 16, 31) != 0) {
0070           errors += 1;
0071           edm::LogError("L1T|EMTF") << "Payload[6] has more than 16 bits in 'AMC Data Header'";
0072         }
0073         if (GetHexBits(payload[7], 16, 31) != 0) {
0074           errors += 1;
0075           edm::LogError("L1T|EMTF") << "Payload[7] has more than 16 bits in 'AMC Data Header'";
0076         }
0077         if (GetHexBits(payload[8], 16, 31) != 0) {
0078           errors += 1;
0079           edm::LogError("L1T|EMTF") << "Payload[8] has more than 16 bits in 'AMC Data Header'";
0080         }
0081         if (GetHexBits(payload[9], 16, 31) != 0) {
0082           errors += 1;
0083           edm::LogError("L1T|EMTF") << "Payload[9] has more than 16 bits in 'AMC Data Header'";
0084         }
0085         if (GetHexBits(payload[10], 16, 31) != 0) {
0086           errors += 1;
0087           edm::LogError("L1T|EMTF") << "Payload[10] has more than 16 bits in 'AMC Data Header'";
0088         }
0089         if (GetHexBits(payload[11], 16, 31) != 0) {
0090           errors += 1;
0091           edm::LogError("L1T|EMTF") << "Payload[11] has more than 16 bits in 'AMC Data Header'";
0092         }
0093 
0094         uint16_t HD1a = payload[0];
0095         uint16_t HD1b = payload[1];
0096         uint16_t HD1c = payload[2];
0097         uint16_t HD1d = payload[3];
0098         uint16_t HD2a = payload[4];
0099         uint16_t HD2b = payload[5];
0100         uint16_t HD2c = payload[6];
0101         uint16_t HD2d = payload[7];
0102         uint16_t HD3a = payload[8];
0103         uint16_t HD3b = payload[9];
0104         uint16_t HD3c = payload[10];
0105         uint16_t HD3d = payload[11];
0106 
0107         //Check Format
0108         if (GetHexBits(HD1a, 12, 15) != 9) {
0109           errors += 1;
0110           edm::LogError("L1T|EMTF") << "Format identifier bits in HD1a are incorrect";
0111         }
0112         if (GetHexBits(HD1b, 12, 15) != 9) {
0113           errors += 1;
0114           edm::LogError("L1T|EMTF") << "Format identifier bits in HD1b are incorrect";
0115         }
0116         if (GetHexBits(HD1c, 12, 15) != 9) {
0117           errors += 1;
0118           edm::LogError("L1T|EMTF") << "Format identifier bits in HD1c are incorrect";
0119         }
0120         if (GetHexBits(HD1d, 12, 15) != 9) {
0121           errors += 1;
0122           edm::LogError("L1T|EMTF") << "Format identifier bits in HD1d are incorrect";
0123         }
0124         if (GetHexBits(HD2a, 12, 15) != 10) {
0125           errors += 1;
0126           edm::LogError("L1T|EMTF") << "Format identifier bits in HD2a are incorrect";
0127         }
0128         if (GetHexBits(HD2b, 12, 15) != 10) {
0129           errors += 1;
0130           edm::LogError("L1T|EMTF") << "Format identifier bits in HD2b are incorrect";
0131         }
0132         if (GetHexBits(HD2c, 12, 15) != 10) {
0133           errors += 1;
0134           edm::LogError("L1T|EMTF") << "Format identifier bits in HD2c are incorrect";
0135         }
0136         if (GetHexBits(HD2d, 12, 15) != 10) {
0137           errors += 1;
0138           edm::LogError("L1T|EMTF") << "Format identifier bits in HD2d are incorrect";
0139         }
0140         if (GetHexBits(HD3a, 15, 15) != 1) {
0141           errors += 1;
0142           edm::LogError("L1T|EMTF") << "Format identifier bits in HD3a are incorrect";
0143         }
0144         if (GetHexBits(HD3b, 15, 15) != 0) {
0145           errors += 1;
0146           edm::LogError("L1T|EMTF") << "Format identifier bits in HD3b are incorrect";
0147         }
0148         if (GetHexBits(HD3c, 15, 15) != 0) {
0149           errors += 1;
0150           edm::LogError("L1T|EMTF") << "Format identifier bits in HD3c are incorrect";
0151         }
0152         if (GetHexBits(HD3d, 15, 15) != 0) {
0153           errors += 1;
0154           edm::LogError("L1T|EMTF") << "Format identifier bits in HD3d are incorrect";
0155         }
0156 
0157         return errors;
0158       }
0159 
0160       bool HeadersBlockUnpacker::unpack(const Block& block, UnpackerCollections* coll) {
0161         // std::cout << "Inside EMTFBlockHeaders.cc: unpack" << std::endl;
0162 
0163         // Get the payload for this block, made up of 16-bit words (0xffff)
0164         // Format defined in MTF7Payload::getBlock() in src/Block.cc
0165         // payload[0] = bits 0-15, payload[1] = 16-31, payload[3] = 32-47, etc.
0166         auto payload = block.payload();
0167 
0168         // Check Format of Payload
0169         l1t::emtf::AMC13Header AMC13Header_;
0170         l1t::emtf::MTF7Header MTF7Header_;
0171         l1t::emtf::EventHeader EventHeader_;
0172         for (int err = 0; err < checkFormat(block); err++)
0173           EventHeader_.add_format_error();
0174 
0175         // Assign payload to 16-bit words
0176         uint16_t HD1a = payload[0];
0177         uint16_t HD1b = payload[1];
0178         // uint16_t HD1c = payload[2];
0179         uint16_t HD1d = payload[3];
0180         uint16_t HD2a = payload[4];
0181         uint16_t HD2b = payload[5];
0182         uint16_t HD2c = payload[6];
0183         uint16_t HD2d = payload[7];
0184         uint16_t HD3a = payload[8];
0185         uint16_t HD3b = payload[9];
0186         uint16_t HD3c = payload[10];
0187         uint16_t HD3d = payload[11];
0188 
0189         // res is a pointer to a collection of EMTFDaqOut class objects
0190         // There is one EMTFDaqOut for each MTF7 (60 deg. sector) in the event
0191         EMTFDaqOutCollection* res;
0192         res = static_cast<EMTFCollections*>(coll)->getEMTFDaqOuts();
0193 
0194         EMTFDaqOut EMTFDaqOut_;
0195         res->push_back(EMTFDaqOut_);
0196         int iOut = res->size() - 1;
0197 
0198         //////////////////////////////////////
0199         // Unpack the AMC13 header information
0200         //////////////////////////////////////
0201 
0202         if ((res->at(iOut)).HasAMC13Header() == true) {
0203           (res->at(iOut)).add_format_error();
0204           edm::LogError("L1T|EMTF") << "Why is there already an AMC13Header object?";
0205           goto write_AMC13;
0206         }
0207 
0208         // TODO: Write functions in interface/AMC13Spec.h (as in AMCSpec.h) to extract all AMC13 header and trailer info
0209         // TODO: Edit interface/Block.h to have a amc13() function similar to amc()
0210 
0211         // AMC13Header_.set_orn( block.amc13().get() )          {  orn = bits; };
0212         // AMC13Header_.set_lv1_id( block.amc13().get() )       {  lv1_id = bits; };
0213         // AMC13Header_.set_bx_id( block.amc13().get() )        {  bx_id = bits; };
0214         // AMC13Header_.set_source_id( block.amc13().get() )    {  source_id = bits; };
0215         // AMC13Header_.set_evt_ty( block.amc13().get() )       {  evt_ty = bits; };
0216         // AMC13Header_.set_fov( block.amc13().get() )          {  fov = bits; };
0217         // AMC13Header_.set_ufov( block.amc13().get() )         {  ufov = bits; };
0218         // AMC13Header_.set_res( block.amc13().get() )          {  res = bits; };
0219         // AMC13Header_.set_namc( block.amc13().get() )         {  namc = bits; };
0220         // AMC13Header_.set_h( block.amc13().get() )            {  h = bits; };
0221         // AMC13Header_.set_x( block.amc13().get() )            {  x = bits; };
0222         // AMC13Header_.set_dataword(uint64_t bits)  { dataword = bits; };
0223 
0224       write_AMC13:
0225 
0226         (res->at(iOut)).set_AMC13Header(AMC13Header_);
0227 
0228         /////////////////////////////////////
0229         // Unpack the MTF7 header information
0230         /////////////////////////////////////
0231 
0232         if ((res->at(iOut)).HasMTF7Header() == true) {
0233           (res->at(iOut)).add_format_error();
0234           edm::LogError("L1T|EMTF") << "Why is there already an MTF7Header object?";
0235           goto write_MTF7;
0236         }
0237 
0238         // AMC header info defined in interface/AMCSpec.h
0239         MTF7Header_.set_amc_number(block.amc().getAMCNumber());
0240         MTF7Header_.set_bx_id(block.amc().getBX());
0241         MTF7Header_.set_orbit_number(block.amc().getOrbitNumber());
0242         MTF7Header_.set_board_id(block.amc().getBoardID());
0243         MTF7Header_.set_lv1_id(block.amc().getLV1ID());
0244         MTF7Header_.set_data_length(block.amc().getSize());
0245         MTF7Header_.set_user_id(block.amc().getUserData());
0246         // MTF7Header_.set_dataword(uint64_t bits)  { dataword = bits;    };
0247 
0248       write_MTF7:
0249 
0250         (res->at(iOut)).set_MTF7Header(MTF7Header_);
0251 
0252         /////////////////////////////////////////////
0253         // Unpack the Event Record header information
0254         /////////////////////////////////////////////
0255 
0256         if ((res->at(iOut)).HasEventHeader() == true) {
0257           (res->at(iOut)).add_format_error();
0258           edm::LogError("L1T|EMTF") << "Why is there already an EventHeader object?";
0259           goto write_Event;
0260         }
0261         if (EventHeader_.Format_errors() > 0)
0262           goto write_Event;
0263 
0264         EventHeader_.set_l1a(GetHexBits(HD1a, 0, 11, HD1b, 0, 11));
0265         EventHeader_.set_l1a_BXN(GetHexBits(HD1d, 0, 11));
0266         EventHeader_.set_sp_TS(GetHexBits(HD2b, 8, 11));
0267         EventHeader_.set_endcap(GetHexBits(HD2b, 11, 11) ? -1 : 1);
0268         EventHeader_.set_sector(GetHexBits(HD2b, 8, 10) + 1);
0269         EventHeader_.set_sp_ersv(GetHexBits(HD2b, 5, 7));
0270         EventHeader_.set_sp_addr(GetHexBits(HD2b, 0, 4));
0271         EventHeader_.set_tbin(GetHexBits(HD2c, 8, 10));
0272         EventHeader_.set_ddm(GetHexBits(HD2c, 7, 7));
0273         EventHeader_.set_spa(GetHexBits(HD2c, 6, 6));
0274         EventHeader_.set_rpca(GetHexBits(HD2c, 5, 5));
0275         EventHeader_.set_skip(GetHexBits(HD2c, 4, 4));
0276         EventHeader_.set_rdy(GetHexBits(HD2c, 3, 3));
0277         EventHeader_.set_bsy(GetHexBits(HD2c, 2, 2));
0278         EventHeader_.set_osy(GetHexBits(HD2c, 1, 1));
0279         EventHeader_.set_wof(GetHexBits(HD2c, 0, 0));
0280         EventHeader_.set_me1a(GetHexBits(HD2d, 0, 11));
0281         EventHeader_.set_me1b(GetHexBits(HD3a, 0, 8));
0282         EventHeader_.set_me2(GetHexBits(HD3b, 0, 10));
0283         EventHeader_.set_me3(GetHexBits(HD3c, 0, 10));
0284         EventHeader_.set_me4(GetHexBits(HD3d, 0, 10));
0285         EventHeader_.set_cppf(GetHexBits(HD3a, 11, 14, HD3b, 11, 13));
0286         EventHeader_.set_cppf_crc(GetHexBits(HD3c, 11, 14, HD3d, 11, 13));
0287         EventHeader_.set_gem(GetHexBits(HD2a, 0, 6));
0288         EventHeader_.set_gem_crc(GetHexBits(HD2a, 7, 11, HD3a, 9, 10));
0289         // EventHeader_.set_dataword(uint64_t bits)  { dataword = bits;  };
0290 
0291       write_Event:
0292 
0293         (res->at(iOut)).set_EventHeader(EventHeader_);
0294 
0295         // Finished with unpacking headers
0296         return true;
0297 
0298       }  // End bool HeadersBlockUnpacker::unpack
0299 
0300       // bool HeadersBlockPacker::pack(const Block& block, UnpackerCollections *coll) {
0301       //    std::cout << "Inside HeadersBlockPacker::pack" << std::endl;
0302       //    return true;
0303       // } // End bool HeadersBlockPacker::pack
0304 
0305     }  // End namespace emtf
0306   }    // End namespace stage2
0307 }  // End namespace l1t
0308 
0309 DEFINE_L1T_UNPACKER(l1t::stage2::emtf::HeadersBlockUnpacker);
0310 // DEFINE_L1T_PACKER(l1t::stage2::HeadersBlockPacker);