LDMX Software
simcore::g4user::PrimaryGeneratorAction Class Reference

Implementation of Geant4 primary generator action. More...

#include <PrimaryGeneratorAction.h>

Public Member Functions

 PrimaryGeneratorAction (const framework::config::Parameters &parameters)
 
virtual ~PrimaryGeneratorAction ()=default
 Class destructor.
 
void GeneratePrimaries (G4Event *event) override
 Generate primaries for the event.
 

Private Attributes

bool time_shift_primaries_ {true}
 Should we time-shift so that the primary vertices arrive (or originate) at t=0ns at z_=0mm?
 

Detailed Description

Implementation of Geant4 primary generator action.

Definition at line 35 of file PrimaryGeneratorAction.h.

Constructor & Destructor Documentation

◆ PrimaryGeneratorAction()

simcore::g4user::PrimaryGeneratorAction::PrimaryGeneratorAction ( const framework::config::Parameters & parameters)

Definition at line 29 of file PrimaryGeneratorAction.cxx.

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}
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
bool time_shift_primaries_
Should we time-shift so that the primary vertices arrive (or originate) at t=0ns at z_=0mm?

Member Function Documentation

◆ GeneratePrimaries()

void simcore::g4user::PrimaryGeneratorAction::GeneratePrimaries ( G4Event * event)
override

Generate primaries for the event.

This is called by the RunManager before being given to the EventManager to process. This means we must create the UserEventInformation here so that it is accessible for including the weights imported by the primary generators.

See also
G4RunManager::GenerateEvent for where this method is called
G4RunManager::ProcessOneEvent for where G4RunManager::GenerateEvent is called

Set UserInformation for primary vertices if they haven't been set before.

Some features downstream of the primaries require certain user info to function properly. This ensures that it happens.

Makes sure that each particle on each primary vertex has

  1. A defined UserPrimaryParticleInformation member
  2. The HepEvtStatus for this primary info is non-zero

If we are passed configuration to smear the beam spot, we smear the beam spot around the spot generated by the primary generator.

If we are configured to time-shift the primaries, we shift them so that t=0 coincides with primaries arriving at (or coming from) the target.

Parameters
eventThe Geant4 event.

Definition at line 52 of file PrimaryGeneratorAction.cxx.

52 {
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
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}

Member Data Documentation

◆ time_shift_primaries_

bool simcore::g4user::PrimaryGeneratorAction::time_shift_primaries_ {true}
private

Should we time-shift so that the primary vertices arrive (or originate) at t=0ns at z_=0mm?

Note
This should remain true unless the user knows what they are doing!

Definition at line 90 of file PrimaryGeneratorAction.h.

90{true};

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