12#include "G4ParticleTable.hh"
13#include "Randomize.hh"
26 const std::string& punit,
27 const std::string& arandomMomentumModel,
30 const std::string& arandomThetaModel,
33 const std::string& aunit,
40 const std::string& vunit,
41 const std::string& arandomVertexModel,
42 const std::shared_ptr<GLogger>& logger,
43 int agenerator_type) :
45 generator_type(agenerator_type),
46 multiplicity(amultiplicity),
61 delta_v(G4ThreeVector(
78 auto particleTable = G4ParticleTable::GetParticleTable();
79 runtimeRecords.clear();
83 auto particleDef = particleTable->FindParticle(name);
87 double mass = particleDef->GetPDGMass();
88 particleGun->SetParticleDefinition(particleDef);
92 for (
int i = 0; i < multiplicity; i++) {
93 auto pmev = calculateMomentum();
94 auto kenergy = sqrt(pmev * pmev + mass * mass) - mass;
95 auto thetaRad = randomizeNumberFromSigmaWithModel(theta, delta_theta, randomThetaModel) / CLHEP::rad;
96 auto phiRad = randomizeNumberFromSigmaWithModel(phi, delta_phi,
gutilities::uniform) / CLHEP::rad;
97 auto beamDirection = calculateBeamDirection(thetaRad, phiRad);
98 auto vertex = calculateVertex();
100 setRunTimeQuantities(kenergy, beamDirection, vertex);
101 runtimeRecords.push_back({
106 thetaRad * CLHEP::rad,
111 particleGun->SetParticleEnergy(kenergy);
112 particleGun->SetParticleMomentumDirection(beamDirection);
113 particleGun->SetParticlePosition(vertex);
114 particleGun->GeneratePrimaryVertex(anEvent);
121 "Particle <", name,
"> not found in G4ParticleTable* ", particleTable);
126 "G4ParticleTable not found - G4ParticleGun*: ", particleGun);
131double Gparticle::calculateMomentum() {
133 double pmev = randomizeNumberFromSigmaWithModel(p, delta_p, randomMomentumModel);
138double Gparticle::calculateKinEnergy(
double mass) {
139 double pmev = calculateMomentum();
141 return sqrt(pmev * pmev + mass * mass) - mass;
145G4ThreeVector Gparticle::calculateBeamDirection(
double thetaRad,
double phiRad) {
146 G4ThreeVector pdir = G4ThreeVector(
147 cos(phiRad) * sin(thetaRad),
148 sin(phiRad) * sin(thetaRad),
155G4ThreeVector Gparticle::calculateVertex() {
158 switch (randomVertexModel) {
177 double max_radius = delta_v.r();
184 radius = x * x + y * y + z * z;
186 while (radius > max_radius);
207double Gparticle::randomizeNumberFromSigmaWithModel(
double center,
double delta,
gutilities::randomModel model)
const {
211 return center + ( 2.0 * G4UniformRand() - 1.0 ) * delta;
215 return G4RandGauss::shoot(center, delta);
221 double lower = ( center - delta ) / CLHEP::rad;
222 double upper = ( center + delta ) / CLHEP::rad;
223 double center_rad = 0;
227 do { center_rad = acos(1 - 2 * G4UniformRand()); }
228 while (center_rad < lower || center_rad > upper);
232 center_rad = theta / CLHEP::rad;
235 return center_rad * CLHEP::rad;
252 constexpr int label_w = 15;
253 constexpr int value_w = 12;
256 auto show = [&](
const std::string& label,
const auto& value) {
257 os << left << setw(label_w) << label <<
' '
258 << setw(value_w) << right << setw(value_w) << value <<
'\n';
262 auto showf = [&](
const std::string& label,
double value,
int prec = 3) {
263 std::streamsize old_prec = os.precision();
264 auto old_flags = os.flags();
266 os << left << setw(label_w) << label <<
' '
267 << right << setw(value_w) << std::fixed
268 << std::setprecision(prec) << value <<
'\n';
270 os.precision(old_prec);
275 auto show_pm = [&](
const std::string& label,
276 double val,
double err,
278 std::streamsize old_prec = os.precision();
279 auto old_flags = os.flags();
281 os << left << setw(label_w) << label <<
' '
282 << right << setw(value_w) << std::fixed << setw(value_w)
283 << std::setprecision(prec) << val
284 <<
" ± " << std::setprecision(prec) << err <<
'\n';
286 os.precision(old_prec);
294 <<
" ┌─────────────────────────────────────────────────┐\n"
295 <<
" │ GParticle │\n"
296 <<
" └─────────────────────────────────────────────────┘\n";
301 os << left << setw(label_w) <<
" name:" << right << setw(value_w)
302 << gp.name <<
"(pid " << gp.pid <<
")\n";
304 show(
" multiplicity:", std::to_string(gp.multiplicity));
305 showf(
" mass [MeV]:", gp.get_mass());
309 gp.delta_p / CLHEP::MeV);
311 show(
" p model:", to_string(gp.randomMomentumModel));
313 show_pm(
" theta [deg]:",
314 gp.theta / CLHEP::deg,
315 gp.delta_theta / CLHEP::deg);
317 show(
" theta model:", to_string(gp.randomThetaModel));
319 show_pm(
" phi [deg]:",
321 gp.delta_phi / CLHEP::deg);
323 os << left << setw(label_w) <<
" vertex [cm]:" <<
' '
324 << gp.v <<
" ± " << gp.delta_v <<
'\n';
326 show(
" vertex model:", to_string(gp.randomVertexModel));
328 showf(
" kinematic energy [MeV]:", gp.kinenergy / CLHEP::MeV);
329 os << left << setw(label_w) <<
" beam direction :" <<
' '
330 << gp.beamDirection <<
'\n';
332 os << left << setw(label_w) <<
" vertex :" <<
' '
333 << gp.vertex <<
'\n';
340int Gparticle::get_pdg_id() {
341 auto particleTable = G4ParticleTable::GetParticleTable();
344 auto particleDef = particleTable->FindParticle(name);
346 if (particleDef !=
nullptr) {
return particleDef->GetPDGEncoding(); }
349 "Particle <", name,
"> not found in G4ParticleTable* ", particleTable);
354 "G4ParticleTable not found - G4ParticleGun*: ", particleTable);
359double Gparticle::get_mass()
const {
360 auto particleTable = G4ParticleTable::GetParticleTable();
363 auto particleDef = particleTable->FindParticle(name);
366 double mass = particleDef->GetPDGMass();
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Lightweight particle specification and primary vertex shooter.
void shootParticle(G4ParticleGun *particleGun, G4Event *anEvent)
Shoots this particle configuration into a Geant4 event.
Gparticle(const std::string &name, int multiplicity, double p, double delta_p, const std::string &punit, const std::string &randomMomentumModel, double theta, double delta_theta, const std::string &thetaModel, double phi, double delta_phi, const std::string &aunit, double avx, double avy, double avz, double adelta_vx, double adelta_vy, double adelta_vz, const std::string &vunit, const std::string &randomVertexModel, const std::shared_ptr< GLogger > &logger, int generator_type=1)
Constructs a particle configuration from user-facing parameters.
Conventions and error codes for the gparticle module.
#define ERR_GPARTICLETABLENOTFOUND
G4ParticleTable could not be obtained (unexpected runtime state).
#define ERR_GPARTICLENOTFOUND
Requested particle name was not found in the G4ParticleTable.
std::ostream & operator<<(std::ostream &os, const Gparticle &gp)
Definition of the Gparticle class used by the gparticle module.
Public API for defining and parsing gparticle-related options.
double getG4Number(const string &v, bool warnIfNotUnit=false)
randomModel stringToRandomModel(const std::string &str)