CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/SimTracker/TrackAssociation/src/TrackAssociatorByChi2.cc

Go to the documentation of this file.
00001 #include "SimTracker/TrackAssociation/interface/TrackAssociatorByChi2.h"
00002 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
00003 
00004 #include "DataFormats/Math/interface/deltaPhi.h"
00005 #include "DataFormats/GeometrySurface/interface/Line.h"
00006 #include "DataFormats/GeometryVector/interface/Pi.h"
00007 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00008 #include "TrackingTools/PatternTools/interface/TSCBLBuilderNoMaterial.h"
00009 
00010 using namespace edm;
00011 using namespace reco;
00012 using namespace std;
00013 
00014 double TrackAssociatorByChi2::compareTracksParam ( TrackCollection::const_iterator rt, 
00015                                                    SimTrackContainer::const_iterator st, 
00016                                                    const math::XYZTLorentzVectorD vertexPosition, 
00017                                                    GlobalVector magField,
00018                                                    TrackBase::CovarianceMatrix invertedCovariance,
00019                                                    const reco::BeamSpot& bs) const{
00020   
00021   Basic3DVector<double> momAtVtx(st->momentum().x(),st->momentum().y(),st->momentum().z());
00022   Basic3DVector<double> vert = (Basic3DVector<double>) vertexPosition;
00023 
00024   std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, st->charge(), bs);
00025   if (params.first){
00026     TrackBase::ParameterVector sParameters = params.second;
00027     TrackBase::ParameterVector rParameters = rt->parameters();
00028 
00029     TrackBase::ParameterVector diffParameters = rParameters - sParameters;
00030     diffParameters[2] = reco::deltaPhi(diffParameters[2],0.f);
00031     double chi2 = ROOT::Math::Dot(diffParameters * invertedCovariance, diffParameters);
00032     
00033     return chi2;
00034   } else {
00035     return 10000000000.;
00036   }
00037 }
00038 
00039 
00040 TrackAssociatorByChi2::RecoToSimPairAssociation 
00041 TrackAssociatorByChi2::compareTracksParam(const TrackCollection& rtColl,
00042                                           const SimTrackContainer& stColl,
00043                                           const SimVertexContainer& svColl,
00044                                           const reco::BeamSpot& bs) const{
00045   
00046   RecoToSimPairAssociation outputVec;
00047 
00048   for (TrackCollection::const_iterator track=rtColl.begin(); track!=rtColl.end(); track++){
00049      Chi2SimMap outMap;
00050 
00051     TrackBase::ParameterVector rParameters = track->parameters();
00052 
00053     TrackBase::CovarianceMatrix recoTrackCovMatrix = track->covariance();
00054     if (onlyDiagonal){
00055       for (unsigned int i=0;i<5;i++){
00056         for (unsigned int j=0;j<5;j++){
00057           if (i!=j) recoTrackCovMatrix(i,j)=0;
00058         }
00059       }
00060     }
00061     recoTrackCovMatrix.Invert();
00062 
00063     for (SimTrackContainer::const_iterator st=stColl.begin(); st!=stColl.end(); st++){
00064 
00065       Basic3DVector<double> momAtVtx(st->momentum().x(),st->momentum().y(),st->momentum().z());
00066       Basic3DVector<double> vert = (Basic3DVector<double>)  svColl[st->vertIndex()].position();
00067 
00068       std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, st->charge(), bs);
00069       if (params.first){
00070         TrackBase::ParameterVector sParameters = params.second;
00071       
00072         TrackBase::ParameterVector diffParameters = rParameters - sParameters;
00073         diffParameters[2] = reco::deltaPhi(diffParameters[2],0.f);
00074         double chi2 = ROOT::Math::Dot(diffParameters * recoTrackCovMatrix, diffParameters);
00075         chi2/=5;
00076         if (chi2<chi2cut) outMap[chi2]=*st;
00077       }
00078     }
00079     outputVec.push_back(RecoToSimPair(*track,outMap));
00080   }
00081   return outputVec;
00082 }
00083 
00084 double TrackAssociatorByChi2::getChi2(TrackBase::ParameterVector& rParameters,
00085                                       TrackBase::CovarianceMatrix& recoTrackCovMatrix,
00086                                       Basic3DVector<double>& momAtVtx,
00087                                       Basic3DVector<double>& vert,
00088                                       int& charge,
00089                                       const reco::BeamSpot& bs) const{
00090   
00091   double chi2;
00092   
00093   std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, charge, bs);
00094   if (params.first){
00095     TrackBase::ParameterVector sParameters=params.second;
00096     
00097     TrackBase::ParameterVector diffParameters = rParameters - sParameters;
00098     diffParameters[2] = reco::deltaPhi(diffParameters[2],0.f);
00099     chi2 = ROOT::Math::Dot(diffParameters * recoTrackCovMatrix, diffParameters);
00100     chi2 /= 5;
00101     
00102     LogDebug("TrackAssociator") << "====NEW RECO TRACK WITH PT=" << sin(rParameters[1])*float(charge)/rParameters[0] << "====\n" 
00103                                 << "qoverp sim: " << sParameters[0] << "\n" 
00104                                 << "lambda sim: " << sParameters[1] << "\n" 
00105                                 << "phi    sim: " << sParameters[2] << "\n" 
00106                                 << "dxy    sim: " << sParameters[3] << "\n" 
00107                                 << "dsz    sim: " << sParameters[4] << "\n" 
00108                                 << ": " /*<< */ << "\n" 
00109                                 << "qoverp rec: " << rParameters[0] << "\n" 
00110                                 << "lambda rec: " << rParameters[1] << "\n" 
00111                                 << "phi    rec: " << rParameters[2] << "\n" 
00112                                 << "dxy    rec: " << rParameters[3] << "\n" 
00113                                 << "dsz    rec: " << rParameters[4] << "\n" 
00114                                 << ": " /*<< */ << "\n" 
00115                                 << "chi2: " << chi2 << "\n";
00116     
00117     return chi2;  
00118   } else {
00119     return 10000000000.;
00120   }
00121 }
00122 
00123 
00124 double TrackAssociatorByChi2::associateRecoToSim( TrackCollection::const_iterator rt, 
00125                                                   TrackingParticleCollection::const_iterator tp, 
00126                                                   const reco::BeamSpot& bs) const{  
00127   TrackBase::ParameterVector rParameters = rt->parameters();
00128   TrackBase::CovarianceMatrix recoTrackCovMatrix = rt->covariance();
00129   if (onlyDiagonal){
00130     for (unsigned int i=0;i<5;i++){
00131       for (unsigned int j=0;j<5;j++){
00132         if (i!=j) recoTrackCovMatrix(i,j)=0;
00133       }
00134     }
00135   } 
00136   
00137   recoTrackCovMatrix.Invert();
00138   Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
00139   Basic3DVector<double> vert(tp->vertex().x(),tp->vertex().y(),tp->vertex().z());
00140   int charge = tp->charge();
00141   return getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
00142 }
00143 
00144 pair<bool,TrackBase::ParameterVector> 
00145 TrackAssociatorByChi2::parametersAtClosestApproach(Basic3DVector<double> vertex,
00146                                                    Basic3DVector<double> momAtVtx,
00147                                                    float charge,
00148                                                    const BeamSpot& bs) const{
00149   
00150   TrackBase::ParameterVector sParameters;
00151   try {
00152     FreeTrajectoryState ftsAtProduction(GlobalPoint(vertex.x(),vertex.y(),vertex.z()),
00153                                         GlobalVector(momAtVtx.x(),momAtVtx.y(),momAtVtx.z()),
00154                                         TrackCharge(charge),
00155                                         theMF.product());
00156     TSCBLBuilderNoMaterial tscblBuilder;
00157     TrajectoryStateClosestToBeamLine tsAtClosestApproach = tscblBuilder(ftsAtProduction,bs);//as in TrackProducerAlgorithm
00158     
00159     GlobalPoint v = tsAtClosestApproach.trackStateAtPCA().position();
00160     GlobalVector p = tsAtClosestApproach.trackStateAtPCA().momentum();
00161     sParameters[0] = tsAtClosestApproach.trackStateAtPCA().charge()/p.mag();
00162     sParameters[1] = Geom::halfPi() - p.theta();
00163     sParameters[2] = p.phi();
00164     sParameters[3] = (-v.x()*sin(p.phi())+v.y()*cos(p.phi()));
00165     sParameters[4] = v.z()*p.perp()/p.mag() - (v.x()*p.x()+v.y()*p.y())/p.perp() * p.z()/p.mag();
00166     
00167     return pair<bool,TrackBase::ParameterVector>(true,sParameters);
00168   } catch ( ... ) {
00169     return pair<bool,TrackBase::ParameterVector>(false,sParameters);
00170   }
00171 }
00172 
00173 RecoToSimCollection TrackAssociatorByChi2::associateRecoToSim(const edm::RefToBaseVector<reco::Track>& tC, 
00174                                                               const edm::RefVector<TrackingParticleCollection>& tPCH,
00175                                                               const edm::Event * e,
00176                                                               const edm::EventSetup *setup ) const{
00177   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00178   e->getByLabel(bsSrc,recoBeamSpotHandle);
00179   reco::BeamSpot bs = *recoBeamSpotHandle;      
00180 
00181   RecoToSimCollection  outputCollection;
00182 
00183   TrackingParticleCollection tPC;
00184   if (tPCH.size()!=0)  tPC = *tPCH.product();
00185 
00186   int tindex=0;
00187   for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
00188 
00189     LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
00190                                 << "rec::Track #"<<tindex<<" with pt=" << (*rt)->pt() <<  "\n"
00191                                 << "===========================================" << "\n";
00192  
00193     TrackBase::ParameterVector rParameters = (*rt)->parameters();
00194 
00195     TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
00196     if (onlyDiagonal){
00197       for (unsigned int i=0;i<5;i++){
00198         for (unsigned int j=0;j<5;j++){
00199           if (i!=j) recoTrackCovMatrix(i,j)=0;
00200         }
00201       }
00202     } 
00203 
00204     recoTrackCovMatrix.Invert();
00205 
00206     int tpindex =0;
00207     for (TrackingParticleCollection::const_iterator tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
00208         
00209       //skip tps with a very small pt
00210       //if (sqrt(tp->momentum().perp2())<0.5) continue;
00211       int charge = tp->charge();
00212       if (charge==0) continue;
00213       Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
00214       Basic3DVector<double> vert=(Basic3DVector<double>) tp->vertex();
00215 
00216       double chi2 = getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
00217       
00218       if (chi2<chi2cut) {
00219         outputCollection.insert(tC[tindex], 
00220                                 std::make_pair(edm::Ref<TrackingParticleCollection>(tPCH, tpindex),
00221                                                -chi2));//-chi2 because the Association Map is ordered using std::greater
00222       }
00223     }
00224   }
00225   outputCollection.post_insert();
00226   return outputCollection;
00227 }
00228 
00229 
00230 SimToRecoCollection TrackAssociatorByChi2::associateSimToReco(const edm::RefToBaseVector<reco::Track>& tC, 
00231                                                               const edm::RefVector<TrackingParticleCollection>& tPCH,
00232                                                               const edm::Event * e,
00233                                                               const edm::EventSetup *setup ) const {
00234   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00235   e->getByLabel(bsSrc,recoBeamSpotHandle);
00236   reco::BeamSpot bs = *recoBeamSpotHandle;      
00237 
00238   SimToRecoCollection  outputCollection;
00239 
00240   TrackingParticleCollection tPC;
00241   if (tPCH.size()!=0)  tPC = *tPCH.product();
00242 
00243   int tpindex =0;
00244   for (TrackingParticleCollection::const_iterator tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
00245     
00246     //skip tps with a very small pt
00247     //if (sqrt(tp->momentum().perp2())<0.5) continue;
00248     int charge = tp->charge();
00249     if (charge==0) continue;
00250     
00251     LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
00252                                 << "TrackingParticle #"<<tpindex<<" with pt=" << sqrt(tp->momentum().perp2()) << "\n"
00253                                 << "===========================================" << "\n";
00254     
00255     Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
00256     Basic3DVector<double> vert(tp->vertex().x(),tp->vertex().y(),tp->vertex().z());
00257       
00258     int tindex=0;
00259     for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
00260       
00261       TrackBase::ParameterVector rParameters = (*rt)->parameters();      
00262       TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
00263       if (onlyDiagonal) {
00264         for (unsigned int i=0;i<5;i++){
00265           for (unsigned int j=0;j<5;j++){
00266             if (i!=j) recoTrackCovMatrix(i,j)=0;
00267           }
00268         }
00269       }
00270       recoTrackCovMatrix.Invert();
00271       
00272       double chi2 = getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
00273       
00274       if (chi2<chi2cut) {
00275         outputCollection.insert(edm::Ref<TrackingParticleCollection>(tPCH, tpindex),
00276                                 std::make_pair(tC[tindex],
00277                                                -chi2));//-chi2 because the Association Map is ordered using std::greater
00278       }
00279     }
00280   }
00281   outputCollection.post_insert();
00282   return outputCollection;
00283 }
00284 
00285 
00286 
00287 
00288 RecoToGenCollection TrackAssociatorByChi2::associateRecoToGen(const edm::RefToBaseVector<reco::Track>& tC, 
00289                                                               const edm::RefVector<GenParticleCollection>& tPCH,
00290                                                               const edm::Event * e,
00291                                                               const edm::EventSetup *setup ) const{
00292   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00293   e->getByLabel(bsSrc,recoBeamSpotHandle);
00294   reco::BeamSpot bs = *recoBeamSpotHandle;      
00295 
00296   RecoToGenCollection  outputCollection;
00297 
00298   GenParticleCollection tPC;
00299   if (tPCH.size()!=0)  tPC = *tPCH.product();
00300 
00301   int tindex=0;
00302   for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
00303 
00304     LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
00305                                 << "rec::Track #"<<tindex<<" with pt=" << (*rt)->pt() <<  "\n"
00306                                 << "===========================================" << "\n";
00307  
00308     TrackBase::ParameterVector rParameters = (*rt)->parameters();
00309 
00310     TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
00311     if (onlyDiagonal){
00312       for (unsigned int i=0;i<5;i++){
00313         for (unsigned int j=0;j<5;j++){
00314           if (i!=j) recoTrackCovMatrix(i,j)=0;
00315         }
00316       }
00317     } 
00318 
00319     recoTrackCovMatrix.Invert();
00320 
00321     int tpindex =0;
00322     for (GenParticleCollection::const_iterator tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
00323         
00324       //skip tps with a very small pt
00325       //if (sqrt(tp->momentum().perp2())<0.5) continue;
00326       int charge = tp->charge();
00327       if (charge==0) continue;
00328       Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
00329       Basic3DVector<double> vert=(Basic3DVector<double>) tp->vertex();
00330 
00331       double chi2 = getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
00332       
00333       if (chi2<chi2cut) {
00334         outputCollection.insert(tC[tindex], 
00335                                 std::make_pair(edm::Ref<GenParticleCollection>(tPCH, tpindex),
00336                                                -chi2));//-chi2 because the Association Map is ordered using std::greater
00337       }
00338     }
00339   }
00340   outputCollection.post_insert();
00341   return outputCollection;
00342 }
00343 
00344 
00345 GenToRecoCollection TrackAssociatorByChi2::associateGenToReco(const edm::RefToBaseVector<reco::Track>& tC, 
00346                                                               const edm::RefVector<GenParticleCollection>& tPCH,
00347                                                               const edm::Event * e,
00348                                                               const edm::EventSetup *setup ) const {
00349 
00350   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00351   e->getByLabel(bsSrc,recoBeamSpotHandle);
00352   reco::BeamSpot bs = *recoBeamSpotHandle;      
00353 
00354   GenToRecoCollection  outputCollection;
00355 
00356   GenParticleCollection tPC;
00357   if (tPCH.size()!=0)  tPC = *tPCH.product();
00358 
00359   int tpindex =0;
00360   for (GenParticleCollection::const_iterator tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
00361     
00362     //skip tps with a very small pt
00363     //if (sqrt(tp->momentum().perp2())<0.5) continue;
00364     int charge = tp->charge();
00365     if (charge==0) continue;
00366     
00367     LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
00368                                 << "TrackingParticle #"<<tpindex<<" with pt=" << sqrt(tp->momentum().perp2()) << "\n"
00369                                 << "===========================================" << "\n";
00370     
00371     Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
00372     Basic3DVector<double> vert(tp->vertex().x(),tp->vertex().y(),tp->vertex().z());
00373       
00374     int tindex=0;
00375     for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
00376       
00377       TrackBase::ParameterVector rParameters = (*rt)->parameters();      
00378       TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
00379       if (onlyDiagonal) {
00380         for (unsigned int i=0;i<5;i++){
00381           for (unsigned int j=0;j<5;j++){
00382             if (i!=j) recoTrackCovMatrix(i,j)=0;
00383           }
00384         }
00385       }
00386       recoTrackCovMatrix.Invert();
00387       
00388       double chi2 = getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
00389       
00390       if (chi2<chi2cut) {
00391         outputCollection.insert(edm::Ref<GenParticleCollection>(tPCH, tpindex),
00392                                 std::make_pair(tC[tindex],
00393                                                -chi2));//-chi2 because the Association Map is ordered using std::greater
00394       }
00395     }
00396   }
00397   outputCollection.post_insert();
00398   return outputCollection;
00399 }