Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-04-30 22:24:27

0001 #ifndef L1Trigger_Phase2L1ParticleFlow_l1converters_tracks_tkinput_ref_h
0002 #define L1Trigger_Phase2L1ParticleFlow_l1converters_tracks_tkinput_ref_h
0003 
0004 #include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
0005 #include <cstdio>
0006 #include <algorithm>
0007 
0008 namespace edm {
0009   class ParameterSet;
0010   class ParameterSetDescription;
0011 }  // namespace edm
0012 
0013 namespace l1ct {
0014   class TrackInputEmulator {
0015   public:
0016     enum class Region { Barrel, Endcap, Any };  // but only Endcap supported for now
0017 
0018     /// encoding used in the digitized track word
0019     enum class Encoding {
0020       Stepping,  // J = sign(F) * floor(abs(F)/LSB);  F = sign(J) * ( abs(J) + 0.5 ) * LSB
0021       Biased,    // J = floor(F/LSB);  F = (J + 0.5)*LSB
0022       Unbiased   // J = round(F/LSB);  F = J * LSB
0023     };
0024 
0025     TrackInputEmulator(const edm::ParameterSet &iConfig);
0026     TrackInputEmulator(Region = Region::Endcap,
0027                        Encoding encoding = Encoding::Stepping,
0028                        bool bitwise = true,
0029                        bool slim = true);
0030 
0031     static edm::ParameterSetDescription getParameterSetDescription();
0032 
0033     std::pair<l1ct::TkObjEmu, bool> decodeTrack(ap_uint<96> tkword, const l1ct::PFRegionEmu &sector) const {
0034       return decodeTrack(tkword, sector, bitwise_, slim_);
0035     }
0036     std::pair<l1ct::TkObjEmu, bool> decodeTrack(ap_uint<96> tkword,
0037                                                 const l1ct::PFRegionEmu &sector,
0038                                                 bool bitwise) const {
0039       return decodeTrack(tkword, sector, bitwise, slim_);
0040     }
0041 
0042     std::pair<l1ct::TkObjEmu, bool> decodeTrack(ap_uint<96> tkword,
0043                                                 const l1ct::PFRegionEmu &sector,
0044                                                 bool bitwise,
0045                                                 bool slim) const;
0046 
0047     //== Unpackers ==
0048     static bool valid(const ap_uint<96> &tkword) { return tkword[95]; }
0049     static bool charge(const ap_uint<96> &tkword) { return !tkword[94]; }
0050 
0051     static ap_int<15> signedRinv(const ap_uint<96> &tkword) { return ap_int<15>(tkword(94, 80)); }
0052     static ap_int<12> signedZ0(const ap_uint<96> &tkword) { return ap_int<12>(tkword(47, 36)); }
0053     static ap_int<13> signedDxy(const ap_uint<96> &tkword) { return ap_int<13>(tkword(31, 19)); }
0054     static ap_int<16> signedTanl(const ap_uint<96> &tkword) { return ap_int<16>(tkword(63, 48)); }
0055     static ap_int<12> signedPhi(const ap_uint<96> &tkword) { return ap_int<12>(tkword(79, 68)); }
0056 
0057     //=== Floating point conversions ===
0058     /// just unpack tanl to a float
0059     float floatTanl(ap_int<16> tanl) const { return toFloat_(tanl) / (1 << 12); }
0060 
0061     /// convert track-word int tanl into float eta (at vertex) in radiants (exact)
0062     float floatEta(ap_int<16> tanl) const;
0063 
0064     /// convert track-word Rinv into float pt (almost exact)
0065     float floatPt(ap_int<15> Rinv) const;
0066 
0067     /// convert track-word int phi into float phi (at vertex) in radiants (exact)
0068     float floatPhi(ap_int<12> phi) const;
0069 
0070     /// convert track-word int z0 into float z0 in cm (exact)
0071     float floatZ0(ap_int<12> z0) const;
0072 
0073     /// convert track-word int dxy into float dxy in cm (exact)
0074     float floatDxy(ap_int<13> dxy) const;
0075 
0076     //=== Configuration of floating point conversions
0077     void setRinvToPtFactor(float rInvToPt) { rInvToPt_ = rInvToPt; }
0078     void setPhiScale(float phiScale) { phiScale_ = phiScale; }
0079     void setZ0Scale(float z0Scale) { z0Scale_ = z0Scale; }
0080     void setDxyScale(float dxyScale) { dxyScale_ = dxyScale; }
0081 
0082     //=== Bitwise accurate conversions ===
0083     l1ct::pt_t convPt(ap_int<15> Rinv) const;
0084 
0085     /// convert track-word int tanl into eta *at vertex* in layer 1 units
0086     l1ct::glbeta_t convEta(ap_int<16> tanl) const;
0087 
0088     /// convert track-word int phi into phi *at vertex* in layer 1 units
0089     l1ct::phi_t convPhi(ap_int<12> phi) const;
0090 
0091     l1ct::z0_t convZ0(ap_int<12> z0) const;
0092 
0093     l1ct::dxy_t convDxy(ap_int<13> dxy) const;
0094 
0095     //=== Configuration for bitwise accurate conversions ===
0096     void configPt(int lutBits);
0097 
0098     void configEta(int lutBits, int preOffs, int shift, int postOffs, bool lutSigned, bool endcap);
0099 
0100     void configPhi(int bits);
0101 
0102     void configZ0(int bits);
0103 
0104     void configDxy(int bits);
0105 
0106     //=== Track propagation to calo (float parametrization, no rounding) ===
0107     //
0108     // barrel DEta propagation, in layer-1 units (float parameterization, no rounding)
0109     float floatDEtaBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0110     // barrel DPhi propagation, in layer-1 units (float parameterization, no rounding)
0111     float floatDPhiBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0112 
0113     void setDEtaBarrelParams(float pZ0) { dEtaBarrelParamZ0_ = pZ0; }
0114     void setDPhiBarrelParams(float pC) { dPhiBarrelParamC_ = pC; }
0115 
0116     //=== Track propagation to calo (bitwise accurate) ===
0117     l1ct::tkdeta_t calcDEtaBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0118     l1ct::tkdphi_t calcDPhiBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0119 
0120     //=== Configuration of bitwise accurate propagation to calo ===
0121     void configDEtaBarrel(int dEtaBarrelBits, int dEtaBarrelZ0PreShift, int dEtaBarrelZ0PostShift, float offs = 0);
0122     void configDPhiBarrel(int dPhiBarrelBits, int dPhiBarrelRInvPreShift, int dPhiBarrelRInvPostShift, float offs = 0);
0123 
0124     // endcap DEta propagation, in layer-1 units (float parameterization, no rounding)
0125     float floatDEtaHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0126     // endcap DPhi propagation, in layer-1 units (float parameterization, no rounding)
0127     float floatDPhiHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0128 
0129     void setDEtaHGCalParams(float pZ0, float pRinv2C, float pRinv2ITanl1, float pRinv2ITanl2) {
0130       dEtaHGCalParamZ0_ = pZ0;
0131       dEtaHGCalParamRInv2C_ = pRinv2C;
0132       dEtaHGCalParamRInv2ITanl1_ = pRinv2ITanl1;
0133       dEtaHGCalParamRInv2ITanl2_ = pRinv2ITanl2;
0134     }
0135     void setDPhiHGCalParams(float pZ0, float pC) {
0136       dPhiHGCalParamZ0_ = pZ0;
0137       dPhiHGCalParamC_ = pC;
0138     }
0139 
0140     //=== Track propagation to calo (bitwise accurate) ===
0141     l1ct::tkdeta_t calcDEtaHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0142     l1ct::tkdphi_t calcDPhiHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0143 
0144     //=== Configuration of bitwise accurate propagation to calo ===
0145     void configDEtaHGCal(int dEtaHGCalBits,
0146                          int dEtaHGCalZ0PreShift,
0147                          int dEtaHGCalRInvPreShift,
0148                          int dEtaHGCalLUTBits,
0149                          int dEtaHGCalLUTShift,
0150                          float offs = 0);
0151     void configDPhiHGCal(int dPhiHGCalBits,
0152                          int dPhiHGCalZ0PreShift,
0153                          int dPhiHGCalZ0PostShift,
0154                          int dPhiHGCalRInvShift,
0155                          int dPhiHGCalTanlInvShift,
0156                          int dPhiHGCalTanlLUTBits,
0157                          float offs = 0);
0158 
0159     /// conservative cut to select tracks that may have |eta| > 1.25 or |calo eta| > 1.25
0160     static bool mayReachHGCal(ap_int<16> tanl) { return (tanl > 6000) || (tanl < -6000); }
0161     /// conservative cut to avoid filling LUTs outside of the tracker range
0162     static bool withinTracker(ap_int<16> tanl) { return (-25000 < tanl) && (tanl < 25000); }
0163     /// conservative cut to avoid filling LUTs outside of the barrel range
0164     static bool withinBarrel(ap_int<16> tanl) { return (-13000 < tanl) && (tanl < 13000); }
0165 
0166     void setDebug(bool debug = true) { debug_ = debug; }
0167 
0168     // access to bare LUTs
0169     //const std::vector<int> &dEtaBarrelLUT() const { return dEtaBarrelLUT_; }
0170     //const std::vector<int> &dPhiBarrelTanlLUT() const { return dPhiBarrelTanlLUT_; }
0171     const std::vector<int> &dEtaHGCalLUT() const { return dEtaHGCalLUT_; }
0172     const std::vector<int> &dPhiHGCalTanlLUT() const { return dPhiHGCalTanlLUT_; }
0173     const std::vector<int> &tanlLUT() const { return tanlLUT_; }
0174     const std::vector<l1ct::pt_t> &ptLUT() const { return ptLUT_; }
0175 
0176     unsigned int countSetBits(unsigned int n) const {
0177       unsigned int count = 0;
0178       while (n) {
0179         n &= (n - 1);
0180         count++;
0181       }
0182       return count;
0183     }
0184 
0185   protected:
0186     // utilities
0187     template <int N>
0188     inline float toFloat_(ap_int<N> signedVal) const {
0189       float ret = signedVal.to_float();
0190       switch (encoding_) {
0191         case Encoding::Stepping:
0192           return (signedVal >= 0 ? ret + 0.5 : ret - 0.5);
0193         case Encoding::Biased:
0194           return ret + 0.5;
0195         default:
0196           return ret;
0197       }
0198     }
0199 
0200     /// Region for which the emulation is configured
0201     Region region_;
0202 
0203     /// Encoding used for track word inputs
0204     Encoding encoding_;
0205 
0206     /// Whether to run the bitwise accurate or floating point conversions
0207     bool bitwise_;
0208 
0209     /// Whether to unpack and populate also nstubs and various chi2 variables (needed for CompibedID in the endcap)
0210     bool slim_;
0211 
0212     /// Main constants
0213     float rInvToPt_, phiScale_, z0Scale_, dxyScale_;
0214 
0215     /// Parameters for track propagation in floating point
0216     float dEtaBarrelParamZ0_;
0217     float dPhiBarrelParamC_;
0218 
0219     /// Parameters for track propagation in floating point
0220     float dEtaHGCalParamZ0_, dEtaHGCalParamRInv2C_, dEtaHGCalParamRInv2ITanl1_, dEtaHGCalParamRInv2ITanl2_;
0221     float dPhiHGCalParamZ0_, dPhiHGCalParamC_;
0222 
0223     // vtx phi conversion parameters
0224     int vtxPhiMult_, vtxPhiOffsPos_, vtxPhiOffsNeg_, vtxPhiBitShift_;
0225 
0226     // z0 conversion parameters
0227     int z0Mult_, z0OffsPos_, z0OffsNeg_, z0BitShift_;
0228 
0229     // dxy conversion parameters
0230     int dxyMult_, dxyOffsPos_, dxyOffsNeg_, dxyBitShift_;
0231 
0232     // deta parameters in barrel region
0233     int dEtaBarrelBits_, dEtaBarrelZ0PreShift_, dEtaBarrelZ0PostShift_, dEtaBarrelOffs_, dEtaBarrelZ0_;
0234 
0235     // dphi parameters in barrel region
0236     int dPhiBarrelBits_, dPhiBarrelRInvPreShift_, dPhiBarrelRInvPostShift_, dPhiBarrelOffs_, dPhiBarrelC_;
0237 
0238     // deta parameters in hgcal region
0239     int dEtaHGCalBits_, dEtaHGCalZ0PreShift_, dEtaHGCalZ0_, dEtaHGCalRInvPreShift_, dEtaHGCalTanlShift_,
0240         dEtaHGCalLUTShift_, dEtaHGCalTanlTermOffs_, dEtaHGCalTanlTermShift_, dEtaHGCalOffs_;
0241     std::vector<int> dEtaHGCalLUT_;
0242 
0243     // dphi parameters in hgcal region
0244     int dPhiHGCalBits_, dPhiHGCalZ0PreShift_, dPhiHGCalZ0_, dPhiHGCalZ0PostShift_, dPhiHGCalRInvShift_,
0245         dPhiHGCalTanlShift_, dPhiHGCalTanlInvShift_, dPhiHGCalPreOffs_, dPhiHGCalOffs_;
0246     std::vector<int> dPhiHGCalTanlLUT_;
0247 
0248     // tanl to eta LUT parameters
0249     int tanlLUTPreOffs_, tanlLUTShift_, tanlLUTPostOffs_;
0250     std::vector<int> tanlLUT_;
0251     bool tanlLUTSigned_;
0252 
0253     // Rinv to pR LUT parameters
0254     int ptLUTShift_;
0255     std::vector<l1ct::pt_t> ptLUT_;
0256 
0257     /// enable debug printout in some metods
0258     bool debug_;
0259   };
0260 }  // namespace l1ct
0261 
0262 #endif