gparticle
Loading...
Searching...
No Matches
gparticle_options.cc
Go to the documentation of this file.
1// gparticle
2#include "gparticle_options.h"
4#include "gparticle_reader.h"
5
6// gemc
7#include "gfactory_options.h"
8
9// namespace to define options
10namespace gparticle {
11using std::string;
12using std::vector;
13
14// Build a vector of configured Gparticle instances from the structured "gparticle" option node.
15// The detailed API contract is documented in gparticle_options.h.
16vector<GparticlePtr> getGParticlesFromOption(const std::shared_ptr<GOptions>& gopts, std::shared_ptr<GLogger>& logger) {
17 // Retrieve the structured option node that contains an array of particle definitions.
18 auto gparticle_node = gopts->getOptionNode("gparticle");
19
20 vector<GparticlePtr> gparticles;
21 // Reserve storage up-front to avoid reallocations (and therefore avoid extra shared_ptr moves).
22 gparticles.reserve(gparticle_node.size()); // no reallocations ⇒ no moves
23
24 // Translate each structured node item into a configured Gparticle instance.
25 for (auto gparticle_item : gparticle_node) {
26 // Each get_variable_in_option<T>(...) call:
27 // - extracts the key from this structured item
28 // - applies the provided default if the key is missing
29 // - keeps the typing explicit (string/int/double)
30 gparticles.emplace_back(std::make_shared<Gparticle>(
31 gopts->get_variable_in_option<string>(gparticle_item, "name", goptions::NODFLT),
32 gopts->get_variable_in_option<int>(gparticle_item, "multiplicity", 1),
33 gopts->get_variable_in_option<double>(gparticle_item, "p", GPARTICLENOTDEFINED),
34 gopts->get_variable_in_option<double>(gparticle_item, "delta_p", 0),
35 gopts->get_variable_in_option<string>(gparticle_item, "punit", "MeV"),
36 gopts->get_variable_in_option<string>(gparticle_item, "randomMomentumModel", "uniform"),
37
38 gopts->get_variable_in_option<double>(gparticle_item, "theta", 0),
39 gopts->get_variable_in_option<double>(gparticle_item, "delta_theta", 0),
40 gopts->get_variable_in_option<string>(gparticle_item, "randomThetaModel", "uniform"),
41 gopts->get_variable_in_option<double>(gparticle_item, "phi", 0),
42 gopts->get_variable_in_option<double>(gparticle_item, "delta_phi", 0),
43 gopts->get_variable_in_option<string>(gparticle_item, "aunit", "deg"),
44
45 gopts->get_variable_in_option<double>(gparticle_item, "vx", 0),
46 gopts->get_variable_in_option<double>(gparticle_item, "vy", 0),
47 gopts->get_variable_in_option<double>(gparticle_item, "vz", 0),
48
49 gopts->get_variable_in_option<double>(gparticle_item, "delta_vx", 0),
50 gopts->get_variable_in_option<double>(gparticle_item, "delta_vy", 0),
51 gopts->get_variable_in_option<double>(gparticle_item, "delta_vz", 0),
52 gopts->get_variable_in_option<string>(gparticle_item, "vunit", "cm"),
53
54 gopts->get_variable_in_option<string>(gparticle_item, "randomVertexModel", "uniform"),
55 logger
56 ));
57 }
58
59 return gparticles;
60}
61
62vector<GparticlePtr> getGParticles(const std::shared_ptr<GOptions>& gopts, std::shared_ptr<GLogger>& logger) {
63 auto gparticles = getGParticlesFromOption(gopts, logger);
64 auto source_particles = getGParticlesFromSources(gopts, logger);
65 gparticles.insert(gparticles.end(), source_particles.begin(), source_particles.end());
66 return gparticles;
67}
68
69
70// Define the gparticle option schema and its human-readable help text.
71// The detailed API contract is documented in gparticle_options.h.
74
75 string help = "Adds a particle to the event generator. \n ";
76 help += "The particle is generated with a fixed or randomized momentum, angles, and vertex. \n \n";
77 help += "Examples: \n";
78 help += " - 5 GeV electron along z: \n";
79 help += "-gparticle=\"[{name: e-, p: 5000}]\" \n \n";
80 help += " - one electron and two protons, with the protons spread in theta: \n \n";
81 help +=
82 "-gparticle=\"[{name: e-, p: 2300, theta: 23.0}, {name: proton, multiplicity: 2, p: 1200, theta: 14.0, delta_theta: 10}]\"\n";
83
84 // The variable definitions below determine:
85 // - key name as it appears in the structured option item
86 // - default value (or "no default" marker)
87 // - user-facing description used by help/usage output
88 vector<GVariable> gparticle_v = {
89 {"name", goptions::NODFLT, "Particle name (mandatory), for example \"proton\""},
90 {"multiplicity", 1, "How many copies of this particle will be generated in each event"},
91 {"p", goptions::NODFLT, "Particle momentum"},
92 {"delta_p", 0, "Particle momentum range, centered on p."},
93 {"punit", "MeV", "Geant4 unit for the particle momentum. "},
94 {"randomMomentumModel", "uniform", "Momentum randomization. 'gaussian' (use deltas as sigmas)"},
95 {"theta", 0, "Particle polar angle. "},
96 {"delta_theta", 0, "Particle polar angle range, centered on theta. "},
97 {
98 "randomThetaModel", "uniform",
99 "Distribute cos(theta) or theta. 'cosine': cos(theta) is uniform. 'uniform': theta is uniform"
100 },
101 {"phi", 0, "Particle azimuthal angle. "},
102 {"delta_phi", 0, "Particle azimuthal angle range, centered on phi. "},
103 {"aunit", "deg", "Geant4 unit for the particle angles. "},
104 {"vx", 0, "Particle vertex x component. "},
105 {"vy", 0, "Particle vertex y component. "},
106 {"vz", 0, "Particle vertex z component. "},
107 {"delta_vx", 0, "Particle vertex range of the x component. "},
108 {"delta_vy", 0, "Particle vertex range of the y component. "},
109 {"delta_vz", 0, "Particle vertex range of the z component. "},
110 {"vunit", "cm", "Unit for the particle vertex. "},
111 {
112 "randomVertexModel", "uniform",
113 "Vertex randomization. Default: 'uniform'. Alternative: 'gaussian' (use deltas as sigmas), 'sphere'"
114 }
115 };
116
117 goptions.defineOption("gparticle", "define the generator particle(s)", gparticle_v, help);
118
119 string file_help = "Adds particles to the event generator from particle-definition files. \n";
120 file_help += "The option is cumulative and each entry selects a reader by format and filename. \n \n";
121 file_help += "Built-in formats: \n";
122 for (const auto& format : supported_static_reader_formats()) { file_help += " - " + format + "\n"; }
123 file_help += "\n";
124 file_help += "Formats are case-insensitive. Additional formats can be provided by dynamic plugins named ";
125 file_help += "gparticle_<format>_plugin.gplugin exporting GParticleReaderFactory. \n \n";
126 file_help += "Example: \n";
127 file_help += "-gparticlefile=\"[{format: lund, filename: a.lund}]\" \n";
128
129 vector<GVariable> gparticlefile_v = {
130 {"format", goptions::NODFLT, "Particle file format, for example \"lund\""},
131 {"filename", goptions::NODFLT, "Input filename containing particle definitions"}
132 };
133
134 goptions.defineOption("gparticlefile", "define generator particles from file(s)", gparticlefile_v, file_help);
135
137
138 return goptions;
139}
140} // 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.
vector< GparticlePtr > getGParticlesFromOption(const std::shared_ptr< GOptions > &gopts, std::shared_ptr< GLogger > &logger)
Builds inline generator particles from only the -gparticle option.
GOptions defineOptions()
Defines the structured options used by the gparticle module.
GOptions defineOptions()
const std::string NODFLT
const std::vector< std::string > & supported_static_reader_formats()
Returns built-in file-reader format tokens.
std::vector< GparticlePtr > getGParticlesFromSources(const std::shared_ptr< GOptions > &gopts, std::shared_ptr< GLogger > &logger)
Loads propagated particles from all configured file sources.