BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSLinkEventAction.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 "BDSAuxiliaryNavigator.hh"
19#include "BDSDebug.hh"
20#include "BDSGlobalConstants.hh"
21#include "BDSHitSamplerLink.hh"
22#include "BDSLinkEventAction.hh"
23#include "BDSLinkEventInfo.hh"
24#include "BDSLinkRunAction.hh"
25#include "BDSOutput.hh"
26#include "BDSSDSampler.hh"
27#include "BDSSDSamplerLink.hh"
28#include "BDSSDManager.hh"
29#include "BDSUtilities.hh"
30
31#include "globals.hh"
32#include "G4Event.hh"
33#include "G4EventManager.hh"
34#include "G4HCofThisEvent.hh"
35#include "G4PropagatorInField.hh"
36#include "G4Run.hh"
37#include "G4SDManager.hh"
38#include "G4THitsMap.hh"
39#include "G4TransportationManager.hh"
40#include "G4VUserEventInformation.hh"
41
42#include <map>
43
44
45BDSLinkEventAction::BDSLinkEventAction(BDSOutput* outputIn,
46 BDSLinkRunAction* runActionIn,
47 G4bool debugIn):
48 output(outputIn),
49 runAction(runActionIn),
50 debug(debugIn),
51 collIDSamplerLink(-1),
52 collIDSampler(-1),
53 currentEventIndex(0),
54 primaryAbsorbedInCollimator(false)
55{
57 verboseEventBDSIM = globals->VerboseEventBDSIM();
58 verboseEventStart = globals->VerboseEventStart();
59 verboseEventStop = BDS::VerboseEventStop(verboseEventStart, globals->VerboseEventContinueFor());
60 printModulo = globals->PrintModuloEvents();
61}
62
63BDSLinkEventAction::~BDSLinkEventAction()
64{;}
65
66void BDSLinkEventAction::BeginOfEventAction(const G4Event* evt)
67{
68 currentEventIndex = evt->GetEventID();
69 primaryAbsorbedInCollimator = false;
70
71 // reset navigators to ensure no mis-navigating and that events are truly independent
72 BDSAuxiliaryNavigator::ResetNavigatorStates();
73 // make further attempts to clear Geant4's tracking history between events to make them
74 // truly independent.
75 G4Navigator* trackingNavigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
76 trackingNavigator->ResetStackAndState();
77 G4TransportationManager* tm = G4TransportationManager::GetTransportationManager();
78 int i = 0;
79 for (auto it = tm->GetActiveNavigatorsIterator(); i < (int)tm->GetNoActiveNavigators(); it++)
80 {(*it)->ResetStackAndState(); i++;}
81 tm->GetPropagatorInField()->ClearPropagatorState(); // <- this one really makes a difference
82
83 // number feedback
84 if (debug)
85 {
86 if (currentEventIndex % printModulo == 0)
87 {G4cout << "---> Begin of event: " << currentEventIndex << G4endl;}
88 }
89 if (verboseEventBDSIM) // always print this out
90 {G4cout << __METHOD_NAME__ << "event #" << currentEventIndex << G4endl;}
91
92 // cache hit collection IDs for quicker access
93 if (collIDSamplerLink < 0)
94 {// if one is -1 then all need initialised.
95 G4SDManager* g4SDMan = G4SDManager::GetSDMpointer();
96 BDSSDManager* bdsSDMan = BDSSDManager::Instance();
97 collIDSamplerLink = g4SDMan->GetCollectionID(bdsSDMan->SamplerLink()->GetName());
98 collIDSampler = g4SDMan->GetCollectionID(bdsSDMan->SamplerPlane()->GetName());
99 }
100}
101
102void BDSLinkEventAction::EndOfEventAction(const G4Event* evt)
103{
104 // Get the hits collection of this event - all hits from different SDs.
105 G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
106 typedef BDSHitsCollectionSamplerLink slhc;
107 slhc* samplerLink = HCE ? dynamic_cast<slhc*>(HCE->GetHC(collIDSamplerLink)) : nullptr;
108 typedef BDSHitsCollectionSampler shc;
109 shc* sampHC = HCE ? dynamic_cast<shc*>(HCE->GetHC(collIDSampler)) : nullptr;
110 std::vector<shc*> allSamplerHits = {sampHC};
111
112 G4VUserEventInformation* evtInfoG4 = evt->GetUserInformation();
113 BDSLinkEventInfo* evtInfo = dynamic_cast<BDSLinkEventInfo*>(evtInfoG4);
114 G4int primaryExternalParticleID = 0;
115 G4int primaryExternalParentID = 0;
116 if (evtInfo)
117 {
118 primaryExternalParticleID = evtInfo->externalParticleIDofPrimary;
119 primaryExternalParentID = evtInfo->externalParentIDofPrimary;
120 }
121
122 if (!samplerLink)
123 {return;}
124 if (samplerLink->entries() <= 0)
125 {return;}
126 else
127 {runAction->AppendHits(currentEventIndex, primaryExternalParticleID, primaryExternalParentID, samplerLink);}
128
129 output->FillEvent(nullptr,
130 evt->GetPrimaryVertex(),
131 allSamplerHits,
132 std::vector<BDSHitsCollectionSamplerCylinder*>(),
133 std::vector<BDSHitsCollectionSamplerSphere*>(),
134 samplerLink,
135 nullptr,
136 nullptr,
137 nullptr,
138 nullptr,
139 nullptr,
140 nullptr,
141 nullptr,
142 std::vector<const BDSTrajectoryPointHit*>(),
143 std::vector<const BDSTrajectoryPointHit*>(),
144 nullptr,
145 nullptr,
146 nullptr,
147 std::map<G4String, G4THitsMap<G4double>*>(),
148 BDSGlobalConstants::Instance()->TurnsTaken());
149}
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.
BDSOutput * output
Cache of output instance. Not owned by this class.
Simple extension to cache extra variables through an event.
Simplified run action to hold link hits.
Output base class that defines interface for all output types.
Definition BDSOutput.hh:72
void FillEvent(const BDSEventInfo *info, const G4PrimaryVertex *vertex, const std::vector< BDSHitsCollectionSampler * > &samplerHitsPlane, const std::vector< BDSHitsCollectionSamplerCylinder * > &samplerHitsCylinder, const std::vector< BDSHitsCollectionSamplerSphere * > &samplerHitsSphere, const BDSHitsCollectionSamplerLink *samplerHitsLink, const BDSHitsCollectionEnergyDeposition *energyLoss, const BDSHitsCollectionEnergyDeposition *energyLossFull, const BDSHitsCollectionEnergyDeposition *energyLossVacuum, const BDSHitsCollectionEnergyDeposition *energyLossTunnel, const BDSHitsCollectionEnergyDepositionGlobal *energyLossWorld, const BDSHitsCollectionEnergyDepositionGlobal *energyLossWorldContents, const BDSHitsCollectionEnergyDepositionGlobal *worldExitHits, const std::vector< const BDSTrajectoryPointHit * > &primaryHits, const std::vector< const BDSTrajectoryPointHit * > &primaryLosses, const BDSTrajectoriesToStore *trajectories, const BDSHitsCollectionCollimator *collimatorHits, const BDSHitsCollectionApertureImpacts *apertureImpactHits, const std::map< G4String, G4THitsMap< G4double > * > &scorerHitsMap, const G4int turnsTaken)
Copy event information from Geant4 simulation structures to output structures.
Definition BDSOutput.cc:298
A singleton class that holds all required sensitive detector class instances.
BDSSDSamplerLink * SamplerLink() const
SD for link samplers.
BDSSDSampler * SamplerPlane() const
SD for samplers (plane type). See also SamplerPlaneWithFilter below.
G4int VerboseEventStop(G4int verboseEventStart, G4int verboseEventContinueFor)