18#include "BDSBunchHalo.hh"
20#include "BDSException.hh"
21#include "BDSParticleCoordsFull.hh"
23#include "parser/beam.h"
27#include "Randomize.hh"
28#include "CLHEP/Random/RandFlat.h"
29#include "CLHEP/Units/SystemOfUnits.h"
33BDSBunchHalo::BDSBunchHalo():
35 alphaX(0.0), alphaY(0.0),
36 betaX(0.0), betaY(0.0),
37 emitX(0.0), emitY(0.0),
38 gammaX(0.0), gammaY(0.0),
39 sigmaX(0.0), sigmaY(0.0),
40 sigmaXp(0.0), sigmaYp(0.0),
41 haloNSigmaXInner(0.0), haloNSigmaXOuter(0.0),
42 haloNSigmaYInner(0.0), haloNSigmaYOuter(0.0),
51 haloPSWeightParameter(0.0),
53 emitInnerX(0.0), emitInnerY(0.0),
54 emitOuterX(0.0), emitOuterY(0.0),
56 xpMax(0.0), ypMax(0.0)
59BDSBunchHalo::~BDSBunchHalo()
65 G4Transform3D beamlineTransformIn,
66 const G4double beamlineSIn)
98 emitInnerX = std::pow(haloNSigmaXInner, 2) *
emitX;
99 emitInnerY = std::pow(haloNSigmaYInner, 2) *
emitY;
100 emitOuterX = std::pow(haloNSigmaXOuter, 2) *
emitX;
101 emitOuterY = std::pow(haloNSigmaYOuter, 2) *
emitY;
103 xMax = haloNSigmaXOuter *
sigmaX;
104 yMax = haloNSigmaYOuter *
sigmaY;
105 xpMax = std::sqrt(std::pow(haloNSigmaXOuter, 2) *
emitX *
gammaX);
106 ypMax = std::sqrt(std::pow(haloNSigmaYOuter, 2) *
emitY *
gammaY);
125 G4double dx = xMax * (1 - 2 * G4RandFlat::shoot());
126 G4double dy = yMax * (1 - 2 * G4RandFlat::shoot());
127 G4double dxp = xpMax * (1 - 2 * G4RandFlat::shoot());
128 G4double dyp = ypMax * (1 - 2 * G4RandFlat::shoot());
131 double emitXSp =
gammaX * std::pow(std::abs(dx), 2) + (2. *
alphaX * dx * dxp) +
betaX * std::pow(std::abs(dxp), 2);
132 double emitYSp =
gammaY * std::pow(std::abs(dy), 2) + (2. *
alphaY * dy * dyp) +
betaY * std::pow(std::abs(dyp), 2);
136 if ((std::abs(emitXSp) < emitInnerX || std::abs(emitYSp) < emitInnerY) ||
137 (std::abs(emitXSp) > emitOuterX || std::abs(emitYSp) > emitOuterY) ||
138 (std::abs(dx) < (haloXCutInner *
sigmaX)) ||
139 (std::abs(dy) < (haloYCutInner *
sigmaY)) ||
140 (std::abs(dx) > (haloXCutOuter *
sigmaX)) ||
141 (std::abs(dy) > (haloYCutOuter *
sigmaY)) ||
142 (std::abs(dxp) < (haloXpCutInner *
sigmaXp)) ||
143 (std::abs(dyp) < (haloYpCutInner *
sigmaYp)) ||
144 (std::abs(dxp) > (haloXpCutOuter *
sigmaXp)) ||
145 (std::abs(dyp) > (haloYpCutOuter *
sigmaYp)) )
154 if (weightFunction ==
"flat" || weightFunction.empty() || weightFunction ==
"one")
159 else if (weightFunction ==
"oneoverr")
162 wx = std::pow(std::abs(emitInnerX / emitXSp), haloPSWeightParameter);
163 wy = std::pow(std::abs(emitInnerY / emitYSp), haloPSWeightParameter);
165 else if (weightFunction ==
"oneoverrsqrd")
168 double eXsqrd = std::pow(std::abs(emitXSp), 2);
169 double eYsqrd = std::pow(std::abs(emitYSp), 2);
170 double eXInsq = std::pow(std::abs(emitInnerX), 2);
171 double eYInsq = std::pow(std::abs(emitInnerY), 2);
172 wx = std::pow(std::abs(eXInsq / eXsqrd), haloPSWeightParameter);
173 wy = std::pow(std::abs(eYInsq / eYsqrd), haloPSWeightParameter);
175 else if (weightFunction ==
"exp")
177 wx = std::exp(-(emitXSp * haloPSWeightParameter) / (emitInnerX));
178 wy = std::exp(-(emitYSp * haloPSWeightParameter) / (emitInnerY));
182 G4cout << __METHOD_NAME__ << emitXSp/
emitX <<
" " << emitYSp/
emitY <<
" " << wx <<
" " << wy << G4endl;
185 if (G4RandFlat::shoot() > wx && G4RandFlat::shoot() > wy)
191 xp += dxp * CLHEP::rad;
192 yp += dyp * CLHEP::rad;
197 G4cout << __METHOD_NAME__ <<
"selected> " << dx <<
" " << dy <<
" " << dxp <<
" " << dyp << G4endl;
211 {
throw BDSException(__METHOD_NAME__,
"emitx must be finite!");}
213 {
throw BDSException(__METHOD_NAME__,
"emity must be finite!");}
216 {
throw BDSException(__METHOD_NAME__,
"betx must be finite!");}
218 {
throw BDSException(__METHOD_NAME__,
"bety must be finite!");}
220 std::vector<G4String> weightFunctions = {
"",
"one",
"flat",
"oneoverr",
"oneoverrsqrd",
"exp"};
221 auto search = std::find(weightFunctions.begin(), weightFunctions.end(), weightFunction);
222 if (search == weightFunctions.end())
224 G4cerr << __METHOD_END__ <<
"invalid haloPSWeightFunction \"" << weightFunction <<
"\"" << G4endl;
225 G4cout <<
"Available weight functions are:" << G4endl;
226 for (
const auto& w : weightFunctions)
227 {G4cout << w << G4endl;}
231 if (haloNSigmaXInner <= 0)
232 {
throw BDSException(__METHOD_NAME__,
"haloNSigmaXInner <= 0");}
234 if (haloNSigmaYInner <= 0)
235 {
throw BDSException(__METHOD_NAME__,
"haloNSigmaYInner <= 0");}
237 if (haloNSigmaXInner > haloNSigmaXOuter)
238 {
throw BDSException(__METHOD_NAME__,
"haloNSigmaXInner cannot be less than haloNSigmaXOuter");}
240 if (haloNSigmaYInner > haloNSigmaYOuter)
241 {
throw BDSException(__METHOD_NAME__,
"haloNSigmaYInner cannot be less than haloNSigmaYOuter");}
243 if (haloXCutInner < 0)
244 {
throw BDSException(__METHOD_NAME__,
"haloXCutInner < 0");}
246 if (haloYCutInner < 0)
247 {
throw BDSException(__METHOD_NAME__,
"haloYCutInner < 0");}
249 if (haloXCutOuter <= haloXCutInner)
250 {
throw BDSException(__METHOD_NAME__,
"haloXCutOuter must be greater than haloXCutInner!");}
252 if (haloYCutOuter <= haloYCutInner)
253 {
throw BDSException(__METHOD_NAME__,
"haloYCutOuter must be greater than haloYCutInner!");}
255 if (haloXpCutInner < 0)
256 {
throw BDSException(__METHOD_NAME__,
"haloXpCutInner < 0");}
258 if (haloYpCutInner < 0)
259 {
throw BDSException(__METHOD_NAME__,
"haloYpCutInner < 0");}
261 if (haloXpCutOuter <= haloXpCutInner)
262 {
throw BDSException(__METHOD_NAME__,
"haloXpCutOuter must be greater than haloXpCutInner!");}
264 if (haloYpCutOuter <= haloYpCutInner)
265 {
throw BDSException(__METHOD_NAME__,
"haloYpCutOuter must be greater than haloYpCutInner!");}
G4double sigmaX
Twiss parameter.
G4double sigmaYp
Twiss parameter.
G4double alphaY
Twiss parameter.
G4double gammaY
Twiss parameter.
G4double emitX
Twiss parameter.
G4double gammaX
Twiss parameter.
G4double sigmaXp
Twiss parameter.
G4double emitY
Twiss parameter.
G4double sigmaY
Twiss parameter.
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
virtual BDSParticleCoordsFull GetNextParticleLocal()
G4double betaY
Twiss parameter.
virtual void CheckParameters()
G4double alphaX
Twiss parameter.
G4double betaX
Twiss parameter.
The base class for bunch distribution generators.
G4double Yp0
Centre of distributions.
G4double T0
Centre of distributions.
G4double S0
Centre of distributions.
G4double Z0
Centre of distributions.
G4double X0
Centre of distributions.
G4double Zp0
Centre of distributions.
G4double Xp0
Centre of distributions.
G4double E0
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 SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
static G4double CalculateZp(G4double xp, G4double yp, G4double Zp0)
Calculate zp safely based on other components.
virtual void CheckParameters()
General exception with possible name of object and message.
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
double alfy
initial twiss parameters
double haloXCutOuter
for the halo distribution
double haloNSigmaYInner
for the halo distribution
double haloXpCutOuter
for the halo distribution
double haloXCutInner
for the halo distribution
double haloYpCutInner
for the halo distribution
double betx
initial twiss parameters
double haloYCutOuter
for the halo distribution
double haloNSigmaXInner
for the halo distribution
double haloNSigmaYOuter
for the halo distribution
double haloYCutInner
for the halo distribution
double haloNSigmaXOuter
for the halo distribution
double alfx
initial twiss parameters
double bety
initial twiss parameters
double haloPSWeightParameter
for the halo distribution
std::string haloPSWeightFunction
for the halo distribution
double haloXpCutInner
for the halo distribution
double haloYpCutOuter
for the halo distribution