Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4ParticleGunMessenger.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// G4ParticleGunMessenger class implementation
27//
28// Author: Makoto Asai, 1997
29// --------------------------------------------------------------------
30
32#include "G4SystemOfUnits.hh"
33#include "G4ParticleGun.hh"
34#include "G4Geantino.hh"
35#include "G4ThreeVector.hh"
36#include "G4ParticleTable.hh"
37#include "G4IonTable.hh"
38#include "G4UIdirectory.hh"
40#include "G4UIcmdWithAString.hh"
42#include "G4UIcmdWith3Vector.hh"
45#include "G4ios.hh"
46#include "G4Tokenizer.hh"
47
49 : fParticleGun(fPtclGun)
50{
51 particleTable = G4ParticleTable::GetParticleTable();
52
53 gunDirectory = new G4UIdirectory("/gun/");
54 gunDirectory->SetGuidance("Particle Gun control commands.");
55
56 listCmd = new G4UIcmdWithoutParameter("/gun/List",this);
57 listCmd->SetGuidance("List available particles.");
58 listCmd->SetGuidance(" Invoke G4ParticleTable.");
59
60 particleCmd = new G4UIcmdWithAString("/gun/particle",this);
61 particleCmd->SetGuidance("Set particle to be generated.");
62 particleCmd->SetGuidance(" (geantino is default)");
63 particleCmd->SetGuidance(" (ion can be specified for shooting ions)");
64 particleCmd->SetParameterName("particleName",true);
65 particleCmd->SetDefaultValue("geantino");
66 G4String candidateList;
67 G4ParticleTable::G4PTblDicIterator* itr = particleTable->GetIterator();
68 itr->reset();
69 while( (*itr)() )
70 {
71 G4ParticleDefinition* pd = itr->value();
72 if( !(pd->IsShortLived()) || pd->GetDecayTable() != nullptr )
73 {
74 candidateList += pd->GetParticleName();
75 candidateList += " ";
76 }
77 }
78 candidateList += "ion ";
79 particleCmd->SetCandidates(candidateList);
80
81 directionCmd = new G4UIcmdWith3Vector("/gun/direction",this);
82 directionCmd->SetGuidance("Set momentum direction.");
83 directionCmd->SetGuidance(" Direction needs not to be a unit vector.");
84 directionCmd->SetParameterName("ex","ey","ez",true,true);
85 directionCmd->SetRange("ex != 0 || ey != 0 || ez != 0");
86
87 energyCmd = new G4UIcmdWithADoubleAndUnit("/gun/energy",this);
88 energyCmd->SetGuidance("Set kinetic energy.");
89 energyCmd->SetParameterName("Energy",true,true);
90 energyCmd->SetDefaultUnit("GeV");
91 //energyCmd->SetUnitCategory("Energy");
92 //energyCmd->SetUnitCandidates("eV keV MeV GeV TeV");
93
94 momCmd = new G4UIcmdWith3VectorAndUnit("/gun/momentum",this);
95 momCmd->SetGuidance("Set momentum. This command is equivalent to two commands");
96 momCmd->SetGuidance(" /gun/direction and /gun/momentumAmp");
97 momCmd->SetParameterName("px","py","pz",true,true);
98 momCmd->SetRange("px != 0 || py != 0 || pz != 0");
99 momCmd->SetDefaultUnit("GeV");
100
101 momAmpCmd = new G4UIcmdWithADoubleAndUnit("/gun/momentumAmp",this);
102 momAmpCmd->SetGuidance("Set absolute value of momentum.");
103 momAmpCmd->SetGuidance(" Direction should be set by /gun/direction command.");
104 momAmpCmd->SetGuidance(" This command should be used alternatively with /gun/energy.");
105 momAmpCmd->SetParameterName("Momentum",true,true);
106 momAmpCmd->SetDefaultUnit("GeV");
107
108 positionCmd = new G4UIcmdWith3VectorAndUnit("/gun/position",this);
109 positionCmd->SetGuidance("Set starting position of the particle.");
110 positionCmd->SetParameterName("X","Y","Z",true,true);
111 positionCmd->SetDefaultUnit("cm");
112 // positionCmd->SetUnitCategory("Length");
113 // positionCmd->SetUnitCandidates("microm mm cm m km");
114
115 timeCmd = new G4UIcmdWithADoubleAndUnit("/gun/time",this);
116 timeCmd->SetGuidance("Set initial time of the particle.");
117 timeCmd->SetParameterName("t0",true,true);
118 timeCmd->SetDefaultUnit("ns");
119 // timeCmd->SetUnitCategory("Time");
120 // timeCmd->SetUnitCandidates("ns ms s");
121
122 polCmd = new G4UIcmdWith3Vector("/gun/polarization",this);
123 polCmd->SetGuidance("Set polarization.");
124 polCmd->SetParameterName("Px","Py","Pz",true,true);
125 polCmd->SetRange("Px>=-1.&&Px<=1.&&Py>=-1.&&Py<=1.&&Pz>=-1.&&Pz<=1.");
126
127 numberCmd = new G4UIcmdWithAnInteger("/gun/number",this);
128 numberCmd->SetGuidance("Set number of particles to be generated.");
129 numberCmd->SetParameterName("N",true,true);
130 numberCmd->SetRange("N>0");
131
132 ionCmd = new G4UIcommand("/gun/ion",this);
133 ionCmd->SetGuidance("Set properties of ion to be generated.");
134 ionCmd->SetGuidance("[usage] /gun/ion Z A [Q E flb]");
135 ionCmd->SetGuidance(" Z:(int) AtomicNumber");
136 ionCmd->SetGuidance(" A:(int) AtomicMass");
137 ionCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
138 ionCmd->SetGuidance(" E:(double) Excitation energy (in keV)");
139 ionCmd->SetGuidance(" flb:(char) Floating level base");
140
141 G4UIparameter* param;
142 param = new G4UIparameter("Z",'i',false);
143 ionCmd->SetParameter(param);
144 param = new G4UIparameter("A",'i',false);
145 ionCmd->SetParameter(param);
146 param = new G4UIparameter("Q",'i',true);
147 param->SetDefaultValue(-1);
148 ionCmd->SetParameter(param);
149 param = new G4UIparameter("E",'d',true);
150 param->SetDefaultValue(0.0);
151 ionCmd->SetParameter(param);
152 param = new G4UIparameter("flb",'c',true);
153 param->SetDefaultValue("noFloat");
154 param->SetParameterCandidates("noFloat X Y Z U V W R S T A B C D E");
155 ionCmd->SetParameter(param);
156
157 ionLvlCmd = new G4UIcommand("/gun/ionL",this);
158 ionLvlCmd->SetGuidance("THIS COMMAND IS DEPRECATED and will be removed in future releases.");
159 ionLvlCmd->SetGuidance(" Use /gun/ion instead.");
160 ionLvlCmd->SetGuidance(" Set properties of ion to be generated.");
161 ionLvlCmd->SetGuidance(" [usage] /gun/ionL Z A [Q I]");
162 ionLvlCmd->SetGuidance(" Z:(int) AtomicNumber");
163 ionLvlCmd->SetGuidance(" A:(int) AtomicMass");
164 ionLvlCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
165 ionLvlCmd->SetGuidance(" I:(int) Level number of metastable state (0 = ground)");
166
167 G4UIparameter* paraml;
168 paraml = new G4UIparameter("Z",'i',false);
169 ionLvlCmd->SetParameter(paraml);
170 paraml = new G4UIparameter("A",'i',false);
171 ionLvlCmd->SetParameter(paraml);
172 paraml = new G4UIparameter("Q",'i',true);
173 paraml->SetDefaultValue(-1);
174 ionLvlCmd->SetParameter(paraml);
175 paraml = new G4UIparameter("I",'i',true);
176 paraml->SetDefaultValue("0");
177 ionLvlCmd->SetParameter(paraml);
178
179 // Set initial value to G4ParticleGun
180 //
182 fParticleGun->SetParticleMomentumDirection( G4ThreeVector(1.0,0.0,0.0) );
183 fParticleGun->SetParticleEnergy( 1.0*GeV );
184 fParticleGun->SetParticlePosition(G4ThreeVector(0.0*cm, 0.0*cm, 0.0*cm));
185 fParticleGun->SetParticleTime( 0.0*ns );
186}
187
189{
190 delete listCmd;
191 delete particleCmd;
192 delete directionCmd;
193 delete energyCmd;
194 delete momCmd;
195 delete momAmpCmd;
196 delete positionCmd;
197 delete timeCmd;
198 delete polCmd;
199 delete numberCmd;
200 delete ionCmd;
201 delete ionLvlCmd;
202 delete gunDirectory;
203}
204
206SetNewValue(G4UIcommand* command, G4String newValues)
207{
209 if (command==listCmd)
210 {
211 particleTable->DumpTable();
212 }
213 else if (command==particleCmd)
214 {
215 if (newValues =="ion")
216 {
217 fShootIon = true;
218 }
219 else
220 {
221 fShootIon = false;
222 G4ParticleDefinition* pd = particleTable->FindParticle(newValues);
223 if(pd != nullptr)
224 {
225 fParticleGun->SetParticleDefinition( pd );
226 }
227 else
228 {
229 ed << "Particle [" << newValues << "] is not found.";
230 command->CommandFailed(ed);
231 }
232 }
233
234 } else if( command==directionCmd )
235 { fParticleGun->SetParticleMomentumDirection(directionCmd->GetNew3VectorValue(newValues)); }
236 else if( command==energyCmd )
237 { fParticleGun->SetParticleEnergy(energyCmd->GetNewDoubleValue(newValues)); }
238 else if( command==momCmd )
239 { fParticleGun->SetParticleMomentum(momCmd->GetNew3VectorValue(newValues)); }
240 else if( command==momAmpCmd )
241 { fParticleGun->SetParticleMomentum(momAmpCmd->GetNewDoubleValue(newValues)); }
242 else if( command==positionCmd )
243 { fParticleGun->SetParticlePosition(positionCmd->GetNew3VectorValue(newValues)); }
244 else if( command==timeCmd )
245 { fParticleGun->SetParticleTime(timeCmd->GetNewDoubleValue(newValues)); }
246 else if( command==polCmd )
247 { fParticleGun->SetParticlePolarization(polCmd->GetNew3VectorValue(newValues)); }
248 else if( command==numberCmd )
249 { fParticleGun->SetNumberOfParticles(numberCmd->GetNewIntValue(newValues)); }
250 else if( command==ionCmd )
251 {
252 if (fShootIon)
253 {
254 IonCommand(newValues);
255 }
256 else
257 {
258 ed << "Set /gun/particle to ion before using /gun/ion command";
259 command->CommandFailed(ed);
260 }
261 }
262 else if( command==ionLvlCmd )
263 {
264 G4ExceptionDescription depWarn;
265 depWarn << "\nCommand /gun/ionL is deprecated and will be removed in future releases.\n"
266 << "Use /gun/ion instead.\n";
267 G4Exception("G4ParticleGunMessenger::SetNewValue", "IonLWarn",
268 JustWarning, depWarn);
269
270 if (fShootIon)
271 {
272 IonLevelCommand(newValues);
273 }
274 else
275 {
276 ed << "Set /gun/particle to ion before using /gun/ion command";
277 command->CommandFailed(ed);
278 }
279 }
280}
281
283{
284 G4String cv;
285
286 if( command==directionCmd )
287 { cv = directionCmd->ConvertToString(fParticleGun->GetParticleMomentumDirection()); }
288 else if( command==particleCmd )
289 { cv = fParticleGun->GetParticleDefinition()->GetParticleName(); }
290 else if( command==energyCmd )
291 {
292 G4double ene = fParticleGun->GetParticleEnergy();
293 if(ene == 0.)
294 { G4cerr << " G4ParticleGun: was defined in terms of momentum." << G4endl; }
295 else
296 { cv = energyCmd->ConvertToString(ene,"GeV"); }
297 }
298 else if( command==momCmd || command==momAmpCmd )
299 {
300 G4double mom = fParticleGun->GetParticleMomentum();
301 if(mom == 0.)
302 {
303 G4cerr << " G4ParticleGun: was defined in terms of kinetic energy."
304 << G4endl;
305 }
306 else
307 {
308 if( command==momCmd )
309 { cv = momCmd->ConvertToString(mom*(fParticleGun->GetParticleMomentumDirection()),"GeV"); }
310 else
311 { cv = momAmpCmd->ConvertToString(mom,"GeV"); }
312 }
313 }
314 else if( command==positionCmd )
315 { cv = positionCmd->ConvertToString(fParticleGun->GetParticlePosition(),"cm"); }
316 else if( command==timeCmd )
317 { cv = timeCmd->ConvertToString(fParticleGun->GetParticleTime(),"ns"); }
318 else if( command==polCmd )
319 { cv = polCmd->ConvertToString(fParticleGun->GetParticlePolarization()); }
320 else if( command==numberCmd )
321 { cv = numberCmd->ConvertToString(fParticleGun->GetNumberOfParticles()); }
322 else if( command==ionCmd )
323 {
324 if (fShootIon)
325 {
326 cv = ItoS(fAtomicNumber) + " " + ItoS(fAtomicMass) + " ";
327 cv += ItoS(fIonCharge);
328 }
329 else
330 {
331 cv = "";
332 }
333 }
334 return cv;
335}
336
337void G4ParticleGunMessenger::IonLevelCommand(const G4String& newValues)
338{
339 G4Tokenizer next( newValues );
340
341 // Check argument
342 //
343 fAtomicNumber = StoI(next());
344 fAtomicMass = StoI(next());
345 G4String sQ = next();
346 if (sQ.empty() || StoI(sQ)<0)
347 {
348 fIonCharge = fAtomicNumber;
349 }
350 else
351 {
352 fIonCharge = StoI(sQ);
353 }
354 sQ = next();
355 if (sQ.empty())
356 {
357 fIonEnergyLevel = 0;
358 }
359 else
360 {
361 fIonEnergyLevel = StoI(sQ);
362 }
364 ->GetIon(fAtomicNumber,fAtomicMass,fIonEnergyLevel);
365 if (ion == nullptr)
366 {
368 ed << "Ion with Z = " << fAtomicNumber << ", A = " << fAtomicMass
369 << ", I = " << fIonEnergyLevel << " is not defined ";
370 ionLvlCmd->CommandFailed(ed);
371 }
372 else
373 {
374 fParticleGun->SetParticleDefinition(ion);
375 fParticleGun->SetParticleCharge(fIonCharge*eplus);
376 }
377}
378
379void G4ParticleGunMessenger::IonCommand(const G4String& newValues)
380{
381 G4Tokenizer next( newValues );
382
383 // Check argument
384 //
385 fAtomicNumber = StoI(next());
386 fAtomicMass = StoI(next());
387 fIonCharge = fAtomicNumber;
388 fIonExciteEnergy = 0.0;
389 fIonFloatingLevelBase = '\0';
390 G4String sQ = next();
391 if (!(sQ.empty()))
392 {
393 if (StoI(sQ)>=0)
394 fIonCharge = StoI(sQ);
395
396 sQ = next();
397 if (!(sQ.empty()))
398 {
399 fIonExciteEnergy = StoD(sQ) * keV;
400
401 sQ = next();
402 if (sQ.empty()||sQ=="noFloat")
403 { fIonFloatingLevelBase = '\0'; }
404 else
405 { fIonFloatingLevelBase = sQ[(std::size_t)0]; }
406 }
407 }
409 ->GetIon(fAtomicNumber,fAtomicMass,fIonExciteEnergy,fIonFloatingLevelBase);
410 if (ion==nullptr)
411 {
413 ed << "Ion with Z=" << fAtomicNumber;
414 ed << " A=" << fAtomicMass << "is not defined";
415 ionCmd->CommandFailed(ed);
416 }
417 else
418 {
419 fParticleGun->SetParticleDefinition(ion);
420 fParticleGun->SetParticleCharge(fIonCharge*eplus);
421 }
422}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
static G4Geantino * Geantino()
Definition G4Geantino.cc:81
G4ParticleDefinition * GetIon(G4int Z, G4int A, G4int lvl=0)
static G4IonTable * GetIonTable()
G4DecayTable * GetDecayTable() const
const G4String & GetParticleName() const
void SetNewValue(G4UIcommand *command, G4String newValues) override
G4ParticleGunMessenger(G4ParticleGun *fPtclGun)
G4String GetCurrentValue(G4UIcommand *command) override
G4ThreeVector GetParticlePolarization() const
G4ParticleMomentum GetParticleMomentumDirection() const
G4double GetParticleMomentum() const
void SetNumberOfParticles(G4int i)
void SetParticlePolarization(G4ThreeVector aVal)
G4ParticleDefinition * GetParticleDefinition() const
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
G4int GetNumberOfParticles() const
void SetParticleEnergy(G4double aKineticEnergy)
void SetParticleMomentumDirection(G4ParticleMomentum aMomDirection)
void SetParticleMomentum(G4double aMomentum)
G4double GetParticleEnergy() const
void SetParticleCharge(G4double aCharge)
void reset(G4bool ifSkipIon=true)
G4PTblDicIterator * GetIterator() const
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
static G4ParticleTable * GetParticleTable()
void DumpTable(const G4String &particle_name="ALL")
void SetDefaultUnit(const char *defUnit)
static G4ThreeVector GetNew3VectorValue(const char *paramString)
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
static G4ThreeVector GetNew3VectorValue(const char *paramString)
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
void SetDefaultUnit(const char *defUnit)
static G4double GetNewDoubleValue(const char *paramString)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
void SetCandidates(const char *candidateList)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
void SetDefaultValue(const char *defVal)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
static G4int GetNewIntValue(const char *paramString)
static G4String ConvertToString(G4bool boolVal)
void SetParameter(G4UIparameter *const newParameter)
void SetGuidance(const char *aGuidance)
void CommandFailed(G4int errCode, G4ExceptionDescription &ed)
void SetRange(const char *rs)
G4String ItoS(G4int i)
G4double StoD(const G4String &s)
G4int StoI(const G4String &s)
void SetDefaultValue(const char *theDefaultValue)
void SetParameterCandidates(const char *theString)
void SetParticleTime(G4double aTime)
G4ThreeVector GetParticlePosition()
void SetParticlePosition(G4ThreeVector aPosition)