Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-09 23:33:29

0001 #ifndef EventFilter_L1TRawToDigi_Block_h
0002 #define EventFilter_L1TRawToDigi_Block_h
0003 
0004 #include <memory>
0005 #include <vector>
0006 #include <optional>
0007 
0008 #include "EventFilter/L1TRawToDigi/interface/AMCSpec.h"
0009 #include "DataFormats/L1Trigger/interface/BxBlock.h"
0010 
0011 namespace l1t {
0012   enum block_t { MP7 = 0, CTP7, MTF7 };
0013   namespace mtf7 {
0014     enum mtf7_block_t {
0015       // The "0b" prefix indicates binary; the block header id is stored in decimal.
0016       // Bits are the left-most bit (D15) of every 16-bit word in the format document.
0017       // Bottom-to-top in the document maps to left-to-right in each of the bit pattern.
0018       EvHd = 0b000111111111,  ///< Event Record Header   : block->header().getID() = 511
0019       CnBlk = 0b0010,         ///< Block of Counters     : block->header().getID() = 2
0020       ME = 0b0011,            ///< ME Data Record        : block->header().getID() = 3
0021       RPC = 0b0100,           ///< RPC Data Record       : block->header().getID() = 4
0022       GEM = 0b0111,           ///< GEM Data Record       : block->header().getID() = 7
0023       // FIXME, not currently defined... guess? JS - 01.07.20
0024       ME0 = 0b0110,        ///< ME0 Data Record       : block->header().getID() = 6
0025       SPOut = 0b01100101,  ///< SP Output Data Record : block->header().getID() = 101
0026       EvTr = 0b11111111    ///< Event Record Trailer  : block->header().getID() = 255
0027     };
0028   }
0029 
0030   class BlockHeader {
0031   public:
0032     BlockHeader(unsigned int id, unsigned int size, unsigned int capID = 0, unsigned int flags = 0, block_t type = MP7)
0033         : id_(id), size_(size), capID_(capID), flags_(flags), type_(type) {}
0034     // Create a MP7 block header: everything is contained in the raw uint32
0035     BlockHeader(const uint32_t* data)
0036         : id_((data[0] >> ID_shift) & ID_mask),
0037           size_((data[0] >> size_shift) & size_mask),
0038           capID_((data[0] >> capID_shift) & capID_mask),
0039           flags_((data[0] >> flags_shift) & flags_mask),
0040           type_(MP7) {}
0041 
0042     bool operator<(const BlockHeader& o) const { return getID() < o.getID(); };
0043 
0044     unsigned int getID() const { return id_; };
0045     unsigned int getSize() const { return size_; };
0046     unsigned int getCapID() const { return capID_; };
0047     unsigned int getFlags() const { return flags_; };
0048     block_t getType() const { return type_; };
0049 
0050     uint32_t raw() const;
0051 
0052   private:
0053     static constexpr unsigned CTP7_shift = 0;
0054     static constexpr unsigned CTP7_mask = 0xffff;
0055     static constexpr unsigned ID_shift = 24;
0056     static constexpr unsigned ID_mask = 0xff;
0057     static constexpr unsigned size_shift = 16;
0058     static constexpr unsigned size_mask = 0xff;
0059     static constexpr unsigned capID_shift = 8;
0060     static constexpr unsigned capID_mask = 0xff;
0061     static constexpr unsigned flags_shift = 0;
0062     static constexpr unsigned flags_mask = 0xff;
0063 
0064     unsigned int id_;
0065     unsigned int size_;
0066     unsigned int capID_;
0067     unsigned int flags_;
0068     block_t type_;
0069   };
0070 
0071   class Block {
0072   public:
0073     Block(const BlockHeader& h, const uint32_t* payload_start, const uint32_t* payload_end)
0074         : header_(h), payload_(payload_start, payload_end) {}
0075     Block(unsigned int id,
0076           const std::vector<uint32_t>& payload,
0077           unsigned int capID = 0,
0078           unsigned int flags = 0,
0079           block_t type = MP7)
0080         : header_(id, payload.size(), capID, flags, type), payload_(payload) {}
0081 
0082     bool operator<(const Block& o) const { return header() < o.header(); };
0083 
0084     inline unsigned int getSize() const { return payload_.size() + 1; };
0085 
0086     BlockHeader header() const { return header_; };
0087     const std::vector<uint32_t>& payload() const { return payload_; };
0088 
0089     void amc(const amc::Header& h) { amc_ = h; };
0090     amc::Header amc() const { return amc_; };
0091 
0092     BxBlocks getBxBlocks(unsigned int payloadWordsPerBx, bool bxHeader) const;
0093 
0094   private:
0095     BlockHeader header_;
0096     amc::Header amc_;
0097     std::vector<uint32_t> payload_;
0098   };
0099 
0100   typedef std::vector<Block> Blocks;
0101 
0102   class Payload {
0103   public:
0104     Payload(const uint32_t* data, const uint32_t* end) : data_(data), end_(end), algo_(0), infra_(0) {}
0105     virtual ~Payload() {}
0106     virtual unsigned getAlgorithmFWVersion() const { return algo_; };
0107     virtual unsigned getInfrastructureFWVersion() const { return infra_; };
0108     virtual unsigned getHeaderSize() const = 0;
0109     // Read header from data_ and advance data_ to point behind the
0110     // header.  Called by getBlock(), which also checks that data_ !=
0111     // end_ before calling (assumes size of one 32 bit word).
0112     virtual BlockHeader getHeader() = 0;
0113     virtual std::optional<Block> getBlock();
0114 
0115   protected:
0116     const uint32_t* data_;
0117     const uint32_t* end_;
0118 
0119     unsigned algo_;
0120     unsigned infra_;
0121   };
0122 
0123   class MP7Payload : public Payload {
0124   public:
0125     MP7Payload(const uint32_t* data, const uint32_t* end, bool legacy_mc = false);
0126     unsigned getHeaderSize() const override { return 1; };
0127     BlockHeader getHeader() override;
0128   };
0129 
0130   class MTF7Payload : public Payload {
0131   public:
0132     MTF7Payload(const uint32_t* data, const uint32_t* end);
0133     // Unused methods - we override getBlock() instead
0134     unsigned getHeaderSize() const override { return 0; };
0135     BlockHeader getHeader() override { return BlockHeader(nullptr); };
0136     std::optional<Block> getBlock() override;
0137 
0138   private:
0139     // sizes in 16 bit words
0140     static constexpr unsigned header_size = 12;
0141     static constexpr unsigned counter_size = 4;
0142     static constexpr unsigned trailer_size = 8;
0143 
0144     /// Start of the EMTF DAQ payload, in number of 64-bit words
0145     static constexpr unsigned DAQ_PAYLOAD_OFFSET = 4;
0146     /// Maximum number of BX per MTF7 payload
0147     static constexpr unsigned MAX_BX_PER_PAYLOAD = 8;
0148     /// Maximum number of CSC words per MTF7 payload per bx: 9 links/sectors, 6 stations, 2 LCTs
0149     static constexpr unsigned ME_MAX_PER_BX = 108;
0150     /// Maximum number of RPC words per MTF7 payload per bx: 7 links/sectors, 6 stations, 2 segments
0151     static constexpr unsigned RPC_MAX_PER_BX = 84;
0152     /// Maximum number of GE1/1 words per MTF7 payload per bx: 7 GE1/1 links, 2 layers, 8 clusters
0153     static constexpr unsigned GE11_MAX_PER_BX = 112;
0154     /// TODO: Maximum number of GE2/1 words per MTF7 payload per bx: ?? GE2/1 links, 2 layers, ?? clusters
0155     static constexpr unsigned GE21_MAX_PER_BX = 0;
0156     /// TODO: Maximum number of ME0 words per MTF7 payload per bx: ?? ME0 links, ?? layers, ?? clusters
0157     static constexpr unsigned ME0_MAX_PER_BX = 0;
0158     /// Maximum number of SPz words per MTF7 payload per bx: 3 tracks, 2 words per track
0159     static constexpr unsigned SP_MAX_PER_BX = 6;
0160     /// Maximum number of 64-bit words in the EMTF payload
0161     static constexpr unsigned PAYLOAD_MAX_SIZE =
0162         MAX_BX_PER_PAYLOAD *
0163             (ME_MAX_PER_BX + RPC_MAX_PER_BX + GE11_MAX_PER_BX + GE21_MAX_PER_BX + ME0_MAX_PER_BX + SP_MAX_PER_BX) +
0164         (trailer_size / 4);
0165 
0166     static constexpr unsigned max_block_length_ = 3;         ///< maximum of the block length (64bits)
0167     static const std::vector<unsigned int> block_patterns_;  ///< bit patterns of the first bits (of 16bit words)
0168 
0169     int count(unsigned int pattern, unsigned int length) const;
0170     bool valid(unsigned int pattern) const;
0171   };
0172 
0173   class CTP7Payload : public Payload {
0174   public:
0175     CTP7Payload(const uint32_t* data, const uint32_t* end, amc::Header amcHeader);
0176     unsigned getHeaderSize() const override { return 2; };
0177     BlockHeader getHeader() override;
0178     std::optional<Block> getBlock() override;
0179 
0180   private:
0181     // FIXME check values
0182     static constexpr unsigned size_mask = 0xff;
0183     static constexpr unsigned size_shift = 16;
0184 
0185     unsigned size_;
0186     unsigned capId_;
0187     unsigned bx_per_l1a_;
0188     unsigned calo_bxid_;
0189     unsigned six_hcal_feature_bits_;
0190     unsigned slot7_card_;
0191     amc::Header amcHeader_;
0192   };
0193 }  // namespace l1t
0194 
0195 #endif