Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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//
27// $Id$
28//
29
30// G4ParticleGun
31#include "G4ParticleGun.hh"
32#include "G4SystemOfUnits.hh"
33#include "G4PrimaryParticle.hh"
35#include "G4Event.hh"
36#include "G4ios.hh"
37
39{
41}
42
44{
46 NumberOfParticlesToBeGenerated = numberofparticles;
47}
48
50 (G4ParticleDefinition * particleDef, G4int numberofparticles)
51{
53 NumberOfParticlesToBeGenerated = numberofparticles;
54 SetParticleDefinition( particleDef );
55}
56
58{
61 G4ThreeVector zero;
63 particle_energy = 0.0;
65 particle_position = zero;
66 particle_time = 0.0;
68 particle_charge = 0.0;
69 theMessenger = new G4ParticleGunMessenger(this);
70}
71
73{
74 delete theMessenger;
75}
76
80 "G4ParticleGun::G4ParticleGun","Event0191",FatalException,
81 "G4ParticleGun : Copy constructor should not be used."); }
82
83const G4ParticleGun& G4ParticleGun::operator=(const G4ParticleGun& right)
85 "G4ParticleGun::operator=","Event0192",FatalException,
86 "G4ParticleGun : Equal operator should not be used.");
87 return right; }
88
89G4int G4ParticleGun::operator==(const G4ParticleGun& /*right*/) const
91 "G4ParticleGun::operator==","Event0193",FatalException,
92 "G4ParticleGun : == operator should not be used.");
93 return true; }
94
95G4int G4ParticleGun::operator!=(const G4ParticleGun& /*right*/) const
97 "G4ParticleGun::operator!=","Event0193",FatalException,
98 "G4ParticleGun : != operator should not be used.");
99 return false; }
100
102 (G4ParticleDefinition * aParticleDefinition)
103{
104 if(!aParticleDefinition)
105 {
106 G4Exception("G4ParticleGun::SetParticleDefinition()","Event0101",
107 FatalException,"Null pointer is given.");
108 }
109 if(aParticleDefinition->IsShortLived())
110 {
111 if(!(aParticleDefinition->GetDecayTable()))
112 {
114 ED << "G4ParticleGun does not support shooting a short-lived particle without a valid decay table." << G4endl;
115 ED << "G4ParticleGun::SetParticleDefinition for "
116 << aParticleDefinition->GetParticleName() << " is ignored." << G4endl;
117 G4Exception("G4ParticleGun::SetParticleDefinition()","Event0102",
118 JustWarning,ED);
119 return;
120 }
121 }
122 particle_definition = aParticleDefinition;
124 if(particle_momentum>0.0)
125 {
128 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
129 }
130}
131
133{
134 particle_energy = aKineticEnergy;
135 if(particle_momentum>0.0){
137 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
138 << G4endl;
139 }else{
140 G4cout << "G4ParticleGun::" << " " << G4endl;
141 }
142 G4cout << " was defined in terms of Momentum: "
143 << particle_momentum/GeV << "GeV/c" << G4endl;
144 G4cout << " is now defined in terms of KineticEnergy: "
145 << particle_energy/GeV << "GeV" << G4endl;
146 particle_momentum = 0.0;
147 }
148}
149
151{
152 if(particle_energy>0.0){
154 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
155 << G4endl;
156 }else{
157 G4cout << "G4ParticleGun::" << " " << G4endl;
158 }
159 G4cout << " was defined in terms of KineticEnergy: "
160 << particle_energy/GeV << "GeV" << G4endl;
161 G4cout << " is now defined in terms Momentum: "
162 << aMomentum/GeV << "GeV/c" << G4endl;
163 }
165 {
166 G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
167 G4cout <<"Zero Mass is assumed"<<G4endl;
168 particle_momentum = aMomentum;
169 particle_energy = aMomentum;
170 }
171 else
172 {
174 particle_momentum = aMomentum;
176 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
177 }
178}
179
181{
182 if(particle_energy>0.0){
184 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
185 << G4endl;
186 }else{
187 G4cout << "G4ParticleGun::" << " " << G4endl;
188 }
189 G4cout << " was defined in terms of KineticEnergy: "
190 << particle_energy/GeV << "GeV" << G4endl;
191 G4cout << " is now defined in terms Momentum: "
192 << aMomentum.mag()/GeV << "GeV/c" << G4endl;
193 }
195 {
196 G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
197 G4cout <<"Zero Mass is assumed"<<G4endl;
198 particle_momentum_direction = aMomentum.unit();
199 particle_momentum = aMomentum.mag();
200 particle_energy = aMomentum.mag();
201 }
202 else
203 {
205 particle_momentum = aMomentum.mag();
206 particle_momentum_direction = aMomentum.unit();
208 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
209 }
210}
211
213{
214 if(particle_definition==0) return;
215
216 // create a new vertex
217 G4PrimaryVertex* vertex =
219
220 // create new primaries and set them to the vertex
222 for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ){
223 G4PrimaryParticle* particle =
226 particle->SetMass( mass );
228 particle->SetCharge( particle_charge );
232 vertex->SetPrimary( particle );
233 }
234
235 evt->AddPrimaryVertex( vertex );
236}
237
238
@ JustWarning
@ FatalException
G4ThreeVector G4ParticleMomentum
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cout
double z() const
Hep3Vector unit() const
double x() const
double y() const
double mag() const
void AddPrimaryVertex(G4PrimaryVertex *aPrimaryVertex)
Definition: G4Event.hh:142
G4double GetPDGCharge() const
G4DecayTable * GetDecayTable() const
const G4String & GetParticleName() const
G4double particle_momentum
virtual void SetInitialValues()
G4double particle_energy
virtual ~G4ParticleGun()
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
G4ThreeVector particle_polarization
G4ParticleMomentum particle_momentum_direction
G4int NumberOfParticlesToBeGenerated
G4double particle_charge
void SetParticleEnergy(G4double aKineticEnergy)
G4ParticleDefinition * particle_definition
virtual void GeneratePrimaryVertex(G4Event *evt)
void SetParticleMomentum(G4double aMomentum)
void SetCharge(G4double chg)
void SetPolarization(const G4ThreeVector &pol)
void SetKineticEnergy(G4double eKin)
void SetMomentumDirection(const G4ThreeVector &p)
void SetMass(G4double mas)
void SetPrimary(G4PrimaryParticle *pp)
G4ThreeVector particle_position
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76