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 - 2023.
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 "BDSUtilities.hh"
28
29#include "globals.hh" // geant4 types / globals
30#include "G4TrackingManager.hh"
31#include "G4Track.hh"
32#include "G4VPhysicalVolume.hh"
33
34#include <set>
35
36class G4LogicalVolume;
37
39 G4bool storeTrajectoryIn,
40 const BDS::TrajectoryOptions& storeTrajectoryOptionsIn,
41 BDSEventAction* eventActionIn,
42 G4int verboseSteppingEventStartIn,
43 G4int verboseSteppingEventStopIn,
44 G4bool verboseSteppingPrimaryOnlyIn,
45 G4int verboseSteppingLevelIn):
46 interactive(!batchMode),
47 storeTrajectory(storeTrajectoryIn),
48 storeTrajectoryOptions(storeTrajectoryOptionsIn),
49 eventAction(eventActionIn),
50 verboseSteppingEventStart(verboseSteppingEventStartIn),
51 verboseSteppingEventStop(verboseSteppingEventStopIn),
52 verboseSteppingPrimaryOnly(verboseSteppingPrimaryOnlyIn),
53 verboseSteppingLevel(verboseSteppingLevelIn)
54{;}
55
57{
59 G4int eventIndex = eventAction->CurrentEventIndex();
60 G4bool verboseSteppingThisEvent = BDS::VerboseThisEvent(eventIndex, verboseSteppingEventStart, verboseSteppingEventStop);
61 G4bool primaryParticle = track->GetParentID() == 0;
63
64 if (primaryParticle && verboseSteppingThisEvent)
65 {fpTrackingManager->GetSteppingManager()->SetVerboseLevel(verboseSteppingLevel);}
66 else if (!primaryParticle && verboseSteppingThisEvent && !verboseSteppingPrimaryOnly)
67 {fpTrackingManager->GetSteppingManager()->SetVerboseLevel(verboseSteppingLevel);}
68
69 if (!primaryParticle)
70 {// ie secondary particle
71 // only store if we want to or interactive
72 if (storeTrajectory || interactive)
73 {
74 auto traj = new BDSTrajectory(track,
77 fpTrackingManager->SetStoreTrajectory(1);
78 fpTrackingManager->SetTrajectory(traj);
79 }
80 else // mark as don't store
81 {fpTrackingManager->SetStoreTrajectory(0);}
82 }
83 else
84 {// it's a primary particle
85 // if it's a primary track then we always store something
86 // but only store the actual trajectory points if we explicitly want
87 // trajectory points or we're using the visualiser.
88 G4bool storePoints = storeTrajectory || interactive;
89 auto traj = new BDSTrajectoryPrimary(track,
92 storePoints);
94 fpTrackingManager->SetStoreTrajectory(1);
95 fpTrackingManager->SetTrajectory(traj);
96 }
97}
98
100{
101 // turn off verbosity always as we selectively turn it on in the start tracking option
102 fpTrackingManager->GetSteppingManager()->SetVerboseLevel(0);
103
104#ifdef BDSDEBUG
105 G4int trackID = track->GetTrackID();
106 if (trackID < 100)
107 {// limit range of debug output
108 auto status = track->GetTrackStatus();
109 G4String name;
110 switch (status)
111 {
112 case G4TrackStatus::fAlive:
113 {name = "fAlive"; break;}
114 case G4TrackStatus::fStopButAlive:
115 {name = "fStopButAlive"; break;}
116 case G4TrackStatus::fKillTrackAndSecondaries:
117 {name = "fKillTrackAndSecondaries"; break;}
118 case G4TrackStatus::fStopAndKill:
119 {name = "fStopAndKill"; break;}
120 default:
121 {name = "other"; break;}
122 }
123 G4cout << "track ID " << trackID << " status " << name << G4endl;
124 }
125#endif
126 if (track->GetParentID() == 0)
127 {
128 G4LogicalVolume* lv = track->GetVolume()->GetLogicalVolume();
129 std::set<G4LogicalVolume*>* collimators = BDSAcceleratorModel::Instance()->VolumeSet("collimators");
130 if (collimators->find(lv) != collimators->end())
132 }
133}
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
const G4bool interactive
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.