/home/cern/BDSIM_new/src/BDSPrimaryGeneratorAction.cc

00001 /* BDSIM code.    Version 1.0
00002    Author: Grahame A. Blair, Royal Holloway, Univ. of London.
00003    Last modified 28.12.2004
00004    Copyright (c) 2004 by G.A.Blair.  ALL RIGHTS RESERVED. 
00005 
00006    Modified 22.03.05 by J.C.Carter, Royal Holloway, Univ. of London.
00007    Added GABs SynchGen code
00008 */
00009 
00010 
00011 const int DEBUG = 0;
00012 
00013 //==================================================================
00014 //==================================================================
00015 #include "BDSGlobalConstants.hh" // must be first in include list
00016 #include "BDSPrimaryGeneratorAction.hh"
00017 
00018 #include "BDSDetectorConstruction.hh"
00019 
00020 #include "G4Event.hh"
00021 #include "G4ParticleGun.hh"
00022 #include "G4ParticleTable.hh"
00023 #include "G4ParticleDefinition.hh"
00024 #include "G4EventManager.hh"
00025 #include "G4StackManager.hh"
00026 #include "G4Track.hh"
00027 #include "G4Trajectory.hh"
00028 
00029 #include "Randomize.hh"
00030 
00031 //#include "BDSExtract.hh"
00032 #include "BDSBunch.hh"
00033 
00034 #include<iostream>
00035 
00036 extern BDSBunch theBunch;
00037 
00038 
00039 //===================================================
00040 // Keep initial point in phase space for diagnostics
00041 G4double
00042   initial_x, initial_xp,
00043   initial_y, initial_yp,
00044   initial_z, initial_zp,
00045   initial_E, initial_t;
00046 
00047 BDSPrimaryGeneratorAction::BDSPrimaryGeneratorAction(
00048                                               BDSDetectorConstruction* BDSDC)
00049 :BDSDetector(BDSDC)
00050 {
00051  
00052   particleGun  = new G4ParticleGun(1); // 1-particle gun
00053 
00054   // initialize with default values... 
00055   // they will be overridden in GeneratePrimaries function
00056 
00057   // particleGun->SetParticleDefinition(BDSGlobals->
00058   //                                    GetParticleDefinition());
00059 
00060   particleGun->SetParticleDefinition(G4ParticleTable::GetParticleTable()->
00061                                      FindParticle("e-"));
00062 
00063   
00064   if(BDSGlobals->GetUseSynchPrimaryGen()) // synchrotron radiation generator
00065     {
00066       itsBDSSynchrotronRadiation=new BDSSynchrotronRadiation("tmpSynRad");
00067       G4double R=BDSGlobals->GetSynchPrimaryLength()/
00068         BDSGlobals->GetSynchPrimaryAngle();   
00069       itsSynchCritEng=3./2.*hbarc/pow(electron_mass_c2,3)*
00070         pow(BDSGlobals->GetBeamKineticEnergy(),3)/R;
00071       
00072       G4cout<<" BDSPrimaryGeneratorAction:  Critical Energy="<<
00073         itsSynchCritEng/keV<<" keV"<<G4endl;
00074       
00075       particleGun->SetParticleDefinition(G4ParticleTable::GetParticleTable()->
00076                      FindParticle("gamma"));
00077     }
00078 
00079   particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
00080   particleGun->SetParticlePosition(G4ThreeVector(0.*cm,0.*cm,0.*cm));
00081   particleGun->SetParticleEnergy(BDSGlobals->GetBeamKineticEnergy());
00082 }
00083 
00084 //===================================================
00085 
00086 BDSPrimaryGeneratorAction::~BDSPrimaryGeneratorAction()
00087 {
00088   delete particleGun;
00089 }
00090 
00091 //===================================================
00092 
00093 void BDSPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
00094 {
00095   //this function is called at the begining of event
00096 
00097   G4double x0, y0, z0, xp, yp, zp, t, E;
00098 
00099   if(!BDSGlobals->getReadFromStack()){
00100     theBunch.GetNextParticle(x0,y0,z0,xp,yp,zp,t,E); // get next starting point
00101   }
00102   else if(BDSGlobals->holdingQueue.size()!=0){
00103     tmpParticle holdingParticle = BDSGlobals->holdingQueue.front();
00104     x0 = holdingParticle.x;
00105     y0 = holdingParticle.y;
00106     z0 = holdingParticle.z;
00107     xp = holdingParticle.xp;
00108     yp = holdingParticle.yp;
00109     zp = holdingParticle.zp;
00110     t = holdingParticle.t;
00111     E = holdingParticle.E;
00112     if(DEBUG) printf("Particles left %i: %f %f %f %f %f %f %f %f\n",
00113         (int)BDSGlobals->holdingQueue.size(),x0,y0,z0,xp,yp,zp,t,E);
00114     BDSGlobals->holdingQueue.pop_front();
00115   }
00116   else G4Exception("No new particles to fire...\n");
00117 
00118   G4ThreeVector PartMomDir=G4ThreeVector(xp,yp,zp);
00119  
00120   particleGun->SetParticleDefinition(BDSGlobals->GetParticleDefinition());
00121   particleGun->SetParticlePosition(G4ThreeVector(x0,y0,z0 ) );
00122   particleGun->SetParticleEnergy(E);
00123   particleGun->SetParticleMomentumDirection(PartMomDir);
00124   particleGun->SetParticleTime(t);
00125 
00126   particleGun->GeneratePrimaryVertex(anEvent);
00127 
00128   G4double totalE = E+BDSGlobals->GetParticleDefinition()->GetPDGMass();
00129   if(DEBUG )
00130     {
00131       G4cout
00132         << "BDSPrimaryGeneratorAction: " << G4endl
00133         << "  position= " << particleGun->GetParticlePosition()/m<<" m"<<G4endl
00134         << "  kinetic energy= " << E/GeV << " GeV" << G4endl
00135         << "  total energy= " << totalE/GeV << " GeV" << G4endl
00136         << "  momentum direction= " << PartMomDir << G4endl;
00137     }
00138 
00139   // save initial values outside scope for entry into the samplers:
00140   initial_x=x0;
00141   initial_xp=xp;
00142   initial_y=y0;
00143   initial_yp=yp;
00144   initial_t=t;
00145   initial_z=z0;
00146   initial_zp=zp;
00147   // total energy is used elsewhere:
00148   initial_E=totalE;
00149 }
00150 
00151 
00152 //===================================================
00153 

Generated on Wed Mar 5 17:25:22 2008 for BDSIM by  doxygen 1.5.3