Skip to content

Commit 5ede81f

Browse files
committed
added lund format through factory mechanism. distributing events over threads.
1 parent 8501695 commit 5ede81f

15 files changed

Lines changed: 845 additions & 11 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// actions
2+
#include "generator/gPrimaryGeneratorAction.h"
3+
4+
// gparticle
5+
#include "gparticle_reader.h"
6+
7+
// geant4
8+
#include "G4Event.hh"
9+
#include "G4RunManagerFactory.hh"
10+
#include "QBBC.hh"
11+
12+
// c++
13+
#include <cstdlib>
14+
#include <iostream>
15+
#include <memory>
16+
17+
int main(int argc, char* argv[]) {
18+
auto option_definitions = gparticle::defineOptions();
19+
option_definitions += gprimaryaction::defineOptions();
20+
21+
auto gopts = std::make_shared<GOptions>(argc, argv, option_definitions);
22+
auto log = std::make_shared<GLogger>(gopts, FUNCTION_NAME, GPRIMARYGENERATORACTION_LOGGER);
23+
24+
auto runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
25+
runManager->SetUserInitialization(new QBBC);
26+
27+
auto source_events = gparticle::getGParticleEventsFromSources(gopts, log);
28+
if (source_events.size() != 11) {
29+
std::cerr << "Expected 11 gparticlefile events, got " << source_events.size() << '\n';
30+
delete runManager;
31+
return EXIT_FAILURE;
32+
}
33+
34+
GPrimaryGeneratorAction generator(gopts);
35+
auto inline_particles = gparticle::getGParticlesFromOption(gopts, log);
36+
for (size_t event_index = 0; event_index < source_events.size(); event_index++) {
37+
G4Event event(static_cast<G4int>(event_index));
38+
generator.GeneratePrimaries(&event);
39+
40+
const auto expected_vertices = static_cast<G4int>(inline_particles.size() + source_events[event_index].size());
41+
if (event.GetNumberOfPrimaryVertex() != expected_vertices) {
42+
std::cerr << "Event " << event_index << " expected " << expected_vertices
43+
<< " primary vertices, got " << event.GetNumberOfPrimaryVertex() << '\n';
44+
delete runManager;
45+
return EXIT_FAILURE;
46+
}
47+
}
48+
49+
delete runManager;
50+
return EXIT_SUCCESS;
51+
}

actions/generator/gPrimaryGeneratorAction.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
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.
812
GPrimaryGeneratorAction::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+
}

actions/generator/gPrimaryGeneratorAction.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "goptions.h"
88
#include "gbase.h"
99
#include "gparticle_options.h"
10+
#include "gparticle_reader.h"
1011

1112
// geant4
1213
#include "G4VUserPrimaryGeneratorAction.hh"
@@ -114,4 +115,13 @@ class GPrimaryGeneratorAction : public GBase<GPrimaryGeneratorAction>, public G4
114115
* the opportunity to contribute primary content to the event.
115116
*/
116117
std::vector<GparticlePtr> gparticles;
117-
};
118+
119+
/**
120+
* \brief File-backed particle events, indexed by Geant4 event id.
121+
*
122+
* These records are kept separate from \ref gparticles so worker threads do not
123+
* replay the full file on every event. Each generated event consumes only the
124+
* matching file event record.
125+
*/
126+
GParticleEvents gparticleFileEvents;
127+
};

actions/meson.build

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
sub_dir_name = meson.current_source_dir().split('/').get(-1)
2-
internal_deps = []
2+
internal_deps = ['goptions', 'guts', 'glogging', 'gbase', 'gfactory', 'gparticle']
3+
4+
dis_file = meson.project_source_root() + '/gparticle/examples/test_dis.dat'
5+
generator_file_events = [
6+
'-gparticlefile="[{format: lund, filename: ' + dis_file + '}]"',
7+
'-gparticle="[{name: e-, p: 2300, theta: 23.0}]"',
8+
'-verbosity.generator=2'
9+
]
310

411
LD += {
512
'name' : sub_dir_name,
@@ -20,4 +27,10 @@ LD += {
2027
),
2128
'dependencies' : [yaml_cpp_dep, clhep_deps, qt6_deps, geant4_core_deps],
2229
'internal_dependencies' : internal_deps,
30+
'examples' : {
31+
'test_generator_lund_file_events' : [
32+
files('examples/generator_file_events_example.cc'),
33+
generator_file_events
34+
],
35+
}
2336
}

gfactory/gfactory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ std::shared_ptr<T> GManager::LoadAndRegisterObjectFromLibrary(std::string_view
276276
if (pluginLib && pluginLib->handle) {
277277
// The product type performs the symbol lookup and raw allocation.
278278
T* raw = T::instantiate(pluginLib->handle, gopts);
279+
if (raw == nullptr) {
280+
log->error(ERR_FACTORYNOTFOUND, "Plugin ", name, " could not instantiate its factory object.");
281+
}
279282

280283
// Standardize logger wiring on the instance.
281284
raw->set_loggers(gopts);

0 commit comments

Comments
 (0)