Geant4 11.3.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4ParticleGun.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4ParticleGun class implementation
27//
28// Author: Makoto Asai, 1997
29// --------------------------------------------------------------------
30
31#include "G4ParticleGun.hh"
32#include "G4SystemOfUnits.hh"
33#include "G4PrimaryParticle.hh"
35#include "G4Event.hh"
36#include "G4ios.hh"
37
42
44{
46 NumberOfParticlesToBeGenerated = numberofparticles;
47}
48
50 G4int numberofparticles)
51{
53 NumberOfParticlesToBeGenerated = numberofparticles;
54 SetParticleDefinition( particleDef );
55}
56
71
73{
74 delete theMessenger;
75}
76
79{
80 if(aParticleDefinition == nullptr)
81 {
82 G4Exception("G4ParticleGun::SetParticleDefinition()", "Event0101",
83 FatalException, "Null pointer is given.");
84 }
85 if(aParticleDefinition->IsShortLived())
86 {
87 if(aParticleDefinition->GetDecayTable() == nullptr)
88 {
90 ED << "G4ParticleGun does not support shooting a short-lived "
91 << "particle without a valid decay table." << G4endl;
92 ED << "G4ParticleGun::SetParticleDefinition for "
93 << aParticleDefinition->GetParticleName() << " is ignored." << G4endl;
94 G4Exception("G4ParticleGun::SetParticleDefinition()", "Event0102",
95 JustWarning, ED);
96 return;
97 }
98 }
99 particle_definition = aParticleDefinition;
100 particle_charge = particle_definition->GetPDGCharge();
101 if(particle_momentum>0.0)
102 {
103 G4double mass = particle_definition->GetPDGMass();
105 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
106 }
107}
108
110{
111 particle_energy = aKineticEnergy;
112 if(particle_momentum>0.0)
113 {
114 if(particle_definition != nullptr)
115 {
116 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
117 << G4endl;
118 }
119 else
120 {
121 G4cout << "G4ParticleGun::" << " " << G4endl;
122 }
123 G4cout << " was defined in terms of Momentum: "
124 << particle_momentum/GeV << "GeV/c" << G4endl;
125 G4cout << " is now defined in terms of KineticEnergy: "
126 << particle_energy/GeV << "GeV" << G4endl;
127 particle_momentum = 0.0;
128 }
129}
130
132{
133 if(particle_energy>0.0)
134 {
135 if(particle_definition != nullptr)
136 {
137 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
138 << G4endl;
139 }
140 else
141 {
142 G4cout << "G4ParticleGun::" << " " << G4endl;
143 }
144 G4cout << " was defined in terms of KineticEnergy: "
145 << particle_energy/GeV << "GeV" << G4endl;
146 G4cout << " is now defined in terms Momentum: "
147 << aMomentum/GeV << "GeV/c" << G4endl;
148 }
149 if(particle_definition == nullptr)
150 {
151 G4cout << "Particle Definition not defined yet for G4ParticleGun"
152 << G4endl;
153 G4cout << "Zero Mass is assumed" << G4endl;
154 particle_momentum = aMomentum;
155 particle_energy = aMomentum;
156 }
157 else
158 {
159 G4double mass = particle_definition->GetPDGMass();
160 particle_momentum = aMomentum;
162 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
163 }
164}
165
167{
168 if(particle_energy>0.0)
169 {
170 if(particle_definition != nullptr)
171 {
172 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
173 << G4endl;
174 }
175 else
176 {
177 G4cout << "G4ParticleGun::" << " " << G4endl;
178 }
179 G4cout << " was defined in terms of KineticEnergy: "
180 << particle_energy/GeV << "GeV" << G4endl;
181 G4cout << " is now defined in terms Momentum: "
182 << aMomentum.mag()/GeV << "GeV/c" << G4endl;
183 }
184 if(particle_definition == nullptr)
185 {
186 G4cout << "Particle Definition not defined yet for G4ParticleGun"
187 << G4endl;
188 G4cout << "Zero Mass is assumed" << G4endl;
189 particle_momentum_direction = aMomentum.unit();
190 particle_momentum = aMomentum.mag();
191 particle_energy = aMomentum.mag();
192 }
193 else
194 {
195 G4double mass = particle_definition->GetPDGMass();
196 particle_momentum = aMomentum.mag();
197 particle_momentum_direction = aMomentum.unit();
199 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
200 }
201}
202
204{
205 if(particle_definition == nullptr)
206 {
208 ED << "Particle definition is not defined." << G4endl;
209 ED << "G4ParticleGun::SetParticleDefinition() has to be invoked beforehand."
210 << G4endl;
211 G4Exception("G4ParticleGun::GeneratePrimaryVertex()", "Event0109",
212 FatalException, ED);
213 return;
214 }
215
216 // Create a new vertex
217 //
218 auto* vertex =
220
221 // Create new primaries and set them to the vertex
222 //
223 G4double mass = particle_definition->GetPDGMass();
224 for( G4int i=0; i<NumberOfParticlesToBeGenerated; ++i )
225 {
226 auto* particle =
228 particle->SetKineticEnergy( particle_energy );
229 particle->SetMass( mass );
230 particle->SetMomentumDirection( particle_momentum_direction );
231 particle->SetCharge( particle_charge );
232 particle->SetPolarization(particle_polarization.x(),
235 particle->SetWeight( particle_weight );
236 vertex->SetPrimary( particle );
237 }
238 evt->AddPrimaryVertex( vertex );
239}
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
G4ThreeVector G4ParticleMomentum
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
Hep3Vector unit() const
double mag() const
void AddPrimaryVertex(G4PrimaryVertex *aPrimaryVertex)
Definition G4Event.hh:129
G4DecayTable * GetDecayTable() const
const G4String & GetParticleName() const
G4double particle_momentum
virtual void SetInitialValues()
G4double particle_energy
void GeneratePrimaryVertex(G4Event *evt) override
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
G4ThreeVector particle_polarization
G4ParticleMomentum particle_momentum_direction
G4int NumberOfParticlesToBeGenerated
G4double particle_charge
~G4ParticleGun() override
void SetParticleEnergy(G4double aKineticEnergy)
G4ParticleDefinition * particle_definition
void SetParticleMomentum(G4double aMomentum)
G4double particle_weight
G4ThreeVector particle_position