SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
SlicApplication.cc
Go to the documentation of this file.
1 // SLIC
2 #include "SlicApplication.hh"
4 #include "CommandQueue.hh"
5 #include "FieldManager.hh"
7 #include "LcioManager.hh"
8 #include "MCParticleManager.hh"
9 #include "ModuleRegistry.hh"
10 #include "PackageInfo.hh"
11 #include "RunManager.hh"
12 #include "TrackManager.hh"
13 #include "TimeUtil.hh"
14 #include "HepPDTManager.hh"
15 #include "VRML2WriterMessenger.hh"
16 #include "PhysicsListManager.hh"
17 
18 // LCDD
19 #include "lcdd/util/StringUtil.hh"
20 #include "lcdd/core/LCDDDetectorConstruction.hh"
21 
22 // Geant4
23 #include "G4ApplicationState.hh"
24 #include "G4StateManager.hh"
25 #include "G4VisExecutive.hh"
26 #include "G4UIExecutive.hh"
27 
28 namespace slic {
29 
31  Module("SlicApplication", false), m_appMessenger(0), m_runManager(0), m_mode(SlicApplication::eBatch), m_returnCode(0), m_isInitialized(false), m_geant4VersionString(0) {
32 #ifdef SLIC_LOG
33  log() << LOG::okay << LOG::head << "SLIC is starting." << LOG::done;
34 #endif
35 }
36 
38  if (m_appMessenger != 0) {
39  delete m_appMessenger;
40  }
41 
42  // Deletion of all modules is commented out as iterating the
43  // map causes a seg fault in more recent gcc versions like 9,
44  // for unknown reasons. Should not have any effect since the
45  // application is exiting anyways and there are no modules
46  // which perform any meaningful cleanup in their destructors. --JM
47 /*
48 #ifdef linux
49 #ifdef SLIC_LOG
50  log().debug("Deleting registered modules ...");
51 #endif
52  ModuleRegistry::instance()->deleteModules();
53 #endif
54 */
55 
56  // Delete the G4RunManager, which also deletes all the Geant4-related modules.
57 //#ifdef SLIC_LOG
58 // log().debug("Deleting G4RunManager ...");
59 //#endif
60 // delete G4RunManager::GetRunManager();
61 
62 #ifdef SLIC_LOG
63  log() << LOG::okay << LOG::head << "SLIC is exiting." << LOG::done;
64  log() << "return code: " << getReturnCode() << " => " << getErrorCodeString(getReturnCode()) << LOG::done;
65 #endif
66 
67 }
68 
69 void SlicApplication::initialize(int argc, char** argv) {
70  // Set the name and basename of slic's binary from the command line arguments.
71  m_binaryname = std::string(argv[0]);
73 
74  // Process arguments using the CommandLineProcessor.
76  cmd->process(argc, argv);
77 
78  m_ui = new G4UIExecutive(argc, argv);
79 }
80 
82  if (!m_isInitialized) {
83  // Print the SLIC splash screen.
85 
86  // Initialize the application messenger.
88 
89  // Create a new RunManager.
90  m_runManager = new RunManager();
91 
93 
94  // Initialize the LcioManager.
96 
97  // Initialize the magnetic field messenger.
99 
100  // Setup the LCDD subsystem.
101  initializeLCDD();
102 
103  // Initialize the tracker manager.
105 
106  // Initialize the MCParticle manager.
108 
109  // Initialize visualization.
110  initializeVis();
111 
112  // Set state variable.
113  m_isInitialized = true;
114  } else {
115  log().warning("Call to SlicApplication::initialize() was ignored. SLIC is already initialized!");
116  }
117 }
118 
120 {
121  G4VisExecutive* vis = new G4VisExecutive();
122  vis->Initialize();
123 
124  // VRML writer within SLIC.
125  new VRML2WriterMessenger();
126 }
127 
129  // LCDD geometry subsystem.
130  m_runManager->SetUserInitialization(new LCDDDetectorConstruction());
131 }
132 
134  m_mode = rmode;
135 }
136 
137 const std::string& SlicApplication::getBinaryName() const {
138  return m_binaryname;
139 }
140 
141 const std::string& SlicApplication::getBinaryBasename() const {
142  return m_binarybasename;
143 }
144 
146  return m_returnCode;
147 }
148 
150  m_returnCode = rc;
151 }
152 
154  std::string::size_type sidx = m_binaryname.find_last_of('/');
155  if (sidx != std::string::npos) {
156  m_binarybasename = std::string(m_binaryname, sidx + 1);
157  }
158 }
159 
161  return m_mode;
162 }
163 
165  if (abort) {
166  G4RunManager::GetRunManager()->AbortRun(abort);
167  }
168 }
169 
171  return m_runManager;
172 }
173 
175  // Initialize if not already.
176  if (!m_isInitialized) {
177  initialize();
178  }
179 
180  // Retrieve the list of Geant4 commands created by CommandLineProcessor.
181  CommandQueue* q = CommandLineProcessor::instance()->getCommandQueue();
182 
183  // Print, execute, and clear the CommandQueue.
184 #ifdef SLIC_LOG
185  log().verbose("Printing the Geant4 command queue ...");
186  q->printOut();
187 #endif
188  q->execute();
189  q->clear();
190 
191  // Start the UI session if in interactive mode.
192  if (getMode() == eInteractive) {
193  log().verbose("Starting interactive session ...");
194  m_ui->SessionStart();
195  delete m_ui;
196  }
197 }
198 
200  CommandLineProcessor::instance()->printVersion();
201 }
202 
204  log() << LOG::done;
205  log() << LOG::always << "*************************************************************" << LOG::done;
206  log() << LOG::always << " Application : " << PackageInfo::getFullName() << " (" << PackageInfo::getShortName() << ")" << LOG::done;
207  log() << LOG::always << " Version : " << PackageInfo::getVersion() << LOG::done;
208  log() << LOG::always << "*************************************************************" << LOG::done;
209 }
210 
212  CommandLineProcessor::instance()->printUsage();
213 }
214 
215 } // namespace slic
static CommandLineProcessor * instance()
Command line processor for SLIC that uses getopt.
void process(int argc, char **argv)
A queue of G4 commands to be executed in order.
Definition: CommandQueue.hh:18
LogStream & verbose()
Definition: LogStream.hh:238
LogStream & warning()
Definition: LogStream.hh:254
Base class for slic code modules that provides common functionality, such as logging to an output str...
Definition: Module.hh:25
LogStream & log()
Definition: Module.hh:99
Extension of G4RunManager.
Definition: RunManager.hh:18
G4UImessenger for the SLIC application.
Singleton class for a Geant4 application.
void setMode(ERunMode rmode)
RunManager * getRunManager()
SlicApplicationMessenger * m_appMessenger
ERunMode getMode() const
const std::string & getBinaryName() const
const char * getErrorCodeString(unsigned int errorCode)
void setAborting(bool a=true)
const std::string & getBinaryBasename() const
This is the G4UImessenger for the VRML2Writer class.
@ okay
Definition: LogStream.hh:33
@ always
Definition: LogStream.hh:33
@ head
Definition: LogStream.hh:26
@ done
Definition: LogStream.hh:26