CMS 3D CMS Logo

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