CMS 3D CMS Logo

MkStandaloneSeqs.cc
Go to the documentation of this file.
3 
7 
9 
10 #include "oneapi/tbb/parallel_for.h"
11 
12 namespace mkfit {
13 
14  namespace StdSeq {
15 
16  //=========================================================================
17  // Hit processing
18  //=========================================================================
19 
21  eoh.reset();
22 
23  // fill vector of hits in each layer
24  // XXXXMT: Does it really makes sense to multi-thread this?
25  TBB_PARALLEL_FOR(tbb::blocked_range<int>(0, ev.layerHits_.size()), [&](const tbb::blocked_range<int> &layers) {
26  for (int ilay = layers.begin(); ilay < layers.end(); ++ilay) {
27  eoh.suckInHits(ilay, ev.layerHits_[ilay]);
28  }
29  });
30  eoh.setBeamSpot(ev.beamSpot_);
31  }
32 
34  /*
35  // Mark tracks as duplicates; if within CMSSW, remove duplicate tracks from fit or candidate track collection
36  if (Config::removeDuplicates) {
37  if (Config::quality_val || Config::sim_val || Config::cmssw_val) {
38  clean_duplicates(event->candidateTracks_);
39  if (Config::backwardFit)
40  clean_duplicates(event->fitTracks_);
41  }
42  // For the MEIF benchmarks and the stress tests, no validation flags are set so we will enter this block
43  else {
44  // Only care about the candidate tracks here; no need to run the duplicate removal on both candidate and fit tracks
45  clean_duplicates(event->candidateTracks_);
46  }
47  }
48  */
49  }
50 
51  //=========================================================================
52  // Random stuff
53  //=========================================================================
54 
56  // Ripped out of MkBuilder::begin_event, ifdefed under DEBUG
57 
58  std::vector<Track> &simtracks = event->simTracks_;
59 
60  for (int itrack = 0; itrack < (int)simtracks.size(); ++itrack) {
61  // bool debug = true;
62  Track track = simtracks[itrack];
63  // simtracks are initially written with label = index; uncomment in case tracks were edited
64  // if (track.label() != itrack) {
65  // dprintf("Bad label for simtrack %d -- %d\n", itrack, track.label());
66  // }
67 
68  dprint("MX - simtrack with nHits=" << track.nFoundHits() << " chi2=" << track.chi2() << " pT=" << track.pT()
69  << " phi=" << track.momPhi() << " eta=" << track.momEta());
70  }
71 
72  for (int itrack = 0; itrack < (int)simtracks.size(); ++itrack) {
73  for (int ihit = 0; ihit < simtracks[itrack].nFoundHits(); ++ihit) {
74  dprint("track #" << itrack << " hit #" << ihit
75  << " hit pos=" << simtracks[itrack].hitsVector(event->layerHits_)[ihit].position()
76  << " phi=" << simtracks[itrack].hitsVector(event->layerHits_)[ihit].phi());
77  }
78  }
79  }
80 
81  void track_print(Event *event, const Track &t, const char *pref) {
82  printf("%s with q=%+i pT=%7.3f eta=% 7.3f nHits=%2d label=%4d\nState:\n",
83  pref,
84  t.charge(),
85  t.pT(),
86  t.momEta(),
87  t.nFoundHits(),
88  t.label());
89 
90  print(t.state());
91 
92  printf("Hits:\n");
93  for (int ih = 0; ih < t.nTotalHits(); ++ih) {
94  int lyr = t.getHitLyr(ih);
95  int idx = t.getHitIdx(ih);
96  if (idx >= 0) {
97  const Hit &hit = event->layerHits_[lyr][idx];
98  printf(" hit %2d lyr=%2d idx=%4d pos r=%7.3f z=% 8.3f mc_hit=%4d mc_trk=%4d\n",
99  ih,
100  lyr,
101  idx,
102  hit.r(),
103  hit.z(),
104  hit.mcHitID(),
105  hit.mcTrackID(event->simHitsInfo_));
106  } else
107  printf(" hit %2d idx=%i\n", ih, t.getHitIdx(ih));
108  }
109  }
110 
111  //------------------------------------------------------------------------------
112  // Non-ROOT validation
113  //------------------------------------------------------------------------------
114 
116  quality_reset();
117 
118  std::map<int, int> cmsswLabelToPos;
120  for (size_t itrack = 0; itrack < event->cmsswTracks_.size(); itrack++) {
121  cmsswLabelToPos[event->cmsswTracks_[itrack].label()] = itrack;
122  }
123  }
124 
125  for (size_t itrack = 0; itrack < event->candidateTracks_.size(); itrack++) {
126  quality_process(event, event->candidateTracks_[itrack], itrack, cmsswLabelToPos);
127  }
128 
129  quality_print();
130  add_to_quality_sum(*this);
131  }
132 
134 
135  void Quality::quality_process(Event *event, Track &tkcand, const int itrack, std::map<int, int> &cmsswLabelToPos) {
136  // KPM: Do not use this method for validating CMSSW tracks if we ever build a DumbCMSSW function for them to print out...
137  // as we would need to access seeds through map of seed ids...
138 
139  // initialize track extra (input original seed label)
140  const auto label = tkcand.label();
141  TrackExtra extra(label);
142 
143  // track_print(event, tkcand, "quality_process -> track_print:");
144 
145  // access temp seed trk and set matching seed hits
146  const auto &seed = event->seedTracks_[itrack];
147  extra.findMatchingSeedHits(tkcand, seed, event->layerHits_);
148 
149  // set mcTrackID through 50% hit matching after seed
150  extra.setMCTrackIDInfo(
151  tkcand, event->layerHits_, event->simHitsInfo_, event->simTracks_, false, (Config::seedInput == simSeeds));
152  const int mctrk = extra.mcTrackID();
153 
154  // int mctrk = tkcand.label(); // assumes 100% "efficiency"
155 
156  const float pT = tkcand.pT();
157  float pTmc = 0.f, etamc = 0.f, phimc = 0.f;
158  float pTr = 0.f;
159  int nfoundmc = -1;
160 
161  if (mctrk < 0 || static_cast<size_t>(mctrk) >= event->simTracks_.size()) {
162  ++m_cnt_nomc;
163  dprintf("XX bad track idx %d, orig label was %d\n", mctrk, label);
164  } else {
165  auto &simtrack = event->simTracks_[mctrk];
166  pTmc = simtrack.pT();
167  etamc = simtrack.momEta();
168  phimc = simtrack.momPhi();
169  pTr = pT / pTmc;
170 
171  nfoundmc = simtrack.nUniqueLayers();
172 
173  ++m_cnt;
174  if (pTr > 0.9 && pTr < 1.1)
175  ++m_cnt1;
176  if (pTr > 0.8 && pTr < 1.2)
177  ++m_cnt2;
178 
179  if (tkcand.nFoundHits() >= 0.8f * nfoundmc) {
180  ++m_cnt_8;
181  if (pTr > 0.9 && pTr < 1.1)
182  ++m_cnt1_8;
183  if (pTr > 0.8 && pTr < 1.2)
184  ++m_cnt2_8;
185  }
186 
187  // perl -ne 'print if m/FOUND_LABEL\s+[-\d]+/o;' | sort -k2 -n
188  // grep "FOUND_LABEL" | sort -n -k 8,8 -k 2,2
189  // printf("FOUND_LABEL %6d pT_mc= %8.2f eta_mc= %8.2f event= %d\n", label, pTmc, etamc, event->evtID());
190  }
191 
192  float pTcmssw = 0.f, etacmssw = 0.f, phicmssw = 0.f;
193  int nfoundcmssw = -1;
195  if (cmsswLabelToPos.count(label)) {
196  auto &cmsswtrack = event->cmsswTracks_[cmsswLabelToPos[label]];
197  pTcmssw = cmsswtrack.pT();
198  etacmssw = cmsswtrack.momEta();
199  phicmssw = cmsswtrack.swimPhiToR(tkcand.x(), tkcand.y()); // to get rough estimate of diff in phi
200  nfoundcmssw = cmsswtrack.nUniqueLayers();
201  }
202  }
203 
205  std::lock_guard<std::mutex> printlock(Event::printmutex);
206  printf(
207  "MX - found track with chi2= %6.3f nFoundHits= %2d pT= %7.4f eta= %7.4f phi= %7.4f nfoundmc= %2d pTmc= "
208  "%7.4f etamc= %7.4f phimc= %7.4f nfoundcmssw= %2d pTcmssw= %7.4f etacmssw= %7.4f phicmssw= %7.4f lab= %d\n",
209  tkcand.chi2(),
210  tkcand.nFoundHits(),
211  pT,
212  tkcand.momEta(),
213  tkcand.momPhi(),
214  nfoundmc,
215  pTmc,
216  etamc,
217  phimc,
218  nfoundcmssw,
219  pTcmssw,
220  etacmssw,
221  phicmssw,
222  label);
223  }
224  }
225 
227  if (!Config::silent) {
228  std::lock_guard<std::mutex> printlock(Event::printmutex);
229  std::cout << "found tracks=" << m_cnt << " in pT 10%=" << m_cnt1 << " in pT 20%=" << m_cnt2
230  << " no_mc_assoc=" << m_cnt_nomc << std::endl;
231  std::cout << " nH >= 80% =" << m_cnt_8 << " in pT 10%=" << m_cnt1_8 << " in pT 20%=" << m_cnt2_8
232  << std::endl;
233  }
234  }
235 
237 
239  static std::mutex q_mutex;
240  std::lock_guard<std::mutex> q_lock(q_mutex);
241 
242  s_quality_sum.m_cnt += q.m_cnt;
243  s_quality_sum.m_cnt1 += q.m_cnt1;
244  s_quality_sum.m_cnt2 += q.m_cnt2;
245  s_quality_sum.m_cnt_8 += q.m_cnt_8;
246  s_quality_sum.m_cnt1_8 += q.m_cnt1_8;
247  s_quality_sum.m_cnt2_8 += q.m_cnt2_8;
248  s_quality_sum.m_cnt_nomc += q.m_cnt_nomc;
249  }
250 
251  //------------------------------------------------------------------------------
252  // Root validation
253  //------------------------------------------------------------------------------
254 
256  // get labels correct first
257  event->relabel_bad_seedtracks();
258  event->relabel_cmsswtracks_from_seeds();
259 
260  //collection cleaning
261  if (Config::nItersCMSSW > 0)
262  event->select_tracks_iter(Config::nItersCMSSW);
263 
264  // set the track collections to each other
265  event->candidateTracks_ = event->cmsswTracks_;
266  event->fitTracks_ = event->candidateTracks_;
267 
268  // prep the tracks + extras
271 
272  // validate
273  event->validate();
274  }
275 
277  // score the tracks
278  score_tracks(event->seedTracks_);
279  score_tracks(event->candidateTracks_);
280 
281  // deal with fit tracks
282  if (Config::backwardFit) {
283  score_tracks(event->fitTracks_);
284  } else
285  event->fitTracks_ = event->candidateTracks_;
286 
287  // sort hits + make extras, align if needed
289  if (Config::cmssw_val)
291 
292  // validate
293  event->validate();
294  }
295 
297  // seed tracks extras always needed
299  prep_tracks(event, event->seedTracks_, event->seedTracksExtra_, true);
300  } else if (Config::cmssw_val) // seed tracks are not validated, labels used for maps --> do NOT align index and labels!
301  {
302  prep_tracks(event, event->seedTracks_, event->seedTracksExtra_, false);
303  }
304 
305  // make extras + align index == label() for candidate tracks
306  prep_tracks(event, event->candidateTracks_, event->candidateTracksExtra_, true);
307  prep_tracks(event, event->fitTracks_, event->fitTracksExtra_, true);
308  }
309 
311  // First prep sim tracks to have hits sorted, then mark unfindable if too short
312  prep_reftracks(event, event->simTracks_, event->simTracksExtra_, false);
313 
314  // Now, make sure sim track shares at least four hits with a single cmssw seed.
315  // This ensures we factor out any weakness from CMSSW
316 
317  // First, make a make a map of [lyr][hit idx].vector(seed trk labels)
318  LayIdxIDVecMapMap seedHitIDMap;
319  std::map<int, int> labelNHitsMap;
320  std::map<int, int> labelAlgoMap;
321  std::map<int, std::vector<int>> labelSeedHitsMap;
322  for (const auto &seedtrack : event->seedTracks_) {
323  for (int ihit = 0; ihit < seedtrack.nTotalHits(); ihit++) {
324  const auto lyr = seedtrack.getHitLyr(ihit);
325  const auto idx = seedtrack.getHitIdx(ihit);
326 
327  if (lyr < 0 || idx < 0)
328  continue; // standard check
329  seedHitIDMap[lyr][idx].push_back(seedtrack.label());
330  labelSeedHitsMap[seedtrack.label()].push_back(lyr);
331  }
332  labelNHitsMap[seedtrack.label()] = seedtrack.nTotalHits();
333  labelAlgoMap[seedtrack.label()] = seedtrack.algoint();
334  }
335 
336  // Then, loop over sim tracks, and add up how many lyrs they possess of a single seed track
337  unsigned int count = 0;
338  for (auto &simtrack : event->simTracks_) {
339  if (simtrack.isNotFindable())
340  continue; // skip ones we already know are bad
341  TrkIDLaySetMap seedIDMap;
342  for (int ihit = 0; ihit < simtrack.nTotalHits(); ihit++) {
343  const auto lyr = simtrack.getHitLyr(ihit);
344  const auto idx = simtrack.getHitIdx(ihit);
345 
346  if (lyr < 0 || idx < 0)
347  continue; // standard check
348 
349  if (!seedHitIDMap.count(lyr))
350  continue; // ensure seed hit map has at least one entry for this layer
351  if (!seedHitIDMap.at(lyr).count(idx))
352  continue; // ensure seed hit map has at least one entry for this idx
353 
354  for (const auto label : seedHitIDMap.at(lyr).at(idx)) {
355  const auto &seedLayers = labelSeedHitsMap[label];
356  if (std::find(seedLayers.begin(), seedLayers.end(), lyr) != seedLayers.end()) //seed check moved here
357  seedIDMap[label].emplace(lyr);
358  }
359  }
360 
361  // now see if one of the seedIDs matched has at least 4 hits!
362  bool isSimSeed = false;
363  for (const auto &seedIDpair : seedIDMap) {
364  if ((int)seedIDpair.second.size() == labelNHitsMap[seedIDpair.first]) {
365  isSimSeed = true;
367  simtrack.setAlgoint(labelAlgoMap[seedIDpair.first]);
369  event->simTracksExtra_[count].addAlgo(labelAlgoMap[seedIDpair.first]);
370  //break;
371  }
372  }
374  // Apply MTV selection criteria and then return
375  if (simtrack.prodType() != Track::ProdType::Signal || simtrack.charge() == 0 || simtrack.posR() > 2.5 ||
376  std::abs(simtrack.z()) > 30 || std::abs(simtrack.momEta()) > 3.0)
377  simtrack.setNotFindable();
378  else if (Config::mtvRequireSeeds && !isSimSeed)
379  simtrack.setNotFindable();
380  } else {
381  // set findability based on bool isSimSeed
382  if (!isSimSeed)
383  simtrack.setNotFindable();
384  }
385  count++;
386  }
387  }
388 
389  void prep_cmsswtracks(Event *event) { prep_reftracks(event, event->cmsswTracks_, event->cmsswTracksExtra_, true); }
390 
391  void prep_reftracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks) {
392  prep_tracks(event, tracks, extras, realigntracks);
393 
394  // mark cmsswtracks as unfindable if too short
395  for (auto &track : tracks) {
396  const int nlyr = track.nUniqueLayers();
397  if (nlyr < Config::cmsSelMinLayers)
398  track.setNotFindable();
399  }
400  }
401 
402  void prep_tracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks) {
403  for (size_t i = 0; i < tracks.size(); i++) {
404  extras.emplace_back(tracks[i].label());
405  }
406  if (realigntracks)
407  event->validation_.alignTracks(tracks, extras, false);
408  }
409 
411  auto score_func = IterationConfig::get_track_scorer("default");
412  for (auto &track : tracks) {
413  track.setScore(getScoreCand(score_func, track));
414  }
415  }
416 
417  } // namespace StdSeq
418 
419 } // namespace mkfit
float getScoreCand(const track_score_func &score_func, const Track &cand1, bool penalizeTailMissHits=false, bool inFindCandidates=false)
Definition: Track.h:601
void prep_reftracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks)
std::vector< TrackExtra > TrackExtraVec
float pT() const
Definition: Track.h:157
void findMatchingSeedHits(const Track &reco_trk, const Track &seed_trk, const std::vector< HitVec > &layerHits)
Definition: TrackExtra.cc:13
static std::mutex mutex
Definition: Proxy.cc:8
float chi2() const
Definition: Track.h:172
std::map< int, std::unordered_set< int > > TrkIDLaySetMap
Definition: TrackExtra.h:38
void root_val(Event *event)
int label() const
Definition: Track.h:174
#define TBB_PARALLEL_FOR
Definition: Debug.h:143
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
int mcTrackID() const
Definition: TrackExtra.h:79
void quality_process(Event *event, Track &tkcand, const int itrack, std::map< int, int > &cmsswLabelToPos)
void setMCTrackIDInfo(const Track &trk, const std::vector< HitVec > &layerHits, const MCHitInfoVec &globalHitInfo, const TrackVec &simtracks, const bool isSeed, const bool isPure)
Definition: TrackExtra.cc:106
void prep_simtracks(Event *event)
int nFoundHits() const
Definition: Track.h:511
float momEta() const
Definition: Track.h:161
float y() const
Definition: Track.h:147
char const * label
void prep_tracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks)
void loadHitsAndBeamSpot(Event &ev, EventOfHits &eoh)
void track_print(Event *event, const Track &t, const char *pref)
void score_tracks(TrackVec &tracks)
void prep_recotracks(Event *event)
void setBeamSpot(const BeamSpot &bs)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static Quality s_quality_sum
static std::mutex printmutex
Definition: Event.h:82
static void add_to_quality_sum(const Quality &q)
std::vector< Track > TrackVec
static track_score_func get_track_scorer(const std::string &name)
void print(std::string_view label, const MeasurementState &s)
Definition: Hit.cc:8
void suckInHits(int layer, const HitVec &hitv)
#define dprint(x)
Definition: Debug.h:95
void dump_simtracks(Event *event)
float x() const
Definition: Track.h:146
void root_val_dumb_cmssw(Event *event)
void quality_val(Event *event)
void handle_duplicates(Event *)
float momPhi() const
Definition: Track.h:160
std::map< int, std::map< int, std::vector< int > > > LayIdxIDVecMapMap
Definition: TrackExtra.h:37
#define dprintf(...)
Definition: Debug.h:98
Definition: event.py:1
void prep_cmsswtracks(Event *event)