Geant4_10
HadrontherapyMatrix.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 // This is the *BASIC* version of Hadrontherapy, a Geant4-based application
27 // See more at: http://g4advancedexamples.lngs.infn.it/Examples/hadrontherapy
28 //
29 // Visit the Hadrontherapy web site (http://www.lns.infn.it/link/Hadrontherapy) to request
30 // the *COMPLETE* version of this program, together with its documentation;
31 // Hadrontherapy (both basic and full version) are supported by the Italian INFN
32 // Institute in the framework of the MC-INFN Group
33 //
34 
35 #include <fstream>
36 #include <iostream>
37 #include <sstream>
38 #include <iomanip>
39 
40 #include "HadrontherapyMatrix.hh"
43 #include "globals.hh"
44 #include "G4SystemOfUnits.hh"
45 #include "G4RunManager.hh"
46 #include "G4ParticleGun.hh"
47 
48 // Units definition: CLHEP/Units/SystemOfUnits.h
49 //
50 HadrontherapyMatrix* HadrontherapyMatrix::instance = NULL;
52 
53 // Only return a pointer to matrix
55 {
56  return instance;
57 }
58  // This STATIC method delete (!) the old matrix and rewrite a new object returning a pointer to it
59  // TODO A check on the parameters is required!
61 {
62  if (instance) delete instance;
63  instance = new HadrontherapyMatrix(voxelX, voxelY, voxelZ, mass);
64  instance -> Initialize();
65  return instance;
66 }
67 HadrontherapyMatrix::HadrontherapyMatrix(G4int voxelX, G4int voxelY, G4int voxelZ, G4double mass):
68  stdFile("Dose.out"),
69  doseUnit(gray)
70 {
71  // Number of the voxels of the phantom
72  // For Y = Z = 1 the phantom is divided in slices (and not in voxels)
73  // orthogonal to the beam axis
74  numberOfVoxelAlongX = voxelX;
75  numberOfVoxelAlongY = voxelY;
76  numberOfVoxelAlongZ = voxelZ;
77  massOfVoxel = mass;
78  // Create the dose matrix
79  matrix = new G4double[numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ];
80  if (matrix)
81  {
82  G4cout << "HadrontherapyMatrix: Memory space to store physical dose into " <<
83  numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ <<
84  " voxels has been allocated " << G4endl;
85  }
86  else G4Exception("HadrontherapyMatrix::HadrontherapyMatrix()", "Hadrontherapy0005", FatalException, "Can't allocate memory to store physical dose!");
87  // Hit voxel (TrackID) marker
88  // This array mark the status of voxel, if a hit occur, with the trackID of the particle
89  // Must be initialized
90  hitTrack = new G4int[numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ];
91  ClearHitTrack();
92 }
93 
96 {
97  delete[] matrix;
98  delete[] hitTrack;
99  // free fluences/dose data memory
100  Clear();
101 }
102 
105 {
106  for (size_t i=0; i<ionStore.size(); i++)
107  {
108  delete[] ionStore[i].dose;
109  delete[] ionStore[i].fluence;
110  }
111  ionStore.clear();
112 }
113 
115 // Initialise the elements of the matrix to zero
117 {
118  // Clear ions store
119  Clear();
120  // Clear dose
121  for(int i=0;i<numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ;i++)
122  {
123  matrix[i] = 0;
124  }
125 }
128  // Print generated nuclides list
130 {
131  for (size_t i=0; i<ionStore.size(); i++)
132  {
133  G4cout << ionStore[i].name << G4endl;
134  }
135 }
137  // Clear Hit voxel (TrackID) markers
139 {
140  for(G4int i=0; i<numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ; i++) hitTrack[i] = 0;
141 }
142  // Return Hit status
144 {
145  return &(hitTrack[Index(i,j,k)]);
146 }
148 // Dose methods...
149 // Fill DOSE/fluence matrix for secondary particles:
150 // If fluence parameter is true (default value is FALSE) then fluence at voxel (i, j, k) is increased.
151 // The energyDeposit parameter fill the dose matrix for voxel (i,j,k)
153 
155  G4ParticleDefinition* particleDef,
156  G4int i, G4int j, G4int k,
157  G4double energyDeposit,
158  G4bool fluence)
159 {
160  if ( (energyDeposit <=0. && !fluence) || !secondary) return false;
161  // Get Particle Data Group particle ID
162  G4int PDGencoding = particleDef -> GetPDGEncoding();
163  PDGencoding -= PDGencoding%10;
164 
165  // Search for already allocated data...
166  for (size_t l=0; l < ionStore.size(); l++)
167  {
168  if (ionStore[l].PDGencoding == PDGencoding )
169  { // Is it a primary or a secondary particle?
170  if ( (trackID ==1 && ionStore[l].isPrimary) || (trackID !=1 && !ionStore[l].isPrimary))
171  {
172  if (energyDeposit > 0.) ionStore[l].dose[Index(i, j, k)] += energyDeposit;
173 
174  // Fill a matrix per each ion with the fluence
175  if (fluence) ionStore[l].fluence[Index(i, j, k)]++;
176  return true;
177  }
178  }
179  }
180 
181  G4int Z = particleDef-> GetAtomicNumber();
182  G4int A = particleDef-> GetAtomicMass();
183 
184  G4String fullName = particleDef -> GetParticleName();
185  G4String name = fullName.substr (0, fullName.find("[") ); // cut excitation energy
186  // Let's put a new particle in our store...
187  ion newIon =
188  {
189  (trackID == 1) ? true:false,
190  PDGencoding,
191  name,
192  name.length(),
193  Z,
194  A,
195  new G4double[numberOfVoxelAlongX * numberOfVoxelAlongY * numberOfVoxelAlongZ],
196  new unsigned int[numberOfVoxelAlongX * numberOfVoxelAlongY * numberOfVoxelAlongZ]
197  };
198  // Initialize data
199  if (newIon.dose && newIon.fluence)
200  {
201  for(G4int q=0; q<numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ; q++)
202  {
203  newIon.dose[q] = 0.;
204  newIon.fluence[q] = 0;
205  }
206  if (energyDeposit > 0.) newIon.dose[Index(i, j, k)] += energyDeposit;
207  if (fluence) newIon.fluence[Index(i, j, k)]++;
208 
209  ionStore.push_back(newIon);
210 
211  // TODO Put some verbosity check
212  /*
213  G4cout << "Memory space to store the DOSE/FLUENCE into " <<
214  numberOfVoxelAlongX*numberOfVoxelAlongY*numberOfVoxelAlongZ <<
215  " voxels has been allocated for the nuclide " << newIon.name <<
216  " (Z = " << Z << ", A = " << A << ")" << G4endl ;
217  */
218  return true;
219  }
220  else // XXX Out of memory! XXX
221  {
222  return false;
223  }
224 
225 }
226 
229  // Methods to store data to filenames...
232  //
233  // General method to store matrix data to filename
235 {
236  if (data)
237  {
238  ofs.open(file, std::ios::out);
239  if (ofs.is_open())
240  {
241  for(G4int i = 0; i < numberOfVoxelAlongX; i++)
242  for(G4int j = 0; j < numberOfVoxelAlongY; j++)
243  for(G4int k = 0; k < numberOfVoxelAlongZ; k++)
244  {
245  G4int n = Index(i, j, k);
246  // Check for data type: u_int, G4double, XXX
247  if (psize == sizeof(unsigned int))
248  {
249  unsigned int* pdata = (unsigned int*)data;
250  if (pdata[n]) ofs << i << '\t' << j << '\t' <<
251  k << '\t' << pdata[n] << G4endl;
252  }
253  else if (psize == sizeof(G4double))
254  {
255  G4double* pdata = (G4double*)data;
256  if (pdata[n]) ofs << i << '\t' << j << '\t' <<
257  k << '\t' << pdata[n] << G4endl;
258  }
259  }
260  ofs.close();
261  }
262  }
263 }
264 
265  // Store fluence per single ion in multiple files
267 {
268  for (size_t i=0; i < ionStore.size(); i++){
269  StoreMatrix(ionStore[i].name + "_Fluence.out", ionStore[i].fluence, sizeof(unsigned int));
270  }
271 }
272  // Store dose per single ion in multiple files
274 {
275 
276  for (size_t i=0; i < ionStore.size(); i++){
277  StoreMatrix(ionStore[i].name + "_Dose.out", ionStore[i].dose, sizeof(G4double));
278  }
279 }
281  // Store dose for all ions into a single file and into ntuples.
282  // Please note that this function is called via messenger commands
283  // defined in the HadrontherapyAnalysisFileMessenger.cc class file
285 {
286 #define width 15L
287  filename = (file=="") ? stdFile:file;
288  // Sort like periodic table
289  std::sort(ionStore.begin(), ionStore.end());
290  G4cout << "Dose is being written to " << filename << G4endl;
291  ofs.open(filename, std::ios::out);
292  if (ofs.is_open())
293  {
294  // Write the voxels index and the list of particles/ions
295  ofs << std::setprecision(6) << std::left <<
296  "i\tj\tk\t";
297  // Total dose
298  ofs << std::setw(width) << "Dose(Gy)";
299  if (secondary)
300  {
301  for (size_t l=0; l < ionStore.size(); l++)
302  {
303  G4String a = (ionStore[l].isPrimary) ? "_1":""; // is it a primary?
304  ofs << std::setw(width) << ionStore[l].name + a <<
305  std::setw(width) << ionStore[l].name + a;
306  }
307  ofs << G4endl;
308 
309  /*
310  * PDGencondig
311  */
312  /*
313  ofs << std::setprecision(6) << std::left <<
314  "0\t0\t0\t";
315 
316  // Total dose
317  ofs << std::setw(width) << '0';
318  for (size_t l=0; l < ionStore.size(); l++)
319  {
320  ofs << std::setw(width) << ionStore[l].PDGencoding <<
321  std::setw(width) << ionStore[l].PDGencoding;
322  }
323  ofs << G4endl;
324  */
325  }
326  // Write data
327  for(G4int i = 0; i < numberOfVoxelAlongX; i++)
328  for(G4int j = 0; j < numberOfVoxelAlongY; j++)
329  for(G4int k = 0; k < numberOfVoxelAlongZ; k++)
330  {
331  G4int n = Index(i, j, k);
332  // Write only not identically null data lines
333  if (matrix[n])
334  {
335  ofs << G4endl;
336  ofs << i << '\t' << j << '\t' << k << '\t';
337  // Total dose
338  ofs << std::setw(width) << (matrix[n]/massOfVoxel)/doseUnit;
339  if (secondary)
340  {
341  for (size_t l=0; l < ionStore.size(); l++)
342  {
343  // Fill ASCII file rows
344  ofs << std::setw(width) << ionStore[l].dose[n]/massOfVoxel/doseUnit <<
345  std::setw(width) << ionStore[l].fluence[n];
346  }
347  }
348  }
349  }
350  ofs.close();
351  }
352 }
354 
355 #ifdef G4ANALYSIS_USE_ROOT
356 void HadrontherapyMatrix::StoreDoseFluenceRoot()
357 {
359  if (analysis -> IsTheTFile())
360  {
361  for(G4int i = 0; i < numberOfVoxelAlongX; i++)
362  for(G4int j = 0; j < numberOfVoxelAlongY; j++)
363  for(G4int k = 0; k < numberOfVoxelAlongZ; k++)
364  {
365  G4int n = Index(i, j, k);
366  for (size_t l=0; l < ionStore.size(); l++)
367 
368  {
369  // Do the same work for .root file: fill dose/fluence ntuple
370  analysis -> FillVoxelFragmentTuple( i, j, k,
371  ionStore[l].A,
372  ionStore[l].Z,
373  ionStore[l].dose[n]/massOfVoxel/doseUnit,
374  ionStore[l].fluence[n] );
375 
376 
377  }
378  }
379  }
380 }
381 #endif
382 
384  G4double energyDeposit)
385 {
386  if (matrix)
387  matrix[Index(i,j,k)] += energyDeposit;
388 
389  // Store the energy deposit in the matrix element corresponding
390  // to the phantom voxel
391 }
393 {
394  // Convert energy deposited to dose.
395  // Store the information of the matrix in a ntuple and in
396  // a 1D Histogram
397 #ifdef G4ANALYSIS_USE_ROOT
399 #endif
400 
401  if (matrix)
402  {
403  for(G4int i = 0; i < numberOfVoxelAlongX; i++)
404  for(G4int j = 0; j < numberOfVoxelAlongY; j++)
405  for(G4int k = 0; k < numberOfVoxelAlongZ; k++)
406  {
407 #ifdef G4ANALYSIS_USE_ROOT
408  G4int n = Index(i,j,k);
409  if (analysis -> IsTheTFile() )
410  {
411  analysis -> FillEnergyDeposit(i, j, k, matrix[n]/massOfVoxel/doseUnit);
412  analysis -> BraggPeak(i, matrix[n]/massOfVoxel/doseUnit);
413  }
414 #endif
415  }
416  }
417 }
418 
static HadrontherapyAnalysisManager * GetInstance()
tuple a
Definition: test.py:11
Float_t dose
G4double * dose
G4int Index(G4int i, G4int j, G4int k)
#define width
void StoreDoseFluenceAscii(G4String filename="")
const XML_Char * name
Definition: expat.h:151
subroutine sort(A, N)
Definition: dpm25nuc7.f:4670
TFile * file
int G4int
Definition: G4Types.hh:78
static HadrontherapyMatrix * GetInstance()
Char_t n[5]
void StoreMatrix(G4String file, void *data, size_t psize)
G4GLOB_DLL std::ostream G4cout
unsigned int * fluence
Float_t Z
Definition: plot.C:39
bool G4bool
Definition: G4Types.hh:79
G4int * GetHitTrack(G4int i, G4int j, G4int k)
G4bool Fill(G4int, G4ParticleDefinition *particleDef, G4int i, G4int j, G4int k, G4double energyDeposit, G4bool fluence=false)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
const XML_Char const XML_Char * data
Definition: expat.h:268