Geant4  10.01.p02
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 //
27 // $Id: G4ParticleGunMessenger.cc 78841 2014-01-28 08:52:40Z gcosmo $
28 //
29 
31 #include "G4SystemOfUnits.hh"
32 #include "G4ParticleGun.hh"
33 #include "G4Geantino.hh"
34 #include "G4ThreeVector.hh"
35 #include "G4ParticleTable.hh"
36 #include "G4IonTable.hh"
37 #include "G4UIdirectory.hh"
39 #include "G4UIcmdWithAString.hh"
41 #include "G4UIcmdWith3Vector.hh"
43 #include "G4UIcmdWithAnInteger.hh"
44 #include "G4ios.hh"
45 #include "G4Tokenizer.hh"
46 
48  :fParticleGun(fPtclGun),fShootIon(false),
49  fAtomicNumber(0),fAtomicMass(0),fIonCharge(0),fIonExciteEnergy(0.0),fIonEnergyLevel(0)
50 {
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;
68  itr->reset();
69  while( (*itr)() )
70  {
71  G4ParticleDefinition* pd = itr->value();
72  if( !(pd->IsShortLived()) || pd->GetDecayTable() )
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 /gun/direction and /gun/momentumAmp");
96  momCmd->SetParameterName("px","py","pz",true,true);
97  momCmd->SetRange("px != 0 || py != 0 || pz != 0");
98  momCmd->SetDefaultUnit("GeV");
99 
100  momAmpCmd = new G4UIcmdWithADoubleAndUnit("/gun/momentumAmp",this);
101  momAmpCmd->SetGuidance("Set absolute value of momentum.");
102  momAmpCmd->SetGuidance("Direction should be set by /gun/direction command.");
103  momAmpCmd->SetGuidance("This command should be used alternatively with /gun/energy.");
104  momAmpCmd->SetParameterName("Momentum",true,true);
105  momAmpCmd->SetDefaultUnit("GeV");
106 
107  positionCmd = new G4UIcmdWith3VectorAndUnit("/gun/position",this);
108  positionCmd->SetGuidance("Set starting position of the particle.");
109  positionCmd->SetParameterName("X","Y","Z",true,true);
111  //positionCmd->SetUnitCategory("Length");
112  //positionCmd->SetUnitCandidates("microm mm cm m km");
113 
114  timeCmd = new G4UIcmdWithADoubleAndUnit("/gun/time",this);
115  timeCmd->SetGuidance("Set initial time of the particle.");
116  timeCmd->SetParameterName("t0",true,true);
117  timeCmd->SetDefaultUnit("ns");
118  //timeCmd->SetUnitCategory("Time");
119  //timeCmd->SetUnitCandidates("ns ms s");
120 
121  polCmd = new G4UIcmdWith3Vector("/gun/polarization",this);
122  polCmd->SetGuidance("Set polarization.");
123  polCmd->SetParameterName("Px","Py","Pz",true,true);
124  polCmd->SetRange("Px>=-1.&&Px<=1.&&Py>=-1.&&Py<=1.&&Pz>=-1.&&Pz<=1.");
125 
126  numberCmd = new G4UIcmdWithAnInteger("/gun/number",this);
127  numberCmd->SetGuidance("Set number of particles to be generated.");
128  numberCmd->SetParameterName("N",true,true);
129  numberCmd->SetRange("N>0");
130 
131  ionCmd = new G4UIcommand("/gun/ion",this);
132  ionCmd->SetGuidance("Set properties of ion to be generated.");
133  ionCmd->SetGuidance("[usage] /gun/ion Z A Q");
134  ionCmd->SetGuidance(" Z:(int) AtomicNumber");
135  ionCmd->SetGuidance(" A:(int) AtomicMass");
136  ionCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
137  ionCmd->SetGuidance(" E:(double) Excitation energy (in keV)");
138 
139  G4UIparameter* param;
140  param = new G4UIparameter("Z",'i',false);
141  ionCmd->SetParameter(param);
142  param = new G4UIparameter("A",'i',false);
143  ionCmd->SetParameter(param);
144  param = new G4UIparameter("Q",'i',true);
145  param->SetDefaultValue(-1);
146  ionCmd->SetParameter(param);
147  param = new G4UIparameter("E",'d',true);
148  param->SetDefaultValue(0.0);
149  ionCmd->SetParameter(param);
150 
151  ionLvlCmd = new G4UIcommand("/gun/ionL",this);
152  ionLvlCmd->SetGuidance("Set properties of ion to be generated.");
153  ionLvlCmd->SetGuidance("[usage] /gun/ion Z A Q I");
154  ionLvlCmd->SetGuidance(" Z:(int) AtomicNumber");
155  ionLvlCmd->SetGuidance(" A:(int) AtomicMass");
156  ionLvlCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
157  ionLvlCmd->SetGuidance(" I:(int) Level number of metastable state (0 = ground)");
158 
159  G4UIparameter* paraml;
160  paraml = new G4UIparameter("Z",'i',false);
161  ionLvlCmd->SetParameter(paraml);
162  paraml = new G4UIparameter("A",'i',false);
163  ionLvlCmd->SetParameter(paraml);
164  paraml = new G4UIparameter("Q",'i',true);
165  paraml->SetDefaultValue(-1);
166  ionLvlCmd->SetParameter(paraml);
167  paraml = new G4UIparameter("I",'i',true);
168  paraml->SetDefaultValue("0");
169  ionLvlCmd->SetParameter(paraml);
170 
171  // set initial value to G4ParticleGun
177 }
178 
180 {
181  delete listCmd;
182  delete particleCmd;
183  delete directionCmd;
184  delete energyCmd;
185  delete momCmd;
186  delete momAmpCmd;
187  delete positionCmd;
188  delete timeCmd;
189  delete polCmd;
190  delete numberCmd;
191  delete ionCmd;
192  delete ionLvlCmd;
193  delete gunDirectory;
194 }
195 
197 {
198  if (command==listCmd) {
200  } else if (command==particleCmd) {
201  if (newValues =="ion") {
202  fShootIon = true;
203  } else {
204  fShootIon = false;
206  if(pd != 0)
208  }
209 
210  } else if( command==directionCmd )
212  else if( command==energyCmd )
214  else if( command==momCmd )
216  else if( command==momAmpCmd )
218  else if( command==positionCmd )
220  else if( command==timeCmd )
222  else if( command==polCmd )
224  else if( command==numberCmd )
226  else if( command==ionCmd )
227  { IonCommand(newValues); }
228  else if( command==ionLvlCmd )
229  { IonLevelCommand(newValues); }
230 }
231 
233 {
234  G4String cv;
235 
236  if( command==directionCmd )
238  else if( command==particleCmd )
240  else if( command==energyCmd )
241  {
243  if(ene == 0.)
244  { G4cerr << " G4ParticleGun: was defined in terms of momentum." << G4endl; }
245  else
246  { cv = energyCmd->ConvertToString(ene,"GeV"); }
247  }
248  else if( command==momCmd || command==momAmpCmd )
249  {
251  if(mom == 0.)
252  { G4cerr << " G4ParticleGun: was defined in terms of kinetic energy." << G4endl; }
253  else
254  {
255  if( command==momCmd )
257  else
258  { cv = momAmpCmd->ConvertToString(mom,"GeV"); }
259  }
260  }
261  else if( command==positionCmd )
263  else if( command==timeCmd )
265  else if( command==polCmd )
267  else if( command==numberCmd )
269  else if( command==ionCmd )
270  {
271  if (fShootIon) {
272  cv = ItoS(fAtomicNumber) + " " + ItoS(fAtomicMass) + " ";
273  cv += ItoS(fIonCharge);
274  } else {
275  cv = "";
276  }
277  }
278  return cv;
279 }
280 
281 #include "G4IonTable.hh"
282 
284 {
285  if (fShootIon) {
286  G4Tokenizer next( newValues );
287  // check argument
288  fAtomicNumber = StoI(next());
289  fAtomicMass = StoI(next());
290  G4String sQ = next();
291  if (sQ.isNull() || StoI(sQ)<0) {
293  } else {
294  fIonCharge = StoI(sQ);
295  }
296  sQ = next();
297  if (sQ.isNull()) {
298  fIonEnergyLevel = 0;
299  } else {
300  fIonEnergyLevel = StoI(sQ);
301  }
304  if (ion == 0) {
305  G4cout << "Ion with Z = " << fAtomicNumber << ", A = " << fAtomicMass
306  << ", I = " << fIonEnergyLevel << " is not defined " << G4endl;
307  } else {
310  }
311  } else {
312  G4cout << "Set /gun/particle to ion before using /gun/ion command";
313  G4cout << G4endl;
314  }
315 }
316 
318 {
319  if (fShootIon) {
320  G4Tokenizer next( newValues );
321  // check argument
322  fAtomicNumber = StoI(next());
323  fAtomicMass = StoI(next());
324  G4String sQ = next();
325  if (sQ.isNull() || StoI(sQ)<0) {
327  } else {
328  fIonCharge = StoI(sQ);
329  sQ = next();
330  if (sQ.isNull()) {
331  fIonExciteEnergy = 0.0;
332  } else {
333  fIonExciteEnergy = StoD(sQ) * keV;
334  }
335  }
338  if (ion==0) {
339  G4cout << "Ion with Z=" << fAtomicNumber;
340  G4cout << " A=" << fAtomicMass << "is not defined" << G4endl;
341  } else {
344  }
345  } else {
346  G4cout << "Set /gun/particle to ion before using /gun/ion command";
347  G4cout << G4endl;
348  }
349 }
350 
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
static const double cm
Definition: G4SIunits.hh:106
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
G4String GetCurrentValue(G4UIcommand *command)
void SetParticleMomentum(G4double aMomentum)
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
void DumpTable(const G4String &particle_name="ALL")
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
CLHEP::Hep3Vector G4ThreeVector
G4UIcmdWith3Vector * polCmd
static G4int GetNewIntValue(const char *paramString)
void SetDefaultUnit(const char *defUnit)
G4ParticleDefinition * GetIon(G4int Z, G4int A, G4int lvl=0)
Definition: G4IonTable.cc:463
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
G4double GetParticleMomentum() const
static G4ThreeVector GetNew3VectorValue(const char *paramString)
G4UIcmdWith3Vector * directionCmd
void SetDefaultValue(const char *theDefaultValue)
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:371
void SetNewValue(G4UIcommand *command, G4String newValues)
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
static G4double GetNewDoubleValue(const char *paramString)
G4ThreeVector GetParticlePosition()
void IonLevelCommand(G4String newValues)
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
const G4String & GetParticleName() const
G4UIcmdWithAString * particleCmd
G4UIcmdWithADoubleAndUnit * momAmpCmd
G4ParticleMomentum GetParticleMomentumDirection() const
G4DecayTable * GetDecayTable() const
void SetParticlePolarization(G4ThreeVector aVal)
G4ThreeVector GetParticlePolarization() const
void SetParticlePosition(G4ThreeVector aPosition)
G4UIcmdWithADoubleAndUnit * energyCmd
void IonCommand(G4String newValues)
G4GLOB_DLL std::ostream G4cout
void reset(G4bool ifSkipIon=true)
void SetParticleCharge(G4double aCharge)
static G4ThreeVector GetNew3VectorValue(const char *paramString)
G4UIcmdWith3VectorAndUnit * positionCmd
G4UIcmdWithADoubleAndUnit * timeCmd
G4String ItoS(G4int i)
void SetRange(const char *rs)
Definition: G4UIcommand.hh:125
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
void SetNumberOfParticles(G4int i)
static const double GeV
Definition: G4SIunits.hh:196
G4ParticleGunMessenger(G4ParticleGun *fPtclGun)
G4int StoI(G4String s)
static G4IonTable * GetIonTable()
Definition: G4IonTable.hh:78
G4int GetNumberOfParticles() const
void SetParticleEnergy(G4double aKineticEnergy)
G4UIcmdWith3VectorAndUnit * momCmd
static G4ParticleTable * GetParticleTable()
void SetDefaultValue(const char *defVal)
G4double StoD(G4String s)
void SetDefaultUnit(const char *defUnit)
G4ParticleDefinition * GetParticleDefinition() const
static G4Geantino * Geantino()
Definition: G4Geantino.cc:87
void SetCandidates(const char *candidateList)
#define G4endl
Definition: G4ios.hh:61
static const double keV
Definition: G4SIunits.hh:195
G4UIcmdWithAnInteger * numberCmd
double G4double
Definition: G4Types.hh:76
static const double eplus
Definition: G4SIunits.hh:178
G4UIcmdWithoutParameter * listCmd
G4PTblDicIterator * GetIterator() const
#define ns
Definition: xmlparse.cc:597
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
G4bool isNull() const
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
G4double GetParticleEnergy() const
G4GLOB_DLL std::ostream G4cerr
void SetParticleTime(G4double aTime)