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