LDMX Software
ParticleGun.cxx
Go to the documentation of this file.
1
9
10/*~~~~~~~~~~~~~~~~*/
11/* C++ StdLib */
12/*~~~~~~~~~~~~~~~~*/
13
14/*~~~~~~~~~~~~*/
15/* Geant4 */
16/*~~~~~~~~~~~~*/
17#include "G4Event.hh"
18#include "G4ParticleTable.hh"
19#include "G4SystemOfUnits.hh"
20#include "G4ThreeVector.hh"
21
22/*~~~~~~~~~~~~~~~*/
23/* Framework */
24/*~~~~~~~~~~~~~~~*/
25#include "Framework/Configure/Parameters.h"
26
27namespace simcore {
28namespace generators {
29
30ParticleGun::ParticleGun(const std::string& name,
31 const framework::config::Parameters& parameters)
32 : PrimaryGenerator(name, parameters) {
33 verbosity_ = parameters.get<int>("verbosity");
34
35 auto particle_table{G4ParticleTable::GetParticleTable()};
36
37 auto particle{parameters.get<std::string>("particle")};
38 if (auto particle_def{particle_table->FindParticle(particle)};
39 particle_def != 0) {
40 if (verbosity_ > 1) {
41 std::cout << "[ ParticleGun ] : Firing particle of type " << particle
42 << std::endl;
43 }
44 the_gun_.SetParticleDefinition(particle_def);
45 }
46
47 auto energy{parameters.get<double>("energy")};
48 if (verbosity_ > 1) {
49 std::cout << "[ ParticleGun ] : Setting energy to " << energy * GeV
50 << std::endl;
51 }
52 the_gun_.SetParticleEnergy(energy * GeV);
53
54 auto position{parameters.get<std::vector<double> >("position")};
55 if (!position.empty()) {
56 G4ThreeVector p_vec(position[0] * mm, position[1] * mm, position[2] * mm);
57 if (verbosity_ > 1) {
58 std::cout << "[ ParticleGun ] : position " << p_vec << std::endl;
59 }
60 the_gun_.SetParticlePosition(p_vec);
61 }
62
63 auto time{parameters.get<double>("time")};
64 if (time < 0) time = 0.0;
65 if (verbosity_ > 1) {
66 std::cout << "[ ParticleGun ] : Setting particle time to " << time
67 << std::endl;
68 }
69 the_gun_.SetParticleTime(time * ns);
70
71 auto direction{parameters.get<std::vector<double> >("direction")};
72 if (!direction.empty()) {
73 G4ThreeVector d_vec(direction[0], direction[1], direction[2]);
74 if (verbosity_ > 1) {
75 std::cout << "[ ParticleGun ] : direction " << d_vec.unit() << std::endl;
76 }
77 the_gun_.SetParticleMomentumDirection(d_vec);
78 }
79}
80
81void ParticleGun::GeneratePrimaryVertex(G4Event* event) {
82 // Call G4 class method to generate primaries.
83 the_gun_.GeneratePrimaryVertex(event);
84
85 // Apply beam spot smearing if configured for this generator
86 if (useBeamspot() && event->GetNumberOfPrimaryVertex() > 0) {
87 smearBeamspot(
88 event->GetPrimaryVertex(event->GetNumberOfPrimaryVertex() - 1));
89 }
90}
91
92void ParticleGun::RecordConfig(const std::string& id, ldmx::RunHeader& rh) {
93 rh.setStringParameter(id + " Class", "simcore::generators::ParticleGun");
94 rh.setFloatParameter(id + " Time [ns]", the_gun_.GetParticleTime());
95 rh.setFloatParameter(id + " Energy [GeV]",
96 the_gun_.GetParticleEnergy() / GeV);
97 rh.setStringParameter(id + " Particle",
98 the_gun_.GetParticleDefinition()->GetParticleName());
99 rh.setFloatParameter(id + " X [mm]", the_gun_.GetParticlePosition().x());
100 rh.setFloatParameter(id + " Y [mm]", the_gun_.GetParticlePosition().y());
101 rh.setFloatParameter(id + " Z [mm]", the_gun_.GetParticlePosition().z());
102 rh.setFloatParameter(id + " Direction X",
103 the_gun_.GetParticleMomentumDirection().x());
104 rh.setFloatParameter(id + " Direction Y",
105 the_gun_.GetParticleMomentumDirection().y());
106 rh.setFloatParameter(id + " Direction Z",
107 the_gun_.GetParticleMomentumDirection().z());
108}
109
110} // namespace generators
111} // namespace simcore
112
Extension of G4ParticleGun.
#define DECLARE_GENERATOR(CLASS)
@macro DECLARE_GENERATOR
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
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:68
void setFloatParameter(const std::string &name, float value)
Set a float parameter value.
Definition RunHeader.h:236
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.
Class that extends the functionality of G4ParticleGun.
Definition ParticleGun.h:30
int verbosity_
LDMX Verbosity for this generator.
Definition ParticleGun.h:69
ParticleGun(const std::string &name, const framework::config::Parameters &parameters)
Constructor.
G4ParticleGun the_gun_
The actual Geant4 implementation of the ParticleGun.
Definition ParticleGun.h:64
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...