BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSLinkBunch.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 "BDSLinkBunch.hh"
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSIonDefinition.hh"
22#include "BDSParticleDefinition.hh"
23#include "BDSPhysicsUtilities.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
33BDSLinkBunch::BDSLinkBunch():
34 BDSBunch("sixtracklink"),
35 currentIndex(0),
36 currentExternalParticleID(0),
37 currentExternalParentID(0),
38 currentParticleDefinition(nullptr),
39 size(0)
40{;}
41
42BDSLinkBunch::~BDSLinkBunch()
43{;}
44
46{
47 if (Size() == 0)
48 {throw BDSException(__METHOD_NAME__,"No particles have been added");}
49
50 if (currentIndex >= size)
51 {
52 G4cout << __METHOD_NAME__ << "looping to start of bunch" << G4endl;
53 currentIndex = 0;
54 }
55
56 G4int ci = currentIndex;
57 currentIndex++;
58
59 auto particle = particles[ci];
60 currentParticleDefinition = particle->particleDefinition;
62 //UpdateGeant4ParticleDefinition(particleDefinition->PDGID()); // TBC
64
65 currentExternalParticleID = particle->externalParticleID;
66 currentExternalParentID = particle->externalParentID;
67
68 return particle->coords;
69}
70
72 const BDSParticleCoordsFull& coordsIn,
73 int externalParticleID,
74 int externalParentID)
75{
76 particles.emplace_back(new BDSParticleExternal(particleDefinitionIn, coordsIn, externalParticleID, externalParentID));
77 size = (G4int)particles.size();
78}
79
81{
82 currentIndex = 0;
83 size = 0;
84 for (auto p : particles)
85 {delete p;}
86 particles.clear();
87}
88
90{
91 G4ParticleDefinition* newParticleDefinition = nullptr;
93 {
94 G4ParticleTable::G4PTblEncodingDictionary* encoding = G4ParticleTable::fEncodingDictionary;
95 auto search = encoding->find(pdgID);
96 if (search != encoding->end())
97 {newParticleDefinition = search->second;}
98 else
99 {throw BDSException(__METHOD_NAME__,"PDG ID \"" + std::to_string(pdgID) + "not found in particle table");}
100 }
101 else
102 {
103 G4IonTable* ionTable = G4ParticleTable::GetParticleTable()->GetIonTable();
105 newParticleDefinition = ionTable->GetIon(ionDefinition->Z(),
106 ionDefinition->A(),
107 ionDefinition->ExcitationEnergy());
108 }
109 particleDefinition->UpdateG4ParticleDefinition(newParticleDefinition);
110 // Note we don't need to take care of electrons here. These are automatically
111 // allocated by Geant4 when it converts the primary vertex to a dynamic particle
112 // (in the process of constructing a track from it) (done in G4PrimaryTransformer)
113 // this relies on the charge being set correctly - Geant4 detects this isn't the same
114 // as Z and adds electrons accordingly.
115#if G4VERSION_NUMBER > 1049
116 // in the case of ions the particle definition is only available now
117 // fix the looping thresholds now it's available
118 BDS::FixGeant105ThreshholdsForParticle(newParticleDefinition);
119#endif
120}
121
123{
124 if (!currentParticleDefinition->IsAnIon())
125 {return;}
126
127 G4IonTable* ionTable = G4ParticleTable::GetParticleTable()->GetIonTable();
128 BDSIonDefinition* ionDefinition = currentParticleDefinition->IonDefinition();
129 G4ParticleDefinition* ionParticleDef = ionTable->GetIon(ionDefinition->Z(),
130 ionDefinition->A(),
131 ionDefinition->ExcitationEnergy());
132 currentParticleDefinition->UpdateG4ParticleDefinition(ionParticleDef);
133 // Note we don't need to take care of electrons here. These are automatically
134 // allocated by Geant4 when it converts the primary vertex to a dynamic particle
135 // (in the process of constructing a track from it) (done in G4PrimaryTransformer)
136 // this relies on the charge being set correctly - Geant4 detects this isn't the same
137 // as Z and adds electrons accordingly.
138#if G4VERSION_NUMBER > 1049
139 // in the case of ions the particle definition is only available now
140 // fix the looping thresholds now it's available
142#endif
143}
The base class for bunch distribution generators.
Definition BDSBunch.hh:46
G4bool particleDefinitionHasBeenUpdated
Definition BDSBunch.hh:202
BDSParticleDefinition * particleDefinition
Particle definition for bunch - this class owns it.
Definition BDSBunch.hh:198
General exception with possible name of object and message.
Class to parse an ion particle definition.
G4int A() const
Accessor.
G4double ExcitationEnergy() const
Accessor.
G4int Z() const
Accessor.
virtual void UpdateIonDefinition()
Override method in BDSBunch to use our local currentParticleDefinition member.
void UpdateGeant4ParticleDefinition(G4int pdgID)
TBC.
void AddParticle(BDSParticleDefinition *particleDefinitionIn, const BDSParticleCoordsFull &coordsIn, int externalParticleID, int externalParentID)
Append particle to the bunch for tracking.
void ClearParticles()
Delete all particle objects in the bunch and clear the vector.
G4int size
Number of particles (1 counting).
size_t Size() const
Accessor.
virtual BDSParticleCoordsFull GetNextParticleLocal()
Get the next particle.
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)