Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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// G4UImanager
27//
28// Author: Makoto Asai, 1997
29// --------------------------------------------------------------------
30
31#include "G4UImanager.hh"
32
35#include "G4StateManager.hh"
36#include "G4Threading.hh"
37#include "G4Tokenizer.hh"
38#include "G4UIaliasList.hh"
39#include "G4UIbatch.hh"
40#include "G4UIbridge.hh"
41#include "G4UIcommand.hh"
42#include "G4UIcommandTree.hh"
44#include "G4UIsession.hh"
45#include "G4UnitsMessenger.hh"
46#include "G4Filesystem.hh"
47#include "G4ios.hh"
48
49#include <fstream>
50#include <sstream>
51#include <system_error>
52
53G4bool G4UImanager::doublePrecisionStr = false;
54G4int G4UImanager::igThreadID = -1;
55
56// --------------------------------------------------------------------
57G4UImanager*& G4UImanager::fUImanager()
58{
59 G4ThreadLocalStatic G4UImanager* _instance = nullptr;
60 return _instance;
61}
62
63// --------------------------------------------------------------------
64G4bool& G4UImanager::fUImanagerHasBeenKilled()
65{
66 G4ThreadLocalStatic G4bool _instance = false;
67 return _instance;
68}
69
70// --------------------------------------------------------------------
71G4UImanager*& G4UImanager::fMasterUImanager()
72{
73 static G4UImanager* _instance = nullptr;
74 return _instance;
75}
76
77// --------------------------------------------------------------------
79{
80 if (fUImanager() == nullptr) {
81 if (!fUImanagerHasBeenKilled()) {
82 fUImanager() = new G4UImanager;
83 fUImanager()->CreateMessenger();
84 }
85 }
86 return fUImanager();
87}
88
89// --------------------------------------------------------------------
91{
92 return fMasterUImanager();
93}
94
95// --------------------------------------------------------------------
97{
98 treeTop = new G4UIcommandTree("/");
99 aliasList = new G4UIaliasList;
100 SetCoutDestination(session);
101 commandStack = new std::vector<G4String>;
102}
103
104// --------------------------------------------------------------------
105void G4UImanager::CreateMessenger()
106{
107 UImessenger = new G4UIcontrolMessenger;
108 UnitsMessenger = new G4UnitsMessenger;
109 CoutMessenger = new G4LocalThreadCoutMessenger;
110}
111
112// --------------------------------------------------------------------
114{
115 if (bridges != nullptr) {
116 for (auto bridge : *bridges) {
117 delete bridge;
118 }
119 delete bridges;
120 }
121 SetCoutDestination(nullptr);
122 histVec.clear();
123 if (saveHistory) {
124 historyFile.close();
125 }
126 delete CoutMessenger;
127 delete UnitsMessenger;
128 delete UImessenger;
129 delete treeTop;
130 delete aliasList;
131 fUImanagerHasBeenKilled() = true;
132 fUImanager() = nullptr;
133 if (commandStack != nullptr) {
134 commandStack->clear();
135 delete commandStack;
136 }
137 if (threadID >= 0) {
138 delete threadCout;
140 threadID = -1;
141 }
142}
143
144// --------------------------------------------------------------------
146{
147 doublePrecisionStr = val;
148}
149
150// --------------------------------------------------------------------
152{
153 return doublePrecisionStr;
154}
155
156// --------------------------------------------------------------------
158{
159 G4String theCommand = aCommand;
160 savedCommand = treeTop->FindPath(theCommand);
161 if (savedCommand == nullptr) {
162 G4cerr << "command not found" << G4endl;
163 return G4String();
164 }
165 return savedCommand->GetCurrentValue();
166}
167
168// --------------------------------------------------------------------
169G4String G4UImanager::GetCurrentStringValue(const char* aCommand, G4int parameterNumber,
170 G4bool reGet)
171{
172 if (reGet || savedCommand == nullptr) {
173 savedParameters = GetCurrentValues(aCommand);
174 }
175 G4Tokenizer savedToken(savedParameters);
176 G4String token;
177 for (G4int i_thParameter = 0; i_thParameter < parameterNumber; ++i_thParameter) {
178 token = savedToken();
179 if (token.empty()) {
180 return G4String();
181 }
182 if (token[(size_t)0] == '"') {
183 token.append(" ");
184 token.append(savedToken("\""));
185 }
186 }
187 return token;
188}
189
190// --------------------------------------------------------------------
191G4String G4UImanager::GetCurrentStringValue(const char* aCommand, const char* aParameterName,
192 G4bool reGet)
193{
194 if (reGet || savedCommand == nullptr) {
195 G4String parameterValues = GetCurrentValues(aCommand);
196 }
197 for (G4int i = 0; i < (G4int)savedCommand->GetParameterEntries(); ++i) {
198 if (aParameterName == savedCommand->GetParameter(i)->GetParameterName()) {
199 return GetCurrentStringValue(aCommand, i + 1, false);
200 }
201 }
202 return G4String();
203}
204
205// --------------------------------------------------------------------
206G4int G4UImanager::GetCurrentIntValue(const char* aCommand, const char* aParameterName,
207 G4bool reGet)
208{
209 G4String targetParameter = GetCurrentStringValue(aCommand, aParameterName, reGet);
210 G4int value;
211 const char* t = targetParameter;
212 std::istringstream is(t);
213 is >> value;
214 return value;
215}
216
217// --------------------------------------------------------------------
218G4int G4UImanager::GetCurrentIntValue(const char* aCommand, G4int parameterNumber, G4bool reGet)
219{
220 G4String targetParameter = GetCurrentStringValue(aCommand, parameterNumber, reGet);
221 G4int value;
222 const char* t = targetParameter;
223 std::istringstream is(t);
224 is >> value;
225 return value;
226}
227
228// --------------------------------------------------------------------
229G4double G4UImanager::GetCurrentDoubleValue(const char* aCommand, const char* aParameterName,
230 G4bool reGet)
231{
232 G4String targetParameter = GetCurrentStringValue(aCommand, aParameterName, reGet);
233 G4double value;
234 const char* t = targetParameter;
235 std::istringstream is(t);
236 is >> value;
237 return value;
238}
239
240// --------------------------------------------------------------------
241G4double G4UImanager::GetCurrentDoubleValue(const char* aCommand, G4int parameterNumber,
242 G4bool reGet)
243{
244 G4String targetParameter = GetCurrentStringValue(aCommand, parameterNumber, reGet);
245 G4double value;
246 const char* t = targetParameter;
247 std::istringstream is(t);
248 is >> value;
249 return value;
250}
251
252// --------------------------------------------------------------------
254{
255 treeTop->AddNewCommand(newCommand);
256 if (fMasterUImanager() != nullptr && G4Threading::G4GetThreadId() == 0) {
257 fMasterUImanager()->AddWorkerCommand(newCommand);
258 }
259}
260
261// --------------------------------------------------------------------
262void G4UImanager::AddWorkerCommand(G4UIcommand* newCommand)
263{
264 treeTop->AddNewCommand(newCommand, true);
265}
266
267// --------------------------------------------------------------------
269{
270 treeTop->RemoveCommand(aCommand);
271 if (fMasterUImanager() != nullptr && G4Threading::G4GetThreadId() == 0) {
272 fMasterUImanager()->RemoveWorkerCommand(aCommand);
273 }
274}
275
276// --------------------------------------------------------------------
277void G4UImanager::RemoveWorkerCommand(G4UIcommand* aCommand)
278{
279 treeTop->RemoveCommand(aCommand, true);
280}
281
282// --------------------------------------------------------------------
283void G4UImanager::ExecuteMacroFile(const char* fileName)
284{
285 G4UIsession* batchSession = new G4UIbatch(fileName, session);
286 session = batchSession;
287 lastRC = 0;
288 G4UIsession* previousSession = session->SessionStart();
289 lastRC = session->GetLastReturnCode();
290 delete session;
291 session = previousSession;
292}
293
294// --------------------------------------------------------------------
295void G4UImanager::LoopS(const char* valueList)
296{
297 G4String vl = valueList;
298 G4Tokenizer parameterToken(vl);
299 G4String mf = parameterToken();
300 G4String vn = parameterToken();
301 G4String c1 = parameterToken();
302 c1 += " ";
303 c1 += parameterToken();
304 c1 += " ";
305 c1 += parameterToken();
306 const char* t1 = c1;
307 std::istringstream is(t1);
308 G4double d1;
309 G4double d2;
310 G4double d3;
311 is >> d1 >> d2 >> d3;
312 Loop(mf, vn, d1, d2, d3);
313}
314
315// --------------------------------------------------------------------
316void G4UImanager::Loop(const char* macroFile, const char* variableName, G4double initialValue,
317 G4double finalValue, G4double stepSize)
318{
319 G4String cd;
320 if (stepSize > 0) {
321 for (G4double d = initialValue; d <= finalValue; d += stepSize) {
322 std::ostringstream os;
323 os << d;
324 cd += os.str();
325 cd += " ";
326 }
327 }
328 else {
329 for (G4double d = initialValue; d >= finalValue; d += stepSize) {
330 std::ostringstream os;
331 os << d;
332 cd += os.str();
333 cd += " ";
334 }
335 }
336 Foreach(macroFile, variableName, cd);
337}
338
339// --------------------------------------------------------------------
340void G4UImanager::ForeachS(const char* valueList)
341{
342 const G4String& vl = valueList;
343 G4Tokenizer parameterToken(vl);
344 const G4String& mf = parameterToken();
345 const G4String& vn = parameterToken();
346 G4String c1 = parameterToken();
347 G4String ca;
348 while (!((ca = parameterToken()).empty())) {
349 c1 += " ";
350 c1 += ca;
351 }
352
353 G4String aliasValue = std::move(c1);
354 if (aliasValue[0] == '"') {
355 G4String strippedValue;
356 if (aliasValue.back() == '"') {
357 strippedValue = aliasValue.substr(1, aliasValue.length() - 2);
358 }
359 else {
360 strippedValue = aliasValue.substr(1, aliasValue.length() - 1);
361 }
362 aliasValue = std::move(strippedValue);
363 }
364
365 // Foreach(mf,vn,c1);
366 Foreach(mf, vn, aliasValue);
367}
368
369// --------------------------------------------------------------------
370void G4UImanager::Foreach(const char* macroFile, const char* variableName, const char* candidates)
371{
372 G4String candidatesString = candidates;
373 G4Tokenizer parameterToken(candidatesString);
374 G4String cd;
375 while (!((cd = parameterToken()).empty())) {
376 G4String vl = variableName;
377 vl += " ";
378 vl += cd;
379 SetAlias(vl);
381 if (lastRC != 0) {
383 ed << "Loop aborted due to a command execution error - "
384 << "error code " << lastRC;
385 G4Exception("G4UImanager::Foreach", "UIMAN0201", JustWarning, ed);
386 break;
387 }
388 }
389}
390
391// --------------------------------------------------------------------
393{
394 G4String aCommand = aCmd;
395 std::size_t ia = aCommand.find('{');
396 std::size_t iz = aCommand.find('#');
397 while ((ia != std::string::npos) && ((iz == std::string::npos) || (ia < iz))) {
398 G4int ibx = -1;
399 while (ibx < 0) {
400 std::size_t ib = aCommand.find('}');
401 if (ib == std::string::npos) {
402 G4cerr << aCommand << G4endl;
403 for (std::size_t i = 0; i < ia; ++i) {
404 G4cerr << " ";
405 }
406 G4cerr << "^" << G4endl;
407 G4cerr << "Unmatched alias parenthesis -- command ignored" << G4endl;
408 G4String nullStr;
409 return nullStr;
410 }
411 G4String ps = aCommand.substr(ia + 1, aCommand.length() - (ia + 1));
412 std::size_t ic = ps.find('{');
413 std::size_t id = ps.find('}');
414 if (ic != std::string::npos && ic < id) {
415 ia += ic + 1;
416 }
417 else {
418 ibx = (G4int)ib;
419 }
420 }
421 //--- Here ia represents the position of innermost "{"
422 //--- and ibx represents corresponding "}"
423 G4String subs;
424 if (ia > 0) {
425 subs = aCommand.substr(0, ia);
426 }
427 G4String alis = aCommand.substr(ia + 1, ibx - ia - 1);
428 G4String rems = aCommand.substr(ibx + 1, aCommand.length() - ibx);
429 const G4String* alVal = aliasList->FindAlias(alis);
430 if (alVal == nullptr) {
431 G4cerr << "Alias <" << alis << "> not found -- command ignored" << G4endl;
432 G4String nullStr;
433 return nullStr;
434 }
435 aCommand = subs + (*alVal) + rems;
436 ia = aCommand.find('{');
437 }
438 return aCommand;
439}
440
441// --------------------------------------------------------------------
443{
444 return ApplyCommand(aCmd.data());
445}
446
447// --------------------------------------------------------------------
449{
450 G4String aCommand = aCmd;
451 if(fRecordDepth<0)
452 {
453 // while recording a command to a macro file, skip solving and record the command as-is
454 aCommand = SolveAlias(aCmd);
455 if (aCommand.empty()) {
456 return fAliasNotFound;
457 }
458 }
459 if (verboseLevel != 0) {
461 fLastCommandOutputTreated = false;
462 }
463 G4cout << aCommand << G4endl;
464 }
465 G4String commandString;
466 G4String commandParameter;
467
468 std::size_t iAt = aCommand.find('@');
469 if (iAt != std::string::npos) {
470 G4String commandStr1 = aCommand.substr(0,iAt);
471 G4String commandStr2 = aCommand.substr(iAt+1,aCommand.length() - (iAt + 1));
472 std::size_t iAt2 = commandStr2.find('@');
473 G4String tmpFileName;
474 G4bool tmpFile = false;
475 if (iAt2 != std::string::npos) {
476 if(iAt2 == 0) {
477 // Two '@'s are connected. temporal file will be created
478 tmpFileName = "tmptmp_";
479 tmpFileName += G4UIcommand::ConvertToString(fRecordDepth+1);
480 tmpFileName += ".tmpmac";
481 tmpFile = true;
482 } else {
483 tmpFileName = commandStr2.substr(0,iAt2);
484 }
485 } else {
486 return fAliasNotFound;
487 }
488 G4String commandStr3 = commandStr2.substr(iAt2+1,commandStr2.length()-(iAt2+1));
489 G4String revisedCommand = commandStr1;
490 revisedCommand += " ";
491 revisedCommand += tmpFileName;
492 revisedCommand += " ";
493 revisedCommand += commandStr3;
494 StartRecording(tmpFileName,false,tmpFile,revisedCommand);
495 return fCommandSucceeded;
496 }
497
498 std::size_t i = aCommand.find(' ');
499 if (i != std::string::npos) {
500 commandString = aCommand.substr(0, i);
501 commandParameter = aCommand.substr(i + 1, aCommand.length() - (i + 1));
502 }
503 else {
504 commandString = aCommand;
505 }
506
507 // remove doubled slash
508 std::size_t len = commandString.length();
509 std::size_t ll = 0;
510 G4String a1;
511 G4String a2;
512 while (ll < len - 1) {
513 if (commandString.substr(ll, 2) == "//") {
514 if (ll == 0) {
515 // Safe because index argument always 0
516 commandString.erase(ll, 1);
517 }
518 else {
519 a1 = commandString.substr(0, ll);
520 a2 = commandString.substr(ll + 1, len - ll - 1);
521 commandString = a1 + a2;
522 }
523 --len;
524 }
525 else {
526 ++ll;
527 }
528 }
529
530 if (isMaster && bridges != nullptr) {
531 for (auto bridge : *bridges) {
532 G4int leng = bridge->DirLength();
533 if (commandString.substr(0, leng) == bridge->DirName()) {
534 return bridge->LocalUI()->ApplyCommand(commandString + " " + commandParameter);
535 }
536 }
537 }
538
539 G4UIcommand* targetCommand = treeTop->FindPath(commandString);
540 if (targetCommand == nullptr) {
541 if (ignoreCmdNotFound) {
542 if (stackCommandsForBroadcast) {
543 commandStack->push_back(commandString + " " + commandParameter);
544 }
545 return fCommandSucceeded;
546 }
547
548 return fCommandNotFound;
549 }
550
551 if (stackCommandsForBroadcast && targetCommand->ToBeBroadcasted()) {
552 commandStack->push_back(commandString + " " + commandParameter);
553 }
554
555 if (!(targetCommand->IsAvailable())) {
557 }
558
559 if (saveHistory) {
560 historyFile << aCommand << G4endl;
561 }
562 if (G4int(histVec.size()) >= maxHistSize) {
563 histVec.erase(histVec.begin());
564 }
565 histVec.push_back(aCommand);
566
567 if(fRecordDepth>=0) {
568 if(aCommand == "/control/endRecord") {
569 EndRecording();
570 return fCommandSucceeded;
571 } else if(commandString != "/control/recordToMacro") {
572 RecordCommand(aCommand);
573 return fCommandSucceeded;
574 }
575 }
576
577 targetCommand->ResetFailure();
578 G4int commandFailureCode = targetCommand->DoIt(commandParameter);
579 if (commandFailureCode == 0) {
580 G4int additionalFailureCode = targetCommand->IfCommandFailed();
581 if (additionalFailureCode > 0) {
583 msg << targetCommand->GetFailureDescription() << "\n"
584 << "Error code : " << additionalFailureCode;
585 G4Exception("G4UImanager::ApplyCommand", "UIMAN0123", JustWarning, msg);
586 commandFailureCode += additionalFailureCode;
587 }
588 }
589 return commandFailureCode;
590}
591
592// --------------------------------------------------------------------
594{
595 return FindCommand(aCmd.data());
596}
597
598// --------------------------------------------------------------------
600{
601 const G4String& aCommand = SolveAlias(aCmd);
602 if (aCommand.empty()) {
603 return nullptr;
604 }
605
606 G4String commandString;
607
608 std::size_t i = aCommand.find(' ');
609 if (i != std::string::npos) {
610 commandString = aCommand.substr(0, i);
611 }
612 else {
613 commandString = aCommand;
614 }
615
616 return treeTop->FindPath(commandString);
617}
618
619// --------------------------------------------------------------------
620void G4UImanager::StoreHistory(const char* fileName)
621{
622 StoreHistory(true, fileName);
623}
624
625// --------------------------------------------------------------------
626void G4UImanager::StoreHistory(G4bool historySwitch, const char* fileName)
627{
628 if (historySwitch) {
629 if (saveHistory) {
630 historyFile.close();
631 }
632 historyFile.open((char*)fileName);
633 saveHistory = true;
634 }
635 else {
636 historyFile.close();
637 saveHistory = false;
638 }
639 saveHistory = historySwitch;
640}
641
642// --------------------------------------------------------------------
643void G4UImanager::PauseSession(const char* msg)
644{
645 if (session != nullptr) {
646 session->PauseSessionStart(msg);
647 }
648}
649
650// --------------------------------------------------------------------
651void G4UImanager::ListCommands(const char* direct)
652{
653 G4UIcommandTree* comTree = FindDirectory(direct);
654 if (comTree != nullptr) {
655 comTree->List();
656 }
657 else {
658 G4cout << direct << " is not found." << G4endl;
659 }
660}
661
662// --------------------------------------------------------------------
663G4UIcommandTree* G4UImanager::FindDirectory(const char* dirName)
664{
665 const G4String& aDirName = dirName;
666 G4String targetDir = G4StrUtil::strip_copy(aDirName);
667 if (targetDir.back() != '/') {
668 targetDir += "/";
669 }
670 G4UIcommandTree* comTree = treeTop;
671 if (targetDir == "/") {
672 return comTree;
673 }
674 std::size_t idx = 1;
675 while (idx < targetDir.length() - 1) {
676 std::size_t i = targetDir.find('/', idx);
677 G4String targetDirString = targetDir.substr(0, i + 1);
678 comTree = comTree->GetTree(targetDirString);
679 if (comTree == nullptr) {
680 return nullptr;
681 }
682 idx = i + 1;
683 }
684 return comTree;
685}
686
687// --------------------------------------------------------------------
689{
690 if (pauseAtBeginOfEvent) {
691 if (requestedState == G4State_EventProc
692 && G4StateManager::GetStateManager()->GetPreviousState() == G4State_GeomClosed)
693 {
694 PauseSession("BeginOfEvent");
695 }
696 }
697 if (pauseAtEndOfEvent) {
698 if (requestedState == G4State_GeomClosed
699 && G4StateManager::GetStateManager()->GetPreviousState() == G4State_EventProc)
700 {
701 PauseSession("EndOfEvent");
702 }
703 }
704 return true;
705}
706
707// --------------------------------------------------------------------
709{
710 G4iosSetDestination(value);
711}
712
713// --------------------------------------------------------------------
714void G4UImanager::SetAlias(const char* aliasLine)
715{
716 const G4String& aLine = aliasLine;
717 std::size_t i = aLine.find(' ');
718 const G4String& aliasName = aLine.substr(0, i);
719 G4String aliasValue = aLine.substr(i + 1, aLine.length() - (i + 1));
720 if (aliasValue[0] == '"') {
721 G4String strippedValue;
722 if (aliasValue.back() == '"') {
723 strippedValue = aliasValue.substr(1, aliasValue.length() - 2);
724 }
725 else {
726 strippedValue = aliasValue.substr(1, aliasValue.length() - 1);
727 }
728 aliasValue = std::move(strippedValue);
729 }
730
731 aliasList->ChangeAlias(aliasName, aliasValue);
732}
733
734// --------------------------------------------------------------------
735void G4UImanager::RemoveAlias(const char* aliasName)
736{
737 const G4String& aL = aliasName;
738 G4String targetAlias = G4StrUtil::strip_copy(aL);
739 aliasList->RemoveAlias(targetAlias);
740}
741
742// --------------------------------------------------------------------
744{
745 aliasList->List();
746}
747
748// --------------------------------------------------------------------
749void G4UImanager::CreateHTML(const char* dir)
750{
751 G4UIcommandTree* tr = FindDirectory(dir);
752 if (tr != nullptr) {
753 tr->CreateHTML();
754 }
755 else {
756 G4cerr << "Directory <" << dir << "> is not found." << G4endl;
757 }
758}
759
760// --------------------------------------------------------------------
762{
763 searchDirs.clear();
764
765 std::size_t idxfirst = 0;
766 std::size_t idxend = 0;
767 G4String pathstring = "";
768 while ((idxend = searchPath.find(':', idxfirst)) != G4String::npos) {
769 pathstring = searchPath.substr(idxfirst, idxend - idxfirst);
770 if (!pathstring.empty()) {
771 searchDirs.push_back(pathstring);
772 }
773 idxfirst = idxend + 1;
774 }
775
776 pathstring = searchPath.substr(idxfirst, searchPath.size() - idxfirst);
777 if (!pathstring.empty()) {
778 searchDirs.push_back(std::move(pathstring));
779 }
780}
781
782// --------------------------------------------------------------------
783static G4bool FileFound(const G4String& fname)
784{
785 G4bool qopen = false;
786 std::ifstream fs;
787 fs.open(fname.c_str(), std::ios::in);
788 if (fs.good()) {
789 fs.close();
790 qopen = true;
791 }
792 return qopen;
793}
794
795// --------------------------------------------------------------------
797{
798 G4String macrofile = fname;
799
800 for (const auto& searchDir : searchDirs) {
801 const G4String& fullpath = searchDir + "/" + fname;
802 if (FileFound(fullpath)) {
803 macrofile = fullpath;
804 break;
805 }
806 }
807 return macrofile;
808}
809
810// --------------------------------------------------------------------
811std::vector<G4String>* G4UImanager::GetCommandStack()
812{
813 std::vector<G4String>* returnValue = commandStack;
814 commandStack = new std::vector<G4String>;
815 return returnValue;
816}
817
818// --------------------------------------------------------------------
820{
821 if (brg->LocalUI() == this) {
822 G4Exception("G4UImanager::RegisterBridge()", "UI7002", FatalException,
823 "G4UIBridge cannot bridge between same object.");
824 }
825 else {
826 bridges->push_back(brg);
827 }
828}
829
830// --------------------------------------------------------------------
832{
833 threadID = tId;
835 threadCout = new G4MTcoutDestination(threadID);
836 threadCout->SetIgnoreCout(igThreadID);
837}
838
839// --------------------------------------------------------------------
841{
845 threadCout = new G4MTcoutDestination(threadID);
846 threadCout->SetPrefixString(pref);
847 threadCout->SetIgnoreCout(igThreadID);
848}
849
850// --------------------------------------------------------------------
851void G4UImanager::SetCoutFileName(const G4String& fileN, G4bool ifAppend)
852{
853 // for sequential mode, ignore this method.
854 if (threadID < 0) {
855 return;
856 }
857
858 if (fileN == "**Screen**") {
859 threadCout->SetCoutFileName(fileN, ifAppend);
860 }
861 else {
862 std::stringstream fn;
863 fn << "G4W_" << threadID << "_" << fileN;
864 threadCout->SetCoutFileName(fn.str(), ifAppend);
865 }
866}
867
868// --------------------------------------------------------------------
869void G4UImanager::SetCerrFileName(const G4String& fileN, G4bool ifAppend)
870{
871 // for sequential mode, ignore this method.
872 if (threadID < 0) {
873 return;
874 }
875
876 if (fileN == "**Screen**") {
877 threadCout->SetCerrFileName(fileN, ifAppend);
878 }
879 else {
880 std::stringstream fn;
881 fn << "G4W_" << threadID << "_" << fileN;
882 threadCout->SetCerrFileName(fn.str(), ifAppend);
883 }
884}
885
886// --------------------------------------------------------------------
888{
889 // for sequential mode, ignore this method.
890 if (threadID < 0) {
891 return;
892 }
893 threadCout->SetPrefixString(s);
894}
895
896// --------------------------------------------------------------------
898{
899 // for sequential mode, ignore this method.
900 if (threadID < 0) {
901 return;
902 }
903 threadCout->EnableBuffering(flg);
904}
905
906// --------------------------------------------------------------------
908{
909 // for sequential mode, ignore this method.
910 if (threadID < 0) {
911 igThreadID = tid;
912 return;
913 }
914 threadCout->SetIgnoreCout(tid);
915}
916
917// --------------------------------------------------------------------
919{
920 // for sequential mode, ignore this method.
921 if (threadID < 0) {
922 return;
923 }
924 threadCout->SetIgnoreInit(flg);
925}
926
927// --------------------------------------------------------------------
929{
930 // There may be no session - pure batch mode (session == nullptr)
931 // If there is a session, it may be a batch session used for processing macros.
932 // Find base session of this hierarchy of batch sessions.
933 G4UIsession* baseSession = session;
934 while (auto aBatchSession = dynamic_cast<G4UIbatch*>(baseSession)) {
935 auto previousSession = aBatchSession->GetPreviousSession();
936 if (previousSession == nullptr) {
937 // No previouse session - aBatchSession is the desired base session
938 baseSession = aBatchSession;
939 break;
940 }
941 // There is a previous session, which may or may not be a batch.
942 // If not, it will be our desired base - so record and test again.
943 baseSession = previousSession;
944 }
945 return baseSession;
946}
947
948// --------------------------------------------------------------------
949void G4UImanager::StartRecording(G4String fn, G4bool ifAppend, G4bool ifTemp, G4String assocCmd)
950{
951 fRecordDepth++;
952 fRecordFileName.push_back(std::pair<G4String,G4bool>(fn,ifTemp));
953 fAccosiatedCommand.push_back(assocCmd);
954 G4cout << "G4UImanager::StartRecording [" << fRecordDepth << "] " << fn << G4endl;
955 auto mode = std::ios_base::out;
956 if(ifAppend) mode = std::ios_base::app;
957 auto rf = new std::ofstream;
958 rf->open(fn,mode);
959 fRecordFile.push_back(rf);
960}
961
962// --------------------------------------------------------------------
964{
965 *(fRecordFile[fRecordDepth]) << aCommand << G4endl;
966}
967
968// --------------------------------------------------------------------
970{
971 G4int retVal = fCommandSucceeded;
972 fRecordFile[fRecordDepth]->close();
973 delete fRecordFile[fRecordDepth];
974 fRecordFile.pop_back();
975 G4String assocCmd = fAccosiatedCommand[fRecordDepth];
976 fAccosiatedCommand.pop_back();
977 G4cout << "G4UImanager::EndRecording [" << fRecordDepth << "] "
978 << fRecordFileName[fRecordDepth].first << G4endl;
979 fRecordDepth--;
980 if(assocCmd!="**NOCMD**") {
981 retVal = ApplyCommand(assocCmd);
982 }
983 if(fRecordDepth<0) {
984 while(fRecordFileName.size()>0) {
985 G4String fn = fRecordFileName.back().first;
986 G4bool ifTemp = fRecordFileName.back().second;
987 fRecordFileName.pop_back();
988 if(ifTemp) {
989 std::error_code ec;
990 G4bool res = G4fs::remove(fn.c_str(), ec);
991 if(!res) {
993 ed << "Error removing temporary macro file " << fn << " : "
994 << ec.message();
995 G4Exception("G4UImanager::EndRecording()", "UIMAN0801", JustWarning, ed);
996 }
997 }
998 }
999 }
1000 return retVal;
1001}
1002
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
@ fCommandNotFound
@ fAliasNotFound
@ fIllegalApplicationState
@ fCommandSucceeded
void G4iosFinalization()
Definition G4ios.cc:297
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
void G4iosSetDestination(G4coutDestination *sink)
Definition G4ios.cc:305
G4GLOB_DLL std::ostream G4cout
void G4iosInitialization()
Definition G4ios.cc:296
static G4StateManager * GetStateManager()
G4UImanager * LocalUI() const
Definition G4UIbridge.hh:54
G4UIcommandTree * GetTree(G4int i)
void AddNewCommand(G4UIcommand *newCommand, G4bool workerThreadOnly=false)
void CreateHTML(const G4String &="")
void RemoveCommand(G4UIcommand *aCommand, G4bool workerThreadOnly=false)
G4bool ToBeBroadcasted() const
G4int IfCommandFailed()
virtual G4int DoIt(const G4String &parameterList)
static G4String ConvertToString(G4bool boolVal)
void ResetFailure()
G4bool IsAvailable()
const G4String & GetFailureDescription()
~G4UImanager() override
static G4bool DoublePrecisionStr()
void SetCerrFileName(const G4String &fileN="G4cerr.txt", G4bool ifAppend=true)
void SetCoutDestination(G4UIsession *const value)
static void UseDoublePrecisionStr(G4bool val)
void ForeachS(const char *valueList)
void SetUpForAThread(G4int tId)
std::vector< G4String > * GetCommandStack()
void Foreach(const char *macroFile, const char *variableName, const char *candidates)
G4int ApplyCommand(const char *aCommand)
void SetThreadIgnoreInit(G4bool flg=true)
void CreateHTML(const char *dir="/")
void Loop(const char *macroFile, const char *variableName, G4double initialValue, G4double finalValue, G4double stepSize=1.0)
G4int GetCurrentIntValue(const char *aCommand, G4int parameterNumber=1, G4bool reGet=true)
void SetThreadPrefixString(const G4String &prefix="W")
void LoopS(const char *valueList)
void StoreHistory(const char *fileName="G4history.macro")
void ListCommands(const char *direc)
G4double GetCurrentDoubleValue(const char *aCommand, G4int parameterNumber=1, G4bool reGet=true)
void ExecuteMacroFile(const char *fileName)
void SetCoutFileName(const G4String &fileN="G4cout.txt", G4bool ifAppend=true)
static G4UImanager * GetMasterUIpointer()
G4String GetCurrentStringValue(const char *aCommand, G4int parameterNumber=1, G4bool reGet=true)
void SetUpForSpecialThread(const G4String &aPrefix)
void AddNewCommand(G4UIcommand *newCommand)
G4String GetCurrentValues(const char *aCommand)
void SetThreadIgnore(G4int tid=0)
void SetAlias(const char *aliasLine)
G4String FindMacroPath(const G4String &fname) const
void RemoveAlias(const char *aliasName)
G4bool Notify(G4ApplicationState requestedState) override
G4String SolveAlias(const char *aCmd)
void ListAlias()
void RemoveCommand(G4UIcommand *aCommand)
G4UIcommand * FindCommand(const char *aCommand)
void ParseMacroSearchPath()
static G4UImanager * GetUIpointer()
void StartRecording(G4String fn, G4bool ifAppend, G4bool ifTemp=false, G4String assocCmd="**NOCMD**")
void RegisterBridge(G4UIbridge *brg)
G4int EndRecording()
G4UImanager(const G4UImanager &)=delete
G4UIsession * GetBaseSession() const
void SetThreadUseBuffer(G4bool flg=true)
void RecordCommand(const G4String &aCommand)
virtual G4UIsession * SessionStart()
G4int GetLastReturnCode() const
G4VStateDependent(G4bool bottom=false)
G4String strip_copy(G4String str, char ch=' ')
Return copy of string with leading and trailing characters removed.
G4bool IsMasterThread()
G4int G4GetThreadId()
void G4SetThreadId(G4int aNewValue)
#define G4ThreadLocalStatic
Definition tls.hh:76