CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/RecoEgamma/EgammaPhotonAlgos/src/ConversionTrackPairFinder.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 #include <memory>
00004 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionTrackPairFinder.h"
00005 // Framework
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 //
00008 
00009 //
00010 #include <vector>
00011 #include <map>
00012 
00013 
00014 //using namespace std;
00015 
00016 ConversionTrackPairFinder::ConversionTrackPairFinder( ){ 
00017 
00018   LogDebug("ConversionTrackPairFinder") << " CTOR  " <<  "\n";  
00019 
00020 }
00021 
00022 ConversionTrackPairFinder::~ConversionTrackPairFinder() {
00023 
00024   LogDebug("ConversionTrackPairFinder") << " DTOR " <<  "\n";  
00025     
00026 }
00027 
00028 
00029 
00030 
00031  std::map<std::vector<reco::TransientTrack>,  reco::CaloClusterPtr, CompareTwoTracksVectors>  ConversionTrackPairFinder::run(std::vector<reco::TransientTrack> outInTrk,  
00032                                                                                                         const edm::Handle<reco::TrackCollection>& outInTrkHandle,
00033                                                                                                         const edm::Handle<reco::TrackCaloClusterPtrAssociation>& outInTrackSCAssH, 
00034                                                                                                         std::vector<reco::TransientTrack> inOutTrk, 
00035                                                                                                         const edm::Handle<reco::TrackCollection>& inOutTrkHandle,
00036                                                                                                         const edm::Handle<reco::TrackCaloClusterPtrAssociation>& inOutTrackSCAssH  ) 
00037 {
00038 
00039   
00040   LogDebug("ConversionTrackPairFinder")  << "ConversionTrackPairFinder::run " <<  "\n";  
00041   
00042   std::vector<reco::TransientTrack>  selectedOutInTk;
00043   std::vector<reco::TransientTrack>  selectedInOutTk;
00044   std::vector<reco::TransientTrack>  allSelectedTk;
00045   std::map<reco::TransientTrack,  reco::CaloClusterPtr,CompareTwoTracks> scTrkAssocMap; 
00046   std::multimap<int,reco::TransientTrack,std::greater<int> >  auxMap; 
00047  
00048   bool oneLeg=false;
00049   bool noTrack=false;  
00050   
00051   
00052   
00053   int iTrk=0;
00054   for( std::vector<reco::TransientTrack>::iterator  iTk =  outInTrk.begin(); iTk !=  outInTrk.end(); iTk++) {
00055     edm::Ref<reco::TrackCollection> trackRef(outInTrkHandle, iTrk );    
00056     iTrk++;
00057     
00058     if ( iTk->numberOfValidHits() <3 ||   iTk->normalizedChi2() > 5000 ) continue; 
00059     if ( fabs(iTk->impactPointState().globalPosition().x()) > 110 ||
00060          fabs(iTk->impactPointState().globalPosition().y()) > 110 ||
00061          fabs(iTk->impactPointState().globalPosition().z()) > 280 ) continue;
00062     
00063     //    std::cout  << " Out In Track charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner pt  " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";  
00064     const reco::TrackTransientTrack* ttt = dynamic_cast<const reco::TrackTransientTrack*>(iTk->basicTransientTrack());
00065     reco::TrackRef myTkRef= ttt->persistentTrackRef(); 
00066     //std::cout <<  " ConversionTrackPairFinder persistent track ref hits " << myTkRef->recHitsSize() << " inner pt  " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";  
00067     //    std::cout <<  " ConversionTrackPairFinder track from handle hits " << trackRef->recHitsSize() << " inner pt  " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";  
00068 
00069     const reco::CaloClusterPtr  aClus = (*outInTrackSCAssH)[trackRef];
00070 
00071     //    std::cout << "ConversionTrackPairFinder  Reading the OutIn Map  " << *outInTrackSCAss[trackRef] <<  " " << &outInTrackSCAss[trackRef] <<  std::endl;
00072     //    std::cout << "ConversionTrackPairFinder  Out In track belonging to SC with energy " << aClus->energy() << "\n"; 
00073 
00074     int nHits=iTk->recHitsSize();
00075     scTrkAssocMap[*iTk]= aClus;
00076     auxMap.insert(std::pair<int,reco::TransientTrack >(nHits,(*iTk)) );
00077     selectedOutInTk.push_back(*iTk);
00078     allSelectedTk.push_back(*iTk);
00079 
00080 
00081     
00082   }
00083 
00084 
00085   iTrk=0;
00086   for(  std::vector<reco::TransientTrack>::iterator  iTk =  inOutTrk.begin(); iTk !=  inOutTrk.end(); iTk++) {
00087     edm::Ref<reco::TrackCollection> trackRef(inOutTrkHandle, iTrk );
00088     iTrk++;
00089     
00090     if ( iTk->numberOfValidHits() <3 ||   iTk->normalizedChi2() >5000 ) continue; 
00091     if ( fabs(iTk->impactPointState().globalPosition().x()) > 110 ||
00092          fabs(iTk->impactPointState().globalPosition().y()) > 110 ||
00093          fabs(iTk->impactPointState().globalPosition().z()) > 280 ) continue;
00094     
00095     //    std::cout << " In Out Track charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner pt  " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";   
00096     const reco::TrackTransientTrack* ttt = dynamic_cast<const reco::TrackTransientTrack*>(iTk->basicTransientTrack());
00097     reco::TrackRef myTkRef= ttt->persistentTrackRef(); 
00098     // std::cout <<  " ConversionTrackPairFinder persistent track ref hits " << myTkRef->recHitsSize() << " inner pt  " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";  
00099     //    std::cout <<  " ConversionTrackPairFinder track from handle hits " << trackRef->recHitsSize() << " inner pt  " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";  
00100     
00101     const reco::CaloClusterPtr  aClus = (*inOutTrackSCAssH)[trackRef];
00102 
00103     //    std::cout << "ConversionTrackPairFinder  Filling the InOut Map  " << &(*inOutTrackSCAss[trackRef]) << " " << &inOutTrackSCAss[trackRef] <<  std::endl;
00104     // std::cout << "ConversionTrackPairFinder  In Out  track belonging to SC with energy " << aClus.energy() << "\n"; 
00105     
00106     scTrkAssocMap[*iTk]= aClus;
00107     int nHits=iTk->recHitsSize();
00108     auxMap.insert(std::pair<int,reco::TransientTrack >(nHits,(*iTk)) );
00109     selectedInOutTk.push_back(*iTk);
00110     allSelectedTk.push_back(*iTk);    
00111 
00112 
00113     
00114   }
00115   
00116 
00117   //  std::cout << " ConversionTrackPairFinder allSelectedTk size " << allSelectedTk.size() << "  scTrkAssocMap  size " <<  scTrkAssocMap.size() << "\n"; 
00118   
00119   // Sort tracks in decreasing number of hits
00120   if(selectedOutInTk.size() > 0)
00121     std::stable_sort(selectedOutInTk.begin(), selectedOutInTk.end(), ByNumOfHits());
00122   if(selectedInOutTk.size() > 0)
00123     std::stable_sort(selectedInOutTk.begin(), selectedInOutTk.end(), ByNumOfHits());
00124   if(allSelectedTk.size() > 0)
00125     std::stable_sort(allSelectedTk.begin(),   allSelectedTk.end(),   ByNumOfHits());
00126   
00127   
00128   
00129   //  for(  std::vector<reco::TransientTrack>::const_iterator  iTk =  selectedOutInTk.begin(); iTk !=  selectedOutInTk.end(); iTk++) {
00130     // std::cout << " Selected Out In  Tracks charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner momentum " << iTk->track().innerMomentum() << "\n";  
00131   //}
00132   //  for(  std::vector<reco::TransientTrack>::const_iterator  iTk =  selectedInOutTk.begin(); iTk !=  selectedInOutTk.end(); iTk++) {
00133   // std::cout << " Selected In Out Tracks charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner momentum " << iTk->track().innerMomentum() << "\n";  
00134   // }
00135   //  for(  std::vector<reco::TransientTrack>::const_iterator  iTk =  allSelectedTk.begin(); iTk !=  allSelectedTk.end(); iTk++) {
00136   // std::cout << " All Selected  Tracks charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " chi2 " << iTk->normalizedChi2() <<  " pt " <<  sqrt(iTk->track().innerMomentum().perp2()) << "\n";  
00137   //}
00138   
00139 
00140   
00141   
00142   std::vector<reco::TransientTrack > thePair(2);
00143   std::vector<std::vector<reco::TransientTrack> > allPairs;
00144 
00145   std::map<std::vector<reco::TransientTrack>,  reco::CaloClusterPtr, CompareTwoTracksVectors > allPairSCAss;
00146   std::map<std::vector<reco::TransientTrack>,  reco::CaloClusterPtr, CompareTwoTracksVectors> allPairOrdInPtSCAss;
00147   std::map<reco::TransientTrack,  reco::CaloClusterPtr>::const_iterator iMap1;
00148   std::map<reco::TransientTrack,  reco::CaloClusterPtr>::const_iterator iMap2;
00149 
00150  for( iMap1 =   scTrkAssocMap.begin(); iMap1 !=   scTrkAssocMap.end(); ++iMap1) {
00151  
00152    //   std::cout << " Ass map track  charge " << (iMap1->first).charge()  <<" pt " << sqrt(((iMap1->first)).track().innerMomentum().Perp2()) << " SC E  " << (iMap1->second)->energy() << " SC eta " << (iMap1->second)->eta() <<  " SC phi " << (iMap1->second)->phi() << std::endl;
00153   }
00154 
00155 
00156  std::multimap<int, reco::TransientTrack>::const_iterator iAux;
00157 
00158 
00159  // for( iAux = auxMap.begin(); iAux!= auxMap.end(); ++iAux) {
00160  //  //   std::cout << " Aux Map  " << (iAux->first)  <<" pt " << sqrt(((iAux->second)).track().innerMomentum().Perp2()) << std::endl;
00161  // for( iMap1 =   scTrkAssocMap.begin(); iMap1 !=   scTrkAssocMap.end(); ++iMap1) {
00162  //   if ( (iMap1->first) == (iAux->second) ) std::cout << " ass SC " <<  (iMap1->second)->energy() << std::endl;
00163  // }
00164  // }
00165 
00166   if ( scTrkAssocMap.size() > 2 ){
00167     
00168 
00169     for( iMap1 =   scTrkAssocMap.begin(); iMap1 !=   scTrkAssocMap.end(); ++iMap1) {
00170       for( iMap2 =  iMap1; iMap2 !=   scTrkAssocMap.end(); ++iMap2) {
00171         // consider only tracks associated to the same SC 
00172 
00173         if (  (iMap1->second) != (iMap2->second) ) continue;  
00174         
00175         if (   ((iMap1->first)).charge() *  ((iMap2->first)).charge()  < 0 ) {
00176           
00177           //      std::cout << " ConversionTrackPairFinde All selected from the map First  Track charge " <<   (iMap1->first).charge() << " Num of RecHits " <<  ((iMap1->first)).recHitsSize() << " inner pt " <<  sqrt(((iMap1->first)).track().innerMomentum().Perp2()) << " Ass SC " << (iMap1->second)->energy() <<  "\n";  
00178           
00179           //  std::cout << " ConversionTrackPairFinde All selected from the map Second  Track charge " <<   ((iMap2->first)).charge() << " Num of RecHits " <<  ((iMap2->first)).recHitsSize() << " inner pt " <<  sqrt(((iMap2->first)).track().innerMomentum().Perp2()) << " Ass SC " << (iMap2->second)->energy()  <<  "\n";  
00180 
00181 
00182 
00183           thePair.clear();
00184           thePair.push_back( iMap1->first );
00185           thePair.push_back( iMap2->first  );
00186           allPairs.push_back ( thePair );       
00187           allPairSCAss[thePair]= iMap1->second; 
00188 
00189         }
00190       }
00191     }
00192 
00193     //    std::cout << " ConversionTrackPairFinder  INTERMIDIATE allPairSCAss size " << allPairSCAss.size() << "\n";
00194 
00195     if ( allPairSCAss.size() == 0) { 
00196       //      std::cout << " All Tracks had the same charge: Need to send out a single track  " <<   "\n";
00197 
00198       for( iMap1 =   scTrkAssocMap.begin(); iMap1 !=   scTrkAssocMap.end(); ++iMap1) {
00199 
00200         thePair.clear();
00201         thePair.push_back(iMap1->first);
00202         allPairs.push_back ( thePair );
00203         allPairSCAss[thePair]= iMap1->second; 
00204         
00205       }
00206 
00207     }
00208 
00209 
00210 
00211 
00212 
00213   } else if (  (scTrkAssocMap.size() ==2) ) {
00214     
00215     iMap1=scTrkAssocMap.begin();//get the first
00216     iMap2=iMap1;
00217     iMap2++;//get the second
00218     if (  (iMap1->second) == (iMap2->second)  ) {
00219       if  ( (iMap1->first).charge() * (iMap2->first).charge() < 0 )  {
00220         
00221         //      std::cout << " ConversionTrackPairFinder Case when  (scTrkAssocMap.size() ==2)  " <<   (iMap1->first).charge() << std::endl;
00222         //std::cout << " Num of RecHits " <<  ((iMap1->first)).recHitsSize() << std::endl;
00223         //      std::cout << " inner pt " <<  sqrt(((iMap1->first)).track().innerMomentum().Perp2()) << std::endl; 
00224         //std::cout << " Ass SC " << (iMap1->second)->energy() <<  "\n";  
00225 
00226         //      std::cout << " ConversionTrackPairFinder Case when  (scTrkAssocMap.size() ==2)  " <<   (iMap2->first).charge() << std::endl;
00227         //      std::cout << " Num of RecHits " <<  ((iMap2->first)).recHitsSize() << std::endl;
00228         //std::cout << " inner pt " <<  sqrt(((iMap2->first)).track().innerMomentum().Perp2()) << std::endl; 
00229         //std::cout << " Ass SC " << (iMap2->second)->energy() <<  "\n";  
00230         
00231         thePair.clear();
00232         thePair.push_back( iMap1->first );
00233         thePair.push_back( iMap2->first );
00234         allPairs.push_back ( thePair ); 
00235         
00236         allPairSCAss[thePair]= iMap1->second; 
00237 
00238 
00239       } else {
00240         //std::cout << " ConversionTrackPairFinder oneLeg case when 2 tracks with same sign Pick up the longest one" << std::endl;
00241         if (  ((iMap1->first)).recHitsSize() > ((iMap2->first)).recHitsSize() ) {
00242           thePair.clear();
00243           thePair.push_back(iMap1->first);
00244           allPairs.push_back ( thePair );
00245           allPairSCAss[thePair]= iMap1->second; 
00246         } else {
00247           thePair.clear();
00248           thePair.push_back(iMap2->first);
00249           allPairs.push_back ( thePair );
00250           allPairSCAss[thePair]= iMap2->second;
00251         }
00252       }
00253       
00254     }
00255 
00256   } else if  (scTrkAssocMap.size() ==1   ) { 
00257     //    std::cout << " ConversionTrackPairFinder oneLeg case when 1 track only " << std::endl;
00258     oneLeg=true;  
00259   } else {
00260     noTrack=true;
00261   } 
00262   
00263   
00264   if ( oneLeg ) {
00265     thePair.clear();               
00266     // std::cout << " ConversionTrackPairFinder oneLeg case charge  " << std::endl;
00267                                                       
00268                                                       
00269     iMap1=scTrkAssocMap.begin();   
00270                                                       
00271     //std::cout << " ConversionTrackPairFinder oneLeg case charge  " <<   (iMap1->first).charge() << " Num of RecHits " <<  ((iMap1->first)).recHitsSize() << " inner pt " <<  sqrt(((iMap1->first)).track().innerMomentum().Perp2()) << " Ass SC " << (iMap1->second)->energy() <<  "\n";  
00272                                                       
00273     thePair.push_back(iMap1->first);
00274     allPairs.push_back ( thePair );
00275     allPairSCAss[thePair]= iMap1->second; 
00276 
00277     // std::cout << "  WARNING ConversionTrackPairFinder::tracks The candidate has just one leg. Need to find another way to evaltuate the vertex !!! "   << "\n";
00278   }
00279   
00280   if ( noTrack) {
00281     //    std::cout << "  WARNING ConversionTrackPairFinder::tracks case noTrack "   << "\n";  
00282     thePair.clear();  
00283     allPairSCAss.clear();
00284   }
00285   
00286 
00287 
00289   for( iMap1 =   scTrkAssocMap.begin(); iMap1 !=   scTrkAssocMap.end(); ++iMap1) {
00290     
00291     int nFound=0;
00292     for (  std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr>::const_iterator iPair= allPairSCAss.begin(); iPair!= allPairSCAss.end(); ++iPair ) {
00293       if (  (iMap1->second) == (iPair->second)  ) nFound++;
00294     }
00295     
00296     if ( nFound == 0) {
00297       //      std::cout << " nFound zero case " << std::endl;
00298       int iList=0;
00299       for( iAux = auxMap.begin(); iAux!= auxMap.end(); ++iAux) {
00300         if ( (iMap1->first) == (iAux->second) && iList==0 )  {
00301           thePair.clear();   
00302           thePair.push_back(iAux->second);
00303           allPairSCAss[thePair]= iMap1->second;         
00304           
00305         }
00306         
00307         iList++;
00308       }  
00309     }
00310     
00311   }
00312 
00313 
00314 
00315 
00316   // order the tracks in the pair in order of decreasing pt 
00317     for (  std::map<std::vector<reco::TransientTrack>,  reco::CaloClusterPtr>::const_iterator iPair= allPairSCAss.begin(); iPair!= allPairSCAss.end(); ++iPair ) {
00318       thePair.clear();
00319       if ( (iPair->first).size() ==2 ) {
00320         if (  sqrt((iPair->first)[0].track().innerMomentum().perp2()) > sqrt((iPair->first)[1].track().innerMomentum().perp2())  ) {
00321           thePair.push_back((iPair->first)[0]);
00322           thePair.push_back((iPair->first)[1]);
00323         } else {
00324           thePair.push_back((iPair->first)[1]);
00325           thePair.push_back((iPair->first)[0]);
00326         }
00327       } else {
00328         thePair.push_back((iPair->first)[0]);
00329       }
00330 
00331       allPairOrdInPtSCAss[thePair]=iPair->second;
00332     }
00333 
00334 
00335     //    std::cout << " ConversionTrackPairFinder FINAL allPairOrdInPtSCAss size " << allPairOrdInPtSCAss.size() << "\n";
00336     // for (  std::map<std::vector<reco::TransientTrack>,  reco::CaloClusterPtr>::const_iterator iPair= allPairOrdInPtSCAss.begin(); iPair!= allPairOrdInPtSCAss.end(); ++iPair ) {
00337     // std::cout << " ConversionTrackPairFindder FINAL allPairOrdInPtSCAss " << (iPair->first).size() << " SC Energy " << (iPair->second)->energy() << " eta " << (iPair->second)->eta() << " phi " <<  (iPair->second)->phi() << "\n";  
00338     // std::cout << " ConversionTrackPairFindder FINAL allPairOrdInPtSCAss (iPair->first).size() " << (iPair->first).size() << std::endl;
00339       
00340     // for ( std::vector<reco::TransientTrack>::const_iterator iTk=(iPair->first).begin(); iTk!= (iPair->first).end(); ++iTk) {
00341     //  std::cout << " ConversionTrackPair ordered track pt " << sqrt(iTk->track().innerMomentum().perp2()) << std::endl;
00342     // }
00343     //}
00344 
00345   
00346 
00347   return allPairOrdInPtSCAss;
00348  
00349 
00350 }
00351 
00352 
00353 
00354 
00355 
00356 
00357 
00358