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) 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 "BDSPhysicalVolumeInfoRegistry.hh"
22#include "BDSPhysicalVolumeInfo.hh"
23#include "BDSStep.hh"
24#include "BDSTrajectoryPoint.hh"
25#include "BDSTrajectoryPointIon.hh"
26#include "BDSTrajectoryPointLocal.hh"
27#include "BDSTrajectoryPointLink.hh"
28#include "BDSPhysicalConstants.hh"
29#include "BDSPhysicsUtilities.hh"
30#include "BDSUtilities.hh"
31#ifdef BDSDEBUG_H
32#include "BDSProcessMap.hh"
33#endif
34
35#include "globals.hh"
36#include "G4Allocator.hh"
37#include "G4ProcessType.hh"
38#include "G4Step.hh"
39#include "G4ThreeVector.hh"
40#include "G4Track.hh"
41#include "G4TransportationProcessType.hh"
42#include "G4VProcess.hh"
43
44#include <ostream>
45
46class G4Material;
47
48G4Allocator<BDSTrajectoryPoint> bdsTrajectoryPointAllocator;
49
51
52// Don't use transform caching in the aux navigator as it's used for all over the geometry here.
54
56 G4TrajectoryPoint(G4ThreeVector())
57{
59}
60
62 G4bool storeExtrasLocal,
63 G4bool storeExtrasLink,
64 G4bool storeExtrasIon):
65 G4TrajectoryPoint(track->GetPosition())
66{
68
69 // Need to store the creator process
70 const G4VProcess* creatorProcess = track->GetCreatorProcess();
71 if (creatorProcess)
72 {
73 preProcessType = track->GetCreatorProcess()->GetProcessType();
74 preProcessSubType = track->GetCreatorProcess()->GetProcessSubType();
77 }
78
79 preWeight = track->GetWeight();
81 energyDeposit = 0.0; // does not lose any energy
82 preEnergy = track->GetKineticEnergy();
84 preMomentum = track->GetMomentum();
86 preGlobalTime = track->GetGlobalTime();
88 // when we construct a trajectory from a track it hasn't taken a step yet,
89 // so we don't know the material
90 material = nullptr;
91
92#ifdef BDSDEBUG
93 G4cout << __METHOD_NAME__ << "Process (main|sub) (" << BDSProcessMap::Instance()->GetProcessName(postProcessType, postProcessSubType) << ")" << G4endl;
94#endif
95
96 // s position for pre and post step point
97 // with a track, we're at the start and have no step - use 1nm for step to aid geometrical lookup
98 BDSStep localPosMom = auxNavigator->ConvertToLocal(track->GetPosition(),
99 track->GetMomentum(),
100 1*CLHEP::nm,
101 true);
102 prePosLocal = localPosMom.PreStepPoint();
105 if (info)
106 {
107 G4double sCentre = info->GetSPos();
108 preS = sCentre + localPosMom.PreStepPoint().z();
109 postS = preS; // no step length yet
112 }
113
114 if (storeExtrasLocal)
115 {extraLocal = new BDSTrajectoryPointLocal(prePosLocal, localPosMom.PostStepPoint());} // "post step point" is momentum
116
117 if (storeExtrasLink)
118 {StoreExtrasLink(track);}
119
120 if (storeExtrasIon)
121 {StoreExtrasIon(track);}
122}
123
125 G4bool storeExtrasLocal,
126 G4bool storeExtrasLink,
127 G4bool storeExtrasIon):
128 G4TrajectoryPoint(step->GetPostStepPoint()->GetPosition())
129{
131
132 const G4StepPoint* prePoint = step->GetPreStepPoint();
133 const G4StepPoint* postPoint = step->GetPostStepPoint();
134 const G4VProcess* preProcess = prePoint->GetProcessDefinedStep();
135 const G4VProcess* postProcess = postPoint->GetProcessDefinedStep();
136
137 if (preProcess)
138 {
139 preProcessType = preProcess->GetProcessType();
140 preProcessSubType = preProcess->GetProcessSubType();
141 }
142 if (postProcess)
143 {
144 postProcessType = postProcess->GetProcessType();
145 postProcessSubType = postProcess->GetProcessSubType();
146 }
147
148 preWeight = prePoint->GetWeight();
149 postWeight = postPoint->GetWeight();
150 energyDeposit = step->GetTotalEnergyDeposit();
151 preEnergy = prePoint->GetKineticEnergy();
152 postEnergy = postPoint->GetKineticEnergy();
153 preMomentum = prePoint->GetMomentum();
154 postMomentum = postPoint->GetMomentum();
155 preGlobalTime = prePoint->GetGlobalTime();
156 postGlobalTime = postPoint->GetGlobalTime();
157 material = prePoint->GetMaterial();
158
159#ifdef BDSDEBUG
160 G4cout << __METHOD_NAME__ << BDSProcessMap::Instance()->GetProcessName(postProcessType, postProcessSubType) << G4endl;
161#endif
162
163 // get local coordinates and volume for transform
164 BDSStep localPosition = auxNavigator->ConvertToLocal(step);
165 prePosLocal = localPosition.PreStepPoint();
166 postPosLocal = localPosition.PostStepPoint();
168 if (info)
169 {
170 G4double sCentre = info->GetSPos();
171 preS = sCentre + localPosition.PreStepPoint().z();
172 postS = sCentre + localPosition.PostStepPoint().z();
175 }
176
177 if (storeExtrasLocal)
178 {
179 G4ThreeVector postLocalMom = auxNavigator->ConvertAxisToLocal(postMomentum); // uses cached transform
180 extraLocal = new BDSTrajectoryPointLocal(prePosLocal, postLocalMom);
181 }
182
183 G4Track* track = step->GetTrack();
184 if (storeExtrasLink)
185 {StoreExtrasLink(track);}
186
187 if (storeExtrasIon)
188 {StoreExtrasIon(track);}
189}
190
192 G4TrajectoryPoint(static_cast<const G4TrajectoryPoint&>(other))
193{
194 extraLocal = other.extraLocal ? new BDSTrajectoryPointLocal(*other.extraLocal) : nullptr;
195 extraLink = other.extraLink ? new BDSTrajectoryPointLink(*other.extraLink) : nullptr;
196 extraIon = other.extraIon ? new BDSTrajectoryPointIon(*other.extraIon) : nullptr;
201
202 preWeight = other.preWeight;
203 postWeight = other.postWeight;
204 preEnergy = other.preEnergy;
205 postEnergy = other.postEnergy;
206 preMomentum = other.preMomentum;
209 preS = other.preS;
210 postS = other.postS;
214 beamline = other.beamline;
215 prePosLocal = other.prePosLocal;
217 material = other.material;
218}
219
220BDSTrajectoryPoint::~BDSTrajectoryPoint()
221{
222 delete extraLocal;
223 delete extraLink;
224 delete extraIon;
225}
226
228{
229 preProcessType = -1;
231 postProcessType = -1;
233 preWeight = -1.;
234 postWeight = -1.;
235 preEnergy = -1.;
236 postEnergy = -1.;
237 preMomentum = G4ThreeVector();
238 postMomentum = G4ThreeVector();
239 energyDeposit = 0.0;
240 preS = -1000;
241 postS = -1000;
242 preGlobalTime = 0;
243 postGlobalTime = 0;
244 beamlineIndex = -1;
245 beamline = nullptr;
246 prePosLocal = G4ThreeVector();
247 postPosLocal = G4ThreeVector();
248 material = nullptr;
249 extraLocal = nullptr;
250 extraLink = nullptr;
251 extraIon = nullptr;
252}
253
254void BDSTrajectoryPoint::StoreExtrasLink(const G4Track* track)
255{
256 const G4DynamicParticle* dynamicParticleDef = track->GetDynamicParticle();
257 G4double charge = dynamicParticleDef->GetCharge();
258 G4double rigidity = 0;
259 if (BDS::IsFinite(charge))
260 {rigidity = BDS::Rigidity(track->GetMomentum().mag(), charge);}
261 extraLink = new BDSTrajectoryPointLink((G4int)charge,
262 BDSGlobalConstants::Instance()->TurnsTaken(),
263 dynamicParticleDef->GetMass(),
264 rigidity);
265}
266
267void BDSTrajectoryPoint::StoreExtrasIon(const G4Track* track)
268{
269 const G4ParticleDefinition* particleDef = track->GetParticleDefinition();
270 const G4DynamicParticle* dynamicParticleDef = track->GetDynamicParticle();
271 G4bool isIon = BDS::IsIon(dynamicParticleDef);
272 G4int nElectrons = dynamicParticleDef->GetTotalOccupancy();
273 extraIon = new BDSTrajectoryPointIon(isIon,
274 particleDef->GetAtomicMass(),
275 particleDef->GetAtomicNumber(),
276 nElectrons);
277}
278
279
281{
282 // use general static function
286
287#ifdef BDSDEBUG
288 if (isScatteringPoint)
289 {
290 G4cout << "Interaction point found at " << GetPreS()/CLHEP::m << " m - "
292 }
293#endif
294 return isScatteringPoint;
295}
296
298{
299 // if prestep process doesn't exist it won't be set and will
300 // default to value in InitialiseVariables which is -1
301 G4bool preStep = (preProcessType != G4ProcessType::fTransportation && preProcessType != G4ProcessType::fParallel);
302 G4bool posStep = (postProcessType != G4ProcessType::fTransportation && postProcessType != G4ProcessType::fParallel);
303 return preStep || posStep;
304}
305
306std::ostream& operator<< (std::ostream& out, BDSTrajectoryPoint const &p)
307{
308 out << p.GetPosition();
309 return out;
310}
311
313{
314 return std::hypot(prePosLocal.x(), prePosLocal.y());
315}
316
318{
319 return std::hypot(postPosLocal.x(), postPosLocal.y());
320}
321
323{
324 const G4StepPoint* postPoint = step->GetPostStepPoint();
325 const G4VProcess* postProcess = postPoint->GetProcessDefinedStep();
326
327 G4double totalEnergyDeposit = step->GetTotalEnergyDeposit();
328
329 G4Track* t = step->GetTrack();
330 if (t->GetCurrentStepNumber() == 1 && t->GetStepLength() < 1e-5 && totalEnergyDeposit < 1e-5)
331 {return false;} // ignore the first really small step
332
333 G4int postProcessType = -1;
334 G4int postProcessSubType = -1;
335 if (postProcess)
336 {
337 postProcessType = postProcess->GetProcessType();
338 postProcessSubType = postProcess->GetProcessSubType();
339 }
340
342}
343
344G4bool BDSTrajectoryPoint::IsScatteringPoint(G4int postProcessType,
345 G4int postProcessSubType,
346 G4double totalEnergyDeposit)
347{
348 // test against things we want to exclude like tracking - these are not
349 // points of scattering
350 G4bool initialised = postProcessType != -1;
351 G4bool notTransportation = postProcessType != G4ProcessType::fTransportation;
352 G4bool notGeneral = (postProcessType != G4ProcessType::fGeneral) && (postProcessSubType != STEP_LIMITER);
353 G4bool notParallel = postProcessType != G4ProcessType::fParallel;
354 G4bool notUndefined = postProcessType != G4ProcessType::fNotDefined; // for crystal channelling
355
356 // energy can change in transportation step (EM)
357 if (totalEnergyDeposit > dEThresholdForScattering)
358 {return true;}
359
360 G4bool result = initialised && notTransportation && notGeneral && notParallel && notUndefined;
361 return result;
362}
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:32
G4ThreeVector PostStepPoint() const
Accessor.
Definition BDSStep.hh:42
G4VPhysicalVolume * VolumeForTransform() const
Accessor.
Definition BDSStep.hh:43
G4ThreeVector PreStepPoint() const
Accessor.
Definition BDSStep.hh:41
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.