Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // >> 27 // $Id: GammaRayTelPrimaryGeneratorAction.cc 66508 2012-12-19 10:16:45Z gcosmo $ 27 // ------------------------------------------- 28 // ------------------------------------------------------------ 28 // GEANT 4 class implementation file 29 // GEANT 4 class implementation file 29 // CERN Geneva Switzerland 30 // CERN Geneva Switzerland 30 // 31 // 31 // 32 // 32 // ------------ GammaRayTelPrimaryGenerat 33 // ------------ GammaRayTelPrimaryGeneratorAction ------ 33 // by G.Santin, F.Longo & R.Giannit 34 // by G.Santin, F.Longo & R.Giannitrapani (13 nov 2000) 34 // 35 // 35 // ******************************************* 36 // ************************************************************ 36 37 >> 38 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... >> 39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... >> 40 37 #include "G4RunManager.hh" 41 #include "G4RunManager.hh" 38 #include "GammaRayTelPrimaryGeneratorAction.hh 42 #include "GammaRayTelPrimaryGeneratorAction.hh" 39 43 40 #include "GammaRayTelDetectorConstruction.hh" 44 #include "GammaRayTelDetectorConstruction.hh" 41 #include "GammaRayTelPrimaryGeneratorMessenger 45 #include "GammaRayTelPrimaryGeneratorMessenger.hh" 42 46 43 #include "G4PhysicalConstants.hh" 47 #include "G4PhysicalConstants.hh" 44 #include "G4SystemOfUnits.hh" 48 #include "G4SystemOfUnits.hh" 45 #include "G4Event.hh" 49 #include "G4Event.hh" 46 #include "G4ParticleGun.hh" 50 #include "G4ParticleGun.hh" 47 #include "G4GeneralParticleSource.hh" 51 #include "G4GeneralParticleSource.hh" 48 #include "G4ParticleTable.hh" 52 #include "G4ParticleTable.hh" 49 #include "G4ParticleDefinition.hh" 53 #include "G4ParticleDefinition.hh" 50 #include "Randomize.hh" 54 #include "Randomize.hh" 51 55 52 //....oooOO0OOooo........oooOO0OOooo........oo 56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 53 57 54 GammaRayTelPrimaryGeneratorAction::GammaRayTel << 58 GammaRayTelPrimaryGeneratorAction::GammaRayTelPrimaryGeneratorAction() 55 detector = static_cast<const GammaRayTelDe << 59 :rndmFlag("off"),nSourceType(0),nSpectrumType(0) 56 << 60 { 57 // create a messenger for this class << 61 G4RunManager* runManager = G4RunManager::GetRunManager(); 58 << 62 GammaRayTelDetector = 59 gunMessenger = new GammaRayTelPrimaryGener << 63 (GammaRayTelDetectorConstruction*)(runManager->GetUserDetectorConstruction()); 60 << 64 61 constexpr auto NUMBER_OF_PARTICLES{1}; << 65 62 particleGun = new G4ParticleGun(NUMBER_OF_ << 66 //create a messenger for this class 63 << 67 64 // default particle kinematic << 68 gunMessenger = new GammaRayTelPrimaryGeneratorMessenger(this); 65 << 69 66 auto *particleTable = G4ParticleTable::Get << 70 G4int n_particle = 1; 67 auto *particle = particleTable->FindPartic << 71 68 particleGun->SetParticleDefinition(particl << 72 particleGun = new G4ParticleGun(n_particle); 69 particleGun->SetParticleMomentumDirection( << 73 // default particle kinematic 70 << 74 71 constexpr auto PARTICLE_ENERGY{30. * MeV}; << 75 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable(); 72 particleGun->SetParticleEnergy(PARTICLE_EN << 76 G4String particleName; 73 << 77 G4ParticleDefinition* particle 74 auto position = 0.5 * (detector->GetWorldS << 78 = particleTable->FindParticle(particleName="e-"); 75 particleGun->SetParticlePosition(G4ThreeVe << 79 particleGun->SetParticleDefinition(particle); 76 particleSource = new G4GeneralParticleSour << 80 particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,-1.)); >> 81 particleGun->SetParticleEnergy(30.*MeV); >> 82 G4double position = 0.5*(GammaRayTelDetector->GetWorldSizeZ()); >> 83 particleGun->SetParticlePosition(G4ThreeVector(0.*cm,0.*cm,position)); >> 84 particleSource = new G4GeneralParticleSource(); >> 85 77 } 86 } 78 87 79 //....oooOO0OOooo........oooOO0OOooo........oo 88 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 80 89 81 GammaRayTelPrimaryGeneratorAction::~GammaRayTe << 90 GammaRayTelPrimaryGeneratorAction::~GammaRayTelPrimaryGeneratorAction() >> 91 { >> 92 if (sourceGun) 82 delete particleGun; 93 delete particleGun; >> 94 else 83 delete particleSource; 95 delete particleSource; 84 delete gunMessenger; << 96 >> 97 delete gunMessenger; 85 } 98 } 86 99 87 //....oooOO0OOooo........oooOO0OOooo........oo 100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 88 101 89 void GammaRayTelPrimaryGeneratorAction::Genera << 102 void GammaRayTelPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) 90 if (sourceGun) { << 103 { 91 G4cout << "Using G4ParticleGun... " << << 104 if (sourceGun) 92 << 105 { 93 // this function is called at the begi << 106 94 // << 107 //this function is called at the begining of event 95 G4double x0 = 0. * cm; << 108 // 96 G4double y0 = 0. * cm; << 109 G4double z0 = 0.5*(GammaRayTelDetector->GetWorldSizeZ()); 97 G4double z0 = 0.5 * (detector->GetWorl << 110 G4double x0 = 0.*cm, y0 = 0.*cm; 98 << 111 99 G4ThreeVector pos0; << 112 G4ThreeVector pos0; 100 auto vertex0 = G4ThreeVector(x0, y0, z << 113 G4ThreeVector dir0; 101 auto momentumDirection0 = G4ThreeVecto << 114 G4ThreeVector vertex0 = G4ThreeVector(x0,y0,z0); 102 << 115 103 G4double theta; << 116 dir0 = G4ThreeVector(0.,0.,-1.); 104 G4double phi; << 117 105 G4double y = 0.; << 118 G4double theta, phi, y, f; 106 G4double f = 0.; << 119 G4double theta0=0.; 107 G4double theta0 = 0.; << 120 G4double phi0=0.; 108 G4double phi0 = 0.; << 121 109 << 122 switch(nSourceType) { 110 switch (sourceType) { << 123 case 0: 111 case 0: << 124 particleGun->SetParticlePosition(vertex0); 112 particleGun->SetParticlePosition(v << 125 particleGun->SetParticleMomentumDirection(dir0); 113 particleGun->SetParticleMomentumDi << 126 break; 114 break; << 127 case 1: 115 case 1: << 128 // GS: Generate random position on the 4PIsphere to create a unif. distrib. 116 // GS: Generate random position on << 129 // GS: on the sphere 117 // GS: on the sphere << 130 phi = G4UniformRand() * twopi; 118 phi = G4UniformRand() * twopi; << 131 do { 119 do { << 132 y = G4UniformRand()*1.0; 120 y = G4UniformRand() * 1.0; << 133 theta = G4UniformRand() * pi; 121 theta = G4UniformRand() * pi; << 134 f = std::sin(theta); 122 f = std::sin(theta); << 135 } while (y > f); 123 } while (y > f); << 136 vertex0 = G4ThreeVector(1.,0.,0.); 124 vertex0 = G4ThreeVector(1., 0., 0. << 137 vertex0.setMag(dVertexRadius); 125 vertex0.setMag(vertexRadius); << 138 vertex0.setTheta(theta); 126 vertex0.setTheta(theta); << 139 vertex0.setPhi(phi); 127 vertex0.setPhi(phi); << 140 particleGun->SetParticlePosition(vertex0); 128 particleGun->SetParticlePosition(v << 141 129 << 142 dir0 = G4ThreeVector(1.,0.,0.); 130 momentumDirection0 = G4ThreeVector << 143 do { 131 << 144 phi = G4UniformRand() * twopi; 132 do { << 145 do { 133 phi = G4UniformRand() * twopi; << 146 y = G4UniformRand()*1.0; 134 do { << 147 theta = G4UniformRand() * pi; 135 y = G4UniformRand() * 1.0; << 148 f = std::sin(theta); 136 theta = G4UniformRand() * << 149 } while (y > f); 137 f = std::sin(theta); << 150 dir0.setPhi(phi); 138 } while (y > f); << 151 dir0.setTheta(theta); 139 momentumDirection0.setPhi(phi) << 152 } while (vertex0.dot(dir0) >= -0.7 * vertex0.mag()); 140 momentumDirection0.setTheta(th << 153 particleGun->SetParticleMomentumDirection((G4ParticleMomentum)dir0); 141 } while (vertex0.dot(momentumDirec << 154 142 << 155 break; 143 particleGun->SetParticleMomentumDi << 156 case 2: 144 << 157 // GS: Generate random position on the upper semi-sphere z>0 to create a unif. distrib. 145 break; << 158 // GS: on a plane 146 case 2: << 159 phi = G4UniformRand() * twopi; 147 // GS: Generate random position on << 160 do { 148 // GS: on a plane << 161 y = G4UniformRand()*1.0; 149 phi = G4UniformRand() * twopi; << 162 theta = G4UniformRand() * halfpi; 150 << 163 f = std::sin(theta) * std::cos(theta); 151 do { << 164 } while (y > f); 152 y = G4UniformRand() * 1.0; << 165 vertex0 = G4ThreeVector(1.,0.,0.); 153 theta = G4UniformRand() * half << 166 154 f = std::sin(theta) * std::cos << 167 G4double xy = GammaRayTelDetector->GetWorldSizeXY(); 155 } while (y > f); << 168 G4double z = GammaRayTelDetector->GetWorldSizeZ(); 156 << 169 157 vertex0 = G4ThreeVector(1., 0., 0. << 170 if (dVertexRadius > xy*0.5) 158 << 171 { 159 auto xy = detector->GetWorldSizeXY << 172 G4cout << "vertexRadius too big " << G4endl; 160 auto z = detector->GetWorldSizeZ() << 173 G4cout << "vertexRadius setted to " << xy*0.45 << G4endl; 161 << 174 dVertexRadius = xy*0.45; 162 if (vertexRadius > xy * 0.5) { << 175 } 163 G4cout << "vertexRadius too bi << 176 164 G4cout << "vertexRadius set to << 177 if (dVertexRadius > z*0.5) 165 vertexRadius = xy * 0.45; << 178 { 166 } << 179 G4cout << "vertexRadius too high " << G4endl; 167 << 180 G4cout << "vertexRadius setted to " << z*0.45 << G4endl; 168 if (vertexRadius > z * 0.5) { << 181 dVertexRadius = z*0.45; 169 G4cout << "vertexRadius too hi << 182 } 170 G4cout << "vertexRadius set to << 183 171 vertexRadius = z * 0.45; << 184 172 } << 185 vertex0.setMag(dVertexRadius); 173 << 186 vertex0.setTheta(theta); 174 vertex0.setMag(vertexRadius); << 187 vertex0.setPhi(phi); 175 vertex0.setTheta(theta); << 188 176 vertex0.setPhi(phi); << 189 // GS: Get the user defined direction for the primaries and 177 << 190 // GS: Rotate the random position according to the user defined direction for the particle 178 // GS: Get the user defined direct << 191 179 // GS: Rotate the random position << 192 dir0 = particleGun->GetParticleMomentumDirection(); 180 << 193 if (dir0.mag() > 0.001) 181 momentumDirection0 = particleGun-> << 194 { 182 if (momentumDirection0.mag() > 0.0 << 195 theta0 = dir0.theta(); 183 theta0 = momentumDirection0.th << 196 phi0 = dir0.phi(); 184 phi0 = momentumDirection0.phi( << 197 } 185 } << 198 186 << 199 if (theta0!=0.) 187 if (theta0 != 0.) { << 200 { 188 G4ThreeVector rotationAxis(1., << 201 G4ThreeVector rotationAxis(1.,0.,0.); 189 rotationAxis.setPhi(phi0 + hal << 202 rotationAxis.setPhi(phi0+halfpi); 190 vertex0.rotate(theta0 + pi, ro << 203 vertex0.rotate(theta0+pi,rotationAxis); 191 } << 204 } 192 particleGun->SetParticlePosition(v << 205 particleGun->SetParticlePosition(vertex0); 193 break; << 206 break; 194 } << 207 } 195 << 208 196 constexpr auto INITIAL_PARTICLE_ENERGY << 209 197 G4double particleEnergy = INITIAL_PART << 210 G4double pEnergy; 198 << 211 199 switch (spectrumType) { << 212 switch(nSpectrumType) { 200 case 0: // Uniform energy (1 GeV - 10 << 213 case 0: 201 y = G4UniformRand(); << 214 break; 202 particleEnergy = y * 9.0 * GeV + 1 << 215 case 1: 203 G4cout << "Particle energy: " << p << 216 break; 204 break; << 217 case 2: 205 case 1: // Logarithmic energy << 218 do { 206 y = G4UniformRand(); << 219 y = G4UniformRand()*100000.0; 207 particleEnergy = std::pow(10, y) * << 220 pEnergy = G4UniformRand() * 10. * GeV; 208 G4cout << "Particle energy: " << p << 221 f = std::pow(pEnergy * (1/GeV), -4.); 209 break; << 222 } while (y > f); 210 case 2: // Power law (-4) << 223 211 do { << 224 particleGun->SetParticleEnergy(pEnergy); 212 y = G4UniformRand() * 100000.0 << 225 213 particleEnergy = G4UniformRand << 226 break; 214 f = std::pow(particleEnergy * << 227 case 3: 215 } while (y > f); << 228 break; 216 // particleGun->SetParticleEnergy( << 229 } 217 break; << 230 218 case 3: // Monochromatic << 231 particleGun->GeneratePrimaryVertex(anEvent); 219 particleEnergy = particleGun->GetP << 220 // 100 MeV; << 221 G4cout << "Particle energy: " << p << 222 break; << 223 } << 224 particleGun->SetParticleEnergy(particl << 225 G4cout << "Particle: " << particleGun- << 226 particleGun->GeneratePrimaryVertex(eve << 227 } else { << 228 particleSource->GeneratePrimaryVertex( << 229 } 232 } >> 233 else >> 234 { >> 235 particleSource->GeneratePrimaryVertex(anEvent); >> 236 } >> 237 230 } 238 } >> 239 >> 240 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... >> 241 >> 242 >> 243 >> 244 >> 245 >> 246 >> 247 >> 248 231 249