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