Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:19:39

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /*
0027  * =============================================================================
0028  *
0029  *       Filename:  cexmc.cc
0030  *
0031  *    Description:  main
0032  *
0033  *        Version:  1.0
0034  *        Created:  10.10.2009 23:24:39
0035  *       Revision:  none
0036  *       Compiler:  gcc
0037  *
0038  *         Author:  Alexey Radkov (), 
0039  *        Company:  PNPI
0040  *
0041  * =============================================================================
0042  */
0043 
0044 #include <set>
0045 #ifdef CEXMC_USE_PERSISTENCY
0046 #include <boost/algorithm/string.hpp>
0047 #include <boost/archive/archive_exception.hpp>
0048 #ifdef CEXMC_USE_CUSTOM_FILTER
0049 #include <boost/variant/get.hpp>
0050 #endif
0051 #endif
0052 #include <G4UImanager.hh>
0053 #include <G4String.hh>
0054 #include <G4UIsession.hh>
0055 #include <G4UIterminal.hh>
0056 #ifdef G4UI_USE_TCSH
0057 #include <G4UItcsh.hh>
0058 #endif
0059 #ifdef G4UI_USE_QT
0060 #include <G4UIQt.hh>
0061 #endif
0062 #include <G4VisExecutive.hh>
0063 #include "CexmcRunManager.hh"
0064 #include "CexmcHistoManager.hh"
0065 #include "CexmcSetup.hh"
0066 #include "CexmcPhysicsList.hh"
0067 #include "CexmcPhysicsManager.hh"
0068 #include "CexmcPrimaryGeneratorAction.hh"
0069 #include "CexmcTrackingAction.hh"
0070 #include "CexmcSteppingAction.hh"
0071 #include "CexmcEventAction.hh"
0072 #include "CexmcRunAction.hh"
0073 #include "CexmcMessenger.hh"
0074 #include "CexmcException.hh"
0075 #include "CexmcBasicPhysicsSettings.hh"
0076 #include "CexmcCommon.hh"
0077 
0078 
0079 namespace
0080 {
0081     const G4String  CexmcVisManagerVerboseLevel( "errors" );
0082 }
0083 
0084 
0085 struct  CexmcCmdLineData
0086 {
0087     CexmcCmdLineData() : isInteractive( false ), startQtSession( false ),
0088                          preinitMacro( "" ), initMacro( "" ), rProject( "" ),
0089                          wProject( "" ), overrideExistingProject( false ),
0090                          customFilter( "" )
0091     {}
0092 
0093     G4bool                  isInteractive;
0094     G4bool                  startQtSession;
0095     G4String                preinitMacro;
0096     G4String                initMacro;
0097     G4String                rProject;
0098     G4String                wProject;
0099     G4bool                  overrideExistingProject;
0100     CexmcOutputDataTypeSet  outputData;
0101     G4String                customFilter;
0102 };
0103 
0104 
0105 void  printUsage( void )
0106 {
0107 #ifdef CEXMC_PROG_NAME
0108     const char *  progName( CEXMC_PROG_NAME );
0109 #else
0110     const char *  progName( "cexmc" );
0111 #endif
0112 
0113     G4cout << "Usage: " << progName << " [-i] "
0114 #ifdef G4UI_USE_QT
0115                            "[-g] "
0116 #endif
0117                            "[-p preinit_macro] [-m init_macro] "
0118 #ifdef CEXMC_USE_PERSISTENCY
0119                            "[[-y] -w project]" << G4endl <<
0120               "             [-r project "
0121 #ifdef CEXMC_USE_CUSTOM_FILTER
0122                            "[-f filter_script] "
0123 #endif
0124                            "[-o list]]"
0125 #endif
0126            << G4endl;
0127     G4cout << "or     " << progName << " [--help | -h]" << G4endl;
0128     G4cout << "           -i - run in interactive mode" << G4endl;
0129 #ifdef G4UI_USE_QT
0130     G4cout << "           -g - start graphical interface (Qt), implies "
0131                               "interactive mode " << G4endl;
0132 #endif
0133     G4cout << "           -p - use specified preinit macro file " << G4endl;
0134     G4cout << "           -m - use specified init macro file " << G4endl;
0135 #ifdef CEXMC_USE_PERSISTENCY
0136     G4cout << "           -w - save data in specified project files" << G4endl;
0137     G4cout << "           -r - read data from specified project files" <<
0138               G4endl;
0139 #ifdef CEXMC_USE_CUSTOM_FILTER
0140     G4cout << "           -f - use specified custom filter script" << G4endl;
0141 #endif
0142     G4cout << "           -o - comma-separated list of data to output, "
0143                               "possible values:" << G4endl <<
0144               "                run, geom, events" << G4endl;
0145     G4cout << "           -y - force project override" << G4endl;
0146 #endif
0147     G4cout << "  --help | -h - print this message and exit " << G4endl;
0148 }
0149 
0150 
0151 G4bool  parseArgs( int  argc, char **  argv, CexmcCmdLineData &  cmdLineData )
0152 {
0153     if ( argc < 2 )
0154         return false;
0155 
0156     for ( G4int  i( 1 ); i < argc; ++i )
0157     {
0158         do
0159         {
0160             if ( G4String( argv[ i ] ) == "--help" )
0161             {
0162                 return false;
0163             }
0164             if ( G4String( argv[ i ] ) == "-h" )
0165             {
0166                 return false;
0167             }
0168             if ( G4String( argv[ i ], 2 ) == "-i" )
0169             {
0170                 cmdLineData.isInteractive = true;
0171                 break;
0172             }
0173 #ifdef G4UI_USE_QT
0174             if ( G4String( argv[ i ], 2 ) == "-g" )
0175             {
0176                 cmdLineData.isInteractive = true;
0177                 cmdLineData.startQtSession = true;
0178                 break;
0179             }
0180 #endif
0181             if ( G4String( argv[ i ], 2 ) == "-p" )
0182             {
0183                 cmdLineData.preinitMacro = argv[ i ] + 2;
0184                 if ( cmdLineData.preinitMacro == "" )
0185                 {
0186                     if ( ++i >= argc )
0187                         throw CexmcException( CexmcCmdLineParseException );
0188                     cmdLineData.preinitMacro = argv[ i ];
0189                 }
0190                 break;
0191             }
0192             if ( G4String( argv[ i ], 2 ) == "-m" )
0193             {
0194                 cmdLineData.initMacro = argv[ i ] + 2;
0195                 if ( cmdLineData.initMacro == "" )
0196                 {
0197                     if ( ++i >= argc )
0198                         throw CexmcException( CexmcCmdLineParseException );
0199                     cmdLineData.initMacro = argv[ i ];
0200                 }
0201                 break;
0202             }
0203 #ifdef CEXMC_USE_PERSISTENCY
0204             if ( G4String( argv[ i ], 2 ) == "-w" )
0205             {
0206                 cmdLineData.wProject = argv[ i ] + 2;
0207                 if ( cmdLineData.wProject == "" )
0208                 {
0209                     if ( ++i >= argc )
0210                         throw CexmcException( CexmcCmdLineParseException );
0211                     cmdLineData.wProject = argv[ i ];
0212                 }
0213                 break;
0214             }
0215             if ( G4String( argv[ i ], 2 ) == "-r" )
0216             {
0217                 cmdLineData.rProject = argv[ i ] + 2;
0218                 if ( cmdLineData.rProject == "" )
0219                 {
0220                     if ( ++i >= argc )
0221                         throw CexmcException( CexmcCmdLineParseException );
0222                     cmdLineData.rProject = argv[ i ];
0223                 }
0224                 break;
0225             }
0226             if ( G4String( argv[ i ], 2 ) == "-y" )
0227             {
0228                 cmdLineData.overrideExistingProject = true;
0229                 break;
0230             }
0231             if ( G4String( argv[ i ], 2 ) == "-o" )
0232             {
0233                 std::string  outputData( argv[ i ] + 2 );
0234                 if ( outputData == "" )
0235                 {
0236                     if ( ++i >= argc )
0237                         throw CexmcException( CexmcCmdLineParseException );
0238                     outputData = argv[ i ];
0239                 }
0240                 std::set< std::string >  tokens;
0241                 boost::split( tokens, outputData, boost::is_any_of( "," ) );
0242                 for ( std::set< std::string >::iterator  k( tokens.begin() );
0243                                                         k != tokens.end(); ++k )
0244                 {
0245                     do
0246                     {
0247                         if ( *k == "run" )
0248                         {
0249                             cmdLineData.outputData.insert( CexmcOutputRun );
0250                             break;
0251                         }
0252                         if ( *k == "geom" )
0253                         {
0254                             cmdLineData.outputData.insert(
0255                                                           CexmcOutputGeometry );
0256                             break;
0257                         }
0258                         if ( *k == "events" )
0259                         {
0260                             cmdLineData.outputData.insert( CexmcOutputEvents );
0261                             break;
0262                         }
0263                         throw CexmcException( CexmcCmdLineParseException );
0264                     } while ( false );
0265                 }
0266                 break;
0267             }
0268 #ifdef CEXMC_USE_CUSTOM_FILTER
0269             if ( G4String( argv[ i ], 2 ) == "-f" )
0270             {
0271                 cmdLineData.customFilter = argv[ i ] + 2;
0272                 if ( cmdLineData.customFilter == "" )
0273                 {
0274                     if ( ++i >= argc )
0275                         throw CexmcException( CexmcCmdLineParseException );
0276                     cmdLineData.customFilter = argv[ i ];
0277                 }
0278                 break;
0279             }
0280 #endif
0281 #endif
0282 
0283             throw CexmcException( CexmcCmdLineParseException );
0284 
0285         } while ( false );
0286     }
0287 
0288     return true;
0289 }
0290 
0291 
0292 int  main( int  argc, char **  argv )
0293 {
0294 
0295     G4UIsession *     session( NULL );
0296 
0297 
0298     CexmcCmdLineData  cmdLineData;
0299 #ifdef CEXMC_USE_PERSISTENCY
0300     G4bool            outputDataOnly( false );
0301 #endif
0302 
0303     try
0304     {
0305         if ( ! parseArgs( argc, argv, cmdLineData ) )
0306         {
0307             printUsage();
0308             return 0;
0309         }
0310 #ifdef CEXMC_USE_PERSISTENCY
0311         if ( cmdLineData.rProject != "" &&
0312              cmdLineData.rProject == cmdLineData.wProject )
0313             throw CexmcException( CexmcCmdLineParseException );
0314         if ( cmdLineData.rProject == "" && ! cmdLineData.outputData.empty() )
0315             throw CexmcException( CexmcCmdLineParseException );
0316 #ifdef CEXMC_USE_CUSTOM_FILTER
0317         if ( cmdLineData.rProject == "" && ! cmdLineData.customFilter.empty() )
0318             throw CexmcException( CexmcCmdLineParseException );
0319 #endif
0320         if ( cmdLineData.wProject != "" && ! cmdLineData.outputData.empty() )
0321             throw CexmcException( CexmcCmdLineParseException );
0322         outputDataOnly = ! cmdLineData.outputData.empty();
0323 #endif
0324     }
0325     catch ( CexmcException &  e )
0326     {
0327         G4cout << e.what() << G4endl;
0328         return 1;
0329     }
0330     catch ( ... )
0331     {
0332         G4cout << "Unknown exception caught when parsing args" << G4endl;
0333         return 1;
0334     }
0335 
0336     CexmcRunManager *  runManager( NULL );
0337     CexmcMessenger::Instance();
0338 
0339     G4VisManager *     visManager( NULL );
0340 
0341 #ifdef CEXMC_USE_ROOT
0342     CexmcHistoManager::Instance();
0343 #endif
0344 
0345     try
0346     {
0347         runManager = new CexmcRunManager( cmdLineData.wProject,
0348                                           cmdLineData.rProject,
0349                                           cmdLineData.overrideExistingProject );
0350 #ifdef CEXMC_USE_PERSISTENCY
0351 #ifdef CEXMC_USE_CUSTOM_FILTER
0352         runManager->SetCustomFilter( cmdLineData.customFilter );
0353 #endif
0354 
0355         if ( outputDataOnly )
0356         {
0357             /* we will need an arbitrary physics list to get access to particle
0358              * table if events output was ordered */
0359             CexmcOutputDataTypeSet::const_iterator  found(
0360                             cmdLineData.outputData.find( CexmcOutputEvents ) );
0361             if ( found != cmdLineData.outputData.end() )
0362                 runManager->SetUserInitialization(
0363                                 CexmcChargeExchangePMFactory::
0364                                             Create( CexmcPionZeroProduction ) );
0365             runManager->PrintReadData( cmdLineData.outputData );
0366             delete runManager;
0367             return 0;
0368         }
0369 #endif
0370 
0371         G4UImanager *  uiManager( G4UImanager::GetUIpointer() );
0372 
0373         if ( cmdLineData.preinitMacro != "" )
0374             uiManager->ApplyCommand( "/control/execute " +
0375                                      cmdLineData.preinitMacro );
0376 
0377         CexmcProductionModelType  productionModelType(
0378                                         runManager->GetProductionModelType() );
0379 
0380         if ( productionModelType == CexmcUnknownProductionModel )
0381             throw CexmcException( CexmcPreinitException );
0382 
0383         G4VUserPhysicsList *    physicsList( CexmcChargeExchangePMFactory::
0384                                                 Create( productionModelType ) );
0385         CexmcPhysicsManager *   physicsManager(
0386                         dynamic_cast< CexmcPhysicsManager * >( physicsList ) );
0387         CexmcProductionModel *  productionModel(
0388                                         physicsManager->GetProductionModel() );
0389 
0390         if ( ! productionModel )
0391             throw CexmcException( CexmcWeirdException );
0392 
0393         G4cout << CEXMC_LINE_START << "Production model '" <<
0394                 productionModel->GetName() << "' instantiated" << G4endl;
0395 
0396         runManager->SetUserInitialization( physicsList );
0397 
0398         CexmcSetup *   setup( new CexmcSetup( runManager->GetGdmlFileName(),
0399                                     runManager->ShouldGdmlFileBeValidated() ) );
0400 
0401         runManager->SetUserInitialization( setup );
0402 
0403         runManager->Initialize();
0404 
0405         runManager->SetPhysicsManager( physicsManager );
0406 
0407         runManager->SetUserAction( new CexmcPrimaryGeneratorAction(
0408                                                             physicsManager ) );
0409 
0410         runManager->SetUserAction( new CexmcEventAction( physicsManager ) );
0411 
0412         runManager->SetUserAction( new CexmcRunAction( physicsManager ) );
0413 
0414         runManager->SetUserAction( new CexmcTrackingAction( physicsManager ) );
0415 
0416         runManager->SetUserAction( new CexmcSteppingAction( physicsManager ) );
0417 
0418 #ifdef CEXMC_USE_ROOT
0419         CexmcHistoManager::Instance()->Initialize();
0420 #endif
0421 
0422 
0423         if ( cmdLineData.isInteractive )
0424         {
0425             visManager = new G4VisExecutive( CexmcVisManagerVerboseLevel );
0426             visManager->Initialize();
0427         }
0428 
0429 
0430 #ifdef CEXMC_USE_PERSISTENCY
0431         if ( runManager->ProjectIsRead() )
0432         {
0433             runManager->ReadProject();
0434             runManager->PrintReadRunData();
0435         }
0436 #endif
0437 
0438         if ( cmdLineData.initMacro != "" )
0439             uiManager->ApplyCommand( "/control/execute " +
0440                                      cmdLineData.initMacro );
0441 
0442         if ( cmdLineData.isInteractive )
0443             productionModel->PrintInitialData();
0444 
0445 
0446         if ( cmdLineData.isInteractive )
0447         {
0448             if ( cmdLineData.startQtSession )
0449             {
0450 #ifdef G4UI_USE_QT
0451                 /* no need to pass all command line options to QApplication */
0452                 session = new G4UIQt( 1, argv );
0453                 const G4String &  guiMacroName( runManager->GetGuiMacroName() );
0454                 if ( guiMacroName != "" )
0455                     uiManager->ApplyCommand( "/control/execute " +
0456                                              guiMacroName );
0457 #ifdef CEXMC_USE_ROOTQT
0458                 CexmcHistoManager::Instance()->EnableLiveHistograms( session );
0459 #endif
0460 #endif
0461             }
0462             else
0463             {
0464 #ifdef G4UI_USE_TCSH
0465                 session = new G4UIterminal( new G4UItcsh );
0466 #else
0467                 session = new G4UIterminal;
0468 #endif
0469             }
0470             if ( session )
0471                 session->SessionStart();
0472         }
0473 
0474 #ifdef CEXMC_USE_PERSISTENCY
0475         if ( runManager->ProjectIsSaved() )
0476             runManager->SaveProject();
0477 #endif
0478     }
0479     catch ( CexmcException &  e )
0480     {
0481         G4cout << e.what() << G4endl;
0482     }
0483 #ifdef CEXMC_USE_PERSISTENCY
0484     catch ( boost::archive::archive_exception &  e )
0485     {
0486         G4cout << CEXMC_LINE_START << "Serialization error: " << e.what() <<
0487                   G4endl;
0488     }
0489 #ifdef CEXMC_USE_CUSTOM_FILTER
0490     catch ( boost::bad_get &  e )
0491     {
0492         G4cout << CEXMC_LINE_START << "Custom filter error: " << e.what() <<
0493                   G4endl;
0494     }
0495 #endif
0496 #endif
0497     catch ( ... )
0498     {
0499         G4cout << "Unknown exception caught" << G4endl;
0500     }
0501 
0502 #ifdef CEXMC_USE_ROOT
0503     CexmcHistoManager::Destroy();
0504 #endif
0505 
0506     CexmcMessenger::Destroy();
0507 
0508     delete session;
0509     delete visManager;
0510     delete runManager;
0511 
0512     return 0;
0513 }
0514