Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/intercoms/src/G4UImanager.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /intercoms/src/G4UImanager.cc (Version 11.3.0) and /intercoms/src/G4UImanager.cc (Version 1.0)


                                                   >>   1 // This code implementation is the intellectual property of
                                                   >>   2 // the GEANT4 collaboration.
  1 //                                                  3 //
  2 // ******************************************* <<   4 // By copying, distributing or modifying the Program (or any work
  3 // * License and Disclaimer                    <<   5 // based on the Program) you indicate your acceptance of this statement,
  4 // *                                           <<   6 // and all its terms.
  5 // * The  Geant4 software  is  copyright of th << 
  6 // * the Geant4 Collaboration.  It is provided << 
  7 // * conditions of the Geant4 Software License << 
  8 // * LICENSE and available at  http://cern.ch/ << 
  9 // * include a list of copyright holders.      << 
 10 // *                                           << 
 11 // * Neither the authors of this software syst << 
 12 // * institutes,nor the agencies providing fin << 
 13 // * work  make  any representation or  warran << 
 14 // * regarding  this  software system or assum << 
 15 // * use.  Please see the license in the file  << 
 16 // * for the full disclaimer and the limitatio << 
 17 // *                                           << 
 18 // * This  code  implementation is the result  << 
 19 // * technical work of the GEANT4 collaboratio << 
 20 // * By using,  copying,  modifying or  distri << 
 21 // * any work based  on the software)  you  ag << 
 22 // * use  in  resulting  scientific  publicati << 
 23 // * acceptance of all terms of the Geant4 Sof << 
 24 // ******************************************* << 
 25 //                                                  7 //
 26 // G4UImanager                                 <<   8 // $Id: G4UImanager.cc,v 1.3.2.1 1999/12/07 20:49:03 gunter Exp $
                                                   >>   9 // GEANT4 tag $Name: geant4-01-00 $
 27 //                                                 10 //
 28 // Author: Makoto Asai, 1997                   <<  11 // 
 29 // ------------------------------------------- <<  12 // ---------------------------------------------------------------------
 30                                                    13 
 31 #include "G4UImanager.hh"                          14 #include "G4UImanager.hh"
 32                                                <<  15 #include "G4UIcommandStatus.hh"
 33 #include "G4LocalThreadCoutMessenger.hh"       << 
 34 #include "G4MTcoutDestination.hh"              << 
 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"                      16 #include "G4UIcommandTree.hh"
 43 #include "G4UIcontrolMessenger.hh"             <<  17 #include "G4UIcommand.hh"
 44 #include "G4UIsession.hh"                          18 #include "G4UIsession.hh"
                                                   >>  19 #include "G4UIbatch.hh"
                                                   >>  20 #include "G4UIcontrolMessenger.hh"
 45 #include "G4UnitsMessenger.hh"                     21 #include "G4UnitsMessenger.hh"
 46 #include "G4ios.hh"                                22 #include "G4ios.hh"
                                                   >>  23 #include "G4strstreambuf.hh"
 47                                                    24 
 48 #include <fstream>                             <<  25 #ifdef WIN32
 49 #include <sstream>                             <<  26 #  include <Strstrea.h>
 50                                                <<  27 #else
 51 G4bool G4UImanager::doublePrecisionStr = false <<  28 #  include <strstream.h>
 52 G4int G4UImanager::igThreadID = -1;            <<  29 #endif
 53                                                <<  30 
 54 // ------------------------------------------- <<  31 G4UImanager * G4UImanager::fUImanager = 0;
 55 G4UImanager*& G4UImanager::fUImanager()        <<  32 G4bool G4UImanager::fUImanagerHasBeenKilled = false;
 56 {                                              <<  33 
 57   G4ThreadLocalStatic G4UImanager* _instance = <<  34 G4UImanager * G4UImanager::GetUIpointer()
 58   return _instance;                            <<  35 {
 59 }                                              <<  36   if(!fUImanager) 
 60                                                <<  37   {
 61 // ------------------------------------------- <<  38     if(!fUImanagerHasBeenKilled)
 62 G4bool& G4UImanager::fUImanagerHasBeenKilled() <<  39     {
 63 {                                              <<  40       fUImanager = new G4UImanager;
 64   G4ThreadLocalStatic G4bool _instance = false <<  41       fUImanager->CreateMessenger();
 65   return _instance;                            << 
 66 }                                              << 
 67                                                << 
 68 // ------------------------------------------- << 
 69 G4UImanager*& G4UImanager::fMasterUImanager()  << 
 70 {                                              << 
 71   static G4UImanager* _instance = nullptr;     << 
 72   return _instance;                            << 
 73 }                                              << 
 74                                                << 
 75 // ------------------------------------------- << 
 76 G4UImanager* G4UImanager::GetUIpointer()       << 
 77 {                                              << 
 78   if (fUImanager() == nullptr) {               << 
 79     if (!fUImanagerHasBeenKilled()) {          << 
 80       fUImanager() = new G4UImanager;          << 
 81       fUImanager()->CreateMessenger();         << 
 82     }                                              42     }
 83   }                                                43   }
 84   return fUImanager();                         <<  44   return fUImanager;
 85 }                                                  45 }
 86                                                    46 
 87 // ------------------------------------------- <<  47 G4UImanager::G4UImanager():savedCommand(NULL)
 88 G4UImanager* G4UImanager::GetMasterUIpointer() << 
 89 {                                              << 
 90   return fMasterUImanager();                   << 
 91 }                                              << 
 92                                                << 
 93 // ------------------------------------------- << 
 94 G4UImanager::G4UImanager() : G4VStateDependent << 
 95 {                                                  48 {
 96   treeTop = new G4UIcommandTree("/");              49   treeTop = new G4UIcommandTree("/");
 97   aliasList = new G4UIaliasList;               <<  50   G4String nullString;
                                                   >>  51   savedParameters = nullString;
                                                   >>  52   verboseLevel = 0;
                                                   >>  53   saveHistory = false;
                                                   >>  54   session = NULL;
 98   SetCoutDestination(session);                     55   SetCoutDestination(session);
 99   commandStack = new std::vector<G4String>;    << 
100 }                                                  56 }
101                                                    57 
102 // ------------------------------------------- << 
103 void G4UImanager::CreateMessenger()                58 void G4UImanager::CreateMessenger()
104 {                                                  59 {
105   UImessenger = new G4UIcontrolMessenger;          60   UImessenger = new G4UIcontrolMessenger;
106   UnitsMessenger = new G4UnitsMessenger;           61   UnitsMessenger = new G4UnitsMessenger;
107   CoutMessenger = new G4LocalThreadCoutMesseng << 
108 }                                                  62 }
109                                                    63 
110 // ------------------------------------------- << 
111 G4UImanager::~G4UImanager()                        64 G4UImanager::~G4UImanager()
112 {                                                  65 {
113   if (bridges != nullptr) {                    <<  66   SetCoutDestination(NULL);
114     for (auto bridge : *bridges) {             << 
115       delete bridge;                           << 
116     }                                          << 
117     delete bridges;                            << 
118   }                                            << 
119   SetCoutDestination(nullptr);                 << 
120   histVec.clear();                                 67   histVec.clear();
121   if (saveHistory) {                           <<  68   if(saveHistory) historyFile.close();
122     historyFile.close();                       << 
123   }                                            << 
124   delete CoutMessenger;                        << 
125   delete UnitsMessenger;                       << 
126   delete UImessenger;                              69   delete UImessenger;
                                                   >>  70   delete UnitsMessenger;
127   delete treeTop;                                  71   delete treeTop;
128   delete aliasList;                            <<  72   fUImanagerHasBeenKilled = true;
129   fUImanagerHasBeenKilled() = true;            <<  73   fUImanager = NULL;
130   fUImanager() = nullptr;                      << 
131   if (commandStack != nullptr) {               << 
132     commandStack->clear();                     << 
133     delete commandStack;                       << 
134   }                                            << 
135   if (threadID >= 0) {                         << 
136     delete threadCout;                         << 
137     G4iosFinalization();                       << 
138     threadID = -1;                             << 
139   }                                            << 
140 }                                                  74 }
141                                                    75 
142 // ------------------------------------------- <<  76 G4UImanager::G4UImanager(const G4UImanager &right) { }
143 void G4UImanager::UseDoublePrecisionStr(G4bool <<  77 const G4UImanager & G4UImanager::operator=(const G4UImanager &right)
144 {                                              <<  78 { return right; }
145   doublePrecisionStr = val;                    <<  79 int G4UImanager::operator==(const G4UImanager &right) const
146 }                                              <<  80 { return false; }
                                                   >>  81 int G4UImanager::operator!=(const G4UImanager &right) const
                                                   >>  82 { return true; }
147                                                    83 
148 // ------------------------------------------- <<  84 G4String G4UImanager::GetCurrentValues(const char * aCommand)
149 G4bool G4UImanager::DoublePrecisionStr()       << 
150 {                                              << 
151   return doublePrecisionStr;                   << 
152 }                                              << 
153                                                << 
154 // ------------------------------------------- << 
155 G4String G4UImanager::GetCurrentValues(const c << 
156 {                                                  85 {
157   G4String theCommand = aCommand;                  86   G4String theCommand = aCommand;
158   savedCommand = treeTop->FindPath(theCommand) <<  87   savedCommand = treeTop->FindPath( theCommand );
159   if (savedCommand == nullptr) {               <<  88   if( savedCommand == NULL ) 
160     G4cerr << "command not found" << G4endl;   <<  89   {
                                                   >>  90     G4cerr << "command not found" << endl;
161     return G4String();                             91     return G4String();
162   }                                                92   }
163   return savedCommand->GetCurrentValue();          93   return savedCommand->GetCurrentValue();
164 }                                                  94 }
165                                                    95 
166 // ------------------------------------------- <<  96 G4String G4UImanager::GetCurrentStringValue(const char * aCommand, 
167 G4String G4UImanager::GetCurrentStringValue(co <<  97 int parameterNumber, G4bool reGet)
168                                             G4 << 
169 {                                                  98 {
170   if (reGet || savedCommand == nullptr) {      <<  99   if(reGet || savedCommand == NULL)
171     savedParameters = GetCurrentValues(aComman << 100   {
                                                   >> 101     savedParameters = GetCurrentValues( aCommand );
172   }                                               102   }
173   G4Tokenizer savedToken(savedParameters);     << 103   G4Tokenizer savedToken( savedParameters );
174   G4String token;                                 104   G4String token;
175   for (G4int i_thParameter = 0; i_thParameter  << 105   for(int i_thParameter=0;i_thParameter<parameterNumber;i_thParameter++)
                                                   >> 106   {
176     token = savedToken();                         107     token = savedToken();
177     if (token.empty()) {                       << 108     if( token.isNull() ) return G4String();
178       return G4String();                       << 109     if( token[(size_t)0] == '"' )
179     }                                          << 110     {
180     if (token[(size_t)0] == '"') {             << 
181       token.append(" ");                          111       token.append(" ");
182       token.append(savedToken("\""));             112       token.append(savedToken("\""));
183     }                                             113     }
184   }                                               114   }
185   return token;                                   115   return token;
186 }                                                 116 }
187                                                   117 
188 // ------------------------------------------- << 118 G4String G4UImanager::GetCurrentStringValue(const char * aCommand, 
189 G4String G4UImanager::GetCurrentStringValue(co << 119 const char * aParameterName, G4bool reGet)
190                                             G4 << 120 {
191 {                                              << 121   if(reGet || savedCommand == NULL)
192   if (reGet || savedCommand == nullptr) {      << 122   {
193     G4String parameterValues = GetCurrentValue << 123     G4String parameterValues = GetCurrentValues( aCommand );
194   }                                            << 124   }
195   for (G4int i = 0; i < (G4int)savedCommand->G << 125   for(int i=0;i<savedCommand->GetParameterEntries();i++)
196     if (aParameterName == savedCommand->GetPar << 126   {
197       return GetCurrentStringValue(aCommand, i << 127     if( aParameterName ==
198     }                                          << 128       savedCommand->GetParameter(i)->GetParameterName() )
                                                   >> 129       return GetCurrentStringValue(aCommand,i+1,false);
199   }                                               130   }
200   return G4String();                              131   return G4String();
201 }                                                 132 }
202                                                   133 
203 // ------------------------------------------- << 134 G4int G4UImanager::GetCurrentIntValue(const char * aCommand,
204 G4int G4UImanager::GetCurrentIntValue(const ch << 135 const char * aParameterName, G4bool reGet)
205                                       G4bool r << 
206 {                                                 136 {
207   G4String targetParameter = GetCurrentStringV << 137   G4String targetParameter = 
                                                   >> 138      GetCurrentStringValue( aCommand, aParameterName, reGet );
208   G4int value;                                    139   G4int value;
209   const char* t = targetParameter;                140   const char* t = targetParameter;
210   std::istringstream is(t);                    << 141   istrstream is((char*)t);
211   is >> value;                                    142   is >> value;
212   return value;                                   143   return value;
213 }                                                 144 }
214                                                   145 
215 // ------------------------------------------- << 146 G4int G4UImanager::GetCurrentIntValue(const char * aCommand,
216 G4int G4UImanager::GetCurrentIntValue(const ch << 147 int parameterNumber, G4bool reGet)
217 {                                                 148 {
218   G4String targetParameter = GetCurrentStringV << 149   G4String targetParameter = 
                                                   >> 150      GetCurrentStringValue( aCommand, parameterNumber, reGet );
219   G4int value;                                    151   G4int value;
220   const char* t = targetParameter;                152   const char* t = targetParameter;
221   std::istringstream is(t);                    << 153   istrstream is((char*)t);
222   is >> value;                                    154   is >> value;
223   return value;                                   155   return value;
224 }                                                 156 }
225                                                   157 
226 // ------------------------------------------- << 158 G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand,
227 G4double G4UImanager::GetCurrentDoubleValue(co << 159 const char * aParameterName, G4bool reGet)
228                                             G4 << 
229 {                                                 160 {
230   G4String targetParameter = GetCurrentStringV << 161   G4String targetParameter = 
                                                   >> 162      GetCurrentStringValue( aCommand, aParameterName, reGet );
231   G4double value;                                 163   G4double value;
232   const char* t = targetParameter;                164   const char* t = targetParameter;
233   std::istringstream is(t);                    << 165   istrstream is((char*)t);
234   is >> value;                                    166   is >> value;
235   return value;                                   167   return value;
236 }                                                 168 }
237                                                   169 
238 // ------------------------------------------- << 170 G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand,
239 G4double G4UImanager::GetCurrentDoubleValue(co << 171 int parameterNumber, G4bool reGet)
240                                             G4 << 
241 {                                                 172 {
242   G4String targetParameter = GetCurrentStringV << 173   G4String targetParameter = 
                                                   >> 174      GetCurrentStringValue( aCommand, parameterNumber, reGet );
243   G4double value;                                 175   G4double value;
244   const char* t = targetParameter;                176   const char* t = targetParameter;
245   std::istringstream is(t);                    << 177   istrstream is((char*)t);
246   is >> value;                                    178   is >> value;
247   return value;                                   179   return value;
248 }                                                 180 }
249                                                   181 
250 // ------------------------------------------- << 182 void G4UImanager::AddNewCommand(G4UIcommand * newCommand)
251 void G4UImanager::AddNewCommand(G4UIcommand* n << 
252 {                                              << 
253   treeTop->AddNewCommand(newCommand);          << 
254   if (fMasterUImanager() != nullptr && G4Threa << 
255     fMasterUImanager()->AddWorkerCommand(newCo << 
256   }                                            << 
257 }                                              << 
258                                                << 
259 // ------------------------------------------- << 
260 void G4UImanager::AddWorkerCommand(G4UIcommand << 
261 {                                              << 
262   treeTop->AddNewCommand(newCommand, true);    << 
263 }                                              << 
264                                                << 
265 // ------------------------------------------- << 
266 void G4UImanager::RemoveCommand(G4UIcommand* a << 
267 {                                                 183 {
268   treeTop->RemoveCommand(aCommand);            << 184   treeTop->AddNewCommand( newCommand );
269   if (fMasterUImanager() != nullptr && G4Threa << 
270     fMasterUImanager()->RemoveWorkerCommand(aC << 
271   }                                            << 
272 }                                                 185 }
273                                                   186 
274 // ------------------------------------------- << 187 void G4UImanager::RemoveCommand(G4UIcommand * aCommand)
275 void G4UImanager::RemoveWorkerCommand(G4UIcomm << 
276 {                                                 188 {
277   treeTop->RemoveCommand(aCommand, true);      << 189   treeTop->RemoveCommand( aCommand );
278 }                                                 190 }
279                                                   191 
280 // ------------------------------------------- << 192 void G4UImanager::ExecuteMacroFile(G4String fileName)
281 void G4UImanager::ExecuteMacroFile(const char* << 
282 {                                                 193 {
283   G4UIsession* batchSession = new G4UIbatch(fi << 194   G4UIsession* batchSession = new G4UIbatch(fileName,session);
284   session = batchSession;                         195   session = batchSession;
285   lastRC = 0;                                  << 
286   G4UIsession* previousSession = session->Sess    196   G4UIsession* previousSession = session->SessionStart();
287   lastRC = session->GetLastReturnCode();       << 
288   delete session;                                 197   delete session;
289   session = previousSession;                      198   session = previousSession;
290 }                                                 199 }
291                                                   200 
292 // ------------------------------------------- << 201 int G4UImanager::ApplyCommand(char * aCommand)
293 void G4UImanager::LoopS(const char* valueList) << 
294 {                                              << 
295   G4String vl = valueList;                     << 
296   G4Tokenizer parameterToken(vl);              << 
297   G4String mf = parameterToken();              << 
298   G4String vn = parameterToken();              << 
299   G4String c1 = parameterToken();              << 
300   c1 += " ";                                   << 
301   c1 += parameterToken();                      << 
302   c1 += " ";                                   << 
303   c1 += parameterToken();                      << 
304   const char* t1 = c1;                         << 
305   std::istringstream is(t1);                   << 
306   G4double d1;                                 << 
307   G4double d2;                                 << 
308   G4double d3;                                 << 
309   is >> d1 >> d2 >> d3;                        << 
310   Loop(mf, vn, d1, d2, d3);                    << 
311 }                                              << 
312                                                << 
313 // ------------------------------------------- << 
314 void G4UImanager::Loop(const char* macroFile,  << 
315                        G4double finalValue, G4 << 
316 {                                              << 
317   G4String cd;                                 << 
318   if (stepSize > 0) {                          << 
319     for (G4double d = initialValue; d <= final << 
320       std::ostringstream os;                   << 
321       os << d;                                 << 
322       cd += os.str();                          << 
323       cd += " ";                               << 
324     }                                          << 
325   }                                            << 
326   else {                                       << 
327     for (G4double d = initialValue; d >= final << 
328       std::ostringstream os;                   << 
329       os << d;                                 << 
330       cd += os.str();                          << 
331       cd += " ";                               << 
332     }                                          << 
333   }                                            << 
334   Foreach(macroFile, variableName, cd);        << 
335 }                                              << 
336                                                << 
337 // ------------------------------------------- << 
338 void G4UImanager::ForeachS(const char* valueLi << 
339 {                                              << 
340   const G4String& vl = valueList;              << 
341   G4Tokenizer parameterToken(vl);              << 
342   const G4String& mf = parameterToken();       << 
343   const G4String& vn = parameterToken();       << 
344   G4String c1 = parameterToken();              << 
345   G4String ca;                                 << 
346   while (!((ca = parameterToken()).empty())) { << 
347     c1 += " ";                                 << 
348     c1 += ca;                                  << 
349   }                                            << 
350                                                << 
351   G4String aliasValue = std::move(c1);         << 
352   if (aliasValue[0] == '"') {                  << 
353     G4String strippedValue;                    << 
354     if (aliasValue.back() == '"') {            << 
355       strippedValue = aliasValue.substr(1, ali << 
356     }                                          << 
357     else {                                     << 
358       strippedValue = aliasValue.substr(1, ali << 
359     }                                          << 
360     aliasValue = std::move(strippedValue);     << 
361   }                                            << 
362                                                << 
363   //  Foreach(mf,vn,c1);                       << 
364   Foreach(mf, vn, aliasValue);                 << 
365 }                                              << 
366                                                << 
367 // ------------------------------------------- << 
368 void G4UImanager::Foreach(const char* macroFil << 
369 {                                              << 
370   G4String candidatesString = candidates;      << 
371   G4Tokenizer parameterToken(candidatesString) << 
372   G4String cd;                                 << 
373   while (!((cd = parameterToken()).empty())) { << 
374     G4String vl = variableName;                << 
375     vl += " ";                                 << 
376     vl += cd;                                  << 
377     SetAlias(vl);                              << 
378     ExecuteMacroFile(FindMacroPath(macroFile)) << 
379     if (lastRC != 0) {                         << 
380       G4ExceptionDescription ed;               << 
381       ed << "Loop aborted due to a command exe << 
382          << "error code " << lastRC;           << 
383       G4Exception("G4UImanager::Foreach", "UIM << 
384       break;                                   << 
385     }                                          << 
386   }                                            << 
387 }                                              << 
388                                                << 
389 // ------------------------------------------- << 
390 G4String G4UImanager::SolveAlias(const char* a << 
391 {                                                 202 {
392   G4String aCommand = aCmd;                    << 203   G4String theCommand = aCommand;
393   std::size_t ia = aCommand.find('{');         << 204   return ApplyCommand(theCommand);
394   std::size_t iz = aCommand.find('#');         << 
395   while ((ia != std::string::npos) && ((iz ==  << 
396     G4int ibx = -1;                            << 
397     while (ibx < 0) {                          << 
398       std::size_t ib = aCommand.find('}');     << 
399       if (ib == std::string::npos) {           << 
400         G4cerr << aCommand << G4endl;          << 
401         for (std::size_t i = 0; i < ia; ++i) { << 
402           G4cerr << " ";                       << 
403         }                                      << 
404         G4cerr << "^" << G4endl;               << 
405         G4cerr << "Unmatched alias parenthesis << 
406         G4String nullStr;                      << 
407         return nullStr;                        << 
408       }                                        << 
409       G4String ps = aCommand.substr(ia + 1, aC << 
410       std::size_t ic = ps.find('{');           << 
411       std::size_t id = ps.find('}');           << 
412       if (ic != std::string::npos && ic < id)  << 
413         ia += ic + 1;                          << 
414       }                                        << 
415       else {                                   << 
416         ibx = (G4int)ib;                       << 
417       }                                        << 
418     }                                          << 
419     //--- Here ia represents the position of i << 
420     //--- and ibx represents corresponding "}" << 
421     G4String subs;                             << 
422     if (ia > 0) {                              << 
423       subs = aCommand.substr(0, ia);           << 
424     }                                          << 
425     G4String alis = aCommand.substr(ia + 1, ib << 
426     G4String rems = aCommand.substr(ibx + 1, a << 
427     const G4String* alVal = aliasList->FindAli << 
428     if (alVal == nullptr) {                    << 
429       G4cerr << "Alias <" << alis << "> not fo << 
430       G4String nullStr;                        << 
431       return nullStr;                          << 
432     }                                          << 
433     aCommand = subs + (*alVal) + rems;         << 
434     ia = aCommand.find('{');                   << 
435   }                                            << 
436   return aCommand;                             << 
437 }                                                 205 }
438                                                   206 
439 // ------------------------------------------- << 207 int G4UImanager::ApplyCommand(G4String aCommand)
440 G4int G4UImanager::ApplyCommand(const G4String << 
441 {                                                 208 {
442   return ApplyCommand(aCmd.data());            << 209   if(verboseLevel) G4cout << aCommand << endl;
443 }                                              << 
444                                                   210 
445 // ------------------------------------------- << 
446 G4int G4UImanager::ApplyCommand(const char* aC << 
447 {                                              << 
448   const G4String& aCommand = SolveAlias(aCmd); << 
449   if (aCommand.empty()) {                      << 
450     return fAliasNotFound;                     << 
451   }                                            << 
452   if (verboseLevel != 0) {                     << 
453     if (G4Threading::IsMasterThread()) {       << 
454       fLastCommandOutputTreated = false;       << 
455     }                                          << 
456     G4cout << aCommand << G4endl;              << 
457   }                                            << 
458   G4String commandString;                         211   G4String commandString;
459   G4String commandParameter;                      212   G4String commandParameter;
460                                                << 213   int i = aCommand.index(" ");
461   std::size_t i = aCommand.find(' ');          << 214   if( i != G4std::string::npos )
462   if (i != std::string::npos) {                << 215   {
463     commandString = aCommand.substr(0, i);     << 216     commandString = aCommand(0,i);
464     commandParameter = aCommand.substr(i + 1,  << 217     commandParameter = aCommand(i+1,aCommand.length()-(i+1));
465   }                                               218   }
466   else {                                       << 219   else
                                                   >> 220   {
467     commandString = aCommand;                     221     commandString = aCommand;
468   }                                               222   }
469                                                   223 
470   // remove doubled slash                      << 224   G4UIcommand * targetCommand = treeTop->FindPath( commandString );
471   std::size_t len = commandString.length();    << 225   if( targetCommand == NULL )
472   std::size_t ll = 0;                          << 226   {
473   G4String a1;                                 << 227     // G4cout << commandString << " NOT FOUND." << endl;
474   G4String a2;                                 << 
475   while (ll < len - 1) {                       << 
476     if (commandString.substr(ll, 2) == "//") { << 
477       if (ll == 0) {                           << 
478         // Safe because index argument always  << 
479         commandString.erase(ll, 1);            << 
480       }                                        << 
481       else {                                   << 
482         a1 = commandString.substr(0, ll);      << 
483         a2 = commandString.substr(ll + 1, len  << 
484         commandString = a1 + a2;               << 
485       }                                        << 
486       --len;                                   << 
487     }                                          << 
488     else {                                     << 
489       ++ll;                                    << 
490     }                                          << 
491   }                                            << 
492                                                << 
493   if (isMaster && bridges != nullptr) {        << 
494     for (auto bridge : *bridges) {             << 
495       G4int leng = bridge->DirLength();        << 
496       if (commandString.substr(0, leng) == bri << 
497         return bridge->LocalUI()->ApplyCommand << 
498       }                                        << 
499     }                                          << 
500   }                                            << 
501                                                << 
502   G4UIcommand* targetCommand = treeTop->FindPa << 
503   if (targetCommand == nullptr) {              << 
504     if (ignoreCmdNotFound) {                   << 
505       if (stackCommandsForBroadcast) {         << 
506         commandStack->push_back(commandString  << 
507       }                                        << 
508       return fCommandSucceeded;                << 
509     }                                          << 
510                                                << 
511     return fCommandNotFound;                      228     return fCommandNotFound;
512   }                                               229   }
513                                                   230 
514   if (stackCommandsForBroadcast && targetComma << 231   if(!(targetCommand->IsAvailable())) 
515     commandStack->push_back(commandString + "  << 232   { return fIllegalApplicationState; }
516   }                                            << 233  
517                                                << 234   if(saveHistory) historyFile << aCommand << endl; 
518   if (!(targetCommand->IsAvailable())) {       << 235   histVec.insert(aCommand);
519     return fIllegalApplicationState;           << 236   return targetCommand->DoIt( commandParameter );
520   }                                            << 237 }
521                                                << 238 
522   if (saveHistory) {                           << 239 void G4UImanager::StoreHistory(G4String fileName)
523     historyFile << aCommand << G4endl;         << 240 { StoreHistory(true,fileName); }
524   }                                            << 241 
525   if (G4int(histVec.size()) >= maxHistSize) {  << 242 void G4UImanager::StoreHistory(G4bool historySwitch,G4String fileName)
526     histVec.erase(histVec.begin());            << 243 {
527   }                                            << 244   if(historySwitch)
528   histVec.push_back(aCommand);                 << 245   {
529                                                << 246     if(saveHistory)
530   targetCommand->ResetFailure();               << 247     { historyFile.close(); }
531   G4int commandFailureCode = targetCommand->Do << 248     const char* theFileName = fileName;
532   if (commandFailureCode == 0) {               << 249     historyFile.open((char*)theFileName);
533     G4int additionalFailureCode = targetComman << 
534     if (additionalFailureCode > 0) {           << 
535       G4ExceptionDescription msg;              << 
536       msg << targetCommand->GetFailureDescript << 
537           << "Error code : " << additionalFail << 
538       G4Exception("G4UImanager::ApplyCommand", << 
539       commandFailureCode += additionalFailureC << 
540     }                                          << 
541   }                                            << 
542   return commandFailureCode;                   << 
543 }                                              << 
544                                                << 
545 // ------------------------------------------- << 
546 G4UIcommand* G4UImanager::FindCommand(const G4 << 
547 {                                              << 
548   return FindCommand(aCmd.data());             << 
549 }                                              << 
550                                                << 
551 // ------------------------------------------- << 
552 G4UIcommand* G4UImanager::FindCommand(const ch << 
553 {                                              << 
554   const G4String& aCommand = SolveAlias(aCmd); << 
555   if (aCommand.empty()) {                      << 
556     return nullptr;                            << 
557   }                                            << 
558                                                << 
559   G4String commandString;                      << 
560                                                << 
561   std::size_t i = aCommand.find(' ');          << 
562   if (i != std::string::npos) {                << 
563     commandString = aCommand.substr(0, i);     << 
564   }                                            << 
565   else {                                       << 
566     commandString = aCommand;                  << 
567   }                                            << 
568                                                << 
569   return treeTop->FindPath(commandString);     << 
570 }                                              << 
571                                                << 
572 // ------------------------------------------- << 
573 void G4UImanager::StoreHistory(const char* fil << 
574 {                                              << 
575   StoreHistory(true, fileName);                << 
576 }                                              << 
577                                                << 
578 // ------------------------------------------- << 
579 void G4UImanager::StoreHistory(G4bool historyS << 
580 {                                              << 
581   if (historySwitch) {                         << 
582     if (saveHistory) {                         << 
583       historyFile.close();                     << 
584     }                                          << 
585     historyFile.open((char*)fileName);         << 
586     saveHistory = true;                           250     saveHistory = true;
587   }                                               251   }
588   else {                                       << 252   else
                                                   >> 253   {
589     historyFile.close();                          254     historyFile.close();
590     saveHistory = false;                          255     saveHistory = false;
591   }                                               256   }
592   saveHistory = historySwitch;                    257   saveHistory = historySwitch;
593 }                                                 258 }
594                                                   259 
595 // ------------------------------------------- << 260 void G4UImanager::PauseSession(G4String msg)
596 void G4UImanager::PauseSession(const char* msg << 
597 {                                                 261 {
598   if (session != nullptr) {                    << 262   if(session) session->PauseSessionStart(msg); 
599     session->PauseSessionStart(msg);           << 
600   }                                            << 
601 }                                                 263 }
602                                                   264 
603 // ------------------------------------------- << 265 void G4UImanager::ListCommands(G4String direct)
604 void G4UImanager::ListCommands(const char* dir << 
605 {                                                 266 {
606   G4UIcommandTree* comTree = FindDirectory(dir    267   G4UIcommandTree* comTree = FindDirectory(direct);
607   if (comTree != nullptr) {                    << 268   if(comTree)
608     comTree->List();                           << 269   { comTree->List(); }
609   }                                            << 270   else
610   else {                                       << 271   { G4cout << direct << " is not found." << endl; }
611     G4cout << direct << " is not found." << G4 << 
612   }                                            << 
613 }                                                 272 }
614                                                   273 
615 // ------------------------------------------- << 
616 G4UIcommandTree* G4UImanager::FindDirectory(co    274 G4UIcommandTree* G4UImanager::FindDirectory(const char* dirName)
617 {                                                 275 {
618   const G4String& aDirName = dirName;          << 276   G4String aDirName = dirName;
619   G4String targetDir = G4StrUtil::strip_copy(a << 277   G4String targetDir = aDirName.strip(G4String::both);
620   if (targetDir.back() != '/') {               << 278   if( targetDir( targetDir.length()-1 ) != '/' )
621     targetDir += "/";                          << 279   { targetDir += "/"; }
622   }                                            << 
623   G4UIcommandTree* comTree = treeTop;             280   G4UIcommandTree* comTree = treeTop;
624   if (targetDir == "/") {                      << 281   if( targetDir == "/" )
625     return comTree;                            << 282   { return comTree; }
626   }                                            << 283   int idx = 1;
627   std::size_t idx = 1;                         << 284   while( idx < targetDir.length()-1 )
628   while (idx < targetDir.length() - 1) {       << 285   {
629     std::size_t i = targetDir.find('/', idx);  << 286     int i = targetDir.index("/",idx);
630     G4String targetDirString = targetDir.subst << 287     comTree = comTree->GetTree(targetDir(0,i+1));
631     comTree = comTree->GetTree(targetDirString << 288     if( comTree == NULL )
632     if (comTree == nullptr) {                  << 289     { return NULL; }
633       return nullptr;                          << 290     idx = i+1;
634     }                                          << 
635     idx = i + 1;                               << 
636   }                                               291   }
637   return comTree;                                 292   return comTree;
638 }                                                 293 }
639                                                   294 
640 // ------------------------------------------- << 295 void G4UImanager::Interact()
641 G4bool G4UImanager::Notify(G4ApplicationState  << 
642 {                                                 296 {
643   if (pauseAtBeginOfEvent) {                   << 297   Interact(G4String("G4> "));
644     if (requestedState == G4State_EventProc    << 
645         && G4StateManager::GetStateManager()-> << 
646     {                                          << 
647       PauseSession("BeginOfEvent");            << 
648     }                                          << 
649   }                                            << 
650   if (pauseAtEndOfEvent) {                     << 
651     if (requestedState == G4State_GeomClosed   << 
652         && G4StateManager::GetStateManager()-> << 
653     {                                          << 
654       PauseSession("EndOfEvent");              << 
655     }                                          << 
656   }                                            << 
657   return true;                                 << 
658 }                                                 298 }
659                                                   299 
660 // ------------------------------------------- << 300 void G4UImanager::Interact(char * pC)
661 void G4UImanager::SetCoutDestination(G4UIsessi << 
662 {                                                 301 {
663   G4iosSetDestination(value);                  << 302   G4String cc = pC;
                                                   >> 303   Interact(cc);
664 }                                                 304 }
665                                                   305 
666 // ------------------------------------------- << 306 void G4UImanager::Interact(G4String pC)
667 void G4UImanager::SetAlias(const char* aliasLi << 
668 {                                                 307 {
669   const G4String& aLine = aliasLine;           << 308   G4cerr << "G4UImanager::Interact() is out of date and is not used anymore." << endl;
670   std::size_t i = aLine.find(' ');             << 309   G4cerr << "This method will be removed shortly!!!" << endl;
671   const G4String& aliasName = aLine.substr(0,  << 310   G4cerr << "In case of main() use" << endl;
672   G4String aliasValue = aLine.substr(i + 1, aL << 311   G4cerr << "    G4UIsession * session = new G4UIterminal;" << endl;
673   if (aliasValue[0] == '"') {                  << 312   G4cerr << "    session->SessionStart();" << endl;
674     G4String strippedValue;                    << 313   G4cerr << "In other cases use" << endl;
675     if (aliasValue.back() == '"') {            << 314   G4cerr << "    G4StateManager::GetStateManager()->Pause();" << endl;
676       strippedValue = aliasValue.substr(1, ali << 
677     }                                          << 
678     else {                                     << 
679       strippedValue = aliasValue.substr(1, ali << 
680     }                                          << 
681     aliasValue = std::move(strippedValue);     << 
682   }                                            << 
683                                                << 
684   aliasList->ChangeAlias(aliasName, aliasValue << 
685 }                                                 315 }
686                                                   316 
687 // ------------------------------------------- << 
688 void G4UImanager::RemoveAlias(const char* alia << 
689 {                                              << 
690   const G4String& aL = aliasName;              << 
691   G4String targetAlias = G4StrUtil::strip_copy << 
692   aliasList->RemoveAlias(targetAlias);         << 
693 }                                              << 
694                                                   317 
695 // ------------------------------------------- << 
696 void G4UImanager::ListAlias()                  << 
697 {                                              << 
698   aliasList->List();                           << 
699 }                                              << 
700                                                   318 
701 // ------------------------------------------- << 319  void G4UImanager::SetCoutDestination(G4UIsession *const value)
702 void G4UImanager::CreateHTML(const char* dir)  << 
703 {                                                 320 {
704   G4UIcommandTree* tr = FindDirectory(dir);    << 321     G4coutbuf.SetDestination(value);
705   if (tr != nullptr) {                         << 322     G4cerrbuf.SetDestination(value);
706     tr->CreateHTML();                          << 
707   }                                            << 
708   else {                                       << 
709     G4cerr << "Directory <" << dir << "> is no << 
710   }                                            << 
711 }                                                 323 }
712                                                   324 
713 // ------------------------------------------- << 
714 void G4UImanager::ParseMacroSearchPath()       << 
715 {                                              << 
716   searchDirs.clear();                          << 
717                                                   325 
718   std::size_t idxfirst = 0;                    << 
719   std::size_t idxend = 0;                      << 
720   G4String pathstring = "";                    << 
721   while ((idxend = searchPath.find(':', idxfir << 
722     pathstring = searchPath.substr(idxfirst, i << 
723     if (!pathstring.empty()) {                 << 
724       searchDirs.push_back(pathstring);        << 
725     }                                          << 
726     idxfirst = idxend + 1;                     << 
727   }                                            << 
728                                                << 
729   pathstring = searchPath.substr(idxfirst, sea << 
730   if (!pathstring.empty()) {                   << 
731     searchDirs.push_back(std::move(pathstring) << 
732   }                                            << 
733 }                                              << 
734                                                << 
735 // ------------------------------------------- << 
736 static G4bool FileFound(const G4String& fname) << 
737 {                                              << 
738   G4bool qopen = false;                        << 
739   std::ifstream fs;                            << 
740   fs.open(fname.c_str(), std::ios::in);        << 
741   if (fs.good()) {                             << 
742     fs.close();                                << 
743     qopen = true;                              << 
744   }                                            << 
745   return qopen;                                << 
746 }                                              << 
747                                                << 
748 // ------------------------------------------- << 
749 G4String G4UImanager::FindMacroPath(const G4St << 
750 {                                              << 
751   G4String macrofile = fname;                  << 
752                                                << 
753   for (const auto& searchDir : searchDirs) {   << 
754     const G4String& fullpath = searchDir + "/" << 
755     if (FileFound(fullpath)) {                 << 
756       macrofile = fullpath;                    << 
757       break;                                   << 
758     }                                          << 
759   }                                            << 
760   return macrofile;                            << 
761 }                                              << 
762                                                << 
763 // ------------------------------------------- << 
764 std::vector<G4String>* G4UImanager::GetCommand << 
765 {                                              << 
766   std::vector<G4String>* returnValue = command << 
767   commandStack = new std::vector<G4String>;    << 
768   return returnValue;                          << 
769 }                                              << 
770                                                << 
771 // ------------------------------------------- << 
772 void G4UImanager::RegisterBridge(G4UIbridge* b << 
773 {                                              << 
774   if (brg->LocalUI() == this) {                << 
775     G4Exception("G4UImanager::RegisterBridge() << 
776                 "G4UIBridge cannot bridge betw << 
777   }                                            << 
778   else {                                       << 
779     bridges->push_back(brg);                   << 
780   }                                            << 
781 }                                              << 
782                                                << 
783 // ------------------------------------------- << 
784 void G4UImanager::SetUpForAThread(G4int tId)   << 
785 {                                              << 
786   threadID = tId;                              << 
787   G4iosInitialization();                       << 
788   threadCout = new G4MTcoutDestination(threadI << 
789   threadCout->SetIgnoreCout(igThreadID);       << 
790 }                                              << 
791                                                << 
792 // ------------------------------------------- << 
793 void G4UImanager::SetUpForSpecialThread(const  << 
794 {                                              << 
795   threadID = G4Threading::GENERICTHREAD_ID;    << 
796   G4Threading::G4SetThreadId(threadID);        << 
797   G4iosInitialization();                       << 
798   threadCout = new G4MTcoutDestination(threadI << 
799   threadCout->SetPrefixString(pref);           << 
800   threadCout->SetIgnoreCout(igThreadID);       << 
801 }                                              << 
802                                                << 
803 // ------------------------------------------- << 
804 void G4UImanager::SetCoutFileName(const G4Stri << 
805 {                                              << 
806   // for sequential mode, ignore this method.  << 
807   if (threadID < 0) {                          << 
808     return;                                    << 
809   }                                            << 
810                                                << 
811   if (fileN == "**Screen**") {                 << 
812     threadCout->SetCoutFileName(fileN, ifAppen << 
813   }                                            << 
814   else {                                       << 
815     std::stringstream fn;                      << 
816     fn << "G4W_" << threadID << "_" << fileN;  << 
817     threadCout->SetCoutFileName(fn.str(), ifAp << 
818   }                                            << 
819 }                                              << 
820                                                << 
821 // ------------------------------------------- << 
822 void G4UImanager::SetCerrFileName(const G4Stri << 
823 {                                              << 
824   // for sequential mode, ignore this method.  << 
825   if (threadID < 0) {                          << 
826     return;                                    << 
827   }                                            << 
828                                                << 
829   if (fileN == "**Screen**") {                 << 
830     threadCout->SetCerrFileName(fileN, ifAppen << 
831   }                                            << 
832   else {                                       << 
833     std::stringstream fn;                      << 
834     fn << "G4W_" << threadID << "_" << fileN;  << 
835     threadCout->SetCerrFileName(fn.str(), ifAp << 
836   }                                            << 
837 }                                              << 
838                                                << 
839 // ------------------------------------------- << 
840 void G4UImanager::SetThreadPrefixString(const  << 
841 {                                              << 
842   // for sequential mode, ignore this method.  << 
843   if (threadID < 0) {                          << 
844     return;                                    << 
845   }                                            << 
846   threadCout->SetPrefixString(s);              << 
847 }                                              << 
848                                                << 
849 // ------------------------------------------- << 
850 void G4UImanager::SetThreadUseBuffer(G4bool fl << 
851 {                                              << 
852   // for sequential mode, ignore this method.  << 
853   if (threadID < 0) {                          << 
854     return;                                    << 
855   }                                            << 
856   threadCout->EnableBuffering(flg);            << 
857 }                                              << 
858                                                << 
859 // ------------------------------------------- << 
860 void G4UImanager::SetThreadIgnore(G4int tid)   << 
861 {                                              << 
862   // for sequential mode, ignore this method.  << 
863   if (threadID < 0) {                          << 
864     igThreadID = tid;                          << 
865     return;                                    << 
866   }                                            << 
867   threadCout->SetIgnoreCout(tid);              << 
868 }                                              << 
869                                                << 
870 // ------------------------------------------- << 
871 void G4UImanager::SetThreadIgnoreInit(G4bool f << 
872 {                                              << 
873   // for sequential mode, ignore this method.  << 
874   if (threadID < 0) {                          << 
875     return;                                    << 
876   }                                            << 
877   threadCout->SetIgnoreInit(flg);              << 
878 }                                              << 
879                                                << 
880 G4UIsession* G4UImanager::GetBaseSession() con << 
881 {                                              << 
882   // There may be no session - pure batch mode << 
883   // If there is a session, it may be a batch  << 
884   // Find base session of this hierarchy of ba << 
885   G4UIsession* baseSession = session;          << 
886   while (auto aBatchSession = dynamic_cast<G4U << 
887     auto previousSession = aBatchSession->GetP << 
888     if (previousSession == nullptr) {          << 
889       // No previouse session - aBatchSession  << 
890       baseSession = aBatchSession;             << 
891       break;                                   << 
892     }                                          << 
893     // There is a previous session, which may  << 
894     // If not, it will be our desired base - s << 
895     baseSession = previousSession;             << 
896   }                                            << 
897   return baseSession;                          << 
898 }                                              << 
899                                                   326