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 // $Id: HadrontherapyInteractionParameters.cc; 27 // See more at: https://twiki.cern.ch/twiki/bi << 27 // See more at: http://g4advancedexamples.lngs.infn.it/Examples/hadrontherapy 28 << 29 #include <fstream> << 30 #include <iostream> << 31 #include <sstream> << 32 #include <cmath> << 33 #include <vector> << 34 28 35 #include "HadrontherapyInteractionParameters.h 29 #include "HadrontherapyInteractionParameters.hh" 36 #include "HadrontherapyParameterMessenger.hh" 30 #include "HadrontherapyParameterMessenger.hh" 37 #include "HadrontherapyDetectorConstruction.hh 31 #include "HadrontherapyDetectorConstruction.hh" 38 32 39 #include "globals.hh" << 40 #include "G4SystemOfUnits.hh" << 41 #include "G4UnitsTable.hh" 33 #include "G4UnitsTable.hh" 42 #include "G4UImanager.hh" 34 #include "G4UImanager.hh" 43 #include "G4RunManager.hh" 35 #include "G4RunManager.hh" 44 #include "G4LossTableManager.hh" 36 #include "G4LossTableManager.hh" 45 #include "G4Material.hh" 37 #include "G4Material.hh" 46 #include "G4MaterialCutsCouple.hh" 38 #include "G4MaterialCutsCouple.hh" 47 #include "G4ParticleDefinition.hh" 39 #include "G4ParticleDefinition.hh" 48 #include "G4ParticleTable.hh" 40 #include "G4ParticleTable.hh" 49 #include "G4NistManager.hh" 41 #include "G4NistManager.hh" 50 #include "G4Element.hh" 42 #include "G4Element.hh" >> 43 51 #include "G4StateManager.hh" 44 #include "G4StateManager.hh" 52 45 53 HadrontherapyInteractionParameters::Hadronther << 46 #include "globals.hh" 54 nistEle(new G4NistElementBuilder(0)), << 47 #include <fstream> 55 nistMat(new G4NistMaterialBuilder(nistEle, << 48 #include <iostream> 56 data(G4cout.rdbuf()), << 49 #include <sstream> 57 pMessenger(0), << 50 #include <math.h> 58 beamFlag(false) << 51 #include <unistd.h> 59 52 >> 53 #include <vector> >> 54 >> 55 >> 56 HadrontherapyInteractionParameters::HadrontherapyInteractionParameters(): >> 57 nistEle(new G4NistElementBuilder(0)), >> 58 nistMat(new G4NistMaterialBuilder(nistEle, 0)), >> 59 data(std::cout.rdbuf()), emCal(new G4EmCalculator), >> 60 pMessenger(new HadrontherapyParameterMessenger(this)), >> 61 beamFlag(false) 60 { 62 { 61 if (wantMessenger) pMessenger = new Hadron << 62 } 63 } 63 64 64 HadrontherapyInteractionParameters::~Hadronthe 65 HadrontherapyInteractionParameters::~HadrontherapyInteractionParameters() 65 { 66 { 66 if (pMessenger) delete pMessenger; << 67 delete pMessenger; >> 68 delete emCal; 67 delete nistMat; 69 delete nistMat; 68 delete nistEle; 70 delete nistEle; 69 } 71 } 70 << 71 G4double HadrontherapyInteractionParameters::G << 72 << 73 const G4Material* pMat, << 74 G4double dens) << 75 { << 76 if (dens) return ComputeTotalDEDX(ene, pDe << 77 return ComputeTotalDEDX(ene, pDef, pMat); << 78 } << 79 bool HadrontherapyInteractionParameters::GetSt 72 bool HadrontherapyInteractionParameters::GetStoppingTable(const G4String& vararg) 80 { 73 { 81 // Check arguments 74 // Check arguments 82 if ( !ParseArg(vararg)) return false; 75 if ( !ParseArg(vararg)) return false; 83 // Clear previous energy & mass sp vectors << 76 84 energy.clear(); << 77 std::vector<G4double> energy; 85 massDedx.clear(); << 78 std::vector<G4double> massDedx; >> 79 G4double dedxtot; >> 80 86 // log scale 81 // log scale 87 if (kinEmin != kinEmax && npoints >1) 82 if (kinEmin != kinEmax && npoints >1) 88 { 83 { 89 G4double logmin = std::log10(kinEmin); 84 G4double logmin = std::log10(kinEmin); 90 G4double logmax = std::log10(kinEmax); 85 G4double logmax = std::log10(kinEmax); 91 G4double en; 86 G4double en; 92 // uniform log space 87 // uniform log space 93 for (G4double c = 0.; c < npoints; 88 for (G4double c = 0.; c < npoints; c++) 94 { 89 { 95 en = std::pow(10., logmin + ( c*(logma 90 en = std::pow(10., logmin + ( c*(logmax-logmin) / (npoints - 1.)) ); 96 energy.push_back(en/MeV); << 91 energy.push_back(en); 97 dedxtot = ComputeTotalDEDX (en, parti << 92 dedxtot = emCal -> ComputeTotalDEDX (en, particle, material); 98 massDedx.push_back ( (dedxtot / << 93 massDedx.push_back ( dedxtot / density ); 99 } 94 } 100 } 95 } 101 else // one point only 96 else // one point only 102 { 97 { 103 energy.push_back(kinEmin/MeV); << 98 energy.push_back(kinEmin); 104 dedxtot = ComputeTotalDEDX (kinEmin, pa << 99 dedxtot = emCal -> ComputeTotalDEDX (kinEmin, particle, material); 105 massDedx.push_back ( (dedxtot / density) << 100 massDedx.push_back ( dedxtot / density ); 106 } 101 } 107 102 108 G4cout.precision(6); 103 G4cout.precision(6); 109 data << "MeV " << "MeV*cm2/g 104 data << "MeV " << "MeV*cm2/g " << particle << " (into " << 110 material << ", density = " << G4BestUnit << 105 material << ", density = " << G4BestUnit(density,"Volumic Mass") << ")" << G4endl; 111 data << G4endl; 106 data << G4endl; 112 data << std::left << std::setfill(' '); 107 data << std::left << std::setfill(' '); 113 for (size_t i=0; i<energy.size(); i++){ 108 for (size_t i=0; i<energy.size(); i++){ 114 data << std::setw(16) << energy[i] << mass << 109 data << std::setw(16) << energy[i]/MeV << massDedx[i]/(MeV*cm2/g) << G4endl; 115 } 110 } 116 outfile.close(); 111 outfile.close(); 117 // This will plot << 112 G4String ofName = (filename == "") ? "User terminal": filename; 118 << 119 // Info to user << 120 G4String ofName = (filename == "") ? G4Str << 121 G4cout << "User choice:\n"; 113 G4cout << "User choice:\n"; 122 G4cout << "Kinetic energy lower limit= "<< << 114 G4cout << "Kinetic energy lower limit= "<< G4BestUnit(kinEmin,"Energy") << ", Kinetic energy upper limit= " << G4BestUnit(kinEmax,"Energy") << 123 ", Kinetic energy upper limit= " << G4 << 115 ", npoints= "<< npoints << ", particle= \"" << particle << "\", material= \"" << material << 124 ", npoints= "<< npoints << ", parti << 116 "\", filename= \""<< ofName << "\"" << G4endl; 125 "\", material= \"" << material << "\", fi << 126 ofName << "\"" << G4endl; << 127 return true; 117 return true; 128 } 118 } >> 119 129 // Search for user material choice inside G4Ni 120 // Search for user material choice inside G4NistManager database 130 G4Material* HadrontherapyInteractionParameters << 121 G4Material* HadrontherapyInteractionParameters::GetNistMaterial(G4String material) 131 { 122 { 132 Pmaterial = G4NistManager::Instance()->Fin << 123 Pmaterial = G4NistManager::Instance()->FindOrBuildMaterial(material); 133 if (Pmaterial) density = Pmaterial -> GetD << 124 if (Pmaterial) >> 125 { >> 126 density = Pmaterial -> GetDensity(); >> 127 } 134 return Pmaterial; 128 return Pmaterial; 135 } 129 } 136 // Parse arguments line 130 // Parse arguments line 137 bool HadrontherapyInteractionParameters::Parse 131 bool HadrontherapyInteractionParameters::ParseArg(const G4String& vararg) 138 { 132 { 139 kinEmin = kinEmax = npoints = 0.; 133 kinEmin = kinEmax = npoints = 0.; 140 particle = material = filename = ""; 134 particle = material = filename = ""; 141 // set internal variables 135 // set internal variables 142 std::istringstream strParam(vararg); 136 std::istringstream strParam(vararg); 143 // TODO here check for number and parameters 137 // TODO here check for number and parameters consistency 144 strParam >> std::skipws >> material >> kinEm 138 strParam >> std::skipws >> material >> kinEmin >> kinEmax >> npoints >> particle >> filename; 145 // npoints must be an integer! 139 // npoints must be an integer! 146 npoints = std::floor(npoints); 140 npoints = std::floor(npoints); 147 141 148 // Check that kinEmax >= kinEmin > 0 && npoi 142 // Check that kinEmax >= kinEmin > 0 && npoints >= 1 149 // TODO NIST points and linear scale 143 // TODO NIST points and linear scale 150 if (kinEmax == 0. && kinEmin > 0. ) kinEmax 144 if (kinEmax == 0. && kinEmin > 0. ) kinEmax = kinEmin; 151 if (kinEmax == 0. && kinEmin == 0. ) kinEma 145 if (kinEmax == 0. && kinEmin == 0. ) kinEmax = kinEmin = 1.*MeV; 152 if (kinEmax < kinEmin) 146 if (kinEmax < kinEmin) 153 { 147 { 154 G4cout << "WARNING: kinEmin must not exce 148 G4cout << "WARNING: kinEmin must not exceed kinEmax!" << G4endl; 155 G4cout << "Usage: /parameter/command mat << 149 G4cout << "Usage: /parameter/command material kinetic Emin kinetic Emax nPoints [particle] [output filename]" << G4endl; 156 return false; 150 return false; 157 } 151 } 158 if (npoints < 1) npoints = 1; 152 if (npoints < 1) npoints = 1; 159 153 160 // check if element/material is into datab 154 // check if element/material is into database 161 if (!GetNistMaterial(material) ) 155 if (!GetNistMaterial(material) ) 162 { 156 { 163 G4cout << "WARNING: material \"" << mat 157 G4cout << "WARNING: material \"" << material << "\" doesn't exist in NIST elements/materials" 164 " table [$G4INSTALL/source/material 158 " table [$G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc]" << G4endl; 165 G4cout << "Use command \"/parameter/nis 159 G4cout << "Use command \"/parameter/nist\" to see full materials list" << G4endl; 166 return false; 160 return false; 167 } 161 } 168 // Check for particle 162 // Check for particle 169 if (particle == "") particle = "proton"; / 163 if (particle == "") particle = "proton"; // default to "proton" 170 else if ( !FindParticle(particle) ) << 164 else if ( !emCal->FindParticle(particle) ) 171 { 165 { 172 G4cout << "WARNING: Particle \"" << partic << 166 G4cout << "WARNING: Particle \"" << particle << "\" isn't supported" << G4endl; 173 G4cout << "Try the command \"/particle/lis << 167 G4cout << "Try the command \"/particle/list\" to get full supported particles list" << G4endl; 174 G4cout << "If you are interested in an ion 168 G4cout << "If you are interested in an ion that isn't in this list you must give it to the particle gun." 175 "\nTry the commands:\n/gun/parti << 169 "\nTry the commands \n/gun/particle ion" 176 "\n/gun/ion <atomic number> <mass numb << 170 "\n/gun/ion <atomic number> <mass number> <[charge]>" << G4endl; 177 return false; 171 return false; 178 } 172 } 179 // start physics by forcing a G4RunManager << 173 // start physics by forcing a G4RunManager beamOn(): 180 BeamOn(); 174 BeamOn(); 181 // Set output file 175 // Set output file 182 if( filename != "" ) 176 if( filename != "" ) 183 { 177 { 184 outfile.open(filename,std::ios_base: 178 outfile.open(filename,std::ios_base::trunc); // overwrite existing file 185 data.rdbuf(outfile.rdbuf()); 179 data.rdbuf(outfile.rdbuf()); 186 } 180 } 187 else data.rdbuf(G4cout.rdbuf()); // outpu << 181 else data.rdbuf(std::cout.rdbuf()); // output is G4cout 188 return true; 182 return true; 189 } 183 } 190 // Force physics tables build 184 // Force physics tables build 191 void HadrontherapyInteractionParameters::BeamO 185 void HadrontherapyInteractionParameters::BeamOn() 192 { 186 { 193 // first check if RunManager is above G4St 187 // first check if RunManager is above G4State_Idle 194 G4StateManager* mState = G4StateManager::G 188 G4StateManager* mState = G4StateManager::GetStateManager(); 195 G4ApplicationState aState = mState -> Get 189 G4ApplicationState aState = mState -> GetCurrentState(); 196 if ( aState <= G4State_Idle && beamFlag == 190 if ( aState <= G4State_Idle && beamFlag == false) 197 { 191 { 198 G4cout << "Issuing a G4RunManager::beamO << 192 // G4cout << "Run State " << mState -> GetStateString( aState ) << G4endl; 199 G4cout << "Current Run State is " << mSt << 200 G4RunManager::GetRunManager() -> BeamOn( 193 G4RunManager::GetRunManager() -> BeamOn(0); 201 beamFlag = true; 194 beamFlag = true; 202 } 195 } 203 196 204 } 197 } 205 // print a list of Nist elements and materials 198 // print a list of Nist elements and materials 206 void HadrontherapyInteractionParameters::ListO 199 void HadrontherapyInteractionParameters::ListOfNistMaterials(const G4String& vararg) 207 { 200 { 208 /* 201 /* 209 $G4INSTALL/source/materials/src/G4NistElement 202 $G4INSTALL/source/materials/src/G4NistElementBuilder.cc 210 You can also construct a new material by the 203 You can also construct a new material by the ConstructNewMaterial method: 211 see $G4INSTALL/source/materials/src/G4NistMat 204 see $G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc 212 */ 205 */ 213 // Get simplest full list 206 // Get simplest full list 214 if (vararg =="list") 207 if (vararg =="list") 215 { 208 { 216 const std::vector<G4String>& vec = nist 209 const std::vector<G4String>& vec = nistMat -> GetMaterialNames(); 217 for (size_t i=0; i<vec.size(); i++) 210 for (size_t i=0; i<vec.size(); i++) 218 { 211 { 219 G4cout << std::setw(12) << std::left 212 G4cout << std::setw(12) << std::left << i+1 << vec[i] << G4endl; 220 } 213 } 221 G4cout << G4endl; 214 G4cout << G4endl; 222 } 215 } 223 else if (vararg =="all" || vararg =="simpl 216 else if (vararg =="all" || vararg =="simple" || vararg =="compound" || vararg =="hep" ) 224 { 217 { 225 nistMat -> ListMaterials(vararg); 218 nistMat -> ListMaterials(vararg); 226 } 219 } 227 } 220 } 228 221 229 222 230 223