Geant4 Cross Reference |
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 // Class description: 29 // 30 // This is a singleton class which controls the command manipulation 31 // and the user interface(s) 32 33 // Author: Makoto Asai, 1997 34 // -------------------------------------------------------------------- 35 #ifndef G4UImanager_hh 36 #define G4UImanager_hh 1 37 38 #include "G4UIcommandStatus.hh" 39 #include "G4VStateDependent.hh" 40 #include "globals.hh" 41 42 #include "icomsdefs.hh" 43 44 #include <fstream> 45 #include <vector> 46 47 class G4UIcommandTree; 48 class G4UIcommand; 49 class G4UIsession; 50 class G4UIcontrolMessenger; 51 class G4UnitsMessenger; 52 class G4LocalThreadCoutMessenger; 53 class G4UIaliasList; 54 class G4MTcoutDestination; 55 class G4UIbridge; 56 57 class G4UImanager : public G4VStateDependent 58 { 59 public: 60 // A static method to get the pointer to the only existing object 61 // of this class 62 static G4UImanager* GetUIpointer(); 63 static G4UImanager* GetMasterUIpointer(); 64 65 ~G4UImanager() override; 66 67 G4UImanager(const G4UImanager&) = delete; 68 const G4UImanager& operator=(const G4UImanager&) = delete; 69 G4bool operator==(const G4UImanager&) const = delete; 70 G4bool operator!=(const G4UImanager&) const = delete; 71 72 // This method returns a string which represents the current value(s) 73 // of the parameter(s) of the specified command. Null string will be 74 // returned if the given command is not defined or the command does 75 // not support the GetCurrentValues() method 76 G4String GetCurrentValues(const char* aCommand); 77 78 // This method register a new command 79 void AddNewCommand(G4UIcommand* newCommand); 80 81 // This command removes the registered command. After invokation of this 82 // command, that particular command cannot be applied 83 void RemoveCommand(G4UIcommand* aCommand); 84 85 // A macro file defined by the argument will be read by G4UIbatch object 86 void ExecuteMacroFile(const char* fileName); 87 88 // Execute a macro file more than once with a loop counter 89 void Loop(const char* macroFile, const char* variableName, G4double initialValue, 90 G4double finalValue, G4double stepSize = 1.0); 91 92 // Execute a macro file more than once with an aliased variable which 93 // takes a value in the candidate list 94 void Foreach(const char* macroFile, const char* variableName, const char* candidates); 95 96 // A command (and parameter(s)) given 97 // by the method's argument will be applied. Zero will be returned in 98 // case the command is successfully executed. Positive non-zero value 99 // will be returned if the command cannot be executed. The meaning of 100 // this non-zero value is the following: 101 // The returned number : xyy 102 // x00 : G4CommandStatus.hh enumeration 103 // yy : the problematic parameter (first found) 104 G4int ApplyCommand(const char* aCommand); 105 G4int ApplyCommand(const G4String& aCommand); 106 107 // Find the G4UIcommand object. Null pointer is returned if command 108 // is not found. 109 // Please note that each thread returns different objects if this 110 // method is used in multi-threaded mode. 111 G4UIcommand* FindCommand(const char* aCommand); 112 G4UIcommand* FindCommand(const G4String& aCommand); 113 114 // The executed commands will be stored in the defined file. If 115 // "historySwitch" is false, saving will be suspended 116 void StoreHistory(const char* fileName = "G4history.macro"); 117 void StoreHistory(G4bool historySwitch, const char* fileName = "G4history.macro"); 118 119 // All commands registered under the given directory will be listed to 120 // standard output 121 void ListCommands(const char* direc); 122 123 // Define an alias. The first word of "aliasLine" string is the 124 // alias name and the remaining word(s) is(are) string value 125 // to be aliased 126 void SetAlias(const char* aliasLine); 127 128 // Remove the defined alias 129 void RemoveAlias(const char* aliasName); 130 131 // Print all aliases 132 void ListAlias(); 133 134 // Convert a command string which contains alias(es) 135 G4String SolveAlias(const char* aCmd); 136 137 // Generate HTML files for defined UI commands 138 void CreateHTML(const char* dir = "/"); 139 140 // These methods are used by G4UIcontrolMessenger to use Loop() 141 // and Foreach() methods 142 void LoopS(const char* valueList); 143 void ForeachS(const char* valueList); 144 145 // This method is exclusively invoked by G4StateManager 146 G4bool Notify(G4ApplicationState requestedState) override; 147 148 // These six methods return the current value of a parameter of the 149 // given command. For the first three methods, the ordering number of 150 // the parameter (1 is the first parameter) can be given, whereas, 151 // other three methods can give the parameter name. 152 // If "reGet" is true, actual request of returning the current value 153 // will be sent to the corresponding messenger, while, if it is false, 154 // the value stored in G4Umanager will be used. The later case is valid 155 // for the sequential invokation for the same command 156 G4String GetCurrentStringValue(const char* aCommand, G4int parameterNumber = 1, 157 G4bool reGet = true); 158 G4int GetCurrentIntValue(const char* aCommand, G4int parameterNumber = 1, G4bool reGet = true); 159 G4double GetCurrentDoubleValue(const char* aCommand, G4int parameterNumber = 1, 160 G4bool reGet = true); 161 G4String GetCurrentStringValue(const char* aCommand, const char* aParameterName, 162 G4bool reGet = true); 163 G4int GetCurrentIntValue(const char* aCommand, const char* aParameterName, G4bool reGet = true); 164 G4double GetCurrentDoubleValue(const char* aCommand, const char* aParameterName, 165 G4bool reGet = true); 166 167 // If the Boolean flags are true, Pause() method of G4StateManager is 168 // invoked at the very beginning (before generating a G4Event object) 169 // or at the end of each event. So that, in case a (G)UI session is 170 // defined, the user can interact 171 inline void SetPauseAtBeginOfEvent(G4bool vl) { pauseAtBeginOfEvent = vl; } 172 inline G4bool GetPauseAtBeginOfEvent() const { return pauseAtBeginOfEvent; } 173 inline void SetPauseAtEndOfEvent(G4bool vl) { pauseAtEndOfEvent = vl; } 174 inline G4bool GetPauseAtEndOfEvent() const { return pauseAtEndOfEvent; } 175 176 inline G4UIcommandTree* GetTree() const { return treeTop; } 177 inline G4UIsession* GetSession() const { return session; } 178 inline G4UIsession* GetG4UIWindow() const { return g4UIWindow; } 179 // Find base session in a hierarchy of sessions 180 G4UIsession* GetBaseSession() const; 181 182 // These methods define the active (G)UI session 183 inline void SetSession(G4UIsession* const value) { session = value; } 184 inline void SetG4UIWindow(G4UIsession* const value) { g4UIWindow = value; } 185 186 // This method defines the destination of G4cout/G4cerr stream. 187 // For usual cases, this method will be invoked by a concrete 188 // (G)UI session class object and thus the user needs not to invoke this 189 void SetCoutDestination(G4UIsession* const value); 190 191 inline void SetVerboseLevel(G4int val) { verboseLevel = val; } 192 inline G4int GetVerboseLevel() const { return verboseLevel; } 193 inline G4int GetNumberOfHistory() const { return G4int(histVec.size()); } 194 inline G4String GetPreviousCommand(G4int i) const 195 { 196 G4String st; 197 if (i >= 0 && i < G4int(histVec.size())) { 198 st = histVec[i]; 199 } 200 return st; 201 } 202 inline void SetMaxHistSize(G4int mx) { maxHistSize = mx; } 203 inline G4int GetMaxHistSize() const { return maxHistSize; } 204 205 inline void SetMacroSearchPath(const G4String& path) { searchPath = path; } 206 inline const G4String& GetMacroSearchPath() const { return searchPath; } 207 void ParseMacroSearchPath(); 208 G4String FindMacroPath(const G4String& fname) const; 209 210 inline void SetMasterUIManager(G4bool val) 211 { 212 isMaster = val; 213 stackCommandsForBroadcast = val; 214 if (val && (bridges == nullptr)) { 215 bridges = new std::vector<G4UIbridge*>; 216 fMasterUImanager() = this; 217 } 218 } 219 inline void SetIgnoreCmdNotFound(G4bool val) { ignoreCmdNotFound = val; } 220 221 std::vector<G4String>* GetCommandStack(); 222 void RegisterBridge(G4UIbridge* brg); 223 224 // Setups as above but for a non-worker thread (e.g. vis) 225 void SetUpForAThread(G4int tId); 226 227 void SetUpForSpecialThread(const G4String& aPrefix); 228 229 inline G4int GetThreadID() const { return threadID; } 230 231 void SetCoutFileName(const G4String& fileN = "G4cout.txt", G4bool ifAppend = true); 232 void SetCerrFileName(const G4String& fileN = "G4cerr.txt", G4bool ifAppend = true); 233 void SetThreadPrefixString(const G4String& prefix = "W"); 234 void SetThreadUseBuffer(G4bool flg = true); 235 void SetThreadIgnore(G4int tid = 0); 236 void SetThreadIgnoreInit(G4bool flg = true); 237 inline G4MTcoutDestination* GetThreadCout() { return threadCout; } 238 239 static void UseDoublePrecisionStr(G4bool val); 240 static G4bool DoublePrecisionStr(); 241 242 inline G4int GetLastReturnCode() const { return lastRC; } 243 244 inline bool IsLastCommandOutputTreated() { return fLastCommandOutputTreated; } 245 inline void SetLastCommandOutputTreated() { fLastCommandOutputTreated = true; } 246 247 protected: 248 G4UImanager(); 249 250 private: 251 void AddWorkerCommand(G4UIcommand* newCommand); 252 void RemoveWorkerCommand(G4UIcommand* aCommand); 253 254 void PauseSession(const char* msg); 255 void CreateMessenger(); 256 G4UIcommandTree* FindDirectory(const char* dirName); 257 258 private: 259 G4ICOMS_DLL static G4UImanager*& fUImanager(); // thread-local 260 G4ICOMS_DLL static G4bool& fUImanagerHasBeenKilled(); // thread-local 261 G4ICOMS_DLL static G4UImanager*& fMasterUImanager(); 262 G4UIcommandTree* treeTop = nullptr; 263 G4UIsession* session = nullptr; 264 G4UIsession* g4UIWindow = nullptr; 265 G4UIcontrolMessenger* UImessenger = nullptr; 266 G4UnitsMessenger* UnitsMessenger = nullptr; 267 G4LocalThreadCoutMessenger* CoutMessenger = nullptr; 268 G4String savedParameters; 269 G4UIcommand* savedCommand = nullptr; 270 G4int verboseLevel = 0; 271 std::ofstream historyFile; 272 G4bool saveHistory = false; 273 std::vector<G4String> histVec; 274 G4UIaliasList* aliasList = nullptr; 275 G4int maxHistSize = 20; 276 G4bool pauseAtBeginOfEvent = false; 277 G4bool pauseAtEndOfEvent = false; 278 G4String searchPath = ""; 279 std::vector<G4String> searchDirs; 280 281 G4bool isMaster = false; 282 std::vector<G4UIbridge*>* bridges = nullptr; 283 G4bool ignoreCmdNotFound = false; 284 G4bool stackCommandsForBroadcast = false; 285 std::vector<G4String>* commandStack = nullptr; 286 287 G4int threadID = -1; 288 G4MTcoutDestination* threadCout = nullptr; 289 G4ICOMS_DLL static G4int igThreadID; 290 291 G4ICOMS_DLL static G4bool doublePrecisionStr; 292 293 G4int lastRC = 0; 294 295 G4bool fLastCommandOutputTreated = true; 296 }; 297 298 #endif 299