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().

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


Variable Documentation

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

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

g4beamline