CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/TrackingTools/GsfTools/src/MultiTrajectoryStateMode.cc

Go to the documentation of this file.
00001 #include "TrackingTools/GsfTools/interface/MultiTrajectoryStateMode.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 
00004 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00005 #include "DataFormats/GeometrySurface/interface/Surface.h"
00006 #include "TrackingTools/GsfTools/interface/MultiGaussianStateTransform.h"
00007 #include "TrackingTools/GsfTools/interface/MultiGaussianState1D.h"
00008 #include "TrackingTools/GsfTools/interface/GaussianSumUtilities1D.h"
00009 
00010 #include <iostream>
00011 
00012 bool
00013 MultiTrajectoryStateMode::momentumFromModeCartesian (const TrajectoryStateOnSurface tsos,
00014                                                      GlobalVector& momentum) const
00015 {
00016   //
00017   // clear result vector and check validity of the TSOS
00018   //
00019   momentum = GlobalVector(0.,0.,0.);
00020   if ( !tsos.isValid() ) {
00021     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00022     return false;
00023   }
00024   //  
00025   // 1D mode computation for px, py and pz
00026   // 
00027   std::vector<TrajectoryStateOnSurface> components(tsos.components());
00028   unsigned int numb = components.size();
00029   // vectors of components in x, y and z
00030   std::vector<SingleGaussianState1D> pxStates; pxStates.reserve(numb);
00031   std::vector<SingleGaussianState1D> pyStates; pyStates.reserve(numb);
00032   std::vector<SingleGaussianState1D> pzStates; pzStates.reserve(numb);
00033   // iteration over components
00034   for ( std::vector<TrajectoryStateOnSurface>::const_iterator ic=components.begin();
00035         ic!=components.end(); ++ic ) {
00036     // extraction of parameters and variances
00037     GlobalVector mom(ic->globalMomentum());
00038     AlgebraicSymMatrix66 cov(ic->cartesianError().matrix());
00039     pxStates.push_back(SingleGaussianState1D(mom.x(),cov(3,3),ic->weight()));
00040     pyStates.push_back(SingleGaussianState1D(mom.y(),cov(4,4),ic->weight()));
00041     pzStates.push_back(SingleGaussianState1D(mom.z(),cov(5,5),ic->weight()));
00042   }
00043   //
00044   // transformation in 1D multi-states and creation of utility classes
00045   //
00046   MultiGaussianState1D pxState(pxStates);
00047   MultiGaussianState1D pyState(pyStates);
00048   MultiGaussianState1D pzState(pzStates);
00049   GaussianSumUtilities1D pxUtils(pxState);
00050   GaussianSumUtilities1D pyUtils(pyState);
00051   GaussianSumUtilities1D pzUtils(pzState);
00052   //
00053   // cartesian momentum vector from modes
00054   //
00055   momentum = GlobalVector(pxUtils.mode().mean(),pyUtils.mode().mean(),pzUtils.mode().mean());
00056   return true;
00057 }
00058 
00059 bool
00060 MultiTrajectoryStateMode::positionFromModeCartesian (const TrajectoryStateOnSurface tsos,
00061                                                      GlobalPoint& position) const
00062 {
00063   //
00064   // clear result vector and check validity of the TSOS
00065   //
00066   position = GlobalPoint(0.,0.,0.);
00067   if ( !tsos.isValid() ) {
00068     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00069     return false;
00070   }
00071   //  
00072   // 1D mode computation for x, y and z
00073   // 
00074   std::vector<TrajectoryStateOnSurface> components(tsos.components());
00075   unsigned int numb = components.size();
00076   // vectors of components in x, y and z
00077   std::vector<SingleGaussianState1D> xStates; xStates.reserve(numb);
00078   std::vector<SingleGaussianState1D> yStates; yStates.reserve(numb);
00079   std::vector<SingleGaussianState1D> zStates; zStates.reserve(numb);
00080   // iteration over components
00081   for ( std::vector<TrajectoryStateOnSurface>::const_iterator ic=components.begin();
00082         ic!=components.end(); ++ic ) {
00083     // extraction of parameters and variances
00084     GlobalPoint pos(ic->globalPosition());
00085     AlgebraicSymMatrix66 cov(ic->cartesianError().matrix());
00086     xStates.push_back(SingleGaussianState1D(pos.x(),cov(0,0),ic->weight()));
00087     yStates.push_back(SingleGaussianState1D(pos.y(),cov(1,1),ic->weight()));
00088     zStates.push_back(SingleGaussianState1D(pos.z(),cov(2,2),ic->weight()));
00089   }
00090   //
00091   // transformation in 1D multi-states and creation of utility classes
00092   //
00093   MultiGaussianState1D xState(xStates);
00094   MultiGaussianState1D yState(yStates);
00095   MultiGaussianState1D zState(zStates);
00096   GaussianSumUtilities1D xUtils(xState);
00097   GaussianSumUtilities1D yUtils(yState);
00098   GaussianSumUtilities1D zUtils(zState);
00099   //
00100   // cartesian position vector from modes
00101   //
00102   position = GlobalPoint(xUtils.mode().mean(),yUtils.mode().mean(),zUtils.mode().mean());
00103   return true;
00104 }
00105 
00106 bool
00107 MultiTrajectoryStateMode::momentumFromModeLocal (const TrajectoryStateOnSurface tsos,
00108                                                  GlobalVector& momentum) const
00109 {
00110   //
00111   // clear result vector and check validity of the TSOS
00112   //
00113   momentum = GlobalVector(0.,0.,0.);
00114   if ( !tsos.isValid() ) {
00115     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00116     return false;
00117   }
00118   //  
00119   // mode computation for local co-ordinates q/p, dx/dz, dy/dz
00120   //
00121   double qpMode(0);
00122   double dxdzMode(0);
00123   double dydzMode(0);
00124   //
00125   // first 3 elements of local parameters = q/p, dx/dz, dy/dz
00126   //
00127   for ( unsigned int iv=0; iv<3; ++iv ) {
00128     // extraction of multi-state using helper class
00129     MultiGaussianState1D state1D = MultiGaussianStateTransform::multiState1D(tsos,iv);
00130     GaussianSumUtilities1D utils(state1D);
00131     // mode (in case of failure: mean)
00132     double result = utils.mode().mean();
00133     if ( !utils.modeIsValid() )  result = utils.mean();
00134     if ( iv==0 )  qpMode = result;
00135     else if ( iv==1 )  dxdzMode = result;
00136     else  dydzMode = result;
00137   }
00138   // local momentum vector from dx/dz, dy/dz and q/p + sign of local pz
00139   LocalVector localP(dxdzMode,dydzMode,1.);
00140   localP *= tsos.localParameters().pzSign()/fabs(qpMode)
00141     /sqrt(dxdzMode*dxdzMode+dydzMode*dydzMode+1.);
00142   // conversion to global coordinates
00143   momentum = tsos.surface().toGlobal(localP);
00144   return true;
00145 }
00146 
00147 bool
00148 MultiTrajectoryStateMode::momentumFromModeQP (const TrajectoryStateOnSurface tsos,
00149                                               double& momentum) const
00150 {
00151   //
00152   // clear result vector and check validity of the TSOS
00153   //
00154   momentum = 0.;
00155   if ( !tsos.isValid() ) {
00156     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00157     return false;
00158   }
00159   //  
00160   // mode computation for local co-ordinates q/p, dx/dz, dy/dz
00161   //
00162   double qpMode(0);
00163   //
00164   // first element of local parameters = q/p
00165   //
00166   // extraction of multi-state using helper class
00167   MultiGaussianState1D state1D = MultiGaussianStateTransform::multiState1D(tsos,0);
00168   GaussianSumUtilities1D utils(state1D);
00169   // mode (in case of failure: mean)
00170   qpMode = utils.mode().mean();
00171   if ( !utils.modeIsValid() )  qpMode = utils.mean();
00172 
00173   momentum = 1./fabs(qpMode);
00174   return true;
00175 }
00176 
00177 bool
00178 MultiTrajectoryStateMode::momentumFromModeP (const TrajectoryStateOnSurface tsos,
00179                                              double& momentum) const
00180 {
00181   //
00182   // clear result vector and check validity of the TSOS
00183   //
00184   momentum = 0.;
00185   if ( !tsos.isValid() ) {
00186     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00187     return false;
00188   }
00189   //  
00190   // first element of local parameters = q/p
00191   //
00192   // extraction of multi-state using helper class
00193   MultiGaussianState1D qpMultiState = MultiGaussianStateTransform::multiState1D(tsos,0);
00194   std::vector<SingleGaussianState1D> states(qpMultiState.components());
00195   // transform from q/p to p
00196   for ( unsigned int i=0; i<states.size(); ++i ) {
00197     SingleGaussianState1D& qpState = states[i];
00198     double wgt = qpState.weight();
00199     double qp = qpState.mean();
00200     double varQp = qpState.variance();
00201     double p = 1./fabs(qp);
00202     double varP = p*p*p*p*varQp;
00203     states[i] = SingleGaussianState1D(p,varP,wgt);
00204   }
00205   MultiGaussianState1D pMultiState(states);
00206   GaussianSumUtilities1D utils(pMultiState);
00207   // mode (in case of failure: mean)
00208   momentum = utils.mode().mean();
00209   if ( !utils.modeIsValid() )  momentum = utils.mean();
00210 
00211   return true;
00212 }
00213 
00214 bool
00215 MultiTrajectoryStateMode::positionFromModeLocal (const TrajectoryStateOnSurface tsos,
00216                                                  GlobalPoint& position) const
00217 {
00218   //
00219   // clear result vector and check validity of the TSOS
00220   //
00221   position = GlobalPoint(0.,0.,0.);
00222   if ( !tsos.isValid() ) {
00223     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00224     return false;
00225   }
00226   //  
00227   // mode computation for local co-ordinates x, y
00228   //
00229   double xMode(0);
00230   double yMode(0);
00231   //
00232   // last 2 elements of local parameters = x, y
00233   //
00234   for ( unsigned int iv=3; iv<5; ++iv ) {
00235     // extraction of multi-state using helper class
00236     MultiGaussianState1D state1D = MultiGaussianStateTransform::multiState1D(tsos,iv);
00237     GaussianSumUtilities1D utils(state1D);
00238     // mode (in case of failure: mean)
00239     double result = utils.mode().mean();
00240     if ( !utils.modeIsValid() )  result = utils.mean();
00241     if ( iv==3 )  xMode = result;
00242     else  yMode = result;
00243   }
00244   // local position vector from x, y
00245   LocalPoint localP(xMode,yMode,0.);
00246   // conversion to global coordinates
00247   position = tsos.surface().toGlobal(localP);
00248   return true;
00249 }
00250 
00251 bool
00252 MultiTrajectoryStateMode::momentumFromModePPhiEta (const TrajectoryStateOnSurface tsos,
00253                                                    GlobalVector& momentum) const
00254 {
00255   //
00256   // clear result vector and check validity of the TSOS
00257   //
00258   momentum = GlobalVector(0.,0.,0.);
00259   if ( !tsos.isValid() ) {
00260     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00261     return false;
00262   }
00263   //  
00264   // 1D mode computation for p, phi, eta
00265   // 
00266   std::vector<TrajectoryStateOnSurface> components(tsos.components());
00267   unsigned int numb = components.size();
00268   // vectors of components in p, phi and eta
00269   std::vector<SingleGaussianState1D> pStates; pStates.reserve(numb);
00270   std::vector<SingleGaussianState1D> phiStates; phiStates.reserve(numb);
00271   std::vector<SingleGaussianState1D> etaStates; etaStates.reserve(numb);
00272   // covariances in cartesian and p-phi-eta and jacobian
00273   AlgebraicMatrix33 jacobian;
00274   AlgebraicSymMatrix33 covCart;
00275   AlgebraicSymMatrix33 covPPhiEta;
00276   // iteration over components
00277   for ( std::vector<TrajectoryStateOnSurface>::const_iterator ic=components.begin();
00278         ic!=components.end(); ++ic ) {
00279     // parameters
00280     GlobalVector mom(ic->globalMomentum());
00281     double px = mom.x();
00282     double py = mom.y();
00283     double pz = mom.z();
00284     double p = mom.mag();
00285     double pt2 = mom.perp2();
00286     double phi = mom.phi();
00287     double eta = mom.eta();
00288     // jacobian
00289     jacobian(0,0) = px/p;
00290     jacobian(0,1) = py/p;
00291     jacobian(0,2) = pz/p;
00292     jacobian(1,0) = py/pt2;
00293     jacobian(1,1) = -px/pt2;
00294     jacobian(1,2) = 0;
00295     jacobian(2,0) = px*pz/(pt2*p);
00296     jacobian(2,1) = py*pz/(pt2*p);
00297     jacobian(2,2) = -1./p;
00298     // extraction of the momentum part from the 6x6 cartesian error matrix
00299     // and conversion to p-phi-eta
00300     covCart = ic->cartesianError().matrix().Sub<AlgebraicSymMatrix33>(3,3);
00301     covPPhiEta = ROOT::Math::Similarity(jacobian,covCart);
00302     pStates.push_back(SingleGaussianState1D(p,covPPhiEta(0,0),ic->weight()));
00303     phiStates.push_back(SingleGaussianState1D(phi,covPPhiEta(1,1),ic->weight()));
00304     etaStates.push_back(SingleGaussianState1D(eta,covPPhiEta(2,2),ic->weight()));
00305   }
00306   //
00307   // transformation in 1D multi-states and creation of utility classes
00308   //
00309   MultiGaussianState1D pState(pStates);
00310   MultiGaussianState1D phiState(phiStates);
00311   MultiGaussianState1D etaState(etaStates);
00312   GaussianSumUtilities1D pUtils(pState);
00313   GaussianSumUtilities1D phiUtils(phiState);
00314   GaussianSumUtilities1D etaUtils(etaState);
00315   //
00316   // parameters from mode (in case of failure: mean)
00317   //
00318   double p = pUtils.modeIsValid() ? pUtils.mode().mean() : pUtils.mean();
00319   double phi = phiUtils.modeIsValid() ? phiUtils.mode().mean() : phiUtils.mean();
00320   double eta = etaUtils.modeIsValid() ? etaUtils.mode().mean() : etaUtils.mean();
00321 //   double theta = 2*atan(exp(-eta));
00322   double tanth2 = exp(-eta);
00323   double pt = p*2*tanth2/(1+tanth2*tanth2);  // p*sin(theta)
00324   double pz = p*(1-tanth2*tanth2)/(1+tanth2*tanth2);  // p*cos(theta)
00325   // conversion to a cartesian momentum vector
00326   momentum = GlobalVector(pt*cos(phi),pt*sin(phi),pz);
00327   return true;
00328 }
00329 
00330 int
00331 MultiTrajectoryStateMode::chargeFromMode (const TrajectoryStateOnSurface tsos) const
00332 {
00333   //
00334   // clear result vector and check validity of the TSOS
00335   //
00336   if ( !tsos.isValid() ) {
00337     edm::LogInfo("MultiTrajectoryStateMode") << "Cannot calculate mode from invalid TSOS";
00338     return 0;
00339   }
00340   //  
00341   // mode computation for local co-ordinates q/p
00342   // extraction of multi-state using helper class
00343   MultiGaussianState1D state1D = MultiGaussianStateTransform::multiState1D(tsos,0);
00344   GaussianSumUtilities1D utils(state1D);
00345   // mode (in case of failure: mean)
00346   double result = utils.mode().mean();
00347   if ( !utils.modeIsValid() )  result = utils.mean();
00348 
00349   return result>0. ? 1 : -1;
00350 }
00351