BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSTrackingAction.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSAcceleratorModel.hh"
19#include "BDSDebug.hh"
20#include "BDSEventAction.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSPhysicsUtilities.hh"
23#include "BDSPolarizationState.hh"
24#include "BDSIntegratorMag.hh"
25#include "BDSTrackingAction.hh"
26#include "BDSTrajectory.hh"
27#include "BDSTrajectoryPrimary.hh"
28#include "BDSUserTrackInformation.hh"
29#include "BDSUtilities.hh"
30
31#include "globals.hh" // geant4 types / globals
32#include "G4TrackingManager.hh"
33#include "G4Track.hh"
34#include "G4VPhysicalVolume.hh"
35
36#include <set>
37
38class G4LogicalVolume;
39
40
42 G4bool storeTrajectoryIn,
43 const BDS::TrajectoryOptions& storeTrajectoryOptionsIn,
44 BDSEventAction* eventActionIn,
45 G4int verboseSteppingEventStartIn,
46 G4int verboseSteppingEventStopIn,
47 G4bool verboseSteppingPrimaryOnlyIn,
48 G4int verboseSteppingLevelIn,
49 BDSPolarizationState* defaultPolarisationIn):
50 interactive(!batchMode),
51 storeTrajectory(storeTrajectoryIn),
52 storeTrajectoryOptions(storeTrajectoryOptionsIn),
53 eventAction(eventActionIn),
54 verboseSteppingEventStart(verboseSteppingEventStartIn),
55 verboseSteppingEventStop(verboseSteppingEventStopIn),
56 verboseSteppingPrimaryOnly(verboseSteppingPrimaryOnlyIn),
57 verboseSteppingLevel(verboseSteppingLevelIn),
58 defaultPolarisation(defaultPolarisationIn)
59{;}
60
61BDSTrackingAction::~BDSTrackingAction()
62{
63 delete defaultPolarisation;
64}
65
67{
69 G4int eventIndex = eventAction->CurrentEventIndex();
70 G4bool verboseSteppingThisEvent = BDS::VerboseThisEvent(eventIndex, verboseSteppingEventStart, verboseSteppingEventStop);
71 G4bool primaryParticle = track->GetParentID() == 0;
73
74 if (primaryParticle && verboseSteppingThisEvent)
75 {fpTrackingManager->GetSteppingManager()->SetVerboseLevel(verboseSteppingLevel);}
76 else if (!primaryParticle && verboseSteppingThisEvent && !verboseSteppingPrimaryOnly)
77 {fpTrackingManager->GetSteppingManager()->SetVerboseLevel(verboseSteppingLevel);}
78
79 if (!primaryParticle)
80 {// ie secondary particle
81 // only store if we want to or interactive
82 if (storeTrajectory || interactive)
83 {
84 auto traj = new BDSTrajectory(track,
87 fpTrackingManager->SetStoreTrajectory(1);
88 fpTrackingManager->SetTrajectory(traj);
89 }
90 else // mark as don't store
91 {fpTrackingManager->SetStoreTrajectory(0);}
92 }
93 else
94 {// it's a primary particle
95 // if it's a primary track then we always store something
96 // but only store the actual trajectory points if we explicitly want
97 // trajectory points or we're using the visualiser.
98 G4bool storePoints = storeTrajectory || interactive;
99 auto traj = new BDSTrajectoryPrimary(track,
102 storePoints);
104 fpTrackingManager->SetStoreTrajectory(1);
105 fpTrackingManager->SetTrajectory(traj);
106 }
107
108 if (BDS::IsIon(track->GetParticleDefinition()))
109 {
110 auto* trackInfo = new BDSUserTrackInformation(track->GetDynamicParticle(), defaultPolarisation);
111 track->SetUserInformation(trackInfo);
112 }
113
114 const G4ParticleDefinition *particle = track->GetParticleDefinition();
115 G4ProcessManager* pmanager = particle->GetProcessManager();
116 G4VProcess* process = pmanager->GetProcess("laserCumulativeCompton");
117
118 if (process)
119 {
120 auto* trackInfo = new BDSUserTrackInformation(track->GetDynamicParticle(), defaultPolarisation);
121 track->SetUserInformation(trackInfo);
122 }
123}
124
126{
127 // turn off verbosity always as we selectively turn it on in the start tracking option
128 fpTrackingManager->GetSteppingManager()->SetVerboseLevel(0);
129
130#ifdef BDSDEBUG
131 G4int trackID = track->GetTrackID();
132 if (trackID < 100)
133 {// limit range of debug output
134 auto status = track->GetTrackStatus();
135 G4String name;
136 switch (status)
137 {
138 case G4TrackStatus::fAlive:
139 {name = "fAlive"; break;}
140 case G4TrackStatus::fStopButAlive:
141 {name = "fStopButAlive"; break;}
142 case G4TrackStatus::fKillTrackAndSecondaries:
143 {name = "fKillTrackAndSecondaries"; break;}
144 case G4TrackStatus::fStopAndKill:
145 {name = "fStopAndKill"; break;}
146 default:
147 {name = "other"; break;}
148 }
149 G4cout << "track ID " << trackID << " status " << name << G4endl;
150 }
151#endif
152 if (track->GetParentID() == 0)
153 {
154 G4LogicalVolume* lv = track->GetVolume()->GetLogicalVolume();
155 std::set<G4LogicalVolume*>* collimators = BDSAcceleratorModel::Instance()->VolumeSet("collimators");
156 if (collimators->find(lv) != collimators->end())
158 }
159}
std::set< G4LogicalVolume * > * VolumeSet(const G4String &name)
Returns pointer to a set of logical volumes. If no set by that name exits, create it.
Process information at the event level.
void IncrementNTracks()
Interface for tracking action to increment the number of tracks in each event.
void SetPrimaryAbsorbedInCollimator(G4bool stoppedIn)
Flag that the primary was absorbed in a collimator - can be done externally to this class.
void RegisterPrimaryTrajectory(const BDSTrajectoryPrimary *trajectoryIn)
Append this trajectory to vector of primaries we keep to avoid sifting at the end of event.
static G4bool currentTrackIsPrimary
Polarization state to pass to processes.
virtual void PreUserTrackingAction(const G4Track *track)
Used to decide whether or not to store trajectories.
virtual void PostUserTrackingAction(const G4Track *track)
Detect whether track is a primary and if so whether it ended in a collimator.
BDSTrackingAction()=delete
No default constructor required.
BDSEventAction * eventAction
Cache of trajectory options.
const BDS::TrajectoryOptions storeTrajectoryOptions
Cache of flag from global constants to control storing all trajectories.
Trajectory information for only the primary.
Trajectory information from track including last scatter etc.
G4bool VerboseThisEvent(G4int eventIndex, G4int eventStart, G4int eventStop)
Logic of whether this event should be verbose or not. Code here so it's not duplicated.
G4bool IsIon(const G4ParticleDefinition *particle)
Whether a particle is an ion. A proton is counted NOT as an ion.