CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/TrackingTools/IPTools/src/IPTools.cc

Go to the documentation of this file.
00001 #include "DataFormats/GeometrySurface/interface/Line.h"
00002 
00003 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00004 #include "TrackingTools/GeomPropagators/interface/AnalyticalTrajectoryExtrapolatorToLine.h"
00005 #include "TrackingTools/GeomPropagators/interface/AnalyticalImpactPointExtrapolator.h"
00006 #include "DataFormats/GeometryCommonDetAlgo/interface/Measurement1D.h"
00007 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00008 #include "TrackingTools/IPTools/interface/IPTools.h"
00009 #include "CLHEP/Vector/ThreeVector.h"
00010 #include "CLHEP/Vector/LorentzVector.h"
00011 #include "CLHEP/Matrix/Vector.h"
00012 #include <string>
00013 
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 #include "RecoVertex/VertexTools/interface/VertexDistance3D.h"
00016 #include "RecoVertex/VertexTools/interface/VertexDistanceXY.h"
00017 #include "RecoVertex/VertexPrimitives/interface/ConvertToFromReco.h"
00018 
00019 
00020 namespace IPTools
00021 {
00022   using namespace std;
00023   using namespace reco;
00024 
00025 
00026     std::pair<bool,Measurement1D> absoluteImpactParameter(const TrajectoryStateOnSurface & tsos  , const  reco::Vertex & vertex, VertexDistance & distanceComputer) {
00027         if(!tsos.isValid()) {
00028          return pair<bool,Measurement1D>(false,Measurement1D(0.,0.)) ;
00029         }
00030         GlobalPoint refPoint          = tsos.globalPosition();
00031         GlobalError refPointErr       = tsos.cartesianError().position();
00032         GlobalPoint vertexPosition    = RecoVertex::convertPos(vertex.position());
00033         GlobalError vertexPositionErr = RecoVertex::convertError(vertex.error());
00034         return pair<bool,Measurement1D>(true,distanceComputer.distance(VertexState(vertexPosition,vertexPositionErr), VertexState(refPoint, refPointErr)));
00035    }
00036 
00037     std::pair<bool,Measurement1D> absoluteImpactParameter3D(const reco::TransientTrack & transientTrack, const  reco::Vertex & vertex)
00038     {
00039       AnalyticalImpactPointExtrapolator extrapolator(transientTrack.field());
00040       VertexDistance3D dist;
00041       return absoluteImpactParameter(extrapolator.extrapolate(transientTrack.impactPointState(), RecoVertex::convertPos(vertex.position())), vertex, dist);
00042     }
00043     std::pair<bool,Measurement1D> absoluteTransverseImpactParameter(const reco::TransientTrack & transientTrack, const  reco::Vertex & vertex)
00044     {
00045       TransverseImpactPointExtrapolator extrapolator(transientTrack.field());
00046       VertexDistanceXY dist;
00047       return absoluteImpactParameter(extrapolator.extrapolate(transientTrack.impactPointState(), RecoVertex::convertPos(vertex.position())), vertex,  dist);
00048     }
00049     
00050   pair<bool,Measurement1D> signedTransverseImpactParameter(const TransientTrack & track,
00051                                                            const GlobalVector & direction, const  Vertex & vertex){
00052     //Extrapolate to closest point on transverse plane
00053     TransverseImpactPointExtrapolator extrapolator(track.field());
00054     TrajectoryStateOnSurface closestOnTransversePlaneState = extrapolator.extrapolate(track.impactPointState(),RecoVertex::convertPos(vertex.position()));
00055     
00056     //Compute absolute value
00057     VertexDistanceXY dist;
00058     pair<bool,Measurement1D> result = absoluteImpactParameter(closestOnTransversePlaneState, vertex,  dist);
00059     if(!result.first) return result;
00060 
00061     //Compute Sign
00062     GlobalPoint impactPoint    = closestOnTransversePlaneState.globalPosition();
00063     GlobalVector IPVec(impactPoint.x()-vertex.x(),impactPoint.y()-vertex.y(),0.);
00064     double prod = IPVec.dot(direction);
00065     double sign = (prod>=0) ? 1. : -1.;
00066     
00067     //Apply sign to the result
00068     return pair<bool,Measurement1D>(result.first,Measurement1D(sign*result.second.value(), result.second.error()));
00069   }
00070 
00071   pair<bool,Measurement1D> signedImpactParameter3D(const TransientTrack & track,
00072                                                            const GlobalVector & direction, const  Vertex & vertex){
00073     //Extrapolate to closest point on transverse plane
00074     AnalyticalImpactPointExtrapolator extrapolator(track.field());
00075     TrajectoryStateOnSurface closestIn3DSpaceState = extrapolator.extrapolate(track.impactPointState(),RecoVertex::convertPos(vertex.position()));
00076 
00077     //Compute absolute value
00078     VertexDistance3D dist;
00079     pair<bool,Measurement1D> result = absoluteImpactParameter(closestIn3DSpaceState, vertex,  dist);
00080     if(!result.first) return result;
00081 
00082     //Compute Sign
00083     GlobalPoint impactPoint = closestIn3DSpaceState.globalPosition();
00084     GlobalVector IPVec(impactPoint.x()-vertex.x(),impactPoint.y()-vertex.y(),impactPoint.z()-vertex.z());
00085     double prod = IPVec.dot(direction);
00086     double sign = (prod>=0) ? 1. : -1.;
00087 
00088     //Apply sign to the result
00089     return pair<bool,Measurement1D>(result.first,Measurement1D(sign*result.second.value(), result.second.error()));
00090   }
00091 
00092 
00093 
00094   pair<bool,Measurement1D> signedDecayLength3D(const   TrajectoryStateOnSurface & closestToJetState,
00095                                                const GlobalVector & direction, const  Vertex & vertex)  {
00096 
00097     //Check if extrapolation has been successfull
00098     if(!closestToJetState.isValid()) {
00099       return pair<bool,Measurement1D>(false,Measurement1D(0.,0.));
00100     }
00101 
00102     GlobalVector jetDirection = direction.unit();
00103     GlobalPoint vertexPosition(vertex.x(),vertex.y(),vertex.z());
00104   
00105     double decayLen = jetDirection.dot(closestToJetState.globalPosition()-vertexPosition);
00106 
00107     //error calculation
00108 
00109     AlgebraicVector3 j;
00110     j[0] = jetDirection.x();
00111     j[1] = jetDirection.y();
00112     j[2] = jetDirection.z();
00113     AlgebraicVector6 jj;
00114     jj[0] = jetDirection.x();
00115     jj[1] = jetDirection.y();
00116     jj[2] = jetDirection.z();
00117     jj[3] =0.;
00118     jj[4] =0.;
00119     jj[5] =0.;
00120      
00121     //TODO: FIXME: the extrapolation uncertainty is very relevant here should be taken into account!!
00122     double trackError2 = ROOT::Math::Similarity(jj,closestToJetState.cartesianError().matrix());
00123     double vertexError2 = ROOT::Math::Similarity(j,vertex.covariance());
00124 
00125     double decayLenError = sqrt(trackError2+vertexError2);
00126     
00127     return pair<bool,Measurement1D>(true,Measurement1D(decayLen,decayLenError));
00128 
00129   }
00130 
00131 
00132     
00133   pair<bool,Measurement1D> linearizedSignedImpactParameter3D(const   TrajectoryStateOnSurface & closestToJetState ,
00134                                                    const GlobalVector & direction, const  Vertex & vertex)
00135   {
00136     //Check if extrapolation has been successfull
00137     if(!closestToJetState.isValid()) {
00138       return pair<bool,Measurement1D>(false,Measurement1D(0.,0.));
00139     }
00140 
00141     GlobalPoint vertexPosition(vertex.x(),vertex.y(),vertex.z());
00142     GlobalVector impactParameter = linearImpactParameter(closestToJetState, vertexPosition);
00143     GlobalVector jetDir = direction.unit();
00144     GlobalVector flightDistance(closestToJetState.globalPosition()-vertexPosition);
00145     double theDistanceAlongJetAxis = jetDir.dot(flightDistance);
00146     double signedIP = impactParameter.mag()*((theDistanceAlongJetAxis!=0)?theDistanceAlongJetAxis/abs(theDistanceAlongJetAxis):1.);
00147 
00148 
00149     GlobalVector ipDirection = impactParameter.unit();
00150     //GlobalPoint closestPoint = closestToJetState.globalPosition();
00151     GlobalVector momentumAtClosestPoint = closestToJetState.globalMomentum(); 
00152     GlobalVector momentumDir = momentumAtClosestPoint.unit();
00153     
00154     AlgebraicVector3 deriv_v;
00155     deriv_v[0] = - ipDirection.x();
00156     deriv_v[1] = - ipDirection.y();
00157     deriv_v[2] = - ipDirection.z();
00158 
00159  
00160     AlgebraicVector6 deriv;
00161     deriv[0] = ipDirection.x();
00162     deriv[1] = ipDirection.y();
00163     deriv[2] = ipDirection.z();
00164     deriv[3] = -  (momentumDir.dot(flightDistance)*ipDirection.x())/momentumAtClosestPoint.mag();
00165     deriv[4] = -  (momentumDir.dot(flightDistance)*ipDirection.y())/momentumAtClosestPoint.mag();
00166     deriv[5] = -  (momentumDir.dot(flightDistance)*ipDirection.z())/momentumAtClosestPoint.mag();
00167 
00168     double trackError2 = ROOT::Math::Similarity(deriv , closestToJetState.cartesianError().matrix());
00169     double vertexError2 = ROOT::Math::Similarity(deriv_v , vertex.covariance());
00170     double ipError = sqrt(trackError2+vertexError2);
00171 
00172     return pair<bool,Measurement1D>(true,Measurement1D(signedIP,ipError));
00173   }
00174 
00175 
00176 
00177   TrajectoryStateOnSurface closestApproachToJet(const TrajectoryStateOnSurface & state,const Vertex & vertex, const GlobalVector& direction,const MagneticField * field) {
00178   
00179     Line::PositionType pos(GlobalPoint(vertex.x(),vertex.y(),vertex.z()));
00180     Line::DirectionType dir(direction.unit());
00181     Line jetLine(pos,dir);
00182   
00183     AnalyticalTrajectoryExtrapolatorToLine extrapolator(field);
00184 
00185     return extrapolator.extrapolate(state, jetLine);
00186   }
00187 
00191   GlobalVector linearImpactParameter(const TrajectoryStateOnSurface & state, const GlobalPoint & point)  {
00192 
00193     Line::PositionType pos(state.globalPosition());
00194     Line::DirectionType dir((state.globalMomentum()).unit());
00195     Line trackLine(pos,dir);
00196     GlobalPoint  tmp=point; 
00197     return  trackLine.distance(tmp);
00198   }
00199 
00200   pair<double,Measurement1D> jetTrackDistance(const TransientTrack & track, const GlobalVector & direction, const Vertex & vertex) {
00201     double  theLDist_err(0.);
00202   
00203     //FIXME
00204     float weight=0.;//vertex.trackWeight(track);
00205 
00206     TrajectoryStateOnSurface stateAtOrigin = track.impactPointState(); 
00207     if(!stateAtOrigin.isValid())
00208       {
00209         //TODO: throw instead?
00210         return pair<bool,Measurement1D>(false,Measurement1D(0.,0.));
00211       }
00212    
00213     //get the Track line at origin
00214     Line::PositionType posTrack(stateAtOrigin.globalPosition());
00215     Line::DirectionType dirTrack((stateAtOrigin.globalMomentum()).unit());
00216     Line trackLine(posTrack,dirTrack);
00217     // get the Jet  line 
00218     // Vertex vertex(vertex);
00219     GlobalVector jetVector = direction.unit();    
00220     Line::PositionType posJet(GlobalPoint(vertex.x(),vertex.y(),vertex.z()));
00221     Line::DirectionType dirJet(jetVector);
00222     Line jetLine(posJet,dirJet);
00223   
00224     // now compute the distance between the two lines
00225     // If the track has been used to refit the Primary vertex then sign it positively, otherwise negative
00226     double theDistanceToJetAxis = (jetLine.distance(trackLine)).mag();
00227     if (weight<1) theDistanceToJetAxis= -theDistanceToJetAxis;
00228 
00229     // ... and the flight distance along the Jet axis.
00230     GlobalPoint  V = jetLine.position();    
00231     GlobalVector Q = dirTrack - jetVector.dot(dirTrack) * jetVector;
00232     GlobalVector P = jetVector - jetVector.dot(dirTrack) * dirTrack;
00233     double theDistanceAlongJetAxis = P.dot(V-posTrack)/Q.dot(dirTrack);
00234 
00235     //
00236     // get the covariance matrix of the vertex and compute the error on theDistanceToJetAxis
00237     //
00238     
00240 
00241     // build the vector of closest approach between lines
00242 
00243 
00244     //FIXME: error not computed.
00245     GlobalVector H((jetVector.cross(dirTrack).unit()));
00246     CLHEP::HepVector Hh(3);
00247     Hh[0] = H.x();
00248     Hh[1] = H.y();
00249     Hh[2] = H.z();
00250     
00251     //  theLDist_err = sqrt(vertexError.similarity(Hh));
00252 
00253     //    cout << "distance to jet axis : "<< theDistanceToJetAxis <<" and error : "<< theLDist_err<<endl;
00254     // Now the impact parameter ...
00255 
00256     /*    GlobalPoint T0 = track.position();
00257           GlobalVector D = (T0-V)- (T0-V).dot(dir) * dir;
00258           double IP = D.mag();    
00259           GlobalVector Dold = distance(aTSOS, aJet.vertex(), jetDirection);
00260           double IPold = Dold.mag();
00261     */
00262 
00263 
00264 
00265   
00266     Measurement1D DTJA(theDistanceToJetAxis,theLDist_err);
00267   
00268     return pair<double,Measurement1D> (theDistanceAlongJetAxis,DTJA);
00269   }
00270 
00271 }