CMS 3D CMS Logo

TrackStructures.cc
Go to the documentation of this file.
2 
4 #include "Matriplex/Memory.h"
5 
6 #include "Debug.h"
7 
8 namespace mkfit {
9 
10  //==============================================================================
11  // TrackCand
12  //==============================================================================
13 
14  Track TrackCand::exportTrack(bool remove_missing_hits) const {
15  dprintf("TrackCand::exportTrack label=%5d, total_hits=%2d, overlaps=%2d -- n_seed_hits=%d,prod_type=%d\n",
16  label(),
17  nTotalHits(),
19  getNSeedHits(),
20  (int)prodType());
21 
22  Track res(*this);
23  res.resizeHits(remove_missing_hits ? nFoundHits() : nTotalHits(), nFoundHits());
24  res.setNOverlapHits(nOverlapHits());
25 
26  int nh = nTotalHits();
27  int ch = lastHitIdx_;
28  int good_hits_pos = nFoundHits();
29  while (--nh >= 0) {
30  const HoTNode &hot_node = m_comb_candidate->hot_node(ch);
31  if (remove_missing_hits) {
32  if (hot_node.m_hot.index >= 0)
33  res.setHitIdxAtPos(--good_hits_pos, hot_node.m_hot);
34  } else {
35  res.setHitIdxAtPos(nh, hot_node.m_hot);
36  }
37  dprintf(" nh=%2d, ch=%d, idx=%d lyr=%d prev_idx=%d\n",
38  nh,
39  ch,
40  hot_node.m_hot.index,
41  hot_node.m_hot.layer,
42  hot_node.m_prev_idx);
43  ch = hot_node.m_prev_idx;
44  }
45 
46  return res;
47  }
48 
49  //==============================================================================
50  // CombCandidate
51  //==============================================================================
52 
54  m_trk_cands.emplace_back(TrackCand(seed, this));
55 
57  m_pickup_layer = seed.getLastHitLyr();
58 #ifdef DUMPHITWINDOW
59  m_seed_algo = seed.algoint();
60  m_seed_label = seed.label();
61 #endif
62 
63  TrackCand &cand = m_trk_cands.back();
64  cand.setNSeedHits(seed.nTotalHits());
65  cand.setEtaRegion(region);
66 
67  dprintf("Importing pt=%f eta=%f, lastCcIndex=%d\n", cand.pT(), cand.momEta(), cand.lastCcIndex());
68 
69  for (const HitOnTrack *hp = seed.beginHitsOnTrack(); hp != seed.endHitsOnTrack(); ++hp) {
70  dprintf(" hit idx=%d lyr=%d\n", hp->index, hp->layer);
71  cand.addHitIdx(hp->index, hp->layer, 0.0f);
72  }
73 
74  cand.setScore(getScoreCand(cand));
75  }
76 
77  void CombCandidate::mergeCandsAndBestShortOne(const IterationParams &params, bool update_score, bool sort_cands) {
78  TrackCand *best_short = m_best_short_cand.combCandidate() ? &m_best_short_cand : nullptr;
79 
80  if (!empty()) {
81  if (update_score) {
82  for (auto &c : m_trk_cands)
83  c.setScore(getScoreCand(c));
84  if (best_short)
85  best_short->setScore(getScoreCand(*best_short));
86  }
87  if (sort_cands) {
89  }
90 
91  if (best_short && best_short->score() > m_trk_cands.back().score()) {
92  auto ci = m_trk_cands.begin();
93  while (ci->score() > best_short->score())
94  ++ci;
95 
96  if ((int)m_trk_cands.size() >= params.maxCandsPerSeed)
97  m_trk_cands.pop_back();
98 
99  // To print out what has been replaced -- remove when done with short track handling.
100 #ifdef DEBUG
101  if (ci == m_trk_cands.begin()) {
102  printf("FindTracksStd -- Replacing best cand (%f) with short one (%f) in final sorting\n",
103  m_trk_cands.front().score(),
104  best_short->score());
105  }
106 #endif
107 
108  m_trk_cands.insert(ci, *best_short);
109  }
110 
111  } else if (best_short) {
112  m_trk_cands.push_back(*best_short);
113  }
114 
115  if (best_short)
116  best_short->resetShortTrack();
117 
118  // assert(capacity() == (size_t)Config::maxCandsPerSeed);
119  }
120 
121  void CombCandidate::compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits) {
122  // The best candidate is assumed to be in position 0 (after mergeCandsAndBestShortOne
123  // mergeCandsAndBestShortOne has been called).
124  // Other cands are dropped, their hits are dropped as well.
125  // Seed hits are dropped if remove_seed_hits is true.
126 
127  /* The following considerations are related to the following implementation:
128  minNrOfHitsForRebuild (checked against "nHits - nseed") has a default at 5, except
129  1 in initialStep
130  4 in tobTec and pixelLess
131  https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/plugins/GroupedCkfTrajectoryBuilder.cc#L1015
132 
133  NOTE: some of those can be matched hits !!!
134 
135  the hit splitting is triggered here: https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc#L468
136  after the rebuild has already happened: https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc#L313
137  */
138 
139  assert(!m_trk_cands.empty());
140  m_trk_cands.resize(1);
141  TrackCand &tc = m_trk_cands[0];
142 
143  // Do NOT remove any seed hits if fewer than backward_fit_min_hits hits are available.
144  if (remove_seed_hits && tc.nFoundHits() <= backward_fit_min_hits) {
145  remove_seed_hits = false;
146  }
147 
148  // Stash HoTNodes at the end of m_hots.
149  int stash_end = m_hots.size();
150  int stash_pos = stash_end;
151 
152  int idx = tc.lastCcIndex();
153 
154  if (remove_seed_hits) {
155  // Skip invalid hits that would now be at the head of the candidate.
156  // Make sure to subtract / recount number of hits:
157  // as this is rather involved, just call addHitIdx() repeatedly so counts
158  // of holes get updated correctly.
159  // Though one should not care super much ... it's only relevant for relative scores
160  // and here we are trimming everything down to a single candidate.
161 
162  int n_hits_to_pick = std::max(tc.nFoundHits() - tc.getNSeedHits(), backward_fit_min_hits);
163  while (n_hits_to_pick > 0) {
164  m_hots[--stash_pos] = m_hots[idx];
165  if (m_hots[idx].m_hot.index >= 0)
166  --n_hits_to_pick;
167  idx = m_hots[idx].m_prev_idx;
168  }
169 
170  m_hots_size = 0;
171  m_hots.clear();
172  tc.setLastCcIndex(-1);
173  tc.setNFoundHits(0);
174  tc.setNMissingHits(0);
176  tc.setNTailMinusOneHits(0);
177  while (stash_pos != stash_end && m_hots[stash_pos].m_hot.index < 0)
178  ++stash_pos;
179  while (stash_pos != stash_end) {
180  HoTNode &hn = m_hots[stash_pos];
181  tc.addHitIdx(hn.m_hot.index, hn.m_hot.layer, hn.m_chi2);
182  ++stash_pos;
183  }
184  } else {
185  while (idx != -1) {
186  m_hots[--stash_pos] = m_hots[idx];
187  idx = m_hots[idx].m_prev_idx;
188  }
189 
190  // If we are not removing seed_hits, track is good as it is,
191  // just fixup m_hots and t.lastCcIndex.
192  int pos = 0;
193  while (stash_pos != stash_end) {
194  m_hots[pos].m_hot = m_hots[stash_pos].m_hot;
195  m_hots[pos].m_chi2 = m_hots[stash_pos].m_chi2;
196  m_hots[pos].m_prev_idx = pos - 1;
197  ++pos;
198  ++stash_pos;
199  }
200  m_hots.resize(pos);
201  m_hots_size = pos;
202  tc.setLastCcIndex(pos - 1);
203  }
204  }
205 
207  // Assumes compactifyHitStorageForBestCand() has already been called.
208  //
209  // This is to be called before backward-search to start with a single
210  // input candidate for backward combinatorial search.
211  //
212  // m_state and m_pickup_layer are also set.
213 
214  TrackCand &tc = m_trk_cands[0];
215 
216  m_state = Dormant;
217  m_pickup_layer = m_hots[0].m_hot.layer;
221  tc.setLastCcIndex(0);
223  tc.setNTailMinusOneHits(0);
224  }
225 
227  // mergeCandsAndBestShortOne() has already been called (from MkBuilder::FindXxx()).
228  // We have to fixup the best candidate.
229 
230  TrackCand &tc = m_trk_cands[0];
231 
232  int curr_idx = tc.lastCcIndex();
233  if (curr_idx != 0) {
234  int last_idx = -1, prev_idx;
235  do {
236  prev_idx = m_hots[curr_idx].m_prev_idx;
237 
238  m_hots[curr_idx].m_prev_idx = last_idx;
239 
240  last_idx = curr_idx;
241  curr_idx = prev_idx;
242  } while (prev_idx != -1);
243  }
244 
251  }
252 
253 } // namespace mkfit
int nInsideMinusOneHits() const
void addHitIdx(int hitIdx, int hitLyr, float chi2)
int nOverlapHits() const
void mergeCandsAndBestShortOne(const IterationParams &params, bool update_score, bool sort_cands)
Track exportTrack(bool remove_missing_hits=false) const
CombCandidate * m_comb_candidate
const HoTNode & hot_node(int i) const
short int m_nInsideMinusOneHits_before_bkwsearch
int nTailMinusOneHits() const
void setLastCcIndex(int i)
void setNInsideMinusOneHits(int n)
ProdType prodType() const
Definition: Track.h:261
int label() const
Definition: Track.h:186
void setNFoundHits(int n)
short int nOverlapHits_
short int m_nTailMinusOneHits_before_bkwsearch
assert(be >=bs)
void setNTailMinusOneHits(int n)
Definition: Electron.h:6
int getNSeedHits() const
Definition: Track.h:264
void setScore(float s)
Definition: Track.h:190
void setNMissingHits(int n)
float score() const
Definition: Track.h:185
float getScoreCand(const Track &cand1, bool penalizeTailMissHits=false, bool inFindCandidates=false)
Definition: Track.h:630
CombCandidate * combCandidate() const
uint32_t nh
void compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits)
int lastCcIndex() const
int nTotalHits() const
short int lastHitIdx_
Definition: Track.h:346
bool sortByScoreTrackCand(const TrackCand &cand1, const TrackCand &cand2)
short int m_lastHitIdx_before_bkwsearch
trk_cand_vec_type m_trk_cands
void importSeed(const Track &seed, int region)
int nFoundHits() const
HitOnTrack m_hot
#define dprintf(...)
Definition: Debug.h:93
std::vector< HoTNode > m_hots