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"
35 #include "CLHEP/Units/GlobalSystemOfUnits.h"
46 theG4eManager(G4ErrorPropagatorManager::GetErrorPropagatorManager()),
47 theG4eData(G4ErrorPropagatorData::GetErrorPropagatorData()),
49 LogDebug(
"Geant4e") <<
"Geant4e Propagator initialized";
59 LogDebug(
"Geant4e") <<
"Geant4ePropagator::~Geant4ePropagator()" << std::endl;
75 LogDebug(
"Geant4e") <<
"ensureGeant4eIsInitilized called" << std::endl;
76 if ((G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_PreInit) || forceInit) {
77 LogDebug(
"Geant4e") <<
"Initializing G4 propagator" << std::endl;
83 const G4Field *field = G4TransportationManager::GetTransportationManager()->GetFieldManager()->GetDetectorField();
84 if (field ==
nullptr) {
87 LogDebug(
"Geant4e") <<
"G4 propagator initialized" << std::endl;
89 LogDebug(
"Geant4e") <<
"G4 not in preinit state: " << G4ErrorPropagatorData::GetErrorPropagatorData()->GetState()
93 G4UImanager::GetUIpointer()->ApplyCommand(
"/geant4e/limits/stepLength 10.0 mm");
98 bool moveTargetToEndOfSurface)
const {
102 normalPlane = normalPlane.
unit();
112 return ErrorTargetPair(
false, std::make_shared<G4ErrorPlaneSurfaceTarget>(surfNorm, surfPos));
117 bool moveTargetToEndOfSurface)
const {
121 G4float radCyl = pDest.
radius() * cm;
130 LogDebug(
"Geant4e") <<
"G4e - G4Rotation" << rotCyl <<
"mm";
132 return ErrorTargetPair(!moveTargetToEndOfSurface, std::make_shared<G4ErrorCylSurfaceTarget>(radCyl, posCyl, rotCyl));
167 if (pDest.
localZ(cmsInitPos) * pDest.
localZ(cmsInitMom) < 0) {
168 mode = G4ErrorMode_PropForwards;
169 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'forwards\' indirect "
170 "via the Any direction"
173 mode = G4ErrorMode_PropBackwards;
174 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'backwards\' indirect "
175 "via the Any direction"
195 mode = G4ErrorMode_PropBackwards;
196 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'backwards\' indirect "
197 "via the Any direction";
199 mode = G4ErrorMode_PropForwards;
200 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'forwards\' indirect "
201 "via the Any direction";
207 template <
class SurfaceType>
209 SurfaceType
const &pDest,
215 mode = G4ErrorMode_PropBackwards;
216 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'backwards\' " << std::endl;
218 mode = G4ErrorMode_PropForwards;
219 LogDebug(
"Geant4e") <<
"G4e - Propagator mode is \'forwards\'" << std::endl;
224 edm::LogError(
"Geant4e") <<
"G4e - Unsupported propagation mode";
230 template <
class SurfaceType>
232 const SurfaceType &pDest)
const {
245 bool flipped =
false;
249 cmsInitMom = -cmsInitMom;
254 G4ErrorMode
mode = G4ErrorMode_PropForwards;
259 if (
mode == G4ErrorMode_PropBackwards && !flipped)
260 cmsInitMom = -cmsInitMom;
276 G4ErrorTrajErr g4error(5, 1);
281 LogDebug(
"Geant4e") <<
"CMS - Error matrix: " << std::endl << initErr.
matrix();
283 LogDebug(
"Geant4e") <<
"No error matrix available" << std::endl;
287 LogDebug(
"Geant4e") <<
"G4e - Error matrix: " << std::endl << g4error;
291 if (
mode == G4ErrorMode_PropForwards) {
292 G4ErrorPropagatorData::GetErrorPropagatorData()->SetStage(G4ErrorStage_Inflation);
293 }
else if (
mode == G4ErrorMode_PropBackwards) {
294 G4ErrorPropagatorData::GetErrorPropagatorData()->SetStage(G4ErrorStage_Deflation);
298 LogDebug(
"Geant4e") <<
"G4e - Traj. State: " << (g4eTrajState);
303 double finalPathLength = 0;
305 HepGeom::Point3D<double> finalRecoPos;
307 G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode(
mode);
309 theG4eData->SetTarget(g4eTarget_center.second.get());
310 LogDebug(
"Geant4e") <<
"Running Propagation to the RECO surface" << std::endl;
314 bool continuePropagation =
true;
315 while (continuePropagation) {
317 LogDebug(
"Geant4e") << std::endl <<
"step count " << iterations <<
" step length " << finalPathLength;
328 LogDebug(
"Geant4e") <<
"step Length was " << thisPathLength <<
" cm, current global position: "
331 finalPathLength += thisPathLength;
334 if (std::fabs(finalPathLength) > 200.0f) {
335 LogDebug(
"Geant4e") <<
"ERROR: Quitting propagation: path length mega large" << std::endl;
336 theG4eManager->GetPropagator()->InvokePostUserTrackingAction(g4eTrajState.GetG4Track());
337 continuePropagation =
false;
338 LogDebug(
"Geant4e") <<
"WARNING: Quitting propagation: max path length "
339 "exceeded, returning invalid state"
346 if (
theG4eManager->GetPropagator()->CheckIfLastStep(g4eTrajState.GetG4Track())) {
347 theG4eManager->GetPropagator()->InvokePostUserTrackingAction(g4eTrajState.GetG4Track());
348 continuePropagation =
false;
354 finalPathLength = -finalPathLength;
357 LogDebug(
"Geant4e") <<
"Position on the RECO surface" << g4eTrajState.GetPosition() << std::endl;
358 finalRecoPos = g4eTrajState.GetPosition();
362 LogDebug(
"Geant4e") <<
"Final position of the Track :" << g4eTrajState.GetPosition() << std::endl;
369 const HepGeom::Vector3D<double> momEnd = g4eTrajState.GetMomentum();
380 G4ErrorTrajErr g4errorEnd = g4eTrajState.GetError();
385 if (
mode == G4ErrorMode_PropBackwards) {
391 momEndGV = -momEndGV;
394 LogDebug(
"Geant4e") <<
"G4e - Error matrix after propagation: " << std::endl << g4errorEnd;
396 LogDebug(
"Geant4e") <<
"CMS - Error matrix after propagation: " << std::endl << curvError.
matrix();
419 const Plane &pDest)
const {
450 HepGeom::Point3D<double>
const &surfPos,
452 HepGeom::Normal3D<double>
const &surfNorm,
453 const Plane &pDest)
const {
454 LogDebug(
"Geant4e") <<
"G4e - Destination CMS plane position:" << posPlane <<
"cm\n"
455 <<
"G4e - (Ro, eta, phi): (" << posPlane.
perp() <<
" cm, " << posPlane.
eta()
456 <<
", " << posPlane.
phi().
degrees() <<
" deg)\n"
457 <<
"G4e - Destination G4 plane position: " << surfPos <<
" mm, Ro = " << surfPos.perp()
459 LogDebug(
"Geant4e") <<
"G4e - Destination CMS plane normal : " << normalPlane <<
"\n"
460 <<
"G4e - Destination G4 plane normal : " << normalPlane;
461 LogDebug(
"Geant4e") <<
"G4e - Distance from plane position to plane: " << pDest.
localZ(posPlane) <<
" cm";
464 template <
class SurfaceType>
467 CLHEP::Hep3Vector
const &g4InitPos,
469 CLHEP::Hep3Vector
const &g4InitMom,
470 const SurfaceType &pDest)
const {
471 LogDebug(
"Geant4e") <<
"G4e - Current Context: " << currentContext;
472 LogDebug(
"Geant4e") <<
"G4e - CMS point position:" << cmsInitPos <<
"cm\n"
473 <<
"G4e - (Ro, eta, phi): (" << cmsInitPos.
perp() <<
" cm, " << cmsInitPos.
eta()
474 <<
", " << cmsInitPos.
phi().
degrees() <<
" deg)\n"
475 <<
"G4e - G4 point position: " << g4InitPos <<
" mm, Ro = " << g4InitPos.perp() <<
" mm";
476 LogDebug(
"Geant4e") <<
"G4e - CMS momentum :" << cmsInitMom <<
"GeV\n"
477 <<
" pt: " << cmsInitMom.
perp() <<
"G4e - G4 momentum : " << g4InitMom <<
" MeV";