Belle II Software  release-08-01-10
SteppingAction.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <simulation/kernel/SteppingAction.h>
10 #include <simulation/kernel/UserInfo.h>
11 #include <framework/logging/Logger.h>
12 #include <framework/gearbox/Unit.h>
13 
14 #include <G4UnitsTable.hh>
15 #include <G4Track.hh>
16 
17 
18 using namespace Belle2;
19 using namespace Simulation;
20 
22 {
23  //Default value for the maximum number of steps
24  m_maxNumberSteps = 100000;
25  if (false) {
26  G4Step* aStep;
27  UserSteppingAction(aStep);
28  }
29 }
30 
31 
33 {
34 
35 }
36 
37 
38 void SteppingAction::UserSteppingAction(const G4Step* step)
39 {
40  G4Track* track = step->GetTrack();
41 
42  //------------------------------
43  // Check for NULL world volume
44  //------------------------------
45  if (track->GetVolume() == NULL) {
46  B2WARNING("SteppingAction: Track in NULL volume, terminating!\n"
47  << "step_no=" << track->GetCurrentStepNumber() << " type=" << track->GetDefinition()->GetParticleName()
48  << "\n position=" << G4BestUnit(track->GetPosition(), "Length") << " momentum=" << G4BestUnit(track->GetMomentum(), "Energy"));
49  track->SetTrackStatus(fStopAndKill);
50  return;
51  }
52 
53  //------------------------------
54  // Check for absorbers
55  //------------------------------
56  for (auto& rAbsorber : m_absorbers) {
57  const G4ThreeVector stepPrePos = step->GetPreStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
58  const G4ThreeVector stepPostPos = step->GetPostStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
59  if (stepPrePos.perp() < (rAbsorber * Unit::cm) && stepPostPos.perp() > (rAbsorber * Unit::cm)) {
60  //B2WARNING("SteppingAction: Track across absorbers, terminating!\n"
61  //<< "step_no=" << track->GetCurrentStepNumber() << " type=" << track->GetDefinition()->GetParticleName()
62  //<< "\n position=" << G4BestUnit(track->GetPosition(), "Length") << " momentum=" << G4BestUnit(track->GetMomentum(), "Energy") << "\n PrePos.perp=" << stepPrePos.perp() << ", PostPos.perp=" << stepPostPos.perp() << " cm" );
63  track->SetTrackStatus(fStopAndKill);
64  return;
65  }
66  }
67 
68  //---------------------------------------
69  // Check for very high number of steps.
70  //---------------------------------------
71  if (track->GetCurrentStepNumber() > m_maxNumberSteps) {
72  B2WARNING("SteppingAction: Too many steps for this track, terminating!\n"
73  << "step_no=" << track->GetCurrentStepNumber() << "type=" << track->GetDefinition()->GetParticleName()
74  << "\n position=" << G4BestUnit(track->GetPosition(), "Length") << " momentum=" << G4BestUnit(track->GetMomentum(), "Energy"));
75  track->SetTrackStatus(fStopAndKill);
76  return;
77  }
78 
79  //-----------------------------------------------------------
80  // Check if there is an attached trajectory. If so, fill it.
81  //-----------------------------------------------------------
82  if (m_storeTrajectories) {
83  TrackInfo* info = dynamic_cast<TrackInfo*>(track->GetUserInformation());
84  if (info && info->getTrajectory()) {
85  MCParticleTrajectory& trajectory = *(info->getTrajectory());
86  if (trajectory.empty()) {
87  const G4ThreeVector stepPos = step->GetPreStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
88  const G4ThreeVector stepMom = step->GetPreStepPoint()->GetMomentum() / CLHEP::MeV * Unit::MeV;
89  trajectory.addPoint(
90  stepPos.x(), stepPos.y(), stepPos.z(),
91  stepMom.x(), stepMom.y(), stepMom.z()
92  );
93  }
94  const G4ThreeVector stepPos = step->GetPostStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
95  const G4ThreeVector stepMom = step->GetPostStepPoint()->GetMomentum() / CLHEP::MeV * Unit::MeV;
96  trajectory.addPoint(
97  stepPos.x(), stepPos.y(), stepPos.z(),
98  stepMom.x(), stepMom.y(), stepMom.z()
99  );
100  }
101  }
102 }
Class to save the full simulated trajectory of a particle.
void addPoint(float x, float y, float z, float px, float py, float pz)
Add a point to the trajectory.
bool empty() const
return true if size()==0
std::vector< float > m_absorbers
The absorbers defined at given radii where tracks across them will be destroyed.
virtual void UserSteppingAction(const G4Step *step)
The method will be called at each step during simulation.
virtual ~SteppingAction()
Destructor.
int m_maxNumberSteps
The maximum number of steps before the track transportation is stopped and the track is killed.
bool m_storeTrajectories
if true, check if the track has attached trajectory info and append step information if necessary
UserInfo class which is used to attach additional information to Geant4 particles and tracks.
Definition: UserInfo.h:36
static const double mm
[millimeters]
Definition: Unit.h:70
static const double MeV
[megaelectronvolt]
Definition: Unit.h:114
static const double cm
Standard units with the value = 1.
Definition: Unit.h:47
Abstract base class for different kinds of events.