Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UImanager.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 //
30 // ---------------------------------------------------------------------
31 
32 #include "G4UImanager.hh"
33 #include "G4UIcommandTree.hh"
34 #include "G4UIcommand.hh"
35 #include "G4UIsession.hh"
36 #include "G4UIbatch.hh"
37 #include "G4UIcontrolMessenger.hh"
38 #include "G4UnitsMessenger.hh"
39 #include "G4ios.hh"
40 #include "G4strstreambuf.hh"
41 #include "G4StateManager.hh"
42 #include "G4UIaliasList.hh"
43 #include "G4Tokenizer.hh"
44 
45 #include <sstream>
46 #include <fstream>
47 
48 G4UImanager * G4UImanager::fUImanager = 0;
49 G4bool G4UImanager::fUImanagerHasBeenKilled = false;
50 
52 {
53  if(!fUImanager)
54  {
55  if(!fUImanagerHasBeenKilled)
56  {
57  fUImanager = new G4UImanager;
58  fUImanager->CreateMessenger();
59  }
60  }
61  return fUImanager;
62 }
63 
65  : G4VStateDependent(true),
66  UImessenger(0), UnitsMessenger(0)
67 {
68  savedCommand = 0;
69  treeTop = new G4UIcommandTree("/");
70  aliasList = new G4UIaliasList;
71  G4String nullString;
72  savedParameters = nullString;
73  verboseLevel = 0;
74  saveHistory = false;
75  session = NULL;
76  g4UIWindow = NULL;
77  SetCoutDestination(session);
78  pauseAtBeginOfEvent = false;
79  pauseAtEndOfEvent = false;
80  maxHistSize = 20;
81  searchPath="";
82 }
83 
84 void G4UImanager::CreateMessenger()
85 {
86  UImessenger = new G4UIcontrolMessenger;
87  UnitsMessenger = new G4UnitsMessenger;
88 }
89 
91 {
92  SetCoutDestination(NULL);
93  histVec.clear();
94  if(saveHistory) historyFile.close();
95  delete UImessenger;
96  delete UnitsMessenger;
97  delete treeTop;
98  delete aliasList;
99  fUImanagerHasBeenKilled = true;
100  fUImanager = NULL;
101 }
102 
104  : G4VStateDependent(true)
105 {
106  UImessenger = ui.UImessenger;
107  UnitsMessenger = ui.UnitsMessenger;
108  aliasList = ui.aliasList;
109  g4UIWindow = ui.g4UIWindow;
110  savedCommand= ui.savedCommand;
111  session = ui.session;
112  treeTop = ui.treeTop;
113 }
114 
115 const G4UImanager & G4UImanager::operator=(const G4UImanager &right)
116 { return right; }
117 G4int G4UImanager::operator==(const G4UImanager &right) const
118 { return (this==&right); }
119 G4int G4UImanager::operator!=(const G4UImanager &right) const
120 { return (this!=&right); }
121 
123 {
124  G4String theCommand = aCommand;
125  savedCommand = treeTop->FindPath( theCommand );
126  if( savedCommand == NULL )
127  {
128  G4cerr << "command not found" << G4endl;
129  return G4String();
130  }
131  return savedCommand->GetCurrentValue();
132 }
133 
135 G4int parameterNumber, G4bool reGet)
136 {
137  if(reGet || savedCommand == NULL)
138  {
139  savedParameters = GetCurrentValues( aCommand );
140  }
141  G4Tokenizer savedToken( savedParameters );
142  G4String token;
143  for(G4int i_thParameter=0;i_thParameter<parameterNumber;i_thParameter++)
144  {
145  token = savedToken();
146  if( token.isNull() ) return G4String();
147  if( token[(size_t)0] == '"' )
148  {
149  token.append(" ");
150  token.append(savedToken("\""));
151  }
152  }
153  return token;
154 }
155 
157 const char * aParameterName, G4bool reGet)
158 {
159  if(reGet || savedCommand == NULL)
160  {
161  G4String parameterValues = GetCurrentValues( aCommand );
162  }
163  for(G4int i=0;i<savedCommand->GetParameterEntries();i++)
164  {
165  if( aParameterName ==
166  savedCommand->GetParameter(i)->GetParameterName() )
167  return GetCurrentStringValue(aCommand,i+1,false);
168  }
169  return G4String();
170 }
171 
173 const char * aParameterName, G4bool reGet)
174 {
175  G4String targetParameter =
176  GetCurrentStringValue( aCommand, aParameterName, reGet );
177  G4int value;
178  const char* t = targetParameter;
179  std::istringstream is(t);
180  is >> value;
181  return value;
182 }
183 
185 G4int parameterNumber, G4bool reGet)
186 {
187  G4String targetParameter =
188  GetCurrentStringValue( aCommand, parameterNumber, reGet );
189  G4int value;
190  const char* t = targetParameter;
191  std::istringstream is(t);
192  is >> value;
193  return value;
194 }
195 
197 const char * aParameterName, G4bool reGet)
198 {
199  G4String targetParameter =
200  GetCurrentStringValue( aCommand, aParameterName, reGet );
201  G4double value;
202  const char* t = targetParameter;
203  std::istringstream is(t);
204  is >> value;
205  return value;
206 }
207 
209 G4int parameterNumber, G4bool reGet)
210 {
211  G4String targetParameter =
212  GetCurrentStringValue( aCommand, parameterNumber, reGet );
213  G4double value;
214  const char* t = targetParameter;
215  std::istringstream is(t);
216  is >> value;
217  return value;
218 }
219 
221 {
222  treeTop->AddNewCommand( newCommand );
223 }
224 
226 {
227  treeTop->RemoveCommand( aCommand );
228 }
229 
230 void G4UImanager::ExecuteMacroFile(const char * fileName)
231 {
232  G4UIsession* batchSession = new G4UIbatch(fileName,session);
233  session = batchSession;
234  G4UIsession* previousSession = session->SessionStart();
235  delete session;
236  session = previousSession;
237 }
238 
239 void G4UImanager::LoopS(const char* valueList)
240 {
241  G4String vl = valueList;
242  G4Tokenizer parameterToken(vl);
243  G4String mf = parameterToken();
244  G4String vn = parameterToken();
245  G4String c1 = parameterToken();
246  c1 += " ";
247  c1 += parameterToken();
248  c1 += " ";
249  c1 += parameterToken();
250  const char* t1 = c1;
251  std::istringstream is(t1);
252  G4double d1;
253  G4double d2;
254  G4double d3;
255  is >> d1 >> d2 >> d3;
256  Loop(mf,vn,d1,d2,d3);
257 }
258 
259 void G4UImanager::Loop(const char * macroFile,const char * variableName,
260  G4double initialValue,G4double finalValue,G4double stepSize)
261 {
262  G4String cd;
263  if (stepSize > 0) {
264  for(G4double d=initialValue;d<=finalValue;d+=stepSize)
265  {
266  std::ostringstream os;
267  os << d;
268  cd += os.str();
269  cd += " ";
270  }
271  } else {
272  for(G4double d=initialValue;d>=finalValue;d+=stepSize)
273  {
274  std::ostringstream os;
275  os << d;
276  cd += os.str();
277  cd += " ";
278  }
279  }
280  Foreach(macroFile,variableName,cd);
281 }
282 
283 void G4UImanager::ForeachS(const char* valueList)
284 {
285  G4String vl = valueList;
286  G4Tokenizer parameterToken(vl);
287  G4String mf = parameterToken();
288  G4String vn = parameterToken();
289  G4String c1 = parameterToken();
290  G4String ca;
291  while(!((ca=parameterToken()).isNull()))
292  {
293  c1 += " ";
294  c1 += ca;
295  }
296  Foreach(mf,vn,c1);
297 }
298 
299 void G4UImanager::Foreach(const char * macroFile,const char * variableName,
300  const char * candidates)
301 {
302  G4String candidatesString = candidates;
303  G4Tokenizer parameterToken( candidatesString );
304  G4String cd;
305  while(!((cd=parameterToken()).isNull()))
306  {
307  G4String vl = variableName;
308  vl += " ";
309  vl += cd;
310  SetAlias(vl);
311  ExecuteMacroFile(macroFile);
312  }
313 }
314 
315 
317 {
318  G4String aCommand = aCmd;
319  G4int ia = aCommand.index("{");
320  G4int iz = aCommand.index("#");
321  while((ia != G4int(std::string::npos))&&((iz==G4int(std::string::npos))||(ia<iz)))
322  {
323  G4int ibx = -1;
324  while(ibx<0)
325  {
326  G4int ib = aCommand.index("}");
327  if( ib == G4int(std::string::npos) )
328  {
329  G4cerr << aCommand << G4endl;
330  for(G4int i=0;i<ia;i++) G4cerr << " ";
331  G4cerr << "^" << G4endl;
332  G4cerr << "Unmatched alias parenthis -- command ignored" << G4endl;
333  G4String nullStr;
334  return nullStr;
335  }
336  G4String ps = aCommand(ia+1,aCommand.length()-(ia+1));
337  G4int ic = ps.index("{");
338  G4int id = ps.index("}");
339  if(ic!=G4int(std::string::npos) && ic < id)
340  { ia+=ic+1; }
341  else
342  { ibx = ib; }
343  }
344  //--- Here ia represents the position of innermost "{"
345  //--- and ibx represents corresponding "}"
346  G4String subs;
347  if(ia>0) subs = aCommand(0,ia);
348  G4String alis = aCommand(ia+1,ibx-ia-1);
349  G4String rems = aCommand(ibx+1,aCommand.length()-ibx);
350  // G4cout << "<" << subs << "> <" << alis << "> <" << rems << ">" << G4endl;
351  G4String* alVal = aliasList->FindAlias(alis);
352  if(!alVal)
353  {
354  G4cerr << "Alias <" << alis << "> not found -- command ignored" << G4endl;
355  G4String nullStr;
356  return nullStr;
357  }
358  aCommand = subs+(*alVal)+rems;
359  ia = aCommand.index("{");
360  }
361  return aCommand;
362 }
363 
365 {
366  return ApplyCommand(aCmd.data());
367 }
368 
370 {
371  G4String aCommand = SolveAlias(aCmd);
372  if(aCommand.isNull()) return fAliasNotFound;
373  if(verboseLevel) G4cout << aCommand << G4endl;
374  G4String commandString;
375  G4String commandParameter;
376 
377  G4int i = aCommand.index(" ");
378  if( i != G4int(std::string::npos) )
379  {
380  commandString = aCommand(0,i);
381  commandParameter = aCommand(i+1,aCommand.length()-(i+1));
382  }
383  else
384  {
385  commandString = aCommand;
386  }
387 
388  // remove doubled slash
389  G4int len = commandString.length();
390  G4int ll = 0;
391  G4String a1;
392  G4String a2;
393  while(ll<len-1)
394  {
395  if(commandString(ll,2)=="//")
396  {
397  if(ll==0)
398  { commandString.remove(ll,1); }
399  else
400  {
401  a1 = commandString(0,ll);
402  a2 = commandString(ll+1,len-ll-1);
403  commandString = a1+a2;
404  }
405  len--;
406  }
407  else
408  { ll++; }
409  }
410 
411  G4UIcommand * targetCommand = treeTop->FindPath( commandString );
412  if( targetCommand == NULL )
413  { return fCommandNotFound; }
414 
415  if(!(targetCommand->IsAvailable()))
416  { return fIllegalApplicationState; }
417 
418  if(saveHistory) historyFile << aCommand << G4endl;
419  if( G4int(histVec.size()) >= maxHistSize )
420  { histVec.erase(histVec.begin()); }
421  histVec.push_back(aCommand);
422  return targetCommand->DoIt( commandParameter );
423 }
424 
425 void G4UImanager::StoreHistory(const char* fileName)
426 { StoreHistory(true,fileName); }
427 
428 void G4UImanager::StoreHistory(G4bool historySwitch,const char* fileName)
429 {
430  if(historySwitch)
431  {
432  if(saveHistory)
433  { historyFile.close(); }
434  historyFile.open((char*)fileName);
435  saveHistory = true;
436  }
437  else
438  {
439  historyFile.close();
440  saveHistory = false;
441  }
442  saveHistory = historySwitch;
443 }
444 
445 void G4UImanager::PauseSession(const char* msg)
446 {
447  if(session) session->PauseSessionStart(msg);
448 }
449 
450 void G4UImanager::ListCommands(const char* direct)
451 {
452  G4UIcommandTree* comTree = FindDirectory(direct);
453  if(comTree)
454  { comTree->List(); }
455  else
456  { G4cout << direct << " is not found." << G4endl; }
457 }
458 
459 G4UIcommandTree* G4UImanager::FindDirectory(const char* dirName)
460 {
461  G4String aDirName = dirName;
462  G4String targetDir = aDirName.strip(G4String::both);
463  if( targetDir( targetDir.length()-1 ) != '/' )
464  { targetDir += "/"; }
465  G4UIcommandTree* comTree = treeTop;
466  if( targetDir == "/" )
467  { return comTree; }
468  G4int idx = 1;
469  while( idx < G4int(targetDir.length())-1 )
470  {
471  G4int i = targetDir.index("/",idx);
472  G4String targetDirString = targetDir(0,i+1);
473  comTree = comTree->GetTree(targetDirString);
474  if( comTree == NULL )
475  { return NULL; }
476  idx = i+1;
477  }
478  return comTree;
479 }
480 
482 {
483  //G4cout << G4StateManager::GetStateManager()->GetStateString(requestedState) << " <--- " << G4StateManager::GetStateManager()->GetStateString(G4StateManager::GetStateManager()->GetPreviousState()) << G4endl;
484  if(pauseAtBeginOfEvent)
485  {
486  if(requestedState==G4State_EventProc &&
488  { PauseSession("BeginOfEvent"); }
489  }
490  if(pauseAtEndOfEvent)
491  {
492  if(requestedState==G4State_GeomClosed &&
493  G4StateManager::GetStateManager()->GetPreviousState()==G4State_EventProc)
494  { PauseSession("EndOfEvent"); }
495  }
496  return true;
497 }
498 
499 //void G4UImanager::Interact()
500 //{
501 // Interact(G4String("G4> "));
502 //}
503 
504 //void G4UImanager::Interact(const char * pC)
505 //{
506 // G4cerr << "G4UImanager::Interact() is out of date and is not used anymore." << G4endl;
507 // G4cerr << "This method will be removed shortly!!!" << G4endl;
508 // G4cerr << "In case of main() use" << G4endl;
509 // G4cerr << " G4UIsession * session = new G4UIterminal;" << G4endl;
510 // G4cerr << " session->SessionStart();" << G4endl;
511 // G4cerr << "In other cases use" << G4endl;
512 // G4cerr << " G4StateManager::GetStateManager()->Pause();" << G4endl;
513 //}
514 
515 
516 
518 {
519  G4coutbuf.SetDestination(value);
520  G4cerrbuf.SetDestination(value);
521 }
522 
523 void G4UImanager::SetAlias(const char * aliasLine)
524 {
525  G4String aLine = aliasLine;
526  G4int i = aLine.index(" ");
527  G4String aliasName = aLine(0,i);
528  G4String aliasValue = aLine(i+1,aLine.length()-(i+1));
529  if(aliasValue(0)=='"')
530  {
531  G4String strippedValue;
532  if(aliasValue(aliasValue.length()-1)=='"')
533  { strippedValue = aliasValue(1,aliasValue.length()-2); }
534  else
535  { strippedValue = aliasValue(1,aliasValue.length()-1); }
536  aliasValue = strippedValue;
537  }
538 
539  aliasList->ChangeAlias(aliasName,aliasValue);
540 }
541 
542 void G4UImanager::RemoveAlias(const char * aliasName)
543 {
544  G4String aL = aliasName;
545  G4String targetAlias = aL.strip(G4String::both);
546  aliasList->RemoveAlias(targetAlias);
547 }
548 
550 {
551  aliasList->List();
552 }
553 
554 void G4UImanager::CreateHTML(const char* dir)
555 {
556  G4UIcommandTree* tr = FindDirectory(dir);
557  if(tr!=0)
558  { tr->CreateHTML(); }
559  else
560  { G4cerr << "Directory <" << dir << "> is not found." << G4endl; }
561 }
562 
564 {
565  searchDirs.clear();
566 
567  size_t idxfirst = 0;
568  size_t idxend = 0;
569  G4String pathstring = "";
570  while( (idxend = searchPath.index(':', idxfirst)) != G4String::npos) {
571  pathstring = searchPath.substr(idxfirst, idxend-idxfirst);
572  if(pathstring.size() != 0) searchDirs.push_back(pathstring);
573  idxfirst = idxend + 1;
574  }
575 
576  pathstring = searchPath.substr(idxfirst, searchPath.size()-idxfirst);
577  if(pathstring.size() != 0) searchDirs.push_back(pathstring);
578 }
579 
580 
581 static G4bool FileFound(const G4String& fname)
582 {
583  G4bool qopen = false;
584  std::ifstream fs;
585  fs.open(fname.c_str(), std::ios::in);
586  if(fs.good()) {
587  fs.close();
588  qopen = true;
589  }
590  return qopen;
591 }
592 
594 {
595  G4String macrofile = fname;
596 
597  for (size_t i = 0; i < searchDirs.size(); i++) {
598  G4String fullpath = searchDirs[i] + "/" + fname;
599  if ( FileFound(fullpath) ) {
600  macrofile = fullpath;
601  break;
602  }
603  }
604 
605  return macrofile;
606 }