BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSSDEnergyDepositionGlobal.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSHitEnergyDepositionGlobal.hh"
20#include "BDSSDEnergyDepositionGlobal.hh"
21#include "BDSDebug.hh"
22#include "BDSGlobalConstants.hh"
23#include "BDSUtilities.hh"
24
25#include "globals.hh" // geant4 types / globals
26#include "G4Event.hh"
27#include "G4LogicalVolume.hh"
28#include "G4ParticleDefinition.hh"
29#include "G4SDManager.hh"
30#include "G4Step.hh"
31#include "G4ThreeVector.hh"
32#include "G4Track.hh"
33#include "Randomize.hh"
34
35BDSSDEnergyDepositionGlobal::BDSSDEnergyDepositionGlobal(const G4String& name,
36 G4bool killedParticleMassAddedToElossIn):
37 BDSSensitiveDetector("energy_deposiiton_global/"+name),
38 killedParticleMassAddedToEloss(killedParticleMassAddedToElossIn),
39 colName(name),
40 hits(nullptr),
41 HCIDe(-1),
42 energy(0),
43 preStepKineticEnergy(0),
44 postStepKineticEnergy(0),
45 stepLength(0),
46 weight(0),
47 X(0),
48 Y(0),
49 Z(0),
50 globalTime(0),
51 pdgID(0),
52 trackID(-1),
53 parentID(-1),
54 turnsTaken(0)
55{
56 collectionName.insert(colName);
57}
58
59BDSSDEnergyDepositionGlobal::~BDSSDEnergyDepositionGlobal()
60{;}
61
62void BDSSDEnergyDepositionGlobal::Initialize(G4HCofThisEvent* HCE)
63{
65 if (HCIDe < 0)
66 {HCIDe = G4SDManager::GetSDMpointer()->GetCollectionID(hits);}
67 HCE->AddHitsCollection(HCIDe, hits);
68
69#ifdef BDSDEBUG
70 G4cout << __METHOD_NAME__ << "Hits Collection ID: " << HCIDe << G4endl;
71#endif
72}
73
75 G4TouchableHistory* /*th*/)
76{
77 // Get the energy deposited along the step
78 energy = aStep->GetTotalEnergyDeposit();
79
80 //if the energy is 0, don't do anything
82 {return false;}
83
84 // step points - used many times
85 G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
86 G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
87
88 G4Track* track = aStep->GetTrack();
89 preStepKineticEnergy = preStepPoint->GetKineticEnergy();
90 postStepKineticEnergy = postStepPoint->GetKineticEnergy();
91 stepLength = aStep->GetStepLength();
92 weight = track->GetWeight();
93
94 // attribute the energy deposition to a uniformly random position along the step - correct!
95 // random distance - store to use twice to ensure global and local represent the same point
96 G4double randDist = G4UniformRand();
97
98 // global coordinate positions of the step
99 G4ThreeVector posbefore = preStepPoint->GetPosition();
100 G4ThreeVector posafter = postStepPoint->GetPosition();
101 G4ThreeVector eDepPos = posbefore + randDist*(posafter - posbefore);
102
103 // global
104 X = eDepPos.x();
105 Y = eDepPos.y();
106 Z = eDepPos.z();
107
108 // Just as the energy deposition is attributed to a uniformly random
109 // point between the preStep and the postStep positions, attribute the
110 // deposition to random time between preStep and postStep times,
111 // using the same random number as for the position.
112 G4double preGlobalTime = preStepPoint->GetGlobalTime();
113 G4double postGlobalTime = postStepPoint->GetGlobalTime();
114 globalTime = preGlobalTime + randDist * (postGlobalTime - preGlobalTime);
115
116 pdgID = track->GetDefinition()->GetPDGEncoding();
117 trackID = track->GetTrackID();
118 parentID = track->GetParentID(); // needed later on too
120
121 //create hits and put in hits collection of the event
126 X, Y, Z,
128 pdgID,
129 trackID,
130 parentID,
131 weight,
132 turnsTaken);
133
134 // don't worry, won't add 0 energy tracks as filtered at top by if statement
135 hits->insert(hit);
136
137 return true;
138}
139
141 G4TouchableHistory* /*th*/)
142{
143 energy = killedParticleMassAddedToEloss ? track->GetTotalEnergy() : track->GetKineticEnergy();
144 // if the energy is 0, don't do anything
145 if (!BDS::IsFinite(energy))
146 {return false;}
147
148 preStepKineticEnergy = track->GetKineticEnergy();
150 stepLength = 0;
151
152 const G4ThreeVector& posGlobal = track->GetPosition();
153 X = posGlobal.x();
154 Y = posGlobal.y();
155 Z = posGlobal.z();
156
157 globalTime = track->GetGlobalTime();
158 pdgID = track->GetDefinition()->GetPDGEncoding();
159 trackID = track->GetTrackID();
160 parentID = track->GetParentID();
161 weight = track->GetWeight();
163
168 X, Y, Z,
170 pdgID,
171 trackID,
172 parentID,
173 weight,
174 turnsTaken);
175 hits->insert(hit);
176
177 return true;
178}
179
181{
182 auto hitsVector = hits->GetVector();
183 if (hitsVector->empty())
184 {return nullptr;}
185 else
186 {
187 BDSHitEnergyDepositionGlobal* lastHit = hitsVector->back();
188 return static_cast<G4VHit*>(lastHit);
189 }
190}
static BDSGlobalConstants * Instance()
Access method.
Information recorded for a step leaving a volume.
G4double globalTime
Per hit variable.
G4double stepLength
Per hit variable.
G4double postStepKineticEnergy
Per hit variable.
virtual G4VHit * last() const
Provide access to last hit.
virtual G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *th)
virtual G4bool ProcessHitsTrack(const G4Track *track, G4TouchableHistory *th)
G4double preStepKineticEnergy
Per hit variable.
Virtual class to define interface for ordered multi-sensitive detector.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
const char * GetName(int)
Name of element.
Definition: python.cc:38