Skip to content

Commit 8cd5535

Browse files
authored
Merge pull request #45213 from CMSTrackingPOG/rmTryCatch_from_borzari
Introduce `reco::Track::recHitsOk()` and use it to remove try/catch pattern in `SingleLongTrackProducer`
2 parents babb58f + d6c0d85 commit 8cd5535

File tree

3 files changed

+37
-74
lines changed

3 files changed

+37
-74
lines changed

DataFormats/TrackReco/interface/Track.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ namespace reco {
157157
/// get the residuals
158158
const TrackResiduals& residuals() const { return extra_->residuals(); }
159159

160+
// Check validity of track extra and rechits
161+
bool recHitsOk() const { return extra_.isNonnull() && extra_.isAvailable() && extra_->recHitsOk(); }
162+
160163
private:
161164
/// Reference to additional information stored only on RECO.
162165
TrackExtraRef extra_;

DataFormats/TrackReco/interface/TrackExtraBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ namespace reco {
7676
TrajParams const& trajParams() const { return m_trajParams; }
7777
Chi2sFive const& chi2sX5() const { return m_chi2sX5; }
7878

79+
// Check validity of track rechits
80+
bool recHitsOk() const { return m_hitCollection.isNonnull() && m_hitCollection.isAvailable(); }
81+
7982
private:
8083
edm::RefCore m_hitCollection;
8184
unsigned int m_firstHit;

RecoTracker/FinalTrackSelectors/plugins/SingleLongTrackProducer.cc

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -83,65 +83,52 @@ void SingleLongTrackProducer::produce(edm::Event &iEvent, const edm::EventSetup
8383
return;
8484
}
8585

86-
const reco::Vertex vtx = vertices->at(0);
87-
8886
// Preselection of long quality tracks
87+
const reco::Vertex &vtx = vertices->at(0);
8988
std::vector<reco::Track> selTracks;
9089
reco::Track bestTrack;
9190
unsigned int tMuon = 0;
92-
double fitProb = 100;
91+
double fitProb = std::numeric_limits<double>::max();
9392

9493
for (const auto &track : *tracks) {
9594
const reco::HitPattern &hitpattern = track.hitPattern();
96-
double dR2min = 100.;
95+
double dR2min = std::numeric_limits<double>::max();
9796
double chiNdof = track.normalizedChi2();
9897
double dxy = std::abs(track.dxy(vtx.position()));
9998
double dz = std::abs(track.dz(vtx.position()));
10099

101-
if (track.pt() < minPt)
102-
continue;
103-
104-
if (std::abs(track.eta()) > maxEta)
105-
continue;
106-
107-
if (hitpattern.trackerLayersWithMeasurement() < minNumberOfLayers)
100+
if (track.pt() < minPt || std::abs(track.eta()) > maxEta ||
101+
hitpattern.trackerLayersWithMeasurement() < minNumberOfLayers) {
108102
continue;
103+
}
109104

110-
// Long track needs to be close to a good muon (only if requested)
111-
if (matchInDr > 0.) {
112-
for (const auto &m : *muons) {
113-
if (m.isTrackerMuon()) {
105+
if (matchInDr > 0.0) {
106+
for (const auto &muon : *muons) {
107+
if (muon.isTrackerMuon()) {
114108
tMuon++;
115-
reco::Track matchedTrack = *(m.innerTrack());
116-
// match to general track in deltaR
117-
double dr2 = reco::deltaR2(track, matchedTrack);
118-
if (dr2 < dR2min)
119-
dR2min = dr2;
109+
double dr2 = reco::deltaR2(track, *(muon.innerTrack()));
110+
dR2min = std::min(dR2min, dr2);
120111
}
121112
}
122-
// matchInDr here is defined positive
123113
if (dR2min >= matchInDr * matchInDr)
124114
continue;
125115
}
126-
// do vertex consistency:
127-
bool vertex_match = dxy < maxDxy && dz < maxDz;
128-
if (!(vertex_match))
129-
continue;
130-
if (track.validFraction() < 1.0)
131-
continue;
132-
// only save the track with the smallest chiNdof
133-
if (chiNdof < fitProb) {
116+
117+
if (dxy < maxDxy && dz < maxDz && track.validFraction() >= 1.0 && chiNdof < fitProb) {
134118
fitProb = chiNdof;
135119
bestTrack = track;
136-
bestTrack.setExtra(track.extra());
137120
}
138-
if (debug)
139-
edm::LogPrint("SingleLongTrackProducer") << " deltaR2 (general) track to matched Track: " << dR2min;
140-
if (debug)
141-
edm::LogPrint("SingleLongTrackProducer") << "chi2Ndof:" << chiNdof << " best Track: " << fitProb;
121+
122+
if (debug) {
123+
edm::LogPrint("SingleLongTrackProducer") << "deltaR2 (general) track to matched Track: " << dR2min
124+
<< " chi2Ndof: " << chiNdof << " best Track chi2Ndof: " << fitProb;
125+
}
142126
}
143127

144-
selTracks.push_back(bestTrack);
128+
// Only push if bestTrack was set
129+
if (bestTrack.recHitsOk()) {
130+
selTracks.push_back(bestTrack);
131+
}
145132

146133
if (debug)
147134
edm::LogPrint("SingleLongTrackProducer")
@@ -151,66 +138,36 @@ void SingleLongTrackProducer::produce(edm::Event &iEvent, const edm::EventSetup
151138
bool hitIsNotValid{false};
152139

153140
for (const auto &track : selTracks) {
154-
reco::HitPattern hitpattern = track.hitPattern();
155-
int deref{0};
156-
157-
// this checks track recHits
158-
try { // (Un)Comment this line with /* to (not) allow for events with not valid hits
141+
// Check validity of track extra and rechits
142+
if (track.recHitsOk()) {
143+
reco::HitPattern hitpattern = track.hitPattern();
159144
auto hb = track.recHitsBegin();
160145

146+
// Checking if rechits are valid
161147
for (unsigned int h = 0; h < track.recHitsSize(); h++) {
162148
auto recHit = *(hb + h);
163149
auto const &hit = *recHit;
164-
165150
if (onlyValidHits && !hit.isValid()) {
166151
hitIsNotValid = true;
167152
continue;
168153
}
169154
}
170-
} catch (cms::Exception const &e) {
171-
deref += 1;
172-
if (debug)
173-
std::cerr << e.explainSelf() << std::endl;
174-
}
175155

176-
if (hitIsNotValid == true)
177-
break; // (Un)Comment this line with */ to (not) allow for events with not valid hits
178-
179-
int deref2{0};
180-
181-
// this checks track hitPattern hits
182-
try {
183-
auto hb = track.recHitsBegin();
156+
if (hitIsNotValid == true)
157+
break; // (Un)Comment this line to (not) allow for events with not valid hits
184158

159+
// Checking if hitpattern hits are valid
185160
for (unsigned int h = 0; h < track.recHitsSize(); h++) {
186161
uint32_t pHit = hitpattern.getHitPattern(reco::HitPattern::TRACK_HITS, h);
187-
188-
auto recHit = *(hb + h);
189-
auto const &hit = *recHit;
190-
191-
if (onlyValidHits && !hit.isValid()) {
192-
if (debug)
193-
edm::LogPrint("SingleLongTrackProducer") << "hit not valid: " << h;
194-
continue;
195-
}
196-
197-
// loop over the hits of the track.
198162
if (onlyValidHits && !(hitpattern.validHitFilter(pHit))) {
199163
if (debug)
200164
edm::LogPrint("SingleLongTrackProducer") << "hit not valid: " << h;
201165
continue;
202166
}
203167
}
204168
goodTracks->push_back(track);
205-
} catch (cms::Exception const &e) {
206-
deref2 += 1;
207-
if (debug)
208-
std::cerr << e.explainSelf() << std::endl;
209-
}
210-
211-
if (debug)
212-
edm::LogPrint("SingleLongTrackProducer")
213-
<< "found tracks with " << deref << "missing valid hits and " << deref2 << " missing hit pattern";
169+
} else
170+
break;
214171
}
215172

216173
if (debug) {

0 commit comments

Comments
 (0)