CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HitStructures.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_interface_HitStructures_h
2 #define RecoTracker_MkFitCore_interface_HitStructures_h
3 
8 
9 #include <algorithm>
10 #include <array>
11 
12 namespace mkfit {
13 
15 
16  typedef std::pair<uint16_t, uint16_t> PhiBinInfo_t;
17 
18  typedef std::array<PhiBinInfo_t, Config::m_nphi> vecPhiBinInfo_t;
19 
20  typedef std::vector<vecPhiBinInfo_t> vecvecPhiBinInfo_t;
21 
22  typedef std::array<bool, Config::m_nphi> vecPhiBinDead_t;
23 
24  typedef std::vector<vecPhiBinDead_t> vecvecPhiBinDead_t;
25 
26  //==============================================================================
27 
28  inline bool sortHitsByPhiMT(const Hit& h1, const Hit& h2) {
29  return std::atan2(h1.position()[1], h1.position()[0]) < std::atan2(h2.position()[1], h2.position()[0]);
30  }
31 
32  inline bool sortTrksByPhiMT(const Track& t1, const Track& t2) { return t1.momPhi() < t2.momPhi(); }
33 
34  //==============================================================================
35  //==============================================================================
36 
37  // Note: the same code is used for barrel and endcap. In barrel the longitudinal
38  // bins are in Z and in endcap they are in R -- here this coordinate is called Q
39 
40  // When not defined, hits are accessed from the original hit vector and
41  // only sort ranks are kept for proper access.
42  //
43  //#define COPY_SORTED_HITS
44 
45  class LayerOfHits {
46  public:
47  LayerOfHits() = default;
48 
49  ~LayerOfHits();
50 
51  // Setup and filling
52  //-------------------
53  void setupLayer(const LayerInfo& li);
54 
55  void reset() {}
56 
57  // Get in all hits from given hit-vec
58  void suckInHits(const HitVec& hitv);
59 
60  // Get in all dead regions from given dead-vec
61  void suckInDeads(const DeadVec& deadv);
62 
63  // Use external hit-vec and only use hits that are passed to me.
64  void beginRegistrationOfHits(const HitVec& hitv);
65  void registerHit(int idx);
66  void endRegistrationOfHits(bool build_original_to_internal_map);
67 
68  int nHits() const { return m_n_hits; }
69 
70  // Bin access / queries
71  //----------------------
72  int qBin(float q) const { return (q - m_qmin) * m_fq; }
73 
74  int qBinChecked(float q) const { return std::clamp(qBin(q), 0, m_nq - 1); }
75 
76  // if you don't pass phi in (-pi, +pi), mask away the upper bits using m_phi_mask or use the Checked version.
77  int phiBinFine(float phi) const { return std::floor(m_fphi_fine * (phi + Const::PI)); }
78  int phiBin(float phi) const { return phiBinFine(phi) >> m_phi_bits_shift; }
79 
80  int phiBinChecked(float phi) const { return phiBin(phi) & m_phi_mask; }
81 
82  int phiMaskApply(int in) const { return in & m_phi_mask; }
83 
84  const vecPhiBinInfo_t& vecPhiBinInfo(float q) const { return m_phi_bin_infos[qBin(q)]; }
85 
88  PhiBinInfo_t phi_bin_info(int qi, int pi) const { return m_phi_bin_infos[qi][pi]; }
89  bool phi_bin_dead(int qi, int pi) const { return m_phi_bin_deads[qi][pi]; }
90 
91  float hit_q(int i) const { return m_hit_qs[i]; }
92  float hit_phi(int i) const { return m_hit_phis[i]; }
93 
94  // Use this to map original indices to sorted internal ones. m_ext_idcs needs to be initialized.
95  int getHitIndexFromOriginal(int i) const { return m_ext_idcs[i - m_min_ext_idx]; }
96  // Use this to remap internal hit index to external one.
97  int getOriginalHitIndex(int i) const { return m_hit_ranks[i]; }
98 
99 #ifdef COPY_SORTED_HITS
100  const Hit& refHit(int i) const { return m_hits[i]; }
101  const Hit* hitArray() const { return m_hits; }
102 #else
103  const Hit& refHit(int i) const { return (*m_ext_hits)[i]; }
104  const Hit* hitArray() const { return m_ext_hits->data(); }
105 #endif
106 
107  // Left to document and demonstrate access to bin-info structures.
108  // void selectHitIndices(float q, float phi, float dq, float dphi, std::vector<int>& idcs, bool isForSeeding=false, bool dump=false);
109 
110  void printBins();
111 
112  // Geometry / LayerInfo accessors
113  //--------------------------------
114 
115  const LayerInfo* layer_info() const { return m_layer_info; }
116  int layer_id() const { return m_layer_info->layer_id(); }
117 
118  bool is_barrel() const { return m_is_barrel; }
119  bool is_endcap() const { return !m_is_barrel; }
120 
121  bool is_within_z_limits(float z) const { return m_layer_info->is_within_z_limits(z); }
122  bool is_within_r_limits(float r) const { return m_layer_info->is_within_r_limits(r); }
123 
126  }
127 
130  }
131 
132  bool is_stereo() const { return m_layer_info->is_stereo(); }
133 
134  // Sub-detector type
135  bool is_pixb_lyr() const { return m_layer_info->is_pixb_lyr(); }
136  bool is_pixe_lyr() const { return m_layer_info->is_pixe_lyr(); }
137  bool is_pix_lyr() const { return m_layer_info->is_pix_lyr(); }
138  bool is_tib_lyr() const { return m_layer_info->is_tib_lyr(); }
139  bool is_tob_lyr() const { return m_layer_info->is_tob_lyr(); }
140  bool is_tid_lyr() const { return m_layer_info->is_tid_lyr(); }
141  bool is_tec_lyr() const { return m_layer_info->is_tec_lyr(); }
142 
143  private:
144  // Constants for phi-bin access / index manipulation.
145  static constexpr float m_fphi = Config::m_nphi / Const::TwoPI;
146  static constexpr int m_phi_mask = 0xff;
147  static constexpr int m_phi_bits = 8;
148  static constexpr float m_fphi_fine = 1024 / Const::TwoPI;
149  static constexpr int m_phi_mask_fine = 0x3ff;
150  static constexpr int m_phi_bits_fine = 10; //can't be more than 16
151  static constexpr int m_phi_bits_shift = m_phi_bits_fine - m_phi_bits;
152  static constexpr int m_phi_fine_xmask = ~((1 << m_phi_bits_shift) - 1);
153 
154  void setup_bins(float qmin, float qmax, float dq);
155 
156  void empty_phi_bins(int q_bin, int phi_bin_1, int phi_bin_2, uint16_t hit_count) {
157  for (int pb = phi_bin_1; pb < phi_bin_2; ++pb) {
158  m_phi_bin_infos[q_bin][pb] = {hit_count, hit_count};
159  }
160  }
161 
162  void empty_q_bins(int q_bin_1, int q_bin_2, uint16_t hit_count) {
163  for (int qb = q_bin_1; qb < q_bin_2; ++qb) {
164  empty_phi_bins(qb, 0, Config::m_nphi, hit_count);
165  }
166  }
167 
168  void empty_phi_bins_dead(int q_bin, int phi_bin_1, int phi_bin_2) {
169  for (int pb = phi_bin_1; pb < phi_bin_2; ++pb) {
170  m_phi_bin_deads[q_bin][pb] = false;
171  }
172  }
173 
174  void empty_q_bins_dead(int q_bin_1, int q_bin_2) {
175  for (int qb = q_bin_1; qb < q_bin_2; ++qb) {
177  }
178  }
179 
180 #ifdef COPY_SORTED_HITS
181  void alloc_hits(int size);
182  void free_hits()
183 
184  Hit* m_hits = nullptr;
185  int m_capacity = 0;
186 #else
188 #endif
189  unsigned int* m_hit_ranks = nullptr; // allocated by IceSort via new []
190  std::vector<int> m_ext_idcs;
192  int m_n_hits = 0;
193 
194  // Bin information for hits and dead regions
197 
198  // Cached hit phi and q values to minimize Hit memory access
199  std::vector<float> m_hit_phis;
200  std::vector<float> m_hit_qs;
201 
202  // Geometry / q-binning constants - initialized in setupLayer()
203  const LayerInfo* m_layer_info = nullptr;
204  float m_qmin, m_qmax, m_fq;
205  int m_nq = 0;
207 
208  // Data needed during setup
209  struct HitInfo {
210  float phi;
211  float q;
212  };
213  std::vector<HitInfo> m_hit_infos;
214  std::vector<uint32_t> m_qphifines;
215  };
216 
217  //==============================================================================
218 
219  class EventOfHits {
220  public:
221  EventOfHits(const TrackerInfo& trk_inf);
222 
223  void reset() {
224  for (auto& i : m_layers_of_hits) {
225  i.reset();
226  }
227  }
228 
229  void suckInHits(int layer, const HitVec& hitv) { m_layers_of_hits[layer].suckInHits(hitv); }
230 
231  void suckInDeads(int layer, const DeadVec& deadv) { m_layers_of_hits[layer].suckInDeads(deadv); }
232 
233  const BeamSpot& refBeamSpot() const { return m_beam_spot; }
234  void setBeamSpot(const BeamSpot& bs) { m_beam_spot = bs; }
235 
236  int nLayers() const { return m_n_layers; }
237 
239  const LayerOfHits& operator[](int i) const { return m_layers_of_hits[i]; }
240 
241  private:
242  std::vector<LayerOfHits> m_layers_of_hits;
245  };
246 
247  //==============================================================================
248  // TrackCand, CombinedCandidate and EventOfCombinedCandidates
249  //==============================================================================
250 
251  struct HoTNode {
253  float m_chi2;
255  };
256 
257  struct HitMatch {
258  int m_hit_idx = -1;
259  int m_module_id = -1;
260  float m_chi2 = 1e9;
261 
262  void reset() {
263  m_hit_idx = -1;
264  m_module_id = -1;
265  m_chi2 = 1e9;
266  }
267  };
268 
269  struct HitMatchPair {
271 
272  void reset() {
273  M[0].reset();
274  M[1].reset();
275  }
276 
277  void consider_hit_for_overlap(int hit_idx, int module_id, float chi2) {
278  if (module_id == M[0].m_module_id) {
279  if (chi2 < M[0].m_chi2) {
280  M[0].m_chi2 = chi2;
281  M[0].m_hit_idx = hit_idx;
282  }
283  } else if (module_id == M[1].m_module_id) {
284  if (chi2 < M[1].m_chi2) {
285  M[1].m_chi2 = chi2;
286  M[1].m_hit_idx = hit_idx;
287  }
288  } else {
289  if (M[0].m_chi2 > M[1].m_chi2) {
290  if (chi2 < M[0].m_chi2) {
291  M[0] = {hit_idx, module_id, chi2};
292  }
293  } else {
294  if (chi2 < M[1].m_chi2) {
295  M[1] = {hit_idx, module_id, chi2};
296  }
297  }
298  }
299  }
300 
301  HitMatch* find_overlap(int hit_idx, int module_id) {
302  if (module_id == M[0].m_module_id) {
303  if (M[1].m_hit_idx >= 0)
304  return &M[1];
305  } else if (module_id == M[1].m_module_id) {
306  if (M[0].m_hit_idx >= 0)
307  return &M[0];
308  } else {
309  if (M[0].m_chi2 <= M[1].m_chi2) {
310  if (M[0].m_hit_idx >= 0)
311  return &M[0];
312  } else {
313  if (M[1].m_hit_idx >= 0)
314  return &M[1];
315  }
316  }
317 
318  return nullptr;
319  }
320  };
321 
322  // CcPool - CombCandidate Pool and Allocator
323 
324  template <class T>
325  class CcPool {
326  public:
327  void reset(std::size_t size) {
328  if (size > m_mem.size())
329  m_mem.resize(size);
330  m_pos = 0;
331  m_size = size;
332  }
333 
334  void release() {
335  std::vector<T> tmp;
336  m_mem.swap(tmp);
337  m_pos = 0;
338  m_size = 0;
339  }
340 
341  CcPool(std::size_t size = 0) {
342  if (size)
343  reset(size);
344  }
345 
346  T* allocate(std::size_t n) {
347  if (m_pos + n > m_size)
348  throw std::bad_alloc();
349  T* ret = &m_mem[m_pos];
350  m_pos += n;
351  return ret;
352  }
353 
354  void deallocate(T* p, std::size_t n) noexcept {
355  // we do not care, implied deallocation of the whole pool on reset().
356  }
357 
358  private:
359  std::vector<T> m_mem;
360  std::size_t m_pos = 0;
361  std::size_t m_size = 0;
362  };
363 
364  template <class T>
365  class CcAlloc {
366  public:
367  typedef T value_type;
368 
370 
371  const void* pool_id() const { return m_pool; }
372 
373  T* allocate(std::size_t n) { return m_pool->allocate(n); }
374 
375  void deallocate(T* p, std::size_t n) noexcept { m_pool->deallocate(p, n); }
376 
377  private:
379  };
380 
381  template <class T, class U>
382  bool operator==(const CcAlloc<T>& a, const CcAlloc<U>& b) {
383  return a.pool_id() == b.pool_id();
384  }
385 
386  //------------------------------------------------------------------------------
387 
388  class CombCandidate;
389 
390  class TrackCand : public TrackBase {
391  public:
392  TrackCand() = default;
393 
394  explicit TrackCand(const TrackBase& base, CombCandidate* ccand) : TrackBase(base), m_comb_candidate(ccand) {
395  // Reset hit counters -- caller has to initialize hits.
396  lastHitIdx_ = -1;
397  nFoundHits_ = 0;
398  }
399 
400  // CombCandidate is used as a hit-container for a set of TrackCands originating from
401  // the same seed and track building functions need this access to be able to add hits
402  // into this holder class.
403  // Access is guaranteed to be thread safe as seed ranges pointing into CombCandidate
404  // vector is assigned to threads doing track-finding and final processing is only done
405  // when all worker threads have finished.
408 
409  int lastCcIndex() const { return lastHitIdx_; }
410  int nFoundHits() const { return nFoundHits_; }
411  int nMissingHits() const { return nMissingHits_; }
412  int nOverlapHits() const { return nOverlapHits_; }
413  int nTotalHits() const { return nFoundHits_ + nMissingHits_; }
414 
415  void setLastCcIndex(int i) { lastHitIdx_ = i; }
416  void setNFoundHits(int n) { nFoundHits_ = n; }
417  void setNMissingHits(int n) { nMissingHits_ = n; }
418  void setNOverlapHits(int n) { nOverlapHits_ = n; }
419 
421  int nTailMinusOneHits() const { return nTailMinusOneHits_; }
422 
425 
426  int originIndex() const { return m_origin_index; }
427  void setOriginIndex(int oi) { m_origin_index = oi; }
428 
430  void considerHitForOverlap(int hit_idx, int module_id, float chi2) {
431  m_overlap_hits.consider_hit_for_overlap(hit_idx, module_id, chi2);
432  }
433  HitMatch* findOverlap(int hit_idx, int module_id) { return m_overlap_hits.find_overlap(hit_idx, module_id); }
434 
435  // Inlines after definition of CombCandidate
436 
438  int getLastHitIdx() const;
439  int getLastHitLyr() const;
440 
441  // For additional filter
442  int getLastFoundPixelHitLyr() const;
443  int getLastFoundHitLyr() const;
444  int nUniqueLayers() const;
445 
446  int nLayersByTypeEncoded(const TrackerInfo& trk_inf) const;
447  int nHitsByTypeEncoded(const TrackerInfo& trk_inf) const;
448 
449  int nPixelDecoded(const int& encoded) const { return encoded % 100; }
450  int nStereoDecoded(const int& encoded) const { return (encoded / 100) % 100; }
451  int nMonoDecoded(const int& encoded) const { return (encoded / 10000) % 100; }
452  int nMatchedDecoded(const int& encoded) const { return encoded / 1000000; }
453  int nTotMatchDecoded(const int& encoded) const {
454  return encoded % 100 + (encoded / 100) % 100 + (encoded / 10000) % 100 - encoded / 1000000;
455  }
456 
457  void addHitIdx(int hitIdx, int hitLyr, float chi2);
458 
459  HoTNode& refLastHoTNode(); // for filling up overlap info
460  const HoTNode& refLastHoTNode() const; // for dump traversal
461 
463 
464  Track exportTrack(bool remove_missing_hits = false) const;
465 
468  m_comb_candidate = nullptr;
469  }
470 
471  private:
474 
475  // using TrackBase::lastHitIdx_ to point into hit-on-track-node vector of CombCandidate
476  short int nMissingHits_ = 0;
477  short int nOverlapHits_ = 0;
478 
479  short int nInsideMinusOneHits_ = 0;
480  short int nTailMinusOneHits_ = 0;
481 
482  short int m_origin_index = -1; // index of origin candidate (used for overlaps in Standard)
483  };
484 
485  inline bool sortByScoreTrackCand(const TrackCand& cand1, const TrackCand& cand2) {
486  return cand1.score() > cand2.score();
487  }
488 
489  inline float getScoreCand(const TrackCand& cand1, bool penalizeTailMissHits = false, bool inFindCandidates = false) {
490  int nfoundhits = cand1.nFoundHits();
491  int noverlaphits = cand1.nOverlapHits();
492  int nmisshits = cand1.nInsideMinusOneHits();
493  int ntailmisshits = penalizeTailMissHits ? cand1.nTailMinusOneHits() : 0;
494  float pt = cand1.pT();
495  float chi2 = cand1.chi2();
496  // Do not allow for chi2<0 in score calculation
497  if (chi2 < 0)
498  chi2 = 0.f;
499  return getScoreCalc(nfoundhits, ntailmisshits, noverlaphits, nmisshits, chi2, pt, inFindCandidates);
500  }
501 
502  // CombCandidate -- a set of candidates from a given seed.
503 
505  public:
506  using trk_cand_vec_type = std::vector<TrackCand, CcAlloc<TrackCand>>;
508 
510 
512 
513  // Required by std::uninitialized_fill_n when declaring vector<CombCandidate> in EventOfCombCandidates
516  m_state(o.m_state),
521 #ifdef DUMPHITWINDOW
522  m_seed_algo(o.m_seed_algo),
523  m_seed_label(o.m_seed_label),
524 #endif
526  m_hots(o.m_hots) {
527  }
528 
529  // Required for std::swap().
531  : m_trk_cands(std::move(o.m_trk_cands)),
533  m_state(o.m_state),
538 #ifdef DUMPHITWINDOW
539  m_seed_algo(o.m_seed_algo),
540  m_seed_label(o.m_seed_label),
541 #endif
543  m_hots(std::move(o.m_hots)) {
544  // This is not needed as we do EOCC::reset() after EOCCS::resize which
545  // calls Reset here and all CombCands get cleared.
546  // However, if at some point we start using this for other purposes this needs
547  // to be called as well.
548  // for (auto &tc : *this) tc.setCombCandidate(this);
549  }
550 
551  // Required for std::swap when filtering EventOfCombinedCandidates::m_candidates.
552  // We do not call clear() on vectors as this will be done via EoCCs reset.
553  // Probably would be better (clearer) if there was a special function that does
554  // the swap in here or in EoCCs.
556  m_trk_cands = (std::move(o.m_trk_cands));
557  m_best_short_cand = std::move(o.m_best_short_cand);
558  m_state = o.m_state;
559  m_pickup_layer = o.m_pickup_layer;
560  m_lastHitIdx_before_bkwsearch = o.m_lastHitIdx_before_bkwsearch;
561  m_nInsideMinusOneHits_before_bkwsearch = o.m_nInsideMinusOneHits_before_bkwsearch;
562  m_nTailMinusOneHits_before_bkwsearch = o.m_nTailMinusOneHits_before_bkwsearch;
563 #ifdef DUMPHITWINDOW
564  m_seed_algo = o.m_seed_algo;
565  m_seed_label = o.m_seed_label;
566 #endif
567  m_hots_size = o.m_hots_size;
568  m_hots = std::move(o.m_hots);
569 
570  for (auto& tc : m_trk_cands)
571  tc.setCombCandidate(this);
572 
573  return *this;
574  }
575 
576  // std::vector-like interface to access m_trk_cands
577  bool empty() const { return m_trk_cands.empty(); }
578  trk_cand_vec_type::size_type size() const { return m_trk_cands.size(); }
580  TrackCand& operator[](int i) { return m_trk_cands[i]; }
581  const TrackCand& operator[](int i) const { return m_trk_cands[i]; }
582  TrackCand& front() { return m_trk_cands.front(); }
583  const TrackCand& front() const { return m_trk_cands.front(); }
585  void clear() { m_trk_cands.clear(); }
586 
587  void reset(int max_cands_per_seed, int expected_num_hots) {
588  std::vector<TrackCand, CcAlloc<TrackCand>> tmp(m_trk_cands.get_allocator());
589  m_trk_cands.swap(tmp);
590  m_trk_cands.reserve(max_cands_per_seed); // we *must* never exceed this
591 
593 
594  // state and pickup_layer set in importSeed.
595 
596  // expected_num_hots is different for CloneEngine and Std, especially as long as we
597  // instantiate all candidates before purging them.
598  // ce: N_layer * N_cands ~~ 20 * 6 = 120
599  // std: i don't know, maybe double?
600  m_hots.reserve(expected_num_hots);
601  m_hots_size = 0;
602  m_hots.clear();
603  }
604 
605  void importSeed(const Track& seed, int region);
606 
607  int addHit(const HitOnTrack& hot, float chi2, int prev_idx) {
608  m_hots.push_back({hot, chi2, prev_idx});
609  return m_hots_size++;
610  }
611 
612  void mergeCandsAndBestShortOne(const IterationParams& params, bool update_score, bool sort_cands);
613 
614  void compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits);
615  void beginBkwSearch();
616  void endBkwSearch();
617 
618  // Accessors
619  //-----------
620  int hotsSize() const { return m_hots_size; }
621  const HoTNode& hot_node(int i) const { return m_hots[i]; }
622  HoTNode& hot_node_nc(int i) { return m_hots[i]; }
623  HitOnTrack hot(int i) const { return m_hots[i].m_hot; }
624  // Direct access into array for vectorized code in MkFinder
625  const HoTNode* hotsData() const { return m_hots.data(); }
626 
627  const TrackCand& refBestShortCand() const { return m_best_short_cand; }
628  void setBestShortCand(const TrackCand& tc) { m_best_short_cand = tc; }
629 
630  SeedState_e state() const { return m_state; }
632 
633  int pickupLayer() const { return m_pickup_layer; }
634 
635 #ifdef DUMPHITWINDOW
636  int seed_algo() const { return m_seed_algo; }
637  int seed_label() const { return m_seed_label; }
638 #endif
639 
640  private:
644  int m_pickup_layer : 16;
648 
649 #ifdef DUMPHITWINDOW
650  int m_seed_algo = 0;
651  int m_seed_label = 0;
652 #endif
653  int m_hots_size = 0;
654  std::vector<HoTNode> m_hots;
655  };
656 
657  //==============================================================================
658 
660 
662 
664 
665  inline int TrackCand::getLastFoundHitLyr() const {
666  int nh = nTotalHits();
667  int ch = lastHitIdx_;
668  int ll = -1;
669  while (--nh >= 0) {
670  const HoTNode& hot_node = m_comb_candidate->hot_node(ch);
671  if (hot_node.m_hot.index < 0) {
672  ch = hot_node.m_prev_idx;
673  } else {
674  ll = hot_node.m_hot.layer;
675  break;
676  }
677  }
678  return ll;
679  }
680 
682  int nh = nTotalHits();
683  int ch = lastHitIdx_;
684  int ll = -1;
685  while (--nh >= 0) {
686  const HoTNode& hot_node = m_comb_candidate->hot_node(ch);
687  int tl = hot_node.m_hot.layer;
688  if (hot_node.m_hot.index < 0 || !((0 <= tl && tl <= 3) || (18 <= tl && tl <= 20) || (45 <= tl && tl <= 47))) {
689  ch = hot_node.m_prev_idx;
690  } else if ((0 <= tl && tl <= 3) || (18 <= tl && tl <= 20) || (45 <= tl && tl <= 47)) {
691  ll = hot_node.m_hot.layer;
692  break;
693  }
694  }
695  return ll;
696  }
697 
698  inline int TrackCand::nUniqueLayers() const {
699  int nUL = 0;
700  int prevL = -1;
701  int nh = nTotalHits();
702  int ch = lastHitIdx_;
703 
704  while (--nh >= 0) {
705  const HoTNode& hot_node = m_comb_candidate->hot_node(ch);
706  int thisL = hot_node.m_hot.layer;
707  if (thisL >= 0 && (hot_node.m_hot.index >= 0 || hot_node.m_hot.index == -9) && thisL != prevL) {
708  ++nUL;
709  prevL = thisL;
710  }
711  ch = hot_node.m_prev_idx;
712  }
713  return nUL;
714  }
715 
716  inline int TrackCand::nHitsByTypeEncoded(const TrackerInfo& trk_inf) const {
717  int prevL = -1;
718  bool prevStereo = false;
719  int nh = nTotalHits();
720  int ch = lastHitIdx_;
721  int pix = 0, stereo = 0, mono = 0, matched = 0;
722  int doubleStereo = -1;
723  while (--nh >= 0) {
724  const HoTNode& hot_node = m_comb_candidate->hot_node(ch);
725  int thisL = hot_node.m_hot.layer;
726  if (thisL >= 0 && (hot_node.m_hot.index >= 0 || hot_node.m_hot.index == -9)) {
727  if (trk_inf.is_pix_lyr(thisL))
728  ++pix;
729  else if (trk_inf.is_stereo(thisL)) {
730  ++stereo;
731  if (thisL == prevL)
732  doubleStereo = thisL;
733  } else {
734  //mono if not pixel, nor stereo - can be matched to stereo
735  ++mono;
736  if (prevStereo && thisL == prevL - 1)
737  ++matched;
738  else if (thisL == prevL && thisL == doubleStereo - 1)
739  ++matched; //doubleMatch, the first is counted early on
740  }
741  prevL = thisL;
742  prevStereo = stereo;
743  }
744  ch = hot_node.m_prev_idx;
745  }
746  return pix + 100 * stereo + 10000 * mono + 1000000 * matched;
747  }
748 
749  inline int TrackCand::nLayersByTypeEncoded(const TrackerInfo& trk_inf) const {
750  int prevL = -1;
751  bool prevStereo = false;
752  int nh = nTotalHits();
753  int ch = lastHitIdx_;
754  int pix = 0, stereo = 0, mono = 0, matched = 0;
755  while (--nh >= 0) {
756  const HoTNode& hot_node = m_comb_candidate->hot_node(ch);
757  int thisL = hot_node.m_hot.layer;
758  if (thisL >= 0 && (hot_node.m_hot.index >= 0 || hot_node.m_hot.index == -9) && thisL != prevL) {
759  if (trk_inf.is_pix_lyr(thisL))
760  ++pix;
761  else if (trk_inf.is_stereo(thisL))
762  ++stereo;
763  else {
764  //mono if not pixel, nor stereo - can be matched to stereo
765  ++mono;
766  if (prevStereo && thisL == prevL - 1)
767  ++matched;
768  }
769  prevL = thisL;
770  prevStereo = stereo;
771  }
772  ch = hot_node.m_prev_idx;
773  }
774  return pix + 100 * stereo + 10000 * mono + 1000000 * matched;
775  }
776 
778 
780 
781  //------------------------------------------------------------------------------
782 
783  inline void TrackCand::addHitIdx(int hitIdx, int hitLyr, float chi2) {
784  lastHitIdx_ = m_comb_candidate->addHit({hitIdx, hitLyr}, chi2, lastHitIdx_);
785 
786  if (hitIdx >= 0 || hitIdx == -9) {
787  ++nFoundHits_;
788  chi2_ += chi2;
790  nTailMinusOneHits_ = 0;
791  }
792  //Note that for tracks passing through an inactive module (hitIdx = -7), we do not count the -7 hit against the track when scoring.
793  else {
794  ++nMissingHits_;
795  if (hitIdx == -1)
797  }
798  }
799 
800  //==============================================================================
801 
803  public:
805 
806  void releaseMemory() {
807  { // Get all the destructors called before nuking CcPool.
808  std::vector<CombCandidate> tmp;
809  m_candidates.swap(tmp);
810  }
811  m_capacity = 0;
812  m_size = 0;
813  m_cc_pool.release();
814  }
815 
816  void reset(int new_capacity, int max_cands_per_seed, int expected_num_hots = 128) {
817  m_cc_pool.reset(new_capacity * max_cands_per_seed);
818  if (new_capacity > m_capacity) {
820  std::vector<CombCandidate> tmp(new_capacity, alloc);
821  m_candidates.swap(tmp);
822  m_capacity = new_capacity;
823  }
824  for (int s = 0; s < new_capacity; ++s) {
825  m_candidates[s].reset(max_cands_per_seed, expected_num_hots);
826  }
827  for (int s = new_capacity; s < m_capacity; ++s) {
828  m_candidates[s].reset(0, 0);
829  }
830 
831  m_size = 0;
832  }
833 
834  void resizeAfterFiltering(int n_removed) {
835  assert(n_removed <= m_size);
836  m_size -= n_removed;
837  }
838 
839  void insertSeed(const Track& seed, int region) {
841 
842  m_candidates[m_size].importSeed(seed, region);
843 
844  ++m_size;
845  }
846 
847  void compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits) {
848  for (int i = 0; i < m_size; ++i)
849  m_candidates[i].compactifyHitStorageForBestCand(remove_seed_hits, backward_fit_min_hits);
850  }
851 
852  void beginBkwSearch() {
853  for (int i = 0; i < m_size; ++i)
855  }
856  void endBkwSearch() {
857  for (int i = 0; i < m_size; ++i)
859  }
860 
861  // Accessors
862  int size() const { return m_size; }
863 
864  const CombCandidate& operator[](int i) const { return m_candidates[i]; }
866  CombCandidate& cand(int i) { return m_candidates[i]; }
867 
868  // Direct access for vectorized functions in MkBuilder / MkFinder
869  const std::vector<CombCandidate>& refCandidates() const { return m_candidates; }
870  std::vector<CombCandidate>& refCandidates_nc() { return m_candidates; }
871 
872  private:
874 
875  std::vector<CombCandidate> m_candidates;
876 
878  int m_size;
879  };
880 
881 } // end namespace mkfit
882 #endif
trk_cand_vec_type::size_type size() const
void setOriginIndex(int oi)
tuple base
Main Program
Definition: newFWLiteAna.py:92
static constexpr int m_nphi
Definition: Config.h:81
void endRegistrationOfHits(bool build_original_to_internal_map)
float hit_phi(int i) const
Definition: HitStructures.h:92
bool is_pix_lyr(int i) const
Definition: TrackerInfo.h:154
void addHitIdx(int hitIdx, int hitLyr, float chi2)
T * allocate(std::size_t n)
void reset(std::size_t size)
void deallocate(T *p, std::size_t n) noexcept
int nTailMinusOneHits() const
tuple ret
prodAgent to be discontinued
int originIndex() const
std::vector< CombCandidate > m_candidates
HitMatchPair m_overlap_hits
bool is_within_z_limits(float z) const
Definition: TrackerInfo.h:54
bool is_pixb_lyr() const
Definition: TrackerInfo.h:60
void reset(int max_cands_per_seed, int expected_num_hots)
int qBin(float q) const
Definition: HitStructures.h:72
int phiBinFine(float phi) const
Definition: HitStructures.h:77
const HoTNode & hot_node(int i) const
int qBinChecked(float q) const
Definition: HitStructures.h:74
void mergeCandsAndBestShortOne(const IterationParams &params, bool update_score, bool sort_cands)
bool operator==(const CcAlloc< T > &a, const CcAlloc< U > &b)
void setNOverlapHits(int n)
bool is_pix_lyr() const
Definition: TrackerInfo.h:62
int nTotMatchDecoded(const int &encoded) const
int getOriginalHitIndex(int i) const
Definition: HitStructures.h:97
void resizeAfterFiltering(int n_removed)
const TrackCand & operator[](int i) const
SeedState_e state() const
void consider_hit_for_overlap(int hit_idx, int module_id, float chi2)
const BeamSpot & refBeamSpot() const
CombCandidate * m_comb_candidate
Track exportTrack(bool remove_missing_hits=false) const
std::vector< float > m_hit_phis
CombCandidate & operator=(CombCandidate &&o)
void insertSeed(const Track &seed, int region)
int layer_id() const
void empty_phi_bins_dead(int q_bin, int phi_bin_1, int phi_bin_2)
int getLastFoundHitLyr() const
HitOnTrack getLastHitOnTrack() const
TrackCand m_best_short_cand
int nLayers() const
TrackCand(const TrackBase &base, CombCandidate *ccand)
float score() const
Definition: Track.h:185
HitMatch * findOverlap(int hit_idx, int module_id)
short int m_nInsideMinusOneHits_before_bkwsearch
float chi2() const
Definition: Track.h:184
int nUniqueLayers() const
int nFoundHits() const
void resize(trk_cand_vec_type::size_type count)
std::size_t m_pos
static constexpr int m_phi_mask_fine
const void * pool_id() const
float chi2_
Definition: Track.h:344
const Hit * hitArray() const
void setLastCcIndex(int i)
const HitVec * m_ext_hits
void setNInsideMinusOneHits(int n)
bool phi_bin_dead(int qi, int pi) const
Definition: HitStructures.h:89
void setState(SeedState_e ss)
bool sortHitsByPhiMT(const Hit &h1, const Hit &h2)
Definition: HitStructures.h:28
TrackCand()=default
HitMatch * find_overlap(int hit_idx, int module_id)
void setNFoundHits(int n)
WSR_Result is_within_r_sensitive_region(float r, float dr) const
Definition: TrackerInfo.h:76
const std::vector< CombCandidate > & refCandidates() const
WSR_Result is_within_z_sensitive_region(float z, float dz) const
short int nOverlapHits_
float getScoreCalc(const int nfoundhits, const int ntailholes, const int noverlaphits, const int nmisshits, const float chi2, const float pt, const bool inFindCandidates=false)
Definition: Track.h:606
std::vector< vecPhiBinDead_t > vecvecPhiBinDead_t
Definition: HitStructures.h:24
CombCandidate(const CombCandidate &o)
short int m_nTailMinusOneHits_before_bkwsearch
trk_cand_vec_type::reference emplace_back(TrackCand &tc)
static constexpr int m_phi_mask
int phiMaskApply(int in) const
Definition: HitStructures.h:82
int nLayersByTypeEncoded(const TrackerInfo &trk_inf) const
short int nTailMinusOneHits_
vecvecPhiBinDead_t m_phi_bin_deads
bool sortTrksByPhiMT(const Track &t1, const Track &t2)
Definition: HitStructures.h:32
CombCandidate(const allocator_type &alloc)
TrackCand & operator[](int i)
assert(be >=bs)
constexpr float TwoPI
Definition: Config.h:43
CcPool< TrackCand > m_cc_pool
int nInsideMinusOneHits() const
std::vector< vecPhiBinInfo_t > vecvecPhiBinInfo_t
Definition: HitStructures.h:20
int nMonoDecoded(const int &encoded) const
uint16_t size_type
void setNTailMinusOneHits(int n)
bool is_tib_lyr() const
HoTNode & refLastHoTNode()
void compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits)
const LayerInfo * layer_info() const
constexpr std::array< uint8_t, layerIndexSize > layer
bool empty() const
std::array< PhiBinInfo_t, Config::m_nphi > vecPhiBinInfo_t
Definition: HitStructures.h:18
const Double_t pi
int nHitsByTypeEncoded(const TrackerInfo &trk_inf) const
LayerOfHits()=default
short int nFoundHits_
Definition: Track.h:347
bool is_tid_lyr() const
Definition: TrackerInfo.h:65
int nHits() const
Definition: HitStructures.h:68
std::vector< T > m_mem
void setScore(float s)
Definition: Track.h:190
bool is_endcap() const
void setNMissingHits(int n)
bool is_tec_lyr() const
Definition: TrackerInfo.h:66
int nTotalHits() const
bool is_pixe_lyr() const
Definition: TrackerInfo.h:61
CcPool< T > * m_pool
static constexpr int m_phi_bits
const TrackCand & refBestShortCand() const
int layer_id() const
Definition: TrackerInfo.h:40
void setup_bins(float qmin, float qmax, float dq)
void reset(int new_capacity, int max_cands_per_seed, int expected_num_hots=128)
constexpr float PI
Definition: Config.h:42
void setBeamSpot(const BeamSpot &bs)
bool is_stereo() const
Definition: TrackerInfo.h:50
static constexpr int m_phi_fine_xmask
CombCandidate & cand(int i)
bool is_within_r_limits(float r) const
Definition: TrackerInfo.h:55
int nPixelDecoded(const int &encoded) const
unsigned int * m_hit_ranks
LayerOfHits & operator[](int i)
const vecvecPhiBinDead_t & phi_bin_deads() const
Definition: HitStructures.h:87
bool is_pix_lyr() const
CombCandidate & operator[](int i)
void considerHitForOverlap(int hit_idx, int module_id, float chi2)
def move
Definition: eostools.py:511
float getScoreWorstPossible()
Definition: Track.h:602
void suckInDeads(const DeadVec &deadv)
int nMissingHits() const
const HoTNode * hotsData() const
const vecPhiBinInfo_t & vecPhiBinInfo(float q) const
Definition: HitStructures.h:84
int nMatchedDecoded(const int &encoded) const
bool is_tec_lyr() const
static constexpr float m_fphi_fine
bool is_tid_lyr() const
bool is_tob_lyr() const
Definition: TrackerInfo.h:64
bool is_pixe_lyr() const
int getLastFoundPixelHitLyr() const
int phiBin(float phi) const
Definition: HitStructures.h:78
std::vector< TrackCand, CcAlloc< TrackCand >> trk_cand_vec_type
HitOnTrack hot(int i) const
PhiBinInfo_t phi_bin_info(int qi, int pi) const
Definition: HitStructures.h:88
uint32_t nh
void compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits)
CombCandidate * combCandidate() const
float pT() const
Definition: Track.h:169
bool is_tib_lyr() const
Definition: TrackerInfo.h:63
bool is_stereo(int i) const
Definition: TrackerInfo.h:151
void empty_phi_bins(int q_bin, int phi_bin_1, int phi_bin_2, uint16_t hit_count)
std::vector< Hit > HitVec
std::array< bool, Config::m_nphi > vecPhiBinDead_t
Definition: HitStructures.h:22
int nOverlapHits() const
int lastCcIndex() const
short int lastHitIdx_
Definition: Track.h:346
void setupLayer(const LayerInfo &li)
float getScoreCand(const TrackCand &cand1, bool penalizeTailMissHits=false, bool inFindCandidates=false)
const SVector3 & position() const
Definition: Hit.h:146
short int m_origin_index
static constexpr int m_phi_bits_shift
HoTNode & hot_node_nc(int i)
bool sortByScoreTrackCand(const TrackCand &cand1, const TrackCand &cand2)
void empty_q_bins_dead(int q_bin_1, int q_bin_2)
std::vector< CombCandidate > & refCandidates_nc()
short int m_lastHitIdx_before_bkwsearch
void setCombCandidate(CombCandidate *cc)
void deallocate(T *p, std::size_t n) noexcept
std::vector< DeadRegion > DeadVec
Definition: Hit.h:266
TrackCand & front()
trk_cand_vec_type m_trk_cands
std::vector< float > m_hit_qs
WSR_Result is_within_z_sensitive_region(float z, float dz) const
Definition: TrackerInfo.h:68
double b
Definition: hdecay.h:118
void suckInHits(int layer, const HitVec &hitv)
float score_
Definition: Track.h:345
int phiBinChecked(float phi) const
Definition: HitStructures.h:80
int getLastHitIdx() const
float hit_q(int i) const
Definition: HitStructures.h:91
void suckInHits(const HitVec &hitv)
void beginRegistrationOfHits(const HitVec &hitv)
void importSeed(const Track &seed, int region)
EventOfHits(const TrackerInfo &trk_inf)
bool is_within_r_limits(float r) const
static constexpr int m_phi_bits_fine
bool is_stereo() const
bool is_barrel() const
const Hit & refHit(int i) const
const CombCandidate & operator[](int i) const
const vecvecPhiBinInfo_t & phi_bin_infos() const
Definition: HitStructures.h:86
bool is_within_z_limits(float z) const
double a
Definition: hdecay.h:119
int nStereoDecoded(const int &encoded) const
int addHit(const HitOnTrack &hot, float chi2, int prev_idx)
const LayerOfHits & operator[](int i) const
short int nMissingHits_
vecvecPhiBinInfo_t m_phi_bin_infos
CcAlloc(CcPool< T > *p)
int getHitIndexFromOriginal(int i) const
Definition: HitStructures.h:95
bool is_tob_lyr() const
bool is_pixb_lyr() const
void registerHit(int idx)
const LayerInfo * m_layer_info
CombCandidate(CombCandidate &&o)
WSR_Result is_within_r_sensitive_region(float r, float dr) const
T * allocate(std::size_t n)
int getLastHitLyr() const
std::pair< uint16_t, uint16_t > PhiBinInfo_t
Definition: HitStructures.h:14
short int nInsideMinusOneHits_
CcPool(std::size_t size=0)
tmp
align.sh
Definition: createJobs.py:716
long double T
void suckInDeads(int layer, const DeadVec &deadv)
int pickupLayer() const
HitOnTrack m_hot
std::vector< int > m_ext_idcs
float momPhi() const
Definition: Track.h:172
std::vector< LayerOfHits > m_layers_of_hits
std::vector< HitInfo > m_hit_infos
tuple size
Write out results.
void empty_q_bins(int q_bin_1, int q_bin_2, uint16_t hit_count)
void setBestShortCand(const TrackCand &tc)
const TrackCand & front() const
std::size_t m_size
static constexpr float m_fphi
std::vector< uint32_t > m_qphifines
int hotsSize() const
std::vector< HoTNode > m_hots