CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/TrackingTools/TrajectoryState/src/BasicTrajectoryState.cc

Go to the documentation of this file.
00001 #include "TrackingTools/TrajectoryState/interface/BasicTrajectoryState.h"
00002 #include "TrackingTools/AnalyticalJacobians/interface/JacobianLocalToCurvilinear.h"
00003 #include "TrackingTools/AnalyticalJacobians/interface/JacobianCurvilinearToLocal.h"
00004 #include "TrackingTools/AnalyticalJacobians/interface/JacobianLocalToCartesian.h"
00005 #include "TrackingTools/AnalyticalJacobians/interface/JacobianCartesianToLocal.h"
00006 
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 
00009 
00010 #include <cmath>
00011 #include<sstream>
00012 
00013 BasicTrajectoryState::
00014 BasicTrajectoryState( const FreeTrajectoryState& fts,
00015                             const Surface& aSurface,
00016                             const SurfaceSide side) :
00017   theFreeState( new FreeTrajectoryState(fts)),
00018   theLocalError(InvalidError()),
00019   theLocalParameters(),
00020   theLocalParametersValid(false),
00021   theGlobalParamsUp2Date(true),
00022   theSurfaceSide(side), 
00023   theSurfaceP( &aSurface), 
00024   theWeight(1.),
00025   theField( &fts.parameters().magneticField())
00026 {}    
00027 
00028 BasicTrajectoryState::
00029 BasicTrajectoryState( const GlobalTrajectoryParameters& par,
00030                             const Surface& aSurface,
00031                             const SurfaceSide side) :
00032   theFreeState( new FreeTrajectoryState(par)),
00033   theLocalError(InvalidError()),
00034   theLocalParameters(),
00035   theLocalParametersValid(false),
00036    theGlobalParamsUp2Date(true),
00037   theSurfaceSide(side), 
00038   theSurfaceP( &aSurface), 
00039   theWeight(1.),
00040   theField( &par.magneticField())
00041 {}
00042 
00043 BasicTrajectoryState::
00044 BasicTrajectoryState( const GlobalTrajectoryParameters& par,
00045                             const CartesianTrajectoryError& err,
00046                             const Surface& aSurface,
00047                             const SurfaceSide side) :
00048   theFreeState( new FreeTrajectoryState(par, err)),
00049   theLocalError(InvalidError()),
00050   theLocalParameters(),
00051   theLocalParametersValid(false),
00052   theGlobalParamsUp2Date(true),
00053   theSurfaceSide(side), 
00054   theSurfaceP( &aSurface), 
00055   theWeight(1.),
00056   theField( &par.magneticField())
00057 {}
00058 
00059 BasicTrajectoryState::
00060 BasicTrajectoryState( const GlobalTrajectoryParameters& par,
00061                             const CurvilinearTrajectoryError& err,
00062                             const Surface& aSurface,
00063                             const SurfaceSide side,
00064                             double weight) :
00065   theFreeState( new FreeTrajectoryState(par, err)),
00066   theLocalError(InvalidError()),
00067   theLocalParameters(),
00068   theLocalParametersValid(false),
00069   theGlobalParamsUp2Date(true),
00070   theSurfaceSide(side), 
00071   theSurfaceP( &aSurface), 
00072   theWeight(weight),
00073   theField( &par.magneticField())
00074 {}
00075 
00076 BasicTrajectoryState::
00077 BasicTrajectoryState( const GlobalTrajectoryParameters& par,
00078                             const CurvilinearTrajectoryError& err,
00079                             const Surface& aSurface,
00080                             double weight) :
00081   theFreeState( new FreeTrajectoryState(par, err)),
00082   theLocalError(InvalidError()),
00083   theLocalParameters(),
00084   theLocalParametersValid(false),
00085   theGlobalParamsUp2Date(true),
00086   theSurfaceSide(SurfaceSideDefinition::atCenterOfSurface), 
00087   theSurfaceP( &aSurface), 
00088   theWeight(weight),
00089   theField( &par.magneticField())
00090 {}
00091 
00092 BasicTrajectoryState::
00093 BasicTrajectoryState( const LocalTrajectoryParameters& par,
00094                             const Surface& aSurface,
00095                             const MagneticField* field,
00096                             const SurfaceSide side) :
00097   theFreeState(0),
00098   theLocalError(InvalidError()),
00099   theLocalParameters(par),
00100   theLocalParametersValid(true),
00101   theGlobalParamsUp2Date(false),
00102    theSurfaceSide(side),
00103   theSurfaceP( &aSurface), 
00104   theWeight(1.),
00105   theField(field) 
00106 {}
00107 
00108 BasicTrajectoryState::
00109 BasicTrajectoryState( const LocalTrajectoryParameters& par,
00110                             const LocalTrajectoryError& err,
00111                             const Surface& aSurface,
00112                             const MagneticField* field,
00113                             const SurfaceSide side,
00114                             double weight) :
00115   theFreeState(0),
00116   theLocalError(err),
00117   theLocalParameters(par),
00118   theLocalParametersValid(true),
00119   theGlobalParamsUp2Date(false),
00120   theSurfaceSide(side), 
00121   theSurfaceP( &aSurface),
00122   theWeight(weight),
00123   theField(field)
00124 {}
00125 
00126 BasicTrajectoryState::
00127 BasicTrajectoryState( const LocalTrajectoryParameters& par,
00128                             const LocalTrajectoryError& err,
00129                             const Surface& aSurface,
00130                             const MagneticField* field,
00131                             double weight) :
00132   theFreeState(0),
00133   theLocalError(err),
00134   theLocalParameters(par),
00135   theLocalParametersValid(true),
00136   theGlobalParamsUp2Date(false),
00137   theSurfaceSide(SurfaceSideDefinition::atCenterOfSurface),
00138   theSurfaceP( &aSurface), 
00139   theWeight(weight),
00140   theField(field)
00141 {}
00142 
00143 BasicTrajectoryState::
00144 BasicTrajectoryState(const Surface& aSurface) :
00145   theFreeState(0),
00146   theLocalError(InvalidError()),
00147   theLocalParameters(),
00148   theLocalParametersValid(false),
00149   theGlobalParamsUp2Date(false),
00150   theSurfaceSide(SurfaceSideDefinition::atCenterOfSurface), 
00151   theSurfaceP( &aSurface), 
00152   theWeight(0.),
00153   theField(0)
00154 {}
00155 
00156 
00157 BasicTrajectoryState::~BasicTrajectoryState(){}
00158 
00159 void BasicTrajectoryState::notValid() {
00160   throw TrajectoryStateException("TrajectoryStateOnSurface is invalid and cannot return any parameters");
00161 }
00162 
00163 namespace {
00164   void verifyLocalErr(LocalTrajectoryError const & err ) {
00165     if unlikely(!err.posDef())
00166                  edm::LogWarning("BasicTrajectoryState") << "local error not pos-def\n"
00167                                                          <<  err.matrix();
00168   }
00169   void verifyCurvErr(CurvilinearTrajectoryError const & err ) {
00170     if unlikely(!err.posDef())
00171                  edm::LogWarning("BasicTrajectoryState") << "curv error not pos-def\n" 
00172                                                          <<  err.matrix();
00173   }
00174 
00175 }
00176 
00177 void BasicTrajectoryState::missingError(char const * where) const{
00178   std::stringstream form;
00179   form<<"BasicTrajectoryState: attempt to access errors when none available "
00180       <<where<<".\nfreestate pointer: " <<theFreeState
00181       <<"\nlocal error valid/values :"<< theLocalError.valid() << "\n" 
00182       <<  theLocalError.matrix();
00183 
00184   edm::LogWarning("BasicTrajectoryState") << form.str();
00185 
00186   // throw TrajectoryStateException(form.str());
00187 }
00188 
00189 
00190 
00191 
00192 void BasicTrajectoryState::checkGlobalParameters() const {
00193   if likely(theGlobalParamsUp2Date) return;
00194  
00195   //    cout<<"!theGlobalParamsUp2Date"<<endl;
00196   theGlobalParamsUp2Date = true;
00197   // calculate global parameters from local
00198   GlobalPoint  x = surface().toGlobal(theLocalParameters.position());
00199   GlobalVector p = surface().toGlobal(theLocalParameters.momentum());
00200   // replace in place
00201   FreeTrajectoryState * fts = &(*theFreeState);
00202   if (fts) { 
00203     fts->~FreeTrajectoryState();
00204     new(fts) FreeTrajectoryState(x, p, theLocalParameters.charge(), theField);
00205   }else {
00206     theFreeState.replaceWith(new FreeTrajectoryState(x, p,
00207                                                      theLocalParameters.charge(),
00208                                                      theField));
00209   } 
00210 }
00211 
00212 
00213 
00214 void BasicTrajectoryState::checkCurvilinError() const {
00215   if likely(theFreeState->hasCurvilinearError()) return;
00216 
00217   if unlikely(!theLocalParametersValid) createLocalParameters();
00218   
00219   
00220   JacobianLocalToCurvilinear loc2Curv(surface(), localParameters(), *theField);
00221   const AlgebraicMatrix55& jac = loc2Curv.jacobian();
00222   
00223   const AlgebraicSymMatrix55 &cov = ROOT::Math::Similarity(jac, theLocalError.matrix());
00224 
00225    theFreeState->setCurvilinearError( cov );
00226  
00227    verifyLocalErr(theLocalError);
00228    verifyCurvErr(cov); 
00229 }
00230 
00231 
00232  
00233 // create local parameters from global
00234 void BasicTrajectoryState::createLocalParameters() const {
00235   LocalPoint  x = surface().toLocal(theFreeState->position());
00236   LocalVector p = surface().toLocal(theFreeState->momentum());
00237 // believe p.z() never exactly equals 0.
00238   bool isCharged = theFreeState->charge()!=0;
00239   theLocalParameters =
00240     LocalTrajectoryParameters(isCharged?theFreeState->signedInverseMomentum():1./p.mag(),
00241       p.x()/p.z(), p.y()/p.z(), x.x(), x.y(), p.z()>0. ? 1.:-1., isCharged);
00242   theLocalParametersValid = true;
00243 }
00244 
00245 void BasicTrajectoryState::createLocalError() const {
00246   if likely(theFreeState->hasCurvilinearError())
00247     createLocalErrorFromCurvilinearError();
00248   else theLocalError = InvalidError();
00249 }
00250 
00251 void 
00252 BasicTrajectoryState::createLocalErrorFromCurvilinearError() const {
00253   
00254   JacobianCurvilinearToLocal curv2Loc(surface(), localParameters(), *theField);
00255   const AlgebraicMatrix55& jac = curv2Loc.jacobian();
00256   
00257   const AlgebraicSymMatrix55 &cov = 
00258     ROOT::Math::Similarity(jac, theFreeState->curvilinearError().matrix());
00259   //    cout<<"Clocal via curvilinear error"<<endl;
00260   theLocalError = LocalTrajectoryError(cov);
00261 
00262   verifyCurvErr(theFreeState->curvilinearError());
00263   verifyLocalErr(theLocalError);
00264 
00265 }
00266  
00267 
00268 
00269 void
00270 BasicTrajectoryState::update( const LocalTrajectoryParameters& p,
00271         const Surface& aSurface,
00272         const MagneticField* field,
00273         const SurfaceSide side) 
00274 {
00275     theLocalParameters = p;
00276     if (&aSurface != &*theSurfaceP) theSurfaceP.reset(&aSurface);
00277     theField=field;
00278     theSurfaceSide = side;
00279     theWeight      = 1.0; 
00280     theLocalError = InvalidError();
00281 
00282     theGlobalParamsUp2Date   = false;
00283     theLocalParametersValid  = true;
00284 }
00285 
00286 void
00287 BasicTrajectoryState::update( const LocalTrajectoryParameters& p,
00288         const LocalTrajectoryError& err,
00289         const Surface& aSurface,
00290         const MagneticField* field,
00291         const SurfaceSide side, 
00292         double weight) 
00293 {
00294     theLocalParameters = p;
00295     theLocalError      = err;
00296     if (&aSurface != &*theSurfaceP) theSurfaceP.reset(&aSurface);
00297     theField=field;
00298     theSurfaceSide = side;
00299     theWeight      = weight; 
00300 
00301     theGlobalParamsUp2Date   = false;
00302     theLocalParametersValid  = true;
00303 
00304 }
00305 
00306 void 
00307 BasicTrajectoryState::rescaleError(double factor) {
00308   if unlikely(!hasError()) missingError(" trying to rescale");    
00309   if (theFreeState)
00310     theFreeState->rescaleError(factor);
00311   
00312   if (theLocalError.valid()){
00313     //do it by hand if the free state is not around.
00314     bool zeroField = (theField->nominalValue()==0);
00315     if unlikely(zeroField){
00316       AlgebraicSymMatrix55 errors=theLocalError.matrix();
00317       //scale the 0 indexed covariance by the square root of the factor
00318       for (unsigned int i=1;i!=5;++i)      errors(i,0)*=factor;
00319       double factor_squared=factor*factor;
00320       //scale all others by the scaled factor
00321       for (unsigned int i=1;i!=5;++i)  for (unsigned int j=i;j!=5;++j) errors(i,j)*=factor_squared;
00322       //term 0,0 is not scaled at all
00323       theLocalError = LocalTrajectoryError(errors);
00324     }
00325     else theLocalError *= (factor*factor);
00326   }
00327 }
00328 
00329 FreeTrajectoryState* 
00330 BasicTrajectoryState::freeTrajectoryState(bool withErrors) const {
00331   if(!isValid()) notValid();
00332   checkGlobalParameters();
00333   if(withErrors && hasError()) { // this is the right thing
00334     checkCurvilinError();
00335   }
00336   return &(*theFreeState);
00337 }
00338 
00339 
00340 
00341 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00342 std::vector<TrajectoryStateOnSurface> 
00343 BasicTrajectoryState::components() const {
00344   std::vector<TrajectoryStateOnSurface> result; result.reserve(1);
00345   result.push_back( const_cast<BasicTrajectoryState*>(this));
00346   return result;
00347 }