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