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

Last change on this file since 1218 was 1196, checked in by garnier, 16 years ago

update CVS release candidate geant4.9.3.01

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