LDMX Software
PrimaryGeneratorAction.cxx
Go to the documentation of this file.
1
8
9/*~~~~~~~~~~~~*/
10/* Geant4 */
11/*~~~~~~~~~~~~*/
12#include "G4Event.hh"
13#include "G4RunManager.hh" // Needed for CLHEP
14
15/*~~~~~~~~~~~~~*/
16/* SimCore */
17/*~~~~~~~~~~~~~*/
18#include "SimCore/G4User/UserEventInformation.h"
21
22/*~~~~~~~~~~*/
23/* ROOT */
24/*~~~~~~~~~~*/
25
26namespace simcore {
27
28PrimaryGeneratorAction::PrimaryGeneratorAction(
29 const framework::config::Parameters& parameters)
30 : G4VUserPrimaryGeneratorAction() {
31 time_shift_primaries_ = parameters.get<bool>("time_shift_primaries");
32
33 auto generators{parameters.get<std::vector<framework::config::Parameters> >(
34 "generators", {})};
35 if (generators.empty()) {
36 EXCEPTION_RAISE("MissingGenerator",
37 "Need to define some generator of primaries.");
38 }
39
40 for (auto& generator : generators) {
41 if (not PrimaryGenerator::Factory::get().make(
42 generator.get<std::string>("class_name"),
43 generator.get<std::string>("instance_name"), generator)) {
44 EXCEPTION_RAISE("UnableToCreate",
45 "Unable to create a PrimaryGenerator of type " +
46 generator.get<std::string>("class_name"));
47 }
48 }
49}
50
51void PrimaryGeneratorAction::GeneratePrimaries(G4Event* event) {
52 /*
53 * Create our Event information first so that it
54 * can be accessed by everyone from now on.
55 */
56 // Make sure we aren't overwriting a different information container
57 if (event->GetUserInformation()) {
58 EXCEPTION_RAISE(
59 "Misconfig",
60 "There was a UserEventInformation attached before beginning event."
61 "\nI don't know how this happend!!");
62 }
63
64 // Make our information container and give it to geant4
65 // G4Event owns the event information and will delete it
66 auto event_info = new UserEventInformation;
67 event->SetUserInformation(event_info);
68
69 PrimaryGenerator::Factory::get().apply([event](const auto& generator) {
70 generator->GeneratePrimaryVertex(event);
71 });
72
73 // All beam spot smearing is handled by individual generators
74 int n_pv = event->GetNumberOfPrimaryVertex();
75 if (n_pv > 0) {
76 // loop over all vertices generated
77 for (int i_pv = 0; i_pv < n_pv; ++i_pv) {
78 G4PrimaryVertex* primary_vertex = event->GetPrimaryVertex(i_pv);
79
80 if (not primary_vertex) {
81 EXCEPTION_RAISE(
82 "BadGen",
83 "One of the primary generators created a NULL primary vertex.");
84 }
85
86 // Loop over all particle associated with the primary vertex and
87 // set the generator status to 1.
88 for (int iparticle = 0; iparticle < primary_vertex->GetNumberOfParticle();
89 ++iparticle) {
90 G4PrimaryParticle* primary = primary_vertex->GetPrimary(iparticle);
91
92 if (not primary) {
93 EXCEPTION_RAISE(
94 "BadGen",
95 "One of the primary generators created a NULL primary particle.");
96 }
97
98 auto primary_info{dynamic_cast<UserPrimaryParticleInformation*>(
99 primary->GetUserInformation())};
100 if (not primary_info) {
101 // no user info defined
102 // ==> make a new one
103 primary_info = new UserPrimaryParticleInformation;
104 primary->SetUserInformation(primary_info);
105 } // check if primaryinfo is defined
106
107 int hep_status = primary_info->getHepEvtStatus();
108 if (hep_status <= 0) {
109 // undefined hepStatus ==> set to 1
110 primary_info->setHepEvtStatus(1);
111 } // check if hepStatus defined
112
113 } // iparticle - loop over primary particles from this vertex
114
115 // include the weight of this primary vertex in the event weight
116 event_info->incWeight(primary_vertex->GetWeight());
117
118 // shift so that t=0 coincides with primaries arriving at (or coming from)
119 // the target
120 if (time_shift_primaries_) {
121 primary_vertex->SetT0(primary_vertex->GetT0() +
122 primary_vertex->GetZ0() / 299.702547);
123 }
124
125 } // iPV - loop over primary vertices
126 } else {
127 EXCEPTION_RAISE(
128 "NoPrimaries",
129 "No primary vertices were produced by any of the generators.");
130 }
131}
132} // namespace simcore
Class implementing the Geant4 primary generator action.
Header file for PrimaryGenerator.
Class that provides extra information for Geant4 primary particles.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
Encapsulates user defined information associated with a Geant4 event.
Defines extra information attached to a Geant4 primary particle.
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...