ECCE @ EIC Software
 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"
34 #include "G4Event.hh"
35 #include "G4ios.hh"
36 
38 {
40 }
41 
43 {
45  NumberOfParticlesToBeGenerated = numberofparticles;
46 }
47 
49  (G4ParticleDefinition * particleDef, G4int numberofparticles)
50 {
51  SetInitialValues();
52  NumberOfParticlesToBeGenerated = numberofparticles;
53  SetParticleDefinition( particleDef );
54 }
55 
57 {
59  particle_definition = nullptr;
62  particle_energy = 0.0;
63  particle_momentum = 0.0;
65  particle_time = 0.0;
67  particle_charge = 0.0;
69 }
70 
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 
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  {
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 
132 {
133  particle_energy = aKineticEnergy;
134  if(particle_momentum>0.0){
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 
150 {
151  if(particle_energy>0.0){
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  }
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  {
173  particle_momentum = aMomentum;
175  std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
176  }
177 }
178 
180 {
181  if(particle_energy>0.0){
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  }
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  {
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 
212 {
213  if(!particle_definition)
214  {
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 =
226 
227  // create new primaries and set them to the vertex
229  for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ){
232  particle->SetKineticEnergy( particle_energy );
233  particle->SetMass( mass );
235  particle->SetCharge( particle_charge );
239  vertex->SetPrimary( particle );
240  }
241 
242  evt->AddPrimaryVertex( vertex );
243 }
244 
245