Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 // 27 /// \file PrimaryGeneratorAction.cc 28 /// \brief Implementation of the PrimaryGenera 29 30 #include "PrimaryGeneratorAction.hh" 31 32 #include "G4Event.hh" 33 #include "G4ParticleGun.hh" 34 #include "G4PhysicalConstants.hh" 35 #include "G4SystemOfUnits.hh" 36 #include "Randomize.hh" 37 38 #include <fstream> 39 40 PrimaryGeneratorAction::PrimaryGeneratorAction 41 : G4VUserPrimaryGeneratorAction(), fDetector 42 { 43 fpParticleGun = std::make_unique<G4ParticleG 44 fpParticleGun->SetParticleEnergy(-1); // de 45 } 46 47 //....oooOO0OOooo........oooOO0OOooo........oo 48 49 void PrimaryGeneratorAction::GeneratePrimaries 50 { 51 // user can set the energy in the macro file 52 G4double energy = fpParticleGun->GetParticle 53 54 // only first check is important as only use 55 // hopefully... 56 if (energy == -1) { // if energy is larger 57 // mono-energetic if th 58 // energy is randomized 59 fMonoEnergetic = false; 60 } 61 62 if (!fMonoEnergetic) { 63 energy = GenerateParticleEnergy(); 64 } 65 66 fpParticleGun->SetParticleEnergy(energy); 67 fpParticleGun->SetParticlePosition(GenerateP 68 fpParticleGun->SetParticleMomentumDirection( 69 GenerateParticleDirection()); // directio 70 71 fpParticleGun->GeneratePrimaryVertex(anEvent 72 } 73 74 //....oooOO0OOooo........oooOO0OOooo........oo 75 76 G4double PrimaryGeneratorAction::GenerateParti 77 { 78 if (fEnergySpectrum_length == 0) { // readi 79 std::ifstream fin(fEnergySpectrumFilename) 80 G4String len, gain, offset, counts; 81 fin >> len >> gain >> offset; 82 fEnergySpectrum_length = std::stoi(len); 83 fEnergySpectrum_gain = std::stod(gain); 84 fEnergySpectrum_offset = std::stod(offset) 85 86 fEnergySpectrum_counts.resize(fEnergySpect 87 for (G4int i = 0; i < fEnergySpectrum_leng 88 fin >> counts; 89 fEnergySpectrum_counts[i] = std::stoi(co 90 } 91 } 92 93 auto max_en_counter = fEnergySpectrum_counts 94 auto rand_count = 1 + G4int(G4UniformRand() 95 96 // primitive search for lowest en_id that gi 97 // rand_count: 98 G4int en_id = 0; 99 for (; rand_count > fEnergySpectrum_counts[e 100 ; 101 102 G4int left = fEnergySpectrum_counts[en_id - 103 G4int right = fEnergySpectrum_counts[en_id]; 104 105 G4double en_left = (en_id - 1) * fEnergySpec 106 G4double en_right = en_id * fEnergySpectrum_ 107 108 // linear interpolation: 109 G4double slope = (en_right - en_left) / (rig 110 return (en_left + slope * (rand_count - left 111 } 112 113 //....oooOO0OOooo........oooOO0OOooo........oo 114 115 G4ThreeVector PrimaryGeneratorAction::Generate 116 { 117 // the source is an infinitely thin disk of 118 G4double r = std::sqrt(G4UniformRand()) * fD 119 G4double phi = G4UniformRand() * twopi; 120 121 G4double x = fDetector->GetCollExitPosistion 122 G4double y = r * std::cos(phi); 123 G4double z = r * std::sin(phi); 124 125 return {x, y, z}; 126 } 127 128 //....oooOO0OOooo........oooOO0OOooo........oo 129 130 G4ThreeVector PrimaryGeneratorAction::Generate 131 { 132 G4double phi, theta; 133 G4double px, py, pz; 134 135 phi = G4UniformRand() * twopi; 136 137 // For convenience theta is measured along t 138 // To reduce the number of events, when the 139 // only forward angles in the range [0, max_ 140 // If theta is larger, then the projectile w 141 // Even with this restriction only 1 in 4 pr 142 // collimator. 143 G4double cos_max_theta = 144 std::cos(std::atan(fDetector->GetCollDiame 145 theta = std::acos(cos_max_theta + G4UniformR 146 147 px = std::cos(theta); 148 py = std::sin(theta) * std::sin(phi); 149 pz = std::sin(theta) * std::cos(phi); 150 151 return {px, py, pz}; 152 } 153 //....oooOO0OOooo........oooOO0OOooo........oo 154