BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchSixTrackLink.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 "BDSBunchSixTrackLink.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSIonDefinition.hh"
23#include "BDSParticleDefinition.hh"
24
25#include "globals.hh"
26#include "G4IonTable.hh"
27#include "G4ParticleDefinition.hh"
28#include "G4ParticleTable.hh"
29#include "G4String.hh"
30
31#include <vector>
32
33BDSBunchSixTrackLink::BDSBunchSixTrackLink():
34 currentIndex(0),
35 currentExternalParticleID(0),
36 currentExternalParentID(0),
37 currentParticleDefinition(nullptr),
38 size(0)
39{;}
40
41BDSBunchSixTrackLink::~BDSBunchSixTrackLink()
42{;}
43
45{
46 if (currentIndex >= size)
47 {
48 G4cout << __METHOD_NAME__ << "looping to start of bunch" << G4endl;
49 currentIndex = 0;
50 }
51
52 G4int ci = currentIndex;
53 currentIndex++;
54
55 auto particle = particles[ci];
56 currentParticleDefinition = particle->particleDefinition;
58 //UpdateGeant4ParticleDefinition(particleDefinition->PDGID()); // TBC
60
61 currentExternalParticleID = particle->externalParticleID;
62 currentExternalParentID = particle->externalParentID;
63
64 return particle->coords;
65}
66
68 const BDSParticleCoordsFull& coordsIn,
69 int externalParticleID,
70 int externalParentID)
71{
72 particles.emplace_back(new BDSParticleExternal(particleDefinitionIn, coordsIn, externalParticleID, externalParentID));
73 size = (G4int)particles.size();
74}
75
77{
78 currentIndex = 0;
79 size = 0;
80 for (auto p : particles)
81 {delete p;}
82 particles.clear();
83}
84
86{
87 G4ParticleDefinition* newParticleDefinition = nullptr;
89 {
90 G4ParticleTable::G4PTblEncodingDictionary* encoding = G4ParticleTable::fEncodingDictionary;
91 auto search = encoding->find(pdgID);
92 if (search != encoding->end())
93 {newParticleDefinition = search->second;}
94 else
95 {throw BDSException(__METHOD_NAME__,"PDG ID \"" + std::to_string(pdgID) + "not found in particle table");}
96 }
97 else
98 {
99 G4IonTable* ionTable = G4ParticleTable::GetParticleTable()->GetIonTable();
101 newParticleDefinition = ionTable->GetIon(ionDefinition->Z(),
102 ionDefinition->A(),
103 ionDefinition->ExcitationEnergy());
104 }
105 particleDefinition->UpdateG4ParticleDefinition(newParticleDefinition);
106 // Note we don't need to take care of electrons here. These are automatically
107 // allocated by Geant4 when it converts the primary vertex to a dynamic particle
108 // (in the process of constructing a track from it) (done in G4PrimaryTransformer)
109 // this relies on the charge being set correctly - Geant4 detects this isn't the same
110 // as Z and adds electrons accordingly.
111#if G4VERSION_NUMBER > 1049
112 // in the case of ions the particle definition is only available now
113 // fix the looping thresholds now it's available
114 BDS::FixGeant105ThreshholdsForParticle(newParticleDefinition);
115#endif
116}
117
119{
120 if (!currentParticleDefinition->IsAnIon())
121 {return;}
122
123 G4IonTable* ionTable = G4ParticleTable::GetParticleTable()->GetIonTable();
124 BDSIonDefinition* ionDefinition = currentParticleDefinition->IonDefinition();
125 G4ParticleDefinition* ionParticleDef = ionTable->GetIon(ionDefinition->Z(),
126 ionDefinition->A(),
127 ionDefinition->ExcitationEnergy());
128 currentParticleDefinition->UpdateG4ParticleDefinition(ionParticleDef);
129 // Note we don't need to take care of electrons here. These are automatically
130 // allocated by Geant4 when it converts the primary vertex to a dynamic particle
131 // (in the process of constructing a track from it) (done in G4PrimaryTransformer)
132 // this relies on the charge being set correctly - Geant4 detects this isn't the same
133 // as Z and adds electrons accordingly.
134#if G4VERSION_NUMBER > 1049
135 // in the case of ions the particle definition is only available now
136 // fix the looping thresholds now it's available
138#endif
139}
G4bool particleDefinitionHasBeenUpdated
Definition: BDSBunch.hh:185
BDSParticleDefinition * particleDefinition
Particle definition for bunch - this class owns it.
Definition: BDSBunch.hh:181
General exception with possible name of object and message.
Definition: BDSException.hh:35
Class to parse an ion particle definition.
G4int A() const
Accessor.
G4double ExcitationEnergy() const
Accessor.
G4int Z() const
Accessor.
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
BDSIonDefinition * IonDefinition() const
Accessor.
G4bool IsAnIon() const
Accessor.
void UpdateG4ParticleDefinition(G4ParticleDefinition *particleIn)
A set of particle coordinates from an external interface.
void FixGeant105ThreshholdsForParticle(const G4ParticleDefinition *particleDefinition)