Geant4  10.02.p01
HadrontherapyInteractionParameters.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // Hadrontherapy advanced example for Geant4
27 // See more at: https://twiki.cern.ch/twiki/bin/view/Geant4/AdvancedExamplesHadrontherapy
28 
29 #include <fstream>
30 #include <iostream>
31 #include <sstream>
32 #include <cmath>
33 #include <vector>
34 
38 
39 #include "globals.hh"
40 #include "G4SystemOfUnits.hh"
41 #include "G4UnitsTable.hh"
42 #include "G4UImanager.hh"
43 #include "G4RunManager.hh"
44 #include "G4LossTableManager.hh"
45 #include "G4Material.hh"
46 #include "G4MaterialCutsCouple.hh"
47 #include "G4ParticleDefinition.hh"
48 #include "G4ParticleTable.hh"
49 #include "G4NistManager.hh"
50 #include "G4Element.hh"
51 #include "G4StateManager.hh"
52 
54  nistEle(new G4NistElementBuilder(0)),
55  nistMat(new G4NistMaterialBuilder(nistEle, 0)),
56  data(G4cout.rdbuf()),
57  pMessenger(0),
58  beamFlag(false)
59 #ifdef G4ANALYSIS_USE_ROOT
60  ,theRootCanvas(0),
61  theRootGraph(0)
62 #endif
63 {
64  if (wantMessenger) pMessenger = new HadrontherapyParameterMessenger(this);
65 }
66 
68 {
69  if (pMessenger) delete pMessenger;
70  delete nistMat;
71  delete nistEle;
72 }
73 
75  const G4ParticleDefinition* pDef,
76  const G4Material* pMat,
77  G4double dens)
78 {
79  if (dens) return ComputeTotalDEDX(ene, pDef, pMat)/dens;
80  return ComputeTotalDEDX(ene, pDef, pMat);
81 }
83 {
84  // Check arguments
85  if ( !ParseArg(vararg)) return false;
86  // Clear previous energy & mass sp vectors
87  energy.clear();
88  massDedx.clear();
89  // log scale
90  if (kinEmin != kinEmax && npoints >1)
91  {
92  G4double logmin = std::log10(kinEmin);
93  G4double logmax = std::log10(kinEmax);
94  G4double en;
95  // uniform log space
96  for (G4double c = 0.; c < npoints; c++)
97  {
98  en = std::pow(10., logmin + ( c*(logmax-logmin) / (npoints - 1.)) );
99  energy.push_back(en/MeV);
101  massDedx.push_back ( (dedxtot / density)/(MeV*cm2/g) );
102  }
103  }
104  else // one point only
105  {
106  energy.push_back(kinEmin/MeV);
108  massDedx.push_back ( (dedxtot / density)/(MeV*cm2/g) );
109  }
110 
111  G4cout.precision(6);
112  data << "MeV " << "MeV*cm2/g " << particle << " (into " <<
113  material << ", density = " << G4BestUnit(density,"Volumic Mass") << ")" << G4endl;
114  data << G4endl;
115  data << std::left << std::setfill(' ');
116  for (size_t i=0; i<energy.size(); i++){
117  data << std::setw(16) << energy[i] << massDedx[i] << G4endl;
118  }
119  outfile.close();
120  // This will plot
121 #ifdef G4ANALYSIS_USE_ROOT
122  PlotStopping("pdf");
123 #endif
124 
125 // Info to user
126  G4String ofName = (filename == "") ? "User terminal": filename;
127  G4cout << "User choice:\n";
128  G4cout << "Kinetic energy lower limit= "<< G4BestUnit(kinEmin,"Energy") <<
129  ", Kinetic energy upper limit= " << G4BestUnit(kinEmax,"Energy") <<
130  ", npoints= "<< npoints << ", particle= \"" << particle <<
131  "\", material= \"" << material << "\", filename= \""<<
132  ofName << "\"" << G4endl;
133  return true;
134 }
136 // Save Plot
137 #ifdef G4ANALYSIS_USE_ROOT
138 void HadrontherapyInteractionParameters::PlotStopping(const G4String& filetype)
139 {
140  if (!theRootCanvas)
141  {
142  gROOT->Reset();
143  gROOT->SetStyle("Plain");
144  theRootCanvas = new TCanvas("theRootCanvas","Interaction Parameters",200, 10, 600,400);
145  theRootCanvas -> SetFillColor(20);
146  theRootCanvas -> SetBorderMode(1);
147  theRootCanvas -> SetBorderSize(1);
148  theRootCanvas -> SetFrameBorderMode(0);
149  theRootCanvas -> SetGrid();
150  // Use global pad: root manual pgg 109,...
151  }
152 
153  if (theRootGraph) delete theRootGraph;
154  theRootGraph = new TGraph(energy.size(), &energy[0], &massDedx[0]);
155  //theRootGraph = new TGraph();
156  axisX = theRootGraph -> GetXaxis(),
157  axisY = theRootGraph -> GetYaxis();
158  axisX -> SetTitle("MeV");
159  axisY -> SetTitle("Stopping Power (MeV cm2/g)");
160  //axisX -> SetNdivisions(500,kTRUE);
161  //axisX -> SetTickLength(0.03);
162  //axisX -> SetLabelOffset(2.005);
163  axisX -> SetAxisColor(2);
164  axisY -> SetAxisColor(2);
165  gPad -> SetLogx(1);
166  gPad -> SetLogy(1);
167  theRootGraph -> SetMarkerColor(4);
168  theRootGraph -> SetMarkerStyle(20);// circle
169  theRootGraph -> SetMarkerSize(.5);
170 
171  G4String gName = particle.substr(0, particle.find("[") ); // cut excitation energy
172  gName = gName + "_" + material;
173  G4String fName = "./referenceData/interaction/" + gName + "." + filetype;
174  theRootGraph -> SetTitle(gName);
175  theRootGraph -> Draw("AP");
176  //theRootCanvas -> Update();
177  //theRootCanvas -> Draw();
178  theRootCanvas -> SaveAs(fName);
179 }
180 #endif
181 
182 // Search for user material choice inside G4NistManager database
184 {
186  if (Pmaterial) density = Pmaterial -> GetDensity();
187  return Pmaterial;
188 }
189 // Parse arguments line
191 {
192  kinEmin = kinEmax = npoints = 0.;
193  particle = material = filename = "";
194  // set internal variables
195  std::istringstream strParam(vararg);
196  // TODO here check for number and parameters consistency
197  strParam >> std::skipws >> material >> kinEmin >> kinEmax >> npoints >> particle >> filename;
198  // npoints must be an integer!
199  npoints = std::floor(npoints);
200 
201 // Check that kinEmax >= kinEmin > 0 && npoints >= 1
202 // TODO NIST points and linear scale
203  if (kinEmax == 0. && kinEmin > 0. ) kinEmax = kinEmin;
204  if (kinEmax == 0. && kinEmin == 0. ) kinEmax = kinEmin = 1.*MeV;
205  if (kinEmax < kinEmin)
206  {
207  G4cout << "WARNING: kinEmin must not exceed kinEmax!" << G4endl;
208  G4cout << "Usage: /parameter/command material EkinMin EKinMax nPoints [particle] [output filename]" << G4endl;
209  return false;
210  }
211  if (npoints < 1) npoints = 1;
212 
213  // check if element/material is into database
214  if (!GetNistMaterial(material) )
215  {
216  G4cout << "WARNING: material \"" << material << "\" doesn't exist in NIST elements/materials"
217  " table [$G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc]" << G4endl;
218  G4cout << "Use command \"/parameter/nist\" to see full materials list" << G4endl;
219  return false;
220  }
221  // Check for particle
222  if (particle == "") particle = "proton"; // default to "proton"
223  else if ( !FindParticle(particle) )
224  {
225  G4cout << "WARNING: Particle \"" << particle << "\" isn't supported." << G4endl;
226  G4cout << "Try the command \"/particle/list\" to get full supported particles list." << G4endl;
227  G4cout << "If you are interested in an ion that isn't in this list you must give it to the particle gun."
228  "\nTry the commands:\n/gun/particle ion"
229  "\n/gun/ion <atomic number> <mass number> <[charge]>" << G4endl << G4endl;
230  return false;
231  }
232  // start physics by forcing a G4RunManager::BeamOn():
233  BeamOn();
234  // Set output file
235  if( filename != "" )
236  {
237  outfile.open(filename,std::ios_base::trunc); // overwrite existing file
238  data.rdbuf(outfile.rdbuf());
239  }
240  else data.rdbuf(G4cout.rdbuf()); // output is G4cout!
241  return true;
242 }
243 // Force physics tables build
245 {
246  // first check if RunManager is above G4State_Idle
248  G4ApplicationState aState = mState -> GetCurrentState();
249  if ( aState <= G4State_Idle && beamFlag == false)
250  {
251  G4cout << "Issuing a G4RunManager::beamOn()... ";
252  G4cout << "Current Run State is " << mState -> GetStateString( aState ) << G4endl;
254  beamFlag = true;
255  }
256 
257 }
258 // print a list of Nist elements and materials
260 {
261 /*
262  $G4INSTALL/source/materials/src/G4NistElementBuilder.cc
263  You can also construct a new material by the ConstructNewMaterial method:
264  see $G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc
265 */
266  // Get simplest full list
267  if (vararg =="list")
268  {
269  const std::vector<G4String>& vec = nistMat -> GetMaterialNames();
270  for (size_t i=0; i<vec.size(); i++)
271  {
272  G4cout << std::setw(12) << std::left << i+1 << vec[i] << G4endl;
273  }
274  G4cout << G4endl;
275  }
276  else if (vararg =="all" || vararg =="simple" || vararg =="compound" || vararg =="hep" )
277  {
278  nistMat -> ListMaterials(vararg);
279  }
280 }
281 
282 
G4Material * FindOrBuildMaterial(const G4String &name, G4bool isotopes=true, G4bool warning=false)
static const double MeV
Definition: G4SIunits.hh:211
static const double cm2
Definition: G4SIunits.hh:119
G4String fName
Definition: G4AttUtils.hh:55
G4double ComputeTotalDEDX(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, G4double cut=DBL_MAX)
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
static G4NistManager * Instance()
G4double GetStopping(G4double energy, const G4ParticleDefinition *, const G4Material *, G4double density=0.)
static G4StateManager * GetStateManager()
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
G4bool SetTitle(G4ToolsBaseHisto &baseHisto, const G4String &title)
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
const G4ParticleDefinition * FindParticle(const G4String &)
static const double g
Definition: G4SIunits.hh:180
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4ApplicationState