00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "G4ParticleGun.hh"
00032 #include "G4SystemOfUnits.hh"
00033 #include "G4PrimaryParticle.hh"
00034 #include "G4ParticleGunMessenger.hh"
00035 #include "G4Event.hh"
00036 #include "G4ios.hh"
00037
00038 G4ParticleGun::G4ParticleGun()
00039 {
00040 SetInitialValues();
00041 }
00042
00043 G4ParticleGun::G4ParticleGun(G4int numberofparticles)
00044 {
00045 SetInitialValues();
00046 NumberOfParticlesToBeGenerated = numberofparticles;
00047 }
00048
00049 G4ParticleGun::G4ParticleGun
00050 (G4ParticleDefinition * particleDef, G4int numberofparticles)
00051 {
00052 SetInitialValues();
00053 NumberOfParticlesToBeGenerated = numberofparticles;
00054 SetParticleDefinition( particleDef );
00055 }
00056
00057 void G4ParticleGun::SetInitialValues()
00058 {
00059 NumberOfParticlesToBeGenerated = 1;
00060 particle_definition = 0;
00061 G4ThreeVector zero;
00062 particle_momentum_direction = (G4ParticleMomentum)zero;
00063 particle_energy = 0.0;
00064 particle_momentum = 0.0;
00065 particle_position = zero;
00066 particle_time = 0.0;
00067 particle_polarization = zero;
00068 particle_charge = 0.0;
00069 theMessenger = new G4ParticleGunMessenger(this);
00070 }
00071
00072 G4ParticleGun::~G4ParticleGun()
00073 {
00074 delete theMessenger;
00075 }
00076
00077 G4ParticleGun::G4ParticleGun(const G4ParticleGun& )
00078 :G4VPrimaryGenerator()
00079 { G4Exception(
00080 "G4ParticleGun::G4ParticleGun","Event0191",FatalException,
00081 "G4ParticleGun : Copy constructor should not be used."); }
00082
00083 const G4ParticleGun& G4ParticleGun::operator=(const G4ParticleGun& right)
00084 { G4Exception(
00085 "G4ParticleGun::operator=","Event0192",FatalException,
00086 "G4ParticleGun : Equal operator should not be used.");
00087 return right; }
00088
00089 G4int G4ParticleGun::operator==(const G4ParticleGun& ) const
00090 { G4Exception(
00091 "G4ParticleGun::operator==","Event0193",FatalException,
00092 "G4ParticleGun : == operator should not be used.");
00093 return true; }
00094
00095 G4int G4ParticleGun::operator!=(const G4ParticleGun& ) const
00096 { G4Exception(
00097 "G4ParticleGun::operator!=","Event0193",FatalException,
00098 "G4ParticleGun : != operator should not be used.");
00099 return false; }
00100
00101 void G4ParticleGun::SetParticleDefinition
00102 (G4ParticleDefinition * aParticleDefinition)
00103 {
00104 if(!aParticleDefinition)
00105 {
00106 G4Exception("G4ParticleGun::SetParticleDefinition()","Event0101",
00107 FatalException,"Null pointer is given.");
00108 }
00109 if(aParticleDefinition->IsShortLived())
00110 {
00111 if(!(aParticleDefinition->GetDecayTable()))
00112 {
00113 G4ExceptionDescription ED;
00114 ED << "G4ParticleGun does not support shooting a short-lived particle without a valid decay table." << G4endl;
00115 ED << "G4ParticleGun::SetParticleDefinition for "
00116 << aParticleDefinition->GetParticleName() << " is ignored." << G4endl;
00117 G4Exception("G4ParticleGun::SetParticleDefinition()","Event0102",
00118 JustWarning,ED);
00119 return;
00120 }
00121 }
00122 particle_definition = aParticleDefinition;
00123 particle_charge = particle_definition->GetPDGCharge();
00124 if(particle_momentum>0.0)
00125 {
00126 G4double mass = particle_definition->GetPDGMass();
00127 particle_energy =
00128 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
00129 }
00130 }
00131
00132 void G4ParticleGun::SetParticleEnergy(G4double aKineticEnergy)
00133 {
00134 particle_energy = aKineticEnergy;
00135 if(particle_momentum>0.0){
00136 if(particle_definition){
00137 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
00138 << G4endl;
00139 }else{
00140 G4cout << "G4ParticleGun::" << " " << G4endl;
00141 }
00142 G4cout << " was defined in terms of Momentum: "
00143 << particle_momentum/GeV << "GeV/c" << G4endl;
00144 G4cout << " is now defined in terms of KineticEnergy: "
00145 << particle_energy/GeV << "GeV" << G4endl;
00146 particle_momentum = 0.0;
00147 }
00148 }
00149
00150 void G4ParticleGun::SetParticleMomentum(G4double aMomentum)
00151 {
00152 if(particle_energy>0.0){
00153 if(particle_definition){
00154 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
00155 << G4endl;
00156 }else{
00157 G4cout << "G4ParticleGun::" << " " << G4endl;
00158 }
00159 G4cout << " was defined in terms of KineticEnergy: "
00160 << particle_energy/GeV << "GeV" << G4endl;
00161 G4cout << " is now defined in terms Momentum: "
00162 << aMomentum/GeV << "GeV/c" << G4endl;
00163 }
00164 if(particle_definition==0)
00165 {
00166 G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
00167 G4cout <<"Zero Mass is assumed"<<G4endl;
00168 particle_momentum = aMomentum;
00169 particle_energy = aMomentum;
00170 }
00171 else
00172 {
00173 G4double mass = particle_definition->GetPDGMass();
00174 particle_momentum = aMomentum;
00175 particle_energy =
00176 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
00177 }
00178 }
00179
00180 void G4ParticleGun::SetParticleMomentum(G4ParticleMomentum aMomentum)
00181 {
00182 if(particle_energy>0.0){
00183 if(particle_definition){
00184 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
00185 << G4endl;
00186 }else{
00187 G4cout << "G4ParticleGun::" << " " << G4endl;
00188 }
00189 G4cout << " was defined in terms of KineticEnergy: "
00190 << particle_energy/GeV << "GeV" << G4endl;
00191 G4cout << " is now defined in terms Momentum: "
00192 << aMomentum.mag()/GeV << "GeV/c" << G4endl;
00193 }
00194 if(particle_definition==0)
00195 {
00196 G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
00197 G4cout <<"Zero Mass is assumed"<<G4endl;
00198 particle_momentum_direction = aMomentum.unit();
00199 particle_momentum = aMomentum.mag();
00200 particle_energy = aMomentum.mag();
00201 }
00202 else
00203 {
00204 G4double mass = particle_definition->GetPDGMass();
00205 particle_momentum = aMomentum.mag();
00206 particle_momentum_direction = aMomentum.unit();
00207 particle_energy =
00208 std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
00209 }
00210 }
00211
00212 void G4ParticleGun::GeneratePrimaryVertex(G4Event* evt)
00213 {
00214 if(particle_definition==0) return;
00215
00216
00217 G4PrimaryVertex* vertex =
00218 new G4PrimaryVertex(particle_position,particle_time);
00219
00220
00221 G4double mass = particle_definition->GetPDGMass();
00222 for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ){
00223 G4PrimaryParticle* particle =
00224 new G4PrimaryParticle(particle_definition);
00225 particle->SetKineticEnergy( particle_energy );
00226 particle->SetMass( mass );
00227 particle->SetMomentumDirection( particle_momentum_direction );
00228 particle->SetCharge( particle_charge );
00229 particle->SetPolarization(particle_polarization.x(),
00230 particle_polarization.y(),
00231 particle_polarization.z());
00232 vertex->SetPrimary( particle );
00233 }
00234
00235 evt->AddPrimaryVertex( vertex );
00236 }
00237
00238