g4beamline.cc File Reference

#include <unistd.h>
#include <vector>
#include "G4NistManager.hh"
#include "G4GeometryManager.hh"
#include "BLMPI.hh"
#include "BLNTuple.hh"
#include "BLParam.hh"
#include "BLCommand.hh"
#include "BLGroup.hh"
#include "BLManager.hh"
#include "BLTime.hh"

Functions

void startupPrint (bool continuing)
void g4bl_exit (int value)
int main (int argc, char *argv[])

Variables

BLSetParam maxStep ("maxStep","100.0","Maximum physics step size (mm)")
BLSetParam worldMaterial ("worldMaterial","Vacuum","Material of the World volume")


Function Documentation

void startupPrint ( bool  continuing  ) 

Referenced by BLCMDoutput::command(), and main().

00045 {
00046         printf("*************************************************************\n");
00047         printf(" g4beamline version: %s                    (%s)\n",G4BLVERSION,G4BLDATE);
00048         printf("                      Copyright : Tom Roberts, Muons, Inc.\n");
00049         printf("                        License : Gnu Public License\n");
00050         printf("                            WWW : http://g4beamline.muonsinc.com");
00051         if(!continuing)
00052                 printf("\n*************************************************************\n\n");
00053         fflush(stdout); // for checking the program started up
00054 }

void g4bl_exit ( int  value  ) 

int main ( int  argc,
char *  argv[] 
)

References BLElement::allOK(), BLNTuple::closeAll(), BLManager::delayedConstruction(), BLNTuple::disableOutputFiles(), BLManager::displayVisual(), BLCommand::doCommand(), BLGroup::end(), g4bl_exit(), BLCommand::getErrorCount(), BLManager::getObject(), BLParam::getString(), BLGroup::getWorld(), BLManager::handleCallbacks(), BLMPI::init(), BLManager::initialize(), BLMPI::isMPI(), BLCommand::isValidCommand(), BLMPI::main(), BLManager::nReference(), Param, BLParam::printParam(), BLCommand::readFile(), BLGroup::setMaterial(), BLParam::setParam(), startupPrint(), BLNTuple::summary(), BLTime::timems(), BLManager::trackBeam(), and BLManager::trackTuneAndReferenceParticles().

00072 {
00073 #ifdef WIN32
00074         int crtflg = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) |
00075                         _CRTDBG_ALLOC_MEM_DF |
00076                         _CRTDBG_DELAY_FREE_MEM_DF |
00077                         _CRTDBG_CHECK_ALWAYS_DF;
00078         _CrtSetDbgFlag(crtflg); 
00079 #endif
00080 
00081         // initialize and set up MPI, if appropriate.
00082         // Sets stderr and stdout to /dev/null except for rank 0 or non-MPI.
00083         BLMPI::init(&argc,&argv);
00084 
00085         startupPrint(true);
00086 
00087         if(argc < 2) {
00088             printf("USAGE: g4beamline inputfile [viewer=TYPE] [param=value...]\n");
00089             printf("     viewer=best  (any valid viewer) Display detector and events visually\n");
00090             printf("     viewer=none    (default) Track beam.\n");
00091             printf("\n");
00092             printf("     Commands used by each viewer are in the file viewer.def\n");
00093             ::exit(1);
00094         }
00095 
00096         // set PRNG seed from the clock. Note the seed is probably re-set
00097         // at the start of each event, according to the randomseed command
00098         // (event number if no such command).
00099         CLHEP::HepRandom::setTheSeed(BLTime::timems() & 0x7FFFFFFFL);
00100 
00101         // Ensure the BLManager has been created, and do its delayed constuction
00102         BLManager *mgr = BLManager::getObject();
00103         mgr->delayedConstruction();
00104 
00105         // Handle arguments 2 thru argc-1 as arguments to the param command
00106         // They should be of the form name=value or name="a value with spaces"
00107         // Arguments that consist of a valid command will be kept until all
00108         // parameters are defined, and then will be executed in order.
00109         // Remember the shell will eat " and '
00110         G4String cmd("param");
00111         std::vector<G4String> argCommands;
00112         for(int i=2; i<argc; ++i) {
00113                 cmd += " ";
00114                 G4String s(argv[i]);
00115                 G4String::size_type p = s.find("=");
00116                 if(p == s.npos || s.find(" ") < p) {
00117                         p = s.find(" ");
00118                         G4String c = s.substr(0,p);
00119                         if(BLCommand::isValidCommand(c))
00120                             argCommands.push_back(s);
00121                         else
00122                             printf("*** g4beamline: invalid arg '%s' ignored\n",
00123                                         s.c_str());
00124                         continue;
00125                 }
00126                 cmd += s.substr(0,p+1);
00127                 if(s.find("'") == s.npos) {
00128                         cmd += "'";
00129                         cmd += s.substr(p+1);
00130                         cmd += "'";
00131                 } else if(s.find('"') == s.npos) {
00132                         cmd += '"';
00133                         cmd += s.substr(p+1);
00134                         cmd += '"';
00135                 } else {
00136                         cmd += s;
00137                 }
00138         }
00139         if(cmd.find("=") != cmd.npos)
00140                 BLCommand::doCommand(cmd);
00141         for(unsigned i=0; i<argCommands.size(); ++i)
00142                 BLCommand::doCommand(argCommands[i]);
00143 
00144         // arrange to perform a geometry test
00145         cmd = "geometry";
00146         BLCommand::doCommand(cmd);
00147 
00148         // don't write output files in visual mode
00149         G4String viewer = Param.getString("viewer");
00150         if(viewer != "none") {
00151                 BLNTuple::disableOutputFiles();
00152                 if(BLMPI::isMPI()) {
00153                         G4Exception("main","Viewer and MPI",FatalException,
00154                                 "Visualization cannot be used in MPI mode.");
00155                 }
00156         }
00157 
00158         // read input file and end the World volume
00159         if(BLCommand::readFile(argv[1]) != 0) {
00160                 char tmp[64];
00161                 sprintf(tmp,"There were %d errors in the input",
00162                                 BLCommand::getErrorCount());
00163                 G4Exception("main","Input Errors",FatalException,tmp);
00164         }
00165         BLGroup::getWorld()->end();
00166 
00167         // quit if world is empty
00168         if(BLGroup::getWorld()->getNChildren() == 0) {
00169                 G4Exception("main","Empty World",FatalException,
00170                         "No elements have been placed into the World");
00171         }
00172 
00173         // verify that viewer did not change from command-line
00174         if(viewer != Param.getString("viewer"))
00175                 G4Exception("main","Viewer changed",FatalException,
00176                   "The viewer parameter may only be set on the command-line.");
00177 
00178         Param.printParam();
00179 
00180         // use the worldMaterial.
00181         BLGroup::getWorld()->setMaterial(Param.getString("worldMaterial"));
00182 
00183         // initialize BLManager (construct the geometry, etc.)
00184         mgr->initialize();
00185 
00186         // handle pre-reference callbacks
00187         mgr->handleCallbacks(0);
00188 
00189         // Track reference particle(s), if present
00190         if(mgr->nReference() > 0)
00191                 mgr->trackTuneAndReferenceParticles();
00192 
00193         // handle post-reference callbacks
00194         mgr->handleCallbacks(1);
00195 
00196         // handle MPI (never returns if in MPI mode).
00197         BLMPI::main();
00198 
00199         // handle replace main loop callbacks
00200         mgr->handleCallbacks(3);
00201 
00202         if(viewer != "none") {
00203 #ifdef WIN32
00204                 if(viewer == "best") Param.setParam("viewer","OIWin32");
00205 #else
00206                 if(viewer == "best") Param.setParam("viewer","OIX");
00207 #endif
00208                 if(!BLElement::allOK())
00209                         G4Exception("main","Element Error",JustWarning,
00210                             "Not all BLElement-s are OK -- continuing anyway");
00211                 mgr->displayVisual();
00212         } else {
00213                 if(!BLElement::allOK()) {
00214                         G4Exception("main","Element Error",FatalException,
00215                                 "Not all BLElement-s are OK");
00216                 }
00217                 mgr->trackBeam();
00218                 BLNTuple::summary();
00219                 BLNTuple::closeAll();
00220         }
00221 
00222         // handle post-tracking callbacks
00223         mgr->handleCallbacks(2);
00224 
00225         G4cout << "g4beamline: simulation complete -- exiting" << G4endl;
00226         delete mgr;
00227 
00228         g4bl_exit(0);
00229 }


Variable Documentation

BLSetParam maxStep("maxStep","100.0","Maximum physics step size (mm)")

BLSetParam worldMaterial("worldMaterial","Vacuum","Material of the World volume")

g4beamline