9#include "G4ParticleTable.hh"
10#include "Randomize.hh"
21 const std::string& arandomMomentumModel,
24 const std::string& arandomThetaModel,
33 const std::string& arandomVertexModel,
34 const std::shared_ptr<GLogger>& logger,
35 int agenerator_type) :
37 generator_type(agenerator_type),
38 multiplicity(amultiplicity),
43 delta_theta(adelta_theta),
46 delta_phi(adelta_phi),
47 v(G4ThreeVector(avx, avy, avz)),
48 delta_v(G4ThreeVector(adelta_vx, adelta_vy, adelta_vz)),
61 G4Event* anEvent)
const {
62 auto particleTable = G4ParticleTable::GetParticleTable();
63 std::vector<GparticleRuntimeRecord> runtime_records;
64 if (multiplicity > 0) { runtime_records.reserve(
static_cast<size_t>(multiplicity)); }
68 auto particleDef = particleTable->FindParticle(name);
72 double mass = particleDef->GetPDGMass();
73 particleGun->SetParticleDefinition(particleDef);
77 for (
int i = 0; i < multiplicity; i++) {
78 auto pmev = calculateMomentum();
79 auto kenergy = sqrt(pmev * pmev + mass * mass) - mass;
80 auto thetaRad = randomizeNumberFromSigmaWithModel(theta, delta_theta, randomThetaModel) / CLHEP::rad;
81 auto phiRad = randomizeNumberFromSigmaWithModel(phi, delta_phi,
gutilities::uniform) / CLHEP::rad;
82 auto beamDirection = calculateBeamDirection(thetaRad, phiRad);
83 auto vertex = calculateVertex();
85 runtime_records.push_back({
90 thetaRad * CLHEP::rad,
95 particleGun->SetParticleEnergy(kenergy);
96 particleGun->SetParticleMomentumDirection(beamDirection);
97 particleGun->SetParticlePosition(vertex);
98 particleGun->GeneratePrimaryVertex(anEvent);
102 "Generated particle <%1> pid %2, p [MeV]: %3, theta [deg]: %4, phi [deg]: %5, vertex "
103 "[cm]: %6", name, pid, pmev / CLHEP::MeV, thetaRad / CLHEP::deg, phiRad / CLHEP::deg,
119 return runtime_records;
123double Gparticle::calculateMomentum()
const {
125 double pmev = randomizeNumberFromSigmaWithModel(p, delta_p, randomMomentumModel);
130double Gparticle::calculateKinEnergy(
double mass)
const {
131 double pmev = calculateMomentum();
133 return sqrt(pmev * pmev + mass * mass) - mass;
137G4ThreeVector Gparticle::calculateBeamDirection(
double thetaRad,
double phiRad)
const {
138 G4ThreeVector pdir = G4ThreeVector(
139 cos(phiRad) * sin(thetaRad),
140 sin(phiRad) * sin(thetaRad),
147G4ThreeVector Gparticle::calculateVertex()
const {
150 switch (randomVertexModel) {
169 double max_radius = delta_v.r();
176 radius = x * x + y * y + z * z;
178 while (radius > max_radius * max_radius);
199double Gparticle::randomizeNumberFromSigmaWithModel(
double center,
double delta,
gutilities::randomModel model)
const {
203 return center + ( 2.0 * G4UniformRand() - 1.0 ) * delta;
207 return G4RandGauss::shoot(center, delta);
213 double lower = ( center - delta ) / CLHEP::rad;
214 double upper = ( center + delta ) / CLHEP::rad;
215 double center_rad = 0;
219 do { center_rad = acos(1 - 2 * G4UniformRand()); }
220 while (center_rad < lower || center_rad > upper);
224 center_rad = theta / CLHEP::rad;
227 return center_rad * CLHEP::rad;
244 constexpr int label_w = 15;
245 constexpr int value_w = 12;
248 auto show = [&](
const std::string& label,
const auto& value) {
249 os << left << setw(label_w) << label <<
' '
250 << setw(value_w) << right << setw(value_w) << value <<
'\n';
254 auto showf = [&](
const std::string& label,
double value,
int prec = 3) {
255 std::streamsize old_prec = os.precision();
256 auto old_flags = os.flags();
258 os << left << setw(label_w) << label <<
' '
259 << right << setw(value_w) << std::fixed
260 << std::setprecision(prec) << value <<
'\n';
262 os.precision(old_prec);
267 auto show_pm = [&](
const std::string& label,
268 double val,
double err,
270 std::streamsize old_prec = os.precision();
271 auto old_flags = os.flags();
273 os << left << setw(label_w) << label <<
' '
274 << right << setw(value_w) << std::fixed << setw(value_w)
275 << std::setprecision(prec) << val
276 <<
" ± " << std::setprecision(prec) << err <<
'\n';
278 os.precision(old_prec);
286 <<
" ┌─────────────────────────────────────────────────┐\n"
287 <<
" │ GParticle │\n"
288 <<
" └─────────────────────────────────────────────────┘\n";
293 os << left << setw(label_w) <<
" name:" << right << setw(value_w)
294 << gp.name <<
"(pid " << gp.pid <<
")\n";
296 show(
" multiplicity:", std::to_string(gp.multiplicity));
297 showf(
" mass [MeV]:", gp.get_mass());
301 gp.delta_p / CLHEP::MeV);
303 show(
" p model:",
to_string(gp.randomMomentumModel));
305 show_pm(
" theta [deg]:",
306 gp.theta / CLHEP::deg,
307 gp.delta_theta / CLHEP::deg);
309 show(
" theta model:",
to_string(gp.randomThetaModel));
311 show_pm(
" phi [deg]:",
313 gp.delta_phi / CLHEP::deg);
315 os << left << setw(label_w) <<
" vertex [cm]:" <<
' '
316 << gp.v <<
" ± " << gp.delta_v <<
'\n';
318 show(
" vertex model:",
to_string(gp.randomVertexModel));
324int Gparticle::get_pdg_id() {
325 auto particleTable = G4ParticleTable::GetParticleTable();
328 auto particleDef = particleTable->FindParticle(name);
330 if (particleDef !=
nullptr) {
return particleDef->GetPDGEncoding(); }
345double Gparticle::get_mass()
const {
346 auto particleTable = G4ParticleTable::GetParticleTable();
349 auto particleDef = particleTable->FindParticle(name);
352 double mass = particleDef->GetPDGMass();
std::vector< GparticleRuntimeRecord > shootParticle(G4ParticleGun *particleGun, G4Event *anEvent) const
Shoots this particle configuration into a Geant4 event.
Gparticle(const std::string &name, int multiplicity, double p, double delta_p, const std::string &randomMomentumModel, double theta, double delta_theta, const std::string &thetaModel, double phi, double delta_phi, double avx, double avy, double avz, double adelta_vx, double adelta_vy, double adelta_vz, const std::string &randomVertexModel, const std::shared_ptr< GLogger > &logger, int generator_type=1)
Constructs a particle configuration from pre-converted G4-unit values.
Conventions and error codes for the gparticle module.
std::ostream & operator<<(std::ostream &os, const Gparticle &gp)
Definition of the Gparticle class used by the gparticle module.
constexpr int ERR_GPARTICLETABLENOTFOUND
G4ParticleTable could not be obtained (unexpected runtime state).
constexpr int ERR_GPARTICLENOTFOUND
Requested particle name was not found in the G4ParticleTable.
randomModel stringToRandomModel(const std::string &str)
constexpr const char * to_string(randomModel m) noexcept