BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSTrajectoryPoint.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 "BDSAuxiliaryNavigator.hh"
20#include "BDSDebug.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSPhysicalVolumeInfoRegistry.hh"
23#include "BDSPhysicalVolumeInfo.hh"
24#include "BDSStep.hh"
25#include "BDSTrajectoryPoint.hh"
26#include "BDSTrajectoryPointIon.hh"
27#include "BDSTrajectoryPointLocal.hh"
28#include "BDSTrajectoryPointLink.hh"
29#include "BDSPhysicalConstants.hh"
30#include "BDSPhysicsUtilities.hh"
31#include "BDSUtilities.hh"
32#ifdef BDSDEBUG_H
33#include "BDSProcessMap.hh"
34#endif
35
36#include "globals.hh"
37#include "G4Allocator.hh"
38#include "G4ProcessType.hh"
39#include "G4Step.hh"
40#include "G4ThreeVector.hh"
41#include "G4Track.hh"
42#include "G4TransportationProcessType.hh"
43#include "G4VProcess.hh"
44
45#include <ostream>
46
47class G4Material;
48
49G4Allocator<BDSTrajectoryPoint> bdsTrajectoryPointAllocator;
50
52
53// Don't use transform caching in the aux navigator as it's used for all over the geometry here.
55
57 G4TrajectoryPoint(G4ThreeVector())
58{
60}
61
63 G4bool storeExtrasLocal,
64 G4bool storeExtrasLink,
65 G4bool storeExtrasIon):
66 G4TrajectoryPoint(track->GetPosition())
67{
69
70 // Need to store the creator process
71 const G4VProcess* creatorProcess = track->GetCreatorProcess();
72 if (creatorProcess)
73 {
74 preProcessType = track->GetCreatorProcess()->GetProcessType();
75 preProcessSubType = track->GetCreatorProcess()->GetProcessSubType();
78 }
79
80 preWeight = track->GetWeight();
82 energyDeposit = 0.0; // does not lose any energy
83 preEnergy = track->GetKineticEnergy();
85 preMomentum = track->GetMomentum();
87 preGlobalTime = track->GetGlobalTime();
89 // when we construct a trajectory from a track it hasn't taken a step yet,
90 // so we don't know the material
91 material = nullptr;
92
93#ifdef BDSDEBUG
94 G4cout << __METHOD_NAME__ << "Process (main|sub) (" << BDSProcessMap::Instance()->GetProcessName(postProcessType, postProcessSubType) << ")" << G4endl;
95#endif
96
97 // s position for pre and post step point
98 // with a track, we're at the start and have no step - use 1nm for step to aid geometrical lookup
99 BDSStep localPosMom = auxNavigator->ConvertToLocal(track->GetPosition(),
100 track->GetMomentum(),
101 1*CLHEP::nm,
102 true);
103 prePosLocal = localPosMom.PreStepPoint();
106 if (info)
107 {
108 G4double sCentre = info->GetSPos();
109 preS = sCentre + localPosMom.PreStepPoint().z();
110 postS = preS; // no step length yet
113 }
114
115 if (storeExtrasLocal)
116 {extraLocal = new BDSTrajectoryPointLocal(prePosLocal, localPosMom.PostStepPoint());} // "post step point" is momentum
117
118 if (storeExtrasLink)
119 {StoreExtrasLink(track);}
120
121 if (storeExtrasIon)
122 {StoreExtrasIon(track);}
123}
124
126 G4bool storeExtrasLocal,
127 G4bool storeExtrasLink,
128 G4bool storeExtrasIon):
129 G4TrajectoryPoint(step->GetPostStepPoint()->GetPosition())
130{
132
133 const G4StepPoint* prePoint = step->GetPreStepPoint();
134 const G4StepPoint* postPoint = step->GetPostStepPoint();
135 const G4VProcess* preProcess = prePoint->GetProcessDefinedStep();
136 const G4VProcess* postProcess = postPoint->GetProcessDefinedStep();
137
138 if (preProcess)
139 {
140 preProcessType = preProcess->GetProcessType();
141 preProcessSubType = preProcess->GetProcessSubType();
142 }
143 if (postProcess)
144 {
145 postProcessType = postProcess->GetProcessType();
146 postProcessSubType = postProcess->GetProcessSubType();
147 }
148
149 preWeight = prePoint->GetWeight();
150 postWeight = postPoint->GetWeight();
151 energyDeposit = step->GetTotalEnergyDeposit();
152 preEnergy = prePoint->GetKineticEnergy();
153 postEnergy = postPoint->GetKineticEnergy();
154 preMomentum = prePoint->GetMomentum();
155 postMomentum = postPoint->GetMomentum();
156 preGlobalTime = prePoint->GetGlobalTime();
157 postGlobalTime = postPoint->GetGlobalTime();
158 material = prePoint->GetMaterial();
159
160#ifdef BDSDEBUG
161 G4cout << __METHOD_NAME__ << BDSProcessMap::Instance()->GetProcessName(postProcessType, postProcessSubType) << G4endl;
162#endif
163
164 // get local coordinates and volume for transform
165 BDSStep localPosition = auxNavigator->ConvertToLocal(step);
166 prePosLocal = localPosition.PreStepPoint();
167 postPosLocal = localPosition.PostStepPoint();
169 if (info)
170 {
171 G4double sCentre = info->GetSPos();
172 preS = sCentre + localPosition.PreStepPoint().z();
173 postS = sCentre + localPosition.PostStepPoint().z();
176 }
177
178 if (storeExtrasLocal)
179 {
180 G4ThreeVector postLocalMom = auxNavigator->ConvertAxisToLocal(postMomentum); // uses cached transform
181 extraLocal = new BDSTrajectoryPointLocal(prePosLocal, postLocalMom);
182 }
183
184 G4Track* track = step->GetTrack();
185 if (storeExtrasLink)
186 {StoreExtrasLink(track);}
187
188 if (storeExtrasIon)
189 {StoreExtrasIon(track);}
190}
191
193 G4TrajectoryPoint(static_cast<const G4TrajectoryPoint&>(other))
194{
195 extraLocal = other.extraLocal ? new BDSTrajectoryPointLocal(*other.extraLocal) : nullptr;
196 extraLink = other.extraLink ? new BDSTrajectoryPointLink(*other.extraLink) : nullptr;
197 extraIon = other.extraIon ? new BDSTrajectoryPointIon(*other.extraIon) : nullptr;
202
203 preWeight = other.preWeight;
204 postWeight = other.postWeight;
205 preEnergy = other.preEnergy;
206 postEnergy = other.postEnergy;
207 preMomentum = other.preMomentum;
210 preS = other.preS;
211 postS = other.postS;
215 beamline = other.beamline;
216 prePosLocal = other.prePosLocal;
218 material = other.material;
219}
220
221BDSTrajectoryPoint::~BDSTrajectoryPoint()
222{
223 delete extraLocal;
224 delete extraLink;
225 delete extraIon;
226}
227
229{
230 preProcessType = -1;
232 postProcessType = -1;
234 preWeight = -1.;
235 postWeight = -1.;
236 preEnergy = -1.;
237 postEnergy = -1.;
238 preMomentum = G4ThreeVector();
239 postMomentum = G4ThreeVector();
240 energyDeposit = 0.0;
241 preS = -1000;
242 postS = -1000;
243 preGlobalTime = 0;
244 postGlobalTime = 0;
245 beamlineIndex = -1;
246 beamline = nullptr;
247 prePosLocal = G4ThreeVector();
248 postPosLocal = G4ThreeVector();
249 material = nullptr;
250 extraLocal = nullptr;
251 extraLink = nullptr;
252 extraIon = nullptr;
253}
254
255void BDSTrajectoryPoint::StoreExtrasLink(const G4Track* track)
256{
257 const G4DynamicParticle* dynamicParticleDef = track->GetDynamicParticle();
258 G4double charge = dynamicParticleDef->GetCharge();
259 G4double rigidity = 0;
260 if (BDS::IsFinite(charge))
261 {rigidity = BDS::Rigidity(track->GetMomentum().mag(), charge);}
262 extraLink = new BDSTrajectoryPointLink((G4int)charge,
263 BDSGlobalConstants::Instance()->TurnsTaken(),
264 dynamicParticleDef->GetMass(),
265 rigidity);
266}
267
268void BDSTrajectoryPoint::StoreExtrasIon(const G4Track* track)
269{
270 const G4ParticleDefinition* particleDef = track->GetParticleDefinition();
271 const G4DynamicParticle* dynamicParticleDef = track->GetDynamicParticle();
272 G4bool isIon = BDS::IsIon(dynamicParticleDef);
273 G4int nElectrons = dynamicParticleDef->GetTotalOccupancy();
274 extraIon = new BDSTrajectoryPointIon(isIon,
275 particleDef->GetAtomicMass(),
276 particleDef->GetAtomicNumber(),
277 nElectrons);
278}
279
280
282{
283 // use general static function
287
288#ifdef BDSDEBUG
289 if (isScatteringPoint)
290 {
291 G4cout << "Interaction point found at " << GetPreS()/CLHEP::m << " m - "
293 }
294#endif
295 return isScatteringPoint;
296}
297
299{
300 // if prestep process doesn't exist it won't be set and will
301 // default to value in InitialiseVariables which is -1
302 G4bool preStep = (preProcessType != G4ProcessType::fTransportation && preProcessType != G4ProcessType::fParallel);
303 G4bool posStep = (postProcessType != G4ProcessType::fTransportation && postProcessType != G4ProcessType::fParallel);
304 return preStep || posStep;
305}
306
307std::ostream& operator<< (std::ostream& out, BDSTrajectoryPoint const &p)
308{
309 out << p.GetPosition();
310 return out;
311}
312
314{
315 return std::hypot(prePosLocal.x(), prePosLocal.y());
316}
317
319{
320 return std::hypot(postPosLocal.x(), postPosLocal.y());
321}
322
324{
325 const G4StepPoint* postPoint = step->GetPostStepPoint();
326 const G4VProcess* postProcess = postPoint->GetProcessDefinedStep();
327
328 G4double totalEnergyDeposit = step->GetTotalEnergyDeposit();
329
330 G4Track* t = step->GetTrack();
331 if (t->GetCurrentStepNumber() == 1 && t->GetStepLength() < 1e-5 && totalEnergyDeposit < 1e-5)
332 {return false;} // ignore the first really small step
333
334 G4int postProcessType = -1;
335 G4int postProcessSubType = -1;
336 if (postProcess)
337 {
338 postProcessType = postProcess->GetProcessType();
339 postProcessSubType = postProcess->GetProcessSubType();
340 }
341
343}
344
345G4bool BDSTrajectoryPoint::IsScatteringPoint(G4int postProcessType,
346 G4int postProcessSubType,
347 G4double totalEnergyDeposit)
348{
349 // test against things we want to exclude like tracking - these are not
350 // points of scattering
351 G4bool initialised = postProcessType != -1;
352 G4bool notTransportation = postProcessType != G4ProcessType::fTransportation;
353 G4bool notGeneral = (postProcessType != G4ProcessType::fGeneral) && (postProcessSubType != STEP_LIMITER);
354 G4bool notParallel = postProcessType != G4ProcessType::fParallel;
355 G4bool notUndefined = postProcessType != G4ProcessType::fNotDefined; // for crystal channelling
356
357 // energy can change in transportation step (EM)
358 if (totalEnergyDeposit > dEThresholdForScattering)
359 {return true;}
360
361 G4bool result = initialised && notTransportation && notGeneral && notParallel && notUndefined;
362 return result;
363}
Extra G4Navigator to get coordinate transforms.
G4ThreeVector ConvertAxisToLocal(const G4ThreeVector &globalAxis, const G4bool useCurvilinear=true) const
BDSStep ConvertToLocal(G4Step const *const step, G4bool useCurvilinear=true) const
static BDSGlobalConstants * Instance()
Access method.
BDSPhysicalVolumeInfo * GetInfo(G4VPhysicalVolume *logicalVolume, G4bool isTunnel=false)
static BDSPhysicalVolumeInfoRegistry * Instance()
Singleton accessor.
A class holding any information pertaining to a particular physical volume in a BDSIM geant4 model.
BDSBeamline * GetBeamlineMassWorld() const
Accessor.
G4double GetSPos() const
Get the s position coordinate of the logical volume.
G4int GetBeamlineMassWorldIndex() const
Accessor.
static BDSProcessMap * Instance()
Singleton accessor.
G4String GetProcessName(const G4int &type, const G4int &subType=-1) const
Despatched function to operator() for getting the name of processes.
A simple class to represent the positions of a step.
Definition BDSStep.hh:33
G4ThreeVector PostStepPoint() const
Accessor.
Definition BDSStep.hh:43
G4VPhysicalVolume * VolumeForTransform() const
Accessor.
Definition BDSStep.hh:44
G4ThreeVector PreStepPoint() const
Accessor.
Definition BDSStep.hh:42
Extra information recorded for a single piece of energy deposition.
Extra information recorded for a single piece of energy deposition.
A Point in a trajectory with extra information.
G4double preS
Global curvilinear S coordinate of pre-step point.
G4ThreeVector postMomentum
Momentum of post-step point.
BDSBeamline * beamline
Beam line (if any) point belongs to (always mass world).
void StoreExtrasIon(const G4Track *track)
Utility function to prepare and fill extra ion variables.
G4int beamlineIndex
Index to beam line element in the mass world beam line.
G4int GetPostProcessType() const
Accessor.
G4double PostPosR() const
Return the transverse local radius in x,y.
G4double postGlobalTime
Time since event started of post-step point.
G4double postEnergy
Kinetic energy of post step point.
G4double PrePosR() const
Return the transverse local radius in x,y.
void StoreExtrasLink(const G4Track *track)
Utility function to prepare and fill extra link variables.
static BDSAuxiliaryNavigator * auxNavigator
G4double GetPreS() const
Accessor.
G4Material * material
Material point for pre-step point.
BDSTrajectoryPoint()
Default constructor.
G4double postWeight
Weight associated with post step point.
G4ThreeVector prePosLocal
Local coordinates of pre-step point.
G4int postProcessSubType
Process sub type of post step point.
G4ThreeVector postPosLocal
Local coordinates of post-step point.
G4int GetPostProcessSubType() const
Accessor.
G4bool NotTransportationLimitedStep() const
Return true if step isn't defined by transportation processes.
G4int preProcessType
Process type of pre-step point.
G4double energyDeposit
Total energy deposited during step.
G4bool IsScatteringPoint() const
G4double preWeight
Weight associated with pre-step point.
G4ThreeVector preMomentum
Momentum of pre-step point.
G4int preProcessSubType
Process sub type of pre-step point.
G4double preGlobalTime
Time since event started of pre-step point.
G4double preEnergy
Kinetic energy of pre-step point.
G4double postS
Global curvilinear S coordinate of post step point.
G4int postProcessType
Process type of post step point.
static G4double dEThresholdForScattering
G4double Rigidity(G4double momentumMagnitude, G4double charge)
Calculate the rigidity for a total momentum and charge.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4bool IsIon(const G4ParticleDefinition *particle)
Whether a particle is an ion. A proton is counted NOT as an ion.