Belle II Software development
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/EventAction.h>
11#include <simulation/kernel/RunManager.h>
12#include <simulation/kernel/UserInfo.h>
13#include <framework/core/Environment.h>
14#include <framework/logging/Logger.h>
15#include <framework/gearbox/Unit.h>
16
17#include <G4Track.hh>
18#include <G4UnitsTable.hh>
19#include <G4VProcess.hh>
20#include <G4VProcess.hh>
21
22#include <string>
23
24using namespace Belle2;
25using namespace Simulation;
26
27SteppingAction::SteppingAction()
28{
29 //Default value for the maximum number of steps
30 m_maxNumberSteps = 100000;
31 if (false) {
32 G4Step* aStep;
33 UserSteppingAction(aStep);
34 }
35 m_writeSimSteps = Environment::Instance().getWriteSimSteps();
36}
37
38
39SteppingAction::~SteppingAction()
40{
41
42}
43
44
45void SteppingAction::UserSteppingAction(const G4Step* step)
46{
47 G4Track* track = step->GetTrack();
48
49 //------------------------------
50 // Check for NULL world volume
51 //------------------------------
52 if (track->GetVolume() == NULL) {
53 B2WARNING("SteppingAction: Track in NULL volume, terminating!\n"
54 << "step_no=" << track->GetCurrentStepNumber() << " type=" << track->GetDefinition()->GetParticleName()
55 << "\n position=" << G4BestUnit(track->GetPosition(), "Length") << " momentum=" << G4BestUnit(track->GetMomentum(), "Energy"));
56 track->SetTrackStatus(fStopAndKill);
57 return;
58 }
59
60 //------------------------------
61 // Check for absorbers
62 //------------------------------
63 for (auto& rAbsorber : m_absorbers) {
64 const G4ThreeVector stepPrePos = step->GetPreStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
65 const G4ThreeVector stepPostPos = step->GetPostStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
66 if (stepPrePos.perp() < (rAbsorber * Unit::cm) && stepPostPos.perp() > (rAbsorber * Unit::cm)) {
67 //B2WARNING("SteppingAction: Track across absorbers, terminating!\n"
68 //<< "step_no=" << track->GetCurrentStepNumber() << " type=" << track->GetDefinition()->GetParticleName()
69 //<< "\n position=" << G4BestUnit(track->GetPosition(), "Length") << " momentum=" << G4BestUnit(track->GetMomentum(), "Energy") << "\n PrePos.perp=" << stepPrePos.perp() << ", PostPos.perp=" << stepPostPos.perp() << " cm" );
70 track->SetTrackStatus(fStopAndKill);
71 return;
72 }
73 }
74
75 // If we are running this job for producing virtual reality events, let's run the relevant method
76 if (m_writeSimSteps) {
77 writeVREventStep(step, track);
78 }
79
80 //---------------------------------------
81 // Check for very high number of steps.
82 //---------------------------------------
83 if (track->GetCurrentStepNumber() > m_maxNumberSteps) {
84 B2WARNING("SteppingAction: Too many steps for this track, terminating!\n"
85 << "step_no=" << track->GetCurrentStepNumber() << "type=" << track->GetDefinition()->GetParticleName()
86 << "\n position=" << G4BestUnit(track->GetPosition(), "Length") << " momentum=" << G4BestUnit(track->GetMomentum(), "Energy"));
87 track->SetTrackStatus(fStopAndKill);
88 return;
89 }
90
91 //-----------------------------------------------------------
92 // Check if there is an attached trajectory. If so, fill it.
93 //-----------------------------------------------------------
94 if (m_storeTrajectories) {
95 TrackInfo* info = dynamic_cast<TrackInfo*>(track->GetUserInformation());
96 if (info && info->getTrajectory()) {
97 MCParticleTrajectory& trajectory = *(info->getTrajectory());
98 if (trajectory.empty()) {
99 const G4ThreeVector stepPos = step->GetPreStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
100 const G4ThreeVector stepMom = step->GetPreStepPoint()->GetMomentum() / CLHEP::MeV * Unit::MeV;
101 trajectory.addPoint(
102 stepPos.x(), stepPos.y(), stepPos.z(),
103 stepMom.x(), stepMom.y(), stepMom.z()
104 );
105 }
106 const G4ThreeVector stepPos = step->GetPostStepPoint()->GetPosition() / CLHEP::mm * Unit::mm;
107 const G4ThreeVector stepMom = step->GetPostStepPoint()->GetMomentum() / CLHEP::MeV * Unit::MeV;
108 trajectory.addPoint(
109 stepPos.x(), stepPos.y(), stepPos.z(),
110 stepMom.x(), stepMom.y(), stepMom.z()
111 );
112 }
113 }
114}
115
116// Write (almost) each step to the VR event file
117void SteppingAction::writeVREventStep(const G4Step* step, const G4Track* track)
118{
119 // There must be an open output file (opened in simulation's EventAction)
120 RunManager& runManager = RunManager::Instance();
121 const EventAction* eventAction = (const Belle2::Simulation::EventAction*)runManager.GetUserEventAction();
122 if (eventAction == nullptr)
123 return;
124 std::ofstream* output = eventAction->getVREventStream();
125 if (output == nullptr)
126 return;
127 if (!output->is_open())
128 return;
129 // Ignore (hyper)nuclei heavier than alphas
130 if (abs(track->GetDefinition()->GetPDGEncoding()) > 1000020040)
131 return;
132 // Limit the VR event history to 100 ns
133 G4StepPoint* postStepPoint = step->GetPostStepPoint();
134 if (postStepPoint->GetGlobalTime() > 100.0)
135 return;
136 // Discard soft particles (KE < 500 keV) unless they are optical photons (for which KE=0)
137 G4StepPoint* preStepPoint = step->GetPreStepPoint();
138 double KE = preStepPoint->GetTotalEnergy() - track->GetDefinition()->GetPDGMass();
139 if ((KE < 0.0005) && (track->GetDefinition()->GetParticleName() != "opticalphoton"))
140 return;
141
142 // We will definitely write one record to the VR event file
143 G4String pVolName = track->GetVolume()->GetName();
144 G4String sensitiveDetectorName = "";
145 if (pVolName.compare(0, 4, "PXD.") == 0) {
146 if (pVolName.find(".Active") != std::string::npos) { sensitiveDetectorName = "PXD"; }
147 } else if (pVolName.compare(0, 4, "SVD.") == 0) {
148 if (pVolName.find(".Active") != std::string::npos) { sensitiveDetectorName = "SVD"; }
149 } else if (pVolName.compare(0, 20, "physicalSD_CDCLayer_") == 0) {
150 sensitiveDetectorName = "CDC";
151 } else if (pVolName.compare(0, 19, "TOP.moduleSensitive") == 0) {
152 sensitiveDetectorName = "TOP";
153 } else if (pVolName.compare(0, 23, "av_1_impr_1_cuttest_pv_") == 0) {
154 sensitiveDetectorName = "TOP";
155 } else if (pVolName.compare(0, 12, "moduleWindow") == 0) {
156 sensitiveDetectorName = "ARICH";
157 } else if (pVolName.compare(0, 25, "ARICH.AerogelSupportPlate") == 0) {
158 sensitiveDetectorName = "ARICH";
159 } else if (pVolName.compare(0, 25, "eclBarrelCrystalPhysical_") == 0) {
160 sensitiveDetectorName = "ECL";
161 } else if (pVolName.compare(0, 22, "eclFwdCrystalPhysical_") == 0) {
162 sensitiveDetectorName = "ECL";
163 } else if (pVolName.compare(0, 22, "eclBwdCrystalPhysical_") == 0) {
164 sensitiveDetectorName = "ECL";
165 } else if (pVolName.compare(0, 20, "BKLM.ScintActiveType") == 0) {
166 sensitiveDetectorName = "BKLM";
167 } else if (pVolName.compare(0, 10, "BKLM.Layer") == 0) {
168 if (pVolName.find("GasPhysical") != std::string::npos) {
169 sensitiveDetectorName = "BKLM";
170 }
171 } else if (pVolName.compare(0, 15, "StripSensitive_") == 0) {
172 sensitiveDetectorName = "EKLM";
173 }
174// Content of each record:
175// TrackID,ParentID,ParticleName,Mass,Charge,StepNumber,Status,VolumeName,
176// MaterialName,IsFirstStepInVolume,IsLastStepInVolume,EnergyDeposit,
177// ProcessType,ProcessName,PrePointX,PrePointY,PrePointZ,PrePointT,
178// PrePointPX,PrePointPY,PrePointPZ,PrePointE,PostPointX,PostPointY,
179// PostPointZ,PostPointT,PostPointPX,PostPointPY,PostPointPZ,PostPointE
180 (*output) << std::fixed << std::setprecision(4)
181 << track->GetTrackID() << ","
182 << track->GetParentID() << ","
183 << track->GetDefinition()->GetParticleName() << ","
184 << track->GetDefinition()->GetPDGMass() << ","
185 << int(track->GetDefinition()->GetPDGCharge()) << ","
186 << track->GetCurrentStepNumber() << ","
187 << track->GetTrackStatus() << ","
188 << pVolName << ","
189 << sensitiveDetectorName << ","
190 << track->GetMaterial()->GetName() << ","
191 << step->IsFirstStepInVolume() << ","
192 << step->IsLastStepInVolume() << ","
193 << step->GetTotalEnergyDeposit() << ","
194 << postStepPoint->GetProcessDefinedStep()->GetProcessType() << ","
195 << postStepPoint->GetProcessDefinedStep()->GetProcessName() << ","
196 << preStepPoint->GetPosition().x() << ","
197 << preStepPoint->GetPosition().y() << ","
198 << preStepPoint->GetPosition().z() << ","
199 << preStepPoint->GetGlobalTime() << ","
200 << preStepPoint->GetMomentum().x() << ","
201 << preStepPoint->GetMomentum().y() << ","
202 << preStepPoint->GetMomentum().z() << ","
203 << preStepPoint->GetTotalEnergy() << ","
204 << postStepPoint->GetPosition().x() << ","
205 << postStepPoint->GetPosition().y() << ","
206 << postStepPoint->GetPosition().z() << ","
207 << postStepPoint->GetGlobalTime() << ","
208 << postStepPoint->GetMomentum().x() << ","
209 << postStepPoint->GetMomentum().y() << ","
210 << postStepPoint->GetMomentum().z() << ","
211 << postStepPoint->GetTotalEnergy() << std::endl;
212}
bool getWriteSimSteps() const
Get the flag for writing the simulation steps into an output csv file.
Definition: Environment.h:219
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
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
The Event Action class.
Definition: EventAction.h:36
std::ofstream * getVREventStream() const
This method gets the output stream for the event-history steps.
Definition: EventAction.h:68
The run manager controls the flow of the Geant4 program and manages the event loop(s) within a run.
Definition: RunManager.h:32
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.