Geant4  10.03.p01
 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 //
27 // $Id: G4ParticleGunMessenger.cc 98744 2016-08-09 13:21:26Z 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),
50  fIonFloatingLevelBase('\0'),fIonEnergyLevel(0)
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() )
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 /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("Set properties of ion to be generated.");
159  ionLvlCmd->SetGuidance("[usage] /gun/ionL Z A [Q I]");
160  ionLvlCmd->SetGuidance(" Z:(int) AtomicNumber");
161  ionLvlCmd->SetGuidance(" A:(int) AtomicMass");
162  ionLvlCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
163  ionLvlCmd->SetGuidance(" I:(int) Level number of metastable state (0 = ground)");
164 
165  G4UIparameter* paraml;
166  paraml = new G4UIparameter("Z",'i',false);
167  ionLvlCmd->SetParameter(paraml);
168  paraml = new G4UIparameter("A",'i',false);
169  ionLvlCmd->SetParameter(paraml);
170  paraml = new G4UIparameter("Q",'i',true);
171  paraml->SetDefaultValue(-1);
172  ionLvlCmd->SetParameter(paraml);
173  paraml = new G4UIparameter("I",'i',true);
174  paraml->SetDefaultValue("0");
175  ionLvlCmd->SetParameter(paraml);
176 
177  // set initial value to G4ParticleGun
178  fParticleGun->SetParticleDefinition( G4Geantino::Geantino() );
179  fParticleGun->SetParticleMomentumDirection( G4ThreeVector(1.0,0.0,0.0) );
180  fParticleGun->SetParticleEnergy( 1.0*GeV );
181  fParticleGun->SetParticlePosition(G4ThreeVector(0.0*cm, 0.0*cm, 0.0*cm));
182  fParticleGun->SetParticleTime( 0.0*ns );
183 }
184 
186 {
187  delete listCmd;
188  delete particleCmd;
189  delete directionCmd;
190  delete energyCmd;
191  delete momCmd;
192  delete momAmpCmd;
193  delete positionCmd;
194  delete timeCmd;
195  delete polCmd;
196  delete numberCmd;
197  delete ionCmd;
198  delete ionLvlCmd;
199  delete gunDirectory;
200 }
201 
203 {
204  if (command==listCmd) {
205  particleTable->DumpTable();
206  } else if (command==particleCmd) {
207  if (newValues =="ion") {
208  fShootIon = true;
209  } else {
210  fShootIon = false;
211  G4ParticleDefinition* pd = particleTable->FindParticle(newValues);
212  if(pd != 0)
213  { fParticleGun->SetParticleDefinition( pd ); }
214  }
215 
216  } else if( command==directionCmd )
217  { fParticleGun->SetParticleMomentumDirection(directionCmd->GetNew3VectorValue(newValues)); }
218  else if( command==energyCmd )
219  { fParticleGun->SetParticleEnergy(energyCmd->GetNewDoubleValue(newValues)); }
220  else if( command==momCmd )
221  { fParticleGun->SetParticleMomentum(momCmd->GetNew3VectorValue(newValues)); }
222  else if( command==momAmpCmd )
223  { fParticleGun->SetParticleMomentum(momAmpCmd->GetNewDoubleValue(newValues)); }
224  else if( command==positionCmd )
225  { fParticleGun->SetParticlePosition(positionCmd->GetNew3VectorValue(newValues)); }
226  else if( command==timeCmd )
227  { fParticleGun->SetParticleTime(timeCmd->GetNewDoubleValue(newValues)); }
228  else if( command==polCmd )
229  { fParticleGun->SetParticlePolarization(polCmd->GetNew3VectorValue(newValues)); }
230  else if( command==numberCmd )
231  { fParticleGun->SetNumberOfParticles(numberCmd->GetNewIntValue(newValues)); }
232  else if( command==ionCmd )
233  { IonCommand(newValues); }
234  else if( command==ionLvlCmd )
235  { IonLevelCommand(newValues); }
236 }
237 
239 {
240  G4String cv;
241 
242  if( command==directionCmd )
243  { cv = directionCmd->ConvertToString(fParticleGun->GetParticleMomentumDirection()); }
244  else if( command==particleCmd )
245  { cv = fParticleGun->GetParticleDefinition()->GetParticleName(); }
246  else if( command==energyCmd )
247  {
248  G4double ene = fParticleGun->GetParticleEnergy();
249  if(ene == 0.)
250  { G4cerr << " G4ParticleGun: was defined in terms of momentum." << G4endl; }
251  else
252  { cv = energyCmd->ConvertToString(ene,"GeV"); }
253  }
254  else if( command==momCmd || command==momAmpCmd )
255  {
256  G4double mom = fParticleGun->GetParticleMomentum();
257  if(mom == 0.)
258  { G4cerr << " G4ParticleGun: was defined in terms of kinetic energy." << G4endl; }
259  else
260  {
261  if( command==momCmd )
262  { cv = momCmd->ConvertToString(mom*(fParticleGun->GetParticleMomentumDirection()),"GeV"); }
263  else
264  { cv = momAmpCmd->ConvertToString(mom,"GeV"); }
265  }
266  }
267  else if( command==positionCmd )
268  { cv = positionCmd->ConvertToString(fParticleGun->GetParticlePosition(),"cm"); }
269  else if( command==timeCmd )
270  { cv = timeCmd->ConvertToString(fParticleGun->GetParticleTime(),"ns"); }
271  else if( command==polCmd )
272  { cv = polCmd->ConvertToString(fParticleGun->GetParticlePolarization()); }
273  else if( command==numberCmd )
274  { cv = numberCmd->ConvertToString(fParticleGun->GetNumberOfParticles()); }
275  else if( command==ionCmd )
276  {
277  if (fShootIon) {
278  cv = ItoS(fAtomicNumber) + " " + ItoS(fAtomicMass) + " ";
279  cv += ItoS(fIonCharge);
280  } else {
281  cv = "";
282  }
283  }
284  return cv;
285 }
286 
287 #include "G4IonTable.hh"
288 
289 void G4ParticleGunMessenger::IonLevelCommand(G4String newValues)
290 {
291  if (fShootIon) {
292  G4Tokenizer next( newValues );
293  // check argument
294  fAtomicNumber = StoI(next());
295  fAtomicMass = StoI(next());
296  G4String sQ = next();
297  if (sQ.isNull() || StoI(sQ)<0) {
298  fIonCharge = fAtomicNumber;
299  } else {
300  fIonCharge = StoI(sQ);
301  }
302  sQ = next();
303  if (sQ.isNull()) {
304  fIonEnergyLevel = 0;
305  } else {
306  fIonEnergyLevel = StoI(sQ);
307  }
308  G4ParticleDefinition* ion = 0;
309  ion = G4IonTable::GetIonTable()->GetIon(fAtomicNumber,fAtomicMass,fIonEnergyLevel);
310  if (ion == 0) {
311  G4cout << "Ion with Z = " << fAtomicNumber << ", A = " << fAtomicMass
312  << ", I = " << fIonEnergyLevel << " is not defined " << G4endl;
313  } else {
314  fParticleGun->SetParticleDefinition(ion);
315  fParticleGun->SetParticleCharge(fIonCharge*eplus);
316  }
317  } else {
318  G4cout << "Set /gun/particle to ion before using /gun/ion command";
319  G4cout << G4endl;
320  }
321 }
322 
323 void G4ParticleGunMessenger::IonCommand(G4String newValues)
324 {
325  if (fShootIon) {
326  G4Tokenizer next( newValues );
327  // check argument
328  fAtomicNumber = StoI(next());
329  fAtomicMass = StoI(next());
330  fIonCharge = fAtomicNumber;
331  fIonExciteEnergy = 0.0;
332  fIonFloatingLevelBase = '\0';
333  G4String sQ = next();
334  if (!(sQ.isNull()))
335  {
336  if (StoI(sQ)>=0)
337  fIonCharge = StoI(sQ);
338 
339  sQ = next();
340  if (!(sQ.isNull()))
341  {
342  fIonExciteEnergy = StoD(sQ) * keV;
343 
344  sQ = next();
345  if (sQ.isNull()||sQ=="noFloat")
346  { fIonFloatingLevelBase = '\0'; }
347  else
348  { fIonFloatingLevelBase = sQ[(size_t)0]; }
349  }
350  }
351  G4ParticleDefinition* ion = 0;
352  ion = G4IonTable::GetIonTable()->GetIon( fAtomicNumber, fAtomicMass,
353  fIonExciteEnergy, fIonFloatingLevelBase);
354  if (ion==0) {
355  G4cout << "Ion with Z=" << fAtomicNumber;
356  G4cout << " A=" << fAtomicMass << "is not defined" << G4endl;
357  } else {
358  fParticleGun->SetParticleDefinition(ion);
359  fParticleGun->SetParticleCharge(fIonCharge*eplus);
360  }
361  } else {
362  G4cout << "Set /gun/particle to ion before using /gun/ion command";
363  G4cout << G4endl;
364  }
365 }
366 
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
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
static G4int GetNewIntValue(const char *paramString)
void SetParameterCandidates(const char *theString)
void SetDefaultUnit(const char *defUnit)
G4ParticleDefinition * GetIon(G4int Z, G4int A, G4int lvl=0)
Definition: G4IonTable.cc:503
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)
void SetDefaultValue(const char *theDefaultValue)
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:372
void SetNewValue(G4UIcommand *command, G4String newValues)
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
static G4double GetNewDoubleValue(const char *paramString)
G4ThreeVector GetParticlePosition()
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
const G4String & GetParticleName() const
G4ParticleMomentum GetParticleMomentumDirection() const
G4DecayTable * GetDecayTable() const
void SetParticlePolarization(G4ThreeVector aVal)
G4ThreeVector GetParticlePolarization() const
void SetParticlePosition(G4ThreeVector aPosition)
G4GLOB_DLL std::ostream G4cout
void reset(G4bool ifSkipIon=true)
void SetParticleCharge(G4double aCharge)
static G4ThreeVector GetNew3VectorValue(const char *paramString)
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 constexpr double cm
Definition: G4SIunits.hh:119
static constexpr double eplus
Definition: G4SIunits.hh:199
G4ParticleGunMessenger(G4ParticleGun *fPtclGun)
G4int StoI(G4String s)
static G4IonTable * GetIonTable()
Definition: G4IonTable.hh:78
G4int GetNumberOfParticles() const
void SetParticleEnergy(G4double aKineticEnergy)
static G4ParticleTable * GetParticleTable()
void SetDefaultValue(const char *defVal)
G4double StoD(G4String s)
void SetDefaultUnit(const char *defUnit)
G4ParticleDefinition * GetParticleDefinition() const
static constexpr double GeV
Definition: G4SIunits.hh:217
static G4Geantino * Geantino()
Definition: G4Geantino.cc:87
void SetCandidates(const char *candidateList)
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
static constexpr double keV
Definition: G4SIunits.hh:216
G4PTblDicIterator * GetIterator() const
#define ns
Definition: xmlparse.cc:614
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)