// g4beamline.cc - Main program for g4beamline /* This source file is part of G4beamline, http://g4beamline.muonsinc.com Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. http://www.gnu.org/copyleft/gpl.html */ #ifdef WIN32 #include /*** #include "crtdbg.h" ***/ #else #include #endif #include #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" #include "BLSignal.hh" // nested macros are needed to stringize the value of a preprocessor symbol #define QUOTE(A) QUOTE_(A) #define QUOTE_(A) #A BLSetParam maxStep("maxStep","100.0","Maximum physics step size (mm)"); BLSetParam worldMaterial("worldMaterial","Vacuum","Material of the World volume"); void startupPrint(bool continuing) { printf("*************************************************************\n"); printf(" g4beamline version: %s (%s)\n", QUOTE(G4BLVERSION),QUOTE(G4BLDATE)); printf(" Copyright : Tom Roberts, Muons, Inc.\n"); printf(" License : Gnu Public License\n"); printf(" WWW : http://g4beamline.muonsinc.com"); if(!continuing) printf("\n*************************************************************\n\n"); fflush(stdout); // for checking the program started up } void g4bl_exit(int value) { // Instead of deleting things, just open the geometry to avoid // warnings when exit() is called. G4GeometryManager::GetInstance()->OpenGeometry(); fflush(stdout); BLSignal::writeStackTrace(2); // does nothing if no stack trace exit(value); } #ifdef __APPLE__ extern "C" void abort() { printf("abort() called\n"); fflush(stdout); BLSignal::generateStackTrace(); BLSignal::writeStackTrace(2); _exit(99); } #endif int main(int argc, char *argv[]) { #ifdef WIN32 int crtflg = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF; _CrtSetDbgFlag(crtflg); #endif // initialize and set up MPI, if appropriate. // Sets stderr and stdout to /dev/null except for rank 0 or non-MPI. BLMPI::init(&argc,&argv); // print our process ID (for g4blgui to properly terminate me) #ifdef WIN32 printf("G4beamline Process ID %d\n\n",_getpid()); #else printf("G4beamline Process ID %d\n\n",getpid()); #endif if(argc < 2) { startupPrint(false); printf("USAGE: g4beamline inputfile [viewer=TYPE] [param=value...]\n"); printf(" viewer=best (any valid viewer) Display detector and events visually\n"); printf(" viewer=none (default) Track beam.\n"); ::exit(1); } else { startupPrint(true); } // set PRNG seed from the clock. Note the seed is probably re-set // at the start of each event, according to the randomseed command // (event number if no such command). CLHEP::HepRandom::setTheSeed(BLTime::timems() & 0x7FFFFFFFL); // Ensure the BLManager has been created, and do its delayed constuction BLManager *mgr = BLManager::getObject(); mgr->delayedConstruction(); // Handle arguments 2 thru argc-1 as arguments to the param command // They should be of the form name=value or name="a value with spaces" // Arguments that consist of a valid command will be kept until all // parameters are defined, and then will be executed in order. // Remember the shell will eat " and ' G4String cmd("param"); std::vector argCommands; for(int i=2; iend(); // quit if world is empty if(BLGroup::getWorld()->getNChildren() == 0) { G4Exception("main","Empty World",FatalException, "No elements have been placed into the World"); } // verify that viewer did not change from command-line if(viewer != Param.getString("viewer")) G4Exception("main","Viewer changed",FatalException, "The viewer parameter may only be set on the command-line."); Param.printParam(); // use the worldMaterial. BLGroup::getWorld()->setMaterial(Param.getString("worldMaterial")); // initialize BLManager (construct the geometry, etc.) mgr->initialize(); // handle pre-reference callbacks mgr->handleCallbacks(0); // Track reference particle(s), if present if(mgr->nReference() > 0) mgr->trackTuneAndReferenceParticles(); // handle post-reference callbacks mgr->handleCallbacks(1); // handle MPI (never returns if in MPI mode). BLMPI::main(); // handle replace main loop callbacks mgr->handleCallbacks(3); if(viewer != "none") { #ifdef WIN32 if(viewer == "best") Param.setParam("viewer","OIWin32"); #else if(viewer == "best") Param.setParam("viewer","OIX"); #endif if(!BLElement::allOK()) G4Exception("main","Element Error",JustWarning, "Not all BLElement-s are OK -- continuing anyway"); mgr->displayVisual(); } else { if(!BLElement::allOK()) { G4Exception("main","Element Error",FatalException, "Not all BLElement-s are OK"); } mgr->trackBeam(); BLNTuple::summary(); BLNTuple::closeAll(); } // handle post-tracking callbacks mgr->handleCallbacks(2); G4cout << "g4beamline: simulation complete -- exiting" << G4endl; delete mgr; g4bl_exit(0); }