LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
G4BadIdeaAction.cxx
Go to the documentation of this file.
1 
9 
11 
12 #include "Geant4/G4Step.hh"
13 #include "Geant4/G4StepPoint.hh"
14 #include "Geant4/G4ThreeVector.hh"
15 #include "Geant4/G4Track.hh"
16 
17 namespace larg4 {
18 
19  //----------------------------------------------------------------------------
20  // Constructor.
21  G4BadIdeaAction::G4BadIdeaAction(int trkOption) : fNoIncomingMuons(trkOption)
22  {
23  // trkOption comes from LArG4's fSmartStacking
24  // Negative values effect action in this routine. Positive values
25  // effect action in LArStackingAction.
26  mf::LogWarning("G4BadIdeaAction") << "instantiating the G4BadIdeaAction \n"
27  << "This UserAction is only to be used with "
28  << "Geant4 v4.9.4.p02 to solve a stepping bug.\n"
29  << "If you are using a different version of G4, "
30  << "remove this UserAction from your list in LArG4.cxx";
31  }
32 
33  //----------------------------------------------------------------------------
34  // Destructor.
36 
37  //----------------------------------------------------------------------------
38  // With every step, add to the particle's trajectory.
39  void G4BadIdeaAction::SteppingAction(const G4Step* step)
40  {
45 
46  // If the step size is such that the particle appears to be "stuck"
47  // in its trajectory, give it a kick.
48  const double epsilon = 5000. * std::numeric_limits<double>::epsilon();
49  const double stepSize = step->GetStepLength();
50  G4Track* nonConstTrack = const_cast<G4Track*>(step->GetTrack());
51 
52  if (step->GetTrack()->GetCurrentStepNumber() > 5e4 && stepSize < epsilon &&
53  (step->GetTrack()->GetCurrentStepNumber() % 1000) == 1) {
54 
55  // Cast away the const-ness of the pointer to G4Step.
56  // This is dangerous. Don't do this at home. We're
57  // only doing this because we're desperate.
58  // The need to do this is the result of a bug in Geant4 v4.9.4.p02
59  // We should no longer call this code when we move beyond that version
60  const double kick = 0.001;
61 
62  G4ThreeVector aValue = nonConstTrack->GetPosition();
63 
64  mf::LogWarning("G4BadIdeaAction")
65  << "##### In endless loop. Kicking particle by "
66  << " (+0.001,+0.001,+0.001) --- "
67  << " PDG and encoding " << step->GetTrack()->GetDynamicParticle()->GetPDGcode() << " "
68  << step->GetTrack()->GetDynamicParticle()->GetParticleDefinition()->GetParticleName()
69  << " current step number: " << step->GetTrack()->GetCurrentStepNumber()
70  << " stepsize: " << stepSize << " x,y,z " << aValue.x() << " " << aValue.y() << " "
71  << aValue.z();
72 
73  G4ThreeVector translate(kick, kick, kick);
74  aValue += translate;
75 
76  nonConstTrack->SetPosition(aValue);
77  }
78 
79  if (fNoIncomingMuons < 0) {
80  // This is for overlays of, say, rock muons, which we have
81  G4StepPoint* thePrePoint = step->GetPreStepPoint();
82  G4VPhysicalVolume* thePrePV = thePrePoint->GetPhysicalVolume();
83  G4String thePrePVname = thePrePV->GetName();
84  G4StepPoint* thePostPoint = step->GetPostStepPoint();
85  G4VPhysicalVolume* thePostPV = thePostPoint->GetPhysicalVolume();
86  G4String thePostPVname("null");
87  if (thePostPV) thePostPVname = thePostPV->GetName();
88 
89  if (abs(step->GetTrack()->GetDynamicParticle()->GetPDGcode()) == 13 &&
90  thePostPVname.contains("volTPCActive") && !thePrePVname.contains("volTPCActive"))
91  ((G4Track*)nonConstTrack)->SetTrackStatus(fStopAndKill);
92  }
93 
94  return;
95  }
96 
97 } // namespace LArG4
constexpr auto abs(T v)
Returns the absolute value of the argument.
Geant4 interface.
this UserAction derived class is to implement catches to known bugs in Geant4 that require grabbing c...
std::string translate(errors::ErrorCodes)
Definition: Exception.cc:18
virtual void SteppingAction(const G4Step *)
G4UserSteppingAction interface.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning