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 
53  void CombCandidate::importSeed(const Track &seed, const track_score_func &score_func, int region) {
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(score_func, cand));
75  }
76 
78  const track_score_func &score_func,
79  bool update_score,
80  bool sort_cands) {
81  TrackCand *best_short = m_best_short_cand.combCandidate() ? &m_best_short_cand : nullptr;
82 
83  if (!empty()) {
84  if (update_score) {
85  for (auto &c : m_trk_cands)
86  c.setScore(getScoreCand(score_func, c));
87  if (best_short)
88  best_short->setScore(getScoreCand(score_func, *best_short));
89  }
90  if (sort_cands) {
92  }
93 
94  if (best_short && best_short->score() > m_trk_cands.back().score()) {
95  auto ci = m_trk_cands.begin();
96  while (ci->score() > best_short->score())
97  ++ci;
98 
99  if ((int)m_trk_cands.size() >= params.maxCandsPerSeed)
100  m_trk_cands.pop_back();
101 
102  // To print out what has been replaced -- remove when done with short track handling.
103 #ifdef DEBUG
104  if (ci == m_trk_cands.begin()) {
105  printf("FindTracksStd -- Replacing best cand (%f) with short one (%f) in final sorting\n",
106  m_trk_cands.front().score(),
107  best_short->score());
108  }
109 #endif
110 
111  m_trk_cands.insert(ci, *best_short);
112  }
113 
114  } else if (best_short) {
115  m_trk_cands.push_back(*best_short);
116  }
117 
118  if (best_short)
119  best_short->resetShortTrack();
120 
121  // assert(capacity() == (size_t)Config::maxCandsPerSeed);
122  }
123 
124  void CombCandidate::compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits) {
125  // The best candidate is assumed to be in position 0 (after mergeCandsAndBestShortOne
126  // mergeCandsAndBestShortOne has been called).
127  // Other cands are dropped, their hits are dropped as well.
128  // Seed hits are dropped if remove_seed_hits is true.
129 
130  /* The following considerations are related to the following implementation:
131  minNrOfHitsForRebuild (checked against "nHits - nseed") has a default at 5, except
132  1 in initialStep
133  4 in tobTec and pixelLess
134  https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/plugins/GroupedCkfTrajectoryBuilder.cc#L1015
135 
136  NOTE: some of those can be matched hits !!!
137 
138  the hit splitting is triggered here: https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc#L468
139  after the rebuild has already happened: https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc#L313
140  */
141 
142  assert(!m_trk_cands.empty());
143  m_trk_cands.resize(1);
144  TrackCand &tc = m_trk_cands[0];
145 
146  // Do NOT remove any seed hits if fewer than backward_fit_min_hits hits are available.
147  if (remove_seed_hits && tc.nFoundHits() <= backward_fit_min_hits) {
148  remove_seed_hits = false;
149  }
150 
151  // Stash HoTNodes at the end of m_hots.
152  int stash_end = m_hots.size();
153  int stash_pos = stash_end;
154 
155  int idx = tc.lastCcIndex();
156 
157  if (remove_seed_hits) {
158  // Skip invalid hits that would now be at the head of the candidate.
159  // Make sure to subtract / recount number of hits:
160  // as this is rather involved, just call addHitIdx() repeatedly so counts
161  // of holes get updated correctly.
162  // Though one should not care super much ... it's only relevant for relative scores
163  // and here we are trimming everything down to a single candidate.
164 
165  int n_hits_to_pick = std::max(tc.nFoundHits() - tc.getNSeedHits(), backward_fit_min_hits);
166  while (n_hits_to_pick > 0) {
167  m_hots[--stash_pos] = m_hots[idx];
168  if (m_hots[idx].m_hot.index >= 0)
169  --n_hits_to_pick;
170  idx = m_hots[idx].m_prev_idx;
171  }
172 
173  m_hots_size = 0;
174  m_hots.clear();
175  tc.setLastCcIndex(-1);
176  tc.setNFoundHits(0);
177  tc.setNMissingHits(0);
179  tc.setNTailMinusOneHits(0);
180  while (stash_pos != stash_end && m_hots[stash_pos].m_hot.index < 0)
181  ++stash_pos;
182  while (stash_pos != stash_end) {
183  HoTNode &hn = m_hots[stash_pos];
184  tc.addHitIdx(hn.m_hot.index, hn.m_hot.layer, hn.m_chi2);
185  ++stash_pos;
186  }
187  } else {
188  while (idx != -1) {
189  m_hots[--stash_pos] = m_hots[idx];
190  idx = m_hots[idx].m_prev_idx;
191  }
192 
193  // If we are not removing seed_hits, track is good as it is,
194  // just fixup m_hots and t.lastCcIndex.
195  int pos = 0;
196  while (stash_pos != stash_end) {
197  m_hots[pos].m_hot = m_hots[stash_pos].m_hot;
198  m_hots[pos].m_chi2 = m_hots[stash_pos].m_chi2;
199  m_hots[pos].m_prev_idx = pos - 1;
200  ++pos;
201  ++stash_pos;
202  }
203  m_hots.resize(pos);
204  m_hots_size = pos;
205  tc.setLastCcIndex(pos - 1);
206  }
207  }
208 
210  // Assumes compactifyHitStorageForBestCand() has already been called.
211  //
212  // This is to be called before backward-search to start with a single
213  // input candidate for backward combinatorial search.
214  //
215  // m_state and m_pickup_layer are also set.
216 
217  TrackCand &tc = m_trk_cands[0];
218 
219  m_state = Dormant;
220  m_pickup_layer = m_hots[0].m_hot.layer;
224  tc.setLastCcIndex(0);
226  tc.setNTailMinusOneHits(0);
227  }
228 
230  // Called during filtering following backward search when a TrackCand's
231  // front hits need to be reindexed.
232  // mergeCandsAndBestShortOne() has already been called (from MkBuilder::FindXxx()).
233  // NOTES:
234  // 1. Should only be called once for each i (flag/bit to allow multiple calls can be added).
235  // 2. Alternatively, CombCand could provide hit iterator/exporter that would handle this correctly.
236 
237  TrackCand &tc = m_trk_cands[i];
238 
239  int curr_idx = tc.lastCcIndex();
240  if (curr_idx != 0) {
241  int last_idx = -1, prev_idx;
242  do {
243  prev_idx = m_hots[curr_idx].m_prev_idx;
244 
245  m_hots[curr_idx].m_prev_idx = last_idx;
246 
247  last_idx = curr_idx;
248  curr_idx = prev_idx;
249  } while (prev_idx != -1);
250  }
251 
255  }
256 
257 } // namespace mkfit
int nInsideMinusOneHits() const
float getScoreCand(const track_score_func &score_func, const Track &cand1, bool penalizeTailMissHits=false, bool inFindCandidates=false)
Definition: Track.h:608
void addHitIdx(int hitIdx, int hitLyr, float chi2)
void mergeCandsAndBestShortOne(const IterationParams &params, const track_score_func &score_func, bool update_score, bool sort_cands)
int nOverlapHits() const
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:263
int label() const
Definition: Track.h:188
void setNFoundHits(int n)
short int nOverlapHits_
short int m_nTailMinusOneHits_before_bkwsearch
assert(be >=bs)
void setNTailMinusOneHits(int n)
std::function< track_score_cf > track_score_func
Definition: FunctionTypes.h:40
Definition: Electron.h:6
int getNSeedHits() const
Definition: Track.h:266
void setScore(float s)
Definition: Track.h:192
void setNMissingHits(int n)
float score() const
Definition: Track.h:187
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:348
void repackCandPostBkwSearch(int i)
bool sortByScoreTrackCand(const TrackCand &cand1, const TrackCand &cand2)
short int m_lastHitIdx_before_bkwsearch
trk_cand_vec_type m_trk_cands
int nFoundHits() const
HitOnTrack m_hot
#define dprintf(...)
Definition: Debug.h:98
std::vector< HoTNode > m_hots
void importSeed(const Track &seed, const track_score_func &score_func, int region)