gparticle
Loading...
Searching...
No Matches
gparticle_options.cc
Go to the documentation of this file.
1// gparticle
2#include "gparticle_options.h"
4
5// namespace to define options
6namespace gparticle {
7using std::string;
8using std::vector;
9
10// Build a vector of configured Gparticle instances from the structured "gparticle" option node.
11// The detailed API contract is documented in gparticle_options.h.
12vector<GparticlePtr> getGParticles(const std::shared_ptr<GOptions>& gopts, std::shared_ptr<GLogger>& logger) {
13 // Retrieve the structured option node that contains an array of particle definitions.
14 auto gparticle_node = gopts->getOptionNode("gparticle");
15
16 vector<GparticlePtr> gparticles;
17 // Reserve storage up-front to avoid reallocations (and therefore avoid extra shared_ptr moves).
18 gparticles.reserve(gparticle_node.size()); // no reallocations ⇒ no moves
19
20 // Translate each structured node item into a configured Gparticle instance.
21 for (auto gparticle_item : gparticle_node) {
22 // Each get_variable_in_option<T>(...) call:
23 // - extracts the key from this structured item
24 // - applies the provided default if the key is missing
25 // - keeps the typing explicit (string/int/double)
26 gparticles.emplace_back(std::make_shared<Gparticle>(
27 gopts->get_variable_in_option<string>(gparticle_item, "name", goptions::NODFLT),
28 gopts->get_variable_in_option<int>(gparticle_item, "multiplicity", 1),
29 gopts->get_variable_in_option<double>(gparticle_item, "p", GPARTICLENOTDEFINED),
30 gopts->get_variable_in_option<double>(gparticle_item, "delta_p", 0),
31 gopts->get_variable_in_option<string>(gparticle_item, "punit", "MeV"),
32 gopts->get_variable_in_option<string>(gparticle_item, "randomMomentumModel", "uniform"),
33
34 gopts->get_variable_in_option<double>(gparticle_item, "theta", 0),
35 gopts->get_variable_in_option<double>(gparticle_item, "delta_theta", 0),
36 gopts->get_variable_in_option<string>(gparticle_item, "randomThetaModel", "uniform"),
37 gopts->get_variable_in_option<double>(gparticle_item, "phi", 0),
38 gopts->get_variable_in_option<double>(gparticle_item, "delta_phi", 0),
39 gopts->get_variable_in_option<string>(gparticle_item, "aunit", "deg"),
40
41 gopts->get_variable_in_option<double>(gparticle_item, "vx", 0),
42 gopts->get_variable_in_option<double>(gparticle_item, "vy", 0),
43 gopts->get_variable_in_option<double>(gparticle_item, "vz", 0),
44
45 gopts->get_variable_in_option<double>(gparticle_item, "delta_vx", 0),
46 gopts->get_variable_in_option<double>(gparticle_item, "delta_vy", 0),
47 gopts->get_variable_in_option<double>(gparticle_item, "delta_vz", 0),
48 gopts->get_variable_in_option<string>(gparticle_item, "vunit", "cm"),
49
50 gopts->get_variable_in_option<string>(gparticle_item, "randomVertexModel", "uniform"),
51 logger
52 ));
53 }
54
55 return gparticles;
56}
57
58
59// Define the gparticle option schema and its human-readable help text.
60// The detailed API contract is documented in gparticle_options.h.
63
64 string help = "Adds a particle to the event generator \n ";
65 help += "The particle is generated with a fixed or randomized momentum, angles, and vertex. \n \n";
66 help += "Examples: \n";
67 help += "• 5 GeV electron along z: \n";
68 help += "-gparticle=\"[{pname: e-, p: 5000}]\" \n \n";
69 help += "• three particles, one electron and two protons, identical except spread in theta: \n \n";
70 help +=
71 "-gparticle=\"[{name: e-, p: 2300, theta: 23.0}, {name: proton, multiplicity: 2, p: 1200, theta: 14.0, delta_theta: 10}]\"\n";
72
73 // The variable definitions below determine:
74 // - key name as it appears in the structured option item
75 // - default value (or "no default" marker)
76 // - user-facing description used by help/usage output
77 vector<GVariable> gparticle_v = {
78 {"name", goptions::NODFLT, "Particle name (mandatory), for example \"proton\""},
79 {"multiplicity", 1, "How many copies of this particle will be generated in each event"},
80 {"p", goptions::NODFLT, "Particle momentum"},
81 {"delta_p", 0, "Particle momentum range, centered on p."},
82 {"punit", "MeV", "Geant4 Unit for the particle momentum. "},
83 {"randomMomentumModel", "uniform", "Momentum randomization. 'gaussian' (use deltas as sigmas)"},
84 {"theta", 0, "Particle polar angle. "},
85 {"delta_theta", 0, "Particle polar angle range, centered on theta. "},
86 {
87 "randomThetaModel", "uniform",
88 "Distribute cos(theta) or theta. 'cosine': cos(theta) is uniform. 'uniform': theta is uniform"
89 },
90 {"phi", 0, "Particle azimuthal angle. "},
91 {"delta_phi", 0, "Particle azimuthal angle range, centered on phi. "},
92 {"aunit", "deg", "Geant4 unit for the particle angles. "},
93 {"vx", 0, "Particle vertex x component. "},
94 {"vy", 0, "Particle vertex y component. "},
95 {"vz", 0, "Particle vertex z component. "},
96 {"delta_vx", 0, "Particle vertex range of the x component. "},
97 {"delta_vy", 0, "Particle vertex range of the y component. "},
98 {"delta_vz", 0, "Particle vertex range of the z component. "},
99 {"vunit", "cm", "Unit for the particle vertex. "},
100 {
101 "randomVertexModel", "uniform",
102 "Vertex randomization. Default: 'uniform'. Alternative: 'gaussian' (use deltas as sigmas), 'sphere'"
103 }
104 };
105
106 goptions.defineOption("gparticle", "define the generator particle(s)", gparticle_v, help);
107
108 return goptions;
109}
110} // namespace gparticle
Conventions and error codes for the gparticle module.
Public API for defining and parsing gparticle-related options.
constexpr const char * GPARTICLE_LOGGER
#define GPARTICLENOTDEFINED
Sentinel value used to mark an unset/undefined numeric parameter.
vector< GparticlePtr > getGParticles(const std::shared_ptr< GOptions > &gopts, std::shared_ptr< GLogger > &logger)
Builds the list of generator particles from structured options.
GOptions defineOptions()
Defines the structured options used by the gparticle module.
const std::string NODFLT