00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BDSGlobalConstants.hh"
00011
00012 #include "BDSRunAction.hh"
00013 #include "BDSRunManager.hh"
00014
00015 #include "G4Run.hh"
00016 #include "G4StateManager.hh"
00017 #include "G4UImanager.hh"
00018 #include "G4VVisManager.hh"
00019 #include "G4ios.hh"
00020
00021 #include "BDSAcceleratorComponent.hh"
00022 #include <list>
00023 #include <fstream>
00024 #include <string>
00025
00026 typedef list<BDSAcceleratorComponent*> myBeamline;
00027 extern myBeamline theBeamline;
00028
00029 const int DEBUG = 0;
00030
00031
00032
00033 BDSRunAction::BDSRunAction()
00034 {
00035 }
00036
00037
00038
00039 BDSRunAction::~BDSRunAction()
00040 {}
00041
00042
00043
00044 void BDSRunAction::BeginOfRunAction(const G4Run* aRun)
00045 {
00046
00047
00048 G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
00049
00050
00051
00052
00053
00054
00055 if (G4VVisManager::GetConcreteInstance())
00056 {
00057 G4UImanager* UI = G4UImanager::GetUIpointer();
00058 UI->ApplyCommand("/vis/scene/notifyHandlers");
00059 }
00060
00061
00062 }
00063
00064
00065
00066 void BDSRunAction::EndOfRunAction(const G4Run* aRun)
00067 {
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 if(BDSGlobals->getWaitingForDump())
00092 {
00093
00094 G4cout<<"last event reached! dumping"<<G4endl;
00095
00096 G4StackManager* SM = G4EventManager::GetEventManager()->GetStackManager();
00097
00098 BDSGlobals->setWaitingForDump(false);
00099 BDSGlobals->setDumping(true);
00100
00101 BDSGlobals->fileDump.open(BDSGlobals->GetFifo());
00102 if(!BDSGlobals->fileDump.good()){
00103 G4Exception("BDSGlobals->GetFifo(): fifo not found. Quitting.");
00104 exit(1);
00105 }
00106 BDSGlobals->fileDump << "# nparticles = " << SM->GetNPostponedTrack() << "\n";
00107 SM->TransferStackedTracks(fPostpone, fUrgent);
00108 SM->ReClassify();
00109 BDSGlobals->fileDump.close();
00110 BDSGlobals->setDumping(false);
00111 BDSGlobals->setReading(true);
00112
00113
00114
00115 int token;
00116 G4double x,y,z,t,xp,yp,zp,E;
00117 x = y = z = xp = yp = zp = t = E = 0;
00118 BDSGlobals->holdingQueue.clear();
00119
00120 G4AffineTransform tf = BDSGlobals->GetDumpTransform();
00121 G4ThreeVector pos;
00122 G4ThreeVector momDir;
00123 G4ThreeVector LocalPosition;
00124 G4ThreeVector LocalDirection;
00125
00126 FILE* fifo = fopen(BDSGlobals->GetFifo(),"r");
00127
00128 if(fifo != NULL){
00129 fscanf(fifo,"# nparticles = %i",&token);
00130 G4cout << "# nparticles = " << token << G4endl;
00131 for(int i=0; i< SM->GetNPostponedTrack();i++){
00132 fscanf(fifo,"%lf %lf %lf %lf %lf %lf",&E,&x,&y,&z,&xp,&yp);
00133 if(DEBUG) printf("%f %f %f %f %f %f\n",E,x,y,z,xp,yp);
00134
00135 xp *= 1e-6*radian;
00136 yp *= 1e-6*radian;
00137 zp = sqrt(1-xp*xp-yp*yp)*radian;
00138
00139 pos = G4ThreeVector(x,y,z)*micrometer;
00140 momDir = G4ThreeVector(xp,yp,zp);
00141
00142 LocalPosition = tf.TransformPoint(pos);
00143 LocalDirection = tf.TransformAxis(momDir);
00144
00145 t = -z/c_light;
00146 LocalPosition += LocalDirection.unit()*1e-4*micrometer;
00147
00148 if(DEBUG){
00149 G4cout << "Stacking: Pos = " << pos << G4endl;
00150 G4cout << "LocalPos: Pos = " << LocalPosition << G4endl;
00151 G4cout << "Stacking: mom = " << momDir << G4endl;
00152 G4cout << "LocalDir: mom = " << LocalDirection << G4endl;
00153 }
00154 tmpParticle holdingParticle;
00155 holdingParticle.E = E*GeV - BDSGlobals->GetParticleDefinition()->GetPDGMass();
00156 holdingParticle.t = t;
00157 holdingParticle.xp = LocalDirection.x();
00158 holdingParticle.yp = LocalDirection.y();
00159 holdingParticle.zp = LocalDirection.z();
00160
00161 holdingParticle.x = LocalPosition.x();
00162 holdingParticle.y = LocalPosition.y();
00163 holdingParticle.z = LocalPosition.z();
00164
00165 BDSGlobals->holdingQueue.push_back(holdingParticle);
00166 if(DEBUG) G4cout << "Read particle number " << i << G4endl;
00167 }
00168 sleep(1);
00169 fclose(fifo);
00170 BDSGlobals->setReading(false);
00171 BDSGlobals->setReadFromStack(false);
00172 if(DEBUG) G4cout << "Number read in = " << BDSGlobals->holdingQueue.size() << G4endl;
00173 }
00174 else{
00175 G4Exception("Read from fifo failed: bad file name\n");
00176 exit(1);
00177 }
00178 }
00179 }
00180