Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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$
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 {
50  particleTable = G4ParticleTable::GetParticleTable();
51 
52  gunDirectory = new G4UIdirectory("/gun/");
53  gunDirectory->SetGuidance("Particle Gun control commands.");
54 
55  listCmd = new G4UIcmdWithoutParameter("/gun/List",this);
56  listCmd->SetGuidance("List available particles.");
57  listCmd->SetGuidance(" Invoke G4ParticleTable.");
58 
59  particleCmd = new G4UIcmdWithAString("/gun/particle",this);
60  particleCmd->SetGuidance("Set particle to be generated.");
61  particleCmd->SetGuidance(" (geantino is default)");
62  particleCmd->SetGuidance(" (ion can be specified for shooting ions)");
63  particleCmd->SetParameterName("particleName",true);
64  particleCmd->SetDefaultValue("geantino");
65  G4String candidateList;
66  G4int nPtcl = particleTable->entries();
67  for(G4int i=0;i<nPtcl;i++)
68  {
69  G4ParticleDefinition* pd = particleTable->GetParticle(i);
70  if( !(pd->IsShortLived()) || pd->GetDecayTable() )
71  {
72  candidateList += pd->GetParticleName();
73  candidateList += " ";
74  }
75  }
76  candidateList += "ion ";
77  particleCmd->SetCandidates(candidateList);
78 
79  directionCmd = new G4UIcmdWith3Vector("/gun/direction",this);
80  directionCmd->SetGuidance("Set momentum direction.");
81  directionCmd->SetGuidance("Direction needs not to be a unit vector.");
82  directionCmd->SetParameterName("ex","ey","ez",true,true);
83  directionCmd->SetRange("ex != 0 || ey != 0 || ez != 0");
84 
85  energyCmd = new G4UIcmdWithADoubleAndUnit("/gun/energy",this);
86  energyCmd->SetGuidance("Set kinetic energy.");
87  energyCmd->SetParameterName("Energy",true,true);
88  energyCmd->SetDefaultUnit("GeV");
89  //energyCmd->SetUnitCategory("Energy");
90  //energyCmd->SetUnitCandidates("eV keV MeV GeV TeV");
91 
92  momCmd = new G4UIcmdWith3VectorAndUnit("/gun/momentum",this);
93  momCmd->SetGuidance("Set momentum. This command is equivalent to two commands /gun/direction and /gun/momentumAmp");
94  momCmd->SetParameterName("px","py","pz",true,true);
95  momCmd->SetRange("px != 0 || py != 0 || pz != 0");
96  momCmd->SetDefaultUnit("GeV");
97 
98  momAmpCmd = new G4UIcmdWithADoubleAndUnit("/gun/momentumAmp",this);
99  momAmpCmd->SetGuidance("Set absolute value of momentum.");
100  momAmpCmd->SetGuidance("Direction should be set by /gun/direction command.");
101  momAmpCmd->SetGuidance("This command should be used alternatively with /gun/energy.");
102  momAmpCmd->SetParameterName("Momentum",true,true);
103  momAmpCmd->SetDefaultUnit("GeV");
104 
105  positionCmd = new G4UIcmdWith3VectorAndUnit("/gun/position",this);
106  positionCmd->SetGuidance("Set starting position of the particle.");
107  positionCmd->SetParameterName("X","Y","Z",true,true);
108  positionCmd->SetDefaultUnit("cm");
109  //positionCmd->SetUnitCategory("Length");
110  //positionCmd->SetUnitCandidates("microm mm cm m km");
111 
112  timeCmd = new G4UIcmdWithADoubleAndUnit("/gun/time",this);
113  timeCmd->SetGuidance("Set initial time of the particle.");
114  timeCmd->SetParameterName("t0",true,true);
115  timeCmd->SetDefaultUnit("ns");
116  //timeCmd->SetUnitCategory("Time");
117  //timeCmd->SetUnitCandidates("ns ms s");
118 
119  polCmd = new G4UIcmdWith3Vector("/gun/polarization",this);
120  polCmd->SetGuidance("Set polarization.");
121  polCmd->SetParameterName("Px","Py","Pz",true,true);
122  polCmd->SetRange("Px>=-1.&&Px<=1.&&Py>=-1.&&Py<=1.&&Pz>=-1.&&Pz<=1.");
123 
124  numberCmd = new G4UIcmdWithAnInteger("/gun/number",this);
125  numberCmd->SetGuidance("Set number of particles to be generated.");
126  numberCmd->SetParameterName("N",true,true);
127  numberCmd->SetRange("N>0");
128 
129  ionCmd = new G4UIcommand("/gun/ion",this);
130  ionCmd->SetGuidance("Set properties of ion to be generated.");
131  ionCmd->SetGuidance("[usage] /gun/ion Z A Q");
132  ionCmd->SetGuidance(" Z:(int) AtomicNumber");
133  ionCmd->SetGuidance(" A:(int) AtomicMass");
134  ionCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
135  ionCmd->SetGuidance(" E:(double) Excitation energy (in keV)");
136 
137  G4UIparameter* param;
138  param = new G4UIparameter("Z",'i',false);
139  param->SetDefaultValue("1");
140  ionCmd->SetParameter(param);
141  param = new G4UIparameter("A",'i',false);
142  param->SetDefaultValue("1");
143  ionCmd->SetParameter(param);
144  param = new G4UIparameter("Q",'i',true);
145  param->SetDefaultValue("0");
146  ionCmd->SetParameter(param);
147  param = new G4UIparameter("E",'d',true);
148  param->SetDefaultValue("0.0");
149  ionCmd->SetParameter(param);
150 
151  // set initial value to G4ParticleGun
152  fParticleGun->SetParticleDefinition( G4Geantino::Geantino() );
153  fParticleGun->SetParticleMomentumDirection( G4ThreeVector(1.0,0.0,0.0) );
154  fParticleGun->SetParticleEnergy( 1.0*GeV );
155  fParticleGun->SetParticlePosition(G4ThreeVector(0.0*cm, 0.0*cm, 0.0*cm));
156  fParticleGun->SetParticleTime( 0.0*ns );
157 }
158 
160 {
161  delete listCmd;
162  delete particleCmd;
163  delete directionCmd;
164  delete energyCmd;
165  delete momCmd;
166  delete momAmpCmd;
167  delete positionCmd;
168  delete timeCmd;
169  delete polCmd;
170  delete numberCmd;
171  delete ionCmd;
172  delete gunDirectory;
173 }
174 
176 {
177  if( command==listCmd )
178  { particleTable->DumpTable(); }
179  else if( command==particleCmd )
180  {
181  if (newValues =="ion") {
182  fShootIon = true;
183  } else {
184  fShootIon = false;
185  G4ParticleDefinition* pd = particleTable->FindParticle(newValues);
186  if(pd != 0)
187  { fParticleGun->SetParticleDefinition( pd ); }
188  }
189  }
190  else if( command==directionCmd )
191  { fParticleGun->SetParticleMomentumDirection(directionCmd->GetNew3VectorValue(newValues)); }
192  else if( command==energyCmd )
193  { fParticleGun->SetParticleEnergy(energyCmd->GetNewDoubleValue(newValues)); }
194  else if( command==momCmd )
195  { fParticleGun->SetParticleMomentum(momCmd->GetNew3VectorValue(newValues)); }
196  else if( command==momAmpCmd )
197  { fParticleGun->SetParticleMomentum(momAmpCmd->GetNewDoubleValue(newValues)); }
198  else if( command==positionCmd )
199  { fParticleGun->SetParticlePosition(positionCmd->GetNew3VectorValue(newValues)); }
200  else if( command==timeCmd )
201  { fParticleGun->SetParticleTime(timeCmd->GetNewDoubleValue(newValues)); }
202  else if( command==polCmd )
203  { fParticleGun->SetParticlePolarization(polCmd->GetNew3VectorValue(newValues)); }
204  else if( command==numberCmd )
205  { fParticleGun->SetNumberOfParticles(numberCmd->GetNewIntValue(newValues)); }
206  else if( command==ionCmd )
207  { IonCommand(newValues); }
208 }
209 
211 {
212  G4String cv;
213 
214  if( command==directionCmd )
215  { cv = directionCmd->ConvertToString(fParticleGun->GetParticleMomentumDirection()); }
216  else if( command==particleCmd )
217  { cv = fParticleGun->GetParticleDefinition()->GetParticleName(); }
218  else if( command==energyCmd )
219  {
220  G4double ene = fParticleGun->GetParticleEnergy();
221  if(ene == 0.)
222  { G4cerr << " G4ParticleGun: was defined in terms of momentum." << G4endl; }
223  else
224  { cv = energyCmd->ConvertToString(ene,"GeV"); }
225  }
226  else if( command==momCmd || command==momAmpCmd )
227  {
228  G4double mom = fParticleGun->GetParticleMomentum();
229  if(mom == 0.)
230  { G4cerr << " G4ParticleGun: was defined in terms of kinetic energy." << G4endl; }
231  else
232  {
233  if( command==momCmd )
234  { cv = momCmd->ConvertToString(mom*(fParticleGun->GetParticleMomentumDirection()),"GeV"); }
235  else
236  { cv = momAmpCmd->ConvertToString(mom,"GeV"); }
237  }
238  }
239  else if( command==positionCmd )
240  { cv = positionCmd->ConvertToString(fParticleGun->GetParticlePosition(),"cm"); }
241  else if( command==timeCmd )
242  { cv = timeCmd->ConvertToString(fParticleGun->GetParticleTime(),"ns"); }
243  else if( command==polCmd )
244  { cv = polCmd->ConvertToString(fParticleGun->GetParticlePolarization()); }
245  else if( command==numberCmd )
246  { cv = numberCmd->ConvertToString(fParticleGun->GetNumberOfParticles()); }
247  else if( command==ionCmd )
248  {
249  if (fShootIon) {
250  cv = ItoS(fAtomicNumber) + " " + ItoS(fAtomicMass) + " ";
251  cv += ItoS(fIonCharge);
252  } else {
253  cv = "";
254  }
255  }
256  return cv;
257 }
258 
259 void G4ParticleGunMessenger::IonCommand(G4String newValues)
260 {
261  if (fShootIon) {
262  G4Tokenizer next( newValues );
263  // check argument
264  fAtomicNumber = StoI(next());
265  fAtomicMass = StoI(next());
266  G4String sQ = next();
267  if (sQ.isNull()) {
268  fIonCharge = fAtomicNumber;
269  } else {
270  fIonCharge = StoI(sQ);
271  sQ = next();
272  if (sQ.isNull()) {
273  fIonExciteEnergy = 0.0;
274  } else {
275  fIonExciteEnergy = StoD(sQ) * keV;
276  }
277  }
278 
280  ion = particleTable->GetIon( fAtomicNumber, fAtomicMass, fIonExciteEnergy);
281  if (ion==0) {
282  G4cout << "Ion with Z=" << fAtomicNumber;
283  G4cout << " A=" << fAtomicMass << "is not be defined" << G4endl;
284  } else {
285  fParticleGun->SetParticleDefinition(ion);
286  fParticleGun->SetParticleCharge(fIonCharge*eplus);
287  }
288  } else {
289  G4cout << "Set /gun/particle to ion before using /gun/ion command";
290  G4cout << G4endl;
291  }
292 }
293