| 1 | //
|
|---|
| 2 | // ********************************************************************
|
|---|
| 3 | // * License and Disclaimer *
|
|---|
| 4 | // * *
|
|---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of *
|
|---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and *
|
|---|
| 7 | // * conditions of the Geant4 Software License, included in the file *
|
|---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These *
|
|---|
| 9 | // * include a list of copyright holders. *
|
|---|
| 10 | // * *
|
|---|
| 11 | // * Neither the authors of this software system, nor their employing *
|
|---|
| 12 | // * institutes,nor the agencies providing financial support for this *
|
|---|
| 13 | // * work make any representation or warranty, express or implied, *
|
|---|
| 14 | // * regarding this software system or assume any liability for its *
|
|---|
| 15 | // * use. Please see the license in the file LICENSE and URL above *
|
|---|
| 16 | // * for the full disclaimer and the limitation of liability. *
|
|---|
| 17 | // * *
|
|---|
| 18 | // * This code implementation is the result of the scientific and *
|
|---|
| 19 | // * technical work of the GEANT4 collaboration. *
|
|---|
| 20 | // * By using, copying, modifying or distributing the software (or *
|
|---|
| 21 | // * any work based on the software) you agree to acknowledge its *
|
|---|
| 22 | // * use in resulting scientific publications, and indicate your *
|
|---|
| 23 | // * acceptance of all terms of the Geant4 Software license. *
|
|---|
| 24 | // ********************************************************************
|
|---|
| 25 | //
|
|---|
| 26 | //
|
|---|
| 27 | // $Id: G4ParticleGun.cc,v 1.14 2007/11/07 17:13:19 asaim Exp $
|
|---|
| 28 | // GEANT4 tag $Name: geant4-09-03-cand-01 $
|
|---|
| 29 | //
|
|---|
| 30 |
|
|---|
| 31 | // G4ParticleGun
|
|---|
| 32 | #include "G4ParticleGun.hh"
|
|---|
| 33 | #include "G4PrimaryParticle.hh"
|
|---|
| 34 | #include "G4ParticleGunMessenger.hh"
|
|---|
| 35 | #include "G4Event.hh"
|
|---|
| 36 | #include "G4ios.hh"
|
|---|
| 37 |
|
|---|
| 38 | G4ParticleGun::G4ParticleGun()
|
|---|
| 39 | {
|
|---|
| 40 | SetInitialValues();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | G4ParticleGun::G4ParticleGun(G4int numberofparticles)
|
|---|
| 44 | {
|
|---|
| 45 | SetInitialValues();
|
|---|
| 46 | NumberOfParticlesToBeGenerated = numberofparticles;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | G4ParticleGun::G4ParticleGun
|
|---|
| 50 | (G4ParticleDefinition * particleDef, G4int numberofparticles)
|
|---|
| 51 | {
|
|---|
| 52 | SetInitialValues();
|
|---|
| 53 | NumberOfParticlesToBeGenerated = numberofparticles;
|
|---|
| 54 | SetParticleDefinition( particleDef );
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void G4ParticleGun::SetInitialValues()
|
|---|
| 58 | {
|
|---|
| 59 | NumberOfParticlesToBeGenerated = 1;
|
|---|
| 60 | particle_definition = 0;
|
|---|
| 61 | G4ThreeVector zero;
|
|---|
| 62 | particle_momentum_direction = (G4ParticleMomentum)zero;
|
|---|
| 63 | particle_energy = 0.0;
|
|---|
| 64 | particle_momentum = 0.0;
|
|---|
| 65 | particle_position = zero;
|
|---|
| 66 | particle_time = 0.0;
|
|---|
| 67 | particle_polarization = zero;
|
|---|
| 68 | particle_charge = 0.0;
|
|---|
| 69 | theMessenger = new G4ParticleGunMessenger(this);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | G4ParticleGun::~G4ParticleGun()
|
|---|
| 73 | {
|
|---|
| 74 | delete theMessenger;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | G4ParticleGun::G4ParticleGun(const G4ParticleGun& /*right*/)
|
|---|
| 78 | :G4VPrimaryGenerator()
|
|---|
| 79 | { G4Exception("G4ParticleGun : Copy constructor should not be used."); }
|
|---|
| 80 |
|
|---|
| 81 | const G4ParticleGun& G4ParticleGun::operator=(const G4ParticleGun& right)
|
|---|
| 82 | { G4Exception("G4ParticleGun : Equal operator should not be used."); return right; }
|
|---|
| 83 |
|
|---|
| 84 | G4int G4ParticleGun::operator==(const G4ParticleGun& /*right*/) const
|
|---|
| 85 | { G4Exception("G4ParticleGun : == operator should not be used."); return true; }
|
|---|
| 86 |
|
|---|
| 87 | G4int G4ParticleGun::operator!=(const G4ParticleGun& /*right*/) const
|
|---|
| 88 | { G4Exception("G4ParticleGun : == operator should not be used."); return false; }
|
|---|
| 89 |
|
|---|
| 90 | void G4ParticleGun::SetParticleDefinition
|
|---|
| 91 | (G4ParticleDefinition * aParticleDefinition)
|
|---|
| 92 | {
|
|---|
| 93 | if(!aParticleDefinition)
|
|---|
| 94 | {
|
|---|
| 95 | G4Exception("G4ParticleGun::SetParticleDefinition()","Event00003",FatalException,
|
|---|
| 96 | "Null pointer is given.");
|
|---|
| 97 | }
|
|---|
| 98 | if(aParticleDefinition->IsShortLived())
|
|---|
| 99 | {
|
|---|
| 100 | if(!(aParticleDefinition->GetDecayTable()))
|
|---|
| 101 | {
|
|---|
| 102 | G4cerr << "G4ParticleGun does not support shooting a short-lived particle without a valid decay table." << G4endl;
|
|---|
| 103 | G4cerr << "G4ParticleGun::SetParticleDefinition for "
|
|---|
| 104 | << aParticleDefinition->GetParticleName() << " is ignored." << G4endl;
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | particle_definition = aParticleDefinition;
|
|---|
| 109 | particle_charge = particle_definition->GetPDGCharge();
|
|---|
| 110 | if(particle_momentum>0.0)
|
|---|
| 111 | {
|
|---|
| 112 | G4double mass = particle_definition->GetPDGMass();
|
|---|
| 113 | particle_energy =
|
|---|
| 114 | std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void G4ParticleGun::SetParticleEnergy(G4double aKineticEnergy)
|
|---|
| 119 | {
|
|---|
| 120 | particle_energy = aKineticEnergy;
|
|---|
| 121 | if(particle_momentum>0.0){
|
|---|
| 122 | if(particle_definition){
|
|---|
| 123 | G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
|
|---|
| 124 | << G4endl;
|
|---|
| 125 | }else{
|
|---|
| 126 | G4cout << "G4ParticleGun::" << " " << G4endl;
|
|---|
| 127 | }
|
|---|
| 128 | G4cout << " was defined in terms of Momentum: "
|
|---|
| 129 | << particle_momentum/GeV << "GeV/c" << G4endl;
|
|---|
| 130 | G4cout << " is now defined in terms of KineticEnergy: "
|
|---|
| 131 | << particle_energy/GeV << "GeV" << G4endl;
|
|---|
| 132 | particle_momentum = 0.0;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | void G4ParticleGun::SetParticleMomentum(G4double aMomentum)
|
|---|
| 137 | {
|
|---|
| 138 | if(particle_energy>0.0){
|
|---|
| 139 | if(particle_definition){
|
|---|
| 140 | G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
|
|---|
| 141 | << G4endl;
|
|---|
| 142 | }else{
|
|---|
| 143 | G4cout << "G4ParticleGun::" << " " << G4endl;
|
|---|
| 144 | }
|
|---|
| 145 | G4cout << " was defined in terms of KineticEnergy: "
|
|---|
| 146 | << particle_energy/GeV << "GeV" << G4endl;
|
|---|
| 147 | G4cout << " is now defined in terms Momentum: "
|
|---|
| 148 | << aMomentum/GeV << "GeV/c" << G4endl;
|
|---|
| 149 | }
|
|---|
| 150 | if(particle_definition==0)
|
|---|
| 151 | {
|
|---|
| 152 | G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
|
|---|
| 153 | G4cout <<"Zero Mass is assumed"<<G4endl;
|
|---|
| 154 | particle_momentum = aMomentum;
|
|---|
| 155 | particle_energy = aMomentum;
|
|---|
| 156 | }
|
|---|
| 157 | else
|
|---|
| 158 | {
|
|---|
| 159 | G4double mass = particle_definition->GetPDGMass();
|
|---|
| 160 | particle_momentum = aMomentum;
|
|---|
| 161 | particle_energy =
|
|---|
| 162 | std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | void G4ParticleGun::SetParticleMomentum(G4ParticleMomentum aMomentum)
|
|---|
| 167 | {
|
|---|
| 168 | if(particle_energy>0.0){
|
|---|
| 169 | if(particle_definition){
|
|---|
| 170 | G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
|
|---|
| 171 | << G4endl;
|
|---|
| 172 | }else{
|
|---|
| 173 | G4cout << "G4ParticleGun::" << " " << G4endl;
|
|---|
| 174 | }
|
|---|
| 175 | G4cout << " was defined in terms of KineticEnergy: "
|
|---|
| 176 | << particle_energy/GeV << "GeV" << G4endl;
|
|---|
| 177 | G4cout << " is now defined in terms Momentum: "
|
|---|
| 178 | << aMomentum.mag()/GeV << "GeV/c" << G4endl;
|
|---|
| 179 | }
|
|---|
| 180 | if(particle_definition==0)
|
|---|
| 181 | {
|
|---|
| 182 | G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
|
|---|
| 183 | G4cout <<"Zero Mass is assumed"<<G4endl;
|
|---|
| 184 | particle_momentum_direction = aMomentum.unit();
|
|---|
| 185 | particle_momentum = aMomentum.mag();
|
|---|
| 186 | particle_energy = aMomentum.mag();
|
|---|
| 187 | }
|
|---|
| 188 | else
|
|---|
| 189 | {
|
|---|
| 190 | G4double mass = particle_definition->GetPDGMass();
|
|---|
| 191 | particle_momentum = aMomentum.mag();
|
|---|
| 192 | particle_momentum_direction = aMomentum.unit();
|
|---|
| 193 | particle_energy =
|
|---|
| 194 | std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | void G4ParticleGun::GeneratePrimaryVertex(G4Event* evt)
|
|---|
| 199 | {
|
|---|
| 200 | if(particle_definition==0) return;
|
|---|
| 201 |
|
|---|
| 202 | // create a new vertex
|
|---|
| 203 | G4PrimaryVertex* vertex =
|
|---|
| 204 | new G4PrimaryVertex(particle_position,particle_time);
|
|---|
| 205 |
|
|---|
| 206 | // create new primaries and set them to the vertex
|
|---|
| 207 | G4double mass = particle_definition->GetPDGMass();
|
|---|
| 208 | G4double energy = particle_energy + mass;
|
|---|
| 209 | G4double pmom = std::sqrt(energy*energy-mass*mass);
|
|---|
| 210 | G4double px = pmom*particle_momentum_direction.x();
|
|---|
| 211 | G4double py = pmom*particle_momentum_direction.y();
|
|---|
| 212 | G4double pz = pmom*particle_momentum_direction.z();
|
|---|
| 213 | for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ )
|
|---|
| 214 | {
|
|---|
| 215 | G4PrimaryParticle* particle =
|
|---|
| 216 | new G4PrimaryParticle(particle_definition,px,py,pz);
|
|---|
| 217 | particle->SetMass( mass );
|
|---|
| 218 | particle->SetCharge( particle_charge );
|
|---|
| 219 | particle->SetPolarization(particle_polarization.x(),
|
|---|
| 220 | particle_polarization.y(),
|
|---|
| 221 | particle_polarization.z());
|
|---|
| 222 | vertex->SetPrimary( particle );
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | evt->AddPrimaryVertex( vertex );
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|