Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-26 02:40:30

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