Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4ParticleGun class implementation << 27 // 23 // 28 // Author: Makoto Asai, 1997 << 24 // $Id: G4ParticleGun.cc,v 1.7 2003/11/19 19:28:43 asaim Exp $ 29 // ------------------------------------------- << 25 // GEANT4 tag $Name: geant4-06-00-patch-01 $ >> 26 // 30 27 >> 28 // G4ParticleGun 31 #include "G4ParticleGun.hh" 29 #include "G4ParticleGun.hh" 32 #include "G4SystemOfUnits.hh" << 33 #include "G4PrimaryParticle.hh" 30 #include "G4PrimaryParticle.hh" 34 #include "G4ParticleGunMessenger.hh" 31 #include "G4ParticleGunMessenger.hh" 35 #include "G4Event.hh" 32 #include "G4Event.hh" 36 #include "G4ios.hh" 33 #include "G4ios.hh" 37 34 38 G4ParticleGun::G4ParticleGun() 35 G4ParticleGun::G4ParticleGun() 39 { 36 { 40 SetInitialValues(); 37 SetInitialValues(); 41 } 38 } 42 39 43 G4ParticleGun::G4ParticleGun(G4int numberofpar 40 G4ParticleGun::G4ParticleGun(G4int numberofparticles) 44 { 41 { 45 SetInitialValues(); 42 SetInitialValues(); 46 NumberOfParticlesToBeGenerated = numberofpar 43 NumberOfParticlesToBeGenerated = numberofparticles; 47 } 44 } 48 45 49 G4ParticleGun::G4ParticleGun(G4ParticleDefinit << 46 G4ParticleGun::G4ParticleGun 50 G4int numberofpar << 47 (G4ParticleDefinition * particleDef, G4int numberofparticles) 51 { 48 { 52 SetInitialValues(); 49 SetInitialValues(); 53 NumberOfParticlesToBeGenerated = numberofpar 50 NumberOfParticlesToBeGenerated = numberofparticles; 54 SetParticleDefinition( particleDef ); << 51 particle_definition = particleDef; 55 } 52 } 56 53 57 void G4ParticleGun::SetInitialValues() 54 void G4ParticleGun::SetInitialValues() 58 { 55 { 59 NumberOfParticlesToBeGenerated = 1; 56 NumberOfParticlesToBeGenerated = 1; 60 particle_definition = nullptr; << 57 particle_definition = 0; 61 G4ThreeVector zero; 58 G4ThreeVector zero; 62 particle_momentum_direction = (G4ParticleMom 59 particle_momentum_direction = (G4ParticleMomentum)zero; 63 particle_energy = 0.0; 60 particle_energy = 0.0; 64 particle_momentum = 0.0; << 65 particle_position = zero; 61 particle_position = zero; 66 particle_time = 0.0; 62 particle_time = 0.0; 67 particle_polarization = zero; 63 particle_polarization = zero; 68 particle_charge = 0.0; 64 particle_charge = 0.0; 69 theMessenger = new G4ParticleGunMessenger(th 65 theMessenger = new G4ParticleGunMessenger(this); 70 } 66 } 71 67 72 G4ParticleGun::~G4ParticleGun() 68 G4ParticleGun::~G4ParticleGun() 73 { 69 { 74 delete theMessenger; 70 delete theMessenger; 75 } 71 } 76 72 77 void G4ParticleGun:: << 73 void G4ParticleGun::SetParticleDefinition 78 SetParticleDefinition(G4ParticleDefinition* aP << 74 (G4ParticleDefinition * aParticleDefinition) 79 { 75 { 80 if(aParticleDefinition == nullptr) << 81 { << 82 G4Exception("G4ParticleGun::SetParticleDef << 83 FatalException, "Null pointer << 84 } << 85 if(aParticleDefinition->IsShortLived()) 76 if(aParticleDefinition->IsShortLived()) 86 { 77 { 87 if(aParticleDefinition->GetDecayTable() == << 78 G4cerr << "G4ParticleGun does not support shooting short-lived particle." << G4endl; 88 { << 79 G4cerr << "G4ParticleGun::SetParticleDefinition for " 89 G4ExceptionDescription ED; << 80 << aParticleDefinition->GetParticleName() << " is ignored." << G4endl; 90 ED << "G4ParticleGun does not support sh << 81 return; 91 << "particle without a valid decay ta << 92 ED << "G4ParticleGun::SetParticleDefinit << 93 << aParticleDefinition->GetParticleNa << 94 G4Exception("G4ParticleGun::SetParticleD << 95 JustWarning, ED); << 96 return; << 97 } << 98 } 82 } 99 particle_definition = aParticleDefinition; 83 particle_definition = aParticleDefinition; 100 particle_charge = particle_definition->GetPD 84 particle_charge = particle_definition->GetPDGCharge(); 101 if(particle_momentum>0.0) << 102 { << 103 G4double mass = particle_definition->GetPD << 104 particle_energy = << 105 std::sqrt(particle_momentum*particle_mom << 106 } << 107 } << 108 << 109 void G4ParticleGun::SetParticleEnergy(G4double << 110 { << 111 particle_energy = aKineticEnergy; << 112 if(particle_momentum>0.0) << 113 { << 114 if(particle_definition != nullptr) << 115 { << 116 G4cout << "G4ParticleGun::" << particle_ << 117 << G4endl; << 118 } << 119 else << 120 { << 121 G4cout << "G4ParticleGun::" << " " << G4 << 122 } << 123 G4cout << " was defined in terms of Moment << 124 << particle_momentum/GeV << "GeV/c" << 125 G4cout << " is now defined in terms of Kin << 126 << particle_energy/GeV << "GeV" << 127 particle_momentum = 0.0; << 128 } << 129 } << 130 << 131 void G4ParticleGun::SetParticleMomentum(G4doub << 132 { << 133 if(particle_energy>0.0) << 134 { << 135 if(particle_definition != nullptr) << 136 { << 137 G4cout << "G4ParticleGun::" << particle_ << 138 << G4endl; << 139 } << 140 else << 141 { << 142 G4cout << "G4ParticleGun::" << " " << G4 << 143 } << 144 G4cout << " was defined in terms of Kineti << 145 << particle_energy/GeV << "GeV" < << 146 G4cout << " is now defined in terms Moment << 147 << aMomentum/GeV << "GeV/c" < << 148 } << 149 if(particle_definition == nullptr) << 150 { << 151 G4cout << "Particle Definition not defined << 152 << 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->GetPD << 160 particle_momentum = aMomentum; << 161 particle_energy = << 162 std::sqrt(particle_momentum*particle_mom << 163 } << 164 } 85 } 165 86 166 void G4ParticleGun::SetParticleMomentum(G4Part 87 void G4ParticleGun::SetParticleMomentum(G4ParticleMomentum aMomentum) 167 { 88 { 168 if(particle_energy>0.0) << 89 if(particle_definition==0) 169 { 90 { 170 if(particle_definition != nullptr) << 91 G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl; 171 { << 92 G4cout <<"Zero Mass is assumed"<<G4endl; 172 G4cout << "G4ParticleGun::" << particle_ << 93 particle_momentum_direction = aMomentum.unit(); 173 << G4endl; << 174 } << 175 else << 176 { << 177 G4cout << "G4ParticleGun::" << " " << G4 << 178 } << 179 G4cout << " was defined in terms of Kineti << 180 << particle_energy/GeV << "GeV" < << 181 G4cout << " is now defined in terms Moment << 182 << aMomentum.mag()/GeV << "GeV/c" < << 183 } << 184 if(particle_definition == nullptr) << 185 { << 186 G4cout << "Particle Definition not defined << 187 << G4endl; << 188 G4cout << "Zero Mass is assumed" << G4endl << 189 particle_momentum_direction = aMomentum.un << 190 particle_momentum = aMomentum.mag(); << 191 particle_energy = aMomentum.mag(); 94 particle_energy = aMomentum.mag(); 192 } 95 } 193 else 96 else 194 { 97 { 195 G4double mass = particle_definition->GetP 98 G4double mass = particle_definition->GetPDGMass(); 196 particle_momentum = aMomentum.mag(); << 99 G4double p = aMomentum.mag(); 197 particle_momentum_direction = aMomentum.un << 100 particle_momentum_direction = aMomentum.unit(); 198 particle_energy = << 101 if ((particle_energy>0.0)&&(abs(particle_energy+mass-sqrt(p*p+mass*mass))>keV)) 199 std::sqrt(particle_momentum*particle_mom << 102 { >> 103 G4cout << "G4ParticleGun::" << particle_definition->GetParticleName() << G4endl; >> 104 G4cout << " KineticEnergy and Momentum could be inconsistent" << G4endl; >> 105 G4cout << " (Momentum:" << p/GeV << " GeV/c"; >> 106 G4cout << " Mass:" << mass/GeV << " GeV/c/c)" << G4endl; >> 107 G4cout << " KineticEnergy is overwritten!! "; >> 108 G4cout << particle_energy/GeV << "->"; >> 109 G4cout << (sqrt(p*p+mass*mass)-mass)/GeV << "GeV" << G4endl; >> 110 } >> 111 particle_energy = sqrt(p*p+mass*mass)-mass; 200 } 112 } 201 } 113 } 202 114 203 void G4ParticleGun::GeneratePrimaryVertex(G4Ev 115 void G4ParticleGun::GeneratePrimaryVertex(G4Event* evt) 204 { 116 { 205 if(particle_definition == nullptr) << 117 if(particle_definition==0) return; 206 { << 207 G4ExceptionDescription ED; << 208 ED << "Particle definition is not defined. << 209 ED << "G4ParticleGun::SetParticleDefinitio << 210 << G4endl; << 211 G4Exception("G4ParticleGun::GeneratePrimar << 212 FatalException, ED); << 213 return; << 214 } << 215 118 216 // Create a new vertex << 119 // create a new vertex 217 // << 120 G4PrimaryVertex* vertex = 218 auto* vertex = << 219 new G4PrimaryVertex(particle_position,part 121 new G4PrimaryVertex(particle_position,particle_time); 220 122 221 // Create new primaries and set them to the << 123 // create new primaries and set them to the vertex 222 // << 223 G4double mass = particle_definition->GetPDG 124 G4double mass = particle_definition->GetPDGMass(); 224 for( G4int i=0; i<NumberOfParticlesToBeGener << 125 G4double energy = particle_energy + mass; >> 126 G4double pmom = sqrt(energy*energy-mass*mass); >> 127 G4double px = pmom*particle_momentum_direction.x(); >> 128 G4double py = pmom*particle_momentum_direction.y(); >> 129 G4double pz = pmom*particle_momentum_direction.z(); >> 130 for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ) 225 { 131 { 226 auto* particle = << 132 G4PrimaryParticle* particle = 227 new G4PrimaryParticle(particle_definitio << 133 new G4PrimaryParticle(particle_definition,px,py,pz); 228 particle->SetKineticEnergy( particle_energ << 229 particle->SetMass( mass ); 134 particle->SetMass( mass ); 230 particle->SetMomentumDirection( particle_m << 231 particle->SetCharge( particle_charge ); 135 particle->SetCharge( particle_charge ); 232 particle->SetPolarization(particle_polariz 136 particle->SetPolarization(particle_polarization.x(), 233 particle_polariz << 137 particle_polarization.y(), 234 particle_polariz << 138 particle_polarization.z()); 235 particle->SetWeight( particle_weight ); << 236 vertex->SetPrimary( particle ); 139 vertex->SetPrimary( particle ); 237 } 140 } >> 141 238 evt->AddPrimaryVertex( vertex ); 142 evt->AddPrimaryVertex( vertex ); 239 } 143 } >> 144 >> 145 240 146