source: trunk/source/intercoms/src/G4UImanager.cc@ 1157

Last change on this file since 1157 was 1016, checked in by garnier, 17 years ago

update

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