File indexing completed on 2024-07-16 02:43:05
0001 #include "L1Trigger/Phase2L1GMT/interface/SAMuonCleaner.h"
0002
0003 void SAMuonCleaner::overlapCleanTrack(l1t::SAMuon& source, const l1t::SAMuon& other, bool eq) {
0004 int rank1 = source.hwQual();
0005 int rank2 = other.hwQual();
0006 bool keep = true;
0007 if ((eq && rank1 <= rank2) || ((!eq) && rank1 < rank2))
0008 keep = false;
0009 l1t::MuonStubRefVector stubs;
0010 for (const auto& s1 : source.stubs()) {
0011 bool ok = true;
0012 for (const auto& s2 : other.stubs()) {
0013 if ((*s1) == (*s2) && (!keep))
0014 ok = false;
0015 }
0016 if (ok) {
0017 stubs.push_back(s1);
0018 }
0019 }
0020 source.setStubs(stubs);
0021 }
0022
0023 void SAMuonCleaner::overlapCleanTrackInter(l1t::SAMuon& source, const l1t::SAMuon& other) {
0024 bool keep = false;
0025 l1t::MuonStubRefVector stubs;
0026 for (const auto& s1 : source.stubs()) {
0027 bool ok = true;
0028 for (const auto& s2 : other.stubs()) {
0029 if ((*s1) == (*s2) && (!keep))
0030 ok = false;
0031 }
0032 if (ok) {
0033 stubs.push_back(s1);
0034 }
0035 }
0036 source.setStubs(stubs);
0037 }
0038
0039 std::vector<l1t::SAMuon> SAMuonCleaner::cleanTF(const std::vector<l1t::SAMuon>& tfMuons) {
0040 std::vector<l1t::SAMuon> out;
0041 for (unsigned int i = 0; i < tfMuons.size(); ++i) {
0042 l1t::SAMuon source = tfMuons[i];
0043 for (unsigned int j = 0; j < tfMuons.size(); ++j) {
0044 if (i == j)
0045 continue;
0046 overlapCleanTrack(source, tfMuons[j], false);
0047 }
0048 if (source.stubs().size() > 1)
0049 out.push_back(source);
0050 }
0051 return out;
0052 }
0053
0054 std::vector<l1t::SAMuon> SAMuonCleaner::interTFClean(const std::vector<l1t::SAMuon>& bmtf,
0055 const std::vector<l1t::SAMuon>& omtf,
0056 const std::vector<l1t::SAMuon>& emtf) {
0057 std::vector<l1t::SAMuon> out = emtf;
0058 for (unsigned int i = 0; i < omtf.size(); ++i) {
0059 l1t::SAMuon source = omtf[i];
0060 for (const auto& other : emtf) {
0061 overlapCleanTrackInter(source, other);
0062 }
0063 if (source.stubs().size() > 1)
0064 out.push_back(source);
0065 }
0066 for (unsigned int i = 0; i < bmtf.size(); ++i) {
0067 l1t::SAMuon source = bmtf[i];
0068 for (const auto& other : omtf) {
0069 overlapCleanTrackInter(source, other);
0070 }
0071 if (source.stubs().size() > 1)
0072 out.push_back(source);
0073 }
0074 return out;
0075 }
0076
0077 void SAMuonCleaner::swap(std::vector<l1t::SAMuon>& list, int i, int j) {
0078 const l1t::SAMuon& track1 = list[i];
0079 const l1t::SAMuon& track2 = list[j];
0080
0081 int pt1 = track1.pt();
0082 int pt2 = track2.pt();
0083 bool swap = false;
0084 if (pt1 > pt2)
0085 swap = false;
0086 else
0087 swap = true;
0088
0089 if (swap) {
0090 l1t::SAMuon tmp = list[i];
0091 list[i] = list[j];
0092 list[j] = tmp;
0093 }
0094 }
0095
0096 void SAMuonCleaner::sort(std::vector<l1t::SAMuon>& in) {
0097 l1t::SAMuon nullTrack;
0098 nullTrack.setP4(reco::Candidate::LorentzVector(0, 0, 0, 0.000001));
0099 while (in.size() < 32)
0100 in.push_back(nullTrack);
0101
0102 for (uint iteration = 0; iteration < 16; ++iteration) {
0103 for (uint i = 0; i < 32; i = i + 2) {
0104 swap(in, i, i + 1);
0105 }
0106 for (uint i = 1; i < 31; i = i + 2) {
0107 swap(in, i, i + 1);
0108 }
0109 }
0110
0111 std::vector<l1t::SAMuon> out;
0112 for (const auto& track : in) {
0113 if (!track.stubs().empty() && out.size() < 12)
0114 out.push_back(track);
0115 }
0116 in = out;
0117 }
0118
0119 std::vector<l1t::SAMuon> SAMuonCleaner::cleanTFMuons(const std::vector<l1t::SAMuon>& muons) {
0120 std::vector<l1t::SAMuon> out;
0121
0122
0123 std::vector<l1t::SAMuon> bmtf;
0124 std::vector<l1t::SAMuon> omtf_pos;
0125 std::vector<l1t::SAMuon> emtf_pos;
0126 std::vector<l1t::SAMuon> omtf_neg;
0127 std::vector<l1t::SAMuon> emtf_neg;
0128
0129 for (const auto& mu : muons) {
0130 if (mu.tfType() == l1t::tftype::bmtf) {
0131 bmtf.push_back(mu);
0132 } else if (mu.tfType() == l1t::tftype::omtf_pos) {
0133 omtf_pos.push_back(mu);
0134 } else if (mu.tfType() == l1t::tftype::emtf_pos) {
0135 emtf_pos.push_back(mu);
0136 } else if (mu.tfType() == l1t::tftype::omtf_neg) {
0137 omtf_neg.push_back(mu);
0138 } else if (mu.tfType() == l1t::tftype::emtf_neg) {
0139 emtf_neg.push_back(mu);
0140 }
0141 }
0142
0143 std::vector<l1t::SAMuon> omtf_cleaned = cleanTF(omtf_pos);
0144 std::vector<l1t::SAMuon> omtf_neg_cleaned = cleanTF(omtf_neg);
0145 omtf_cleaned.insert(omtf_cleaned.end(), omtf_neg_cleaned.begin(), omtf_neg_cleaned.end());
0146 sort(omtf_cleaned);
0147
0148 std::vector<l1t::SAMuon> emtf_cleaned = cleanTF(emtf_pos);
0149 std::vector<l1t::SAMuon> emtf_neg_cleaned = cleanTF(emtf_neg);
0150 emtf_cleaned.insert(emtf_cleaned.end(), emtf_neg_cleaned.begin(), emtf_neg_cleaned.end());
0151 sort(emtf_cleaned);
0152
0153 std::vector<l1t::SAMuon> cleaned = interTFClean(bmtf, omtf_cleaned, emtf_cleaned);
0154 sort(cleaned);
0155 return cleaned;
0156 }