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.getParameter<int>("verbosity");
35
36 auto particleTable{G4ParticleTable::GetParticleTable()};
37
38 auto particle{parameters.getParameter<std::string>("particle")};
39 if (auto particleDef{particleTable->FindParticle(particle)};
40 particleDef != 0) {
41 if (verbosity_ > 1) {
42 std::cout << "[ ParticleGun ] : Firing particle of type " << particle
43 << std::endl;
44 }
45 theGun_.SetParticleDefinition(particleDef);
46 }
47
48 auto energy{parameters.getParameter<double>("energy")};
49 if (verbosity_ > 1) {
50 std::cout << "[ ParticleGun ] : Setting energy to " << energy * GeV
51 << std::endl;
52 }
53 theGun_.SetParticleEnergy(energy * GeV);
54
55 auto position{parameters.getParameter<std::vector<double> >("position")};
56 if (!position.empty()) {
57 G4ThreeVector pVec(position[0] * mm, position[1] * mm, position[2] * mm);
58 if (verbosity_ > 1) {
59 std::cout << "[ ParticleGun ] : position " << pVec << std::endl;
60 }
61 theGun_.SetParticlePosition(pVec);
62 }
63
64 auto time{parameters.getParameter<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 theGun_.SetParticleTime(time * ns);
71
72 auto direction{parameters.getParameter<std::vector<double> >("direction")};
73 if (!direction.empty()) {
74 G4ThreeVector dVec(direction[0], direction[1], direction[2]);
75 if (verbosity_ > 1) {
76 std::cout << "[ ParticleGun ] : direction " << dVec.unit() << std::endl;
77 }
78 theGun_.SetParticleMomentumDirection(dVec);
79 }
80}
81
83 // Call G4 class method to generate primaries.
84 theGun_.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]", theGun_.GetParticleTime());
90 rh.setFloatParameter(id + " Energy [GeV]", theGun_.GetParticleEnergy() / GeV);
91 rh.setStringParameter(id + " Particle",
92 theGun_.GetParticleDefinition()->GetParticleName());
93 rh.setFloatParameter(id + " X [mm]", theGun_.GetParticlePosition().x());
94 rh.setFloatParameter(id + " Y [mm]", theGun_.GetParticlePosition().y());
95 rh.setFloatParameter(id + " Z [mm]", theGun_.GetParticlePosition().z());
96 rh.setFloatParameter(id + " Direction X",
97 theGun_.GetParticleMomentumDirection().x());
98 rh.setFloatParameter(id + " Direction Y",
99 theGun_.GetParticleMomentumDirection().y());
100 rh.setFloatParameter(id + " Direction Z",
101 theGun_.GetParticleMomentumDirection().z());
102}
103
104} // namespace generators
105} // namespace simcore
106
Extension of G4ParticleGun.
#define DECLARE_GENERATOR(CLASS)
@macro DECLARE_GENERATOR
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:27
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:54
void setFloatParameter(const std::string &name, float value)
Set a float parameter value.
Definition RunHeader.h:189
void setStringParameter(const std::string &name, std::string value)
Set a string parameter value.
Definition RunHeader.h:214
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.
void GeneratePrimaryVertex(G4Event *event) override
Generate the primary vertices in the Geant4 event.
G4ParticleGun theGun_
The actual Geant4 implementation of the ParticleGun.
Definition ParticleGun.h:64
void RecordConfig(const std::string &id, ldmx::RunHeader &rh) override
Record the configuration of the primary generator into the run header.