Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:22

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