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) Royal Holloway,
3University of London 2001 - 2024.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSAcceleratorModel.hh"
20#include "BDSDebug.hh"
21#include "BDSEventAction.hh"
22#include "BDSGlobalConstants.hh"
23#include "BDSIntegratorMag.hh"
24#include "BDSTrackingAction.hh"
25#include "BDSTrajectory.hh"
26#include "BDSTrajectoryPrimary.hh"
27#include "BDSUserTrackInformation.hh"
28#include "BDSUtilities.hh"
29
30#include "globals.hh" // geant4 types / globals
31#include "G4TrackingManager.hh"
32#include "G4Track.hh"
33#include "G4VPhysicalVolume.hh"
34
35#include <set>
36
37class G4LogicalVolume;
38
40 G4bool storeTrajectoryIn,
41 const BDS::TrajectoryOptions& storeTrajectoryOptionsIn,
42 BDSEventAction* eventActionIn,
43 G4int verboseSteppingEventStartIn,
44 G4int verboseSteppingEventStopIn,
45 G4bool verboseSteppingPrimaryOnlyIn,
46 G4int verboseSteppingLevelIn):
47 interactive(!batchMode),
48 storeTrajectory(storeTrajectoryIn),
49 storeTrajectoryOptions(storeTrajectoryOptionsIn),
50 eventAction(eventActionIn),
51 verboseSteppingEventStart(verboseSteppingEventStartIn),
52 verboseSteppingEventStop(verboseSteppingEventStopIn),
53 verboseSteppingPrimaryOnly(verboseSteppingPrimaryOnlyIn),
54 verboseSteppingLevel(verboseSteppingLevelIn)
55{;}
56
58{
60 G4int eventIndex = eventAction->CurrentEventIndex();
61 G4bool verboseSteppingThisEvent = BDS::VerboseThisEvent(eventIndex, verboseSteppingEventStart, verboseSteppingEventStop);
62 G4bool primaryParticle = track->GetParentID() == 0;
64
65 if (primaryParticle && verboseSteppingThisEvent)
66 {fpTrackingManager->GetSteppingManager()->SetVerboseLevel(verboseSteppingLevel);}
67 else if (!primaryParticle && verboseSteppingThisEvent && !verboseSteppingPrimaryOnly)
68 {fpTrackingManager->GetSteppingManager()->SetVerboseLevel(verboseSteppingLevel);}
69
70 if (!primaryParticle)
71 {// ie secondary particle
72 // only store if we want to or interactive
73 if (storeTrajectory || interactive)
74 {
75 auto traj = new BDSTrajectory(track,
78 fpTrackingManager->SetStoreTrajectory(1);
79 fpTrackingManager->SetTrajectory(traj);
80 }
81 else // mark as don't store
82 {fpTrackingManager->SetStoreTrajectory(0);}
83 }
84 else
85 {// it's a primary particle
86 // if it's a primary track then we always store something
87 // but only store the actual trajectory points if we explicitly want
88 // trajectory points or we're using the visualiser.
89 G4bool storePoints = storeTrajectory || interactive;
90 auto traj = new BDSTrajectoryPrimary(track,
93 storePoints);
95 fpTrackingManager->SetStoreTrajectory(1);
96 fpTrackingManager->SetTrajectory(traj);
97 }
98
99 //if ion add BDSElectronOccupancy
100
101
102 BDSUserTrackInformation* trackInfo = new BDSUserTrackInformation(track->GetDynamicParticle());
103 track->SetUserInformation(trackInfo);
104
105}
106
108{
109 // turn off verbosity always as we selectively turn it on in the start tracking option
110 fpTrackingManager->GetSteppingManager()->SetVerboseLevel(0);
111
112#ifdef BDSDEBUG
113 G4int trackID = track->GetTrackID();
114 if (trackID < 100)
115 {// limit range of debug output
116 auto status = track->GetTrackStatus();
117 G4String name;
118 switch (status)
119 {
120 case G4TrackStatus::fAlive:
121 {name = "fAlive"; break;}
122 case G4TrackStatus::fStopButAlive:
123 {name = "fStopButAlive"; break;}
124 case G4TrackStatus::fKillTrackAndSecondaries:
125 {name = "fKillTrackAndSecondaries"; break;}
126 case G4TrackStatus::fStopAndKill:
127 {name = "fStopAndKill"; break;}
128 default:
129 {name = "other"; break;}
130 }
131 G4cout << "track ID " << trackID << " status " << name << G4endl;
132 }
133#endif
134 if (track->GetParentID() == 0)
135 {
136 G4LogicalVolume* lv = track->GetVolume()->GetLogicalVolume();
137 std::set<G4LogicalVolume*>* collimators = BDSAcceleratorModel::Instance()->VolumeSet("collimators");
138 if (collimators->find(lv) != collimators->end())
140 }
141}
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
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.