19#include "BDSAcceleratorModel.hh"
20#include "BDSBeamline.hh"
23#include "BDSException.hh"
24#include "BDSGlobalConstants.hh"
25#include "BDSIonDefinition.hh"
26#include "BDSParticleCoords.hh"
27#include "BDSParticleCoordsFull.hh"
28#include "BDSParticleCoordsFullGlobal.hh"
29#include "BDSParticleDefinition.hh"
30#include "BDSPhysicsUtilities.hh"
31#include "BDSUtilities.hh"
33#include "parser/beam.h"
35#include "G4IonTable.hh"
36#include "G4ParticleTable.hh"
37#include "G4ThreeVector.hh"
38#include "G4Transform3D.hh"
39#include "G4TwoVector.hh"
40#include "G4Version.hh"
42#include "CLHEP/Geometry/Point3D.h"
54BDSBunch::BDSBunch(
const G4String& nameIn):
56 X0(0.0), Y0(0.0), Z0(0.0), S0(0.0), T0(0.0),
57 Xp0(0.0), Yp0(0.0), Zp0(0.0), E0(0.0), P0(0.0),
59 sigmaT(0.0), sigmaP(0.0), sigmaE(0.0), sigmaEk(0.0),
60 useCurvilinear(false),
61 particleDefinition(nullptr),
62 particleDefinitionHasBeenUpdated(false),
66 generatePrimariesOnly(false),
67 beamlineTransform(G4Transform3D()),
81 G4Transform3D beamlineTransformIn,
87 G4ThreeVector unitZBeamline = G4ThreeVector(0,0,-1).transform(beamlineTransformIn.getRotation());
89 beamlineTransform = G4Transform3D(beamlineTransformIn.getRotation(), beamlineTransformIn.getTranslation()+translation);
93 X0 = beam.
X0 * CLHEP::m;
94 Y0 = beam.
Y0 * CLHEP::m;
95 Z0 = beam.
Z0 * CLHEP::m;
96 S0 = beam.
S0 * CLHEP::m;
97 T0 = beam.
T0 * CLHEP::s;
98 Xp0 = beam.
Xp0 * CLHEP::rad;
99 Yp0 = beam.
Yp0 * CLHEP::rad;
114 std::set<std::string> keysDesign = {
"sigmaE",
"sigmaEk",
"sigmaP"};
122 else if (finiteSigmaP)
127 else if (finiteSigmaEk)
137 G4cout <<
"Beam> sigmaP: " <<
sigmaP << G4endl;
138 G4cout <<
"Beam> sigmaE: " <<
sigmaE << G4endl;
139 G4cout <<
"Beam> sigmaEk: " <<
sigmaEk << G4endl;
147 G4cout << __METHOD_NAME__ <<
"using curvilinear transform" << G4endl;
150 {
throw BDSException(__METHOD_NAME__,
"both Z0 and S0 are defined - please define only one!");}
157 G4double& emittGeometricX,
158 G4double& emittGeometricY,
159 G4double& emittNormalisedX,
160 G4double& emittNormalisedY)
162 std::set<std::string> keysDesignX = {
"emitx",
"emitnx"};
167 emittNormalisedX = G4double(beam.
emitNX);
168 emittGeometricX = G4double(beam.
emitNX) / beamParticle->
Gamma();
172 emittGeometricX = G4double(beam.
emitx);
173 emittNormalisedX = G4double(beam.
emitx) * beamParticle->
Gamma();
176 std::set<std::string> keysDesignY = {
"emity",
"emitny"};
181 emittNormalisedY = G4double(beam.
emitNY);
182 emittGeometricY = G4double(beam.
emitNY) / beamParticle->
Gamma();}
185 emittGeometricY = G4double(beam.
emity);
186 emittNormalisedY = G4double(beam.
emity) * beamParticle->
Gamma();
189 G4cout << __METHOD_NAME__ <<
"Geometric (x): " << emittGeometricX
190 <<
", Normalised (x): " << emittNormalisedX << G4endl;
191 G4cout << __METHOD_NAME__ <<
"Geometric (y): " << emittGeometricY
192 <<
", Normalised (y): " << emittNormalisedY << G4endl;
228 {
throw BDSException(__METHOD_NAME__,
"unable to generate coordinates above rest mass after 100 attempts.");}
268 G4TwoVector xy(localIn.x, localIn.y);
269 G4TwoVector xpyp(localIn.xp, localIn.yp);
274 localIn.xp = xpyp.x();
275 localIn.yp = xpyp.y();
287 {
throw BDSException(__METHOD_NAME__,
"no beamline constructed!");}
291 G4int beamlineIndex = 0;
292 G4double S =
S0 + localIn.z;
294 {
throw BDSException(__METHOD_NAME__,
"Negative S detected for particle.");}
301 G4ThreeVector cMom = G4ThreeVector(localIn.xp, localIn.yp, localIn.zp).transform(cTrans.getRotation());
303 G4ThreeVector cPos = cTrans.getTranslation();
306 cMom.x(), cMom.y(), cMom.z(),
312 G4cout << __METHOD_NAME__ << result << G4endl;
320 G4double transMom = std::pow(xp, 2) + std::pow(yp, 2);
322 if (transMom > (1 - std::numeric_limits<double>::epsilon()))
323 {
throw BDSException(__METHOD_NAME__,
"xp, yp too large, xp: " + std::to_string(xp) +
" yp: " + std::to_string(yp));}
325 {zp = -std::sqrt(1.0 - transMom);}
327 {zp = std::sqrt(1.0 - transMom);}
337 G4IonTable* ionTable = G4ParticleTable::GetParticleTable()->GetIonTable();
339 G4ParticleDefinition* ionParticleDef = ionTable->GetIon(ionDefinition->
Z(),
348#if G4VERSION_NUMBER > 1049
const BDSBeamline * BeamlineMain() const
Accessor.
G4Transform3D GetGlobalEuclideanTransform(G4double s, G4double x=0, G4double y=0, G4int *indexOfFoundElement=nullptr) const
The base class for bunch distribution generators.
BDSParticleCoordsFullGlobal ApplyCurvilinearTransform(const BDSParticleCoordsFull &localIn) const
Calculate the global coordinates from curvilinear coordinates of a beam line.
G4double sigmaE
Centre of distributions.
const BDSBeamline * beamline
A reference to the fully constructed beamline that's lazily instantiated.
G4double Yp0
Centre of distributions.
G4double T0
Centre of distributions.
G4bool finiteSigmaE
Flags to ignore random number generator in case of no finite E or T.
G4double tilt
Centre of distributions.
BDSParticleCoordsFullGlobal ApplyTransform(const BDSParticleCoordsFull &localIn) const
void ApplyTilt(BDSParticleCoordsFull &localIn) const
Apply a rotation about unitZ for the local coordinates according to member variable tilt.
G4double S0
Centre of distributions.
G4double P0
central momentum
G4bool particleDefinitionHasBeenUpdated
G4bool generatePrimariesOnly
virtual void SetGeneratePrimariesOnly(G4bool generatePrimariesOnlyIn)
G4double sigmaP
Centre of distributions.
G4bool useCurvilinear
Whether to ignore z and use s and transform for curvilinear coordinates.
G4Transform3D beamlineTransform
Transform that beam line starts with that will also be applied to coordinates.
G4double sigmaT
Centre of distributions.
G4double Z0
Centre of distributions.
virtual BDSParticleCoordsFull GetNextParticleLocal()
G4double X0
Centre of distributions.
G4double Zp0
Centre of distributions.
virtual void Initialise()
Any initialisation - to be used after SetOptions, then CheckParameters.
G4double Xp0
Centre of distributions.
G4double E0
Centre of distributions.
G4double sigmaEk
Centre of distributions.
static void SetEmittances(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, G4double &emittGeometricX, G4double &emittGeometricY, G4double &emittNormalisedX, G4double &emittNormalisedY)
G4double Y0
Centre of distributions.
virtual void BeginOfRunAction(G4int numberOfEvents)
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
virtual void UpdateIonDefinition()
G4double beamlineS
Beamline initial S position.
BDSParticleCoordsFullGlobal GetNextParticle()
G4bool finiteSigmaT
Flags to ignore random number generator in case of no finite E or T.
static G4double CalculateZp(G4double xp, G4double yp, G4double Zp0)
Calculate zp safely based on other components.
BDSParticleDefinition * particleDefinition
Particle definition for bunch - this class owns it.
virtual BDSParticleCoordsFullGlobal GetNextParticleValid(G4int maxTries=100)
virtual void CheckParameters()
General exception with possible name of object and message.
static BDSGlobalConstants * Instance()
Access method.
Class to parse an ion particle definition.
G4double ExcitationEnergy() const
Accessor.
A set of particle coordinates in both local and global.
G4int beamlineIndex
Optional index for where transform was found.
A set of particle coordinates including energy and weight.
A set of particle coordinates.
BDSParticleCoords ApplyTransform(const G4Transform3D &transform) const
Apply a transform to the coordinates and return a copy of them transformed.
Wrapper for particle definition.
G4double Mass() const
Accessor.
G4double Momentum() const
Accessor.
G4double KineticEnergy() const
Accessor.
G4double Beta() const
Accessor.
G4double Gamma() const
Accessor.
BDSIonDefinition * IonDefinition() const
Accessor.
G4double TotalEnergy() const
Accessor.
G4bool IsAnIon() const
Accessor.
void UpdateG4ParticleDefinition(G4ParticleDefinition *particleIn)
Improve type-safety of native enum data type in C++.
double sigmaE
for the gaussian, elliptic shell, ring distributions
double emity
initial twiss parameters
double emitNY
initial twiss parameters
double tilt
tilt of beam applied as rotation about unit local z
double emitNX
initial twiss parameters
double S0
initial beam centroid
double emitx
initial twiss parameters
double sigmaT
bunch length
double Zp0
initial beam centroid
double Z0
initial beam centroid
double X0
initial beam centroid
double Xp0
initial beam centroid
double Yp0
initial beam centroid
double Y0
initial beam centroid
double T0
initial beam centroid
void ConflictingParametersSet(const GMAD::Beam &beamDefinition, const std::set< std::string > &keys, G4int nSet, G4bool warnZeroParamsSet=true, const G4String &unitString="")
Throw an exception if too few or too many parameters are set for the supplied keys.
void FixGeant105ThreshholdsForParticle(const G4ParticleDefinition *particleDefinition)
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4int NBeamParametersSet(const GMAD::Beam &beamDefinition, const std::set< std::string > &keys)
Count how many out of the set of keys in a beam instance are set.