G4ParticleGun.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 
00030 // G4ParticleGun
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& /*right*/)
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& /*right*/) 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& /*right*/) 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   // create a new vertex
00217   G4PrimaryVertex* vertex = 
00218     new G4PrimaryVertex(particle_position,particle_time);
00219 
00220   // create new primaries and set them to the vertex
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 

Generated on Mon May 27 17:49:16 2013 for Geant4 by  doxygen 1.4.7