CMS 3D CMS Logo

TrackListMerger.cc

Go to the documentation of this file.
00001 //
00002 // Package:         RecoTracker/TrackListMerger
00003 // Class:           RoadSearchHelixMaker
00004 // 
00005 // Description:     TrackList Cleaner and Merger
00006 //
00007 // Original Author: Steve Wagner, stevew@pizero.colorado.edu
00008 // Created:         Sat Jan 14 22:00:00 UTC 2006
00009 //
00010 // $Author: stevew $
00011 // $Date: 2007/07/21 23:32:01 $
00012 // $Revision: 1.3 $
00013 //
00014 
00015 #include <memory>
00016 #include <string>
00017 #include <iostream>
00018 #include <cmath>
00019 #include <vector>
00020 
00021 #include "RecoTracker/RoadSearchHelixMaker/interface/TrackListMerger.h"
00022 
00023 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2DCollection.h"
00024 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2DCollection.h"
00025 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00026 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
00027 
00028 #include "DataFormats/Common/interface/Handle.h"
00029 #include "FWCore/Framework/interface/ESHandle.h"
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00032 
00033 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00034 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00035 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00036 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00037 
00038 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00039 #include "DataFormats/TrackReco/interface/Track.h"
00040 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00041 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00042 #include "DataFormats/TrackReco/src/classes.h"
00043 
00044 namespace cms
00045 {
00046 
00047   TrackListMerger::TrackListMerger(edm::ParameterSet const& conf) : 
00048     conf_(conf)
00049   {
00050     produces<reco::TrackCollection>();
00051 //    produces<reco::TrackExtraCollection>();
00052   }
00053 
00054 
00055   // Virtual destructor needed.
00056   TrackListMerger::~TrackListMerger() { }  
00057 
00058   // Functions that gets called by framework every event
00059   void TrackListMerger::produce(edm::Event& e, const edm::EventSetup& es)
00060   {
00061     // retrieve producer name of input TrackCollection(s)
00062     std::string trackProducer1 = conf_.getParameter<std::string>("TrackProducer1");
00063     std::string trackProducer2 = conf_.getParameter<std::string>("TrackProducer2");
00064 
00065     double maxNormalizedChisq =  conf_.getParameter<double>("MaxNormalizedChisq");
00066     double minPT =  conf_.getParameter<double>("MinPT");
00067     unsigned int minFound = (unsigned int)conf_.getParameter<int>("MinFound");
00068     double epsilon =  conf_.getParameter<double>("Epsilon");
00069     double shareFrac =  conf_.getParameter<double>("ShareFrac");
00070   
00071     //
00072     // extract tracker geometry
00073     //
00074     edm::ESHandle<TrackerGeometry> theG;
00075     es.get<TrackerDigiGeometryRecord>().get(theG);
00076 
00077 //    using namespace reco;
00078 
00079     // get Inputs 
00080     // if 1 input list doesn't exist, make an empty list, issue a warning, and continue
00081     // this allows TrackListMerger to be used as a cleaner only if handed just one list
00082     // if both input lists don't exist, will issue 2 warnings and generate an empty output collection
00083     //
00084     const reco::TrackCollection *TC1 = 0;
00085     try {
00086       edm::Handle<reco::TrackCollection> trackCollection1;
00087       e.getByLabel(trackProducer1, trackCollection1);
00088       TC1 = trackCollection1.product();
00089       //std::cout << "1st collection " << trackProducer1 << " has "<< TC1->size() << " tracks" << std::endl ;
00090     }
00091     catch (edm::Exception const& x) {
00092       if ( x.categoryCode() == edm::errors::ProductNotFound ) {
00093         if ( x.history().size() == 1 ) {
00094           static const reco::TrackCollection s_empty;
00095           TC1 = &s_empty;
00096           edm::LogWarning("TrackListMerger") << "1st TrackCollection " << trackProducer1 << " not found; will only clean 2nd TrackCollection " << trackProducer2 ;
00097         }
00098       }
00099     }
00100     const reco::TrackCollection tC1 = *TC1;
00101 
00102     const reco::TrackCollection *TC2 = 0;
00103     try {
00104       edm::Handle<reco::TrackCollection> trackCollection2;
00105       e.getByLabel(trackProducer2, trackCollection2);
00106       TC2 = trackCollection2.product();
00107       //std::cout << "2nd collection " << trackProducer2 << " has "<< TC2->size() << " tracks" << std::endl ;
00108     }
00109     catch (edm::Exception const& x) {
00110       if ( x.categoryCode() == edm::errors::ProductNotFound ) {
00111         if ( x.history().size() == 1 ) {
00112           static const reco::TrackCollection s_empty;
00113           TC2 = &s_empty;
00114           edm::LogWarning("TrackListMerger") << "2nd TrackCollection " << trackProducer2 << " not found; will only clean 1st TrackCollection " << trackProducer1 ;
00115         }
00116       }
00117     }
00118     const reco::TrackCollection tC2 = *TC2;
00119 
00120     // Step B: create empty output collection
00121     std::auto_ptr<reco::TrackCollection> output(new reco::TrackCollection);
00122 
00123   //
00124   //  no input tracks
00125   //
00126 
00127 //    if ( tC1.empty() ){
00128 //      LogDebug("RoadSearch") << "Found " << output.size() << " clouds.";
00129 //      e.put(output);
00130 //      return;  
00131 //    }
00132 
00133   //
00134   //  quality cuts first
00135   // 
00136     int i;
00137 
00138     std::vector<int> selected1; for (unsigned int i=0; i<tC1.size(); ++i){selected1.push_back(1);}
00139 
00140    if ( 0<tC1.size() ){
00141       i=-1;
00142       for (reco::TrackCollection::const_iterator track=tC1.begin(); track!=tC1.end(); track++){
00143         i++;
00144         if ((short unsigned)track->ndof() < 1){
00145           selected1[i]=0; 
00146           //std::cout << "L1Track "<< i << " rejected in TrackListMerger; ndof() < 1" << std::endl ;
00147           continue;
00148         }
00149         if (track->normalizedChi2() > maxNormalizedChisq){
00150           selected1[i]=0; 
00151           //std::cout << "L1Track "<< i << " rejected in TrackListMerger; normalizedChi2() > maxNormalizedChisq " << track->normalizedChi2() << " " << maxNormalizedChisq << std::endl ;
00152           continue;
00153         }
00154         if (track->found() < minFound){
00155           selected1[i]=0; 
00156           //std::cout << "L1Track "<< i << " rejected in TrackListMerger; found() < minFound " << track->found() << " " << minFound << std::endl ;
00157           continue;
00158         }
00159         if (track->pt() < minPT){
00160           selected1[i]=0; 
00161           //std::cout << "L1Track "<< i << " rejected in TrackListMerger; pt() < minPT " << track->pt() << " " << minPT << std::endl ;
00162           continue;
00163         }
00164       }//end loop over tracks
00165    }//end more than 0 track
00166 
00167 
00168     std::vector<int> selected2; for (unsigned int i=0; i<tC2.size(); ++i){selected2.push_back(1);}
00169 
00170    if ( 0<tC2.size() ){
00171       i=-1;
00172       for (reco::TrackCollection::const_iterator track=tC2.begin(); track!=tC2.end(); track++){
00173         i++;
00174         if ((short unsigned)track->ndof() < 1){
00175           selected2[i]=0; 
00176           //std::cout << "L2Track "<< i << " rejected in TrackListMerger; ndof() < 1" << std::endl ;
00177           continue;
00178         }
00179         if (track->normalizedChi2() > maxNormalizedChisq){
00180           selected2[i]=0; 
00181           //std::cout << "L2Track "<< i << " rejected in TrackListMerger; normalizedChi2() > maxNormalizedChisq " << track->normalizedChi2() << " " << maxNormalizedChisq << std::endl ;
00182           continue;
00183         }
00184         if (track->found() < minFound){
00185           selected2[i]=0; 
00186           //std::cout << "L2Track "<< i << " rejected in TrackListMerger; found() < minFound " << track->found() << " " << minFound << std::endl ;
00187           continue;
00188         }
00189         if (track->pt() < minPT){
00190           selected2[i]=0; 
00191           //std::cout << "L2Track "<< i << " rejected in TrackListMerger; pt() < minPT " << track->pt() << " " << minPT << std::endl ;
00192           continue;
00193         }
00194       }//end loop over tracks
00195    }//end more than 0 track
00196 
00197   //
00198   //  L1 has > 1 track - try merging
00199   //
00200    if ( 1<tC1.size() ){
00201     i=-1;
00202     for (reco::TrackCollection::const_iterator track=tC1.begin(); track!=tC1.end(); track++){
00203       i++; 
00204       //std::cout << "Track number "<< i << std::endl ; 
00205       if (!selected1[i])continue;
00206       int j=-1;
00207       for (reco::TrackCollection::const_iterator track2=tC1.begin(); track2!=tC1.end(); track2++){
00208         j++;
00209         if ((j<=i)||(!selected1[j])||(!selected1[i]))continue;
00210         int noverlap=0;
00211         for (trackingRecHit_iterator it = track->recHitsBegin();  it != track->recHitsEnd(); it++){
00212           if ((*it)->isValid()){
00213             for (trackingRecHit_iterator jt = track2->recHitsBegin();  jt != track2->recHitsEnd(); jt++){
00214               if ((*jt)->isValid()){
00215                 if (((*it)->geographicalId()==(*jt)->geographicalId())&&((*it)->localPosition().x()==(*jt)->localPosition().x()))noverlap++;
00216               }
00217             }
00218           }
00219         }
00220         float fi=float(noverlap)/float(track->recHitsSize()); float fj=float(noverlap)/float(track2->recHitsSize());
00221         //std::cout << " trk1 trk2 nhits1 nhits2 nover " << i << " " << j << " " << track->recHitsSize() << " "  << track2->recHitsSize() << " " << noverlap << " " << fi << " " << fj  <<std::endl;
00222         if ((fi>shareFrac)||(fj>shareFrac)){
00223           if (fi<fj){
00224             selected1[j]=0; 
00225             //std::cout << " removing 2nd trk in pair " << std::endl;
00226           }else{
00227             if (fi>fj){
00228               selected1[i]=0; 
00229               //std::cout << " removing 1st trk in pair " << std::endl;
00230             }else{
00231               //std::cout << " removing worst chisq in pair " << std::endl;
00232               if (track->normalizedChi2() > track2->normalizedChi2()){selected1[i]=0;}else{selected1[j]=0;}
00233             }//end fi > or = fj
00234           }//end fi < fj
00235         }//end got a duplicate
00236       }//end track2 loop
00237     }//end track loop
00238    }//end more than 1 track
00239 
00240   //
00241   //  L2 has > 1 track - try merging
00242   //
00243    if ( 1<tC2.size() ){
00244     int i=-1;
00245     for (reco::TrackCollection::const_iterator track=tC2.begin(); track!=tC2.end(); track++){
00246       i++; 
00247       //std::cout << "Track number "<< i << std::endl ; 
00248       if (!selected2[i])continue;
00249       int j=-1;
00250       for (reco::TrackCollection::const_iterator track2=tC2.begin(); track2!=tC2.end(); track2++){
00251         j++;
00252         if ((j<=i)||(!selected2[j])||(!selected2[i]))continue;
00253         int noverlap=0;
00254         for (trackingRecHit_iterator it = track->recHitsBegin();  it != track->recHitsEnd(); it++){
00255           if ((*it)->isValid()){
00256             for (trackingRecHit_iterator jt = track2->recHitsBegin();  jt != track2->recHitsEnd(); jt++){
00257               if ((*jt)->isValid()){
00258                 if (((*it)->geographicalId()==(*jt)->geographicalId())&&((*it)->localPosition().x()==(*jt)->localPosition().x()))noverlap++;
00259               }
00260             }
00261           }
00262         }
00263         float fi=float(noverlap)/float(track->recHitsSize()); float fj=float(noverlap)/float(track2->recHitsSize());
00264         //std::cout << " trk1 trk2 nhits1 nhits2 nover " << i << " " << j << " " << track->recHitsSize() << " "  << track2->recHitsSize() << " " << noverlap << " " << fi << " " << fj  <<std::endl;
00265         if ((fi>shareFrac)||(fj>shareFrac)){
00266           if (fi<fj){
00267             selected2[j]=0; 
00268             //std::cout << " removing 2nd trk in pair " << std::endl;
00269           }else{
00270             if (fi>fj){
00271               selected2[i]=0; 
00272               //std::cout << " removing 1st trk in pair " << std::endl;
00273             }else{
00274               //std::cout << " removing worst chisq in pair " << std::endl;
00275               if (track->normalizedChi2() > track2->normalizedChi2()){selected2[i]=0;}else{selected2[j]=0;}
00276             }//end fi > or = fj
00277           }//end fi < fj
00278         }//end got a duplicate
00279       }//end track2 loop
00280     }//end track loop
00281    }//end more than 1 track
00282 
00283   //
00284   //  L1 and L2 both have > 0 track - try merging
00285   //
00286    if ( (0<tC1.size())&&(0<tC2.size()) ){
00287     i=-1;
00288     for (reco::TrackCollection::const_iterator track=tC1.begin(); track!=tC1.end(); track++){
00289       i++; 
00290       //std::cout << "L1 Track number "<< i << std::endl ; 
00291       if (!selected1[i])continue;
00292       int j=-1;
00293       for (reco::TrackCollection::const_iterator track2=tC2.begin(); track2!=tC2.end(); track2++){
00294         j++;
00295         if ((!selected2[j])||(!selected1[i]))continue;
00296         int noverlap=0;
00297         for (trackingRecHit_iterator it = track->recHitsBegin();  it != track->recHitsEnd(); it++){
00298           if ((*it)->isValid()){
00299             for (trackingRecHit_iterator jt = track2->recHitsBegin();  jt != track2->recHitsEnd(); jt++){
00300               if ((*jt)->isValid()){
00301                 float delta = fabs ( (*it)->localPosition().x()-(*jt)->localPosition().x() ); 
00302                 if (((*it)->geographicalId()==(*jt)->geographicalId())&&(delta<epsilon))noverlap++;
00303               }
00304             }
00305           }
00306         }
00307         float fi=float(noverlap)/float(track->recHitsSize()); float fj=float(noverlap)/float(track2->recHitsSize());
00308         //std::cout << " trk1 trk2 nhits1 nhits2 nover " << i << " " << j << " " << track->recHitsSize() << " "  << track2->recHitsSize() << " " << noverlap << " " << fi << " " << fj  <<std::endl;
00309         if ((fi>shareFrac)||(fj>shareFrac)){
00310           if (fi<fj){
00311             selected2[j]=0; 
00312             //std::cout << " removing L2 trk in pair " << std::endl;
00313           }else{
00314             if (fi>fj){
00315               selected1[i]=0; 
00316               //std::cout << " removing L1 trk in pair " << std::endl;
00317             }else{
00318               //std::cout << " removing worst chisq in pair " << track->normalizedChi2() << " " << track2->normalizedChi2() << std::endl;
00319               if (track->normalizedChi2() > track2->normalizedChi2()){selected1[i]=0;}else{selected2[j]=0;}
00320             }//end fi > or = fj
00321           }//end fi < fj
00322         }//end got a duplicate
00323       }//end track2 loop
00324     }//end track loop
00325    }//end more than 1 track
00326 
00327   //
00328   //  output selected tracks - if any
00329   //
00330    if ( 0<tC1.size() ){
00331     i=-1;
00332     for (reco::TrackCollection::const_iterator track=tC1.begin(); track!=tC1.end(); track++){
00333       i++;  if (!selected1[i])continue;
00334         reco::Track * theTrack = new reco::Track(track->chi2(),
00335                                                  (short unsigned)track->ndof(),
00336                                                  track->innerPosition(),
00337                                                  track->innerMomentum(),
00338                                                  track->charge(),
00339                                                  track->innerStateCovariance());    
00340       //fill the TrackCollection
00341       reco::TrackExtraRef theTrackExtraRef=track->extra();    
00342       theTrack->setExtra(theTrackExtraRef);    
00343       theTrack->setHitPattern((*theTrackExtraRef).recHits());
00344       output->push_back(*theTrack);
00345       delete theTrack;
00346     }//end faux loop over tracks
00347    }//end more than 0 track
00348    if ( 0<tC2.size() ){
00349     i=-1;
00350     for (reco::TrackCollection::const_iterator track=tC2.begin(); track!=tC2.end(); track++){
00351       i++;  if (!selected2[i])continue;
00352         reco::Track * theTrack = new reco::Track(track->chi2(),
00353                                                  (short unsigned)track->ndof(),
00354                                                  track->innerPosition(),
00355                                                  track->innerMomentum(),
00356                                                  track->charge(),
00357                                                  track->innerStateCovariance());    
00358       //fill the TrackCollection
00359       reco::TrackExtraRef theTrackExtraRef=track->extra();    
00360       theTrack->setExtra(theTrackExtraRef);    
00361       theTrack->setHitPattern((*theTrackExtraRef).recHits());
00362       output->push_back(*theTrack);
00363       delete theTrack;
00364     }//end faux loop over tracks
00365    }//end more than 0 track
00366 
00367 
00368     e.put(output);
00369     return;
00370 
00371   }//end produce
00372 
00373 }

Generated on Tue Jun 9 17:45:40 2009 for CMSSW by  doxygen 1.5.4