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<std::pair<int, int>> *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 = std::min((int)hitsForSeed.size(), mp_iteration_params->maxCandsPerSeed);
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  for (int ih = 0; ih < num_hits; ih++) {
167  const IdxChi2List &h2a = hitsForSeed[ih];
168 
169  TrackCand tc(ccand[h2a.trkIdx]);
170  tc.addHitIdx(h2a.hitIdx, m_layer, h2a.chi2_hit);
171  tc.setScore(h2a.score);
172 
173  if (h2a.hitIdx == -2) {
174  if (h2a.score > ccand.refBestShortCand().score()) {
175  ccand.setBestShortCand(tc);
176  }
177  continue;
178  }
179 
180  // Could also skip storing of cands with last -3 hit.
181 
182  // Squeeze in extra tracks that are better than current one.
183  while (extra_i != extra_e && sortByScoreTrackCand(*extra_i, tc) &&
184  n_pushed < mp_iteration_params->maxCandsPerSeed) {
185  cv.emplace_back(*extra_i);
186  ++n_pushed;
187  ++extra_i;
188  }
189 
190  if (n_pushed >= mp_iteration_params->maxCandsPerSeed)
191  break;
192 
193  // set the overlap if we have a true hit and pT > pTCutOverlap
194  HitMatch *hm;
195  if (tc.pT() > mp_iteration_params->pTCutOverlap && h2a.hitIdx >= 0 &&
196  (hm = ccand[h2a.trkIdx].findOverlap(h2a.hitIdx, h2a.module))) {
197  tc.addHitIdx(hm->m_hit_idx, m_layer, hm->m_chi2);
198  tc.incOverlapCount();
199  }
200 
201  cv.emplace_back(tc);
202  ++n_pushed;
203 
204  if (h2a.hitIdx >= 0) {
205  mp_kalman_update_list->push_back(std::pair<int, int>(m_start_seed + is, n_pushed - 1));
206  }
207  }
208 
209  // Add remaining extras as long as there is still room for them.
210  while (extra_i != extra_e && n_pushed < mp_iteration_params->maxCandsPerSeed) {
211  cv.emplace_back(*extra_i);
212  ++n_pushed;
213  ++extra_i;
214  }
215 
216  // Can not use ccand.swap(cv) -- allocations for TrackCand vectors need to be
217  // in the same memory segment for gather operation to work in backward-fit.
218  ccand.resize(cv.size());
219  for (size_t ii = 0; ii < cv.size(); ++ii) {
220  ccand[ii] = cv[ii];
221  }
222  cv.clear();
223  } else // hitsForSeed.empty()
224  {
225  if (ccand.state() == CombCandidate::Finding) {
226  ccand.clear();
227 
228  while (extra_i != extra_e) {
229  ccand.emplace_back(*extra_i);
230  ++extra_i;
231  }
232  }
233  }
234 
235  extras.clear();
236  }
237  }
238 
239 } // end namespace mkfit
EventOfCombCandidates * mp_event_of_comb_candidates
Definition: CandCloner.h:58
float chi2_hit
Definition: Track.h:42
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:169
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:190
float score() const
Definition: Track.h:185
unsigned int module
Definition: Track.h:35
void begin_eta_bin(EventOfCombCandidates *e_o_ccs, std::vector< std::pair< int, int >> *update_list, std::vector< std::vector< TrackCand >> *extra_cands, int start_seed, int n_seeds)
Definition: CandCloner.cc:25
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:598
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:90
void setup(const IterationParams &ip)
Definition: CandCloner.cc:16
std::vector< std::pair< int, int > > * mp_kalman_update_list
Definition: CandCloner.h:59
const TrackCand & refBestShortCand() const
SeedState_e state() const
void setBestShortCand(const TrackCand &tc)
#define dprintf(...)
Definition: Debug.h:93