CMS 3D CMS Logo

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