00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 const int DEBUG = 0;
00021
00022 #include "BDSGlobalConstants.hh"
00023
00024 #include "G4UImanager.hh"
00025 #include "G4UIterminal.hh"
00026 #include "G4UItcsh.hh"
00027
00028 #include "Randomize.hh"
00029
00030 #ifdef G4VIS_USE
00031 #include "BDSVisManager.hh"
00032 #endif
00033
00034
00035 #include <cstdlib>
00036 #include <cstdio>
00037 #include <unistd.h>
00038 #include <getopt.h>
00039
00040
00041 #include "BDSDetectorConstruction.hh"
00042 #include "BDSPhysicsList.hh"
00043 #include "BDSPrimaryGeneratorAction.hh"
00044 #include "BDSRunAction.hh"
00045 #include "BDSEventAction.hh"
00046 #include "BDSSteppingAction.hh"
00047 #include "BDSStackingAction.hh"
00048 #include "BDSUserTrackingAction.hh"
00049 #include "BDSRunManager.hh"
00050 #include "G4EventManager.hh"
00051 #include "G4TrackingManager.hh"
00052 #include "G4SteppingManager.hh"
00053 #include "G4GeometryTolerance.hh"
00054 #include "BDSGeometryInterface.hh"
00055
00056 #include "BDSOutput.hh"
00057 #include "BDSBunch.hh"
00058 #include "BDSMaterials.hh"
00059
00060 #include "parser/gmad.h"
00061
00062
00063
00064
00065
00066
00067
00068 static void usage()
00069 {
00070 G4cout<<"bdsim : version 0.4 build _UNKNOWN_BUILD_DATE_"<<G4endl;
00071 G4cout<<" (C) 2001-2007 Royal Holloway University London"<<G4endl;
00072 G4cout<<" http://ilc.pp.rhul.ac.uk/bdsim.html"<<G4endl;
00073 G4cout<<G4endl;
00074
00075 G4cout<<"Usage: bdsim [options]"<<G4endl;
00076 G4cout<<"Options:"<<G4endl;
00077 G4cout<<"--file=<filename> : specify the lattice file "<<G4endl
00078 <<"--output=<fmt> : output format (root|ascii), default ascii"<<G4endl
00079 <<"--outfile=<file> : output file name. Will be appended with _N"<<G4endl
00080 <<" where N = 0, 1, 2, 3... etc."<<G4endl
00081 <<"--vis_mac=<file> : file with the visualization macro script, default vis.mac"<<G4endl
00082 <<"--help : display this message"<<G4endl
00083 <<"--verbose : display general parameters before run"<<G4endl
00084 <<"--verbose_event : display information for every event "<<G4endl
00085 <<"--verbose_step : display tracking information after each step"<<G4endl
00086 <<"--verbose_event_num=N : display tracking information for event number N"<<G4endl
00087 <<"--batch : batch mode - no graphics"<<G4endl
00088 <<"--outline=<file> : print geometry info to <file>"<<G4endl
00089 <<"--outline_type=<fmt> : type of outline format"<<G4endl
00090 <<" where fmt = optics | survey"<<G4endl
00091 <<"--materials : list materials included in bdsim by default"<<G4endl;
00092
00093 }
00094
00095
00096 BDSGlobalConstants* BDSGlobals;
00097 BDSOutput bdsOutput;
00098 BDSBunch theBunch;
00099 G4int outputFormat=_ASCII;
00100 G4String outputFilename="output";
00101 G4String outlinefile="BDSOutline.dat";
00102 G4String outlineType="";
00103 G4String inputFilename= "optics.gmad";
00104 G4String visMacroFile="vis.mac";
00105
00106 G4bool outline = false;
00107 G4bool verbose = false;
00108 G4bool verboseStep = false;
00109 G4bool verboseEvent = false;
00110 G4int verboseEventNumber = -1;
00111 G4bool isBatch = false;
00112
00113 G4int verboseRunLevel = 0;
00114 G4int verboseEventLevel = 0;
00115 G4int verboseTrackingLevel = 0;
00116 G4int verboseSteppingLevel = 0;
00117
00118 BDSSamplerSD* BDSSamplerSensDet;
00119
00120 G4int nptwiss = 200;
00121
00122
00123
00124
00125 int main(int argc,char** argv) {
00126
00127
00128
00129
00130
00131 static struct option LongOptions[] = {
00132 { "help" , 0, 0, 0 },
00133 { "outline", 1, 0, 0 },
00134 { "outline_type", 1, 0, 0 },
00135 { "verbose", 0, 0, 0 },
00136 { "verbose_step", 0, 0, 0 },
00137 { "verbose_event", 0, 0, 0 },
00138 { "verbose_event_num", 1, 0, 0 },
00139 { "verbose_G4run", 1, 0, 0 },
00140 { "verbose_G4event", 1, 0, 0 },
00141 { "verbose_G4tracking", 1, 0, 0 },
00142 { "verbose_G4stepping", 1, 0, 0 },
00143 { "file", 1, 0, 0 },
00144 { "vis_mac", 1, 0, 0 },
00145 { "output", 1, 0, 0 },
00146 { "outfile", 1, 0, 0 },
00147 { "batch", 0, 0, 0 },
00148 { "materials", 0, 0, 0 },
00149 { 0, 0, 0, 0 }
00150 };
00151
00152 int OptionIndex = 0;
00153 int c;
00154 int ThisOptionId;
00155
00156 for(;;)
00157 {
00158
00159 ThisOptionId = optind ? optind : 1;
00160 OptionIndex = 0;
00161
00162 c = getopt_long(argc, argv, "Vv",
00163 LongOptions, &OptionIndex );
00164
00165 if ( c == -1 )
00166 break;
00167
00168 switch (c) {
00169 case 0:
00170 if( !strcmp(LongOptions[OptionIndex].name , "help") )
00171 {
00172 usage();
00173 return 1;
00174 }
00175 if( !strcmp(LongOptions[OptionIndex].name , "batch") )
00176 {
00177 isBatch = true;
00178 }
00179 if( !strcmp(LongOptions[OptionIndex].name , "verbose") )
00180 {
00181 verbose = true;
00182 }
00183 if( !strcmp(LongOptions[OptionIndex].name , "verbose_step") )
00184 {
00185 verboseStep = true;
00186
00187 verboseEvent = true;
00188 }
00189 if( !strcmp(LongOptions[OptionIndex].name , "verbose_event") )
00190 {
00191 verboseEvent = true;
00192 }
00193 if( !strcmp(LongOptions[OptionIndex].name , "verbose_event_num") )
00194 {
00195 if(optarg)
00196 verboseEventNumber = atoi(optarg);
00197 }
00198 if( !strcmp(LongOptions[OptionIndex].name , "verbose_G4run") )
00199 {
00200 if(optarg)
00201 verboseRunLevel = atoi(optarg);
00202 }
00203 if( !strcmp(LongOptions[OptionIndex].name , "verbose_G4event") )
00204 {
00205 if(optarg)
00206 verboseEventLevel = atoi(optarg);
00207 }
00208 if( !strcmp(LongOptions[OptionIndex].name , "verbose_G4tracking") )
00209 {
00210 if(optarg)
00211 verboseTrackingLevel = atoi(optarg);
00212 }
00213 if( !strcmp(LongOptions[OptionIndex].name , "verbose_G4stepping") )
00214 {
00215 if(optarg)
00216 verboseSteppingLevel = atoi(optarg);
00217 }
00218 if( !strcmp(LongOptions[OptionIndex].name , "output") )
00219 {
00220 if(optarg) {
00221 if(!strcmp(optarg,"ascii")) outputFormat=_ASCII;
00222 else if (!strcmp(optarg,"root")) outputFormat=_ROOT;
00223 else G4cerr<<"unknown output format "<<optarg<<G4endl;
00224 }
00225 }
00226 if( !strcmp(LongOptions[OptionIndex].name , "outfile") )
00227 {
00228 if(optarg) {
00229 outputFilename=optarg;
00230 }
00231 }
00232 if( !strcmp(LongOptions[OptionIndex].name , "outline") )
00233 {
00234 if(optarg) outlinefile = optarg;
00235 outline=true;
00236 }
00237 if( !strcmp(LongOptions[OptionIndex].name , "outline_type") )
00238 {
00239 if(optarg) outlineType = optarg;
00240 outline=true;
00241 }
00242 if( !strcmp(LongOptions[OptionIndex].name , "file") )
00243 {
00244 if(optarg) {
00245 inputFilename=optarg;
00246 }
00247 else {
00248 G4cout<<"please specify the lattice filename"<<G4endl;
00249 }
00250 }
00251 if( !strcmp(LongOptions[OptionIndex].name , "vis_mac") )
00252 {
00253 if(optarg) {
00254 visMacroFile=optarg;
00255 }
00256 else {
00257 G4cout<<"please specify the visualization macro file"<<G4endl;
00258 }
00259 }
00260 if( !strcmp(LongOptions[OptionIndex].name, "materials") )
00261 {
00262 BDSMaterials::ListMaterials();
00263 return 1;
00264 }
00265 break;
00266 case -1:
00267 break;
00268 default:
00269 break;
00270 }
00271
00272 }
00273
00274
00275
00276
00277
00278
00279 G4cout<<"Using input file: "<<inputFilename<<G4endl;
00280
00281 if( gmad_parser(inputFilename) == -1)
00282 {
00283 G4cout<<"can't open input file "<<inputFilename<<G4endl;
00284 exit(1);
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 BDSGlobals = new BDSGlobalConstants(options);
00295 theBunch.SetOptions(options);
00296
00297
00298
00299
00300
00301
00302 bdsOutput.SetFormat(outputFormat);
00303 G4cout.precision(10);
00304
00305
00306
00307
00308
00309
00310
00311 HepRandom::setTheEngine(new RanecuEngine);
00312
00313 long seed;
00314
00315
00316
00317 #include <ctime>
00318 if(BDSGlobals->GetRandomSeed()>=0)
00319 seed = BDSGlobals->GetRandomSeed();
00320 else
00321 seed = time(NULL);
00322
00323
00324 HepRandom::setTheSeed(seed);
00325
00326 if(DEBUG) G4cout<<"Seed from BDSGlobals="<<BDSGlobals->GetRandomSeed()<<G4endl;
00327 G4cout<<"Random number generator's seed="<<HepRandom::getTheSeed()<<G4endl;
00328
00329
00330
00331
00332
00333
00334
00335 if(DEBUG) G4cout<<"constructing run manager"<<G4endl;
00336 BDSRunManager * runManager = new BDSRunManager;
00337
00338
00339 if(DEBUG) G4cout<<"constructing detector"<<G4endl;
00340 BDSDetectorConstruction* detector = new BDSDetectorConstruction;
00341
00342 G4GeometryTolerance* tolerance = new G4GeometryTolerance();
00343 G4double Toler = 10.*mm;
00344 tolerance->SetSurfaceTolerance(Toler);
00345
00346
00347
00348 if(DEBUG) G4cout<<"user init detector"<<G4endl;
00349 runManager->SetUserInitialization(detector);
00350
00351 if(DEBUG) G4cout<<"constructing phys list"<<G4endl;
00352 BDSPhysicsList* PhysList=new BDSPhysicsList;
00353
00354 if(DEBUG) G4cout<<"user init phys list"<<G4endl;
00355 runManager->SetUserInitialization(PhysList);
00356
00357
00358
00359
00360
00361
00362 if(DEBUG) G4cout<<"user action - detector"<<G4endl;
00363 runManager->SetUserAction(new BDSPrimaryGeneratorAction(detector));
00364
00365 if(DEBUG) G4cout<<"user action - runaction"<<G4endl;
00366 runManager->SetUserAction(new BDSRunAction);
00367
00368 if(DEBUG) G4cout<<"user action - eventaction"<<G4endl;
00369 runManager->SetUserAction(new BDSEventAction());
00370
00371 if(DEBUG) G4cout<<"user action - steppingaction"<<G4endl;
00372 runManager->SetUserAction(new BDSSteppingAction);
00373
00374 if(DEBUG) G4cout<<"user action - trackingaction"<<G4endl;
00375 runManager->SetUserAction(new BDSUserTrackingAction);
00376
00377 if(DEBUG) G4cout<<"user action - stackingaction"<<G4endl;
00378 runManager->SetUserAction(new BDSStackingAction);
00379
00380
00381
00382
00383
00384
00385 if(DEBUG) G4cout<<"init kernel"<<G4endl;
00386 runManager->Initialize();
00387
00388
00389
00390
00391 runManager
00392 ->SetVerboseLevel(verboseRunLevel);
00393 G4EventManager::GetEventManager()
00394 ->SetVerboseLevel(verboseEventLevel);
00395 G4EventManager::GetEventManager()->GetTrackingManager()
00396 ->SetVerboseLevel(verboseTrackingLevel);
00397 G4EventManager::GetEventManager()->GetTrackingManager()->GetSteppingManager()
00398 ->SetVerboseLevel(verboseSteppingLevel);
00399
00400 bdsOutput.Init(0);
00401
00402
00403
00404
00405
00406
00407 if(outline) {
00408 if(DEBUG) G4cout<<"contructing geometry interface"<<G4endl;
00409 BDSGeometryInterface* BDSGI = new BDSGeometryInterface(outlinefile);
00410
00411 if(DEBUG) G4cout<<"writing survey file"<<G4endl;
00412 if(outlineType=="survey") BDSGI->Survey();
00413 if(outlineType=="optics") BDSGI->Optics();
00414
00415 if(DEBUG) G4cout<<"deleting geometry interface"<<G4endl;
00416 delete BDSGI;
00417 }
00418
00419
00420
00421
00422
00423
00424 if(BDSGlobals->DoTwiss())
00425 {
00426
00427 G4cout<<"do twiss"<<G4endl;
00428
00429
00430 G4ProcessManager *pManager = G4Electron::Electron()->GetProcessManager();
00431 G4ProcessVector *procVec=pManager->GetProcessList();
00432 G4int nProc=pManager->GetProcessListLength();
00433
00434
00435 for(G4int iProc=0;iProc<nProc;iProc++)
00436 {
00437 G4String pName=(*procVec)[iProc]->GetProcessName();
00438 if(pName=="BDSSynchRad")
00439 {
00440 G4cout<<"Disabling SR"<<G4endl;
00441 pManager->SetProcessActivation(iProc, false);
00442
00443 }
00444
00445 if(pName=="contSR")
00446 {
00447 G4cout<<"Enabling constSR"<<G4endl;
00448 pManager->SetProcessActivation(iProc, true);
00449
00450 }
00451 }
00452
00453
00454 BDSGlobals->SetStopTracks(true);
00455
00456 runManager->BeamOn(nptwiss);
00457
00458
00459 G4EventManager::GetEventManager()->GetStackManager()->ClearPostponeStack();
00460
00461
00462 BDSGlobals->SetSynchTrackPhotons(options.synchTrackPhotons);
00463
00464
00465 BDSGlobals->SetStopTracks(options.stopTracks);
00466
00467 for(G4int iProc=0;iProc<nProc;iProc++)
00468 {
00469 G4String pName=(*procVec)[iProc]->GetProcessName();
00470 if(pName=="BDSSynchRad")
00471 {
00472 G4cout<<"Enabling SR"<<G4endl;
00473 pManager->SetProcessActivation(iProc, true);
00474
00475 }
00476
00477 if(pName=="contSR")
00478 {
00479 G4cout<<"Disabling constSR"<<G4endl;
00480 pManager->SetProcessActivation(iProc, false);
00481
00482 }
00483
00484 }
00485
00486 G4cout<<"done"<<G4endl;
00487
00488 }
00489
00490
00491 BDSGlobals->SetDoTwiss(false);
00492 BDSGlobals->SetSynchRescale(false);
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507 G4UIsession* session=0;
00508 G4VisManager* visManager=0;
00509
00510 if(!isBatch)
00511 {
00512 #ifdef G4UI_USE_TCSH
00513 session = new G4UIterminal(new G4UItcsh);
00514 #else
00515 session = new G4UIterminal();
00516 #endif
00517
00518 #ifdef G4VIS_USE
00519 if(DEBUG) G4cout<<"Initializing Visualisation Manager"<<G4endl;
00520 visManager = new BDSVisManager;
00521 visManager->Initialize();
00522 #endif
00523
00524
00525 G4UImanager* UI = G4UImanager::GetUIpointer();
00526
00527 UI->ApplyCommand("/control/execute " + visMacroFile);
00528
00529 session->SessionStart();
00530 delete session;
00531
00532 #ifdef G4VIS_USE
00533 if(DEBUG) G4cout<<"Visualisation Manager deleting..."<<G4endl;
00534 delete visManager;
00535 }
00536 #endif
00537 else
00538 {
00539 runManager->BeamOn(BDSGlobals->GetNumberToGenerate());
00540 }
00541
00542
00543
00544
00545
00546
00547 if(DEBUG) G4cout<<"BDSRunManager deleting..."<<G4endl;
00548 delete runManager;
00549
00550 if(DEBUG) G4cout<<"BDSGlobals deleting..."<<G4endl;
00551 delete BDSGlobals;
00552
00553 return 0;
00554 }