LDMX Software
simcore::generators::HepMCPrimaryGenerator Class Reference

Generates a Geant4 event from a HepMCEvent. More...

#include <HepMCPrimaryGenerator.h>

Public Member Functions

 HepMCPrimaryGenerator (const std::string &name, const framework::config::Parameters &parameters)
 Class constructor.
 
virtual ~HepMCPrimaryGenerator ()=default
 Class destructor.
 
void GeneratePrimaryVertex (G4Event *anEvent) override
 Generate vertices in the Geant4 event.
 
void RecordConfig (const std::string &id, ldmx::RunHeader &rh) override
 Record configuration information.
 
- Public Member Functions inherited from simcore::PrimaryGenerator
 PrimaryGenerator (const std::string &name, const framework::config::Parameters &parameters)
 Constructor.
 
 DECLARE_FACTORY_WITH_WAREHOUSE (PrimaryGenerator, std::shared_ptr< PrimaryGenerator >, const std::string &, const framework::config::Parameters &)
 
virtual ~PrimaryGenerator ()=default
 Destructor.
 
std::string name ()
 
void smearBeamspot (G4PrimaryVertex *primary_vertex)
 Apply beam spot smearing to a primary vertex.
 
bool useBeamspot () const
 Check if beam spot smearing is enabled for this generator.
 

Private Attributes

std::string file_path_
 The file path to the HepMC file.
 
hepmc::HepMCReader reader_
 The HepMC reader with the event data.
 
std::vector< double > vertex_
 The vertex offset to apply to the HepMC event vertex.
 

Additional Inherited Members

- Protected Attributes inherited from simcore::PrimaryGenerator
std::string name_ {""}
 Name of the PrimaryGenerator.
 
bool use_beamspot_ {false}
 Flag denoting whether beam spot smearing is enabled for this generator.
 
double beamspot_x_size_ {0}
 Extent of the beamspot in x [mm].
 
double beamspot_y_size_ {0}
 Extent of the beamspot in y [mm].
 
double beamspot_z_size_ {0}
 Extent of the beamspot in z [mm].
 

Detailed Description

Generates a Geant4 event from a HepMCEvent.

Definition at line 32 of file HepMCPrimaryGenerator.h.

Constructor & Destructor Documentation

◆ HepMCPrimaryGenerator()

simcore::generators::HepMCPrimaryGenerator::HepMCPrimaryGenerator ( const std::string & name,
const framework::config::Parameters & parameters )

Class constructor.

Parameters
nameThe name of the generator.
parametersConfiguration parameters.

Definition at line 6 of file HepMCPrimaryGenerator.cxx.

8 : PrimaryGenerator(name, parameters),
9 file_path_{parameters.get<std::string>("file_path")},
11 vertex_{parameters.get<std::vector<double>>("vertex")} {}
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
PrimaryGenerator(const std::string &name, const framework::config::Parameters &parameters)
Constructor.
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.

Member Function Documentation

◆ GeneratePrimaryVertex()

void simcore::generators::HepMCPrimaryGenerator::GeneratePrimaryVertex ( G4Event * anEvent)
overridevirtual

Generate vertices in the Geant4 event.

Parameters
anEventThe Geant4 event.

Implements simcore::PrimaryGenerator.

Definition at line 13 of file HepMCPrimaryGenerator.cxx.

13 {
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}
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.
std::unique_ptr< HepMCEvent > readNextEvent()
Read the next event.

References reader_, simcore::hepmc::HepMCReader::readNextEvent(), simcore::PrimaryGenerator::smearBeamspot(), simcore::PrimaryGenerator::useBeamspot(), and vertex_.

◆ RecordConfig()

void simcore::generators::HepMCPrimaryGenerator::RecordConfig ( const std::string & id,
ldmx::RunHeader & rh )
overridevirtual

Record configuration information.

Parameters
idThe configuration ID.
rhThe run header.

Implements simcore::PrimaryGenerator.

Definition at line 75 of file HepMCPrimaryGenerator.cxx.

76 {
77 rh.setStringParameter(id + " Class",
78 "simcore::generators::HepMCPrimaryGenerator");
79 rh.setStringParameter(id + " HepMC File", file_path_);
80}
void setStringParameter(const std::string &name, std::string value)
Set a string parameter value.
Definition RunHeader.h:222

References file_path_, and ldmx::RunHeader::setStringParameter().

Member Data Documentation

◆ file_path_

std::string simcore::generators::HepMCPrimaryGenerator::file_path_
private

The file path to the HepMC file.

Definition at line 64 of file HepMCPrimaryGenerator.h.

Referenced by RecordConfig().

◆ reader_

hepmc::HepMCReader simcore::generators::HepMCPrimaryGenerator::reader_
private

The HepMC reader with the event data.

Definition at line 69 of file HepMCPrimaryGenerator.h.

Referenced by GeneratePrimaryVertex().

◆ vertex_

std::vector<double> simcore::generators::HepMCPrimaryGenerator::vertex_
private

The vertex offset to apply to the HepMC event vertex.

Definition at line 74 of file HepMCPrimaryGenerator.h.

Referenced by GeneratePrimaryVertex().


The documentation for this class was generated from the following files: