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