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 // Hadrontherapy advanced example for Geant4 << 26 // HadrontherapyPrimarygeneratorAction.cc; 27 // See more at: https://twiki.cern.ch/twiki/bi << 27 // See more at: http://g4advancedexamples.lngs.infn.it/Examples/hadrontherapy >> 28 // ---------------------------------------------------------------------------- >> 29 // GEANT 4 - Hadrontherapy example >> 30 // ---------------------------------------------------------------------------- >> 31 // Code developed by: >> 32 // >> 33 // G.A.P. Cirrone(a)*, F.Romano(a) >> 34 // >> 35 // (a) Laboratori Nazionali del Sud >> 36 // of the INFN, Catania, Italy >> 37 // >> 38 // * cirrone@lns.infn.it >> 39 // >> 40 // ------------------------------------------------------------------------------ 28 41 29 #include "HadrontherapyPrimaryGeneratorAction. 42 #include "HadrontherapyPrimaryGeneratorAction.hh" 30 #include "HadrontherapyPrimaryGeneratorMesseng 43 #include "HadrontherapyPrimaryGeneratorMessenger.hh" 31 << 32 #include "HadrontherapyMatrix.hh" << 33 #include "HadrontherapyDetectorSD.hh" << 34 #include "G4SystemOfUnits.hh" << 35 #include "G4Event.hh" 44 #include "G4Event.hh" 36 #include "G4ParticleGun.hh" 45 #include "G4ParticleGun.hh" 37 #include "G4GeneralParticleSource.hh" << 38 #include "G4ParticleTable.hh" 46 #include "G4ParticleTable.hh" 39 #include "G4ParticleDefinition.hh" 47 #include "G4ParticleDefinition.hh" 40 #include "Randomize.hh" 48 #include "Randomize.hh" 41 #include "G4IonTable.hh" << 49 #include "HadrontherapyAnalysisManager.hh" 42 << 43 << 44 #include "G4VUserPrimaryGeneratorAction.hh" << 45 #include "G4ParticleTable.hh" << 46 50 47 #include "G4Event.hh" << 51 HadrontherapyPrimaryGeneratorAction::HadrontherapyPrimaryGeneratorAction() 48 #include "G4Timer.hh" << 52 { 49 << 53 // Define the messenger 50 #include "G4RunManager.hh" << 54 gunMessenger = new HadrontherapyPrimaryGeneratorMessenger(this); 51 55 >> 56 particleGun = new G4ParticleGun(); 52 57 >> 58 SetDefaultPrimaryParticle(); >> 59 } 53 60 54 ////////////////////////////////////////////// << 61 HadrontherapyPrimaryGeneratorAction::~HadrontherapyPrimaryGeneratorAction() 55 HadrontherapyPrimaryGeneratorAction::Hadronthe << 56 fNewSource(false) << 57 { 62 { 58 PrimaryGeneratorMessenger = new Hadronther << 63 delete particleGun; 59 particleGun = new G4GeneralParticleSource( << 64 60 calculatedPhaseSpaceFileIN = "NULL"; << 65 delete gunMessenger; 61 } 66 } >> 67 >> 68 void HadrontherapyPrimaryGeneratorAction::SetDefaultPrimaryParticle() >> 69 { >> 70 // **************************** >> 71 // Default primary particle >> 72 // **************************** >> 73 >> 74 // Define primary particles: protons >> 75 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable(); >> 76 G4ParticleDefinition* particle = particleTable -> FindParticle("proton"); >> 77 particleGun -> SetParticleDefinition(particle); >> 78 >> 79 // Define the energy of primary particles: >> 80 // gaussian distribution with mean energy = 62.0 *MeV >> 81 // and sigma = 400.0 *keV >> 82 G4double defaultMeanKineticEnergy = 62.0 *MeV; >> 83 meanKineticEnergy = defaultMeanKineticEnergy; >> 84 >> 85 G4double defaultsigmaEnergy = 400.0 *keV; >> 86 sigmaEnergy = defaultsigmaEnergy; >> 87 >> 88 #ifdef ANALYSIS_USE >> 89 // Write these values into the analysis if needed. Have to be written separately on change. >> 90 HadrontherapyAnalysisManager::getInstance()->setBeamMetaData(meanKineticEnergy, sigmaEnergy); >> 91 #endif >> 92 >> 93 // Define the parameters of the initial position: >> 94 // the y, z coordinates have a gaussian distribution >> 95 >> 96 G4double defaultX0 = -2700.0 *mm; >> 97 X0 = defaultX0; >> 98 >> 99 G4double defaultY0 = 0.0 *mm; >> 100 Y0 = defaultY0; >> 101 >> 102 G4double defaultZ0 = 0.0 *mm; >> 103 Z0 = defaultZ0; >> 104 >> 105 G4double defaultsigmaY = 1. *mm; >> 106 sigmaY = defaultsigmaY; >> 107 >> 108 G4double defaultsigmaZ = 1. *mm; >> 109 sigmaZ = defaultsigmaZ; >> 110 >> 111 // Define the parameters of the momentum of primary particles: >> 112 // The momentum along the y and z axis has a gaussian distribution >> 113 G4double defaultsigmaMomentumY = 0.0; >> 114 sigmaMomentumY = defaultsigmaMomentumY; 62 115 63 ////////////////////////////////////////////// << 116 G4double defaultsigmaMomentumZ = 0.0; 64 HadrontherapyPrimaryGeneratorAction::~Hadronth << 117 sigmaMomentumZ = defaultsigmaMomentumZ; 65 { << 66 delete PrimaryGeneratorMessenger; << 67 delete particleGun; << 68 } 118 } 69 119 70 ////////////////////////////////////////////// << 71 void HadrontherapyPrimaryGeneratorAction::Gene 120 void HadrontherapyPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) 72 { 121 { 73 if(fNewSource==true) << 122 #ifdef ANALYSIS_USE >> 123 // Increment the event counter >> 124 HadrontherapyAnalysisManager::getInstance()->startNewEvent(); >> 125 #endif >> 126 >> 127 // **************************************** >> 128 // Set the beam angular apread >> 129 // and spot size >> 130 // beam spot size >> 131 // **************************************** >> 132 >> 133 // Set the position of the primary particles >> 134 G4double x = X0; >> 135 G4double y = Y0; >> 136 G4double z = Z0; >> 137 >> 138 if ( sigmaY > 0.0 ) 74 { 139 { 75 std::ifstream in(calculatedPhaseSpaceFil << 140 y += G4RandGauss::shoot( Y0, sigmaY ); 76 G4double e, xpos, ypos, zpos,dirx,diry,d << 77 G4int PDG; << 78 G4ThreeVector pos,dir; << 79 << 80 if(in.eof()) << 81 { << 82 G4Exception("HadrontherapyPrimaryGenerator << 83 } << 84 << 85 while(!in.eof()) << 86 { << 87 << 88 in >> e >> xpos >> ypos >>zpos >>dirx>>dir << 89 dir= G4ThreeVector(dirx,diry,dirz); << 90 particleGun->GetCurrentSource()->GetEneDis << 91 << 92 particleGun->GetCurrentSource()->GetPartic << 93 particleGun->GetCurrentSource()->GetPartic << 94 particleGun->GetCurrentSource()->GetPartic << 95 particleGun->GetCurrentSource()->GetAngDis << 96 << 97 G4ParticleDefinition* particleDef = nullpt << 98 if (PDG > 1000000000) << 99 { << 100 int a=(PDG-1000000000)-(((PDG-10000000 << 101 if(a>0) << 102 { << 103 PDG=PDG-a; << 104 particleDef = G4IonTable::GetIonTable()- << 105 G4String Nome = particleDef->GetParticle << 106 } << 107 << 108 else << 109 { << 110 particleDef = G4IonTable::GetIonTable()- << 111 G4String Nome = particleDef->GetParticle << 112 } << 113 } << 114 << 115 else << 116 { << 117 particleDef = G4ParticleTable::GetPart << 118 } << 119 << 120 particleGun->GetCurrentSource()->SetPartic << 121 particleGun->GeneratePrimaryVertex(anEvent << 122 << 123 } << 124 << 125 in.close(); << 126 << 127 } 141 } 128 else << 142 >> 143 if ( sigmaZ > 0.0 ) 129 { 144 { 130 particleGun->GeneratePrimaryVertex(anE << 145 z += G4RandGauss::shoot( Z0, sigmaZ ); 131 } 146 } 132 << 147 >> 148 particleGun -> SetParticlePosition(G4ThreeVector( x , y , z ) ); >> 149 >> 150 // ******************************************** >> 151 // Set the beam energy and energy spread >> 152 // ******************************************** >> 153 >> 154 G4double kineticEnergy = G4RandGauss::shoot( meanKineticEnergy, sigmaEnergy ); >> 155 particleGun -> SetParticleEnergy ( kineticEnergy ); >> 156 >> 157 // Set the direction of the primary particles >> 158 G4double momentumX = 1.0; >> 159 G4double momentumY = 0.0; >> 160 G4double momentumZ = 0.0; >> 161 >> 162 if ( sigmaMomentumY > 0.0 ) >> 163 { >> 164 momentumY += G4RandGauss::shoot( 0., sigmaMomentumY ); >> 165 } >> 166 if ( sigmaMomentumZ > 0.0 ) >> 167 { >> 168 momentumZ += G4RandGauss::shoot( 0., sigmaMomentumZ ); >> 169 } >> 170 >> 171 particleGun -> SetParticleMomentumDirection( G4ThreeVector(momentumX,momentumY,momentumZ) ); >> 172 >> 173 // Generate a primary particle >> 174 particleGun -> GeneratePrimaryVertex( anEvent ); >> 175 } >> 176 >> 177 void HadrontherapyPrimaryGeneratorAction::SetmeanKineticEnergy (G4double val ) >> 178 { >> 179 meanKineticEnergy = val; >> 180 #ifdef ANALYSIS_USE >> 181 // Update the beam-data in the analysis manager >> 182 HadrontherapyAnalysisManager::getInstance()->setBeamMetaData(meanKineticEnergy, sigmaEnergy); >> 183 #endif >> 184 >> 185 } >> 186 >> 187 void HadrontherapyPrimaryGeneratorAction::SetsigmaEnergy (G4double val ) >> 188 { >> 189 sigmaEnergy = val; >> 190 #ifdef ANALYSIS_USE >> 191 // Update the sigmaenergy in the metadata. >> 192 HadrontherapyAnalysisManager::getInstance()->setBeamMetaData(meanKineticEnergy, sigmaEnergy); >> 193 #endif 133 } 194 } 134 195 >> 196 void HadrontherapyPrimaryGeneratorAction::SetXposition (G4double val ) >> 197 { X0 = val;} >> 198 >> 199 void HadrontherapyPrimaryGeneratorAction::SetYposition (G4double val ) >> 200 { Y0 = val;} >> 201 >> 202 void HadrontherapyPrimaryGeneratorAction::SetZposition (G4double val ) >> 203 { Z0 = val;} >> 204 >> 205 void HadrontherapyPrimaryGeneratorAction::SetsigmaY (G4double val ) >> 206 { sigmaY = val;} >> 207 >> 208 void HadrontherapyPrimaryGeneratorAction::SetsigmaZ (G4double val ) >> 209 { sigmaZ = val;} >> 210 >> 211 void HadrontherapyPrimaryGeneratorAction::SetsigmaMomentumY (G4double val ) >> 212 { sigmaMomentumY = val;} >> 213 >> 214 void HadrontherapyPrimaryGeneratorAction::SetsigmaMomentumZ (G4double val ) >> 215 { sigmaMomentumZ = val;} 135 216 >> 217 G4double HadrontherapyPrimaryGeneratorAction::GetmeanKineticEnergy(void) >> 218 { return meanKineticEnergy;} 136 219