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