Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-18 04:13:29

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     dumpForHLS_ = true;
0070     if (dumpForHLS_) {
0071       dumpInput.open("Isolation_Mu_Track_infolist.txt", std::ofstream::out);
0072       dumpOutput.open("Isolation_Mu_Isolation.txt", std::ofstream::out);
0073     }
0074   }
0075 
0076   inline Isolation::~Isolation() {
0077     if (dumpForHLS_) {
0078       dumpInput.close();
0079       dumpOutput.close();
0080     }
0081   }
0082 
0083   inline Isolation::Isolation(const Isolation &cpy) : TopoAlgo(cpy) {}
0084 
0085   inline void Isolation::DumpOutputs(std::vector<l1t::TrackerMuon> &trkMus) {
0086     static std::atomic<int> nevto = 0;
0087     auto evto = nevto++;
0088     for (unsigned int i = 0; i < trkMus.size(); ++i) {
0089       auto mu = trkMus.at(i);
0090       if (mu.hwPt() != 0) {
0091         double convertphi = mu.hwPhi() * LSBphi;
0092         if (convertphi > M_PI) {
0093           convertphi -= 2 * M_PI;
0094         }
0095         dumpOutput << evto << " " << i << " " << mu.hwPt() * LSBpt << " " << mu.hwEta() * LSBeta << " " << convertphi
0096                    << " " << mu.hwZ0() * LSBGTz0 << " " << mu.hwIso() << endl;
0097       }
0098     }
0099   }
0100 
0101   inline int Isolation::SetAbsIsolationBits(int accum) {
0102     int iso = (accum <= absiso_thrT ? 3 : accum <= absiso_thrM ? 2 : accum <= absiso_thrL ? 1 : 0);
0103 
0104     if (verbose_) {
0105       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso_threshold L : " << absiso_thrL << " accum " << accum
0106                                 << " bit set : " << (accum < absiso_thrL);
0107       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso_threshold M : " << absiso_thrM << " accum " << accum
0108                                 << " bit set : " << (accum < absiso_thrM);
0109       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso_threshold T : " << absiso_thrT << " accum " << accum
0110                                 << " bit set : " << (accum < absiso_thrT);
0111       edm::LogInfo("Isolation") << " [DEBUG Isolation] : absiso : " << (iso);
0112     }
0113     return iso;
0114   }
0115 
0116   inline int Isolation::SetRelIsolationBits(int accum, int mupt) {
0117     const static reliso_thresh_t relisoL(reliso_thrL);
0118     const static reliso_thresh_t relisoM(reliso_thrM);
0119     const static reliso_thresh_t relisoT(reliso_thrT);
0120 
0121     iso_accum_t thrL = relisoL * mupt;
0122     iso_accum_t thrM = relisoM * mupt;
0123     iso_accum_t thrT = relisoT * mupt;
0124 
0125     int iso = (accum <= thrT.to_int() ? 3 : accum <= thrM.to_int() ? 2 : accum <= thrL.to_int() ? 1 : 0);
0126 
0127     if (verbose_) {
0128       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso_threshold L : " << thrL << " accum " << accum
0129                                 << " bit set : " << (accum < thrL.to_int());
0130       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso_threshold M : " << thrM << " accum " << accum
0131                                 << " bit set : " << (accum < thrM.to_int());
0132       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso_threshold T : " << thrT << " accum " << accum
0133                                 << " bit set : " << (accum < thrT.to_int());
0134       edm::LogInfo("Isolation") << " [DEBUG Isolation] : reliso : " << (iso << 2) << " org " << iso;
0135     }
0136 
0137     return iso << 2;
0138   }
0139 
0140   inline void Isolation::isolation_allmu_alltrk(std::vector<l1t::TrackerMuon> &trkMus,
0141                                                 std::vector<ConvertedTTTrack> &convertedTracks) {
0142     load(trkMus, convertedTracks);
0143     if (dumpForHLS_) {
0144       DumpInputs();
0145     }
0146 
0147     static std::atomic<int> itest = 0;
0148     if (verbose_) {
0149       edm::LogInfo("Isolation") << "........ RUNNING TEST NUMBER .......... " << itest++;
0150     }
0151 
0152     for (auto &mu : trkMus) {
0153       int accum = 0;
0154       int iso_ = 0;
0155       std::vector<unsigned> overlaps;
0156       for (auto t : convertedTracks) {
0157         unsigned ovrl = compute_trk_iso(mu, t);
0158         if (ovrl != 0) {
0159           accum += OverlapRemoval(ovrl, overlaps) * t.pt();
0160         }
0161       }
0162 
0163       // Only 8 bit for accumation?
0164       mu.setHwIsoSum(accum);
0165 
0166       iso_accum_t temp(accum);
0167       accum = temp.to_int();
0168 
0169       mu.setHwIsoSumAp(accum);
0170 
0171       iso_ |= SetAbsIsolationBits(accum);
0172       iso_ |= SetRelIsolationBits(accum, mu.hwPt());
0173 
0174       mu.setHwIso(iso_);
0175     }
0176 
0177     if (dumpForHLS_) {
0178       DumpOutputs(trkMus);
0179     }
0180   }
0181 
0182   // ===  FUNCTION  ============================================================
0183   //         Name:  Isolation::OverlapRemoval
0184   //  Description:
0185   // ===========================================================================
0186   inline int Isolation::OverlapRemoval(unsigned &ovrl, std::vector<unsigned> &overlaps) {
0187     for (auto i : overlaps) {
0188       // same tracks with Phi can be off by 1 LSB
0189       unsigned diff = ovrl - i;
0190       if (diff <= 1 || diff == unsigned(-1)) {
0191         // When Overlap, return 0 so that this track won't be consider
0192         return 0;
0193       }
0194     }
0195     overlaps.push_back(ovrl);
0196     return 1;
0197   }  // -----  end of function Isolation::OverlapRemoval  -----
0198 
0199   inline unsigned Isolation::compute_trk_iso(l1t::TrackerMuon &in_mu, ConvertedTTTrack &in_trk) {
0200     int dphi = deltaPhi(in_mu.hwPhi(), in_trk.phi());
0201     int deta = deltaEta(in_mu.hwEta(), in_trk.eta());
0202     int dz0 = deltaZ0(in_mu.hwZ0(), in_trk.z0());
0203 
0204     bool pass_deta = (deta < c_iso_dangle_max ? true : false);
0205     bool pass_dphi = (dphi < c_iso_dangle_max ? true : false);
0206     bool pass_dz0 = (dz0 < c_iso_dz_max ? true : false);
0207     bool pass_trkpt = (in_trk.pt() >= c_iso_pt_min ? true : false);
0208     bool pass_ovrl = (deta > 0 || dphi > 0 ? true : false);
0209 
0210     if (verbose_) {
0211       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Start of debug msg for compute_trk_iso";
0212       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : incoming muon (pt / eta / phi / z0 / isvalid)";
0213       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : MU  =  " << in_mu.hwPt() << " / " << in_mu.hwEta()
0214                                 << " / " << in_mu.hwPhi() << " / " << in_mu.hwZ0() << " / " << 1;
0215       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : incoming track (pt / eta / phi / z0 / isvalid)";
0216       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : TRK =  " << in_trk.pt() << " / " << in_trk.eta() << " / "
0217                                 << in_trk.phi() << " / " << in_trk.z0() << " / " << 1;
0218       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Delta phi : " << dphi;
0219       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Delta eta : " << deta;
0220       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : Delta z0  : " << dz0;
0221       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_deta      : " << pass_deta;
0222       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_dphi      : " << pass_dphi;
0223       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_dz0       : " << pass_dz0;
0224       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_trkpt     : " << pass_trkpt;
0225       edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : pass_ovrl      : " << pass_ovrl;
0226     }
0227     // match conditions
0228     if (pass_deta && pass_dphi && pass_dz0 && pass_trkpt && pass_ovrl) {
0229       if (verbose_) {
0230         edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : THE TRACK WAS MATCHED";
0231         edm::LogInfo("Isolation") << " [DEBUG compute_trk_iso] : RETURN         : " << in_trk.pt();
0232       }
0233 
0234       //return in_trk.pt();
0235       // Return fixed bit output for duplication removal.
0236       // dZ0(8bis) + deta(10bits)+dphi(10bits)
0237       unsigned int retbits = 0;
0238       retbits |= (dz0 & ((1 << 9) - 1)) << 20;
0239       retbits |= (deta & ((1 << 11) - 1)) << 10;
0240       retbits |= (dphi & ((1 << 11) - 1));
0241       return retbits;
0242     } else {
0243       return 0;
0244     }
0245   }
0246 }  // namespace Phase2L1GMT
0247 #endif  // ----- #ifndef PHASE2GMT_ISOLATION -----