From 05e5ae1bbc3fa2cb83f8e56871cae90bf4c0197b Mon Sep 17 00:00:00 2001 From: Breno Orzari Date: Sat, 8 Jun 2024 06:11:18 +0200 Subject: [PATCH 1/2] Removing try/catch pattern --- DataFormats/TrackReco/interface/Track.h | 3 ++ .../TrackReco/interface/TrackExtraBase.h | 3 ++ .../plugins/SingleLongTrackProducer.cc | 48 ++++--------------- 3 files changed, 15 insertions(+), 39 deletions(-) diff --git a/DataFormats/TrackReco/interface/Track.h b/DataFormats/TrackReco/interface/Track.h index 001b5a7aeb81c..fb71268480e86 100644 --- a/DataFormats/TrackReco/interface/Track.h +++ b/DataFormats/TrackReco/interface/Track.h @@ -157,6 +157,9 @@ namespace reco { /// get the residuals const TrackResiduals& residuals() const { return extra_->residuals(); } + // Check validity of track extra and rechits + bool recHitsOk() const { return extra_.isNonnull() && extra_.isAvailable() && extra_->recHitsOk(); } + private: /// Reference to additional information stored only on RECO. TrackExtraRef extra_; diff --git a/DataFormats/TrackReco/interface/TrackExtraBase.h b/DataFormats/TrackReco/interface/TrackExtraBase.h index 3fe18be4f7fa6..198f76675559b 100644 --- a/DataFormats/TrackReco/interface/TrackExtraBase.h +++ b/DataFormats/TrackReco/interface/TrackExtraBase.h @@ -76,6 +76,9 @@ namespace reco { TrajParams const& trajParams() const { return m_trajParams; } Chi2sFive const& chi2sX5() const { return m_chi2sX5; } + // Check validity of track rechits + bool recHitsOk() const { return m_hitCollection.isNonnull() && m_hitCollection.isAvailable(); } + private: edm::RefCore m_hitCollection; unsigned int m_firstHit; diff --git a/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc b/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc index 8430385347ae4..7644d19696361 100644 --- a/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc +++ b/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc @@ -151,50 +151,27 @@ void SingleLongTrackProducer::produce(edm::Event &iEvent, const edm::EventSetup bool hitIsNotValid{false}; for (const auto &track : selTracks) { - reco::HitPattern hitpattern = track.hitPattern(); - int deref{0}; - - // this checks track recHits - try { // (Un)Comment this line with /* to (not) allow for events with not valid hits + // Check validity of track extra and rechits + if (track.recHitsOk()) { + reco::HitPattern hitpattern = track.hitPattern(); auto hb = track.recHitsBegin(); + // Checking if rechits are valid for (unsigned int h = 0; h < track.recHitsSize(); h++) { auto recHit = *(hb + h); auto const &hit = *recHit; - if (onlyValidHits && !hit.isValid()) { hitIsNotValid = true; continue; } } - } catch (cms::Exception const &e) { - deref += 1; - if (debug) - std::cerr << e.explainSelf() << std::endl; - } - - if (hitIsNotValid == true) - break; // (Un)Comment this line with */ to (not) allow for events with not valid hits - - int deref2{0}; - // this checks track hitPattern hits - try { - auto hb = track.recHitsBegin(); + if (hitIsNotValid == true) + break; // (Un)Comment this line to (not) allow for events with not valid hits + // Checking if hitpattern hits are valid for (unsigned int h = 0; h < track.recHitsSize(); h++) { uint32_t pHit = hitpattern.getHitPattern(reco::HitPattern::TRACK_HITS, h); - - auto recHit = *(hb + h); - auto const &hit = *recHit; - - if (onlyValidHits && !hit.isValid()) { - if (debug) - edm::LogPrint("SingleLongTrackProducer") << "hit not valid: " << h; - continue; - } - - // loop over the hits of the track. if (onlyValidHits && !(hitpattern.validHitFilter(pHit))) { if (debug) edm::LogPrint("SingleLongTrackProducer") << "hit not valid: " << h; @@ -202,15 +179,8 @@ void SingleLongTrackProducer::produce(edm::Event &iEvent, const edm::EventSetup } } goodTracks->push_back(track); - } catch (cms::Exception const &e) { - deref2 += 1; - if (debug) - std::cerr << e.explainSelf() << std::endl; - } - - if (debug) - edm::LogPrint("SingleLongTrackProducer") - << "found tracks with " << deref << "missing valid hits and " << deref2 << " missing hit pattern"; + } else + break; } if (debug) { From 2741744099de9e3eab614d67feab0f20ab59e827 Mon Sep 17 00:00:00 2001 From: mmusich Date: Tue, 11 Jun 2024 06:01:39 +0200 Subject: [PATCH 2/2] refactor SingleLongTrackProducer track selection and use reco::Track::recHitsOk() --- .../plugins/SingleLongTrackProducer.cc | 57 +++++++------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc b/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc index 7644d19696361..a9691bb292138 100644 --- a/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc +++ b/RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc @@ -83,65 +83,52 @@ void SingleLongTrackProducer::produce(edm::Event &iEvent, const edm::EventSetup return; } - const reco::Vertex vtx = vertices->at(0); - // Preselection of long quality tracks + const reco::Vertex &vtx = vertices->at(0); std::vector selTracks; reco::Track bestTrack; unsigned int tMuon = 0; - double fitProb = 100; + double fitProb = std::numeric_limits::max(); for (const auto &track : *tracks) { const reco::HitPattern &hitpattern = track.hitPattern(); - double dR2min = 100.; + double dR2min = std::numeric_limits::max(); double chiNdof = track.normalizedChi2(); double dxy = std::abs(track.dxy(vtx.position())); double dz = std::abs(track.dz(vtx.position())); - if (track.pt() < minPt) - continue; - - if (std::abs(track.eta()) > maxEta) - continue; - - if (hitpattern.trackerLayersWithMeasurement() < minNumberOfLayers) + if (track.pt() < minPt || std::abs(track.eta()) > maxEta || + hitpattern.trackerLayersWithMeasurement() < minNumberOfLayers) { continue; + } - // Long track needs to be close to a good muon (only if requested) - if (matchInDr > 0.) { - for (const auto &m : *muons) { - if (m.isTrackerMuon()) { + if (matchInDr > 0.0) { + for (const auto &muon : *muons) { + if (muon.isTrackerMuon()) { tMuon++; - reco::Track matchedTrack = *(m.innerTrack()); - // match to general track in deltaR - double dr2 = reco::deltaR2(track, matchedTrack); - if (dr2 < dR2min) - dR2min = dr2; + double dr2 = reco::deltaR2(track, *(muon.innerTrack())); + dR2min = std::min(dR2min, dr2); } } - // matchInDr here is defined positive if (dR2min >= matchInDr * matchInDr) continue; } - // do vertex consistency: - bool vertex_match = dxy < maxDxy && dz < maxDz; - if (!(vertex_match)) - continue; - if (track.validFraction() < 1.0) - continue; - // only save the track with the smallest chiNdof - if (chiNdof < fitProb) { + + if (dxy < maxDxy && dz < maxDz && track.validFraction() >= 1.0 && chiNdof < fitProb) { fitProb = chiNdof; bestTrack = track; - bestTrack.setExtra(track.extra()); } - if (debug) - edm::LogPrint("SingleLongTrackProducer") << " deltaR2 (general) track to matched Track: " << dR2min; - if (debug) - edm::LogPrint("SingleLongTrackProducer") << "chi2Ndof:" << chiNdof << " best Track: " << fitProb; + + if (debug) { + edm::LogPrint("SingleLongTrackProducer") << "deltaR2 (general) track to matched Track: " << dR2min + << " chi2Ndof: " << chiNdof << " best Track chi2Ndof: " << fitProb; + } } - selTracks.push_back(bestTrack); + // Only push if bestTrack was set + if (bestTrack.recHitsOk()) { + selTracks.push_back(bestTrack); + } if (debug) edm::LogPrint("SingleLongTrackProducer")