diff --git a/RecoHGCal/GraphReco/plugins/HGCClusterTrackLinker.cc b/RecoHGCal/GraphReco/plugins/HGCClusterTrackLinker.cc index 6dd55a5a82650..42c00048beb37 100644 --- a/RecoHGCal/GraphReco/plugins/HGCClusterTrackLinker.cc +++ b/RecoHGCal/GraphReco/plugins/HGCClusterTrackLinker.cc @@ -80,7 +80,14 @@ void HGCClusterTrackLinker::produce(edm::Event& iEvent, const edm::EventSetup& i if (trackIndices.size()) { // Find best match (just set to the first one for now) // Can do closest in pt or mix of closest in pt and eta/phi - int tidx = 0; + std::vector ptdiffs; + ptdiffs.reserve(trackIndices.size()); + for (size_t entry : trackIndices) { + edm::RefToBase t(tracks, entry); + ptdiffs.push_back(std::abs(t->pt()-cand.pt())); + } + + size_t tidx = std::distance(ptdiffs.begin(), std::min_element(ptdiffs.begin(), ptdiffs.end())); edm::RefToBase matchingTrack(tracks, trackIndices.at(tidx)); trackIndices.erase(trackIndices.begin()+tidx);