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 // Visit the Hadrontherapy web site (http://www.lns.infn.it/link/Hadrontherapy) to request >> 26 // the *COMPLETE* version of this program, together with its documentation; >> 27 // Hadrontherapy (both basic and full version) are supported by the Italian INFN >> 28 // Institute in the framework of the MC-INFN Group 25 // 29 // 26 // Hadrontherapy advanced example for Geant4 << 27 // See more at: https://twiki.cern.ch/twiki/bi << 28 30 29 #include <fstream> 31 #include <fstream> 30 #include <iostream> 32 #include <iostream> 31 #include <sstream> 33 #include <sstream> 32 #include <cmath> 34 #include <cmath> 33 #include <vector> 35 #include <vector> 34 36 35 #include "HadrontherapyInteractionParameters.h 37 #include "HadrontherapyInteractionParameters.hh" 36 #include "HadrontherapyParameterMessenger.hh" 38 #include "HadrontherapyParameterMessenger.hh" 37 #include "HadrontherapyDetectorConstruction.hh 39 #include "HadrontherapyDetectorConstruction.hh" 38 40 39 #include "globals.hh" 41 #include "globals.hh" 40 #include "G4SystemOfUnits.hh" 42 #include "G4SystemOfUnits.hh" 41 #include "G4UnitsTable.hh" 43 #include "G4UnitsTable.hh" 42 #include "G4UImanager.hh" 44 #include "G4UImanager.hh" 43 #include "G4RunManager.hh" 45 #include "G4RunManager.hh" 44 #include "G4LossTableManager.hh" 46 #include "G4LossTableManager.hh" 45 #include "G4Material.hh" 47 #include "G4Material.hh" 46 #include "G4MaterialCutsCouple.hh" 48 #include "G4MaterialCutsCouple.hh" 47 #include "G4ParticleDefinition.hh" 49 #include "G4ParticleDefinition.hh" 48 #include "G4ParticleTable.hh" 50 #include "G4ParticleTable.hh" 49 #include "G4NistManager.hh" 51 #include "G4NistManager.hh" 50 #include "G4Element.hh" 52 #include "G4Element.hh" 51 #include "G4StateManager.hh" 53 #include "G4StateManager.hh" 52 54 53 HadrontherapyInteractionParameters::Hadronther 55 HadrontherapyInteractionParameters::HadrontherapyInteractionParameters(G4bool wantMessenger): 54 nistEle(new G4NistElementBuilder(0)), 56 nistEle(new G4NistElementBuilder(0)), 55 nistMat(new G4NistMaterialBuilder(nistEle, 57 nistMat(new G4NistMaterialBuilder(nistEle, 0)), 56 data(G4cout.rdbuf()), 58 data(G4cout.rdbuf()), 57 pMessenger(0), 59 pMessenger(0), 58 beamFlag(false) 60 beamFlag(false) 59 << 61 #ifdef G4ANALYSIS_USE_ROOT >> 62 ,theRootCanvas(0), >> 63 theRootGraph(0) >> 64 #endif 60 { 65 { 61 if (wantMessenger) pMessenger = new Hadron 66 if (wantMessenger) pMessenger = new HadrontherapyParameterMessenger(this); 62 } 67 } 63 68 64 HadrontherapyInteractionParameters::~Hadronthe 69 HadrontherapyInteractionParameters::~HadrontherapyInteractionParameters() 65 { 70 { 66 if (pMessenger) delete pMessenger; 71 if (pMessenger) delete pMessenger; 67 delete nistMat; 72 delete nistMat; 68 delete nistEle; 73 delete nistEle; 69 } 74 } 70 75 71 G4double HadrontherapyInteractionParameters::G 76 G4double HadrontherapyInteractionParameters::GetStopping (G4double ene, 72 77 const G4ParticleDefinition* pDef, 73 const G4Material* pMat, 78 const G4Material* pMat, 74 G4double dens) 79 G4double dens) 75 { 80 { 76 if (dens) return ComputeTotalDEDX(ene, pDe 81 if (dens) return ComputeTotalDEDX(ene, pDef, pMat)/dens; 77 return ComputeTotalDEDX(ene, pDef, pMat); 82 return ComputeTotalDEDX(ene, pDef, pMat); 78 } 83 } 79 bool HadrontherapyInteractionParameters::GetSt 84 bool HadrontherapyInteractionParameters::GetStoppingTable(const G4String& vararg) 80 { 85 { 81 // Check arguments 86 // Check arguments 82 if ( !ParseArg(vararg)) return false; 87 if ( !ParseArg(vararg)) return false; 83 // Clear previous energy & mass sp vectors 88 // Clear previous energy & mass sp vectors 84 energy.clear(); 89 energy.clear(); 85 massDedx.clear(); 90 massDedx.clear(); 86 // log scale 91 // log scale 87 if (kinEmin != kinEmax && npoints >1) 92 if (kinEmin != kinEmax && npoints >1) 88 { 93 { 89 G4double logmin = std::log10(kinEmin); 94 G4double logmin = std::log10(kinEmin); 90 G4double logmax = std::log10(kinEmax); 95 G4double logmax = std::log10(kinEmax); 91 G4double en; 96 G4double en; 92 // uniform log space 97 // uniform log space 93 for (G4double c = 0.; c < npoints; 98 for (G4double c = 0.; c < npoints; c++) 94 { 99 { 95 en = std::pow(10., logmin + ( c*(logma 100 en = std::pow(10., logmin + ( c*(logmax-logmin) / (npoints - 1.)) ); 96 energy.push_back(en/MeV); 101 energy.push_back(en/MeV); 97 dedxtot = ComputeTotalDEDX (en, parti 102 dedxtot = ComputeTotalDEDX (en, particle, material); 98 massDedx.push_back ( (dedxtot / 103 massDedx.push_back ( (dedxtot / density)/(MeV*cm2/g) ); 99 } 104 } 100 } 105 } 101 else // one point only 106 else // one point only 102 { 107 { 103 energy.push_back(kinEmin/MeV); 108 energy.push_back(kinEmin/MeV); 104 dedxtot = ComputeTotalDEDX (kinEmin, pa 109 dedxtot = ComputeTotalDEDX (kinEmin, particle, material); 105 massDedx.push_back ( (dedxtot / density) 110 massDedx.push_back ( (dedxtot / density)/(MeV*cm2/g) ); 106 } 111 } 107 112 108 G4cout.precision(6); 113 G4cout.precision(6); 109 data << "MeV " << "MeV*cm2/g 114 data << "MeV " << "MeV*cm2/g " << particle << " (into " << 110 material << ", density = " << G4BestUnit 115 material << ", density = " << G4BestUnit(density,"Volumic Mass") << ")" << G4endl; 111 data << G4endl; 116 data << G4endl; 112 data << std::left << std::setfill(' '); 117 data << std::left << std::setfill(' '); 113 for (size_t i=0; i<energy.size(); i++){ 118 for (size_t i=0; i<energy.size(); i++){ 114 data << std::setw(16) << energy[i] << mass 119 data << std::setw(16) << energy[i] << massDedx[i] << G4endl; 115 } 120 } 116 outfile.close(); 121 outfile.close(); 117 // This will plot << 122 // This will plot >> 123 #ifdef G4ANALYSIS_USE_ROOT >> 124 PlotStopping("pdf"); >> 125 #endif 118 126 119 // Info to user 127 // Info to user 120 G4String ofName = (filename == "") ? G4Str << 128 G4String ofName = (filename == "") ? "User terminal": filename; 121 G4cout << "User choice:\n"; 129 G4cout << "User choice:\n"; 122 G4cout << "Kinetic energy lower limit= "<< 130 G4cout << "Kinetic energy lower limit= "<< G4BestUnit(kinEmin,"Energy") << 123 ", Kinetic energy upper limit= " << G4 131 ", Kinetic energy upper limit= " << G4BestUnit(kinEmax,"Energy") << 124 ", npoints= "<< npoints << ", parti 132 ", npoints= "<< npoints << ", particle= \"" << particle << 125 "\", material= \"" << material << "\", fi 133 "\", material= \"" << material << "\", filename= \""<< 126 ofName << "\"" << G4endl; 134 ofName << "\"" << G4endl; 127 return true; 135 return true; 128 } 136 } >> 137 /////////////////////////////////////////////////////////////////////////////////// >> 138 // Save Plot >> 139 #ifdef G4ANALYSIS_USE_ROOT >> 140 void HadrontherapyInteractionParameters::PlotStopping(const G4String& filetype) >> 141 { >> 142 if (!theRootCanvas) >> 143 { >> 144 gROOT->Reset(); >> 145 gROOT->SetStyle("Plain"); >> 146 theRootCanvas = new TCanvas("theRootCanvas","Interaction Parameters",200, 10, 600,400); >> 147 theRootCanvas -> SetFillColor(20); >> 148 theRootCanvas -> SetBorderMode(1); >> 149 theRootCanvas -> SetBorderSize(1); >> 150 theRootCanvas -> SetFrameBorderMode(0); >> 151 theRootCanvas -> SetGrid(); >> 152 // Use global pad: root manual pgg 109,... >> 153 } >> 154 >> 155 if (theRootGraph) delete theRootGraph; >> 156 theRootGraph = new TGraph(energy.size(), &energy[0], &massDedx[0]); >> 157 //theRootGraph = new TGraph(); >> 158 axisX = theRootGraph -> GetXaxis(), >> 159 axisY = theRootGraph -> GetYaxis(); >> 160 axisX -> SetTitle("MeV"); >> 161 axisY -> SetTitle("Stopping Power (MeV cm2/g)"); >> 162 //axisX -> SetNdivisions(500,kTRUE); >> 163 //axisX -> SetTickLength(0.03); >> 164 //axisX -> SetLabelOffset(2.005); >> 165 axisX -> SetAxisColor(2); >> 166 axisY -> SetAxisColor(2); >> 167 gPad -> SetLogx(1); >> 168 gPad -> SetLogy(1); >> 169 theRootGraph -> SetMarkerColor(4); >> 170 theRootGraph -> SetMarkerStyle(20);// circle >> 171 theRootGraph -> SetMarkerSize(.5); >> 172 >> 173 G4String gName = particle.substr(0, particle.find("[") ); // cut excitation energy >> 174 gName = gName + "_" + material; >> 175 G4String fName = "./referenceData/interaction/" + gName + "." + filetype; >> 176 theRootGraph -> SetTitle(gName); >> 177 theRootGraph -> Draw("AP"); >> 178 //theRootCanvas -> Update(); >> 179 //theRootCanvas -> Draw(); >> 180 theRootCanvas -> SaveAs(fName); >> 181 } >> 182 #endif >> 183 129 // Search for user material choice inside G4Ni 184 // Search for user material choice inside G4NistManager database 130 G4Material* HadrontherapyInteractionParameters 185 G4Material* HadrontherapyInteractionParameters::GetNistMaterial(G4String mat) 131 { 186 { 132 Pmaterial = G4NistManager::Instance()->Fin 187 Pmaterial = G4NistManager::Instance()->FindOrBuildMaterial(mat); 133 if (Pmaterial) density = Pmaterial -> GetD 188 if (Pmaterial) density = Pmaterial -> GetDensity(); 134 return Pmaterial; 189 return Pmaterial; 135 } 190 } 136 // Parse arguments line 191 // Parse arguments line 137 bool HadrontherapyInteractionParameters::Parse 192 bool HadrontherapyInteractionParameters::ParseArg(const G4String& vararg) 138 { 193 { 139 kinEmin = kinEmax = npoints = 0.; 194 kinEmin = kinEmax = npoints = 0.; 140 particle = material = filename = ""; 195 particle = material = filename = ""; 141 // set internal variables 196 // set internal variables 142 std::istringstream strParam(vararg); 197 std::istringstream strParam(vararg); 143 // TODO here check for number and parameters 198 // TODO here check for number and parameters consistency 144 strParam >> std::skipws >> material >> kinEm 199 strParam >> std::skipws >> material >> kinEmin >> kinEmax >> npoints >> particle >> filename; 145 // npoints must be an integer! 200 // npoints must be an integer! 146 npoints = std::floor(npoints); 201 npoints = std::floor(npoints); 147 202 148 // Check that kinEmax >= kinEmin > 0 && npoi 203 // Check that kinEmax >= kinEmin > 0 && npoints >= 1 149 // TODO NIST points and linear scale 204 // TODO NIST points and linear scale 150 if (kinEmax == 0. && kinEmin > 0. ) kinEmax 205 if (kinEmax == 0. && kinEmin > 0. ) kinEmax = kinEmin; 151 if (kinEmax == 0. && kinEmin == 0. ) kinEma 206 if (kinEmax == 0. && kinEmin == 0. ) kinEmax = kinEmin = 1.*MeV; 152 if (kinEmax < kinEmin) 207 if (kinEmax < kinEmin) 153 { 208 { 154 G4cout << "WARNING: kinEmin must not exce 209 G4cout << "WARNING: kinEmin must not exceed kinEmax!" << G4endl; 155 G4cout << "Usage: /parameter/command mat << 210 G4cout << "Usage: /parameter/command material kinetic Emin kinetic Emax nPoints [particle] [output filename]" << G4endl; 156 return false; 211 return false; 157 } 212 } 158 if (npoints < 1) npoints = 1; 213 if (npoints < 1) npoints = 1; 159 214 160 // check if element/material is into datab 215 // check if element/material is into database 161 if (!GetNistMaterial(material) ) 216 if (!GetNistMaterial(material) ) 162 { 217 { 163 G4cout << "WARNING: material \"" << mat 218 G4cout << "WARNING: material \"" << material << "\" doesn't exist in NIST elements/materials" 164 " table [$G4INSTALL/source/material 219 " table [$G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc]" << G4endl; 165 G4cout << "Use command \"/parameter/nis 220 G4cout << "Use command \"/parameter/nist\" to see full materials list" << G4endl; 166 return false; 221 return false; 167 } 222 } 168 // Check for particle 223 // Check for particle 169 if (particle == "") particle = "proton"; / 224 if (particle == "") particle = "proton"; // default to "proton" 170 else if ( !FindParticle(particle) ) 225 else if ( !FindParticle(particle) ) 171 { 226 { 172 G4cout << "WARNING: Particle \"" << partic 227 G4cout << "WARNING: Particle \"" << particle << "\" isn't supported." << G4endl; 173 G4cout << "Try the command \"/particle/lis 228 G4cout << "Try the command \"/particle/list\" to get full supported particles list." << G4endl; 174 G4cout << "If you are interested in an ion 229 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 230 "\nTry the commands:\n/gun/particle ion" 176 "\n/gun/ion <atomic number> <mass numb 231 "\n/gun/ion <atomic number> <mass number> <[charge]>" << G4endl << G4endl; 177 return false; 232 return false; 178 } 233 } 179 // start physics by forcing a G4RunManager 234 // start physics by forcing a G4RunManager::BeamOn(): 180 BeamOn(); 235 BeamOn(); 181 // Set output file 236 // Set output file 182 if( filename != "" ) 237 if( filename != "" ) 183 { 238 { 184 outfile.open(filename,std::ios_base: 239 outfile.open(filename,std::ios_base::trunc); // overwrite existing file 185 data.rdbuf(outfile.rdbuf()); 240 data.rdbuf(outfile.rdbuf()); 186 } 241 } 187 else data.rdbuf(G4cout.rdbuf()); // outpu 242 else data.rdbuf(G4cout.rdbuf()); // output is G4cout! 188 return true; 243 return true; 189 } 244 } 190 // Force physics tables build 245 // Force physics tables build 191 void HadrontherapyInteractionParameters::BeamO 246 void HadrontherapyInteractionParameters::BeamOn() 192 { 247 { 193 // first check if RunManager is above G4St 248 // first check if RunManager is above G4State_Idle 194 G4StateManager* mState = G4StateManager::G 249 G4StateManager* mState = G4StateManager::GetStateManager(); 195 G4ApplicationState aState = mState -> Get 250 G4ApplicationState aState = mState -> GetCurrentState(); 196 if ( aState <= G4State_Idle && beamFlag == 251 if ( aState <= G4State_Idle && beamFlag == false) 197 { 252 { 198 G4cout << "Issuing a G4RunManager::beamO 253 G4cout << "Issuing a G4RunManager::beamOn()... "; 199 G4cout << "Current Run State is " << mSt 254 G4cout << "Current Run State is " << mState -> GetStateString( aState ) << G4endl; 200 G4RunManager::GetRunManager() -> BeamOn( 255 G4RunManager::GetRunManager() -> BeamOn(0); 201 beamFlag = true; 256 beamFlag = true; 202 } 257 } 203 258 204 } 259 } 205 // print a list of Nist elements and materials 260 // print a list of Nist elements and materials 206 void HadrontherapyInteractionParameters::ListO 261 void HadrontherapyInteractionParameters::ListOfNistMaterials(const G4String& vararg) 207 { 262 { 208 /* 263 /* 209 $G4INSTALL/source/materials/src/G4NistElement 264 $G4INSTALL/source/materials/src/G4NistElementBuilder.cc 210 You can also construct a new material by the 265 You can also construct a new material by the ConstructNewMaterial method: 211 see $G4INSTALL/source/materials/src/G4NistMat 266 see $G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc 212 */ 267 */ 213 // Get simplest full list 268 // Get simplest full list 214 if (vararg =="list") 269 if (vararg =="list") 215 { 270 { 216 const std::vector<G4String>& vec = nist 271 const std::vector<G4String>& vec = nistMat -> GetMaterialNames(); 217 for (size_t i=0; i<vec.size(); i++) 272 for (size_t i=0; i<vec.size(); i++) 218 { 273 { 219 G4cout << std::setw(12) << std::left 274 G4cout << std::setw(12) << std::left << i+1 << vec[i] << G4endl; 220 } 275 } 221 G4cout << G4endl; 276 G4cout << G4endl; 222 } 277 } 223 else if (vararg =="all" || vararg =="simpl 278 else if (vararg =="all" || vararg =="simple" || vararg =="compound" || vararg =="hep" ) 224 { 279 { 225 nistMat -> ListMaterials(vararg); 280 nistMat -> ListMaterials(vararg); 226 } 281 } 227 } 282 } 228 283 229 284 230 285