actions
Loading...
Searching...
No Matches
gPrimaryGeneratorAction.cc
Go to the documentation of this file.
1// gemc
2#include "gparticle_options.h"
3#include "gparticle_reader.h"
5
6// geant4
7#include "G4Event.hh"
8
9thread_local GParticleEvent GPrimaryGeneratorAction::current_generated_particles;
10thread_local GParticleEvent GPrimaryGeneratorAction::current_generated_tracked_particles;
11thread_local GParticleRecordEvent GPrimaryGeneratorAction::current_generated_particle_records;
12thread_local GParticleRecordEvent GPrimaryGeneratorAction::current_generated_tracked_particle_records;
13
14namespace {
15GParticleRecord make_particle_record(const GparticleRuntimeRecord& particle) {
16 return {
17 particle.name,
18 particle.pid,
19 particle.type,
20 1,
21 particle.p,
22 particle.theta,
23 particle.phi,
24 particle.vertex.x(),
25 particle.vertex.y(),
26 particle.vertex.z()
27 };
28}
29
30void append_runtime_records(GParticleRecordEvent& records, const GparticlePtr& particle) {
31 if (particle == nullptr) { return; }
32 for (const auto& runtime_record : particle->getRuntimeRecords()) {
33 records.emplace_back(make_particle_record(runtime_record));
34 }
35}
36
37void append_untracked_file_records(GParticleRecordEvent& records, const GParticleRecordEvent& source_records) {
38 for (const auto& record : source_records) {
39 if (record.type != 1) {
40 records.emplace_back(record);
41 }
42 }
43}
44}
45
46// Build the primary-generator action, load the configured particle definitions,
47// and guarantee a valid fallback particle when no explicit configuration is present.
48GPrimaryGeneratorAction::GPrimaryGeneratorAction(std::shared_ptr<GOptions> gopts) :
50 gparticleGun(std::make_unique<G4ParticleGun>()),
51 gparticles(std::make_shared<std::vector<GparticlePtr>>(
52 gparticle::getGParticlesFromOption(gopts, log))) {
53 gparticleFileEvents = gparticle::getGParticleEventsFromSources(gopts, log);
54 allGparticleFileRecordEvents = gparticle::getGParticleRecordEventsFromSources(gopts, log);
55
56 if (gparticles->empty() && allGparticleFileRecordEvents.empty()) {
57 auto default_particle = Gparticle::create_default_gparticle(log);
58 log->info(1, "No gparticle was defined. Creating default:", *default_particle);
59 gparticles->emplace_back(default_particle);
60 }
61}
62
64 std::shared_ptr<std::vector<GparticlePtr>> particles) :
66 gparticleGun(std::make_unique<G4ParticleGun>()),
67 gparticles(std::move(particles)) {
68 gparticleFileEvents = gparticle::getGParticleEventsFromSources(gopts, log);
69 allGparticleFileRecordEvents = gparticle::getGParticleRecordEventsFromSources(gopts, log);
70
71 if (gparticles->empty() && allGparticleFileRecordEvents.empty()) {
72 auto default_particle = Gparticle::create_default_gparticle(log);
73 log->info(1, "No gparticle was defined. Creating default:", *default_particle);
74 gparticles->emplace_back(default_particle);
75 }
76}
77
78
79// For each configured particle definition, configure the shared particle gun and
80// inject the corresponding primary information into the current event.
82 current_generated_particles.clear();
83 current_generated_tracked_particles.clear();
84 current_generated_particle_records.clear();
85 current_generated_tracked_particle_records.clear();
86
87 current_generated_particles.insert(current_generated_particles.end(), gparticles->begin(), gparticles->end());
88 current_generated_tracked_particles.insert(current_generated_tracked_particles.end(),
89 gparticles->begin(),
90 gparticles->end());
91
92 const auto event_id = anEvent->GetEventID();
93 if (event_id >= 0 && static_cast<size_t>(event_id) < allGparticleFileRecordEvents.size()) {
94 const auto& event_particles = allGparticleFileRecordEvents[static_cast<size_t>(event_id)];
95 append_untracked_file_records(current_generated_particle_records, event_particles);
96 }
97 if (event_id >= 0 && static_cast<size_t>(event_id) < gparticleFileEvents.size()) {
98 const auto& event_particles = gparticleFileEvents[static_cast<size_t>(event_id)];
99 current_generated_tracked_particles.insert(current_generated_tracked_particles.end(),
100 event_particles.begin(),
101 event_particles.end());
102 }
103
104 for (const auto& gparticle : *gparticles) {
105 if (gparticle != nullptr) {
106 gparticle->shootParticle(gparticleGun.get(), anEvent);
107 append_runtime_records(current_generated_particle_records, gparticle);
108 append_runtime_records(current_generated_tracked_particle_records, gparticle);
109 }
110 }
111
112 if (event_id >= 0 && static_cast<size_t>(event_id) < gparticleFileEvents.size()) {
113 log->info(2, "Generating gparticlefile event ", event_id,
114 " with ", gparticleFileEvents[static_cast<size_t>(event_id)].size(),
115 " propagated particles");
116
117 for (const auto& gparticle : gparticleFileEvents[static_cast<size_t>(event_id)]) {
118 if (gparticle != nullptr) {
119 gparticle->shootParticle(gparticleGun.get(), anEvent);
120 append_runtime_records(current_generated_particle_records, gparticle);
121 append_runtime_records(current_generated_tracked_particle_records, gparticle);
122 }
123 }
124 }
125}
126
128 return current_generated_particles;
129}
130
132 return current_generated_tracked_particles;
133}
134
136 return current_generated_particle_records;
137}
138
140 return current_generated_tracked_particle_records;
141}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
static const GParticleRecordEvent & currentGeneratedParticleRecords()
Returns the current event's full generated-particle records.
static const GParticleEvent & currentGeneratedParticles()
Returns the current event's Geant4-propagated generated particles.
static const GParticleEvent & currentGeneratedTrackedParticles()
Returns the current event's Geant4-tracked generated particles.
static const GParticleRecordEvent & currentGeneratedTrackedParticleRecords()
Returns the current event's Geant4-tracked generated-particle records.
void GeneratePrimaries(G4Event *event) override
Generates the primary content for one event.
GPrimaryGeneratorAction(std::shared_ptr< GOptions > gopts)
Constructs the primary-generator action.
static std::shared_ptr< Gparticle > create_default_gparticle(const std::shared_ptr< GLogger > &log)
Declares GPrimaryGeneratorAction, the primary-particle generation action for the GEMC actions module.
constexpr const char * GPRIMARYGENERATORACTION_LOGGER
std::vector< GparticlePtr > GParticleEvent
std::vector< GParticleRecord > GParticleRecordEvent
std::shared_ptr< Gparticle > GparticlePtr
GParticleRecordEvents getGParticleRecordEventsFromSources(const std::shared_ptr< GOptions > &gopts, std::shared_ptr< GLogger > &logger)
GParticleEvents getGParticleEventsFromSources(const std::shared_ptr< GOptions > &gopts, std::shared_ptr< GLogger > &logger, bool propagated_only=true)
G4ThreeVector vertex