Geant4_10
G4UIcontrolMessenger.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: G4UIcontrolMessenger.cc 68725 2013-04-05 09:27:45Z gcosmo $
28 //
29 
30 #include <stdlib.h>
31 #include "G4UIcontrolMessenger.hh"
32 #include "G4UImanager.hh"
33 #include "G4UIdirectory.hh"
34 #include "G4UIcommand.hh"
35 #include "G4UIparameter.hh"
36 #include "G4UIcmdWithAString.hh"
37 #include "G4UIcmdWithAnInteger.hh"
39 #include "G4UIaliasList.hh"
40 #include "G4StateManager.hh"
41 #include "G4Tokenizer.hh"
42 
43 #include "G4ios.hh"
44 
46 {
47  controlDirectory = new G4UIdirectory("/control/");
48  controlDirectory->SetGuidance("UI control commands.");
49 
50  macroPathCommand = new G4UIcmdWithAString("/control/macroPath",this);
51  macroPathCommand->SetGuidance("Set macro search path"
52  "with colon-separated list.");
53  macroPathCommand->SetParameterName("path",false);
54 
55  ExecuteCommand = new G4UIcmdWithAString("/control/execute",this);
56  ExecuteCommand->SetGuidance("Execute a macro file.");
57  ExecuteCommand->SetParameterName("fileName",false);
58  ExecuteCommand->SetToBeBroadcasted(false);
59 
60  loopCommand = new G4UIcommand("/control/loop",this);
61  loopCommand->SetGuidance("Execute a macro file more than once.");
62  loopCommand->SetGuidance("Loop counter can be used as an aliased variable.");
63  G4UIparameter* param1 = new G4UIparameter("macroFile",'s',false);
64  loopCommand->SetParameter(param1);
65  G4UIparameter* param2 = new G4UIparameter("counterName",'s',false);
66  loopCommand->SetParameter(param2);
67  G4UIparameter* param3 = new G4UIparameter("initialValue",'d',false);
68  loopCommand->SetParameter(param3);
69  G4UIparameter* param4 = new G4UIparameter("finalValue",'d',false);
70  loopCommand->SetParameter(param4);
71  G4UIparameter* param5 = new G4UIparameter("stepSize",'d',true);
72  param5->SetDefaultValue(1.0);
73  loopCommand->SetParameter(param5);
74  loopCommand->SetToBeBroadcasted(false);
75 
76  foreachCommand = new G4UIcommand("/control/foreach",this);
77  foreachCommand->SetGuidance("Execute a macro file more than once.");
78  foreachCommand->SetGuidance("Loop counter can be used as an aliased variable.");
79  foreachCommand->SetGuidance("Values must be separated by a space.");
80  G4UIparameter* param6 = new G4UIparameter("macroFile",'s',false);
81  foreachCommand->SetParameter(param6);
82  G4UIparameter* param7 = new G4UIparameter("counterName",'s',false);
83  foreachCommand->SetParameter(param7);
84  G4UIparameter* param8 = new G4UIparameter("valueList",'s',false);
85  foreachCommand->SetParameter(param8);
86  foreachCommand->SetToBeBroadcasted(false);
87 
88  suppressAbortionCommand = new G4UIcmdWithAnInteger("/control/suppressAbortion",this);
89  suppressAbortionCommand->SetGuidance("Suppress the program abortion caused by G4Exception.");
90  suppressAbortionCommand->SetGuidance("Suppression level = 0 : no suppression");
91  suppressAbortionCommand->SetGuidance(" = 1 : suppress during EventProc state");
92  suppressAbortionCommand->SetGuidance(" = 2 : full suppression, i.e. no abortion by G4Exception");
93  suppressAbortionCommand->SetGuidance("When abortion is suppressed, you will get error messages issued by G4Exception,");
94  suppressAbortionCommand->SetGuidance("and there is NO guarantee for the correct result after the G4Exception error message.");
95  suppressAbortionCommand->SetParameterName("level",true);
96  suppressAbortionCommand->SetRange("level >= 0 && level <= 2");
97  suppressAbortionCommand->SetDefaultValue(0);
98 
99  verboseCommand = new G4UIcmdWithAnInteger("/control/verbose",this);
100  verboseCommand->SetGuidance("Applied command will also be shown on screen.");
101  verboseCommand->SetGuidance("This command is useful with MACRO file.");
102  verboseCommand->SetGuidance(" 0 : silent");
103  verboseCommand->SetGuidance(" 1 : only the valid commands are shown.");
104  verboseCommand->SetGuidance(" 2 : comment lines are also shown (default).");
105  verboseCommand->SetParameterName("switch",true);
106  verboseCommand->SetRange("switch >= 0 && switch <=2");
107  verboseCommand->SetDefaultValue(2);
108 
109  historyCommand = new G4UIcmdWithAString("/control/saveHistory",this);
110  historyCommand->SetGuidance("Store command history to a file.");
111  historyCommand->SetGuidance("Defaul file name is G4history.macro.");
112  historyCommand->SetParameterName("fileName",true);
113  historyCommand->SetDefaultValue("G4History.macro");
114 
115  stopStoreHistoryCommand
116  = new G4UIcmdWithoutParameter("/control/stopSavingHistory",this);
117  stopStoreHistoryCommand->SetGuidance("Stop saving history file.");
118 
119  aliasCommand = new G4UIcommand("/control/alias",this);
120  aliasCommand->SetGuidance("Set an alias.");
121  aliasCommand->SetGuidance("String can be aliased by this command.");
122  aliasCommand->SetGuidance("The string may contain one or more spaces,");
123  aliasCommand->SetGuidance("the string must be enclosed by double quotes (\").");
124  aliasCommand->SetGuidance("To use an alias, enclose the alias name with");
125  aliasCommand->SetGuidance("parenthis \"{\" and \"}\".");
126  G4UIparameter* aliasNameParam = new G4UIparameter("aliasName",'s',false);
127  aliasCommand->SetParameter(aliasNameParam);
128  G4UIparameter* aliasValueParam = new G4UIparameter("aliasValue",'s',false);
129  aliasCommand->SetParameter(aliasValueParam);
130 
131  unaliasCommand = new G4UIcmdWithAString("/control/unalias",this);
132  unaliasCommand->SetGuidance("Remove an alias.");
133  unaliasCommand->SetParameterName("aliasName",false);
134 
135  listAliasCommand = new G4UIcmdWithoutParameter("/control/listAlias",this);
136  listAliasCommand->SetGuidance("List aliases.");
137 
138  getEnvCmd = new G4UIcmdWithAString("/control/getEnv",this);
139  getEnvCmd->SetGuidance("Get a shell environment variable and define it as an alias.");
140 
141  echoCmd = new G4UIcmdWithAString("/control/echo",this);
142  echoCmd->SetGuidance("Display the aliased value.");
143 
144  shellCommand = new G4UIcmdWithAString("/control/shell",this);
145  shellCommand->SetGuidance("Execute a (Unix) SHELL command.");
146 
147  ManualCommand = new G4UIcmdWithAString("/control/manual",this);
148  ManualCommand->SetGuidance("Display all of sub-directories and commands.");
149  ManualCommand->SetGuidance("Directory path should be given by FULL-PATH.");
150  ManualCommand->SetParameterName("dirPath",true);
151  ManualCommand->SetDefaultValue("/");
152  ManualCommand->SetToBeBroadcasted(false);
153 
154  HTMLCommand = new G4UIcmdWithAString("/control/createHTML",this);
155  HTMLCommand->SetGuidance("Generate HTML files for all of sub-directories and commands.");
156  HTMLCommand->SetGuidance("Directory path should be given by FULL-PATH.");
157  HTMLCommand->SetParameterName("dirPath",true);
158  HTMLCommand->SetDefaultValue("/");
159  HTMLCommand->SetToBeBroadcasted(false);
160 
161  maxStoredHistCommand = new G4UIcmdWithAnInteger("/control/maximumStoredHistory",this);
162  maxStoredHistCommand->SetGuidance("Set maximum number of stored UI commands.");
163  maxStoredHistCommand->SetParameterName("max",true);
164  maxStoredHistCommand->SetDefaultValue(20);
165 
166  ifCommand = new G4UIcommand("/control/if",this);
167  ifCommand->SetGuidance("Execute a macro file if the expression is true.");
168  ifCommand->SetGuidance(" Syntax : <double> <comp> <double> <macro_file>");
169  G4UIparameter* leftParam = new G4UIparameter("left",'d',false);
170  ifCommand->SetParameter(leftParam);
171  G4UIparameter* compParam = new G4UIparameter("comp",'s',false);
172  compParam->SetParameterCandidates("> >= < <= == !=");
173  ifCommand->SetParameter(compParam);
174  G4UIparameter* rightParam = new G4UIparameter("right",'d',false);
175  ifCommand->SetParameter(rightParam);
176  G4UIparameter* macroFileParam = new G4UIparameter("aliasValue",'s',false);
177  ifCommand->SetParameter(macroFileParam);
178  ifCommand->SetToBeBroadcasted(false);
179 
180  addCommand = new G4UIcommand("/control/add",this);
181  addCommand->SetGuidance("Define a new alias as the sum of two values.");
182  addCommand->SetGuidance(" Syntax : <new_alias> <value1> <value2>");
183  addCommand->SetGuidance(" <new_alias> may be an already existing alias. If it is the case,");
184  addCommand->SetGuidance(" aliased value is alternated.");
185  G4UIparameter* newAlias1 = new G4UIparameter("new_alias",'s',false);
186  addCommand->SetParameter(newAlias1);
187  G4UIparameter* val1a = new G4UIparameter("value1",'d',false);
188  addCommand->SetParameter(val1a);
189  G4UIparameter* val1b = new G4UIparameter("value2",'d',false);
190  addCommand->SetParameter(val1b);
191 
192  subtractCommand = new G4UIcommand("/control/subtract",this);
193  subtractCommand->SetGuidance("Define a new alias as the subtraction of two values.");
194  subtractCommand->SetGuidance(" Syntax : <new_alias> <value1> <value2>");
195  subtractCommand->SetGuidance(" <new_alias> may be an already existing alias. If it is the case,");
196  subtractCommand->SetGuidance(" aliased value is alternated.");
197  G4UIparameter* newAlias2 = new G4UIparameter("new_alias",'s',false);
198  subtractCommand->SetParameter(newAlias2);
199  G4UIparameter* val2a = new G4UIparameter("value1",'d',false);
200  subtractCommand->SetParameter(val2a);
201  G4UIparameter* val2b = new G4UIparameter("value2",'d',false);
202  subtractCommand->SetParameter(val2b);
203 
204  multiplyCommand = new G4UIcommand("/control/multiply",this);
205  multiplyCommand->SetGuidance("Define a new alias as the multiplification of two values.");
206  multiplyCommand->SetGuidance(" Syntax : <new_alias> <value1> <value2>");
207  multiplyCommand->SetGuidance(" <new_alias> may be an already existing alias. If it is the case,");
208  multiplyCommand->SetGuidance(" aliased value is alternated.");
209  G4UIparameter* newAlias3 = new G4UIparameter("new_alias",'s',false);
210  multiplyCommand->SetParameter(newAlias3);
211  G4UIparameter* val3a = new G4UIparameter("value1",'d',false);
212  multiplyCommand->SetParameter(val3a);
213  G4UIparameter* val3b = new G4UIparameter("value2",'d',false);
214  multiplyCommand->SetParameter(val3b);
215 
216  divideCommand = new G4UIcommand("/control/divide",this);
217  divideCommand->SetGuidance("Define a new alias as the division of two values.");
218  divideCommand->SetGuidance(" Syntax : <new_alias> <value1> <value2>");
219  divideCommand->SetGuidance(" <new_alias> may be an already existing alias. If it is the case,");
220  divideCommand->SetGuidance(" aliased value is alternated.");
221  G4UIparameter* newAlias4 = new G4UIparameter("new_alias",'s',false);
222  divideCommand->SetParameter(newAlias4);
223  G4UIparameter* val4a = new G4UIparameter("value1",'d',false);
224  divideCommand->SetParameter(val4a);
225  G4UIparameter* val4b = new G4UIparameter("value2",'d',false);
226  val4b->SetParameterRange("value2 != 0.");
227  divideCommand->SetParameter(val4b);
228 
229  remainderCommand = new G4UIcommand("/control/remainder",this);
230  remainderCommand->SetGuidance("Define a new alias as the remainder of two values.");
231  remainderCommand->SetGuidance(" Syntax : <new_alias> <value1> <value2>");
232  remainderCommand->SetGuidance(" <new_alias> may be an already existing alias. If it is the case,");
233  remainderCommand->SetGuidance(" aliased value is alternated.");
234  G4UIparameter* newAlias5 = new G4UIparameter("new_alias",'s',false);
235  remainderCommand->SetParameter(newAlias5);
236  G4UIparameter* val5a = new G4UIparameter("value1",'i',false);
237  remainderCommand->SetParameter(val5a);
238  G4UIparameter* val5b = new G4UIparameter("value2",'i',false);
239  val4b->SetParameterRange("value2 != 0");
240  remainderCommand->SetParameter(val5b);
241 
242 }
243 
245 {
246  delete macroPathCommand;
247  delete ExecuteCommand;
248  delete suppressAbortionCommand;
249  delete verboseCommand;
250  delete historyCommand;
251  delete stopStoreHistoryCommand;
252  delete ManualCommand;
253  delete aliasCommand;
254  delete unaliasCommand;
255  delete listAliasCommand;
256  delete getEnvCmd;
257  delete echoCmd;
258  delete shellCommand;
259  delete loopCommand;
260  delete foreachCommand;
261  delete HTMLCommand;
262  delete maxStoredHistCommand;
263  delete ifCommand;
264  delete addCommand;
265  delete subtractCommand;
266  delete multiplyCommand;
267  delete divideCommand;
268  delete remainderCommand;
269 
270  delete controlDirectory;
271 }
272 
274 {
276 
277  if( command == macroPathCommand) {
278  UI-> SetMacroSearchPath(newValue);
279  UI-> ParseMacroSearchPath();
280  }
281  if(command==ExecuteCommand)
282  {
283  UI-> ExecuteMacroFile(UI-> FindMacroPath(newValue));
284  }
285  if(command==suppressAbortionCommand)
286  {
287  G4StateManager::GetStateManager()->SetSuppressAbortion(suppressAbortionCommand->GetNewIntValue(newValue));
288  }
289  if(command==verboseCommand)
290  {
291  UI->SetVerboseLevel(verboseCommand->GetNewIntValue(newValue));
292  }
293  if(command==historyCommand)
294  {
295  UI->StoreHistory(newValue);
296  }
297  if(command==stopStoreHistoryCommand)
298  {
299  UI->StoreHistory(false);
300  }
301  if(command==ManualCommand)
302  {
303  UI->ListCommands(newValue);
304  }
305  if(command==aliasCommand)
306  {
307  UI->SetAlias(newValue);
308  }
309  if(command==unaliasCommand)
310  {
311  UI->RemoveAlias(newValue);
312  }
313  if(command==listAliasCommand)
314  {
315  UI->ListAlias();
316  }
317  if(command==getEnvCmd)
318  {
319  if(getenv(newValue))
320  {
321  G4String st = "/control/alias ";
322  st += newValue;
323  st += " ";
324  st += getenv(newValue);
325  UI->ApplyCommand(st);
326  }
327  else
328  { G4cerr << "<" << newValue << "> is not defined as a shell variable. Command ignored." << G4endl; }
329  }
330  if(command==echoCmd)
331  { G4cout << UI->SolveAlias(newValue) << G4endl; }
332  if(command==shellCommand)
333  {
334  system(newValue);
335  }
336  if(command==loopCommand)
337  {
338  UI->LoopS(newValue);
339  }
340  if(command==foreachCommand)
341  {
342  UI->ForeachS(newValue);
343  }
344  if(command==HTMLCommand)
345  {
346  UI->CreateHTML(newValue);
347  }
348  if(command==maxStoredHistCommand)
349  {
350  UI->SetMaxHistSize(maxStoredHistCommand->GetNewIntValue(newValue));
351  }
352  if(command==ifCommand)
353  {
354  G4Tokenizer next(newValue);
355  G4double l = StoD(next());
356  G4String comp = next();
357  G4double r = StoD(next());
358  G4String mac = next();
359  G4bool x = false;
360  if(comp==">") x = (l>r);
361  else if(comp==">=") x = (l>=r);
362  else if(comp=="<") x = (l<r);
363  else if(comp=="<=") x = (l<=r);
364  else if(comp=="==") x = (l==r);
365  else if(comp=="!=") x = (l!=r);
366  if(x) UI->ExecuteMacroFile(mac);
367  }
368  if(command==addCommand)
369  {
370  G4Tokenizer next(newValue);
371  G4String newA = next();
372  G4double l = StoD(next());
373  G4double r = StoD(next());
374  G4String st = "/control/alias ";
375  st += newA;
376  st += " ";
377  st += DtoS(l+r);
378  UI->ApplyCommand(st);
379  }
380  if(command==subtractCommand)
381  {
382  G4Tokenizer next(newValue);
383  G4String newA = next();
384  G4double l = StoD(next());
385  G4double r = StoD(next());
386  G4String st = "/control/alias ";
387  st += newA;
388  st += " ";
389  st += DtoS(l-r);
390  UI->ApplyCommand(st);
391  }
392  if(command==multiplyCommand)
393  {
394  G4Tokenizer next(newValue);
395  G4String newA = next();
396  G4double l = StoD(next());
397  G4double r = StoD(next());
398  G4String st = "/control/alias ";
399  st += newA;
400  st += " ";
401  st += DtoS(l*r);
402  UI->ApplyCommand(st);
403  }
404  if(command==divideCommand)
405  {
406  G4Tokenizer next(newValue);
407  G4String newA = next();
408  G4double l = StoD(next());
409  G4double r = StoD(next());
410  G4String st = "/control/alias ";
411  st += newA;
412  st += " ";
413  st += DtoS(l/r);
414  UI->ApplyCommand(st);
415  }
416  if(command==remainderCommand)
417  {
418  G4Tokenizer next(newValue);
419  G4String newA = next();
420  G4int l = StoI(next());
421  G4int r = StoI(next());
422  G4String st = "/control/alias ";
423  st += newA;
424  st += " ";
425  st += DtoS(l%r);
426  UI->ApplyCommand(st);
427  }
428 }
429 
431 {
433  G4String currentValue;
434 
435  if( command == macroPathCommand ) {
436  currentValue = UI-> GetMacroSearchPath();
437  }
438  if(command==verboseCommand)
439  {
440  currentValue = verboseCommand->ConvertToString(UI->GetVerboseLevel());
441  }
442  if(command==suppressAbortionCommand)
443  {
444  currentValue = suppressAbortionCommand->ConvertToString(G4StateManager::GetStateManager()->GetSuppressAbortion());
445  }
446  if(command==maxStoredHistCommand)
447  {
448  currentValue = maxStoredHistCommand->ConvertToString(UI->GetMaxHistSize());
449  }
450 
451  return currentValue;
452 }
453 
454 
void ForeachS(const char *valueList)
Definition: G4UImanager.cc:331
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
void ExecuteMacroFile(const char *fileName)
Definition: G4UImanager.cc:278
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
void RemoveAlias(const char *aliasName)
Definition: G4UImanager.cc:616
void SetAlias(const char *aliasLine)
Definition: G4UImanager.cc:597
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
static G4int GetNewIntValue(const char *paramString)
void SetParameterRange(const char *theRange)
void SetParameterCandidates(const char *theString)
void SetNewValue(G4UIcommand *command, G4String newValue)
void SetToBeBroadcasted(G4bool val)
Definition: G4UIcommand.hh:184
void SetDefaultValue(const char *theDefaultValue)
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:357
tuple x
Definition: test.py:50
G4int GetVerboseLevel() const
Definition: G4UImanager.hh:227
int G4int
Definition: G4Types.hh:78
void SetVerboseLevel(G4int val)
Definition: G4UImanager.hh:225
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
void SetSuppressAbortion(G4int i)
static G4StateManager * GetStateManager()
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void SetRange(const char *rs)
Definition: G4UIcommand.hh:125
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
G4String DtoS(G4double a)
G4int StoI(G4String s)
void SetMaxHistSize(G4int mx)
Definition: G4UImanager.hh:238
void ListCommands(const char *direc)
Definition: G4UImanager.cc:524
jump r
Definition: plot.C:36
G4String GetCurrentValue(G4UIcommand *command)
void SetDefaultValue(const char *defVal)
G4double StoD(G4String s)
system("rm -rf dna.root")
G4String SolveAlias(const char *aCmd)
Definition: G4UImanager.cc:364
void ListAlias()
Definition: G4UImanager.cc:623
#define G4endl
Definition: G4ios.hh:61
void LoopS(const char *valueList)
Definition: G4UImanager.cc:287
void SetDefaultValue(G4int defVal)
double G4double
Definition: G4Types.hh:76
void CreateHTML(const char *dir="/")
Definition: G4UImanager.cc:628
void StoreHistory(const char *fileName="G4history.macro")
Definition: G4UImanager.cc:499
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:419
G4GLOB_DLL std::ostream G4cerr
G4int GetMaxHistSize() const
Definition: G4UImanager.hh:240