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