Geant4_10
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 73713 2013-09-09 10:00:06Z 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 {
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() )
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);
110  positionCmd->SetDefaultUnit("cm");
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  param->SetDefaultValue("1");
142  ionCmd->SetParameter(param);
143  param = new G4UIparameter("A",'i',false);
144  param->SetDefaultValue("1");
145  ionCmd->SetParameter(param);
146  param = new G4UIparameter("Q",'i',true);
147  param->SetDefaultValue("0");
148  ionCmd->SetParameter(param);
149  param = new G4UIparameter("E",'d',true);
150  param->SetDefaultValue("0.0");
151  ionCmd->SetParameter(param);
152 
153  ionLvlCmd = new G4UIcommand("/gun/ionL",this);
154  ionLvlCmd->SetGuidance("Set properties of ion to be generated.");
155  ionLvlCmd->SetGuidance("[usage] /gun/ion Z A Q I");
156  ionLvlCmd->SetGuidance(" Z:(int) AtomicNumber");
157  ionLvlCmd->SetGuidance(" A:(int) AtomicMass");
158  ionLvlCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
159  ionLvlCmd->SetGuidance(" I:(int) Level number of metastable state (0 = ground)");
160 
161  G4UIparameter* paraml;
162  paraml = new G4UIparameter("Z",'i',false);
163  paraml->SetDefaultValue("1");
164  ionLvlCmd->SetParameter(paraml);
165  paraml = new G4UIparameter("A",'i',false);
166  paraml->SetDefaultValue("1");
167  ionLvlCmd->SetParameter(paraml);
168  paraml = new G4UIparameter("Q",'i',true);
169  paraml->SetDefaultValue("0");
170  ionLvlCmd->SetParameter(paraml);
171  paraml = new G4UIparameter("I",'i',true);
172  paraml->SetDefaultValue("0");
173  ionLvlCmd->SetParameter(paraml);
174 
175  // set initial value to G4ParticleGun
176  fParticleGun->SetParticleDefinition( G4Geantino::Geantino() );
177  fParticleGun->SetParticleMomentumDirection( G4ThreeVector(1.0,0.0,0.0) );
178  fParticleGun->SetParticleEnergy( 1.0*GeV );
179  fParticleGun->SetParticlePosition(G4ThreeVector(0.0*cm, 0.0*cm, 0.0*cm));
180  fParticleGun->SetParticleTime( 0.0*ns );
181 }
182 
184 {
185  delete listCmd;
186  delete particleCmd;
187  delete directionCmd;
188  delete energyCmd;
189  delete momCmd;
190  delete momAmpCmd;
191  delete positionCmd;
192  delete timeCmd;
193  delete polCmd;
194  delete numberCmd;
195  delete ionCmd;
196  delete ionLvlCmd;
197  delete gunDirectory;
198 }
199 
201 {
202  if (command==listCmd) {
203  particleTable->DumpTable();
204  } else if (command==particleCmd) {
205  if (newValues =="ion") {
206  fShootIon = true;
207  } else {
208  fShootIon = false;
209  G4ParticleDefinition* pd = particleTable->FindParticle(newValues);
210  if(pd != 0)
211  { fParticleGun->SetParticleDefinition( pd ); }
212  }
213 
214  } else if( command==directionCmd )
215  { fParticleGun->SetParticleMomentumDirection(directionCmd->GetNew3VectorValue(newValues)); }
216  else if( command==energyCmd )
217  { fParticleGun->SetParticleEnergy(energyCmd->GetNewDoubleValue(newValues)); }
218  else if( command==momCmd )
219  { fParticleGun->SetParticleMomentum(momCmd->GetNew3VectorValue(newValues)); }
220  else if( command==momAmpCmd )
221  { fParticleGun->SetParticleMomentum(momAmpCmd->GetNewDoubleValue(newValues)); }
222  else if( command==positionCmd )
223  { fParticleGun->SetParticlePosition(positionCmd->GetNew3VectorValue(newValues)); }
224  else if( command==timeCmd )
225  { fParticleGun->SetParticleTime(timeCmd->GetNewDoubleValue(newValues)); }
226  else if( command==polCmd )
227  { fParticleGun->SetParticlePolarization(polCmd->GetNew3VectorValue(newValues)); }
228  else if( command==numberCmd )
229  { fParticleGun->SetNumberOfParticles(numberCmd->GetNewIntValue(newValues)); }
230  else if( command==ionCmd )
231  { IonCommand(newValues); }
232  else if( command==ionLvlCmd )
233  { IonLevelCommand(newValues); }
234 }
235 
237 {
238  G4String cv;
239 
240  if( command==directionCmd )
241  { cv = directionCmd->ConvertToString(fParticleGun->GetParticleMomentumDirection()); }
242  else if( command==particleCmd )
243  { cv = fParticleGun->GetParticleDefinition()->GetParticleName(); }
244  else if( command==energyCmd )
245  {
246  G4double ene = fParticleGun->GetParticleEnergy();
247  if(ene == 0.)
248  { G4cerr << " G4ParticleGun: was defined in terms of momentum." << G4endl; }
249  else
250  { cv = energyCmd->ConvertToString(ene,"GeV"); }
251  }
252  else if( command==momCmd || command==momAmpCmd )
253  {
254  G4double mom = fParticleGun->GetParticleMomentum();
255  if(mom == 0.)
256  { G4cerr << " G4ParticleGun: was defined in terms of kinetic energy." << G4endl; }
257  else
258  {
259  if( command==momCmd )
260  { cv = momCmd->ConvertToString(mom*(fParticleGun->GetParticleMomentumDirection()),"GeV"); }
261  else
262  { cv = momAmpCmd->ConvertToString(mom,"GeV"); }
263  }
264  }
265  else if( command==positionCmd )
266  { cv = positionCmd->ConvertToString(fParticleGun->GetParticlePosition(),"cm"); }
267  else if( command==timeCmd )
268  { cv = timeCmd->ConvertToString(fParticleGun->GetParticleTime(),"ns"); }
269  else if( command==polCmd )
270  { cv = polCmd->ConvertToString(fParticleGun->GetParticlePolarization()); }
271  else if( command==numberCmd )
272  { cv = numberCmd->ConvertToString(fParticleGun->GetNumberOfParticles()); }
273  else if( command==ionCmd )
274  {
275  if (fShootIon) {
276  cv = ItoS(fAtomicNumber) + " " + ItoS(fAtomicMass) + " ";
277  cv += ItoS(fIonCharge);
278  } else {
279  cv = "";
280  }
281  }
282  return cv;
283 }
284 
285 #include "G4IonTable.hh"
286 
287 void G4ParticleGunMessenger::IonLevelCommand(G4String newValues)
288 {
289  if (fShootIon) {
290  G4Tokenizer next( newValues );
291  // check argument
292  fAtomicNumber = StoI(next());
293  fAtomicMass = StoI(next());
294  G4String sQ = next();
295  if (sQ.isNull()) {
296  fIonCharge = fAtomicNumber;
297  } else {
298  fIonCharge = StoI(sQ);
299  sQ = next();
300  if (sQ.isNull()) {
301  fIonEnergyLevel = 0;
302  } else {
303  fIonEnergyLevel = StoI(sQ);
304  }
305  }
307  ion = G4IonTable::GetIonTable()->GetIon(fAtomicNumber,fAtomicMass,fIonEnergyLevel);
308  if (ion == 0) {
309  G4cout << "Ion with Z = " << fAtomicNumber << ", A = " << fAtomicMass
310  << ", I = " << fIonEnergyLevel << " is not defined " << G4endl;
311  } else {
312  fParticleGun->SetParticleDefinition(ion);
313  fParticleGun->SetParticleCharge(fIonCharge*eplus);
314  }
315  } else {
316  G4cout << "Set /gun/particle to ion before using /gun/ion command";
317  G4cout << G4endl;
318  }
319 }
320 
321 void G4ParticleGunMessenger::IonCommand(G4String newValues)
322 {
323  if (fShootIon) {
324  G4Tokenizer next( newValues );
325  // check argument
326  fAtomicNumber = StoI(next());
327  fAtomicMass = StoI(next());
328  G4String sQ = next();
329  if (sQ.isNull()) {
330  fIonCharge = fAtomicNumber;
331  } else {
332  fIonCharge = StoI(sQ);
333  sQ = next();
334  if (sQ.isNull()) {
335  fIonExciteEnergy = 0.0;
336  } else {
337  fIonExciteEnergy = StoD(sQ) * keV;
338  }
339  }
340 
341  G4ParticleDefinition* ion = 0;
342  ion = G4IonTable::GetIonTable()->GetIon( fAtomicNumber, fAtomicMass, fIonExciteEnergy);
343  if (ion==0) {
344  G4cout << "Ion with Z=" << fAtomicNumber;
345  G4cout << " A=" << fAtomicMass << "is not defined" << G4endl;
346  } else {
347  fParticleGun->SetParticleDefinition(ion);
348  fParticleGun->SetParticleCharge(fIonCharge*eplus);
349  }
350  } else {
351  G4cout << "Set /gun/particle to ion before using /gun/ion command";
352  G4cout << G4endl;
353  }
354 }
355 
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 SetDefaultUnit(const char *defUnit)
G4ParticleDefinition * GetIon(G4int Z, G4int A, G4int lvl=0)
Definition: G4IonTable.cc:449
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:357
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)
G4ParticleGunMessenger(G4ParticleGun *fPtclGun)
G4int StoI(G4String s)
static G4IonTable * GetIonTable()
Definition: G4IonTable.hh:80
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 G4Geantino * Geantino()
Definition: G4Geantino.cc:87
void SetCandidates(const char *candidateList)
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
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)