CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/TrackingTools/TrajectoryState/src/BasicSingleTrajectoryState.cc

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