Geant4  10.02.p02
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: G4ParticleGun.cc 97477 2016-06-03 10:13:42Z gcosmo $
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 {
52  SetInitialValues();
53  NumberOfParticlesToBeGenerated = numberofparticles;
54  SetParticleDefinition( particleDef );
55 }
56 
58 {
60  particle_definition = nullptr;
61  G4ThreeVector zero;
63  particle_energy = 0.0;
64  particle_momentum = 0.0;
65  particle_position = zero;
66  particle_time = 0.0;
67  particle_polarization = zero;
68  particle_charge = 0.0;
70 }
71 
73 {
74  delete theMessenger;
75 }
76 
77 //G4ParticleGun::G4ParticleGun(const G4ParticleGun& /*right*/)
78 //:G4VPrimaryGenerator()
79 //{ G4Exception(
80 // "G4ParticleGun::G4ParticleGun","Event0191",FatalException,
81 // "G4ParticleGun : Copy constructor should not be used."); }
82 //
83 //const G4ParticleGun& G4ParticleGun::operator=(const G4ParticleGun& right)
84 //{ G4Exception(
85 // "G4ParticleGun::operator=","Event0192",FatalException,
86 // "G4ParticleGun : Equal operator should not be used.");
87 // return right; }
88 //
89 //G4int G4ParticleGun::operator==(const G4ParticleGun& /*right*/) const
90 //{ G4Exception(
91 // "G4ParticleGun::operator==","Event0193",FatalException,
92 // "G4ParticleGun : == operator should not be used.");
93 // return true; }
94 //
95 //G4int G4ParticleGun::operator!=(const G4ParticleGun& /*right*/) const
96 //{ G4Exception(
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;
123  particle_charge = particle_definition->GetPDGCharge();
124  if(particle_momentum>0.0)
125  {
126  G4double mass = particle_definition->GetPDGMass();
127  particle_energy =
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();
207  particle_energy =
208  std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
209  }
210 }
211 
213 {
214  if(!particle_definition)
215  {
217  ED << "Particle definition is not defined." << G4endl;
218  ED << "G4ParticleGun::SetParticleDefinition() has to be invoked beforehand." << G4endl;
219  G4Exception("G4ParticleGun::GeneratePrimaryVertex()","Event0109",
220  FatalException, ED);
221  return;
222  }
223 
224  // create a new vertex
225  G4PrimaryVertex* vertex =
227 
228  // create new primaries and set them to the vertex
230  for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ){
231  G4PrimaryParticle* particle =
233  particle->SetKineticEnergy( particle_energy );
234  particle->SetMass( mass );
236  particle->SetCharge( particle_charge );
240  vertex->SetPrimary( particle );
241  }
242 
243  evt->AddPrimaryVertex( vertex );
244 }
245 
246 
void SetParticleMomentum(G4double aMomentum)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
CLHEP::Hep3Vector G4ThreeVector
void AddPrimaryVertex(G4PrimaryVertex *aPrimaryVertex)
Definition: G4Event.hh:154
G4double particle_momentum
int G4int
Definition: G4Types.hh:78
virtual void GeneratePrimaryVertex(G4Event *evt)
G4ParticleDefinition * particle_definition
const G4String & GetParticleName() const
G4ThreeVector particle_position
void SetKineticEnergy(G4double eKin)
G4DecayTable * GetDecayTable() const
G4ParticleMomentum particle_momentum_direction
G4GLOB_DLL std::ostream G4cout
virtual void SetInitialValues()
void SetMass(G4double mas)
G4double particle_charge
G4ThreeVector particle_polarization
static const double GeV
Definition: G4SIunits.hh:214
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void SetParticleEnergy(G4double aKineticEnergy)
G4int NumberOfParticlesToBeGenerated
G4double particle_energy
G4double GetPDGMass() const
G4ParticleGunMessenger * theMessenger
void SetMomentumDirection(const G4ThreeVector &p)
void SetCharge(G4double chg)
void SetPrimary(G4PrimaryParticle *pp)
virtual ~G4ParticleGun()
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4double GetPDGCharge() const
G4ThreeVector G4ParticleMomentum
void SetPolarization(const G4ThreeVector &pol)
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)