gparticle
Loading...
Searching...
No Matches
gparticle_lund_reader.cc
Go to the documentation of this file.
1// gparticle
4
5// geant4
6#include "G4ParticleTable.hh"
7
8// c++
9#include <cmath>
10#include <fstream>
11#include <sstream>
12#include <utility>
13#include <vector>
14
15namespace {
16constexpr size_t LUND_MIN_HEADER_COLUMNS = 10;
17constexpr size_t LUND_MAX_HEADER_COLUMNS = 100;
18constexpr size_t LUND_MIN_PARTICLE_COLUMNS = 14;
19constexpr size_t LUND_MAX_PARTICLE_COLUMNS = 100;
20constexpr int LUND_PROPAGATED_TYPE = 1;
21
22bool is_blank_line(const std::string& line) {
23 return line.find_first_not_of(" \t\r\n") == std::string::npos;
24}
25
26std::vector<double> parse_lund_header(const std::string& line) {
27 std::istringstream stream(line);
28 std::vector<double> values;
29 double value = 0;
30
31 while (stream >> value) { values.emplace_back(value); }
32 stream >> std::ws;
33 if (!stream.eof()) { values.clear(); }
34
35 return values;
36}
37
38struct LundParticleLine
39{
40 int index = 0;
41 double lifetime = 0;
42 int type = 0;
43 int pid = 0;
44 int parent = 0;
45 int daughter = 0;
46 double px = 0;
47 double py = 0;
48 double pz = 0;
49 double energy = 0;
50 double mass = 0;
51 double vx = 0;
52 double vy = 0;
53 double vz = 0;
54};
55
56bool parse_lund_particle_line(const std::string& line, LundParticleLine& particle) {
57 std::istringstream stream(line);
58 std::vector<double> values;
59 double value = 0;
60
61 while (stream >> value) { values.emplace_back(value); }
62 stream >> std::ws;
63 if (!stream.eof() ||
64 values.size() < LUND_MIN_PARTICLE_COLUMNS ||
65 values.size() > LUND_MAX_PARTICLE_COLUMNS) {
66 return false;
67 }
68
69 particle.index = static_cast<int>(values[0]);
70 particle.lifetime = values[1];
71 particle.type = static_cast<int>(values[2]);
72 particle.pid = static_cast<int>(values[3]);
73 particle.parent = static_cast<int>(values[4]);
74 particle.daughter = static_cast<int>(values[5]);
75 particle.px = values[6];
76 particle.py = values[7];
77 particle.pz = values[8];
78 particle.energy = values[9];
79 particle.mass = values[10];
80 particle.vx = values[11];
81 particle.vy = values[12];
82 particle.vz = values[13];
83
84 return particle.index == values[0] &&
85 particle.type == values[2] &&
86 particle.pid == values[3] &&
87 particle.parent == values[4] &&
88 particle.daughter == values[5];
89}
90
91std::string particle_name_from_pid(int pid, const std::shared_ptr<GLogger>& logger) {
92 auto particle_table = G4ParticleTable::GetParticleTable();
93 if (particle_table == nullptr) {
94 logger->error(ERR_GPARTICLETABLENOTFOUND, "G4ParticleTable not found while reading Lund particles");
95 return {};
96 }
97
98 auto particle_definition = particle_table->FindParticle(pid);
99 if (particle_definition == nullptr) {
100 logger->error(ERR_GPARTICLENOTFOUND, "Lund pid <", pid, "> was not found in G4ParticleTable");
101 return {};
102 }
103
104 return particle_definition->GetParticleName();
105}
106
107GparticlePtr make_gparticle_from_lund(const LundParticleLine& particle, const std::shared_ptr<GLogger>& logger) {
108 const auto particle_name = particle_name_from_pid(particle.pid, logger);
109 if (particle_name.empty()) { return nullptr; }
110
111 const double momentum = std::sqrt(
112 particle.px * particle.px +
113 particle.py * particle.py +
114 particle.pz * particle.pz
115 );
116
117 double theta = 0;
118 if (momentum > 0) { theta = std::acos(particle.pz / momentum); }
119
120 const double phi = std::atan2(particle.py, particle.px);
121
122 return std::make_shared<Gparticle>(
123 particle_name,
124 1,
125 gutilities::getG4Number(momentum, "GeV"),
126 0,
127 "uniform",
128 gutilities::getG4Number(theta, "rad"),
129 0,
130 "uniform",
131 gutilities::getG4Number(phi, "rad"),
132 0,
133 gutilities::getG4Number(particle.vx, "cm"),
134 gutilities::getG4Number(particle.vy, "cm"),
135 gutilities::getG4Number(particle.vz, "cm"),
136 0,
137 0,
138 0,
139 "uniform",
140 logger,
141 particle.type
142 );
143}
144
145GParticleRecord make_record_from_lund(const LundParticleLine& particle,
146 const std::shared_ptr<GLogger>& logger) {
147 (void)logger;
148
149 const double momentum = std::sqrt(
150 particle.px * particle.px +
151 particle.py * particle.py +
152 particle.pz * particle.pz
153 );
154
155 double theta = 0;
156 if (momentum > 0) { theta = std::acos(particle.pz / momentum); }
157
158 std::string particle_name;
159 auto particle_table = G4ParticleTable::GetParticleTable();
160 if (particle_table != nullptr) {
161 auto particle_definition = particle_table->FindParticle(particle.pid);
162 if (particle_definition != nullptr) { particle_name = particle_definition->GetParticleName(); }
163 }
164 if (particle_name.empty()) { particle_name = std::to_string(particle.pid); }
165
166 return {
167 particle_name,
168 particle.pid,
169 particle.type,
170 1,
171 momentum,
172 theta,
173 std::atan2(particle.py, particle.px),
174 particle.vx,
175 particle.vy,
176 particle.vz
177 };
178}
179
180}
181
183 const std::shared_ptr<GLogger>& logger,
184 bool propagated_only) {
185 GParticleEvents events;
186 std::ifstream input(source.filename);
187
188 if (!input.is_open()) {
189 logger->error(ERR_GPARTICLEFILEOPEN, "Could not open Lund particle file <", source.filename, ">");
190 return events;
191 }
192
193 std::string line;
194 while (std::getline(input, line)) {
195 if (is_blank_line(line)) { continue; }
196
197 const auto header_values = parse_lund_header(line);
198
199 if (header_values.size() < LUND_MIN_HEADER_COLUMNS || header_values.size() > LUND_MAX_HEADER_COLUMNS) {
200 logger->error(ERR_GPARTICLEFILEFORMAT, "Malformed Lund event header in <", source.filename, ">: ", line);
201 continue;
202 }
203
204 const auto particle_count = static_cast<int>(header_values.front());
205 if (particle_count < 0 || particle_count != header_values.front()) {
206 logger->error(ERR_GPARTICLEFILEFORMAT,
207 "Lund event header first column must be a non-negative integer in <",
208 source.filename, ">: ", line);
209 continue;
210 }
211
212 bool have_first_particle_line = false;
213 if (!std::getline(input, line)) {
214 logger->error(ERR_GPARTICLEFILEFORMAT,
215 "Lund event header must be followed by a blank line in <", source.filename, ">");
216 continue;
217 }
218 if (!is_blank_line(line)) { have_first_particle_line = true; }
219
220 GParticleEvent event_particles;
221 for (int i = 0; i < particle_count; i++) {
222 if (have_first_particle_line) {
223 have_first_particle_line = false;
224 }
225 else if (!std::getline(input, line)) {
226 logger->error(ERR_GPARTICLEFILEFORMAT,
227 "Lund event declared ", particle_count, " particles but ended after ",
228 i, " particle lines in <", source.filename, ">");
229 break;
230 }
231
232 if (is_blank_line(line)) {
233 logger->error(ERR_GPARTICLEFILEFORMAT,
234 "Unexpected blank line inside Lund particle block in <", source.filename, ">");
235 continue;
236 }
237
238 LundParticleLine lund_particle;
239 if (!parse_lund_particle_line(line, lund_particle)) {
240 logger->error(ERR_GPARTICLEFILEFORMAT, "Malformed Lund particle line in <", source.filename, ">: ", line);
241 continue;
242 }
243
244 if (lund_particle.index != i + 1) {
245 logger->error(ERR_GPARTICLEFILEFORMAT,
246 "Lund particle index must start from 1 and follow particle order in <",
247 source.filename, ">: ", line);
248 continue;
249 }
250
251 if (propagated_only && lund_particle.type != LUND_PROPAGATED_TYPE) { continue; }
252
253 auto particle = make_gparticle_from_lund(lund_particle, logger);
254 if (particle != nullptr) { event_particles.emplace_back(particle); }
255 }
256
257 events.emplace_back(std::move(event_particles));
258 }
259
260 logger->info(1, "Loaded ", events.size(), " Lund events from <", source.filename, ">");
261 return events;
262}
263
264std::vector<GparticlePtr> GParticleLundReader::loadParticles(const GParticleSourceDefinition& source,
265 const std::shared_ptr<GLogger>& logger) {
266 std::vector<GparticlePtr> particles;
267 for (const auto& event : loadParticleEvents(source, logger)) {
268 particles.insert(particles.end(), event.begin(), event.end());
269 }
270
271 logger->info(1, "Loaded ", particles.size(), " propagated particles from Lund file <", source.filename, ">");
272 return particles;
273}
274
276 const std::shared_ptr<GLogger>& logger) {
278 std::ifstream input(source.filename);
279
280 if (!input.is_open()) {
281 logger->error(ERR_GPARTICLEFILEOPEN, "Could not open Lund particle file <", source.filename, ">");
282 return events;
283 }
284
285 std::string line;
286 while (std::getline(input, line)) {
287 if (is_blank_line(line)) { continue; }
288
289 const auto header_values = parse_lund_header(line);
290 if (header_values.size() < LUND_MIN_HEADER_COLUMNS || header_values.size() > LUND_MAX_HEADER_COLUMNS) {
291 logger->error(ERR_GPARTICLEFILEFORMAT, "Malformed Lund event header in <", source.filename, ">: ", line);
292 continue;
293 }
294
295 const auto particle_count = static_cast<int>(header_values.front());
296 if (particle_count < 0 || particle_count != header_values.front()) {
297 logger->error(ERR_GPARTICLEFILEFORMAT,
298 "Lund event header first column must be a non-negative integer in <",
299 source.filename, ">: ", line);
300 continue;
301 }
302
303 bool have_first_particle_line = false;
304 if (!std::getline(input, line)) {
305 logger->error(ERR_GPARTICLEFILEFORMAT,
306 "Lund event header must be followed by a blank line in <", source.filename, ">");
307 continue;
308 }
309 if (!is_blank_line(line)) { have_first_particle_line = true; }
310
311 GParticleRecordEvent event_particles;
312 for (int i = 0; i < particle_count; i++) {
313 if (have_first_particle_line) {
314 have_first_particle_line = false;
315 }
316 else if (!std::getline(input, line)) {
317 logger->error(ERR_GPARTICLEFILEFORMAT,
318 "Lund event declared ", particle_count, " particles but ended after ",
319 i, " particle lines in <", source.filename, ">");
320 break;
321 }
322
323 if (is_blank_line(line)) {
324 logger->error(ERR_GPARTICLEFILEFORMAT,
325 "Unexpected blank line inside Lund particle block in <", source.filename, ">");
326 continue;
327 }
328
329 LundParticleLine lund_particle;
330 if (!parse_lund_particle_line(line, lund_particle)) {
331 logger->error(ERR_GPARTICLEFILEFORMAT, "Malformed Lund particle line in <", source.filename, ">: ", line);
332 continue;
333 }
334
335 if (lund_particle.index != i + 1) {
336 logger->error(ERR_GPARTICLEFILEFORMAT,
337 "Lund particle index must start from 1 and follow particle order in <",
338 source.filename, ">: ", line);
339 continue;
340 }
341
342 event_particles.emplace_back(make_record_from_lund(lund_particle, logger));
343 }
344
345 events.emplace_back(std::move(event_particles));
346 }
347
348 logger->info(1, "Loaded ", events.size(), " Lund generated-particle record events from <", source.filename, ">");
349 return events;
350}
GParticleRecordEvents loadParticleRecordEvents(const GParticleSourceDefinition &source, const std::shared_ptr< GLogger > &logger) override
Loads Lund events as generated-particle records.
GParticleEvents loadParticleEvents(const GParticleSourceDefinition &source, const std::shared_ptr< GLogger > &logger, bool propagated_only=true) override
Loads Lund events as Geant4-shootable particles.
std::vector< GparticlePtr > loadParticles(const GParticleSourceDefinition &source, const std::shared_ptr< GLogger > &logger) override
Loads all propagated Lund particles as one flattened list.
event
std::string source
int line
Conventions and error codes for the gparticle module.
#define ERR_GPARTICLEFILEFORMAT
Particle input file contained malformed or unsupported data.
#define ERR_GPARTICLEFILEOPEN
Particle input file could not be opened.
#define ERR_GPARTICLETABLENOTFOUND
G4ParticleTable could not be obtained (unexpected runtime state).
#define ERR_GPARTICLENOTFOUND
Requested particle name was not found in the G4ParticleTable.
std::vector< GParticleEvent > GParticleEvents
Sequence of file-backed generated-particle events.
std::vector< GparticlePtr > GParticleEvent
Event-local list of Geant4-propagated generator particles.
std::vector< GParticleRecordEvent > GParticleRecordEvents
Sequence of generated-particle record events indexed by event number.
std::vector< GParticleRecord > GParticleRecordEvent
Event-local list of generated-particle records for output.
std::shared_ptr< Gparticle > GparticlePtr
Shared pointer type used for Gparticle instances.
Definition gparticle.h:453
double getG4Number(const string &v, bool warnIfNotUnit=false)
Immutable generated-particle metadata used for output banks.
One configured -gparticlefile source.