CMS 3D CMS Logo

MkFinder.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_src_MkFinder_h
2 #define RecoTracker_MkFitCore_src_MkFinder_h
3 
4 #include "MkBase.h"
7 
10 
11 // Define to get printouts about track and hit chi2.
12 // See also MkBuilder::backwardFit().
13 
14 //#define DEBUG_BACKWARD_FIT_BH
15 //#define DEBUG_BACKWARD_FIT
16 
17 namespace mkfit {
18 
19  class CandCloner;
20  class CombCandidate;
21  class LayerOfHits;
22  class FindingFoos;
23  class IterationParams;
24  class IterationLayerConfig;
25  class SteeringParams;
26 
27  class Event;
28 
29  struct UpdateIndices {
30  int seed_idx;
31  int cand_idx;
32  int hit_idx;
33  int ovlp_idx;
34 
35  UpdateIndices(int si, int ci, int hi, int oi) : seed_idx(si), cand_idx(ci), hit_idx(hi), ovlp_idx(oi) {}
36  };
37 
38  class MkFinder : public MkBase {
39  friend class MkBuilder;
40 
41  public:
42  static constexpr int MPlexHitIdxMax = 16;
43 
44  using MPlexHitIdx = Matriplex::Matriplex<int, MPlexHitIdxMax, 1, NN>;
45  using MPlexQHoT = Matriplex::Matriplex<HitOnTrack, 1, 1, NN>;
46 
47  //----------------------------------------------------------------------------
48 
49  MkFinder() {}
50 
51  void setup(const PropagationConfig &pc,
52  const IterationConfig &ic,
53  const IterationParams &ip,
54  const IterationLayerConfig &ilc,
55  const SteeringParams &sp,
56  const std::vector<bool> *ihm,
57  const Event *ev,
58  int region,
59  bool infwd);
60  void setup_bkfit(const PropagationConfig &pc, const SteeringParams &sp, const Event *ev);
61  void release();
62 
63  void begin_layer(const LayerOfHits &layer_of_hits);
64  void end_layer();
65 
66  //----------------------------------------------------------------------------
67 
68  void inputTracksAndHitIdx(const std::vector<Track> &tracks, int beg, int end, bool inputProp);
69 
70  void inputTracksAndHitIdx(const std::vector<Track> &tracks,
71  const std::vector<int> &idxs,
72  int beg,
73  int end,
74  bool inputProp,
75  int mp_offset);
76 
77  void inputTracksAndHitIdx(const std::vector<CombCandidate> &tracks,
78  const std::vector<std::pair<int, int>> &idxs,
79  int beg,
80  int end,
81  bool inputProp);
82 
83  void inputTracksAndHits(const std::vector<CombCandidate> &tracks,
84  const LayerOfHits &layer_of_hits,
85  const std::vector<UpdateIndices> &idxs,
86  int beg,
87  int end,
88  bool inputProp);
89  void inputOverlapHits(const LayerOfHits &layer_of_hits, const std::vector<UpdateIndices> &idxs, int beg, int end);
90 
91  void inputTracksAndHitIdx(const std::vector<CombCandidate> &tracks,
92  const std::vector<std::pair<int, IdxChi2List>> &idxs,
93  int beg,
94  int end,
95  bool inputProp);
96 
97  void outputTracksAndHitIdx(std::vector<Track> &tracks, int beg, int end, bool outputProp) const;
98 
100  std::vector<Track> &tracks, const std::vector<int> &idxs, int beg, int end, bool outputProp) const;
101 
102  void outputTrackAndHitIdx(Track &track, int itrack, bool outputProp) const {
103  const int iO = outputProp ? iP : iC;
104  copy_out(track, itrack, iO);
105  }
106 
108  std::vector<Track> &tracks, const std::vector<int> &idxs, int beg, int end, bool outputProp) const {
109  const int iO = outputProp ? iP : iC;
110  for (int i = beg, imp = 0; i < end; ++i, ++imp) {
111  if (!m_Stopped[imp])
112  copy_out(tracks[idxs[i]], imp, iO);
113  }
114  }
115 
116  HitOnTrack bestHitLastHoT(int itrack) const { return m_HoTArrs[itrack][m_NHits(itrack, 0, 0) - 1]; }
117 
119  const LayerOfHits &layer_of_hits, int hit_cnt, MPlexHV &norm, MPlexHV &dir, MPlexHV &pnt, int N_proc) const;
120 
121  //----------------------------------------------------------------------------
122 
124  const float invpt, const float theta, float &min_dq, float &max_dq, float &min_dphi, float &max_dphi);
125 
126  float getHitSelDynamicChi2Cut(const int itrk, const int ipar);
127 
128  void selectHitIndices(const LayerOfHits &layer_of_hits, const int N_proc, bool fill_binsearch_only = false);
129  void selectHitIndicesV2(const LayerOfHits &layer_of_hits, const int N_proc);
130 
131  void addBestHit(const LayerOfHits &layer_of_hits, const int N_proc, const FindingFoos &fnd_foos);
132 
133  //----------------------------------------------------------------------------
134 
135  void findCandidates(const LayerOfHits &layer_of_hits,
136  std::vector<std::vector<TrackCand>> &tmp_candidates,
137  const int offset,
138  const int N_proc,
139  const FindingFoos &fnd_foos);
140 
141  //----------------------------------------------------------------------------
142 
143  void findCandidatesCloneEngine(const LayerOfHits &layer_of_hits,
144  CandCloner &cloner,
145  const int offset,
146  const int N_proc,
147  const FindingFoos &fnd_foos);
148 
149  void updateWithLoadedHit(int N_proc, const LayerOfHits &layer_of_hits, const FindingFoos &fnd_foos);
150 
151  void chi2OfLoadedHit(int N_proc, const FindingFoos &fnd_foos);
152 
153  void copyOutParErr(std::vector<CombCandidate> &seed_cand_vec, int N_proc, bool outputProp) const;
154 
155  //----------------------------------------------------------------------------
156  // Backward fit
157 
158  void bkFitInputTracks(TrackVec &cands, int beg, int end);
159  void bkFitOutputTracks(TrackVec &cands, int beg, int end, bool outputProp);
160 
161  void bkFitInputTracks(EventOfCombCandidates &eocss, int beg, int end);
162  void bkFitOutputTracks(EventOfCombCandidates &eocss, int beg, int end, bool outputProp);
163 
164  void bkFitFitTracksBH(const EventOfHits &eventofhits,
165  const SteeringParams &st_par,
166  const int N_proc,
167  bool chiDebug = false);
168 
169  void bkFitFitTracks(const EventOfHits &eventofhits,
170  const SteeringParams &st_par,
171  const int N_proc,
172  bool chiDebug = false);
173 
174  void bkFitPropTracksToPCA(const int N_proc);
175 
176  //----------------------------------------------------------------------------
177 
178  private:
179  void copy_in(const Track &trk, const int mslot, const int tslot) {
180  m_Err[tslot].copyIn(mslot, trk.errors().Array());
181  m_Par[tslot].copyIn(mslot, trk.parameters().Array());
182 
183  m_Chg(mslot, 0, 0) = trk.charge();
184  m_Chi2(mslot, 0, 0) = trk.chi2();
185  m_Label(mslot, 0, 0) = trk.label();
186 
187  m_NHits(mslot, 0, 0) = trk.nTotalHits();
188  m_NFoundHits(mslot, 0, 0) = trk.nFoundHits();
189 
190  m_NInsideMinusOneHits(mslot, 0, 0) = trk.nInsideMinusOneHits();
191  m_NTailMinusOneHits(mslot, 0, 0) = trk.nTailMinusOneHits();
192 
193  std::copy(trk.beginHitsOnTrack(), trk.endHitsOnTrack(), m_HoTArrs[mslot]);
194  }
195 
196  void copy_out(Track &trk, const int mslot, const int tslot) const {
197  m_Err[tslot].copyOut(mslot, trk.errors_nc().Array());
198  m_Par[tslot].copyOut(mslot, trk.parameters_nc().Array());
199 
200  trk.setCharge(m_Chg(mslot, 0, 0));
201  trk.setChi2(m_Chi2(mslot, 0, 0));
202  trk.setLabel(m_Label(mslot, 0, 0));
203 
204  trk.resizeHits(m_NHits(mslot, 0, 0), m_NFoundHits(mslot, 0, 0));
205  std::copy(m_HoTArrs[mslot], &m_HoTArrs[mslot][m_NHits(mslot, 0, 0)], trk.beginHitsOnTrack_nc());
206  }
207 
208  void copy_in(const TrackCand &trk, const int mslot, const int tslot) {
209  m_Err[tslot].copyIn(mslot, trk.errors().Array());
210  m_Par[tslot].copyIn(mslot, trk.parameters().Array());
211 
212  m_Chg(mslot, 0, 0) = trk.charge();
213  m_Chi2(mslot, 0, 0) = trk.chi2();
214  m_Label(mslot, 0, 0) = trk.label();
215 
216  m_LastHitCcIndex(mslot, 0, 0) = trk.lastCcIndex();
217  m_NFoundHits(mslot, 0, 0) = trk.nFoundHits();
218  m_NMissingHits(mslot, 0, 0) = trk.nMissingHits();
219  m_NOverlapHits(mslot, 0, 0) = trk.nOverlapHits();
220 
221  m_NInsideMinusOneHits(mslot, 0, 0) = trk.nInsideMinusOneHits();
222  m_NTailMinusOneHits(mslot, 0, 0) = trk.nTailMinusOneHits();
223 
224  m_CombCand[mslot] = trk.combCandidate();
225  m_TrkStatus[mslot] = trk.getStatus();
226  }
227 
228  void copy_out(TrackCand &trk, const int mslot, const int tslot) const {
229  m_Err[tslot].copyOut(mslot, trk.errors_nc().Array());
230  m_Par[tslot].copyOut(mslot, trk.parameters_nc().Array());
231 
232  trk.setCharge(m_Chg(mslot, 0, 0));
233  trk.setChi2(m_Chi2(mslot, 0, 0));
234  trk.setLabel(m_Label(mslot, 0, 0));
235 
236  trk.setLastCcIndex(m_LastHitCcIndex(mslot, 0, 0));
237  trk.setNFoundHits(m_NFoundHits(mslot, 0, 0));
238  trk.setNMissingHits(m_NMissingHits(mslot, 0, 0));
239  trk.setNOverlapHits(m_NOverlapHits(mslot, 0, 0));
240 
241  trk.setNInsideMinusOneHits(m_NInsideMinusOneHits(mslot, 0, 0));
242  trk.setNTailMinusOneHits(m_NTailMinusOneHits(mslot, 0, 0));
243 
244  trk.setCombCandidate(m_CombCand[mslot]);
245  trk.setStatus(m_TrkStatus[mslot]);
246  }
247 
248  void add_hit(const int mslot, int index, int layer) {
249  // Only used by BestHit.
250  // m_NInsideMinusOneHits and m_NTailMinusOneHits are maintained here but are
251  // not used and are not copied out (as Track does not have these members).
252 
253  int &n_tot_hits = m_NHits(mslot, 0, 0);
254  int &n_fnd_hits = m_NFoundHits(mslot, 0, 0);
255 
256  if (n_tot_hits < Config::nMaxTrkHits) {
257  m_HoTArrs[mslot][n_tot_hits++] = {index, layer};
258  if (index >= 0) {
259  ++n_fnd_hits;
260  m_NInsideMinusOneHits(mslot, 0, 0) += m_NTailMinusOneHits(mslot, 0, 0);
261  m_NTailMinusOneHits(mslot, 0, 0) = 0;
262  } else if (index == -1) {
263  ++m_NTailMinusOneHits(mslot, 0, 0);
264  }
265  } else {
266  // printf("WARNING MkFinder::add_hit hit-on-track limit reached for label=%d\n", label_);
267 
268  const int LH = Config::nMaxTrkHits - 1;
269 
270  if (index >= 0) {
271  if (m_HoTArrs[mslot][LH].index < 0)
272  ++n_fnd_hits;
273  m_HoTArrs[mslot][LH] = {index, layer};
274  } else if (index == -2) {
275  if (m_HoTArrs[mslot][LH].index >= 0)
276  --n_fnd_hits;
277  m_HoTArrs[mslot][LH] = {index, layer};
278  }
279  }
280  }
281 
282  int num_all_minus_one_hits(const int mslot) const {
283  return m_NInsideMinusOneHits(mslot, 0, 0) + m_NTailMinusOneHits(mslot, 0, 0);
284  }
285 
286  int num_inside_minus_one_hits(const int mslot) const { return m_NInsideMinusOneHits(mslot, 0, 0); }
287 
288  void print_par_err(int corp, int mslot) const;
289 
290  //----------------------------------------------------------------------------
291 
293  MPlexQI m_Label; // seed index in global seed vector (for MC truth match)
294 
297 
299 
300  MPlexQI m_SeedIdx; // seed index in local thread (for bookkeeping at thread level)
301  MPlexQI m_CandIdx; // candidate index for the given seed (for bookkeeping of clone engine)
302  MPlexQI m_SeedOriginIdx; // seed index in MkBuilder seed input vector
303 
304  MPlexQI m_Stopped; // Flag for BestHit that a track has been stopped (and copied out already)
305 
306  // Additions / substitutions for TrackCand copy_in/out()
307  // One could really access the original TrackCand for all of those, especially the ones that
308  // are STD only. This then requires access back to that TrackCand memory.
309  // So maybe one should just have flags for CopyIn methods (or several versions). Yay, etc.
310  MPlexQI m_NMissingHits; // sub: m_NHits, sort of, STD only
311  MPlexQI m_NOverlapHits; // add: num of overlaps registered in HitOnTrack, STD only
312  MPlexQI m_NInsideMinusOneHits; // sub: before we copied all hit idcs and had a loop counting them only
313  MPlexQI m_NTailMinusOneHits; // sub: before we copied all hit idcs and had a loop counting them only
314  MPlexQI m_LastHitCcIndex; // add: index of last hit in m_CombCand hit tree, STD only
315  TrackBase::Status m_TrkStatus[NN]; // STD only, status bits
317  // const TrackCand *m_TrkCand[NN]; // hmmh, could get all data through this guy ... but scattered
318  // storing it in now for bkfit debug printouts
320 
321  // Hit indices into LayerOfHits to explore.
322  WSR_Result m_XWsrResult[NN]; // Could also merge it with m_XHitSize. Or use smaller arrays.
325 
326  // Hit errors / parameters for hit matching, update.
329 
330  // An idea: Do propagation to hit in FindTracksXYZZ functions.
331  // Have some state / functions here that make this short to write.
332  // This would simplify KalmanUtils (remove the propagate functions).
333  // Track errors / parameters propagated to current hit.
334  // MPlexLS candErrAtCurrHit;
335  // MPlexLV candParAtCurrHit;
336 
342  const std::vector<bool> *m_iteration_hit_mask = nullptr;
343  const Event *m_event = nullptr;
345  bool m_in_fwd = true;
346 
347  // Backward fit
348  int m_CurHit[NN];
350  int m_CurNode[NN];
352  };
353 
354 } // end namespace mkfit
355 #endif
MPlexQI m_Chg
Definition: MkBase.h:103
static constexpr int iC
Definition: MkBase.h:18
void copy_in(const Track &trk, const int mslot, const int tslot)
Definition: MkFinder.h:179
static constexpr int MPlexHitIdxMax
Definition: MkFinder.h:42
MPlexQI m_NFoundHits
Definition: MkFinder.h:296
const IterationLayerConfig * m_iteration_layer_config
Definition: MkFinder.h:340
void chi2OfLoadedHit(int N_proc, const FindingFoos &fnd_foos)
Definition: MkFinder.cc:1913
const HitOnTrack * m_HoTArr[NN]
Definition: MkFinder.h:349
static constexpr int iP
Definition: MkBase.h:19
void inputOverlapHits(const LayerOfHits &layer_of_hits, const std::vector< UpdateIndices > &idxs, int beg, int end)
Definition: MkFinder.cc:181
void copy_out(Track &trk, const int mslot, const int tslot) const
Definition: MkFinder.h:196
void packModuleNormDirPnt(const LayerOfHits &layer_of_hits, int hit_cnt, MPlexHV &norm, MPlexHV &dir, MPlexHV &pnt, int N_proc) const
Definition: MkFinder.cc:240
void outputTracksAndHitIdx(std::vector< Track > &tracks, int beg, int end, bool outputProp) const
Definition: MkFinder.cc:217
HitOnTrack m_HoTArrs[NN][Config::nMaxTrkHits]
Definition: MkFinder.h:298
void inputTracksAndHitIdx(const std::vector< Track > &tracks, int beg, int end, bool inputProp)
Definition: MkFinder.cc:99
void bkFitInputTracks(TrackVec &cands, int beg, int end)
Definition: MkFinder.cc:1960
const Event * m_event
Definition: MkFinder.h:343
Matriplex::Matriplex< float, HH, 1, NN > MPlexHV
Definition: Matrix.h:58
MPlexLV m_Par[2]
Definition: MkBase.h:102
MPlexQI m_NMissingHits
Definition: MkFinder.h:310
Trktree trk
Definition: Trktree.cc:2
void selectHitIndices(const LayerOfHits &layer_of_hits, const int N_proc, bool fill_binsearch_only=false)
Definition: MkFinder.cc:320
void setup(const PropagationConfig &pc, const IterationConfig &ic, const IterationParams &ip, const IterationLayerConfig &ilc, const SteeringParams &sp, const std::vector< bool > *ihm, const Event *ev, int region, bool infwd)
Definition: MkFinder.cc:30
void bkFitPropTracksToPCA(const int N_proc)
Definition: MkFinder.cc:2446
const IterationConfig * m_iteration_config
Definition: MkFinder.h:338
void inputTracksAndHits(const std::vector< CombCandidate > &tracks, const LayerOfHits &layer_of_hits, const std::vector< UpdateIndices > &idxs, int beg, int end, bool inputProp)
Definition: MkFinder.cc:149
void release()
Definition: MkFinder.cc:56
void add_hit(const int mslot, int index, int layer)
Definition: MkFinder.h:248
UpdateIndices(int si, int ci, int hi, int oi)
Definition: MkFinder.h:35
void copy_out(TrackCand &trk, const int mslot, const int tslot) const
Definition: MkFinder.h:228
const IterationParams * m_iteration_params
Definition: MkFinder.h:339
MPlexLS m_Err[2]
Definition: MkBase.h:101
void findCandidates(const LayerOfHits &layer_of_hits, std::vector< std::vector< TrackCand >> &tmp_candidates, const int offset, const int N_proc, const FindingFoos &fnd_foos)
Definition: MkFinder.cc:1371
int m_current_region
Definition: MkFinder.h:344
const PropagationConfig * m_prop_config
Definition: MkFinder.h:337
int num_all_minus_one_hits(const int mslot) const
Definition: MkFinder.h:282
Definition: EPCuts.h:4
void outputNonStoppedTracksAndHitIdx(std::vector< Track > &tracks, const std::vector< int > &idxs, int beg, int end, bool outputProp) const
Definition: MkFinder.h:107
constexpr Matriplex::idx_t NN
Definition: Matrix.h:48
TrackBase::Status m_TrkStatus[NN]
Definition: MkFinder.h:315
void setup_bkfit(const PropagationConfig &pc, const SteeringParams &sp, const Event *ev)
Definition: MkFinder.cc:50
LH
angular window limits for traco
Matriplex::Matriplex< int, MPlexHitIdxMax, 1, NN > MPlexHitIdx
Definition: MkFinder.h:44
MPlexQI m_XHitSize
Definition: MkFinder.h:323
MPlexHV m_msPar
Definition: MkFinder.h:328
MPlexQI m_NTailMinusOneHits
Definition: MkFinder.h:313
MPlexQI m_SeedOriginIdx
Definition: MkFinder.h:302
int m_CurNode[NN]
Definition: MkFinder.h:350
MPlexQI m_NHits
Definition: MkFinder.h:295
void begin_layer(const LayerOfHits &layer_of_hits)
Definition: MkFinder.cc:68
MPlexHS m_msErr
Definition: MkFinder.h:327
Matriplex::Matriplex< int, 1, 1, NN > MPlexQI
Definition: Matrix.h:81
TrackCand * m_TrkCand[NN]
Definition: MkFinder.h:319
void updateWithLoadedHit(int N_proc, const LayerOfHits &layer_of_hits, const FindingFoos &fnd_foos)
Definition: MkFinder.cc:1869
MPlexQI m_LastHitCcIndex
Definition: MkFinder.h:314
void selectHitIndicesV2(const LayerOfHits &layer_of_hits, const int N_proc)
Definition: MkFinder.cc:760
std::vector< Track > TrackVec
CombCandidate * m_CombCand[NN]
Definition: MkFinder.h:316
Matriplex::Matriplex< float, 1, 1, NN > MPlexQF
Definition: Matrix.h:80
void bkFitFitTracks(const EventOfHits &eventofhits, const SteeringParams &st_par, const int N_proc, bool chiDebug=false)
Definition: MkFinder.cc:2218
int m_CurHit[NN]
Definition: MkFinder.h:348
const SteeringParams * m_steering_params
Definition: MkFinder.h:341
MPlexQI m_NOverlapHits
Definition: MkFinder.h:311
MPlexHitIdx m_XHitArr
Definition: MkFinder.h:324
Matriplex::MatriplexSym< float, HH, NN > MPlexHS
Definition: Matrix.h:59
MPlexQF m_Chi2
Definition: MkFinder.h:292
void bkFitOutputTracks(TrackVec &cands, int beg, int end, bool outputProp)
Definition: MkFinder.cc:2017
void bkFitFitTracksBH(const EventOfHits &eventofhits, const SteeringParams &st_par, const int N_proc, bool chiDebug=false)
Definition: MkFinder.cc:2065
MPlexQI m_CandIdx
Definition: MkFinder.h:301
MPlexQI m_SeedIdx
Definition: MkFinder.h:300
void copyOutParErr(std::vector< CombCandidate > &seed_cand_vec, int N_proc, bool outputProp) const
Definition: MkFinder.cc:1937
int num_inside_minus_one_hits(const int mslot) const
Definition: MkFinder.h:286
void findCandidatesCloneEngine(const LayerOfHits &layer_of_hits, CandCloner &cloner, const int offset, const int N_proc, const FindingFoos &fnd_foos)
Definition: MkFinder.cc:1644
void copy_in(const TrackCand &trk, const int mslot, const int tslot)
Definition: MkFinder.h:208
MPlexQI m_NInsideMinusOneHits
Definition: MkFinder.h:312
HitOnTrack bestHitLastHoT(int itrack) const
Definition: MkFinder.h:116
const std::vector< bool > * m_iteration_hit_mask
Definition: MkFinder.h:342
Matriplex::Matriplex< HitOnTrack, 1, 1, NN > MPlexQHoT
Definition: MkFinder.h:45
float getHitSelDynamicChi2Cut(const int itrk, const int ipar)
Definition: MkFinder.cc:297
const HoTNode * m_HoTNodeArr[NN]
Definition: MkFinder.h:351
WSR_Result m_XWsrResult[NN]
Definition: MkFinder.h:322
void getHitSelDynamicWindows(const float invpt, const float theta, float &min_dq, float &max_dq, float &min_dphi, float &max_dphi)
Definition: MkFinder.cc:266
MPlexQI m_Stopped
Definition: MkFinder.h:304
constexpr int nMaxTrkHits
Definition: Config.h:43
MPlexQI m_Label
Definition: MkFinder.h:293
void outputTrackAndHitIdx(Track &track, int itrack, bool outputProp) const
Definition: MkFinder.h:102
void end_layer()
Definition: MkFinder.cc:88
void print_par_err(int corp, int mslot) const
Definition: MkFinder.cc:2202
void addBestHit(const LayerOfHits &layer_of_hits, const int N_proc, const FindingFoos &fnd_foos)
Definition: MkFinder.cc:1155