Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-05 02:12:55

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