Skip to content

Commit

Permalink
Merge pull request #46243 from Dr15Jones/fixTPSAlgorithm
Browse files Browse the repository at this point in the history
Improve TPSAlgorithm
  • Loading branch information
cmsbuild authored Oct 10, 2024
2 parents ea0fd48 + d6c7d6a commit 9668a4d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
50 changes: 26 additions & 24 deletions L1Trigger/Phase2L1GMT/interface/TPSAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Phase2L1GMT {
const unsigned int PHIDIVIDER = 1 << (BITSPHI - BITSSTUBCOORD);
const unsigned int ETADIVIDER = 1 << (BITSETA - BITSSTUBETA);

typedef struct {
struct propagation_t {
ap_int<BITSSTUBCOORD> coord1;
ap_uint<BITSSIGMACOORD> sigma_coord1;
ap_int<BITSSTUBCOORD> coord2;
Expand All @@ -30,47 +30,49 @@ namespace Phase2L1GMT {
ap_uint<BITSSIGMAETA> sigma_eta2;
ap_uint<1> valid;
ap_uint<1> is_barrel;
} propagation_t;
};

typedef struct {
struct match_t {
ap_uint<BITSMATCHQUALITY - 2> quality;
ap_uint<BITSSTUBID> id;
ap_uint<2> valid;
bool isGlobal;
bool isGlobal = false;
l1t::SAMuonRef muRef;
l1t::MuonStubRef stubRef;

} match_t;
};

class TPSAlgorithm {
public:
TPSAlgorithm(const edm::ParameterSet& iConfig);
~TPSAlgorithm();
explicit TPSAlgorithm(const edm::ParameterSet& iConfig);
TPSAlgorithm() = delete;
~TPSAlgorithm() = default;

std::vector<PreTrackMatchedMuon> processNonant(const std::vector<ConvertedTTTrack>& convertedTracks,
const l1t::MuonStubRefVector& stubs);
const l1t::MuonStubRefVector& stubs) const;

std::vector<PreTrackMatchedMuon> cleanNeighbor(const std::vector<PreTrackMatchedMuon>& muons,
const std::vector<PreTrackMatchedMuon>& muonsPrevious,
const std::vector<PreTrackMatchedMuon>& muonsNext,
bool equality);
std::vector<l1t::TrackerMuon> convert(std::vector<PreTrackMatchedMuon>& muons, uint maximum);
bool outputGT(std::vector<l1t::TrackerMuon>& muons);
void SetQualityBits(std::vector<l1t::TrackerMuon>& muons);
std::vector<l1t::TrackerMuon> sort(std::vector<l1t::TrackerMuon>& muons, uint maximum);
bool equality) const;
std::vector<l1t::TrackerMuon> convert(const std::vector<PreTrackMatchedMuon>& muons, uint maximum) const;
bool outputGT(std::vector<l1t::TrackerMuon>& muons) const;
void SetQualityBits(std::vector<l1t::TrackerMuon>& muons) const;
std::vector<l1t::TrackerMuon> sort(std::vector<l1t::TrackerMuon>& muons, uint maximum) const;

private:
int verbose_;
propagation_t propagate(const ConvertedTTTrack& track, uint layer);
ap_uint<BITSSIGMAETA + 1> deltaEta(const ap_int<BITSSTUBETA>& eta1, const ap_int<BITSSTUBETA>& eta2);
ap_uint<BITSSIGMACOORD + 1> deltaCoord(const ap_int<BITSSTUBCOORD>& phi1, const ap_int<BITSSTUBCOORD>& phi2);
match_t match(const propagation_t prop, const l1t::MuonStubRef& stub, uint trackID);
match_t propagateAndMatch(const ConvertedTTTrack& track, const l1t::MuonStubRef& stub, uint trackID);
match_t getBest(const std::vector<match_t> matches);
PreTrackMatchedMuon processTrack(const ConvertedTTTrack&, const l1t::MuonStubRefVector&);
ap_uint<5> cleanMuon(const PreTrackMatchedMuon& mu, const PreTrackMatchedMuon& other, bool eq);
void matchingInfos(std::vector<match_t> matchInfo, PreTrackMatchedMuon& muon, ap_uint<BITSMATCHQUALITY>& quality);
std::vector<PreTrackMatchedMuon> clean(std::vector<PreTrackMatchedMuon>& muons);
propagation_t propagate(const ConvertedTTTrack& track, uint layer) const;
ap_uint<BITSSIGMAETA + 1> deltaEta(const ap_int<BITSSTUBETA>& eta1, const ap_int<BITSSTUBETA>& eta2) const;
ap_uint<BITSSIGMACOORD + 1> deltaCoord(const ap_int<BITSSTUBCOORD>& phi1, const ap_int<BITSSTUBCOORD>& phi2) const;
match_t match(const propagation_t prop, const l1t::MuonStubRef& stub, uint trackID) const;
match_t propagateAndMatch(const ConvertedTTTrack& track, const l1t::MuonStubRef& stub, uint trackID) const;
match_t getBest(const std::vector<match_t>& matches) const;
PreTrackMatchedMuon processTrack(const ConvertedTTTrack&, const l1t::MuonStubRefVector&) const;
ap_uint<5> cleanMuon(const PreTrackMatchedMuon& mu, const PreTrackMatchedMuon& other, bool eq) const;
void matchingInfos(const std::vector<match_t>& matchInfo,
PreTrackMatchedMuon& muon,
ap_uint<BITSMATCHQUALITY>& quality) const;
std::vector<PreTrackMatchedMuon> clean(const std::vector<PreTrackMatchedMuon>& muons) const;
};
} // namespace Phase2L1GMT

Expand Down
47 changes: 22 additions & 25 deletions L1Trigger/Phase2L1GMT/src/TPSAlgorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ using namespace Phase2L1GMT;

TPSAlgorithm::TPSAlgorithm(const edm::ParameterSet& iConfig) : verbose_(iConfig.getParameter<int>("verbose")) {}

TPSAlgorithm::~TPSAlgorithm() {}

std::vector<PreTrackMatchedMuon> TPSAlgorithm::processNonant(const std::vector<ConvertedTTTrack>& convertedTracks,
const l1t::MuonStubRefVector& stubs) {
const l1t::MuonStubRefVector& stubs) const {
std::vector<PreTrackMatchedMuon> preMuons;
for (const auto& track : convertedTracks) {
PreTrackMatchedMuon mu = processTrack(track, stubs);
Expand All @@ -21,7 +19,7 @@ std::vector<PreTrackMatchedMuon> TPSAlgorithm::processNonant(const std::vector<C
std::vector<PreTrackMatchedMuon> TPSAlgorithm::cleanNeighbor(const std::vector<PreTrackMatchedMuon>& muons,
const std::vector<PreTrackMatchedMuon>& muonsPrevious,
const std::vector<PreTrackMatchedMuon>& muonsNext,
bool equality) {
bool equality) const {
std::vector<PreTrackMatchedMuon> out;

if (muons.empty())
Expand Down Expand Up @@ -55,7 +53,7 @@ std::vector<PreTrackMatchedMuon> TPSAlgorithm::cleanNeighbor(const std::vector<P
return out;
}

std::vector<l1t::TrackerMuon> TPSAlgorithm::convert(std::vector<PreTrackMatchedMuon>& muons, uint maximum) {
std::vector<l1t::TrackerMuon> TPSAlgorithm::convert(const std::vector<PreTrackMatchedMuon>& muons, uint maximum) const {
std::vector<l1t::TrackerMuon> out;
for (const auto& mu : muons) {
if (out.size() == maximum)
Expand Down Expand Up @@ -83,7 +81,7 @@ std::vector<l1t::TrackerMuon> TPSAlgorithm::convert(std::vector<PreTrackMatchedM
return out;
}

void TPSAlgorithm::SetQualityBits(std::vector<l1t::TrackerMuon>& muons) {
void TPSAlgorithm::SetQualityBits(std::vector<l1t::TrackerMuon>& muons) const {
for (auto& mu : muons) {
// A preliminary suggestion. Need feedback from the menu group
bool veryloose = mu.numberOfMatches() > 0;
Expand All @@ -96,7 +94,7 @@ void TPSAlgorithm::SetQualityBits(std::vector<l1t::TrackerMuon>& muons) {
}
}

bool TPSAlgorithm::outputGT(std::vector<l1t::TrackerMuon>& muons) {
bool TPSAlgorithm::outputGT(std::vector<l1t::TrackerMuon>& muons) const {
for (auto& mu : muons) {
wordtype word1 = 0;
wordtype word2 = 0;
Expand All @@ -121,22 +119,17 @@ bool TPSAlgorithm::outputGT(std::vector<l1t::TrackerMuon>& muons) {
return true;
}

std::vector<l1t::TrackerMuon> TPSAlgorithm::sort(std::vector<l1t::TrackerMuon>& muons, uint maximum) {
std::vector<l1t::TrackerMuon> TPSAlgorithm::sort(std::vector<l1t::TrackerMuon>& muons, uint maximum) const {
if (muons.size() < 2)
return muons;

std::sort(muons.begin(), muons.end(), [](l1t::TrackerMuon a, l1t::TrackerMuon b) { return a.hwPt() > b.hwPt(); });
std::vector<l1t::TrackerMuon> out;
for (unsigned int i = 0; i < muons.size(); ++i) {
out.push_back(muons[i]);
if (i == (maximum - 1))
break;
}
std::vector<l1t::TrackerMuon> out{muons.begin(), muons.begin() + (maximum < muons.size() ? maximum : muons.size())};

return out;
}

propagation_t TPSAlgorithm::propagate(const ConvertedTTTrack& track, uint layer) {
propagation_t TPSAlgorithm::propagate(const ConvertedTTTrack& track, uint layer) const {
static const std::array<const ap_uint<BITSPROPCOORD>*, 5> lt_prop_coord1 = {
{lt_prop_coord1_0, lt_prop_coord1_1, lt_prop_coord1_2, lt_prop_coord1_3, lt_prop_coord1_4}};
static const std::array<const ap_uint<BITSPROPCOORD>*, 5> lt_prop_coord2 = {
Expand Down Expand Up @@ -255,7 +248,8 @@ propagation_t TPSAlgorithm::propagate(const ConvertedTTTrack& track, uint layer)
return out;
}

ap_uint<BITSSIGMAETA + 1> TPSAlgorithm::deltaEta(const ap_int<BITSSTUBETA>& eta1, const ap_int<BITSSTUBETA>& eta2) {
ap_uint<BITSSIGMAETA + 1> TPSAlgorithm::deltaEta(const ap_int<BITSSTUBETA>& eta1,
const ap_int<BITSSTUBETA>& eta2) const {
ap_fixed<BITSSIGMAETA + 2, BITSSIGMAETA + 2, AP_TRN_ZERO, AP_SAT_SYM> dEta = eta1 - eta2;
if (dEta < 0)
return ap_uint<BITSSIGMAETA + 1>(-dEta);
Expand All @@ -264,7 +258,7 @@ ap_uint<BITSSIGMAETA + 1> TPSAlgorithm::deltaEta(const ap_int<BITSSTUBETA>& eta1
}

ap_uint<BITSSIGMACOORD + 1> TPSAlgorithm::deltaCoord(const ap_int<BITSSTUBCOORD>& phi1,
const ap_int<BITSSTUBCOORD>& phi2) {
const ap_int<BITSSTUBCOORD>& phi2) const {
ap_int<BITSSTUBCOORD> dPhiRoll = phi1 - phi2;
ap_ufixed<BITSSIGMACOORD + 1, BITSSIGMACOORD + 1, AP_TRN_ZERO, AP_SAT_SYM> dPhi;
if (dPhiRoll < 0)
Expand All @@ -275,7 +269,7 @@ ap_uint<BITSSIGMACOORD + 1> TPSAlgorithm::deltaCoord(const ap_int<BITSSTUBCOORD>
return ap_uint<BITSSIGMACOORD + 1>(dPhi);
}

match_t TPSAlgorithm::match(const propagation_t prop, const l1t::MuonStubRef& stub, uint trackID) {
match_t TPSAlgorithm::match(const propagation_t prop, const l1t::MuonStubRef& stub, uint trackID) const {
if (verbose_ == 1) {
edm::LogInfo("TPSAlgo") << "Matching to coord1=" << stub->coord1() << " coord2=" << stub->coord2()
<< " eta1=" << stub->eta1() << " eta2=" << stub->eta2();
Expand Down Expand Up @@ -385,12 +379,14 @@ match_t TPSAlgorithm::match(const propagation_t prop, const l1t::MuonStubRef& st
return out;
}

match_t TPSAlgorithm::propagateAndMatch(const ConvertedTTTrack& track, const l1t::MuonStubRef& stub, uint trackID) {
match_t TPSAlgorithm::propagateAndMatch(const ConvertedTTTrack& track,
const l1t::MuonStubRef& stub,
uint trackID) const {
propagation_t prop = propagate(track, stub->tfLayer());
return match(prop, stub, trackID);
}

match_t TPSAlgorithm::getBest(const std::vector<match_t> matches) {
match_t TPSAlgorithm::getBest(const std::vector<match_t>& matches) const {
match_t best = matches[0];
for (const auto& m : matches) {
if (m.quality > best.quality)
Expand All @@ -400,9 +396,9 @@ match_t TPSAlgorithm::getBest(const std::vector<match_t> matches) {
return best;
}

void TPSAlgorithm::matchingInfos(std::vector<match_t> matchInfo,
void TPSAlgorithm::matchingInfos(const std::vector<match_t>& matchInfo,
PreTrackMatchedMuon& muon,
ap_uint<BITSMATCHQUALITY>& quality) {
ap_uint<BITSMATCHQUALITY>& quality) const {
if (!matchInfo.empty()) {
match_t b = getBest(matchInfo);
if (b.valid != 0) {
Expand All @@ -414,7 +410,8 @@ void TPSAlgorithm::matchingInfos(std::vector<match_t> matchInfo,
}
}

PreTrackMatchedMuon TPSAlgorithm::processTrack(const ConvertedTTTrack& track, const l1t::MuonStubRefVector& stubs) {
PreTrackMatchedMuon TPSAlgorithm::processTrack(const ConvertedTTTrack& track,
const l1t::MuonStubRefVector& stubs) const {
std::array<std::vector<match_t>, 6> matchInfos;

if (verbose_ == 1 && !stubs.empty()) {
Expand Down Expand Up @@ -478,7 +475,7 @@ PreTrackMatchedMuon TPSAlgorithm::processTrack(const ConvertedTTTrack& track, co
return muon;
}

ap_uint<5> TPSAlgorithm::cleanMuon(const PreTrackMatchedMuon& mu, const PreTrackMatchedMuon& other, bool eq) {
ap_uint<5> TPSAlgorithm::cleanMuon(const PreTrackMatchedMuon& mu, const PreTrackMatchedMuon& other, bool eq) const {
ap_uint<5> valid = 0;
ap_uint<5> overlap = 0;
constexpr int bittest = 0xfff; // 4095, corresponding to 11bits
Expand Down Expand Up @@ -514,7 +511,7 @@ ap_uint<5> TPSAlgorithm::cleanMuon(const PreTrackMatchedMuon& mu, const PreTrack
return valid;
}

std::vector<PreTrackMatchedMuon> TPSAlgorithm::clean(std::vector<PreTrackMatchedMuon>& muons) {
std::vector<PreTrackMatchedMuon> TPSAlgorithm::clean(const std::vector<PreTrackMatchedMuon>& muons) const {
std::vector<PreTrackMatchedMuon> out;
if (muons.empty())
return out;
Expand Down

0 comments on commit 9668a4d

Please sign in to comment.