CMS 3D CMS Logo

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