CMS 3D CMS Logo

TrackListMerger.cc
Go to the documentation of this file.
1 //
2 // Package: RecoTracker/FinalTrackSelectors
3 // Class: TrackListMerger
4 //
5 // Description: Hit Dumper
6 //
7 // Original Author: Steve Wagner, stevew@pizero.colorado.edu
8 // Created: Sat Jan 14 22:00:00 UTC 2006
9 //
10 //
11 
16 
28 
31 
33  {
34  public:
35 
36  explicit TrackListMerger(const edm::ParameterSet& conf);
37 
38  virtual ~TrackListMerger();
39 
40  virtual void produce(edm::Event& e, const edm::EventSetup& c) override;
41 
42  private:
43 
44  using MVACollection = std::vector<float>;
45  using QualityMaskCollection = std::vector<unsigned char>;
46 
47  std::unique_ptr<reco::TrackCollection> outputTrks;
48  std::unique_ptr<reco::TrackExtraCollection> outputTrkExtras;
49  std::unique_ptr<TrackingRecHitCollection> outputTrkHits;
50  std::unique_ptr<std::vector<Trajectory> > outputTrajs;
51  std::unique_ptr<TrajTrackAssociationCollection > outputTTAss;
52  std::unique_ptr<TrajectorySeedCollection > outputSeeds;
53 
59 
62 
63  struct TkEDGetTokenss {
72  edm::EDGetTokenT<std::vector<Trajectory> > && traj_, edm::EDGetTokenT<TrajTrackAssociationCollection > && tass_,
74  tag(tag_), tk(tk_), traj(traj_), tass(tass_), tsel(tsel_), tmva(tmva_) {}
75  };
76  TkEDGetTokenss edTokens(const edm::InputTag &tag, const edm::InputTag &seltag, const edm::InputTag &mvatag) {
77  return TkEDGetTokenss(tag, consumes<reco::TrackCollection>(tag),
78  consumes<std::vector<Trajectory> >(tag), consumes<TrajTrackAssociationCollection >(tag),
79  consumes<edm::ValueMap<int> >(seltag), consumes<edm::ValueMap<float> >(mvatag));
80  }
82  return TkEDGetTokenss(tag, consumes<reco::TrackCollection>(tag),
83  consumes<std::vector<Trajectory> >(tag), consumes<TrajTrackAssociationCollection >(tag),
85  }
86  std::vector<TkEDGetTokenss> trackProducers_;
87 
89 
91  double minPT_;
92  unsigned int minFound_;
93  float epsilon_;
94  float shareFrac_;
97  std::vector<double> indivShareFrac_;
98 
99  std::vector< std::vector< int> > listsToMerge_;
100  std::vector<bool> promoteQuality_;
101  std::vector<int> hasSelector_;
102  bool copyMVA_;
103 
108 
109 
110  };
111 
112 
113 
114 #include <memory>
115 #include <string>
116 #include <iostream>
117 #include <cmath>
118 #include <vector>
119 
127 
129 
133 
134 //#include "DataFormats/TrackReco/src/classes.h"
135 
137 
138 #ifdef STAT_TSB
139 #include <x86intrin.h>
140 #endif
141 
142 namespace {
143 #ifdef STAT_TSB
144 inline volatile unsigned long long rdtsc() {
145  return __rdtsc();
146 }
147 
148  struct StatCount {
149  float maxDP=0.;
150  float maxDE=0.;
151  unsigned long long st;
152  long long totBegin=0;
153  long long totPre=0;
154  long long totEnd=0;
155  unsigned long long timeNo; // no-overlap
156  unsigned long long timeOv; // overlap
157  void begin(int tt) {
158  totBegin+=tt;
159  }
160  void start() { st=rdtsc(); }
161  void noOverlap() { timeNo += (rdtsc()-st);}
162  void overlap() { timeOv += (rdtsc()-st);}
163  void pre(int tt) { totPre+=tt;}
164  void end(int tt) { totEnd+=tt;}
165  void de(float d) { if (d>maxDE) maxDE=d;}
166  void dp(float d) { if (d>maxDP) maxDP=d;}
167 
168 
169  void print() const {
170  std::cout << "TrackListMerger stat\nBegin/Pre/End/maxDPhi/maxDEta/Overlap/NoOverlap "
171  << totBegin <<'/'<< totPre <<'/'<< totEnd <<'/'<< maxDP <<'/'<< maxDE
172  <<'/'<< timeOv/1000 <<'/'<< timeNo/1000
173  << std::endl;
174  }
175  StatCount() {}
176  ~StatCount() { print();}
177  };
178  StatCount statCount;
179 
180 #else
181  struct StatCount {
182  void begin(int){}
183  void pre(int){}
184  void end(int){}
185  void start(){}
186  void noOverlap(){}
187  void overlap(){}
188  void de(float){}
189  void dp(float){}
190 
191 
192  };
193  [[cms::thread_safe]] StatCount statCount;
194 #endif
195 
196 
197 }
198 
199 
200 
201 namespace {
202  edm::ProductID clusterProductB( const TrackingRecHit *hit){
203  return reinterpret_cast<const BaseTrackerRecHit *>(hit)->firstClusterRef().id();
204  }
205 }
206 
207 
209  copyExtras_ = conf.getUntrackedParameter<bool>("copyExtras", true);
210 
211  std::vector<edm::InputTag> trackProducerTags(conf.getParameter<std::vector<edm::InputTag> >("TrackProducers"));
212  //which of these do I need to turn into vectors?
213  maxNormalizedChisq_ = conf.getParameter<double>("MaxNormalizedChisq");
214  minPT_ = conf.getParameter<double>("MinPT");
215  minFound_ = (unsigned int)conf.getParameter<int>("MinFound");
216  epsilon_ = conf.getParameter<double>("Epsilon");
217  shareFrac_ = conf.getParameter<double>("ShareFrac");
218  allowFirstHitShare_ = conf.getParameter<bool>("allowFirstHitShare");
219  foundHitBonus_ = conf.getParameter<double>("FoundHitBonus");
220  lostHitPenalty_ = conf.getParameter<double>("LostHitPenalty");
221  indivShareFrac_=conf.getParameter<std::vector<double> >("indivShareFrac");
222  std::string qualityStr = conf.getParameter<std::string>("newQuality");
223 
224  if (qualityStr != "") {
225  qualityToSet_ = reco::TrackBase::qualityByName(conf.getParameter<std::string>("newQuality"));
226  }
227  else
228  qualityToSet_ = reco::TrackBase::undefQuality;
229 
230  use_sharesInput_ = true;
231  if ( epsilon_ > 0.0 )use_sharesInput_ = false;
232 
234 
235  for ( unsigned int i=0; i<setsToMerge.size(); i++) {
236  listsToMerge_.push_back(setsToMerge[i].getParameter<std::vector< int> >("tLists"));
237  promoteQuality_.push_back(setsToMerge[i].getParameter<bool>("pQual"));
238  }
239  hasSelector_ = conf.getParameter<std::vector<int> > ("hasSelector");
240  copyMVA_ = conf.getParameter<bool>("copyMVA");
241 
242  std::vector<edm::InputTag> selectors(conf.getParameter<std::vector<edm::InputTag> >("selectedTrackQuals"));
243  std::vector<edm::InputTag> mvaStores;
244  if(conf.exists("mvaValueTags")){
245  mvaStores = conf.getParameter<std::vector<edm::InputTag> >("mvaValueTags");
246  }else{
247  for (int i = 0; i < (int)selectors.size(); i++){
248  edm::InputTag ntag(selectors[i].label(),"MVAVals");
249  mvaStores.push_back(ntag);
250  }
251  }
252  unsigned int numTrkColl = trackProducerTags.size();
253  if (numTrkColl != hasSelector_.size() || numTrkColl != selectors.size()) {
254  throw cms::Exception("Inconsistent size") << "need same number of track collections and selectors";
255  }
256  if (numTrkColl != hasSelector_.size() || numTrkColl != mvaStores.size()) {
257  throw cms::Exception("Inconsistent size") << "need same number of track collections and MVA stores";
258  }
259  for (unsigned int i = indivShareFrac_.size(); i < numTrkColl; i++) {
260  // edm::LogWarning("TrackListMerger") << "No indivShareFrac for " << trackProducersTags <<". Using default value of 1";
261  indivShareFrac_.push_back(1.0);
262  }
263 
264  trkQualMod_=conf.getParameter<bool>("writeOnlyTrkQuals");
265  if ( trkQualMod_) {
266  bool ok=true;
267  for ( unsigned int i=1; i<numTrkColl; i++) {
268  if (!(trackProducerTags[i]==trackProducerTags[0])) ok=false;
269  }
270  if ( !ok) {
271  throw cms::Exception("Bad input") << "to use writeOnlyTrkQuals=True all input InputTags must be the same";
272  }
273  produces<edm::ValueMap<int> >();
274  produces<QualityMaskCollection>("QualityMasks");
275  }
276  else{
277  produces<reco::TrackCollection>();
278 
279  makeReKeyedSeeds_ = conf.getUntrackedParameter<bool>("makeReKeyedSeeds",false);
280  if (makeReKeyedSeeds_){
281  copyExtras_=true;
282  produces<TrajectorySeedCollection>();
283  }
284 
285  if (copyExtras_) {
286  produces<reco::TrackExtraCollection>();
287  produces<TrackingRecHitCollection>();
288  }
289  produces< std::vector<Trajectory> >();
290  produces< TrajTrackAssociationCollection >();
291  }
292  produces<edm::ValueMap<float> >("MVAVals");
293  produces<MVACollection>("MVAValues");
294 
295  // Do all the consumes
296  trackProducers_.resize(numTrkColl);
297  for (unsigned int i = 0; i < numTrkColl; ++i) {
298  trackProducers_[i] = hasSelector_[i]>0 ? edTokens(trackProducerTags[i], selectors[i], mvaStores[i]) : edTokens(trackProducerTags[i], mvaStores[i]);
299  }
300 
301  priorityName_ = conf.getParameter<std::string>("trackAlgoPriorityOrder");
302 }
303 
304 
305 // Virtual destructor needed.
307 
308  // Functions that gets called by framework every event
310  {
311  // extract tracker geometry
312  //
313  //edm::ESHandle<TrackerGeometry> theG;
314  //es.get<TrackerDigiGeometryRecord>().get(theG);
315 
316  // using namespace reco;
317 
319  es.get<CkfComponentsRecord>().get(priorityName_, priorityH);
320  auto const & trackAlgoPriorityOrder = *priorityH;
321 
322  // get Inputs
323  // if 1 input list doesn't exist, make an empty list, issue a warning, and continue
324  // this allows TrackListMerger to be used as a cleaner only if handed just one list
325  // if both input lists don't exist, will issue 2 warnings and generate an empty output collection
326  //
327  static const reco::TrackCollection s_empty;
328 
329  std::vector<const reco::TrackCollection *> trackColls;
330  std::vector<edm::Handle<reco::TrackCollection> > trackHandles(trackProducers_.size());
331  for ( unsigned int i=0; i<trackProducers_.size(); i++) {
332  trackColls.push_back(0);
333  //edm::Handle<reco::TrackCollection> trackColl;
334  e.getByToken(trackProducers_[i].tk, trackHandles[i]);
335  if (trackHandles[i].isValid()) {
336  trackColls[i]= trackHandles[i].product();
337  } else {
338  edm::LogWarning("TrackListMerger") << "TrackCollection " << trackProducers_[i].tag <<" not found";
339  trackColls[i]=&s_empty;
340  }
341  }
342 
343  unsigned int collsSize =trackColls.size();
344  unsigned int rSize=0;
345  unsigned int trackCollSizes[collsSize];
346  unsigned int trackCollFirsts[collsSize];
347  for (unsigned int i=0; i!=collsSize; i++) {
348  trackCollSizes[i]=trackColls[i]->size();
349  trackCollFirsts[i]=rSize;
350  rSize+=trackCollSizes[i];
351  }
352 
353  statCount.begin(rSize);
354 
355  //
356  // quality cuts first
357  //
358  int i=-1;
359 
360  int selected[rSize];
361  int indexG[rSize];
362  bool trkUpdated[rSize];
363  int trackCollNum[rSize];
364  int trackQuals[rSize];
365  float trackMVAs[rSize];
366  reco::TrackBase::TrackAlgorithm oriAlgo[rSize];
367  std::vector<reco::TrackBase::AlgoMask> algoMask(rSize);
368  for (unsigned int j=0; j<rSize;j++) {
369  indexG[j]=-1; selected[j]=1; trkUpdated[j]=false; trackCollNum[j]=0; trackQuals[j]=0;trackMVAs[j] = -998.0;oriAlgo[j]=reco::TrackBase::undefAlgorithm;
370  }
371 
372  int ngood=0;
373  for (unsigned int j=0; j!= collsSize; j++) {
374  const reco::TrackCollection *tC1=trackColls[j];
375 
376  edm::Handle<edm::ValueMap<int> > trackSelColl;
377  edm::Handle<edm::ValueMap<float> > trackMVAStore;
378  if ( copyMVA_ )
379  e.getByToken(trackProducers_[j].tmva, trackMVAStore);
380  if ( hasSelector_[j]>0 ){
381  e.getByToken(trackProducers_[j].tsel, trackSelColl);
382  }
383 
384  if ( 0<tC1->size() ){
385  unsigned int iC=0;
386  for (reco::TrackCollection::const_iterator track=tC1->begin(); track!=tC1->end(); track++){
387  i++;
388  trackCollNum[i]=j;
389  trackQuals[i]=track->qualityMask();
390  oriAlgo[i]=track->originalAlgo();
391  algoMask[i]=track->algoMask();
392 
393  reco::TrackRef trkRef=reco::TrackRef(trackHandles[j],iC);
394  if ( copyMVA_ )
395  if( (*trackMVAStore).contains(trkRef.id()) ) trackMVAs[i] = (*trackMVAStore)[trkRef];
396  if ( hasSelector_[j]>0 ) {
397  int qual=(*trackSelColl)[trkRef];
398  if ( qual < 0 ) {
399  selected[i]=0;
400  iC++;
401  continue;
402  }else{
403  trackQuals[i]=qual;
404  }
405  }
406  iC++;
407  selected[i]=trackQuals[i]+10;//10 is magic number used throughout...
408  if ((short unsigned)track->ndof() < 1){
409  selected[i]=0;
410  continue;
411  }
412  if (track->normalizedChi2() > maxNormalizedChisq_){
413  selected[i]=0;
414  continue;
415  }
416  if (track->found() < minFound_){
417  selected[i]=0;
418  continue;
419  }
420  if (track->pt() < minPT_){
421  selected[i]=0;
422  continue;
423  }
424  // good!
425  indexG[i] = ngood++;
426  //if ( beVerb) std::cout << "inverb " << track->pt() << " " << selected[i] << std::endl;
427  }//end loop over tracks
428  }//end more than 0 track
429  } // loop over trackcolls
430 
431 
432  statCount.pre(ngood);
433 
434  //cache the id and rechits of valid hits
435  typedef std::pair<unsigned int, const TrackingRecHit*> IHit;
436  std::vector<std::vector<IHit>> rh1(ngood); // "not an array" of vectors!
437  //const TrackingRecHit* fh1[ngood]; // first hit...
439  float score[ngood];
440 
441 
442  for ( unsigned int j=0; j<rSize; j++) {
443  if (selected[j]==0) continue;
444  int i = indexG[j];
445  assert(i>=0);
446  unsigned int collNum=trackCollNum[j];
447  unsigned int trackNum=j-trackCollFirsts[collNum];
448  const reco::Track *track=&((trackColls[collNum])->at(trackNum));
449 
450  algo[i]=track->algo();
451  int validHits=track->numberOfValidHits();
452  int validPixelHits=track->hitPattern().numberOfValidPixelHits();
453  int lostHits=track->numberOfLostHits();
454  score[i] = foundHitBonus_*validPixelHits+foundHitBonus_*validHits - lostHitPenalty_*lostHits - track->chi2();
455 
456 
457  rh1[i].reserve(validHits) ;
458  auto compById = [](IHit const & h1, IHit const & h2) {return h1.first < h2.first;};
459  for (trackingRecHit_iterator it = track->recHitsBegin(); it != track->recHitsEnd(); ++it) {
460  const TrackingRecHit* hit = (*it);
461  unsigned int id = hit->rawId() ;
462  if(hit->geographicalId().subdetId()>2) id &= (~3); // mask mono/stereo in strips...
463  if likely(hit->isValid()) { rh1[i].emplace_back(id,hit); std::push_heap(rh1[i].begin(),rh1[i].end(),compById); }
464  }
465  std::sort_heap(rh1[i].begin(),rh1[i].end(),compById);
466  }
467 
468  //DL here
469  if likely(ngood>1 && collsSize>1)
470  for ( unsigned int ltm=0; ltm<listsToMerge_.size(); ltm++) {
471  int saveSelected[rSize];
472  bool notActive[collsSize];
473  for (unsigned int cn=0;cn!=collsSize;++cn)
474  notActive[cn]= find(listsToMerge_[ltm].begin(),listsToMerge_[ltm].end(),cn)==listsToMerge_[ltm].end();
475 
476  for ( unsigned int i=0; i<rSize; i++) saveSelected[i]=selected[i];
477 
478  //DL protect against 0 tracks?
479  for ( unsigned int i=0; i<rSize-1; i++) {
480  if (selected[i]==0) continue;
481  unsigned int collNum=trackCollNum[i];
482 
483  //check that this track is in one of the lists for this iteration
484  if (notActive[collNum]) continue;
485 
486  int k1 = indexG[i];
487  unsigned int nh1=rh1[k1].size();
488  int qualityMaskT1 = trackQuals[i];
489 
490  int nhit1 = nh1; // validHits[k1];
491  float score1 = score[k1];
492 
493  // start at next collection
494  for ( unsigned int j=i+1; j<rSize; j++) {
495  if (selected[j]==0) continue;
496  unsigned int collNum2=trackCollNum[j];
497  if ( (collNum == collNum2) && indivShareFrac_[collNum] > 0.99) continue;
498  //check that this track is in one of the lists for this iteration
499  if (notActive[collNum2]) continue;
500 
501  int k2 = indexG[j];
502 
503  int newQualityMask = -9; //avoid resetting quality mask if not desired 10+ -9 =1
504  if (promoteQuality_[ltm]) {
505  int maskT1= saveSelected[i]>1? saveSelected[i]-10 : qualityMaskT1;
506  int maskT2= saveSelected[j]>1? saveSelected[j]-10 : trackQuals[j];
507  newQualityMask =(maskT1 | maskT2); // take OR of trackQuality
508  }
509  unsigned int nh2=rh1[k2].size();
510  int nhit2 = nh2;
511 
512 
513  auto share = use_sharesInput_ ?
514  [](const TrackingRecHit* it,const TrackingRecHit* jt, float)->bool { return it->sharesInput(jt,TrackingRecHit::some); } :
515  [](const TrackingRecHit* it,const TrackingRecHit* jt, float eps)->bool {
516  float delta = std::abs ( it->localPosition().x()-jt->localPosition().x() );
517  return (it->geographicalId()==jt->geographicalId())&&(delta<eps);
518  };
519 
520  statCount.start();
521 
522  //loop over rechits
523  int noverlap=0;
524  int firstoverlap=0;
525  // check first hit (should use REAL first hit?)
526  if unlikely(allowFirstHitShare_ && rh1[k1][0].first==rh1[k2][0].first ) {
527  const TrackingRecHit* it = rh1[k1][0].second;
528  const TrackingRecHit* jt = rh1[k2][0].second;
529  if (share(it,jt,epsilon_)) firstoverlap=1;
530  }
531 
532 
533  // exploit sorting
534  unsigned int jh=0;
535  unsigned int ih=0;
536  while (ih!=nh1 && jh!=nh2) {
537  // break if not enough to go...
538  // if ( nprecut-noverlap+firstoverlap > int(nh1-ih)) break;
539  // if ( nprecut-noverlap+firstoverlap > int(nh2-jh)) break;
540  auto const id1 = rh1[k1][ih].first;
541  auto const id2 = rh1[k2][jh].first;
542  if (id1<id2) ++ih;
543  else if (id2<id1) ++jh;
544  else {
545  // in case of split-hit do full conbinatorics
546  auto li=ih; while( (++li)!=nh1 && id1 == rh1[k1][li].first);
547  auto lj=jh; while( (++lj)!=nh2 && id2 == rh1[k2][lj].first);
548  for (auto ii=ih; ii!=li; ++ii)
549  for (auto jj=jh; jj!=lj; ++jj) {
550  const TrackingRecHit* it = rh1[k1][ii].second;
551  const TrackingRecHit* jt = rh1[k2][jj].second;
552  if (share(it,jt,epsilon_)) noverlap++;
553  }
554  jh=lj; ih=li;
555  } // equal ids
556 
557  } //loop over ih & jh
558 
559  bool dupfound = (collNum != collNum2) ? (noverlap-firstoverlap) > (std::min(nhit1,nhit2)-firstoverlap)*shareFrac_ :
560  (noverlap-firstoverlap) > (std::min(nhit1,nhit2)-firstoverlap)*indivShareFrac_[collNum];
561 
562  auto seti = [&](unsigned int ii, unsigned int jj) {
563  selected[jj]=0;
564  selected[ii]=10+newQualityMask; // add 10 to avoid the case where mask = 1
565  trkUpdated[ii]=true;
566  if (trackAlgoPriorityOrder.priority(oriAlgo[jj]) < trackAlgoPriorityOrder.priority(oriAlgo[ii])) oriAlgo[ii] = oriAlgo[jj];
567  algoMask[ii] |= algoMask[jj];
568  algoMask[jj] = algoMask[ii]; // in case we keep discarded
569  };
570 
571  if ( dupfound ) {
572  float score2 = score[k2];
573  constexpr float almostSame = 0.01f; // difference rather than ratio due to possible negative values for score
574  if ( score1 - score2 > almostSame ) {
575  seti(i,j);
576  } else if ( score2 - score1 > almostSame ) {
577  seti(j,i);
578  }else{
579  // If tracks from both iterations are virtually identical, choose the one with the best quality or with lower algo
582  //same quality, pick earlier algo
583  if (trackAlgoPriorityOrder.priority(algo[k1]) <= trackAlgoPriorityOrder.priority(algo[k2])) {
584  seti(i,j);
585  } else {
586  seti(j,i);
587  }
588  } else if ((trackQuals[j] & (1<<reco::TrackBase::loose|1<<reco::TrackBase::tight|1<<reco::TrackBase::highPurity) ) <
590  seti(i,j);
591  }else{
592  seti(j,i);
593  }
594  }//end fi < fj
595  statCount.overlap();
596  /*
597  if (at0[k1]&&at0[k2]) {
598  statCount.dp(dphi);
599  if (dz<1.f) statCount.de(deta);
600  }
601  */
602  }//end got a duplicate
603  else {
604  statCount.noOverlap();
605  }
606  //stop if the ith track is now unselected
607  if (selected[i]==0) break;
608  }//end track2 loop
609  }//end track loop
610  } //end loop over track list sets
611 
612 
613 
614  auto vmMVA = std::make_unique<edm::ValueMap<float>>();
615  edm::ValueMap<float>::Filler fillerMVA(*vmMVA);
616 
617 
618 
619  // special case - if just doing the trkquals
620  if (trkQualMod_) {
621  unsigned int tSize=trackColls[0]->size();
622  auto vm = std::make_unique<edm::ValueMap<int>>();
624 
625  std::vector<int> finalQuals(tSize,-1); //default is unselected
626  for ( unsigned int i=0; i<rSize; i++) {
627  unsigned int tNum=i%tSize;
628 
629  if (selected[i]>1 ) {
630  finalQuals[tNum]=selected[i]-10;
631  if (trkUpdated[i])
632  finalQuals[tNum]=(finalQuals[tNum] | (1<<qualityToSet_));
633  }
634  if ( selected[i]==1 )
635  finalQuals[tNum]=trackQuals[i];
636  }
637 
638  filler.insert(trackHandles[0], finalQuals.begin(),finalQuals.end());
639  filler.fill();
640 
641  e.put(std::move(vm));
642  for (auto & q : finalQuals) q=std::max(q,0);
643  auto quals = std::make_unique<QualityMaskCollection>(finalQuals.begin(),finalQuals.end());
644  e.put(std::move(quals),"QualityMasks");
645 
646  std::vector<float> mvaVec(tSize,-99);
647 
648  for(unsigned int i = 0; i < rSize; i++){
649  unsigned int tNum = i % tSize;
650  mvaVec[tNum] = trackMVAs[tNum];
651  }
652 
653  fillerMVA.insert(trackHandles[0],mvaVec.begin(),mvaVec.end());
654  fillerMVA.fill();
655  if ( copyMVA_) {
656  e.put(std::move(vmMVA),"MVAVals");
657  auto mvas = std::make_unique<MVACollection>(mvaVec.begin(),mvaVec.end());
658  e.put(std::move(mvas),"MVAValues");
659  }
660  return;
661  }
662 
663 
664  //
665  // output selected tracks - if any
666  //
667 
668  std::vector<reco::TrackRef> trackRefs(rSize);
669  std::vector<edm::RefToBase<TrajectorySeed>> seedsRefs(rSize);
670 
671  unsigned int nToWrite=0;
672  for ( unsigned int i=0; i<rSize; i++)
673  if (selected[i]!=0) nToWrite++;
674 
675  std::vector<float> mvaVec;
676 
677  outputTrks = std::make_unique<reco::TrackCollection>();
678  outputTrks->reserve(nToWrite);
679  refTrks = e.getRefBeforePut<reco::TrackCollection>();
680 
681  if (copyExtras_) {
682  outputTrkExtras = std::make_unique<reco::TrackExtraCollection>();
683  outputTrkExtras->reserve(nToWrite);
684  refTrkExtras = e.getRefBeforePut<reco::TrackExtraCollection>();
685  outputTrkHits = std::make_unique<TrackingRecHitCollection>();
686  outputTrkHits->reserve(nToWrite*25);
687  refTrkHits = e.getRefBeforePut<TrackingRecHitCollection>();
688  if (makeReKeyedSeeds_){
689  outputSeeds = std::make_unique<TrajectorySeedCollection>();
690  outputSeeds->reserve(nToWrite);
691  refTrajSeeds = e.getRefBeforePut<TrajectorySeedCollection>();
692  }
693  }
694 
695 
696  outputTrajs = std::make_unique<std::vector<Trajectory>>();
697  outputTrajs->reserve(rSize);
698 
699  for ( unsigned int i=0; i<rSize; i++) {
700  if (selected[i]==0) {
701  trackRefs[i]=reco::TrackRef();
702  continue;
703  }
704 
705  unsigned int collNum=trackCollNum[i];
706  unsigned int trackNum=i-trackCollFirsts[collNum];
707  const reco::Track *track=&((trackColls[collNum])->at(trackNum));
708  outputTrks->push_back( reco::Track( *track ) );
709  mvaVec.push_back(trackMVAs[i]);
710  if (selected[i]>1 ) {
711  outputTrks->back().setQualityMask(selected[i]-10);
712  if (trkUpdated[i])
713  outputTrks->back().setQuality(qualityToSet_);
714  }
715  //might duplicate things, but doesnt hurt
716  if ( selected[i]==1 )
717  outputTrks->back().setQualityMask(trackQuals[i]);
718  outputTrks->back().setOriginalAlgorithm(oriAlgo[i]);
719  outputTrks->back().setAlgoMask(algoMask[i]);
720 
721  // if ( beVerb ) std::cout << "selected " << outputTrks->back().pt() << " " << outputTrks->back().qualityMask() << " " << selected[i] << std::endl;
722 
723  //fill the TrackCollection
724  if (copyExtras_) {
725  edm::RefToBase<TrajectorySeed> origSeedRef = track->seedRef();
726  //creating a seed with rekeyed clusters if required
727  if (makeReKeyedSeeds_){
728  bool doRekeyOnThisSeed=false;
729 
730  edm::InputTag clusterRemovalInfos("");
731  //grab on of the hits of the seed
732  if (origSeedRef->nHits()!=0){
733  TrajectorySeed::const_iterator firstHit=origSeedRef->recHits().first;
734  const TrackingRecHit *hit = &*firstHit;
735  if (firstHit->isValid()){
736  edm::ProductID pID=clusterProductB(hit);
737  // the cluster collection either produced a removalInfo or mot
738  //get the clusterremoval info from the provenance: will rekey if this is found
740  edm::Provenance prov=e.getProvenance(pID);
741  clusterRemovalInfos=edm::InputTag(prov.moduleLabel(),
742  prov.productInstanceName(),
743  prov.processName());
744  doRekeyOnThisSeed=e.getByLabel(clusterRemovalInfos,CRIh);
745  }//valid hit
746  }//nhit!=0
747 
748  if (doRekeyOnThisSeed && !(clusterRemovalInfos==edm::InputTag(""))) {
749  ClusterRemovalRefSetter refSetter(e,clusterRemovalInfos);
750  TrajectorySeed::recHitContainer newRecHitContainer;
751  newRecHitContainer.reserve(origSeedRef->nHits());
752  TrajectorySeed::const_iterator iH=origSeedRef->recHits().first;
753  TrajectorySeed::const_iterator iH_end=origSeedRef->recHits().second;
754  for (;iH!=iH_end;++iH){
755  newRecHitContainer.push_back(*iH);
756  refSetter.reKey(&newRecHitContainer.back());
757  }
758  outputSeeds->push_back( TrajectorySeed( origSeedRef->startingState(),
759  newRecHitContainer,
760  origSeedRef->direction()));
761  }
762  //doRekeyOnThisSeed=true
763  else{
764  //just copy the one we had before
765  outputSeeds->push_back( TrajectorySeed(*origSeedRef));
766  }
767  edm::Ref<TrajectorySeedCollection> pureRef(refTrajSeeds, outputSeeds->size()-1);
768  origSeedRef=edm::RefToBase<TrajectorySeed>( pureRef);
769  }//creating a new seed and rekeying it rechit clusters.
770 
771  // Fill TrackExtra collection
772  outputTrkExtras->push_back( reco::TrackExtra(
773  track->outerPosition(), track->outerMomentum(), track->outerOk(),
774  track->innerPosition(), track->innerMomentum(), track->innerOk(),
775  track->outerStateCovariance(), track->outerDetId(),
776  track->innerStateCovariance(), track->innerDetId(),
777  track->seedDirection(), origSeedRef ) );
778  seedsRefs[i]=origSeedRef;
779  outputTrks->back().setExtra( reco::TrackExtraRef( refTrkExtras, outputTrkExtras->size() - 1) );
780  reco::TrackExtra & tx = outputTrkExtras->back();
781  tx.setResiduals(track->residuals());
782 
783  // fill TrackingRecHits
784  unsigned nh1=track->recHitsSize();
785  tx.setHits(refTrkHits,outputTrkHits->size(),nh1);
786  tx.setTrajParams(track->extra()->trajParams(),track->extra()->chi2sX5());
787  assert(tx.trajParams().size()==tx.recHitsSize());
788  for (auto hh = track->recHitsBegin(), eh=track->recHitsEnd(); hh!=eh; ++hh ) {
789  outputTrkHits->push_back( (*hh)->clone() );
790  }
791  }
792  trackRefs[i] = reco::TrackRef(refTrks, outputTrks->size() - 1);
793 
794 
795  }//end faux loop over tracks
796 
797  //Fill the trajectories, etc. for 1st collection
798  refTrajs = e.getRefBeforePut< std::vector<Trajectory> >();
799 
800  outputTTAss = std::make_unique<TrajTrackAssociationCollection>(refTrajs, refTrks);
801 
802  for (unsigned int ti=0; ti<trackColls.size(); ti++) {
805  e.getByToken(trackProducers_[ti].traj, hTraj1);
806  e.getByToken(trackProducers_[ti].tass, hTTAss1);
807 
808  if (hTraj1.failedToGet() || hTTAss1.failedToGet()) continue;
809 
810  for (size_t i = 0, n = hTraj1->size(); i < n; ++i) {
811  edm::Ref< std::vector<Trajectory> > trajRef(hTraj1, i);
813  if (match != hTTAss1->end()) {
814  const edm::Ref<reco::TrackCollection> &trkRef = match->val;
815  uint32_t oldKey = trackCollFirsts[ti]+static_cast<uint32_t>(trkRef.key());
816  if (trackRefs[oldKey].isNonnull()) {
817  outputTrajs->push_back( *trajRef );
818  //if making extras and the seeds at the same time, change the seed ref on the trajectory
819  if (copyExtras_ && makeReKeyedSeeds_)
820  outputTrajs->back().setSeedRef( seedsRefs[oldKey] );
821  outputTTAss->insert ( edm::Ref< std::vector<Trajectory> >(refTrajs, outputTrajs->size() - 1),
822  trackRefs[oldKey] );
823  }
824  }
825  }
826  }
827 
828  statCount.end(outputTrks->size());
829 
830  edm::ProductID nPID = refTrks.id();
831  edm::TestHandle<reco::TrackCollection> outHandle(outputTrks.get(),nPID);
832  fillerMVA.insert(outHandle,mvaVec.begin(),mvaVec.end());
833  fillerMVA.fill();
834 
835  e.put(std::move(outputTrks));
836  if ( copyMVA_ ) {
837  e.put(std::move(vmMVA),"MVAVals");
838  auto mvas = std::make_unique<MVACollection>(mvaVec.begin(),mvaVec.end());
839  e.put(std::move(mvas),"MVAValues");
840  }
841  if (copyExtras_) {
842  e.put(std::move(outputTrkExtras));
843  e.put(std::move(outputTrkHits));
844  if (makeReKeyedSeeds_)
845  e.put(std::move(outputSeeds));
846  }
847  e.put(std::move(outputTrajs));
848  e.put(std::move(outputTTAss));
849  return;
850 
851  }//end produce
852 
853 
856 
858 
PropagationDirection direction() const
dbl * delta
Definition: mlp_gen.cc:36
const edm::RefToBase< TrajectorySeed > & seedRef() const
Definition: Track.h:213
T getParameter(std::string const &) const
#define dso_hidden
T getUntrackedParameter(std::string const &, T const &) const
TrackListMerger(const edm::ParameterSet &conf)
std::vector< unsigned char > QualityMaskCollection
std::unique_ptr< TrajTrackAssociationCollection > outputTTAss
reference back()
Definition: OwnVector.h:395
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
std::vector< bool > promoteQuality_
const TrackExtraRef & extra() const
reference to "extra" object
Definition: Track.h:189
const_iterator end() const
last iterator over the map (read only)
reco::TrackRefProd refTrks
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
edm::EDGetTokenT< TrajTrackAssociationCollection > tass
edm::RefProd< std::vector< Trajectory > > refTrajs
TkEDGetTokenss edTokens(const edm::InputTag &tag, const edm::InputTag &mvatag)
virtual void produce(Event &, EventSetup const &)=0
TrackQuality
track quality
Definition: TrackBase.h:151
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:119
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
std::string print(const Track &, edm::Verbosity=edm::Concise)
Track print utility.
Definition: print.cc:10
void reKey(TrackingRecHit *hit) const
const_iterator find(const key_type &k) const
find element with specified reference key
reco::TrackBase::TrackQuality qualityToSet_
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:50
unsigned short numberOfLostHits() const
number of cases where track crossed a layer without getting a hit.
Definition: TrackBase.h:826
std::unique_ptr< TrajectorySeedCollection > outputSeeds
key_type key() const
Accessor for product key.
Definition: Ref.h:265
std::string const & processName() const
Definition: Provenance.h:52
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
std::vector< float > MVACollection
edm::RefProd< TrajectorySeedCollection > refTrajSeeds
std::unique_ptr< std::vector< Trajectory > > outputTrajs
#define constexpr
ProductID id() const
Accessor for product ID.
Definition: Ref.h:259
TrackAlgorithm
track algorithm
Definition: TrackBase.h:99
TrackingRecHitRefProd refTrkHits
virtual void produce(edm::Event &e, const edm::EventSetup &c) override
std::vector< double > indivShareFrac_
bool overlap(const reco::Muon &muon1, const reco::Muon &muon2, double pullX=1.0, double pullY=1.0, bool checkAdjacentChambers=false)
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
TrackAlgorithm algo() const
Definition: TrackBase.h:497
#define unlikely(x)
#define likely(x)
void push_back(D *&d)
Definition: OwnVector.h:290
std::unique_ptr< TrackingRecHitCollection > outputTrkHits
edm::EDGetTokenT< edm::ValueMap< int > > tsel
std::vector< TrajectorySeed > TrajectorySeedCollection
double chi2() const
chi-squared of the fit
Definition: TrackBase.h:549
recHitContainer::const_iterator const_iterator
unsigned int recHitsSize() const
number of RecHits
TrajParams const & trajParams() const
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:75
edm::EDGetTokenT< reco::TrackCollection > tk
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
TkEDGetTokenss edTokens(const edm::InputTag &tag, const edm::InputTag &seltag, const edm::InputTag &mvatag)
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:820
#define end
Definition: vmac.h:37
T min(T a, T b)
Definition: MathUtil.h:58
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
virtual ~TrackListMerger()
virtual LocalPoint localPosition() const =0
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:416
RefProd< PROD > getRefBeforePut()
Definition: Event.h:134
std::unique_ptr< reco::TrackExtraCollection > outputTrkExtras
std::vector< int > hasSelector_
ii
Definition: cuy.py:588
bool failedToGet() const
Definition: HandleBase.h:78
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
double maxNormalizedChisq_
PTrajectoryStateOnDet const & startingState() const
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:125
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:70
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:45
const PropagationDirection & seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:204
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:446
edm::EDGetTokenT< edm::ValueMap< float > > tmva
auto dp
Definition: deltaR.h:22
const T & get() const
Definition: EventSetup.h:55
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:20
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:80
range recHits() const
bool isValid() const
std::string const & moduleLabel() const
Definition: Provenance.h:50
unsigned int nHits() const
#define begin
Definition: vmac.h:30
unsigned int minFound_
std::vector< TkEDGetTokenss > trackProducers_
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
const TrackResiduals & residuals() const
get the residuals
Definition: Track.h:218
int numberOfValidPixelHits() const
Definition: HitPattern.h:838
std::unique_ptr< reco::TrackCollection > outputTrks
edm::EDGetTokenT< std::vector< Trajectory > > traj
std::array< unsigned int, reco::TrackBase::algoSize > trackAlgoPriorityOrder
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
TkEDGetTokenss(const edm::InputTag &tag_, edm::EDGetTokenT< reco::TrackCollection > &&tk_, edm::EDGetTokenT< std::vector< Trajectory > > &&traj_, edm::EDGetTokenT< TrajTrackAssociationCollection > &&tass_, edm::EDGetTokenT< edm::ValueMap< int > > &&tsel_, edm::EDGetTokenT< edm::ValueMap< float > > &&tmva_)
DetId geographicalId() const
ProductIndex id() const
Definition: ProductID.h:38
std::string const & productInstanceName() const
Definition: Provenance.h:53
std::string priorityName_
std::vector< std::vector< int > > listsToMerge_
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:81
T x() const
Definition: PV3DBase.h:62
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
def move(src, dest)
Definition: eostools.py:510
id_type rawId() const
void reserve(size_t)
Definition: OwnVector.h:284
void setTrajParams(TrajParams tmps, Chi2sFive chi2s)
reco::TrackExtraRefProd refTrkExtras
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109
void setResiduals(const TrackResiduals &r)
set the residuals
Definition: TrackExtra.h:174