Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:26

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<16> signedTanl(const ap_uint<96> &tkword) { return ap_int<16>(tkword(63, 48)); }
0054     static ap_int<12> signedPhi(const ap_uint<96> &tkword) { return ap_int<12>(tkword(79, 68)); }
0055 
0056     //=== Floating point conversions ===
0057     /// just unpack tanl to a float
0058     float floatTanl(ap_int<16> tanl) const { return toFloat_(tanl) / (1 << 12); }
0059 
0060     /// convert track-word int tanl into float eta (at vertex) in radiants (exact)
0061     float floatEta(ap_int<16> tanl) const;
0062 
0063     /// convert track-word Rinv into float pt (almost exact)
0064     float floatPt(ap_int<15> Rinv) const;
0065 
0066     /// convert track-word int phi into float phi (at vertex) in radiants (exact)
0067     float floatPhi(ap_int<12> phi) const;
0068 
0069     /// convert track-word int z0 into float z0 in cm (exact)
0070     float floatZ0(ap_int<12> z0) const;
0071 
0072     //=== Configuration of floating point conversions
0073     void setRinvToPtFactor(float rInvToPt) { rInvToPt_ = rInvToPt; }
0074     void setPhiScale(float phiScale) { phiScale_ = phiScale; }
0075     void setZ0Scale(float z0Scale) { z0Scale_ = z0Scale; }
0076 
0077     //=== Bitwise accurate conversions ===
0078     l1ct::pt_t convPt(ap_int<15> Rinv) const;
0079 
0080     /// convert track-word int tanl into eta *at vertex* in layer 1 units
0081     l1ct::glbeta_t convEta(ap_int<16> tanl) const;
0082 
0083     /// convert track-word int phi into phi *at vertex* in layer 1 units
0084     l1ct::phi_t convPhi(ap_int<12> phi) const;
0085 
0086     l1ct::z0_t convZ0(ap_int<12> z0) const;
0087 
0088     //=== Configuration for bitwise accurate conversions ===
0089     void configPt(int lutBits);
0090 
0091     void configEta(int lutBits, int preOffs, int shift, int postOffs, bool lutSigned, bool endcap);
0092 
0093     void configPhi(int bits);
0094 
0095     void configZ0(int bits);
0096 
0097     //=== Track propagation to calo (float parametrization, no rounding) ===
0098     //
0099     // barrel DEta propagation, in layer-1 units (float parameterization, no rounding)
0100     float floatDEtaBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0101     // barrel DPhi propagation, in layer-1 units (float parameterization, no rounding)
0102     float floatDPhiBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0103 
0104     void setDEtaBarrelParams(float pZ0) { dEtaBarrelParamZ0_ = pZ0; }
0105     void setDPhiBarrelParams(float pC) { dPhiBarrelParamC_ = pC; }
0106 
0107     //=== Track propagation to calo (bitwise accurate) ===
0108     l1ct::tkdeta_t calcDEtaBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0109     l1ct::tkdphi_t calcDPhiBarrel(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0110 
0111     //=== Configuration of bitwise accurate propagation to calo ===
0112     void configDEtaBarrel(int dEtaBarrelBits, int dEtaBarrelZ0PreShift, int dEtaBarrelZ0PostShift, float offs = 0);
0113     void configDPhiBarrel(int dPhiBarrelBits, int dPhiBarrelRInvPreShift, int dPhiBarrelRInvPostShift, float offs = 0);
0114 
0115     // endcap DEta propagation, in layer-1 units (float parameterization, no rounding)
0116     float floatDEtaHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0117     // endcap DPhi propagation, in layer-1 units (float parameterization, no rounding)
0118     float floatDPhiHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0119 
0120     void setDEtaHGCalParams(float pZ0, float pRinv2C, float pRinv2ITanl1, float pRinv2ITanl2) {
0121       dEtaHGCalParamZ0_ = pZ0;
0122       dEtaHGCalParamRInv2C_ = pRinv2C;
0123       dEtaHGCalParamRInv2ITanl1_ = pRinv2ITanl1;
0124       dEtaHGCalParamRInv2ITanl2_ = pRinv2ITanl2;
0125     }
0126     void setDPhiHGCalParams(float pZ0, float pC) {
0127       dPhiHGCalParamZ0_ = pZ0;
0128       dPhiHGCalParamC_ = pC;
0129     }
0130 
0131     //=== Track propagation to calo (bitwise accurate) ===
0132     l1ct::tkdeta_t calcDEtaHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0133     l1ct::tkdphi_t calcDPhiHGCal(ap_int<12> z0, ap_int<15> Rinv, ap_int<16> tanl) const;
0134 
0135     //=== Configuration of bitwise accurate propagation to calo ===
0136     void configDEtaHGCal(int dEtaHGCalBits,
0137                          int dEtaHGCalZ0PreShift,
0138                          int dEtaHGCalRInvPreShift,
0139                          int dEtaHGCalLUTBits,
0140                          int dEtaHGCalLUTShift,
0141                          float offs = 0);
0142     void configDPhiHGCal(int dPhiHGCalBits,
0143                          int dPhiHGCalZ0PreShift,
0144                          int dPhiHGCalZ0PostShift,
0145                          int dPhiHGCalRInvShift,
0146                          int dPhiHGCalTanlInvShift,
0147                          int dPhiHGCalTanlLUTBits,
0148                          float offs = 0);
0149 
0150     /// conservative cut to select tracks that may have |eta| > 1.25 or |calo eta| > 1.25
0151     static bool mayReachHGCal(ap_int<16> tanl) { return (tanl > 6000) || (tanl < -6000); }
0152     /// conservative cut to avoid filling LUTs outside of the tracker range
0153     static bool withinTracker(ap_int<16> tanl) { return (-25000 < tanl) && (tanl < 25000); }
0154     /// conservative cut to avoid filling LUTs outside of the barrel range
0155     static bool withinBarrel(ap_int<16> tanl) { return (-13000 < tanl) && (tanl < 13000); }
0156 
0157     void setDebug(bool debug = true) { debug_ = debug; }
0158 
0159     // access to bare LUTs
0160     //const std::vector<int> &dEtaBarrelLUT() const { return dEtaBarrelLUT_; }
0161     //const std::vector<int> &dPhiBarrelTanlLUT() const { return dPhiBarrelTanlLUT_; }
0162     const std::vector<int> &dEtaHGCalLUT() const { return dEtaHGCalLUT_; }
0163     const std::vector<int> &dPhiHGCalTanlLUT() const { return dPhiHGCalTanlLUT_; }
0164     const std::vector<int> &tanlLUT() const { return tanlLUT_; }
0165     const std::vector<l1ct::pt_t> &ptLUT() const { return ptLUT_; }
0166 
0167     unsigned int countSetBits(unsigned int n) const {
0168       unsigned int count = 0;
0169       while (n) {
0170         n &= (n - 1);
0171         count++;
0172       }
0173       return count;
0174     }
0175 
0176   protected:
0177     // utilities
0178     template <int N>
0179     inline float toFloat_(ap_int<N> signedVal) const {
0180       float ret = signedVal.to_float();
0181       switch (encoding_) {
0182         case Encoding::Stepping:
0183           return (signedVal >= 0 ? ret + 0.5 : ret - 0.5);
0184         case Encoding::Biased:
0185           return ret + 0.5;
0186         default:
0187           return ret;
0188       }
0189     }
0190 
0191     /// Region for which the emulation is configured
0192     Region region_;
0193 
0194     /// Encoding used for track word inputs
0195     Encoding encoding_;
0196 
0197     /// Whether to run the bitwise accurate or floating point conversions
0198     bool bitwise_;
0199 
0200     /// Whether to unpack and populate also nstubs and various chi2 variables (needed for CompibedID in the endcap)
0201     bool slim_;
0202 
0203     /// Main constants
0204     float rInvToPt_, phiScale_, z0Scale_;
0205 
0206     /// Parameters for track propagation in floating point
0207     float dEtaBarrelParamZ0_;
0208     float dPhiBarrelParamC_;
0209 
0210     /// Parameters for track propagation in floating point
0211     float dEtaHGCalParamZ0_, dEtaHGCalParamRInv2C_, dEtaHGCalParamRInv2ITanl1_, dEtaHGCalParamRInv2ITanl2_;
0212     float dPhiHGCalParamZ0_, dPhiHGCalParamC_;
0213 
0214     // vtx phi conversion parameters
0215     int vtxPhiMult_, vtxPhiOffsPos_, vtxPhiOffsNeg_, vtxPhiBitShift_;
0216 
0217     // z0 conversion parameters
0218     int z0Mult_, z0OffsPos_, z0OffsNeg_, z0BitShift_;
0219 
0220     // deta parameters in barrel region
0221     int dEtaBarrelBits_, dEtaBarrelZ0PreShift_, dEtaBarrelZ0PostShift_, dEtaBarrelOffs_, dEtaBarrelZ0_;
0222 
0223     // dphi parameters in barrel region
0224     int dPhiBarrelBits_, dPhiBarrelRInvPreShift_, dPhiBarrelRInvPostShift_, dPhiBarrelOffs_, dPhiBarrelC_;
0225 
0226     // deta parameters in hgcal region
0227     int dEtaHGCalBits_, dEtaHGCalZ0PreShift_, dEtaHGCalZ0_, dEtaHGCalRInvPreShift_, dEtaHGCalTanlShift_,
0228         dEtaHGCalLUTShift_, dEtaHGCalTanlTermOffs_, dEtaHGCalTanlTermShift_, dEtaHGCalOffs_;
0229     std::vector<int> dEtaHGCalLUT_;
0230 
0231     // dphi parameters in hgcal region
0232     int dPhiHGCalBits_, dPhiHGCalZ0PreShift_, dPhiHGCalZ0_, dPhiHGCalZ0PostShift_, dPhiHGCalRInvShift_,
0233         dPhiHGCalTanlShift_, dPhiHGCalTanlInvShift_, dPhiHGCalPreOffs_, dPhiHGCalOffs_;
0234     std::vector<int> dPhiHGCalTanlLUT_;
0235 
0236     // tanl to eta LUT parameters
0237     int tanlLUTPreOffs_, tanlLUTShift_, tanlLUTPostOffs_;
0238     std::vector<int> tanlLUT_;
0239     bool tanlLUTSigned_;
0240 
0241     // Rinv to pR LUT parameters
0242     int ptLUTShift_;
0243     std::vector<l1ct::pt_t> ptLUT_;
0244 
0245     /// enable debug printout in some metods
0246     bool debug_;
0247   };
0248 }  // namespace l1ct
0249 
0250 #endif