CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/TrackPropagation/Geant4e/src/Geant4ePropagator.cc

Go to the documentation of this file.
00001 
00002 //Geant4e
00003 #include "TrackPropagation/Geant4e/interface/Geant4ePropagator.h"
00004 #include "TrackPropagation/Geant4e/interface/ConvertFromToCLHEP.h"
00005 #include "TrackPropagation/Geant4e/interface/Geant4eSteppingAction.h"
00006 
00007 //CMSSW
00008 #include "MagneticField/Engine/interface/MagneticField.h"
00009 #include "DataFormats/TrajectorySeed/interface/PropagationDirection.h"
00010 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00011 #include "TrackingTools/TrajectoryState/interface/SurfaceSideDefinition.h"
00012 #include "DataFormats/GeometrySurface/interface/Cylinder.h"
00013 #include "DataFormats/GeometrySurface/interface/Plane.h"
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 //Geant4
00017 #include "G4ErrorFreeTrajState.hh"
00018 #include "G4ErrorPlaneSurfaceTarget.hh"
00019 #include "G4ErrorCylSurfaceTarget.hh"
00020 #include "G4ErrorPropagatorData.hh"
00021 #include "G4EventManager.hh"
00022 #include "G4SteppingControl.hh"
00023 
00024 //CLHEP
00025 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00026 
00027 
00030 Geant4ePropagator::Geant4ePropagator(const MagneticField* field,
00031                                      const char* particleName,
00032                                      PropagationDirection dir):
00033   Propagator(dir),
00034   theField(field),
00035   theParticleName(particleName),
00036   theG4eManager(G4ErrorPropagatorManager::GetErrorPropagatorManager()),
00037   theSteppingAction(0) {
00038 
00039   G4ErrorPropagatorData::SetVerbose(0);
00040 }
00041 
00044 Geant4ePropagator::~Geant4ePropagator() {
00045 }
00046 
00047 //
00049 //
00050 
00055 TrajectoryStateOnSurface 
00056 Geant4ePropagator::propagate (const FreeTrajectoryState& ftsStart, 
00057                               const Plane& pDest) const {
00058 
00059   if(theG4eManager->PrintG4ErrorState() == "G4ErrorState_PreInit")
00060     theG4eManager->InitGeant4e();
00061 
00062   if (!theSteppingAction) {
00063     theSteppingAction = new Geant4eSteppingAction;
00064     theG4eManager->SetUserAction(theSteppingAction);
00065   }
00066 
00068   // Construct the target surface
00069   //
00070 
00071   //* Get position and normal (orientation) of the destination plane
00072   GlobalPoint posPlane = pDest.toGlobal(LocalPoint(0,0,0));
00073   GlobalVector normalPlane = pDest.toGlobal(LocalVector(0,0,1.)); 
00074   normalPlane = normalPlane.unit();
00075 
00076   //* Transform this into HepGeom::Point3D<double>  and HepGeom::Normal3D<double>  that define a plane for
00077   //  Geant4e.
00078   //  CMS uses cm and GeV while Geant4 uses mm and MeV
00079   HepGeom::Point3D<double>   surfPos  = 
00080     TrackPropagation::globalPointToHepPoint3D(posPlane);
00081   HepGeom::Normal3D<double>  surfNorm = 
00082     TrackPropagation::globalVectorToHepNormal3D(normalPlane);
00083 
00084   //DEBUG
00085   LogDebug("Geant4e") << "G4e -  Destination CMS plane position:" << posPlane << "cm\n"
00086                       << "G4e -                  (Ro, eta, phi): (" 
00087                       << posPlane.perp() << " cm, " 
00088                       << posPlane.eta() << ", " 
00089                       << posPlane.phi().degrees() << " deg)\n"
00090                       << "G4e -  Destination G4  plane position: " << surfPos
00091                       << " mm, Ro = " << surfPos.perp() << " mm";
00092   LogDebug("Geant4e") << "G4e -  Destination CMS plane normal  : " 
00093                       << normalPlane << "\n"
00094                       << "G4e -  Destination G4  plane normal  : " 
00095                       << normalPlane;
00096   LogDebug("Geant4e") << "G4e -  Distance from plane position to plane: " 
00097                       << pDest.localZ(posPlane) << " cm";
00098   //DEBUG
00099 
00100   //* Set the target surface
00101   G4ErrorSurfaceTarget* g4eTarget = new G4ErrorPlaneSurfaceTarget(surfNorm,
00102                                                                   surfPos);
00103 
00104   //g4eTarget->Dump("G4e - ");
00105   //
00107 
00109   // Find initial point
00110   //
00111 
00112   // * Get the starting point and direction and convert them to CLHEP::Hep3Vector 
00113   //   for G4. CMS uses cm and GeV while Geant4 uses mm and MeV
00114   GlobalPoint  cmsInitPos = ftsStart.position();
00115   GlobalVector cmsInitMom = ftsStart.momentum();
00116 
00117   CLHEP::Hep3Vector g4InitPos = 
00118     TrackPropagation::globalPointToHep3Vector(cmsInitPos);
00119   CLHEP::Hep3Vector g4InitMom = 
00120     TrackPropagation::globalVectorToHep3Vector(cmsInitMom*GeV);
00121 
00122   //DEBUG
00123   LogDebug("Geant4e") << "G4e -  Initial CMS point position:" << cmsInitPos 
00124                       << "cm\n"
00125                       << "G4e -              (Ro, eta, phi): (" 
00126                       << cmsInitPos.perp() << " cm, " 
00127                       << cmsInitPos.eta() << ", " 
00128                       << cmsInitPos.phi().degrees() << " deg)\n"
00129                       << "G4e -  Initial G4  point position: " << g4InitPos 
00130                       << " mm, Ro = " << g4InitPos.perp() << " mm";
00131   LogDebug("Geant4e") << "G4e -  Initial CMS momentum      :" << cmsInitMom 
00132                       << "GeV\n"
00133                       << "G4e -  Initial G4  momentum      : " << g4InitMom 
00134                       << " MeV";
00135   LogDebug("Geant4e") << "G4e -  Distance from initial point to plane: " 
00136                       << pDest.localZ(cmsInitPos) << " cm";
00137   //DEBUG
00138 
00139   //
00141 
00143   // Set particle name
00144   //
00145   int charge = ftsStart.charge();
00146   std::string particleName  = theParticleName;
00147 
00148   if (charge > 0) {
00149       particleName += "+";
00150   } else {
00151       particleName += "-";
00152   }
00153 
00154   LogDebug("Geant4e") << "G4e -  Particle name: " << particleName;
00155 
00156   //
00158 
00160   //Set the error and trajectories, and finally propagate
00161   //
00162   G4ErrorTrajErr g4error( 5, 1 );
00163   if(ftsStart.hasError()) {
00164     const CurvilinearTrajectoryError initErr = ftsStart.curvilinearError();
00165     g4error = TrackPropagation::algebraicSymMatrix55ToG4ErrorTrajErr( initErr , charge); //The error matrix
00166   }
00167   LogDebug("Geant4e") << "G4e -  Error matrix: " << g4error;
00168 
00169   G4ErrorFreeTrajState* g4eTrajState = 
00170     new G4ErrorFreeTrajState(particleName, g4InitPos, g4InitMom, g4error);
00171   LogDebug("Geant4e") << "G4e -  Traj. State: " << (*g4eTrajState);
00172 
00173   //Set the mode of propagation according to the propagation direction
00174   G4ErrorMode mode = G4ErrorMode_PropForwards;
00175 
00176   if (propagationDirection() == oppositeToMomentum) {
00177     mode = G4ErrorMode_PropBackwards;
00178     LogDebug("Geant4e") << "G4e -  Propagator mode is \'backwards\'";
00179   } else if(propagationDirection() == alongMomentum) {
00180     LogDebug("Geant4e") << "G4e -  Propagator mode is \'forwards\'";
00181   } else {   //Mode must be anyDirection then - need to figure out for Geant which it is
00182     std::cout << "Determining actual direction";
00183     if(pDest.localZ(cmsInitPos)*pDest.localZ(cmsInitMom) < 0) {
00184       LogDebug("Geant4e") << "G4e -  Propagator mode is \'forwards\'";
00185       std::cout << ", got forwards" << std::endl;
00186     } else {
00187       mode = G4ErrorMode_PropBackwards;
00188       LogDebug("Geant4e") << "G4e -  Propagator mode is \'backwards\'";
00189       std::cout << ", got backwards" << std::endl;
00190     }
00191   }
00192 
00193   //
00195 
00197   // Propagate
00198 
00199   int ierr;
00200   if(mode == G4ErrorMode_PropBackwards) {
00201     //To make geant transport the particle correctly need to give it the opposite momentum
00202     //because geant flips the B field bending and adds energy instead of subtracting it
00203     //but still wants the momentum "backwards"
00204     g4eTrajState->SetMomentum( -g4eTrajState->GetMomentum());
00205     ierr = theG4eManager->Propagate( g4eTrajState, g4eTarget, mode);
00206     g4eTrajState->SetMomentum( -g4eTrajState->GetMomentum());
00207   } else {
00208     ierr = theG4eManager->Propagate( g4eTrajState, g4eTarget, mode);
00209   }
00210   LogDebug("Geant4e") << "G4e -  Return error from propagation: " << ierr;
00211 
00212   if(ierr!=0) {
00213     LogDebug("Geant4e") << "G4e - Error is not 0, returning invalid trajectory";
00214     return TrajectoryStateOnSurface();
00215   }
00216 
00217   //
00219 
00221   // Retrieve the state in the end from Geant4e, convert them to CMS vectors
00222   // and points, and build global trajectory parameters.
00223   // CMS uses cm and GeV while Geant4 uses mm and MeV
00224   //
00225   HepGeom::Point3D<double>  posEnd = g4eTrajState->GetPosition();
00226   HepGeom::Vector3D<double>  momEnd = g4eTrajState->GetMomentum();
00227 
00228   GlobalPoint  posEndGV = TrackPropagation::hepPoint3DToGlobalPoint(posEnd);
00229   GlobalVector momEndGV = TrackPropagation::hep3VectorToGlobalVector(momEnd)/GeV;
00230 
00231   //DEBUG
00232   LogDebug("Geant4e") << "G4e -  Final CMS point position:" << posEndGV 
00233                       << "cm\n"
00234                       << "G4e -            (Ro, eta, phi): (" 
00235                       << posEndGV.perp() << " cm, " 
00236                       << posEndGV.eta() << ", " 
00237                       << posEndGV.phi().degrees() << " deg)\n"
00238                       << "G4e -  Final G4  point position: " << posEnd 
00239                       << " mm,\tRo =" << posEnd.perp()  << " mm";
00240   LogDebug("Geant4e") << "G4e -  Final CMS momentum      :" << momEndGV
00241                       << "GeV\n"
00242                       << "G4e -  Final G4  momentum      : " << momEnd 
00243                       << " MeV";
00244   LogDebug("Geant4e") << "G4e -  Distance from final point to plane: " 
00245                       << pDest.localZ(posEndGV) << " cm";
00246   //DEBUG
00247 
00248   GlobalTrajectoryParameters tParsDest(posEndGV, momEndGV, charge, theField);
00249 
00250 
00251   // Get the error covariance matrix from Geant4e. It comes in curvilinear
00252   // coordinates so use the appropiate CMS class  
00253   G4ErrorTrajErr g4errorEnd = g4eTrajState->GetError();
00254   CurvilinearTrajectoryError 
00255     curvError(TrackPropagation::g4ErrorTrajErrToAlgebraicSymMatrix55(g4errorEnd, charge));
00256   LogDebug("Geant4e") << "G4e -  Error matrix after propagation: " << g4errorEnd;
00257 
00259   // We set the SurfaceSide to atCenterOfSurface.                       //
00261   LogDebug("Geant4e") << "G4e -  SurfaceSide is always atCenterOfSurface after propagation";
00262   SurfaceSideDefinition::SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface;
00263   //
00265 
00266   return TrajectoryStateOnSurface(tParsDest, curvError, pDest, side);
00267 }
00268 
00269 //Require method with input TrajectoryStateOnSurface to be used in track fitting
00270 //Don't need extra info about starting surface; use regular propagation method
00271 TrajectoryStateOnSurface
00272 Geant4ePropagator::propagate (const TrajectoryStateOnSurface& tsos, const Plane& plane) const {
00273   const FreeTrajectoryState ftsStart = *tsos.freeState();
00274   return propagate(ftsStart,plane);
00275 }
00276 
00277 
00281 TrajectoryStateOnSurface 
00282 Geant4ePropagator::propagate (const FreeTrajectoryState& ftsStart, 
00283                               const Cylinder& cDest) const {
00284 
00285   if(theG4eManager->PrintG4ErrorState() == "G4ErrorState_PreInit")
00286     theG4eManager->InitGeant4e();
00287   if (!theSteppingAction) {
00288     theSteppingAction = new Geant4eSteppingAction;
00289     theG4eManager->SetUserAction(theSteppingAction);
00290   }
00291 
00292   //Get Cylinder parameters.
00293   //CMS uses cm and GeV while Geant4 uses mm and MeV.
00294   // - Radius
00295   G4float radCyl = cDest.radius()*cm;
00296   // - Position: PositionType & GlobalPoint are Basic3DPoint<float,GlobalTag>
00297   G4ThreeVector posCyl = 
00298     TrackPropagation::globalPointToHep3Vector(cDest.position());
00299   // - Rotation: Type in CMSSW is RotationType == TkRotation<T>, T=float
00300   G4RotationMatrix rotCyl = 
00301     TrackPropagation::tkRotationFToHepRotation(cDest.rotation());
00302 
00303   //DEBUG
00304   TkRotation<float>  rotation = cDest.rotation();
00305   LogDebug("Geant4e") << "G4e -  TkRotation" << rotation;
00306   LogDebug("Geant4e") << "G4e -  G4Rotation" << rotCyl << "mm";
00307 
00308 
00309   //Set the target surface
00310   G4ErrorSurfaceTarget* g4eTarget = new G4ErrorCylSurfaceTarget(radCyl, posCyl,
00311                                                                 rotCyl);
00312 
00313   //DEBUG
00314   LogDebug("Geant4e") << "G4e -  Destination CMS cylinder position:" << cDest.position() << "cm\n"
00315                       << "G4e -  Destination CMS cylinder radius:" << cDest.radius() << "cm\n"
00316                       << "G4e -  Destination CMS cylinder rotation:" << cDest.rotation() << "\n";
00317   LogDebug("Geant4e") << "G4e -  Destination G4  cylinder position: " << posCyl << "mm\n"
00318                       << "G4e -  Destination G4  cylinder radius:" << radCyl << "mm\n"
00319                       << "G4e -  Destination G4  cylinder rotation:" << rotCyl << "\n";
00320 
00321 
00322   //Get the starting point and direction and convert them to CLHEP::Hep3Vector for G4
00323   //CMS uses cm and GeV while Geant4 uses mm and MeV
00324   GlobalPoint  cmsInitPos = ftsStart.position();
00325   GlobalVector cmsInitMom = ftsStart.momentum();
00326 
00327   CLHEP::Hep3Vector g4InitMom = 
00328     TrackPropagation::globalVectorToHep3Vector(cmsInitMom*GeV);
00329   CLHEP::Hep3Vector g4InitPos = 
00330     TrackPropagation::globalPointToHep3Vector(cmsInitPos);
00331 
00332   //DEBUG
00333   LogDebug("Geant4e") << "G4e -  Initial CMS point position:" << cmsInitPos 
00334                       << "cm\n"
00335                       << "G4e -              (Ro, eta, phi): (" 
00336                       << cmsInitPos.perp() << " cm, " 
00337                       << cmsInitPos.eta() << ", " 
00338                       << cmsInitPos.phi().degrees() << " deg)\n"
00339                       << "G4e -  Initial G4  point position: " << g4InitPos 
00340                       << " mm, Ro = " << g4InitPos.perp() << " mm";
00341   LogDebug("Geant4e") << "G4e -  Initial CMS momentum      :" << cmsInitMom 
00342                       << "GeV\n"
00343                       << "G4e -  Initial G4  momentum      : " << g4InitMom 
00344                       << " MeV";
00345 
00346   //Set particle name
00347   int charge = ftsStart.charge();
00348   std::string particleName  = theParticleName;
00349   if (charge > 0)
00350     particleName += "+";
00351   else
00352     particleName += "-";
00353   LogDebug("Geant4e") << "G4e -  Particle name: " << particleName;
00354 
00355   //Set the error and trajectories, and finally propagate
00356   G4ErrorTrajErr g4error( 5, 1 );
00357   if(ftsStart.hasError()) {
00358     const CurvilinearTrajectoryError initErr = ftsStart.curvilinearError();
00359     g4error = TrackPropagation::algebraicSymMatrix55ToG4ErrorTrajErr( initErr , charge); //The error matrix
00360   }
00361   LogDebug("Geant4e") << "G4e -  Error matrix: " << g4error;
00362 
00363   G4ErrorFreeTrajState* g4eTrajState = 
00364     new G4ErrorFreeTrajState(particleName, g4InitPos, g4InitMom, g4error);
00365   LogDebug("Geant4e") << "G4e -  Traj. State: " << (*g4eTrajState);
00366 
00367   //Set the mode of propagation according to the propagation direction
00368   G4ErrorMode mode = G4ErrorMode_PropForwards;
00369 
00370   if (propagationDirection() == oppositeToMomentum) {
00371     mode = G4ErrorMode_PropBackwards;
00372     LogDebug("Geant4e") << "G4e -  Propagator mode is \'backwards\'";
00373   } else if(propagationDirection() == alongMomentum) {
00374     LogDebug("Geant4e") << "G4e -  Propagator mode is \'forwards\'";
00375   } else {
00376     //------------------------------------
00377     //For cylinder assume outside is backwards, inside is along
00378     //General use for particles from collisions
00379     LocalPoint lpos = cDest.toLocal(cmsInitPos);
00380     Surface::Side theSide = cDest.side(lpos,0);
00381     if(theSide==SurfaceOrientation::positiveSide){  //outside cylinder
00382       mode = G4ErrorMode_PropBackwards;
00383       LogDebug("Geant4e") << "G4e -  Propagator mode is \'backwards\'";
00384     } else { //inside cylinder
00385       LogDebug("Geant4e") << "G4e -  Propagator mode is \'forwards\'";
00386     }
00387 
00388   }
00389 
00391   // Propagate
00392 
00393   int ierr;
00394   if(mode == G4ErrorMode_PropBackwards) {
00395     //To make geant transport the particle correctly need to give it the opposite momentum
00396     //because geant flips the B field bending and adds energy instead of subtracting it
00397     //but still wants the momentum "backwards"
00398     g4eTrajState->SetMomentum( -g4eTrajState->GetMomentum());
00399     ierr = theG4eManager->Propagate( g4eTrajState, g4eTarget, mode);
00400     g4eTrajState->SetMomentum( -g4eTrajState->GetMomentum());
00401   } else {
00402     ierr = theG4eManager->Propagate( g4eTrajState, g4eTarget, mode);
00403   }
00404   LogDebug("Geant4e") << "G4e -  Return error from propagation: " << ierr;
00405 
00406   if(ierr!=0) {
00407     LogDebug("Geant4e") << "G4e - Error is not 0, returning invalid trajectory";
00408     return TrajectoryStateOnSurface();
00409   }
00410 
00411   // Retrieve the state in the end from Geant4e, converte them to CMS vectors
00412   // and points, and build global trajectory parameters
00413   // CMS uses cm and GeV while Geant4 uses mm and MeV
00414   HepGeom::Point3D<double>  posEnd = g4eTrajState->GetPosition();
00415   HepGeom::Vector3D<double>  momEnd = g4eTrajState->GetMomentum();
00416 
00417   GlobalPoint  posEndGV = TrackPropagation::hepPoint3DToGlobalPoint(posEnd);
00418   GlobalVector momEndGV = TrackPropagation::hep3VectorToGlobalVector(momEnd)/GeV;
00419 
00420 
00421   //DEBUG
00422   LogDebug("Geant4e") << "G4e -  Final CMS point position:" << posEndGV 
00423                       << "cm\n"
00424                       << "G4e -            (Ro, eta, phi): (" 
00425                       << posEndGV.perp() << " cm, " 
00426                       << posEndGV.eta() << ", " 
00427                       << posEndGV.phi().degrees() << " deg)\n"
00428                       << "G4e -  Final G4  point position: " << posEnd 
00429                       << " mm,\tRo =" << posEnd.perp()  << " mm";
00430   LogDebug("Geant4e") << "G4e -  Final CMS momentum      :" << momEndGV
00431                       << "GeV\n"
00432                       << "G4e -  Final G4  momentum      : " << momEnd 
00433                       << " MeV";
00434 
00435   GlobalTrajectoryParameters tParsDest(posEndGV, momEndGV, charge, theField);
00436 
00437 
00438   // Get the error covariance matrix from Geant4e. It comes in curvilinear
00439   // coordinates so use the appropiate CMS class  
00440   G4ErrorTrajErr g4errorEnd = g4eTrajState->GetError();
00441   CurvilinearTrajectoryError 
00442     curvError(TrackPropagation::g4ErrorTrajErrToAlgebraicSymMatrix55(g4errorEnd, charge));
00443   LogDebug("Geant4e") << "G4e -  Error matrix after propagation: " << g4errorEnd;
00444 
00446   // We set the SurfaceSide to atCenterOfSurface.                       //
00448 
00449   SurfaceSideDefinition::SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface;
00450 
00451   return TrajectoryStateOnSurface(tParsDest, curvError, cDest, side);
00452 }
00453 
00454 
00455 //Require method with input TrajectoryStateOnSurface to be used in track fitting
00456 //Don't need extra info about starting surface; use regular propagation method
00457 TrajectoryStateOnSurface
00458 Geant4ePropagator::propagate (const TrajectoryStateOnSurface& tsos, const Cylinder& cyl) const {
00459   const FreeTrajectoryState ftsStart = *tsos.freeState();
00460   return propagate(ftsStart,cyl);
00461 }
00462 
00463 
00464 //
00466 //
00467 
00474 std::pair< TrajectoryStateOnSurface, double> 
00475 Geant4ePropagator::propagateWithPath (const FreeTrajectoryState& ftsStart, 
00476                                       const Plane& pDest) const {
00477 
00478   theSteppingAction->reset();
00479 
00480   //Finally build the pair<...> that needs to be returned where the second
00481   //parameter is the exact path length. Currently calculated with a stepping
00482   //action that adds up the length of every step
00483   return TsosPP(propagate(ftsStart,pDest), theSteppingAction->trackLength());
00484 }
00485 
00486 std::pair< TrajectoryStateOnSurface, double> 
00487 Geant4ePropagator::propagateWithPath (const FreeTrajectoryState& ftsStart,
00488                                       const Cylinder& cDest) const {
00489   theSteppingAction->reset();
00490 
00491   //Finally build the pair<...> that needs to be returned where the second
00492   //parameter is the exact path length. Currently calculated with a stepping
00493   //action that adds up the length of every step
00494   return TsosPP(propagate(ftsStart,cDest), theSteppingAction->trackLength());
00495 }
00496 
00497 std::pair< TrajectoryStateOnSurface, double> 
00498 Geant4ePropagator::propagateWithPath (const TrajectoryStateOnSurface& tsosStart, 
00499                                       const Plane& pDest) const {
00500 
00501   theSteppingAction->reset();
00502 
00503   //Finally build the pair<...> that needs to be returned where the second
00504   //parameter is the exact path length. Currently calculated with a stepping
00505   //action that adds up the length of every step
00506   return TsosPP(propagate(tsosStart,pDest), theSteppingAction->trackLength());
00507 }
00508 
00509 std::pair< TrajectoryStateOnSurface, double> 
00510 Geant4ePropagator::propagateWithPath (const TrajectoryStateOnSurface& tsosStart,
00511                                       const Cylinder& cDest) const {
00512   theSteppingAction->reset();
00513 
00514   //Finally build the pair<...> that needs to be returned where the second
00515   //parameter is the exact path length. Currently calculated with a stepping
00516   //action that adds up the length of every step
00517   return TsosPP(propagate(tsosStart,cDest), theSteppingAction->trackLength());
00518 }