SlicApplication.cc

Go to the documentation of this file.
00001 // $Header: /cvs/lcd/slic/src/SlicApplication.cc,v 1.49 2010/01/08 23:18:56 jeremy Exp $
00002 
00003 #include "SlicApplication.hh"
00004 
00005 // slic
00006 #include "CommandLineProcessor.hh"
00007 #include "CommandQueue.hh"
00008 #include "EventDebugger.hh"
00009 #include "FieldManager.hh"
00010 #include "SlicApplicationMessenger.hh"
00011 #include "LCDD.hh"
00012 #include "LcioManager.hh"
00013 #include "ModuleRegistry.hh"
00014 #include "PackageInfo.hh"
00015 #include "RunManager.hh"
00016 #include "StringUtil.hh"
00017 #include "TimeUtil.hh"
00018 #include "TrajectoryManager.hh"
00019 #include "HepPDTManager.hh"
00020 
00021 // test VRML writer
00022 #include "VRML2WriterMessenger.hh"
00023 
00024 // lcdd
00025 #include "LCDDDetectorConstruction.hh"
00026 
00027 #ifdef USE_G4NIST
00028 #include "NistWriterMessenger.hh"
00029 #endif
00030 
00031 // geant4
00032 #include "G4ApplicationState.hh"
00033 #include "G4StateManager.hh"
00034 
00035 #ifndef G4UI_NONE
00036 #include "G4UImanager.hh"
00037 #include "G4UIterminal.hh"
00038 #include "G4UItcsh.hh"
00039 #include "G4UIcsh.hh"
00040 
00041 #ifdef G4UI_USE_XM
00042 #include "G4UIXm.hh"
00043 #endif
00044 
00045 #ifdef G4UI_USE_GAG
00046 #include "G4UIGAG.hh"
00047 #endif
00048 #endif // G4UI_NONE
00049 
00050 #ifdef HAVE_G4VERSION
00051 #include "G4Version.hh"
00052 #endif
00053 
00054 #include "PhysicsListManager.hh"
00055 
00056 namespace slic
00057 {
00058 
00059     SlicApplication::SlicApplication()
00060         : Module("SlicApplication", false),
00061           m_session(0),
00062 #ifdef G4VIS_USE
00063           m_visManager(0),
00064 #endif
00065           m_appMessenger(0),
00066           m_runManager(0),
00067           m_mode(SlicApplication::eBatch),
00068           m_returnCode(0),
00069           m_setRunAbort(false),
00070           m_isInitialized(false),
00071           m_geant4VersionString(0)
00072     {
00073 #ifdef SLIC_LOG
00074         log() << LOG::okay << LOG::head << "SLIC is starting." << LOG::done;
00075 #endif
00076     }
00077 
00078     SlicApplication::~SlicApplication()
00079     {
00080         if (m_session != 0) 
00081         {
00082             delete m_session;
00083         }
00084 
00085         if (m_appMessenger != 0) 
00086         {
00087             delete m_appMessenger;
00088         }
00089 
00090         // Delete all the modules.  Only seems to work without seg fault on Linux.
00091         // https://jira.slac.stanford.edu/browse/SLIC-185
00092 #ifdef linux 
00093 #ifdef SLIC_LOG
00094         log().debug("Deleting registered modules ...");
00095 #endif
00096         ModuleRegistry::instance()->deleteModules();
00097 #endif
00098 
00099         // Delete the G4RunManager, which also deletes all the Geant4-related modules.
00100 #ifdef SLIC_LOG
00101         log().debug("Deleting G4RunManager ...");
00102 #endif
00103         delete G4RunManager::GetRunManager();
00104 
00105 #ifdef SLIC_LOG
00106         log() << LOG::okay << LOG::head << "SLIC is exiting." << LOG::done;
00107 #endif
00108     }
00109 
00110     void SlicApplication::initialize(int argc, char** argv)
00111     {
00112         // Set the name and basename of slic's binary from the command line arguments.
00113         m_binaryname = std::string(argv[0]);
00114         setBinaryBasename();
00115 
00116         // Process arguments using the CommandLineProcessor.
00117         CommandLineProcessor* cmd = CommandLineProcessor::instance();
00118         cmd->process(argc, argv);
00119     }
00120 
00121     void SlicApplication::initialize()
00122     {
00123         if (!m_isInitialized) 
00124         {
00125             // Print the SLIC splash screen.
00126             printSplashScreen();
00127 
00128             // Initialize the application messenger.
00129             m_appMessenger = new SlicApplicationMessenger(this);
00130 
00131             // Create a new RunManager.
00132             m_runManager = new RunManager();         
00133 
00134             // Initialize the physics list manager.
00135             PhysicsListManager::instance();
00136 
00137             // Initialize the LcioManager.
00138             LcioManager::instance();
00139 
00140             // Initialize the magnetic field messenger.
00141             FieldManager::instance();
00142       
00143             // Setup the LCDD subsystem.
00144             initializeLCDD();
00145 
00146 #ifdef USE_G4NIST
00147             // Setup the NIST Writer.
00148             initializeNistWriter();
00149 #endif
00150 
00151             // Initialize visualization.
00152 #ifdef G4VIS_USE
00153             initializeVis();
00154 #endif
00155 
00156             // Initialize the UI.
00157 #ifndef G4UI_NONE
00158             initializeUI();
00159 #endif
00160 
00161             HepPDTManager::instance();
00162 
00163             // Set state variable.
00164             m_isInitialized = true;
00165         }
00166         else 
00167         {
00168             log().warning("Call to SlicApplication::initialize() was ignored.  SLIC is already initialized!");
00169         }
00170     }
00171 
00172 #ifndef G4UI_NONE
00173     void SlicApplication::initializeUI()
00174     {
00175         // A tcsh terminal.
00176 #ifdef G4UI_USE_TCSH
00177         m_session = new G4UIterminal(new G4UItcsh);
00178 
00179         // A csh terminal.
00180 #elif G4UI_USE_CSH
00181         m_session = new G4UIterminal(new G4UIcsh);
00182 
00183         // GAG.
00184 #elif G4UI_USE_GAG
00185         session = new G4UIGAG;
00186 
00187         // Motif GUI.
00188 #elif G4UI_USE_XM
00189         m_session = new G4UIXm( 0, NULL );
00190 
00191         // Add file and exit buttons to Motif UI
00192         if ( m_session ) 
00193         {
00194             G4UImanager* ui = G4UImanager::GetUIpointer();
00195             ui->ApplyCommand("/gui/addMenu file File");
00196             ui->ApplyCommand("/gui/addButton file Exit \"exit\"");
00197         }
00198         // Plain terminal is the fallback.
00199 #else
00200         m_session = new G4UIterminal();
00201 #endif
00202 
00203         if ( !m_session ) 
00204         {
00205             log().fatal( "Failed to initialize UI session." );
00206             G4Exception( "Failed to initialize UI session." );
00207         }
00208     }
00209 #endif
00210 
00211 #ifdef G4VIS_USE
00212     void SlicApplication::initializeVis()
00213     {
00214         m_visManager = new VisManager();
00215         m_visManager->Initialize();
00216     
00217         // VRML test
00218         new VRML2WriterMessenger();    
00219     }
00220 #endif
00221 
00222     void SlicApplication::initializeLCDD()
00223     {
00224         // LCDD geometry subsystem.
00225         m_runManager->SetUserInitialization((new LCDD())->getDetectorConstruction());
00226     }   
00227 
00228     void SlicApplication::setMode(SlicApplication::ERunMode rmode)
00229     {
00230         m_mode = rmode;
00231     }
00232 
00233     const std::string& SlicApplication::getBinaryName() const
00234     {
00235         return m_binaryname;
00236     }
00237 
00238     const std::string& SlicApplication::getBinaryBasename() const
00239     {
00240         return m_binarybasename;
00241     }
00242 
00243     int SlicApplication::getReturnCode() const
00244     {
00245         return m_returnCode;
00246     }
00247 
00248     void SlicApplication::setReturnCode(int rc)
00249     {
00250         m_returnCode = rc;
00251     }
00252 
00253     void SlicApplication::setBinaryBasename()
00254     {
00255         std::string::size_type sidx = m_binaryname.find_last_of('/');
00256         if (sidx != std::string::npos) 
00257         {
00258             m_binarybasename = std::string(m_binaryname, sidx + 1);
00259         }
00260     }
00261 
00262     SlicApplication::ERunMode SlicApplication::getMode() const
00263     {
00264         return m_mode;
00265     }
00266 
00267     void SlicApplication::setAborting(bool a)
00268     {
00269         if (a) {
00270             // FIXME: Does this actually do anything to current G4Event?
00271             G4RunManager::GetRunManager()->AbortRun(a);
00272         }
00273 
00274         m_setRunAbort = a;
00275     }
00276 
00277     bool SlicApplication::isAborting() const
00278     {
00279         return m_setRunAbort;
00280     }
00281 
00282     RunManager* SlicApplication::getRunManager()
00283     {
00284         return m_runManager;
00285     }
00286 
00287     void SlicApplication::run()
00288     {
00289         // Initialize if not already.
00290         if (!m_isInitialized) 
00291         {
00292             initialize();
00293         }
00294 
00295         // Retrieve the list of Geant4 commands created by CommandLineProcessor.
00296         CommandQueue* q = CommandLineProcessor::instance()->getCommandQueue();
00297 
00298         // Print, execute, and clear the CommandQueue.
00299 #ifdef SLIC_LOG
00300         log().verbose("Printing the Geant4 command queue ...");
00301         q->printOut();
00302 #endif
00303         q->execute();
00304         q->clear();
00305     
00306         // Start the UI session if in interactive mode.
00307 #ifndef G4UI_NONE
00308         if ( getMode() == eInteractive ) 
00309         {
00310             log().verbose("Starting interactive session ...");
00311             m_session->SessionStart();
00312         }
00313 #endif    
00314     }
00315 
00316     void SlicApplication::printVersion()
00317     {
00318         CommandLineProcessor::instance()->printVersion();
00319     }
00320 
00321     void SlicApplication::printSplashScreen()
00322     {
00323         log() << LOG::done;
00324         log() << LOG::always << "*************************************************************" << LOG::done;
00325         log() << LOG::always << " App     : " << PackageInfo::getNameString() << " (" << PackageInfo::getAbbrevString() << ")" << LOG::done;
00326         log() << LOG::always << " Version : " << PackageInfo::getVersionString() << LOG::done;
00327         log() << LOG::always << " Date    : " << PackageInfo::getChangeDateString() << LOG::done;
00328         log() << LOG::always << " Authors : " << PackageInfo::getAuthorString() << LOG::done;
00329         log() << LOG::always << " Inst    : " << PackageInfo::getInstitutionString() << LOG::done;
00330         log() << LOG::always << " WWW     : " << PackageInfo::getWWW() << LOG::done;
00331         log() << LOG::always << " Contact : " << PackageInfo::getEmail() << LOG::done;
00332         log() << LOG::always << "*************************************************************" << LOG::done;
00333     }
00334 
00335     void SlicApplication::printUsage()
00336     {
00337         CommandLineProcessor::instance()->printUsage();
00338     }
00339 
00340 #ifdef USE_G4NIST
00341     void SlicApplication::initializeNistWriter()
00342     {
00343         new NistWriterMessenger();
00344     }
00345 #endif
00346 
00347     std::string SlicApplication::getGeant4VersionString()
00348     {
00349         if (m_geant4VersionString == 0) 
00350         {
00351             std::string g4ver;
00352 #ifdef HAVE_G4VERSION
00353             g4ver = StringUtil::toString(G4VERSION_NUMBER);
00354 #else
00355             // Massage the string returned by G4RunManager into format
00356             // returned by G4Version in recent Geant4 versions.
00357             g4ver = G4RunManager::GetRunManager()->GetVersionString();
00358             std::string::size_type start = g4ver.find("geant4-",0) + 7;
00359             std::string::size_type end = g4ver.find(" ",start);
00360             g4ver = g4ver.substr(start, end - start);
00361             std::string g4major = g4ver.substr(1,1);
00362             std::string g4minor = g4ver.substr(4,1);
00363             std::string g4patch = "0";
00364             if (g4ver.find("patch") != string::npos) {
00365                 g4patch = g4ver.substr(13,1);
00366             }      
00367             g4ver = g4major + g4minor + g4patch;
00368 #endif
00369             std::stringstream s;
00370             s << "geant4-" << "v" << g4ver[0] << "r" << g4ver[1] << "p" << g4ver[2];
00371             m_geant4VersionString = new std::string(s.str());
00372         }
00373         return *m_geant4VersionString;
00374     }
00375     
00376 } // namespace slic

Generated on Mon Jun 7 17:45:21 2010 for Simulator for the Linear Collider by  doxygen 1.5.4