LDMX Software
HepMCPrimaryGenerator.cxx
2
3#include "G4RunManager.hh"
5
6namespace simcore {
7namespace generators {
8
10 const std::string& name, const framework::config::Parameters& parameters)
11 : PrimaryGenerator(name, parameters),
12 file_path_{parameters.get<std::string>("file_path")},
13 reader_{file_path_},
14 vertex_{parameters.get<std::vector<double>>("vertex")} {}
15
17 std::unique_ptr<simcore::hepmc::HepMCEvent> hepmc_event =
19
20 if (hepmc_event != nullptr) {
21 // Create a primary vertex for the Geant4 event
22 // This is a raw pointer, and GEANT4 will delete it
23 G4PrimaryVertex* vertex = new G4PrimaryVertex();
24 vertex->SetPosition(hepmc_event->getVertex()[0] + vertex_[0],
25 hepmc_event->getVertex()[1] + vertex_[1],
26 hepmc_event->getVertex()[2] + vertex_[2]);
27 vertex->SetT0(hepmc_event->getVertexTime());
28 vertex->SetWeight(hepmc_event->getEventWeight());
29
30 // Map to track parent-daughter relationships
31 std::map<std::shared_ptr<HepMC3::GenParticle>, G4PrimaryParticle*>
32 particle_map;
33
34 const auto& particles = hepmc_event->getParticles();
35 for (const auto& particle : particles) {
36 // Create a primary particle for Geant4
37 // This is a raw pointer, and GEANT4 will delete it
38 G4PrimaryParticle* primary = new G4PrimaryParticle();
39
40 primary->SetPDGcode(particle->getPdgId());
41
42 // Set the primary particle's momentum
43 // HepMC3 uses GeV by default, which is what Geant4 expects
44 primary->Set4Momentum(particle->getMomentum(0) * GeV, // px
45 particle->getMomentum(1) * GeV, // py
46 particle->getMomentum(2) * GeV, // pz
47 particle->getMomentum(3) * GeV // E
48 );
49
50 auto primary_info = std::make_unique<UserPrimaryParticleInformation>();
51 primary_info->setHepEvtStatus(particle->getStatus());
52 primary->SetUserInformation(primary_info.release());
53
54 // Store the particle in the map for potential parent-daughter
55 // relationships
56 particle_map[particle->getGenParticle()] = primary;
57
58 // Add the particle to the vertex
59 // In HepMC, we add all final state particles directly to the vertex
60 // since we've already filtered for final state particles
61 vertex->SetPrimary(primary);
62 }
63
64 anEvent->AddPrimaryVertex(vertex);
65
66 // Apply beam spot smearing if configured for this generator
67 if (useBeamspot()) {
68 smearBeamspot(vertex);
69 }
70
71 } else {
72 ldmx_log(error) << "Ran out of input events so run will be aborted!";
73 G4RunManager::GetRunManager()->AbortRun(true);
74 anEvent->SetEventAborted();
75 }
76}
77
78void HepMCPrimaryGenerator::RecordConfig(const std::string& id,
79 ldmx::RunHeader& rh) {
80 rh.setStringParameter(id + " Class",
81 "simcore::generators::HepMCPrimaryGenerator");
82 rh.setStringParameter(id + " HepMC File", file_path_);
83}
84
85} // namespace generators
86} // namespace simcore
87
Class for generating a Geant4 event from HepMC event data.
#define DECLARE_GENERATOR(CLASS)
@macro DECLARE_GENERATOR
Class that provides extra information for Geant4 primary particles.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:68
void setStringParameter(const std::string &name, std::string value)
Set a string parameter value.
Definition RunHeader.h:261
Interface that defines a simulation primary generator.
bool useBeamspot() const
Check if beam spot smearing is enabled for this generator.
void smearBeamspot(G4PrimaryVertex *primary_vertex)
Apply beam spot smearing to a primary vertex.
Generates a Geant4 event from a HepMCEvent.
std::vector< double > vertex_
The vertex offset to apply to the HepMC event vertex.
hepmc::HepMCReader reader_
The HepMC reader with the event data.
std::string file_path_
The file path to the HepMC file.
HepMCPrimaryGenerator(const std::string &name, const framework::config::Parameters &parameters)
Class constructor.
void GeneratePrimaryVertex(G4Event *anEvent) override
Generate vertices in the Geant4 event.
void RecordConfig(const std::string &id, ldmx::RunHeader &rh) override
Record configuration information.
std::unique_ptr< HepMCEvent > readNextEvent()
Read the next event.
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...