ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ParticleGunMessenger.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4ParticleGunMessenger.cc
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 //
28 
30 #include "G4SystemOfUnits.hh"
31 #include "G4ParticleGun.hh"
32 #include "G4Geantino.hh"
33 #include "G4ThreeVector.hh"
34 #include "G4ParticleTable.hh"
35 #include "G4IonTable.hh"
36 #include "G4UIdirectory.hh"
38 #include "G4UIcmdWithAString.hh"
40 #include "G4UIcmdWith3Vector.hh"
42 #include "G4UIcmdWithAnInteger.hh"
43 #include "G4ios.hh"
44 #include "G4Tokenizer.hh"
45 
47  :fParticleGun(fPtclGun),fShootIon(false),
48  fAtomicNumber(0),fAtomicMass(0),fIonCharge(0),fIonExciteEnergy(0.0),
49  fIonFloatingLevelBase('\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 E flb]");
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  ionCmd->SetGuidance(" flb:(char) Floating level base");
139 
140  G4UIparameter* param;
141  param = new G4UIparameter("Z",'i',false);
142  ionCmd->SetParameter(param);
143  param = new G4UIparameter("A",'i',false);
144  ionCmd->SetParameter(param);
145  param = new G4UIparameter("Q",'i',true);
146  param->SetDefaultValue(-1);
147  ionCmd->SetParameter(param);
148  param = new G4UIparameter("E",'d',true);
149  param->SetDefaultValue(0.0);
150  ionCmd->SetParameter(param);
151  param = new G4UIparameter("flb",'c',true);
152  param->SetDefaultValue("noFloat");
153  param->SetParameterCandidates("noFloat X Y Z U V W R S T A B C D E");
154  ionCmd->SetParameter(param);
155 
156  ionLvlCmd = new G4UIcommand("/gun/ionL",this);
157  ionLvlCmd->SetGuidance("THIS COMMAND IS DEPRECATED and will be removed in release 10.5.");
158  ionLvlCmd->SetGuidance("Use /gun/ion instead.");
159  ionLvlCmd->SetGuidance("Set properties of ion to be generated.");
160  ionLvlCmd->SetGuidance("[usage] /gun/ionL Z A [Q I]");
161  ionLvlCmd->SetGuidance(" Z:(int) AtomicNumber");
162  ionLvlCmd->SetGuidance(" A:(int) AtomicMass");
163  ionLvlCmd->SetGuidance(" Q:(int) Charge of Ion (in unit of e)");
164  ionLvlCmd->SetGuidance(" I:(int) Level number of metastable state (0 = ground)");
165 
166  G4UIparameter* paraml;
167  paraml = new G4UIparameter("Z",'i',false);
168  ionLvlCmd->SetParameter(paraml);
169  paraml = new G4UIparameter("A",'i',false);
170  ionLvlCmd->SetParameter(paraml);
171  paraml = new G4UIparameter("Q",'i',true);
172  paraml->SetDefaultValue(-1);
173  ionLvlCmd->SetParameter(paraml);
174  paraml = new G4UIparameter("I",'i',true);
175  paraml->SetDefaultValue("0");
176  ionLvlCmd->SetParameter(paraml);
177 
178  // set initial value to G4ParticleGun
184 }
185 
187 {
188  delete listCmd;
189  delete particleCmd;
190  delete directionCmd;
191  delete energyCmd;
192  delete momCmd;
193  delete momAmpCmd;
194  delete positionCmd;
195  delete timeCmd;
196  delete polCmd;
197  delete numberCmd;
198  delete ionCmd;
199  delete ionLvlCmd;
200  delete gunDirectory;
201 }
202 
204 {
206  if (command==listCmd) {
208  } else if (command==particleCmd) {
209  if (newValues =="ion") {
210  fShootIon = true;
211  } else {
212  fShootIon = false;
214  if(pd != 0) {
216  } else {
217  ed << "Particle [" << newValues << "] is not found.";
218  command->CommandFailed(ed);
219  }
220  }
221 
222  } else if( command==directionCmd )
224  else if( command==energyCmd )
226  else if( command==momCmd )
228  else if( command==momAmpCmd )
230  else if( command==positionCmd )
232  else if( command==timeCmd )
234  else if( command==polCmd )
236  else if( command==numberCmd )
238  else if( command==ionCmd )
239  {
240  if (fShootIon) {
241  IonCommand(newValues);
242  } else {
243  ed << "Set /gun/particle to ion before using /gun/ion command";
244  command->CommandFailed(ed);
245  }
246  }
247  else if( command==ionLvlCmd )
248  {
249  G4ExceptionDescription depWarn;
250  depWarn << "\nCommand /gun/ionL is deprecated and will be removed in release 10.5.\n"
251  << "Use /gun/ion instead.\n";
252  G4Exception("G4ParticleGunMessenger::SetNewValue","IonLWarn",JustWarning,depWarn);
253 
254  if (fShootIon) {
255  IonLevelCommand(newValues);
256  } else {
257  ed << "Set /gun/particle to ion before using /gun/ion command";
258  command->CommandFailed(ed);
259  }
260  }
261 }
262 
264 {
265  G4String cv;
266 
267  if( command==directionCmd )
269  else if( command==particleCmd )
271  else if( command==energyCmd )
272  {
274  if(ene == 0.)
275  { G4cerr << " G4ParticleGun: was defined in terms of momentum." << G4endl; }
276  else
277  { cv = energyCmd->ConvertToString(ene,"GeV"); }
278  }
279  else if( command==momCmd || command==momAmpCmd )
280  {
282  if(mom == 0.)
283  { G4cerr << " G4ParticleGun: was defined in terms of kinetic energy." << G4endl; }
284  else
285  {
286  if( command==momCmd )
288  else
289  { cv = momAmpCmd->ConvertToString(mom,"GeV"); }
290  }
291  }
292  else if( command==positionCmd )
294  else if( command==timeCmd )
296  else if( command==polCmd )
298  else if( command==numberCmd )
300  else if( command==ionCmd )
301  {
302  if (fShootIon) {
303  cv = ItoS(fAtomicNumber) + " " + ItoS(fAtomicMass) + " ";
304  cv += ItoS(fIonCharge);
305  } else {
306  cv = "";
307  }
308  }
309  return cv;
310 }
311 
312 #include "G4IonTable.hh"
313 
315 {
316  G4Tokenizer next( newValues );
317  // check argument
318  fAtomicNumber = StoI(next());
319  fAtomicMass = StoI(next());
320  G4String sQ = next();
321  if (sQ.isNull() || StoI(sQ)<0) {
323  } else {
324  fIonCharge = StoI(sQ);
325  }
326  sQ = next();
327  if (sQ.isNull()) {
328  fIonEnergyLevel = 0;
329  } else {
330  fIonEnergyLevel = StoI(sQ);
331  }
334  if (ion == 0) {
336  ed << "Ion with Z = " << fAtomicNumber << ", A = " << fAtomicMass
337  << ", I = " << fIonEnergyLevel << " is not defined ";
339  } else {
342  }
343 }
344 
346 {
347  G4Tokenizer next( newValues );
348  // check argument
349  fAtomicNumber = StoI(next());
350  fAtomicMass = StoI(next());
352  fIonExciteEnergy = 0.0;
353  fIonFloatingLevelBase = '\0';
354  G4String sQ = next();
355  if (!(sQ.isNull()))
356  {
357  if (StoI(sQ)>=0)
358  fIonCharge = StoI(sQ);
359 
360  sQ = next();
361  if (!(sQ.isNull()))
362  {
363  fIonExciteEnergy = StoD(sQ) * keV;
364 
365  sQ = next();
366  if (sQ.isNull()||sQ=="noFloat")
367  { fIonFloatingLevelBase = '\0'; }
368  else
369  { fIonFloatingLevelBase = sQ[(size_t)0]; }
370  }
371  }
375  if (ion==0) {
377  ed << "Ion with Z=" << fAtomicNumber;
378  ed << " A=" << fAtomicMass << "is not defined";
379  ionCmd->CommandFailed(ed);
380  } else {
383  }
384 }
385