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