CMS 3D CMS Logo

buildtestMPlex.cc
Go to the documentation of this file.
7 
8 #include "oneapi/tbb/parallel_for.h"
9 
10 #include <memory>
11 
12 namespace mkfit {
13 
14  inline bool sortByHitsChi2(const std::pair<Track, TrackState> &cand1, const std::pair<Track, TrackState> &cand2) {
15  if (cand1.first.nFoundHits() == cand2.first.nFoundHits())
16  return cand1.first.chi2() < cand2.first.chi2();
17 
18  return cand1.first.nFoundHits() > cand2.first.nFoundHits();
19  }
20 
21  inline bool sortByPhi(const Hit &hit1, const Hit &hit2) {
22  return std::atan2(hit1.y(), hit1.x()) < std::atan2(hit2.y(), hit2.x());
23  }
24 
25  inline bool sortByEta(const Hit &hit1, const Hit &hit2) { return hit1.eta() < hit2.eta(); }
26 
27  inline bool sortTracksByEta(const Track &track1, const Track &track2) { return track1.momEta() < track2.momEta(); }
28 
29  inline bool sortTracksByPhi(const Track &track1, const Track &track2) { return track1.momPhi() < track2.momPhi(); }
30 
32  const std::vector<std::vector<Track>> &m_track_candidates;
33 
34  sortTracksByPhiStruct(std::vector<std::vector<Track>> *track_candidates) : m_track_candidates(*track_candidates) {}
35 
36  bool operator()(const std::pair<int, int> &track1, const std::pair<int, int> &track2) {
37  return m_track_candidates[track1.first][track1.second].posPhi() <
38  m_track_candidates[track2.first][track2.second].posPhi();
39  }
40  };
41 
42  // within a layer with a "reasonable" geometry, ordering by Z is the same as eta
43  inline bool sortByZ(const Hit &hit1, const Hit &hit2) { return hit1.z() < hit2.z(); }
44 
45  //==============================================================================
46  // NaN and Silly track parameter check
47  //==============================================================================
48 
49  namespace {
50 
51  int check_nan_n_silly(TrackVec &tracks, const char *prefix) {
52  int count = 0;
53  for (auto &t : tracks) {
54  if (t.hasSillyValues(Const::nan_n_silly_print_bad_cands_bkfit, false, prefix)) {
55  ++count;
56  }
57  }
58  return count;
59  }
60 
61  void check_nan_n_silly_candidates(Event &ev) {
62  // MIMI -- nan_n_silly_per_layer_count is in MkBuilder, could be in MkJob.
63  // if (Const::nan_n_silly_check_cands_every_layer)
64  // {
65  // int sc = (int) ev.nan_n_silly_per_layer_count_;
66  // if (sc > 0)
67  // printf("Nan'n'Silly: Number of silly candidates over all layers = %d\n", sc);
68  // }
70  int sc = check_nan_n_silly(ev.candidateTracks_, "Pre-bkfit silly check");
71  if (sc > 0)
72  printf("Nan'n'Silly: Number of silly pre-bkfit candidates = %d\n", sc);
73  }
74  }
75 
76  void check_nan_n_silly_bkfit(Event &ev) {
78  int sc = check_nan_n_silly(ev.fitTracks_, "Post-bkfit silly check");
79  if (sc > 0)
80  printf("Nan'n'Silly: Number of silly post-bkfit candidates = %d\n", sc);
81  }
82  }
83 
84  } // namespace
85 
86  //==============================================================================
87  // runBuildTestPlexDumbCMSSW
88  //==============================================================================
89 
91  const IterationConfig &itconf = Config::ItrInfo[0];
92 
93  MkJob job({Config::TrkInfo, itconf, eoh, eoh.refBeamSpot()});
94 
95  builder.begin_event(&job, &ev, __func__);
96 
99  }
100 
101  builder.end_event();
102  }
103 
104  //==============================================================================
105  // runBuildTestPlexBestHit
106  //==============================================================================
107 
108  double runBuildingTestPlexBestHit(Event &ev, const EventOfHits &eoh, MkBuilder &builder) {
109  const IterationConfig &itconf = Config::ItrInfo[0];
110 
111  const bool validation_on = (Config::sim_val || Config::quality_val);
112 
113  if (validation_on) {
114  TrackVec seeds1;
115 
116  unsigned int algorithms[] = {4}; //only initialStep
117 
118  for (auto const &s : ev.seedTracks_) {
119  //keep seeds form the first iteration for processing
120  if (std::find(algorithms, algorithms + 1, s.algoint()) != algorithms + 1)
121  seeds1.push_back(s);
122  }
123  ev.seedTracks_.swap(seeds1); //necessary for the validation - PrepareSeeds
124  ev.relabel_bad_seedtracks(); //necessary for the validation - PrepareSeeds
125  }
126 
127  IterationMaskIfc mask_ifc;
128 
129  // To disable hit-masks, pass nullptr in place of &mask_ifc to MkJob ctor
130  // and optionally comment out ev.fill_hitmask_bool_vectors() call.
131 
132  ev.fill_hitmask_bool_vectors(itconf.m_track_algorithm, mask_ifc.m_mask_vector);
133 
134  MkJob job({Config::TrkInfo, itconf, eoh, eoh.refBeamSpot(), &mask_ifc});
135 
136  builder.begin_event(&job, &ev, __func__);
137 
138  bool seeds_sorted = false;
139  // CCCC builder.PrepareSeeds();
140 
141  // EventOfCandidates event_of_cands;
142  builder.find_tracks_load_seeds_BH(ev.seedTracks_, seeds_sorted);
143 
144 #ifdef USE_VTUNE_PAUSE
145  __SSC_MARK(0x111); // use this to resume Intel SDE at the same point
146  __itt_resume();
147 #endif
148 
149  double time = dtime();
150 
151  builder.findTracksBestHit();
152 
153  time = dtime() - time;
154 
155 #ifdef USE_VTUNE_PAUSE
156  __itt_pause();
157  __SSC_MARK(0x222); // use this to pause Intel SDE at the same point
158 #endif
159 
160  // Hack, get the tracks out.
161  ev.candidateTracks_ = builder.ref_tracks();
162 
163  // For best hit, the candidateTracks_ vector is the direct input to the backward fit so only need to do clean_duplicates once
165  //Mark tracks as duplicates; if within CMSSW, remove duplicate tracks before backward fit
166  // CCCC if (Config::removeDuplicates) {
167  // CCCC StdSeq::clean_duplicates(ev.candidateTracks_);
168  // CCCC }
169  }
170 
171  job.switch_to_backward();
172 
173  // now do backwards fit... do we want to time this section?
174  if (Config::backwardFit) {
175  builder.backwardFitBH();
176  ev.fitTracks_ = builder.ref_tracks();
177  }
178 
179  if (Config::quality_val) {
180  StdSeq::Quality qval;
181  qval.quality_val(&ev);
182  } else if (Config::sim_val || Config::cmssw_val) {
184  }
185 
186  builder.end_event();
187 
188  // ev.print_tracks(ev.candidateTracks_, true);
189 
190  return time;
191  }
192 
193  //==============================================================================
194  // runBuildTestPlex Combinatorial: Standard TBB
195  //==============================================================================
196 
197  double runBuildingTestPlexStandard(Event &ev, const EventOfHits &eoh, MkBuilder &builder) {
198  const IterationConfig &itconf = Config::ItrInfo[0];
199 
200  const bool validation_on = (Config::sim_val || Config::quality_val);
201 
202  if (validation_on) {
203  TrackVec seeds1;
204 
205  unsigned int algorithms[] = {4}; //only initialStep
206 
207  for (auto const &s : ev.seedTracks_) {
208  //keep seeds form the first iteration for processing
209  if (std::find(algorithms, algorithms + 1, s.algoint()) != algorithms + 1)
210  seeds1.push_back(s);
211  }
212  ev.seedTracks_.swap(seeds1); //necessary for the validation - PrepareSeeds
213  ev.relabel_bad_seedtracks(); //necessary for the validation - PrepareSeeds
214  }
215 
216  IterationMaskIfc mask_ifc;
217 
218  // To disable hit-masks, pass nullptr in place of &mask_ifc to MkJob ctor
219  // and optionally comment out ev.fill_hitmask_bool_vectors() call.
220 
221  ev.fill_hitmask_bool_vectors(itconf.m_track_algorithm, mask_ifc.m_mask_vector);
222 
223  MkJob job({Config::TrkInfo, itconf, eoh, eoh.refBeamSpot(), &mask_ifc});
224 
225  builder.begin_event(&job, &ev, __func__);
226 
227  bool seeds_sorted = false;
228  // CCCC builder.PrepareSeeds();
229 
230  builder.find_tracks_load_seeds(ev.seedTracks_, seeds_sorted);
231 
232 #ifdef USE_VTUNE_PAUSE
233  __SSC_MARK(0x111); // use this to resume Intel SDE at the same point
234  __itt_resume();
235 #endif
236 
237  double time = dtime();
238 
239  builder.findTracksStandard();
240 
241  time = dtime() - time;
242 
243 #ifdef USE_VTUNE_PAUSE
244  __itt_pause();
245  __SSC_MARK(0x222); // use this to pause Intel SDE at the same point
246 #endif
247 
248  check_nan_n_silly_candidates(ev);
249 
250  // first store candidate tracks
251  builder.export_best_comb_cands(ev.candidateTracks_);
252 
253  job.switch_to_backward();
254 
255  // now do backwards fit... do we want to time this section?
256  if (Config::backwardFit) {
257  // Using the TrackVec version until we home in on THE backward fit etc.
258  // builder.backwardFit();
259  builder.select_best_comb_cands();
260  builder.backwardFitBH();
261  ev.fitTracks_ = builder.ref_tracks();
262 
263  check_nan_n_silly_bkfit(ev);
264  }
265 
266  // CCCC StdSeq::handle_duplicates(&ev);
267 
268  if (Config::quality_val) {
269  StdSeq::Quality qval;
270  qval.quality_val(&ev);
271  } else if (Config::sim_val || Config::cmssw_val) {
273  }
274 
275  builder.end_event();
276 
277  // ev.print_tracks(ev.candidateTracks_, true);
278 
279  return time;
280  }
281 
282  //==============================================================================
283  // runBuildTestPlex Combinatorial: CloneEngine TBB
284  //==============================================================================
285 
287  const IterationConfig &itconf = Config::ItrInfo[0];
288 
289  const bool validation_on = (Config::sim_val || Config::quality_val);
290 
291  if (validation_on) {
292  TrackVec seeds1;
293 
294  unsigned int algorithms[] = {4}; //only initialStep
295 
296  for (auto const &s : ev.seedTracks_) {
297  //keep seeds form the first iteration for processing
298  if (std::find(algorithms, algorithms + 1, s.algoint()) != algorithms + 1)
299  seeds1.push_back(s);
300  }
301  ev.seedTracks_.swap(seeds1); //necessary for the validation - PrepareSeeds
302  ev.relabel_bad_seedtracks(); //necessary for the validation - PrepareSeeds
303  }
304 
305  IterationMaskIfc mask_ifc;
306 
307  // To disable hit-masks, pass nullptr in place of &mask_ifc to MkJob ctor
308  // and optionally comment out ev.fill_hitmask_bool_vectors() call.
309 
310  ev.fill_hitmask_bool_vectors(itconf.m_track_algorithm, mask_ifc.m_mask_vector);
311 
312  MkJob job({Config::TrkInfo, itconf, eoh, eoh.refBeamSpot(), &mask_ifc});
313 
314  builder.begin_event(&job, &ev, __func__);
315 
316  bool seeds_sorted = false;
317  // CCCC builder.PrepareSeeds();
318 
319  builder.find_tracks_load_seeds(ev.seedTracks_, seeds_sorted);
320 
321 #ifdef USE_VTUNE_PAUSE
322  __SSC_MARK(0x111); // use this to resume Intel SDE at the same point
323  __itt_resume();
324 #endif
325 
326  double time = dtime();
327 
328  builder.findTracksCloneEngine();
329 
330  time = dtime() - time;
331 
332 #ifdef USE_VTUNE_PAUSE
333  __itt_pause();
334  __SSC_MARK(0x222); // use this to pause Intel SDE at the same point
335 #endif
336 
337  check_nan_n_silly_candidates(ev);
338 
339  // first store candidate tracks - needed for BH backward fit and root_validation
340  builder.export_best_comb_cands(ev.candidateTracks_);
341 
342  job.switch_to_backward();
343 
344  // now do backwards fit... do we want to time this section?
345  if (Config::backwardFit) {
346  // a) TrackVec version:
347  builder.select_best_comb_cands();
348  builder.backwardFitBH();
349  ev.fitTracks_ = builder.ref_tracks();
350 
351  // b) Version that runs on CombCand / TrackCand
352  // builder.backwardFit();
353  // builder.quality_store_tracks(ev.fitTracks_);
354 
355  check_nan_n_silly_bkfit(ev);
356  }
357 
358  // CCCC StdSeq::handle_duplicates(&ev);
359 
360  // validation section
361  if (Config::quality_val) {
362  StdSeq::Quality qval;
363  qval.quality_val(&ev);
364  } else if (Config::sim_val || Config::cmssw_val) {
366  }
367 
368  builder.end_event();
369 
370  // ev.print_tracks(ev.candidateTracks_, true);
371 
372  return time;
373  }
374 
375  //==============================================================================
376  // runBtpCe_MultiIter
377  //
378  // Prototype for running multiple iterations, sequentially, using the same builder.
379  // For cmmsw seeds
380  //
381  // There is, in general, a mess in how tracks are processed, marked, or copied out
382  // in various validation scenarios and export flags.
383  //
384  // In particular, MkBuilder::PrepareSeeds does a lot of things to whole / complete
385  // event,seedTracks_ -- probably this would need to be split into common / and
386  // per-iteration part.
387  // - MkBuilder::prep_*** functions also mostly do not belong there (prep_sim is called from
388  // PrepareSeeds() for cmssw seeds).
389  //
390  // At this point we need to think about what should happen to Event before all the iterations and
391  // after all the iterations ... from the Validation perspective.
392  // And if we care about doing too muich work for seeds that will never get processed.
393  //==============================================================================
394 
395  std::vector<double> runBtpCe_MultiIter(Event &ev, const EventOfHits &eoh, MkBuilder &builder, int n) {
396  std::vector<double> timevec;
397  if (n <= 0)
398  return timevec;
399  timevec.resize(n + 1, 0.0);
400 
401  const bool validation_on = (Config::sim_val || Config::quality_val);
402 
403  TrackVec seeds_used;
404  TrackVec seeds1;
405 
406  unsigned int algorithms[] = {4, 22, 23, 5, 24, 7, 8, 9, 10, 6}; //9 iterations
407 
408  if (validation_on) {
409  for (auto const &s : ev.seedTracks_) {
410  //keep seeds form the first n iterations for processing
411  if (std::find(algorithms, algorithms + n, s.algoint()) != algorithms + n)
412  seeds1.push_back(s);
413  }
414  ev.seedTracks_.swap(seeds1); //necessary for the validation - PrepareSeeds
415  ev.relabel_bad_seedtracks(); //necessary for the validation - PrepareSeeds
416  }
417 
418  IterationMaskIfc mask_ifc;
419  TrackVec seeds;
420  TrackVec tmp_tvec;
421 
422  for (int it = 0; it <= n - 1; ++it) {
423  const IterationConfig &itconf = Config::ItrInfo[it];
424 
425  // To disable hit-masks, pass nullptr in place of &mask_ifc to MkJob ctor
426  // and optionally comment out ev.fill_hitmask_bool_vectors() call.
427 
428  ev.fill_hitmask_bool_vectors(itconf.m_track_algorithm, mask_ifc.m_mask_vector);
429 
430  MkJob job({Config::TrkInfo, itconf, eoh, eoh.refBeamSpot(), &mask_ifc});
431 
432  builder.begin_event(&job, &ev, __func__);
433 
434  { // We could partition seeds once, store beg, end for each iteration in a map or vector.
435  seeds.clear();
436  int nc = 0;
437  for (auto &s : ev.seedTracks_) {
438  if (s.algoint() == itconf.m_track_algorithm) {
439  if (itconf.m_requires_seed_hit_sorting) {
440  s.sortHitsByLayer();
441  }
442  seeds.push_back(s);
443  ++nc;
444  } else if (nc > 0)
445  break;
446  }
447  }
448 
449  bool do_seed_clean = bool(itconf.m_seed_cleaner);
450 
451  if (do_seed_clean)
452  itconf.m_seed_cleaner(seeds, itconf, eoh.refBeamSpot());
453 
454  builder.seed_post_cleaning(seeds);
455 
456  // Add protection in case no seeds are found for iteration
457  if (seeds.size() <= 0)
458  continue;
459 
460  builder.find_tracks_load_seeds(seeds, do_seed_clean);
461 
462  double time = dtime();
463 
464  builder.findTracksCloneEngine();
465 
466  timevec[it] = dtime() - time;
467  timevec[n] += timevec[it];
468 
469  // Print min and max size of hots vectors of CombCands.
470  // builder.find_min_max_hots_size();
471 
472  if (validation_on)
473  seeds_used.insert(seeds_used.end(), seeds.begin(), seeds.end()); //cleaned seeds need to be stored somehow
474 
475  if (itconf.m_pre_bkfit_filter) {
476  builder.filter_comb_cands(itconf.m_pre_bkfit_filter);
477  }
478 
479  builder.select_best_comb_cands();
480 
481  {
482  builder.export_tracks(tmp_tvec);
483  if (itconf.m_duplicate_cleaner)
484  itconf.m_duplicate_cleaner(builder.ref_tracks_nc(), itconf);
485  ev.candidateTracks_.reserve(ev.candidateTracks_.size() + tmp_tvec.size());
486  for (auto &&t : tmp_tvec)
487  ev.candidateTracks_.emplace_back(std::move(t));
488  tmp_tvec.clear();
489  }
490 
491  job.switch_to_backward();
492 
493  // now do backwards fit... do we want to time this section?
494  if (Config::backwardFit) {
495  // a) TrackVec version:
496  // builder.backwardFitBH();
497 
498  // b) Version that runs on CombCand / TrackCand
499  const bool do_backward_search = Config::backwardSearch && itconf.m_backward_search;
500 
501  // We copy seed-hits into Candidates ... now we have to remove them so backward fit stops
502  // before reaching seeding region. Ideally, we wouldn't add them in the first place but
503  // if we want to export full tracks above we need to hold on to them (alternatively, we could
504  // have a pointer to seed track in CombCandidate and copy them from there).
505  if (do_backward_search) {
507  }
508 
509  builder.backwardFit();
510 
511  if (do_backward_search) {
512  builder.beginBkwSearch();
514  builder.endBkwSearch();
515  }
516 
517  if (itconf.m_post_bkfit_filter) {
518  builder.filter_comb_cands(itconf.m_post_bkfit_filter);
519  }
520 
521  builder.filter_comb_cands(StdSeq::qfilter_nan_n_silly<TrackCand>);
522 
523  builder.select_best_comb_cands(true); // true -> clear m_tracks as they were already filled once above
524 
525  if (itconf.m_duplicate_cleaner)
526  itconf.m_duplicate_cleaner(builder.ref_tracks_nc(), itconf);
527 
528  builder.export_tracks(ev.fitTracks_);
529  }
530 
531  builder.end_event();
532  }
533 
534  // MIMI - Fake back event pointer for final processing (that should be done elsewhere)
535  MkJob job({Config::TrkInfo, Config::ItrInfo[0], eoh, eoh.refBeamSpot()});
536  builder.begin_event(&job, &ev, __func__);
537 
538  if (validation_on) {
540  //swap for the cleaned seeds
541  ev.seedTracks_.swap(seeds_used);
542  }
543 
544  check_nan_n_silly_candidates(ev);
545 
547  check_nan_n_silly_bkfit(ev);
548 
549  // validation section
550  if (Config::quality_val) {
551  StdSeq::Quality qval;
552  qval.quality_val(&ev);
553  } else if (Config::sim_val || Config::cmssw_val) {
555  }
556 
557  // ev.print_tracks(ev.candidateTracks_, true);
558 
559  // MIMI Unfake.
560  builder.end_event();
561 
562  // In CMSSW runOneIter we now release memory for comb-cands:
563  builder.release_memory();
564 
565  return timevec;
566  }
567 
568 } // end namespace mkfit
float x() const
Definition: Hit.h:162
void export_tracks(TrackVec &out_vec)
Definition: MkBuilder.cc:387
void findTracksBestHit(SteeringParams::IterationType_e iteration_dir=SteeringParams::IT_FwdSearch)
Definition: MkBuilder.cc:464
const BeamSpot & refBeamSpot() const
constexpr bool nan_n_silly_check_cands_post_bkfit
Definition: Config.h:63
constexpr bool nan_n_silly_print_bad_cands_bkfit
Definition: Config.h:64
double runBuildingTestPlexStandard(Event &ev, const EventOfHits &eoh, MkBuilder &builder)
constexpr bool nan_n_silly_check_cands_pre_bkfit
Definition: Config.h:62
void compactifyHitStorageForBestCand(bool remove_seed_hits, int backward_fit_min_hits)
Definition: MkBuilder.h:66
bool sortTracksByPhi(const Track &track1, const Track &track2)
void root_val(Event *event)
void findTracksCloneEngine(SteeringParams::IterationType_e iteration_dir=SteeringParams::IT_FwdSearch)
Definition: MkBuilder.cc:927
void begin_event(MkJob *job, Event *ev, const char *build_type)
Definition: MkBuilder.cc:197
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
bool sortByHitsChi2(const std::pair< Track, TrackState > &cand1, const std::pair< Track, TrackState > &cand2)
clean_seeds_func m_seed_cleaner
void backwardFitBH()
Definition: MkBuilder.cc:1160
int filter_comb_cands(IterationConfig::filter_candidates_func filter)
Definition: MkBuilder.cc:304
void prep_simtracks(Event *event)
float momEta() const
Definition: Track.h:173
void find_tracks_load_seeds_BH(const TrackVec &in_seeds, const bool seeds_sorted)
Definition: MkBuilder.cc:449
void findTracksStandard(SteeringParams::IterationType_e iteration_dir=SteeringParams::IT_FwdSearch)
Definition: MkBuilder.cc:748
const std::vector< std::vector< Track > > & m_track_candidates
void find_tracks_load_seeds(const TrackVec &in_seeds, const bool seeds_sorted)
Definition: MkBuilder.cc:613
sortTracksByPhiStruct(std::vector< std::vector< Track >> *track_candidates)
void release_memory()
Definition: MkBuilder.cc:225
float eta() const
Definition: Hit.h:169
TrackVec & ref_tracks_nc()
Definition: MkBuilder.h:75
TrackerInfo TrkInfo
void seed_post_cleaning(TrackVec &tv)
Definition: MkBuilder.cc:398
void select_best_comb_cands(bool clear_m_tracks=false, bool remove_missing_hits=false)
Definition: MkBuilder.cc:366
void runBuildingTestPlexDumbCMSSW(Event &ev, const EventOfHits &eoh, MkBuilder &builder)
IterationsInfo ItrInfo
const TrackVec & ref_tracks() const
Definition: MkBuilder.h:74
filter_candidates_func m_post_bkfit_filter
float y() const
Definition: Hit.h:163
bool sortByZ(const Hit &hit1, const Hit &hit2)
std::vector< Track > TrackVec
double runBuildingTestPlexCloneEngine(Event &ev, const EventOfHits &eoh, MkBuilder &builder)
auto const & tracks
cannot be loose
clean_duplicates_func m_duplicate_cleaner
void beginBkwSearch()
Definition: MkBuilder.h:70
double dtime()
std::vector< double > runBtpCe_MultiIter(Event &ev, const EventOfHits &eoh, MkBuilder &builder, int n)
void export_best_comb_cands(TrackVec &out_vec, bool remove_missing_hits=false)
Definition: MkBuilder.cc:372
bool operator()(const std::pair< int, int > &track1, const std::pair< int, int > &track2)
float z() const
Definition: Hit.h:164
void endBkwSearch()
Definition: MkBuilder.h:71
void root_val_dumb_cmssw(Event *event)
void quality_val(Event *event)
bool sortTracksByEta(const Track &track1, const Track &track2)
std::vector< std::vector< bool > > m_mask_vector
bool sortByPhi(const Hit &hit1, const Hit &hit2)
filter_candidates_func m_pre_bkfit_filter
float momPhi() const
Definition: Track.h:172
def move(src, dest)
Definition: eostools.py:511
double runBuildingTestPlexBestHit(Event &ev, const EventOfHits &eoh, MkBuilder &builder)
bool sortByEta(const Hit &hit1, const Hit &hit2)