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  }
131 
133 
134  void Quality::quality_process(Event *event, Track &tkcand, const int itrack, std::map<int, int> &cmsswLabelToPos) {
135  // KPM: Do not use this method for validating CMSSW tracks if we ever build a DumbCMSSW function for them to print out...
136  // as we would need to access seeds through map of seed ids...
137 
138  // initialize track extra (input original seed label)
139  const auto label = tkcand.label();
140  TrackExtra extra(label);
141 
142  // track_print(event, tkcand, "quality_process -> track_print:");
143 
144  // access temp seed trk and set matching seed hits
145  const auto &seed = event->seedTracks_[itrack];
146  extra.findMatchingSeedHits(tkcand, seed, event->layerHits_);
147 
148  // set mcTrackID through 50% hit matching after seed
149  extra.setMCTrackIDInfo(
150  tkcand, event->layerHits_, event->simHitsInfo_, event->simTracks_, false, (Config::seedInput == simSeeds));
151  const int mctrk = extra.mcTrackID();
152 
153  // int mctrk = tkcand.label(); // assumes 100% "efficiency"
154 
155  const float pT = tkcand.pT();
156  float pTmc = 0.f, etamc = 0.f, phimc = 0.f;
157  float pTr = 0.f;
158  int nfoundmc = -1;
159 
160  if (mctrk < 0 || static_cast<size_t>(mctrk) >= event->simTracks_.size()) {
161  ++m_cnt_nomc;
162  dprintf("XX bad track idx %d, orig label was %d\n", mctrk, label);
163  } else {
164  auto &simtrack = event->simTracks_[mctrk];
165  pTmc = simtrack.pT();
166  etamc = simtrack.momEta();
167  phimc = simtrack.momPhi();
168  pTr = pT / pTmc;
169 
170  nfoundmc = simtrack.nUniqueLayers();
171 
172  ++m_cnt;
173  if (pTr > 0.9 && pTr < 1.1)
174  ++m_cnt1;
175  if (pTr > 0.8 && pTr < 1.2)
176  ++m_cnt2;
177 
178  if (tkcand.nFoundHits() >= 0.8f * nfoundmc) {
179  ++m_cnt_8;
180  if (pTr > 0.9 && pTr < 1.1)
181  ++m_cnt1_8;
182  if (pTr > 0.8 && pTr < 1.2)
183  ++m_cnt2_8;
184  }
185 
186  // perl -ne 'print if m/FOUND_LABEL\s+[-\d]+/o;' | sort -k2 -n
187  // grep "FOUND_LABEL" | sort -n -k 8,8 -k 2,2
188  // printf("FOUND_LABEL %6d pT_mc= %8.2f eta_mc= %8.2f event= %d\n", label, pTmc, etamc, event->evtID());
189  }
190 
191  float pTcmssw = 0.f, etacmssw = 0.f, phicmssw = 0.f;
192  int nfoundcmssw = -1;
194  if (cmsswLabelToPos.count(label)) {
195  auto &cmsswtrack = event->cmsswTracks_[cmsswLabelToPos[label]];
196  pTcmssw = cmsswtrack.pT();
197  etacmssw = cmsswtrack.momEta();
198  phicmssw = cmsswtrack.swimPhiToR(tkcand.x(), tkcand.y()); // to get rough estimate of diff in phi
199  nfoundcmssw = cmsswtrack.nUniqueLayers();
200  }
201  }
202 
204  std::lock_guard<std::mutex> printlock(Event::printmutex);
205  printf(
206  "MX - found track with chi2= %6.3f nFoundHits= %2d pT= %7.4f eta= %7.4f phi= %7.4f nfoundmc= %2d pTmc= "
207  "%7.4f etamc= %7.4f phimc= %7.4f nfoundcmssw= %2d pTcmssw= %7.4f etacmssw= %7.4f phicmssw= %7.4f lab= %d\n",
208  tkcand.chi2(),
209  tkcand.nFoundHits(),
210  pT,
211  tkcand.momEta(),
212  tkcand.momPhi(),
213  nfoundmc,
214  pTmc,
215  etamc,
216  phimc,
217  nfoundcmssw,
218  pTcmssw,
219  etacmssw,
220  phicmssw,
221  label);
222  }
223  }
224 
226  if (!Config::silent) {
227  std::lock_guard<std::mutex> printlock(Event::printmutex);
228  std::cout << "found tracks=" << m_cnt << " in pT 10%=" << m_cnt1 << " in pT 20%=" << m_cnt2
229  << " no_mc_assoc=" << m_cnt_nomc << std::endl;
230  std::cout << " nH >= 80% =" << m_cnt_8 << " in pT 10%=" << m_cnt1_8 << " in pT 20%=" << m_cnt2_8
231  << std::endl;
232  }
233  }
234 
235  //------------------------------------------------------------------------------
236  // Root validation
237  //------------------------------------------------------------------------------
238 
240  // get labels correct first
241  event->relabel_bad_seedtracks();
242  event->relabel_cmsswtracks_from_seeds();
243 
244  //collection cleaning
245  if (Config::nItersCMSSW > 0)
246  event->select_tracks_iter(Config::nItersCMSSW);
247 
248  // set the track collections to each other
249  event->candidateTracks_ = event->cmsswTracks_;
250  event->fitTracks_ = event->candidateTracks_;
251 
252  // prep the tracks + extras
255 
256  // validate
257  event->validate();
258  }
259 
261  // score the tracks
262  score_tracks(event->seedTracks_);
263  score_tracks(event->candidateTracks_);
264 
265  // deal with fit tracks
266  if (Config::backwardFit) {
267  score_tracks(event->fitTracks_);
268  } else
269  event->fitTracks_ = event->candidateTracks_;
270 
271  // sort hits + make extras, align if needed
273  if (Config::cmssw_val)
275 
276  // validate
277  event->validate();
278  }
279 
281  // seed tracks extras always needed
283  prep_tracks(event, event->seedTracks_, event->seedTracksExtra_, true);
284  } else if (Config::cmssw_val) // seed tracks are not validated, labels used for maps --> do NOT align index and labels!
285  {
286  prep_tracks(event, event->seedTracks_, event->seedTracksExtra_, false);
287  }
288 
289  // make extras + align index == label() for candidate tracks
290  prep_tracks(event, event->candidateTracks_, event->candidateTracksExtra_, true);
291  prep_tracks(event, event->fitTracks_, event->fitTracksExtra_, true);
292  }
293 
295  // First prep sim tracks to have hits sorted, then mark unfindable if too short
296  prep_reftracks(event, event->simTracks_, event->simTracksExtra_, false);
297 
298  // Now, make sure sim track shares at least four hits with a single cmssw seed.
299  // This ensures we factor out any weakness from CMSSW
300 
301  // First, make a make a map of [lyr][hit idx].vector(seed trk labels)
302  LayIdxIDVecMapMap seedHitIDMap;
303  std::map<int, int> labelNHitsMap;
304  std::map<int, int> labelAlgoMap;
305  std::map<int, std::vector<int>> labelSeedHitsMap;
306  for (const auto &seedtrack : event->seedTracks_) {
307  for (int ihit = 0; ihit < seedtrack.nTotalHits(); ihit++) {
308  const auto lyr = seedtrack.getHitLyr(ihit);
309  const auto idx = seedtrack.getHitIdx(ihit);
310 
311  if (lyr < 0 || idx < 0)
312  continue; // standard check
313  seedHitIDMap[lyr][idx].push_back(seedtrack.label());
314  labelSeedHitsMap[seedtrack.label()].push_back(lyr);
315  }
316  labelNHitsMap[seedtrack.label()] = seedtrack.nTotalHits();
317  labelAlgoMap[seedtrack.label()] = seedtrack.algoint();
318  }
319 
320  // Then, loop over sim tracks, and add up how many lyrs they possess of a single seed track
321  unsigned int count = 0;
322  for (auto &simtrack : event->simTracks_) {
323  if (simtrack.isNotFindable())
324  continue; // skip ones we already know are bad
325  TrkIDLaySetMap seedIDMap;
326  for (int ihit = 0; ihit < simtrack.nTotalHits(); ihit++) {
327  const auto lyr = simtrack.getHitLyr(ihit);
328  const auto idx = simtrack.getHitIdx(ihit);
329 
330  if (lyr < 0 || idx < 0)
331  continue; // standard check
332 
333  if (!seedHitIDMap.count(lyr))
334  continue; // ensure seed hit map has at least one entry for this layer
335  if (!seedHitIDMap.at(lyr).count(idx))
336  continue; // ensure seed hit map has at least one entry for this idx
337 
338  for (const auto label : seedHitIDMap.at(lyr).at(idx)) {
339  const auto &seedLayers = labelSeedHitsMap[label];
340  if (std::find(seedLayers.begin(), seedLayers.end(), lyr) != seedLayers.end()) //seed check moved here
341  seedIDMap[label].emplace(lyr);
342  }
343  }
344 
345  // now see if one of the seedIDs matched has at least 4 hits!
346  bool isSimSeed = false;
347  for (const auto &seedIDpair : seedIDMap) {
348  if ((int)seedIDpair.second.size() == labelNHitsMap[seedIDpair.first]) {
349  isSimSeed = true;
351  simtrack.setAlgoint(labelAlgoMap[seedIDpair.first]);
353  event->simTracksExtra_[count].addAlgo(labelAlgoMap[seedIDpair.first]);
354  //break;
355  }
356  }
358  // Apply MTV selection criteria and then return
359  if (simtrack.prodType() != Track::ProdType::Signal || simtrack.charge() == 0 || simtrack.posR() > 2.5 ||
360  std::abs(simtrack.z()) > 30 || std::abs(simtrack.momEta()) > 3.0)
361  simtrack.setNotFindable();
362  else if (Config::mtvRequireSeeds && !isSimSeed)
363  simtrack.setNotFindable();
364  } else {
365  // set findability based on bool isSimSeed
366  if (!isSimSeed)
367  simtrack.setNotFindable();
368  }
369  count++;
370  }
371  }
372 
373  void prep_cmsswtracks(Event *event) { prep_reftracks(event, event->cmsswTracks_, event->cmsswTracksExtra_, true); }
374 
375  void prep_reftracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks) {
376  prep_tracks(event, tracks, extras, realigntracks);
377 
378  // mark cmsswtracks as unfindable if too short
379  for (auto &track : tracks) {
380  const int nlyr = track.nUniqueLayers();
381  if (nlyr < Config::cmsSelMinLayers)
382  track.setNotFindable();
383  }
384  }
385 
386  void prep_tracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks) {
387  for (size_t i = 0; i < tracks.size(); i++) {
388  extras.emplace_back(tracks[i].label());
389  }
390  if (realigntracks)
391  event->validation_.alignTracks(tracks, extras, false);
392  }
393 
395  auto score_func = IterationConfig::get_track_scorer("default");
396  for (auto &track : tracks) {
397  track.setScore(getScoreCand(score_func, track));
398  }
399  }
400 
401  } // namespace StdSeq
402 
403 } // namespace mkfit
float getScoreCand(const track_score_func &score_func, const Track &cand1, bool penalizeTailMissHits=false, bool inFindCandidates=false)
Definition: Track.h:615
void prep_reftracks(Event *event, TrackVec &tracks, TrackExtraVec &extras, const bool realigntracks)
std::vector< TrackExtra > TrackExtraVec
float pT() const
Definition: Track.h:171
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:186
std::map< int, std::unordered_set< int > > TrkIDLaySetMap
Definition: TrackExtra.h:38
void root_val(Event *event)
int label() const
Definition: Track.h:188
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:525
float momEta() const
Definition: Track.h:175
float y() const
Definition: Track.h:161
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 std::mutex printmutex
Definition: Event.h:81
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:160
void root_val_dumb_cmssw(Event *event)
void quality_val(Event *event)
void handle_duplicates(Event *)
float momPhi() const
Definition: Track.h:174
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)