Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-16 02:43:03

0001 // ===========================================================================
0002 //
0003 //       Filename:  Isolation.h
0004 //
0005 //    Description:
0006 //
0007 //        Version:  1.0
0008 //        Created:  02/23/2021 01:16:43 PM
0009 //       Revision:  none
0010 //       Compiler:  g++
0011 //
0012 //         Author:  Zhenbin Wu, zhenbin.wu@gmail.com
0013 //
0014 // ===========================================================================
0015 
0016 #ifndef PHASE2GMT_ISOLATION
0017 #define PHASE2GMT_ISOLATION
0018 
0019 #include "TopologicalAlgorithm.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 #include <atomic>
0023 
0024 namespace Phase2L1GMT {
0025   class Isolation : public TopoAlgo {
0026   public:
0027     Isolation(const edm::ParameterSet &iConfig);
0028     ~Isolation();
0029     Isolation(const Isolation &cpy);
0030 
0031     unsigned compute_trk_iso(l1t::TrackerMuon &in_mu, ConvertedTTTrack &in_trk);
0032 
0033     void isolation_allmu_alltrk(std::vector<l1t::TrackerMuon> &trkMus, std::vector<ConvertedTTTrack> &convertedTracks);
0034 
0035   private:
0036     void DumpOutputs(std::vector<l1t::TrackerMuon> &trkMus);
0037     int SetAbsIsolationBits(int accum);
0038     int SetRelIsolationBits(int accum, int mupt);
0039     int OverlapRemoval(unsigned &ovrl, std::vector<unsigned> &overlaps);
0040 
0041     const static int c_iso_dangle_max = 260;  //@ <  260 x 2pi/2^13 = 0.2 rad
0042     const static int c_iso_dz_max = 17;       //@ <  17 x 60/2^10 = 1   cm
0043     const static int c_iso_pt_min = 120;      //@ >= , 120  x 25MeV = 3 GeV
0044 
0045     // Assuming 4 bits for Muon isolation
0046     int absiso_thrL;
0047     int absiso_thrM;
0048     int absiso_thrT;
0049     double reliso_thrL;
0050     double reliso_thrM;
0051     double reliso_thrT;
0052     bool verbose_;
0053     bool dumpForHLS_;
0054     std::ofstream dumpOutput;
0055 
0056     typedef ap_ufixed<9, 9, AP_TRN, AP_SAT> iso_accum_t;
0057     typedef ap_ufixed<9, 0> reliso_thresh_t;
0058   };
0059 
0060   inline Isolation::Isolation(const edm::ParameterSet &iConfig)
0061       : absiso_thrL(iConfig.getParameter<int>("AbsIsoThresholdL")),
0062         absiso_thrM(iConfig.getParameter<int>("AbsIsoThresholdM")),
0063         absiso_thrT(iConfig.getParameter<int>("AbsIsoThresholdT")),
0064         reliso_thrL(iConfig.getParameter<double>("RelIsoThresholdL")),
0065         reliso_thrM(iConfig.getParameter<double>("RelIsoThresholdM")),
0066         reliso_thrT(iConfig.getParameter<double>("RelIsoThresholdT")),
0067         verbose_(iConfig.getParameter<int>("verbose")),
0068         dumpForHLS_(iConfig.getParameter<int>("IsodumpForHLS")) {
0069     if (dumpForHLS_) {
0070       dumpInput.open("Isolation_Mu_Track_infolist.txt", std::ofstream::out);
0071       dumpOutput.open("Isolation_Mu_Isolation.txt", std::ofstream::out);
0072     }
0073   }
0074 
0075   inline Isolation::~Isolation() {
0076     if (dumpForHLS_) {
0077       dumpInput.close();
0078       dumpOutput.close();
0079     }
0080   }
0081 
0082   inline Isolation::Isolation(const Isolation &cpy) : TopoAlgo(cpy) {}
0083 
0084   inline void Isolation::DumpOutputs(std::vector<l1t::TrackerMuon> &trkMus) {
0085     static std::atomic<int> nevto = 0;
0086     auto evto = nevto++;
0087     for (unsigned int i = 0; i < trkMus.size(); ++i) {
0088       auto mu = trkMus.at(i);
0089       if (mu.hwPt() != 0) {
0090         double convertphi = mu.hwPhi() * LSBphi;
0091         if (convertphi > M_PI) {
0092           convertphi -= 2 * M_PI;
0093         }
0094         dumpOutput << evto << " " << i << " " << mu.hwPt() * LSBpt << " " << mu.hwEta() * LSBeta << " " << convertphi
0095                    << " " << mu.hwZ0() * LSBGTz0 << " " << mu.hwIso() << endl;
0096       }
0097     }
0098   }
0099 
0100   inline int Isolation::SetAbsIsolationBits(int accum) {
0101     int iso = (accum <= absiso_thrT ? 3 : accum <= absiso_thrM ? 2 : accum <= absiso_thrL ? 1 : 0);
0102 
0103     if (verbose_) {
0104       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso_threshold L : " << absiso_thrL << " accum " << accum
0105                                 << " bit set : " << (accum < absiso_thrL);
0106       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso_threshold M : " << absiso_thrM << " accum " << accum
0107                                 << " bit set : " << (accum < absiso_thrM);
0108       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso_threshold T : " << absiso_thrT << " accum " << accum
0109                                 << " bit set : " << (accum < absiso_thrT);
0110       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso : " << (iso);
0111     }
0112     return iso;
0113   }
0114 
0115   inline int Isolation::SetRelIsolationBits(int accum, int mupt) {
0116     const static reliso_thresh_t relisoL(reliso_thrL);
0117     const static reliso_thresh_t relisoM(reliso_thrM);
0118     const static reliso_thresh_t relisoT(reliso_thrT);
0119 
0120     iso_accum_t thrL = relisoL * mupt;
0121     iso_accum_t thrM = relisoM * mupt;
0122     iso_accum_t thrT = relisoT * mupt;
0123 
0124     int iso = (accum <= thrT.to_int() ? 3 : accum <= thrM.to_int() ? 2 : accum <= thrL.to_int() ? 1 : 0);
0125 
0126     if (verbose_) {
0127       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso_threshold L : " << thrL << " accum " << accum
0128                                 << " bit set : " << (accum < thrL.to_int());
0129       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso_threshold M : " << thrM << " accum " << accum
0130                                 << " bit set : " << (accum < thrM.to_int());
0131       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso_threshold T : " << thrT << " accum " << accum
0132                                 << " bit set : " << (accum < thrT.to_int());
0133       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso : " << (iso << 2) << " org " << iso;
0134     }
0135 
0136     return iso << 2;
0137   }
0138 
0139   inline void Isolation::isolation_allmu_alltrk(std::vector<l1t::TrackerMuon> &trkMus,
0140                                                 std::vector<ConvertedTTTrack> &convertedTracks) {
0141     load(trkMus, convertedTracks);
0142     if (dumpForHLS_) {
0143       DumpInputs();
0144     }
0145 
0146     static std::atomic<int> itest = 0;
0147     if (verbose_) {
0148       edm::LogInfo("Isolation") << "........ RUNNING TEST NUMBER .......... " << itest++;
0149     }
0150 
0151     for (auto &mu : trkMus) {
0152       int accum = 0;
0153       std::vector<unsigned> overlaps;
0154       for (auto t : convertedTracks) {
0155         unsigned ovrl = compute_trk_iso(mu, t);
0156         if (ovrl != 0) {
0157           accum += OverlapRemoval(ovrl, overlaps) * t.pt();
0158         }
0159       }
0160 
0161       // Accumation without fixed bit width
0162       mu.setHwIsoSum(accum);
0163 
0164       // Accumation with fixed bit width
0165       // Bit shifts with 3 bits with LSB of 0.25GeV
0166       iso_accum_t temp(accum);
0167       mu.setHwIsoSumAp(temp.to_int() >> 3);
0168 
0169       //Disable isolation bit, sending isolation sumPT to GT
0170       //iso_ |= SetAbsIsolationBits(accum);
0171       //iso_ |= SetRelIsolationBits(accum, mu.hwPt());
0172 
0173       //mu.setHwIso(iso_);
0174     }
0175 
0176     if (dumpForHLS_) {
0177       DumpOutputs(trkMus);
0178     }
0179   }
0180 
0181   // ===  FUNCTION  ============================================================
0182   //         Name:  Isolation::OverlapRemoval
0183   //  Description:
0184   // ===========================================================================
0185   inline int Isolation::OverlapRemoval(unsigned &ovrl, std::vector<unsigned> &overlaps) {
0186     for (auto i : overlaps) {
0187       // same tracks with Phi can be off by 1 LSB
0188       unsigned diff = ovrl - i;
0189       if (diff <= 1 || diff == unsigned(-1)) {
0190         // When Overlap, return 0 so that this track won't be consider
0191         return 0;
0192       }
0193     }
0194     overlaps.push_back(ovrl);
0195     return 1;
0196   }  // -----  end of function Isolation::OverlapRemoval  -----
0197 
0198   inline unsigned Isolation::compute_trk_iso(l1t::TrackerMuon &in_mu, ConvertedTTTrack &in_trk) {
0199     int dphi = deltaPhi(in_mu.hwPhi(), in_trk.phi());
0200     int deta = deltaEta(in_mu.hwEta(), in_trk.eta());
0201     int dz0 = deltaZ0(in_mu.hwZ0(), in_trk.z0());
0202 
0203     bool pass_deta = (deta < c_iso_dangle_max ? true : false);
0204     bool pass_dphi = (dphi < c_iso_dangle_max ? true : false);
0205     bool pass_dz0 = (dz0 < c_iso_dz_max ? true : false);
0206     bool pass_trkpt = (in_trk.pt() >= c_iso_pt_min ? true : false);
0207     bool pass_ovrl = (deta > 0 || dphi > 0 ? true : false);
0208 
0209     if (verbose_) {
0210       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Start of debug msg for compute_trk_iso";
0211       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : incoming muon (pt / eta / phi / z0 / isvalid)";
0212       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : MU  =  " << in_mu.hwPt() << " / " << in_mu.hwEta()
0213                                 << " / " << in_mu.hwPhi() << " / " << in_mu.hwZ0() << " / " << 1;
0214       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : incoming track (pt / eta / phi / z0 / isvalid)";
0215       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : TRK =  " << in_trk.pt() << " / " << in_trk.eta() << " / "
0216                                 << in_trk.phi() << " / " << in_trk.z0() << " / " << 1;
0217       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Delta phi : " << dphi;
0218       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Delta eta : " << deta;
0219       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Delta z0  : " << dz0;
0220       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_deta      : " << pass_deta;
0221       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_dphi      : " << pass_dphi;
0222       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_dz0       : " << pass_dz0;
0223       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_trkpt     : " << pass_trkpt;
0224       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_ovrl      : " << pass_ovrl;
0225     }
0226     // match conditions
0227     if (pass_deta && pass_dphi && pass_dz0 && pass_trkpt && pass_ovrl) {
0228       if (verbose_) {
0229         edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : THE TRACK WAS MATCHED";
0230         edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : RETURN         : " << in_trk.pt();
0231       }
0232 
0233       //return in_trk.pt();
0234       // Return fixed bit output for duplication removal.
0235       // dZ0(8bis) + deta(10bits)+dphi(10bits)
0236       unsigned int retbits = 0;
0237       retbits |= (dz0 & ((1 << 9) - 1)) << 20;
0238       retbits |= (deta & ((1 << 11) - 1)) << 10;
0239       retbits |= (dphi & ((1 << 11) - 1));
0240       return retbits;
0241     } else {
0242       return 0;
0243     }
0244   }
0245 }  // namespace Phase2L1GMT
0246 #endif  // ----- #ifndef PHASE2GMT_ISOLATION -----