ECCE @ EIC Software
Reference for
ECCE @ EIC
simulation and reconstruction software on GitHub
Home page
Related Pages
Modules
Namespaces
Classes
Files
External Links
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
G4ParticleGun.cc
Go to the documentation of this file.
Or view
the newest version in sPHENIX GitHub for file G4ParticleGun.cc
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
//
28
29
// G4ParticleGun
30
#include "
G4ParticleGun.hh
"
31
#include "
G4SystemOfUnits.hh
"
32
#include "
G4PrimaryParticle.hh
"
33
#include "
G4ParticleGunMessenger.hh
"
34
#include "
G4Event.hh
"
35
#include "
G4ios.hh
"
36
37
G4ParticleGun::G4ParticleGun
()
38
{
39
SetInitialValues
();
40
}
41
42
G4ParticleGun::G4ParticleGun
(
G4int
numberofparticles)
43
{
44
SetInitialValues
();
45
NumberOfParticlesToBeGenerated
= numberofparticles;
46
}
47
48
G4ParticleGun::G4ParticleGun
49
(
G4ParticleDefinition
* particleDef,
G4int
numberofparticles)
50
{
51
SetInitialValues();
52
NumberOfParticlesToBeGenerated = numberofparticles;
53
SetParticleDefinition( particleDef );
54
}
55
56
void
G4ParticleGun::SetInitialValues
()
57
{
58
NumberOfParticlesToBeGenerated
= 1;
59
particle_definition
=
nullptr
;
60
G4ThreeVector
zero
;
61
particle_momentum_direction
= (
G4ParticleMomentum
)zero;
62
particle_energy
= 0.0;
63
particle_momentum
= 0.0;
64
particle_position
=
zero
;
65
particle_time
= 0.0;
66
particle_polarization
=
zero
;
67
particle_charge
= 0.0;
68
theMessenger
=
new
G4ParticleGunMessenger
(
this
);
69
}
70
71
G4ParticleGun::~G4ParticleGun
()
72
{
73
delete
theMessenger
;
74
}
75
76
//G4ParticleGun::G4ParticleGun(const G4ParticleGun& /*right*/)
77
//:G4VPrimaryGenerator()
78
//{ G4Exception(
79
// "G4ParticleGun::G4ParticleGun","Event0191",FatalException,
80
// "G4ParticleGun : Copy constructor should not be used."); }
81
//
82
//const G4ParticleGun& G4ParticleGun::operator=(const G4ParticleGun& right)
83
//{ G4Exception(
84
// "G4ParticleGun::operator=","Event0192",FatalException,
85
// "G4ParticleGun : Equal operator should not be used.");
86
// return right; }
87
//
88
//G4bool G4ParticleGun::operator==(const G4ParticleGun& /*right*/) const
89
//{ G4Exception(
90
// "G4ParticleGun::operator==","Event0193",FatalException,
91
// "G4ParticleGun : == operator should not be used.");
92
// return true; }
93
//
94
//G4bool G4ParticleGun::operator!=(const G4ParticleGun& /*right*/) const
95
//{ G4Exception(
96
// "G4ParticleGun::operator!=","Event0193",FatalException,
97
// "G4ParticleGun : != operator should not be used.");
98
// return false; }
99
100
void
G4ParticleGun::SetParticleDefinition
101
(
G4ParticleDefinition
* aParticleDefinition)
102
{
103
if
(!aParticleDefinition)
104
{
105
G4Exception
(
"G4ParticleGun::SetParticleDefinition()"
,
"Event0101"
,
106
FatalException
,
"Null pointer is given."
);
107
}
108
if
(aParticleDefinition->
IsShortLived
())
109
{
110
if
(!(aParticleDefinition->
GetDecayTable
()))
111
{
112
G4ExceptionDescription
ED;
113
ED <<
"G4ParticleGun does not support shooting a short-lived particle without a valid decay table."
<<
G4endl
;
114
ED <<
"G4ParticleGun::SetParticleDefinition for "
115
<< aParticleDefinition->
GetParticleName
() <<
" is ignored."
<<
G4endl
;
116
G4Exception
(
"G4ParticleGun::SetParticleDefinition()"
,
"Event0102"
,
117
JustWarning
,ED);
118
return
;
119
}
120
}
121
particle_definition = aParticleDefinition;
122
particle_charge = particle_definition->
GetPDGCharge
();
123
if
(particle_momentum>0.0)
124
{
125
G4double
mass
= particle_definition->GetPDGMass();
126
particle_energy =
127
std::sqrt(particle_momentum*particle_momentum+mass*mass)-
mass
;
128
}
129
}
130
131
void
G4ParticleGun::SetParticleEnergy
(
G4double
aKineticEnergy)
132
{
133
particle_energy
= aKineticEnergy;
134
if
(
particle_momentum
>0.0){
135
if
(
particle_definition
){
136
G4cout
<<
"G4ParticleGun::"
<<
particle_definition
->
GetParticleName
()
137
<<
G4endl
;
138
}
else
{
139
G4cout
<<
"G4ParticleGun::"
<<
" "
<<
G4endl
;
140
}
141
G4cout
<<
" was defined in terms of Momentum: "
142
<<
particle_momentum
/
GeV
<<
"GeV/c"
<<
G4endl
;
143
G4cout
<<
" is now defined in terms of KineticEnergy: "
144
<<
particle_energy
/
GeV
<<
"GeV"
<<
G4endl
;
145
particle_momentum
= 0.0;
146
}
147
}
148
149
void
G4ParticleGun::SetParticleMomentum
(
G4double
aMomentum)
150
{
151
if
(
particle_energy
>0.0){
152
if
(
particle_definition
){
153
G4cout
<<
"G4ParticleGun::"
<<
particle_definition
->
GetParticleName
()
154
<<
G4endl
;
155
}
else
{
156
G4cout
<<
"G4ParticleGun::"
<<
" "
<<
G4endl
;
157
}
158
G4cout
<<
" was defined in terms of KineticEnergy: "
159
<<
particle_energy
/
GeV
<<
"GeV"
<<
G4endl
;
160
G4cout
<<
" is now defined in terms Momentum: "
161
<< aMomentum/
GeV
<<
"GeV/c"
<<
G4endl
;
162
}
163
if
(!
particle_definition
)
164
{
165
G4cout
<<
"Particle Definition not defined yet for G4ParticleGun"
<<
G4endl
;
166
G4cout
<<
"Zero Mass is assumed"
<<
G4endl
;
167
particle_momentum
= aMomentum;
168
particle_energy
= aMomentum;
169
}
170
else
171
{
172
G4double
mass
=
particle_definition
->
GetPDGMass
();
173
particle_momentum
= aMomentum;
174
particle_energy
=
175
std::sqrt(
particle_momentum
*
particle_momentum
+mass*mass)-
mass
;
176
}
177
}
178
179
void
G4ParticleGun::SetParticleMomentum
(
G4ParticleMomentum
aMomentum)
180
{
181
if
(
particle_energy
>0.0){
182
if
(
particle_definition
){
183
G4cout
<<
"G4ParticleGun::"
<<
particle_definition
->
GetParticleName
()
184
<<
G4endl
;
185
}
else
{
186
G4cout
<<
"G4ParticleGun::"
<<
" "
<<
G4endl
;
187
}
188
G4cout
<<
" was defined in terms of KineticEnergy: "
189
<<
particle_energy
/
GeV
<<
"GeV"
<<
G4endl
;
190
G4cout
<<
" is now defined in terms Momentum: "
191
<< aMomentum.
mag
()/
GeV
<<
"GeV/c"
<<
G4endl
;
192
}
193
if
(!
particle_definition
)
194
{
195
G4cout
<<
"Particle Definition not defined yet for G4ParticleGun"
<<
G4endl
;
196
G4cout
<<
"Zero Mass is assumed"
<<
G4endl
;
197
particle_momentum_direction
= aMomentum.
unit
();
198
particle_momentum
= aMomentum.
mag
();
199
particle_energy
= aMomentum.
mag
();
200
}
201
else
202
{
203
G4double
mass
=
particle_definition
->
GetPDGMass
();
204
particle_momentum
= aMomentum.
mag
();
205
particle_momentum_direction
= aMomentum.
unit
();
206
particle_energy
=
207
std::sqrt(
particle_momentum
*
particle_momentum
+mass*mass)-
mass
;
208
}
209
}
210
211
void
G4ParticleGun::GeneratePrimaryVertex
(
G4Event
* evt)
212
{
213
if
(!
particle_definition
)
214
{
215
G4ExceptionDescription
ED;
216
ED <<
"Particle definition is not defined."
<<
G4endl
;
217
ED <<
"G4ParticleGun::SetParticleDefinition() has to be invoked beforehand."
<<
G4endl
;
218
G4Exception
(
"G4ParticleGun::GeneratePrimaryVertex()"
,
"Event0109"
,
219
FatalException
, ED);
220
return
;
221
}
222
223
// create a new vertex
224
G4PrimaryVertex
* vertex =
225
new
G4PrimaryVertex
(
particle_position
,
particle_time
);
226
227
// create new primaries and set them to the vertex
228
G4double
mass
=
particle_definition
->
GetPDGMass
();
229
for
(
G4int
i=0; i<
NumberOfParticlesToBeGenerated
; i++ ){
230
G4PrimaryParticle
*
particle
=
231
new
G4PrimaryParticle
(
particle_definition
);
232
particle->
SetKineticEnergy
(
particle_energy
);
233
particle->
SetMass
( mass );
234
particle->
SetMomentumDirection
(
particle_momentum_direction
);
235
particle->
SetCharge
(
particle_charge
);
236
particle->
SetPolarization
(
particle_polarization
.
x
(),
237
particle_polarization
.
y
(),
238
particle_polarization
.
z
());
239
vertex->
SetPrimary
( particle );
240
}
241
242
evt->
AddPrimaryVertex
( vertex );
243
}
244
245
geant4
tree
geant4-10.6-release
source
event
src
G4ParticleGun.cc
Built by
Jin Huang
. updated:
Wed Jun 29 2022 17:25:13
using
1.8.2 with
ECCE GitHub integration