11// gemc
22#include " gparticle_options.h"
3+ #include " gparticle_reader.h"
34#include " gPrimaryGeneratorAction.h"
45
6+ // geant4
7+ #include " G4Event.hh"
8+
59
610// Build the primary-generator action, load the configured particle definitions,
711// and guarantee a valid fallback particle when no explicit configuration is present.
812GPrimaryGeneratorAction::GPrimaryGeneratorAction (std::shared_ptr<GOptions> gopts) :
913 GBase(gopts, GPRIMARYGENERATORACTION_LOGGER ),
1014 gparticleGun(std::make_unique<G4ParticleGun>()) {
11- // Load all configured particle descriptions from the shared options object.
12- gparticles = gparticle::getGParticles (gopts, log);
15+ // Inline gparticle definitions are generated for every event. File-backed
16+ // definitions are event records and are selected by Geant4 event id.
17+ gparticles = gparticle::getGParticlesFromOption (gopts, log);
18+ gparticleFileEvents = gparticle::getGParticleEventsFromSources (gopts, log);
1319
14- if (gparticles.empty ()) {
20+ if (gparticles.empty () && gparticleFileEvents. empty () ) {
1521 // Fall back to a default particle definition so the generator remains usable
1622 // even when no explicit particle configuration was provided.
1723 auto default_particle = Gparticle::create_default_gparticle (log);
@@ -30,4 +36,17 @@ void GPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
3036 gparticle->shootParticle (gparticleGun.get (), anEvent);
3137 }
3238 }
33- }
39+
40+ const auto event_id = anEvent->GetEventID ();
41+ if (event_id >= 0 && static_cast <size_t >(event_id) < gparticleFileEvents.size ()) {
42+ log->info (2 , " Generating gparticlefile event " , event_id,
43+ " with " , gparticleFileEvents[static_cast <size_t >(event_id)].size (),
44+ " propagated particles" );
45+
46+ for (const auto & gparticle : gparticleFileEvents[static_cast <size_t >(event_id)]) {
47+ if (gparticle != nullptr ) {
48+ gparticle->shootParticle (gparticleGun.get (), anEvent);
49+ }
50+ }
51+ }
52+ }
0 commit comments