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