LDMX Software
LHEPrimaryGenerator.cxx
1#include "SimCore/Generators/LHEPrimaryGenerator.h"
2
3#include "Framework/Exception/Exception.h"
4#include "G4IonTable.hh"
5#include "G4RunManager.hh"
7
8namespace simcore {
9namespace generators {
10
12 const std::string& name, const framework::config::Parameters& parameters)
13 : PrimaryGenerator(name, parameters),
14 file_path_{parameters.get<std::string>("file_path")},
15 reader_{file_path_},
16 vertex_{parameters.get<std::vector<double>>("vertex")} {}
17
19 std::unique_ptr<simcore::lhe::LHEEvent> lhe_event = reader_.readNextEvent();
20
21 if (lhe_event != nullptr) {
22 // Create a primary vertex for the Geant4 event
23 // This is a raw pointer, and GEANT4 will delete it
24 G4PrimaryVertex* vertex = new G4PrimaryVertex();
25 vertex->SetPosition(lhe_event->getVertex()[0] + vertex_[0],
26 lhe_event->getVertex()[1] + vertex_[1],
27 lhe_event->getVertex()[2] + vertex_[2]);
28 vertex->SetWeight(lhe_event->getEventWeight());
29
30 std::map<simcore::lhe::LHEParticle*, G4PrimaryParticle*> particle_map;
31
32 const auto& particles = lhe_event->getParticles();
33 for (const auto& particle : particles) {
34 // Check if the particle has a valid, outgoing particle status
35 if (particle->getStatus() > 0) {
36 // Create a primary particle for the Geant4
37 // This is a raw pointer, and GEANT4 will delete it
38 G4PrimaryParticle* primary = new G4PrimaryParticle();
39 // Tungsten ion in the LHE files
40 // TODO: can this never be +623?
41 if (particle->getPdgId() == -623) {
42 G4ParticleDefinition* tungsten_ion_def =
43 G4IonTable::GetIonTable()->GetIon(74, 184, 0.);
44 if (tungsten_ion_def != nullptr) {
45 primary->SetParticleDefinition(tungsten_ion_def);
46 } else {
47 EXCEPTION_RAISE("EventGenerator",
48 "Failed to find particle definition for W ion.");
49 }
50 } else {
51 primary->SetPDGcode(particle->getPdgId());
52 }
53
54 // Set the primary particle's momentum and lifetime
55 primary->Set4Momentum(
56 particle->getMomentum(0) * GeV, particle->getMomentum(1) * GeV,
57 particle->getMomentum(2) * GeV, particle->getMomentum(3) * GeV);
58 primary->SetProperTime(particle->getLifetime() * nanosecond);
59
60 auto primary_info = std::make_unique<UserPrimaryParticleInformation>();
61 primary_info->setHepEvtStatus(particle->getStatus());
62 primary->SetUserInformation(primary_info.release());
63
64 particle_map[particle.get()] = primary;
65
66 /*
67 * Assign primary as daughter but only if the mother is not a DOC
68 * particle.
69 */
70 if (particle->getMotherParticle(0) != nullptr &&
71 particle->getMotherParticle(0)->getStatus() > 0) {
72 G4PrimaryParticle* primary_mom =
73 particle_map[particle->getMotherParticle(0)];
74 if (primary_mom != nullptr) {
75 primary_mom->SetDaughter(primary);
76 }
77 } else {
78 vertex->SetPrimary(primary);
79 }
80 } // end condition for valid, outgoing particle status
81 }
82
83 anEvent->AddPrimaryVertex(vertex);
84
85 // Apply beam spot smearing if configured for this generator
86 if (useBeamspot()) {
87 smearBeamspot(vertex);
88 }
89
90 } else {
91 ldmx_log(error) << "Ran out of input events so run will be aborted!";
92 G4RunManager::GetRunManager()->AbortRun(true);
93 anEvent->SetEventAborted();
94 }
95}
96
97void LHEPrimaryGenerator::RecordConfig(const std::string& id,
98 ldmx::RunHeader& rh) {
99 rh.setStringParameter(id + " Class",
100 "simcore::generators::LHEPrimaryGenerator");
101 rh.setStringParameter(id + " LHE File", file_path_);
102}
103
104} // namespace generators
105} // namespace simcore
106
#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 an LHEEvent.
LHEPrimaryGenerator(const std::string &name, const framework::config::Parameters &parameters)
Class constructor.
std::vector< double > vertex_
The vertex offset to apply to the LHE event vertex.
void RecordConfig(const std::string &id, ldmx::RunHeader &rh) override
Record configuration information.
lhe::LHEReader reader_
The LHE reader with the event data.
void GeneratePrimaryVertex(G4Event *anEvent) override
Generate vertices in the Geant4 event.
std::string file_path_
The file path to the LHE file.
std::unique_ptr< LHEEvent > readNextEvent()
Read the next event.
Definition LHEReader.cxx:17
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...