Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:22:37

0001 #include "RecoTracker/MkFitCore/interface/TrackStructures.h"
0002 
0003 #include "RecoTracker/MkFitCore/interface/IterationConfig.h"
0004 #include "Matriplex/Memory.h"
0005 
0006 #include "Debug.h"
0007 
0008 namespace mkfit {
0009 
0010   //==============================================================================
0011   // TrackCand
0012   //==============================================================================
0013 
0014   Track TrackCand::exportTrack(bool remove_missing_hits) const {
0015     dprintf("TrackCand::exportTrack label=%5d, total_hits=%2d, overlaps=%2d -- n_seed_hits=%d,prod_type=%d\n",
0016             label(),
0017             nTotalHits(),
0018             nOverlapHits_,
0019             getNSeedHits(),
0020             (int)prodType());
0021 
0022     Track res(*this);
0023     res.resizeHits(remove_missing_hits ? nFoundHits() : nTotalHits(), nFoundHits());
0024     res.setNOverlapHits(nOverlapHits());
0025 
0026     int nh = nTotalHits();
0027     int ch = lastHitIdx_;
0028     int good_hits_pos = nFoundHits();
0029     while (--nh >= 0) {
0030       const HoTNode &hot_node = m_comb_candidate->hot_node(ch);
0031       if (remove_missing_hits) {
0032         if (hot_node.m_hot.index >= 0)
0033           res.setHitIdxAtPos(--good_hits_pos, hot_node.m_hot);
0034       } else {
0035         res.setHitIdxAtPos(nh, hot_node.m_hot);
0036       }
0037       dprintf("  nh=%2d, ch=%d, idx=%d lyr=%d prev_idx=%d\n",
0038               nh,
0039               ch,
0040               hot_node.m_hot.index,
0041               hot_node.m_hot.layer,
0042               hot_node.m_prev_idx);
0043       ch = hot_node.m_prev_idx;
0044     }
0045 
0046     return res;
0047   }
0048 
0049   //==============================================================================
0050   // CombCandidate
0051   //==============================================================================
0052 
0053   void CombCandidate::importSeed(const Track &seed, const track_score_func &score_func, int region) {
0054     m_trk_cands.emplace_back(TrackCand(seed, this));
0055 
0056     m_state = CombCandidate::Dormant;
0057     m_pickup_layer = seed.getLastHitLyr();
0058 #ifdef DUMPHITWINDOW
0059     m_seed_algo = seed.algoint();
0060     m_seed_label = seed.label();
0061 #endif
0062 
0063     TrackCand &cand = m_trk_cands.back();
0064     cand.setNSeedHits(seed.nTotalHits());
0065     cand.setEtaRegion(region);
0066 
0067     dprintf("Importing pt=%f eta=%f, lastCcIndex=%d\n", cand.pT(), cand.momEta(), cand.lastCcIndex());
0068 
0069     for (const HitOnTrack *hp = seed.beginHitsOnTrack(); hp != seed.endHitsOnTrack(); ++hp) {
0070       dprintf(" hit idx=%d lyr=%d\n", hp->index, hp->layer);
0071       cand.addHitIdx(hp->index, hp->layer, 0.0f);
0072     }
0073 
0074     cand.setScore(getScoreCand(score_func, cand));
0075   }
0076 
0077   void CombCandidate::mergeCandsAndBestShortOne(const IterationParams &params,
0078                                                 const track_score_func &score_func,
0079                                                 bool update_score,
0080                                                 bool sort_cands) {
0081     TrackCand *best_short = m_best_short_cand.combCandidate() ? &m_best_short_cand : nullptr;
0082 
0083     if (!empty()) {
0084       if (update_score) {
0085         for (auto &c : m_trk_cands)
0086           c.setScore(getScoreCand(score_func, c));
0087         if (best_short)
0088           best_short->setScore(getScoreCand(score_func, *best_short));
0089       }
0090       if (sort_cands) {
0091         std::sort(m_trk_cands.begin(), m_trk_cands.end(), sortByScoreTrackCand);
0092       }
0093 
0094       if (best_short && best_short->score() > m_trk_cands.back().score()) {
0095         auto ci = m_trk_cands.begin();
0096         while (ci->score() > best_short->score())
0097           ++ci;
0098 
0099         if ((int)m_trk_cands.size() >= params.maxCandsPerSeed)
0100           m_trk_cands.pop_back();
0101 
0102           // To print out what has been replaced -- remove when done with short track handling.
0103 #ifdef DEBUG
0104         if (ci == m_trk_cands.begin()) {
0105           printf("FindTracksStd -- Replacing best cand (%f) with short one (%f) in final sorting\n",
0106                  m_trk_cands.front().score(),
0107                  best_short->score());
0108         }
0109 #endif
0110 
0111         m_trk_cands.insert(ci, *best_short);
0112       }
0113 
0114     } else if (best_short) {
0115       m_trk_cands.push_back(*best_short);
0116     }
0117 
0118     if (best_short)
0119       best_short->resetShortTrack();
0120 
0121     // assert(capacity() == (size_t)Config::maxCandsPerSeed);
0122   }
0123 
0124   void CombCandidate::compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits) {
0125     // The best candidate is assumed to be in position 0 (after mergeCandsAndBestShortOne
0126     // mergeCandsAndBestShortOne has been called).
0127     // Other cands are dropped, their hits are dropped as well.
0128     // Seed hits are dropped if remove_seed_hits is true.
0129 
0130     /* The following considerations are related to the following implementation:
0131   minNrOfHitsForRebuild (checked against "nHits - nseed") has a default at 5, except
0132   1 in initialStep
0133   4 in tobTec and pixelLess
0134   https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/plugins/GroupedCkfTrajectoryBuilder.cc#L1015
0135 
0136   NOTE: some of those can be matched hits !!!
0137 
0138   the hit splitting is triggered here: https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc#L468
0139   after the rebuild has already happened: https://github.com/cms-sw/cmssw/blob/master/RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc#L313
0140   */
0141 
0142     assert(!m_trk_cands.empty());
0143     m_trk_cands.resize(1);
0144     TrackCand &tc = m_trk_cands[0];
0145 
0146     // Do NOT remove any seed hits if fewer than backward_fit_min_hits hits are available.
0147     if (remove_seed_hits && tc.nFoundHits() <= backward_fit_min_hits) {
0148       remove_seed_hits = false;
0149     }
0150 
0151     // Stash HoTNodes at the end of m_hots.
0152     int stash_end = m_hots.size();
0153     int stash_pos = stash_end;
0154 
0155     int idx = tc.lastCcIndex();
0156 
0157     if (remove_seed_hits) {
0158       // Skip invalid hits that would now be at the head of the candidate.
0159       // Make sure to subtract / recount number of hits:
0160       // as this is rather involved, just call addHitIdx() repeatedly so counts
0161       // of holes get updated correctly.
0162       // Though one should not care super much ... it's only relevant for relative scores
0163       // and here we are trimming everything down to a single candidate.
0164 
0165       int n_hits_to_pick = std::max(tc.nFoundHits() - tc.getNSeedHits(), backward_fit_min_hits);
0166       while (n_hits_to_pick > 0) {
0167         m_hots[--stash_pos] = m_hots[idx];
0168         if (m_hots[idx].m_hot.index >= 0)
0169           --n_hits_to_pick;
0170         idx = m_hots[idx].m_prev_idx;
0171       }
0172 
0173       m_hots_size = 0;
0174       m_hots.clear();
0175       tc.setLastCcIndex(-1);
0176       tc.setNFoundHits(0);
0177       tc.setNMissingHits(0);
0178       tc.setNInsideMinusOneHits(0);
0179       tc.setNTailMinusOneHits(0);
0180       while (stash_pos != stash_end && m_hots[stash_pos].m_hot.index < 0)
0181         ++stash_pos;
0182       while (stash_pos != stash_end) {
0183         HoTNode &hn = m_hots[stash_pos];
0184         tc.addHitIdx(hn.m_hot.index, hn.m_hot.layer, hn.m_chi2);
0185         ++stash_pos;
0186       }
0187     } else {
0188       while (idx != -1) {
0189         m_hots[--stash_pos] = m_hots[idx];
0190         idx = m_hots[idx].m_prev_idx;
0191       }
0192 
0193       // If we are not removing seed_hits, track is good as it is,
0194       // just fixup m_hots and t.lastCcIndex.
0195       int pos = 0;
0196       while (stash_pos != stash_end) {
0197         m_hots[pos].m_hot = m_hots[stash_pos].m_hot;
0198         m_hots[pos].m_chi2 = m_hots[stash_pos].m_chi2;
0199         m_hots[pos].m_prev_idx = pos - 1;
0200         ++pos;
0201         ++stash_pos;
0202       }
0203       m_hots.resize(pos);
0204       m_hots_size = pos;
0205       tc.setLastCcIndex(pos - 1);
0206     }
0207   }
0208 
0209   void CombCandidate::beginBkwSearch() {
0210     // Assumes compactifyHitStorageForBestCand() has already been called.
0211     //
0212     // This is to be called before backward-search to start with a single
0213     // input candidate for backward combinatorial search.
0214     //
0215     // m_state and m_pickup_layer are also set.
0216 
0217     TrackCand &tc = m_trk_cands[0];
0218 
0219     m_state = Dormant;
0220     m_pickup_layer = m_hots[0].m_hot.layer;
0221     m_lastHitIdx_before_bkwsearch = tc.lastCcIndex();
0222     m_nInsideMinusOneHits_before_bkwsearch = tc.nInsideMinusOneHits();
0223     m_nTailMinusOneHits_before_bkwsearch = tc.nTailMinusOneHits();
0224     tc.setLastCcIndex(0);
0225     tc.setNInsideMinusOneHits(0);
0226     tc.setNTailMinusOneHits(0);
0227   }
0228 
0229   void CombCandidate::repackCandPostBkwSearch(int i) {
0230     // Called during filtering following backward search when a TrackCand's
0231     // front hits need to be reindexed.
0232     // mergeCandsAndBestShortOne() has already been called (from MkBuilder::FindXxx()).
0233     // NOTES:
0234     // 1. Should only be called once for each i (flag/bit to allow multiple calls can be added).
0235     // 2. Alternatively, CombCand could provide hit iterator/exporter that would handle this correctly.
0236 
0237     TrackCand &tc = m_trk_cands[i];
0238 
0239     int curr_idx = tc.lastCcIndex();
0240     if (curr_idx != 0) {
0241       int last_idx = -1, prev_idx;
0242       do {
0243         prev_idx = m_hots[curr_idx].m_prev_idx;
0244 
0245         m_hots[curr_idx].m_prev_idx = last_idx;
0246 
0247         last_idx = curr_idx;
0248         curr_idx = prev_idx;
0249       } while (prev_idx != -1);
0250     }
0251 
0252     tc.setLastCcIndex(m_lastHitIdx_before_bkwsearch);
0253     tc.setNInsideMinusOneHits(m_nInsideMinusOneHits_before_bkwsearch + tc.nInsideMinusOneHits());
0254     tc.setNTailMinusOneHits(m_nTailMinusOneHits_before_bkwsearch + tc.nTailMinusOneHits());
0255   }
0256 
0257 }  // namespace mkfit