CMS 3D CMS Logo

CandCloner.cc
Go to the documentation of this file.
1 #include "CandCloner.h"
2 
4 
5 //#define DEBUG
6 #include "Debug.h"
7 
8 namespace {
9  inline bool sortCandListByScore(const mkfit::IdxChi2List &cand1, const mkfit::IdxChi2List &cand2) {
10  return mkfit::sortByScoreStruct(cand1, cand2);
11  }
12 } // namespace
13 
14 namespace mkfit {
15 
17  mp_iteration_params = &ip;
18  for (int iseed = 0; iseed < s_max_seed_range; ++iseed) {
20  }
21  }
22 
24 
26  std::vector<UpdateIndices> *update_list,
27  std::vector<std::vector<TrackCand>> *extra_cands,
28  int start_seed,
29  int n_seeds) {
31  mp_kalman_update_list = update_list;
32  mp_extra_cands = extra_cands;
33  m_start_seed = start_seed;
34  m_n_seeds = n_seeds;
35  m_hits_to_add.resize(n_seeds);
36 
37  for (int i = 0; i < n_seeds; ++i)
38  m_hits_to_add[i].reserve(4);
39 
40 #ifdef CC_TIME_ETA
41  printf("CandCloner::begin_eta_bin\n");
42  t_eta = dtime();
43 #endif
44  }
45 
46  void CandCloner::begin_layer(int lay) {
47  m_layer = lay;
48 
49  m_idx_max = 0;
50  m_idx_max_prev = 0;
51 
52  mp_kalman_update_list->clear();
53 
54 #ifdef CC_TIME_LAYER
55  t_lay = dtime();
56 #endif
57  }
58 
60  // Do nothing, "secondary" state vars updated when work completed/assigned.
61  }
62 
64  int proc_n = m_idx_max - m_idx_max_prev;
65 
66  dprintf("CandCloner::end_iteration process %d, max_prev=%d, max=%d\n", proc_n, m_idx_max_prev, m_idx_max);
67 
68  if (proc_n >= s_max_seed_range) {
69  // Round to multiple of s_max_seed_range.
71  }
72  }
73 
75  if (m_n_seeds > m_idx_max_prev) {
77  }
78 
79  for (int i = 0; i < m_n_seeds; ++i) {
80  m_hits_to_add[i].clear();
81  }
82 
83 #ifdef CC_TIME_LAYER
84  t_lay = dtime() - t_lay;
85  printf("CandCloner::end_layer %d -- t_lay=%8.6f\n", m_layer, t_lay);
86  printf(" m_idx_max=%d, m_idx_max_prev=%d, issued work=%d\n",
87  m_idx_max,
90 #endif
91  }
92 
94 #ifdef CC_TIME_ETA
95  t_eta = dtime() - t_eta;
96  printf("CandCloner::end_eta_bin t_eta=%8.6f\n", t_eta);
97 #endif
98  }
99  //==============================================================================
100 
102  dprintf("CandCloner::DoWork assigning work from seed %d to %d\n", m_idx_max_prev, idx);
103 
104  int beg = m_idx_max_prev;
105  int the_end = idx;
106 
107  dprintf("CandCloner::DoWork working on beg=%d to the_end=%d\n", beg, the_end);
108 
109  while (beg != the_end) {
110  int end = std::min(beg + s_max_seed_range, the_end);
111 
112  dprintf("CandCloner::DoWork processing %4d -> %4d\n", beg, end);
113 
114  processSeedRange(beg, end);
115 
116  beg = end;
117  }
118 
120  }
121 
122  //==============================================================================
123 
124  void CandCloner::processSeedRange(int is_beg, int is_end) {
125  // Process new hits for a range of seeds.
126 
127  // bool debug = true;
128 
129  dprintf("\nCandCloner::ProcessSeedRange is_beg=%d, is_end=%d\n", is_beg, is_end);
130 
131  //1) sort the candidates
132  for (int is = is_beg; is < is_end; ++is) {
133  std::vector<IdxChi2List> &hitsForSeed = m_hits_to_add[is];
134 
136  std::vector<TrackCand> &extras = (*mp_extra_cands)[is];
137  auto extra_i = extras.begin();
138  auto extra_e = extras.end();
139 
140  // Extras are sorted by candScore.
141 
142 #ifdef DEBUG
143  dprint(" seed n " << is << " with input candidates=" << hitsForSeed.size());
144  for (int ih = 0; ih < (int)hitsForSeed.size(); ih++) {
145  dprint("trkIdx=" << hitsForSeed[ih].trkIdx << " hitIdx=" << hitsForSeed[ih].hitIdx
146  << " chi2=" << hitsForSeed[ih].chi2 << std::endl
147  << " "
148  << "original pt=" << ccand[hitsForSeed[ih].trkIdx].pT() << " "
149  << "nTotalHits=" << ccand[hitsForSeed[ih].trkIdx].nTotalHits() << " "
150  << "nFoundHits=" << ccand[hitsForSeed[ih].trkIdx].nFoundHits() << " "
151  << "chi2=" << ccand[hitsForSeed[ih].trkIdx].chi2());
152  }
153 #endif
154 
155  if (!hitsForSeed.empty()) {
156  //sort the new hits
157  std::sort(hitsForSeed.begin(), hitsForSeed.end(), sortCandListByScore);
158 
159  int num_hits = (int)hitsForSeed.size();
160 
161  // This is from buffer, we know it was cleared after last usage.
162  std::vector<TrackCand> &cv = t_cands_for_next_lay[is - is_beg];
163 
164  int n_pushed = 0;
165 
166  // Loop over available hits. There are extra loop exit checks every time
167  // a new candidate is inserted into the new candidate_vector.
168  for (int ih = 0; ih < num_hits; ih++) {
169  const IdxChi2List &h2a = hitsForSeed[ih];
170 
171  TrackCand tc(ccand[h2a.trkIdx]);
172  tc.addHitIdx(h2a.hitIdx, m_layer, h2a.chi2_hit);
173  tc.setScore(h2a.score);
174 
175  if (h2a.hitIdx == -2) {
176  if (h2a.score > ccand.refBestShortCand().score()) {
177  ccand.setBestShortCand(tc);
178  }
179  continue;
180  }
181 
182  // Could also skip storing of cands with last -3 hit.
183 
184  // Squeeze in extra tracks that are better than current one.
185  while (extra_i != extra_e && sortByScoreTrackCand(*extra_i, tc) &&
186  n_pushed < mp_iteration_params->maxCandsPerSeed) {
187  cv.emplace_back(*extra_i);
188  ++n_pushed;
189  ++extra_i;
190  }
191 
192  if (n_pushed >= mp_iteration_params->maxCandsPerSeed)
193  break;
194 
195  if (h2a.hitIdx >= 0) {
196  mp_kalman_update_list->emplace_back(UpdateIndices(m_start_seed + is, n_pushed, h2a.hitIdx));
197 
198  // set the overlap if we have it and and pT > pTCutOverlap
199  HitMatch *hm;
200  if (tc.pT() > mp_iteration_params->pTCutOverlap &&
201  (hm = ccand[h2a.trkIdx].findOverlap(h2a.hitIdx, h2a.module))) {
202  tc.addHitIdx(hm->m_hit_idx, m_layer, 0);
203  tc.incOverlapCount();
204  }
205  }
206 
207  cv.emplace_back(tc);
208  ++n_pushed;
209 
210  if (n_pushed >= mp_iteration_params->maxCandsPerSeed)
211  break;
212  }
213 
214  // Add remaining extras as long as there is still room for them.
215  while (extra_i != extra_e && n_pushed < mp_iteration_params->maxCandsPerSeed) {
216  cv.emplace_back(*extra_i);
217  ++n_pushed;
218  ++extra_i;
219  }
220 
221  // Can not use ccand.swap(cv) -- allocations for TrackCand vectors need to be
222  // in the same memory segment for gather operation to work in backward-fit.
223  ccand.resize(cv.size());
224  for (size_t ii = 0; ii < cv.size(); ++ii) {
225  ccand[ii] = cv[ii];
226  }
227  cv.clear();
228  } else // hitsForSeed.empty()
229  {
230  if (ccand.state() == CombCandidate::Finding) {
231  ccand.clear();
232 
233  while (extra_i != extra_e) {
234  ccand.emplace_back(*extra_i);
235  ++extra_i;
236  }
237  }
238  }
239 
240  extras.clear();
241  }
242  }
243 
244 } // end namespace mkfit
EventOfCombCandidates * mp_event_of_comb_candidates
Definition: CandCloner.h:58
float chi2_hit
Definition: Track.h:44
std::vector< std::vector< IdxChi2List > > m_hits_to_add
Definition: CandCloner.h:55
void addHitIdx(int hitIdx, int hitLyr, float chi2)
static const int s_max_seed_range
Definition: CandCloner.h:19
float pT() const
Definition: Track.h:171
void resize(trk_cand_vec_type::size_type count)
cv
Definition: cuy.py:363
trk_cand_vec_type::reference emplace_back(TrackCand &tc)
std::vector< std::vector< TrackCand > > t_cands_for_next_lay
Definition: CandCloner.h:71
const IterationParams * mp_iteration_params
Definition: CandCloner.h:57
void end_iteration()
Definition: CandCloner.cc:63
void setScore(float s)
Definition: Track.h:192
float score() const
Definition: Track.h:187
unsigned int module
Definition: Track.h:37
CombCandidate & cand(int i)
std::vector< std::vector< TrackCand > > * mp_extra_cands
Definition: CandCloner.h:60
void processSeedRange(int is_beg, int is_end)
Definition: CandCloner.cc:124
void doWork(int idx)
Definition: CandCloner.cc:101
ii
Definition: cuy.py:589
bool sortByScoreStruct(const IdxChi2List &cand1, const IdxChi2List &cand2)
Definition: Track.h:600
void begin_layer(int lay)
Definition: CandCloner.cc:46
int iseed
Definition: AMPTWrapper.h:134
bool sortByScoreTrackCand(const TrackCand &cand1, const TrackCand &cand2)
double dtime()
void begin_iteration()
Definition: CandCloner.cc:59
#define dprint(x)
Definition: Debug.h:95
void setup(const IterationParams &ip)
Definition: CandCloner.cc:16
const TrackCand & refBestShortCand() const
void begin_eta_bin(EventOfCombCandidates *e_o_ccs, std::vector< UpdateIndices > *update_list, std::vector< std::vector< TrackCand >> *extra_cands, int start_seed, int n_seeds)
Definition: CandCloner.cc:25
std::vector< UpdateIndices > * mp_kalman_update_list
Definition: CandCloner.h:59
SeedState_e state() const
void setBestShortCand(const TrackCand &tc)
#define dprintf(...)
Definition: Debug.h:98