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