20 #include "G4ErrorCylSurfaceTarget.hh" 21 #include "G4ErrorFreeTrajState.hh" 22 #include "G4ErrorPlaneSurfaceTarget.hh" 23 #include "G4ErrorPropagatorData.hh" 24 #include "G4ErrorRunManagerHelper.hh" 25 #include "G4EventManager.hh" 27 #include "G4FieldManager.hh" 28 #include "G4GeometryTolerance.hh" 29 #include "G4SteppingControl.hh" 30 #include "G4TransportationManager.hh" 32 #include "G4UImanager.hh" 33 #include "G4ErrorPropagationNavigator.hh" 34 #include "G4RunManagerKernel.hh" 35 #include "G4StateManager.hh" 38 #include <CLHEP/Units/SystemOfUnits.h> 49 theG4eManager(G4ErrorPropagatorManager::GetErrorPropagatorManager()),
50 theG4eData(G4ErrorPropagatorData::GetErrorPropagatorData()),
52 LogDebug(
"Geant4e") <<
"Geant4e Propagator initialized";
62 LogDebug(
"Geant4e") <<
"Geant4ePropagator::~Geant4ePropagator()" << std::endl;
78 LogDebug(
"Geant4ePropagator") <<
"G4 propagator starts isInitialized, theField: " <<
theField;
80 auto man = G4RunManagerKernel::GetRunManagerKernel();
81 if (G4StateManager::GetStateManager()->GetCurrentState() == G4State_PreInit) {
82 man->SetVerboseLevel(0);
86 G4UImanager::GetUIpointer()->ApplyCommand(
"/geant4e/limits/stepLength 10.0 mm");
88 const G4Field *field = G4TransportationManager::GetTransportationManager()->GetFieldManager()->GetDetectorField();
89 if (field ==
nullptr) {
92 LogDebug(
"Geant4ePropagator") <<
"G4 propagator initialized; field: " << field;
97 bool moveTargetToEndOfSurface)
const {
101 normalPlane = normalPlane.
unit();
111 return ErrorTargetPair(
false, std::make_shared<G4ErrorPlaneSurfaceTarget>(surfNorm, surfPos));
116 bool moveTargetToEndOfSurface)
const {
120 G4float radCyl = pDest.
radius() * CLHEP::cm;
129 LogDebug(
"Geant4e") <<
"G4e - G4Rotation" << rotCyl <<
"mm";
131 return ErrorTargetPair(!moveTargetToEndOfSurface, std::make_shared<G4ErrorCylSurfaceTarget>(radCyl, posCyl, rotCyl));
166 if (pDest.
localZ(cmsInitPos) * pDest.
localZ(cmsInitMom) < 0) {
167 mode = G4ErrorMode_PropForwards;
168 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'forwards\' indirect " 169 "via the Any direction" 172 mode = G4ErrorMode_PropBackwards;
173 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'backwards\' indirect " 174 "via the Any direction" 194 mode = G4ErrorMode_PropBackwards;
195 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'backwards\' indirect " 196 "via the Any direction";
198 mode = G4ErrorMode_PropForwards;
199 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'forwards\' indirect " 200 "via the Any direction";
206 template <
class SurfaceType>
208 SurfaceType
const &pDest,
214 mode = G4ErrorMode_PropBackwards;
215 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'backwards\' " << std::endl;
217 mode = G4ErrorMode_PropForwards;
218 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'forwards\'" << std::endl;
223 edm::LogError(
"Geant4e") <<
"G4e - Unsupported propagation mode";
229 template <
class SurfaceType>
231 const SurfaceType &pDest)
const {
244 bool flipped =
false;
248 cmsInitMom = -cmsInitMom;
253 G4ErrorMode
mode = G4ErrorMode_PropForwards;
258 if (
mode == G4ErrorMode_PropBackwards && !flipped)
259 cmsInitMom = -cmsInitMom;
275 G4ErrorTrajErr g4error(5, 1);
280 LogDebug(
"Geant4e") <<
"CMS - Error matrix: " << std::endl << initErr.
matrix();
282 LogDebug(
"Geant4e") <<
"No error matrix available" << std::endl;
286 LogDebug(
"Geant4e") <<
"G4e - Error matrix: " << std::endl << g4error;
290 if (
mode == G4ErrorMode_PropForwards) {
291 G4ErrorPropagatorData::GetErrorPropagatorData()->SetStage(G4ErrorStage_Inflation);
292 }
else if (
mode == G4ErrorMode_PropBackwards) {
293 G4ErrorPropagatorData::GetErrorPropagatorData()->SetStage(G4ErrorStage_Deflation);
297 LogDebug(
"Geant4e") <<
"G4e - Traj. State: " << (g4eTrajState);
302 double finalPathLength = 0;
304 HepGeom::Point3D<double> finalRecoPos;
306 G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode(
mode);
308 theG4eData->SetTarget(g4eTarget_center.second.get());
309 LogDebug(
"Geant4e") <<
"Running Propagation to the RECO surface" << std::endl;
314 theG4eManager->GetErrorPropagationNavigator()->LocateGlobalPointAndSetup(
315 g4InitPos, &g4InitMom,
false,
false);
317 bool continuePropagation =
true;
318 while (continuePropagation) {
320 LogDebug(
"Geant4e") << std::endl <<
"step count " << iterations <<
" step length " << finalPathLength;
323 theG4eManager->GetErrorPropagationNavigator()->LocateGlobalPointWithinVolume(g4eTrajState.GetPosition());
334 LogDebug(
"Geant4e") <<
"step Length was " << thisPathLength <<
" cm, current global position: " 337 finalPathLength += thisPathLength;
340 if (std::fabs(finalPathLength) > 200.0f) {
341 LogDebug(
"Geant4e") <<
"ERROR: Quitting propagation: path length mega large" << std::endl;
342 theG4eManager->GetPropagator()->InvokePostUserTrackingAction(g4eTrajState.GetG4Track());
343 continuePropagation =
false;
344 LogDebug(
"Geant4e") <<
"WARNING: Quitting propagation: max path length " 345 "exceeded, returning invalid state" 352 if (
theG4eManager->GetPropagator()->CheckIfLastStep(g4eTrajState.GetG4Track())) {
353 theG4eManager->GetPropagator()->InvokePostUserTrackingAction(g4eTrajState.GetG4Track());
354 continuePropagation =
false;
360 finalPathLength = -finalPathLength;
363 LogDebug(
"Geant4e") <<
"Position on the RECO surface" << g4eTrajState.GetPosition() << std::endl;
364 finalRecoPos = g4eTrajState.GetPosition();
368 LogDebug(
"Geant4e") <<
"Final position of the Track :" << g4eTrajState.GetPosition() << std::endl;
375 const HepGeom::Vector3D<double> momEnd = g4eTrajState.GetMomentum();
386 G4ErrorTrajErr g4errorEnd = g4eTrajState.GetError();
391 if (
mode == G4ErrorMode_PropBackwards) {
397 momEndGV = -momEndGV;
400 LogDebug(
"Geant4e") <<
"G4e - Error matrix after propagation: " << std::endl << g4errorEnd;
402 LogDebug(
"Geant4e") <<
"CMS - Error matrix after propagation: " << std::endl << curvError.
matrix();
425 const Plane &pDest)
const {
456 HepGeom::Point3D<double>
const &surfPos,
458 HepGeom::Normal3D<double>
const &surfNorm,
459 const Plane &pDest)
const {
460 LogDebug(
"Geant4e") <<
"G4e - Destination CMS plane position:" << posPlane <<
"cm\n" 461 <<
"G4e - (Ro, eta, phi): (" << posPlane.
perp() <<
" cm, " << posPlane.
eta()
462 <<
", " << posPlane.
phi().
degrees() <<
" deg)\n" 463 <<
"G4e - Destination G4 plane position: " << surfPos <<
" mm, Ro = " << surfPos.perp()
465 LogDebug(
"Geant4e") <<
"G4e - Destination CMS plane normal : " << normalPlane <<
"\n" 466 <<
"G4e - Destination G4 plane normal : " << normalPlane;
467 LogDebug(
"Geant4e") <<
"G4e - Distance from plane position to plane: " << pDest.
localZ(posPlane) <<
" cm";
470 template <
class SurfaceType>
473 CLHEP::Hep3Vector
const &g4InitPos,
475 CLHEP::Hep3Vector
const &g4InitMom,
476 const SurfaceType &pDest)
const {
477 LogDebug(
"Geant4e") <<
"G4e - Current Context: " << currentContext;
478 LogDebug(
"Geant4e") <<
"G4e - CMS point position:" << cmsInitPos <<
"cm\n" 479 <<
"G4e - (Ro, eta, phi): (" << cmsInitPos.
perp() <<
" cm, " << cmsInitPos.
eta()
480 <<
", " << cmsInitPos.
phi().
degrees() <<
" deg)\n" 481 <<
"G4e - G4 point position: " << g4InitPos <<
" mm, Ro = " << g4InitPos.perp() <<
" mm";
482 LogDebug(
"Geant4e") <<
"G4e - CMS momentum :" << cmsInitMom <<
"GeV\n" 483 <<
" pt: " << cmsInitMom.
perp() <<
"G4e - G4 momentum : " << g4InitMom <<
" MeV";
std::pair< TrajectoryStateOnSurface, double > TsosPP
Local3DVector LocalVector
Point3DBase< Scalar, LocalTag > LocalPoint
const CurvilinearTrajectoryError & curvilinearError() const
void debugReportTrackState(std::string const ¤tContext, GlobalPoint const &cmsInitPos, CLHEP::Hep3Vector const &g4InitPos, GlobalVector const &cmsInitMom, CLHEP::Hep3Vector const &g4InitMom, const SurfaceType &pDest) const
HepGeom::Normal3D< double > globalVectorToHepNormal3D(const GlobalVector &p)
Geom::Phi< T > phi() const
double g4doubleToCmsDouble(const G4double &d)
std::pair< TrajectoryStateOnSurface, double > propagateWithPath(const FreeTrajectoryState &, const Plane &) const override
ALPAKA_FN_ACC int side(int ieta, int iphi)
std::string theParticleName
virtual PropagationDirection propagationDirection() const final
CLHEP::Hep3Vector globalVectorToHep3Vector(const GlobalVector &p)
TrackCharge charge() const
Log< level::Error, false > LogError
const MagneticField * theField
LocalPoint toLocal(const GlobalPoint &gp) const
const GlobalTrajectoryParameters & parameters() const
G4ErrorPropagatorManager * theG4eManager
void ensureGeant4eIsInitilized(bool forceInit) const
GlobalPoint position() const
CLHEP::HepRotation tkRotationFToHepRotation(const TkRotation< float > &tkr)
AlgebraicSymMatrix55 g4ErrorTrajErrToAlgebraicSymMatrix55(const G4ErrorTrajErr &e, const int q)
G4ErrorPropagatorData * theG4eData
TrackCharge charge() const
GlobalPoint hepPoint3DToGlobalPoint(const HepGeom::Point3D< double > &r)
std::pair< bool, std::shared_ptr< G4ErrorTarget > > ErrorTargetPair
GlobalVector momentum() const
void debugReportPlaneSetup(GlobalPoint const &posPlane, HepGeom::Point3D< double > const &surfPos, GlobalVector const &normalPlane, HepGeom::Normal3D< double > const &surfNorm, const Plane &pDest) const
bool configureAnyPropagation(G4ErrorMode &mode, SurfaceType const &pDest, GlobalPoint const &cmsInitPos, GlobalVector const &cmsInitMom) const
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
float localZ(const GlobalPoint &gp) const
HepGeom::Point3D< double > globalPointToHepPoint3D(const GlobalPoint &r)
const PositionType & position() const
~Geant4ePropagator() override
const AlgebraicSymMatrix55 & matrix() const
const MagneticField & magneticField() const
Side side(const LocalPoint &p, Scalar toler) const override
GlobalVector hep3VectorToGlobalVector(const CLHEP::Hep3Vector &p)
std::string getSurfaceType(SurfaceType const &surface) const
std::string generateParticleName(int charge) const
Scalar radius() const
Radius of the cylinder.
FreeTrajectoryState const * freeState(bool withErrors=true) const
bool configurePropagation(G4ErrorMode &mode, SurfaceType const &pDest, GlobalPoint const &cmsInitPos, GlobalVector const &cmsInitMom) const
G4ErrorTrajErr algebraicSymMatrix55ToG4ErrorTrajErr(const AlgebraicSymMatrix55 &e, const int q)
const RotationType & rotation() const
Vector3DBase unit() const
ErrorTargetPair transformToG4SurfaceTarget(const SurfaceType &pDest, bool moveTargetToEndOfSurface) const
Geant4ePropagator(const MagneticField *field=nullptr, std::string particleName="mu", PropagationDirection dir=alongMomentum, double plimit=1.0)
CLHEP::Hep3Vector globalPointToHep3Vector(const GlobalPoint &r)
std::pair< TrajectoryStateOnSurface, double > propagateGeneric(const FreeTrajectoryState &ftsStart, const SurfaceType &pDest) const