Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:39

0001 
0002 #ifndef FIRMWARE_TkJetWord_h
0003 #define FIRMWARE_TkJetWord_h
0004 
0005 // Class to store the 128-bit TkJet word for L1 Track Trigger.
0006 // Author: Emily MacDonald, updated by Benjamin Radburn-Smith (September 2022)
0007 // 2nd update: George Karathanasis (Oct 2022)
0008 
0009 #include <vector>
0010 #include <ap_int.h>
0011 #include <cassert>
0012 #include <cmath>
0013 #include <bitset>
0014 #include <string>
0015 #include "DataFormats/L1TrackTrigger/interface/TTTrack_TrackWord.h"
0016 
0017 namespace l1t {
0018 
0019   class TkJetWord {
0020   public:
0021     // ----------constants, enums and typedefs ---------
0022     static constexpr double MAX_Z0 = 30.;
0023     static constexpr double MAX_ETA = 8.;
0024 
0025     enum TkJetBitWidths {
0026       kPtSize = 16,
0027       kPtMagSize = 11,
0028       kGlbEtaSize = 14,
0029       kGlbPhiSize = 13,
0030       kZ0Size = 10,
0031       kNtSize = 5,
0032       kXtSize = 4,
0033       kDispFlagSize = 1,
0034       kUnassignedSize = 65,
0035       kTkJetWordSize =
0036           kPtSize + kGlbEtaSize + kGlbPhiSize + kZ0Size + kNtSize + kXtSize + kDispFlagSize + kUnassignedSize,
0037     };
0038 
0039     enum TkJetBitLocations {
0040       kPtLSB = 0,
0041       kPtMSB = kPtLSB + TkJetBitWidths::kPtSize - 1,
0042       kGlbPhiLSB = kPtMSB + 1,
0043       kGlbPhiMSB = kGlbPhiLSB + TkJetBitWidths::kGlbPhiSize - 1,
0044       kGlbEtaLSB = kGlbPhiMSB + 1,
0045       kGlbEtaMSB = kGlbEtaLSB + TkJetBitWidths::kGlbEtaSize - 1,
0046       kZ0LSB = kGlbEtaMSB + 1,
0047       kZ0MSB = kZ0LSB + TkJetBitWidths::kZ0Size - 1,
0048       kNtLSB = kZ0MSB + 1,
0049       kNtMSB = kNtLSB + TkJetBitWidths::kNtSize - 1,
0050       kXtLSB = kNtMSB + 1,
0051       kXtMSB = kXtLSB + TkJetBitWidths::kXtSize - 1,
0052       kDispFlagLSB = kXtMSB + 1,
0053       kDispFlagMSB = kDispFlagLSB + TkJetBitWidths::kDispFlagSize - 1,
0054       kUnassignedLSB = kDispFlagMSB + 1,
0055       kUnassignedMSB = kUnassignedLSB + TkJetBitWidths::kUnassignedSize - 1,
0056     };
0057 
0058     typedef ap_ufixed<kPtSize, kPtMagSize, AP_TRN, AP_SAT> pt_t;
0059     typedef ap_int<kGlbEtaSize> glbeta_t;
0060     typedef ap_int<kGlbPhiSize> glbphi_t;
0061     typedef ap_int<kZ0Size> z0_t;   // 40cm / 0.1
0062     typedef ap_uint<kNtSize> nt_t;  //number of tracks
0063     typedef ap_uint<kXtSize> nx_t;  //number of tracks with xbit = 1
0064     typedef ap_uint<kDispFlagSize> dispflag_t;
0065     typedef ap_uint<TkJetBitWidths::kUnassignedSize> tkjetunassigned_t;  // Unassigned bits
0066     typedef std::bitset<TkJetBitWidths::kTkJetWordSize> tkjetword_bs_t;
0067     typedef ap_uint<TkJetBitWidths::kTkJetWordSize> tkjetword_t;
0068 
0069   public:
0070     // ----------Constructors --------------------------
0071     TkJetWord() {}
0072     TkJetWord(pt_t pt,
0073               glbeta_t eta,
0074               glbphi_t phi,
0075               z0_t z0,
0076               nt_t nt,
0077               nx_t nx,
0078               dispflag_t dispflag,
0079               tkjetunassigned_t unassigned);
0080 
0081     ~TkJetWord() {}
0082 
0083     // ----------copy constructor ----------------------
0084     TkJetWord(const TkJetWord& word) { tkJetWord_ = word.tkJetWord_; }
0085 
0086     // ----------operators -----------------------------
0087     TkJetWord& operator=(const TkJetWord& word) {
0088       tkJetWord_ = word.tkJetWord_;
0089       return *this;
0090     }
0091 
0092     // ----------member functions (getters) ------------
0093     // These functions return arbitarary precision words (lists of bits) for each quantity
0094     pt_t ptWord() const {
0095       pt_t ret;
0096       ret.V = tkJetWord()(TkJetBitLocations::kPtMSB, TkJetBitLocations::kPtLSB);
0097       return ret;
0098     }
0099     glbeta_t glbEtaWord() const {
0100       glbeta_t ret;
0101       ret.V = tkJetWord()(TkJetBitLocations::kGlbEtaMSB, TkJetBitLocations::kGlbEtaLSB);
0102       return ret;
0103     }
0104     glbphi_t glbPhiWord() const {
0105       glbphi_t ret;
0106       ret.V = tkJetWord()(TkJetBitLocations::kGlbPhiMSB, TkJetBitLocations::kGlbPhiLSB);
0107       return ret;
0108     }
0109     z0_t z0Word() const {
0110       z0_t ret;
0111       ret.V = tkJetWord()(TkJetBitLocations::kZ0MSB, TkJetBitLocations::kZ0LSB);
0112       return ret;
0113     }
0114     nt_t ntWord() const {
0115       nt_t ret;
0116       ret.V = tkJetWord()(TkJetBitLocations::kNtMSB, TkJetBitLocations::kNtLSB);
0117       return ret;
0118     }
0119     nx_t xtWord() const {
0120       nx_t ret;
0121       ret.V = tkJetWord()(TkJetBitLocations::kXtMSB, TkJetBitLocations::kXtLSB);
0122       return ret;
0123     }
0124     dispflag_t dispFlagWord() const {
0125       dispflag_t ret;
0126       ret.V = tkJetWord()(TkJetBitLocations::kDispFlagMSB, TkJetBitLocations::kDispFlagLSB);
0127       return ret;
0128     }
0129 
0130     tkjetunassigned_t unassignedWord() const {
0131       return tkJetWord()(TkJetBitLocations::kUnassignedMSB, TkJetBitLocations::kUnassignedLSB);
0132     }
0133     tkjetword_t tkJetWord() const { return tkjetword_t(tkJetWord_.to_string().c_str(), 2); }
0134 
0135     // These functions return the packed bits in integer format for each quantity
0136     // Signed quantities have the sign enconded in the left-most bit.
0137     unsigned int ptBits() const { return ptWord().range().to_uint(); }
0138     unsigned int glbEtaBits() const { return glbEtaWord().to_uint(); }
0139     unsigned int glbPhiBits() const { return glbPhiWord().to_uint(); }
0140     unsigned int z0Bits() const { return z0Word().range().to_uint(); }
0141     unsigned int ntBits() const { return ntWord().to_uint(); }
0142     unsigned int xtBits() const { return xtWord().to_uint(); }
0143     unsigned int dispFlagBits() const { return dispFlagWord().to_uint(); }
0144     unsigned int unassignedBits() const { return unassignedWord().to_uint(); }
0145 
0146     // These functions return the unpacked and converted values
0147     // These functions return real numbers converted from the digitized quantities by unpacking the 64-bit vertex word
0148     float pt() const { return ptWord().to_float(); }
0149     float glbeta() const {
0150       return unpackSignedValue(
0151           glbEtaWord(), TkJetBitWidths::kGlbEtaSize, (MAX_ETA) / (1 << TkJetBitWidths::kGlbEtaSize));
0152     }
0153     float glbphi() const {
0154       return unpackSignedValue(
0155           glbPhiWord(), TkJetBitWidths::kGlbPhiSize, (2. * std::abs(M_PI)) / (1 << TkJetBitWidths::kGlbPhiSize));
0156     }
0157     float z0() const {
0158       return unpackSignedValue(z0Word(), TkJetBitWidths::kZ0Size, MAX_Z0 / (1 << TkJetBitWidths::kZ0Size));
0159     }
0160     int nt() const { return (ap_ufixed<kNtSize + 2, kNtSize>(ntWord())).to_int(); }
0161     int xt() const { return (ap_ufixed<kXtSize + 2, kXtSize>(xtWord())).to_int(); }
0162     int dispflag() const { return (ap_ufixed<kDispFlagSize + 2, kDispFlagSize>(dispFlagWord())).to_int(); }
0163     unsigned int unassigned() const { return unassignedWord().to_uint(); }
0164 
0165     // ----------member functions (setters) ------------
0166     void setTkJetWord(pt_t pt,
0167                       glbeta_t eta,
0168                       glbphi_t phi,
0169                       z0_t z0,
0170                       nt_t nt,
0171                       nx_t nx,
0172                       dispflag_t dispflag,
0173                       tkjetunassigned_t unassigned);
0174 
0175   private:
0176     // ----------private member functions --------------
0177     double unpackSignedValue(unsigned int bits, unsigned int nBits, double lsb) const {
0178       int isign = 1;
0179       unsigned int digitized_maximum = (1 << nBits) - 1;
0180       if (bits & (1 << (nBits - 1))) {  // check the sign
0181         isign = -1;
0182         bits = (1 << (nBits + 1)) - bits;  // if negative, flip everything for two's complement encoding
0183       }
0184       return (double(bits & digitized_maximum) + 0.5) * lsb * isign;
0185     }
0186 
0187     // ----------member data ---------------------------
0188     tkjetword_bs_t tkJetWord_;
0189   };
0190 
0191   typedef std::vector<l1t::TkJetWord> TkJetWordCollection;
0192 
0193 }  // namespace l1t
0194 
0195 #endif