source: trunk/examples/advanced/hadrontherapy/src/HadrontherapyMatrix.cc@ 1305

Last change on this file since 1305 was 1230, checked in by garnier, 16 years ago

update to geant4.9.3

File size: 4.7 KB
Line 
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// $Id: HadrontherapyMatrix.cc;
27// See more at: http://g4advancedexamples.lngs.infn.it/Examples/hadrontherapy//
28
29#include "HadrontherapyMatrix.hh"
30#include "HadrontherapyAnalysisManager.hh"
31#include "globals.hh"
32#include <fstream>
33
34HadrontherapyMatrix* HadrontherapyMatrix::instance = NULL;
35// Static method that only return a pointer to the matrix object
36HadrontherapyMatrix* HadrontherapyMatrix::getInstance()
37{
38 return instance;
39}
40// This STATIC method delete (!) the old matrix and rewrite a new object returning a pointer to it
41HadrontherapyMatrix* HadrontherapyMatrix::getInstance(G4int voxelX, G4int voxelY, G4int voxelZ)
42{
43 if (instance) delete instance;
44 instance = new HadrontherapyMatrix(voxelX, voxelY, voxelZ);
45 instance -> Initialize();
46 return instance;
47}
48
49HadrontherapyMatrix::HadrontherapyMatrix(G4int voxelX, G4int voxelY, G4int voxelZ):
50 matrix(0)
51{
52// Number of the voxels of the phantom
53// For Y = Z = 1 the phantom is divided in slices (and not in voxels)
54// orthogonal to the beam axis
55 numberVoxelX = voxelX;
56 numberVoxelY = voxelY;
57 numberVoxelZ = voxelZ;
58 // Create the dose matrix
59 matrix = new G4double[numberVoxelX*numberVoxelY*numberVoxelZ];
60 if (matrix) G4cout << "Matrix: Memory space to store physical dose into " <<
61 numberVoxelX*numberVoxelY*numberVoxelZ <<
62 " voxels has been allocated " << G4endl;
63 else G4Exception("Can't allocate memory to store physical dose!");
64}
65
66HadrontherapyMatrix::~HadrontherapyMatrix()
67{
68 delete[] matrix;
69}
70
71void HadrontherapyMatrix::flush()
72{
73 if(matrix)
74 for(int i=0; i<numberVoxelX*numberVoxelY*numberVoxelZ; i++)
75 {
76 matrix[i] = 0;
77 }
78}
79void HadrontherapyMatrix::Initialize()
80{
81// Initialise the elements of the matrix to zero
82 for(G4int i = 0; i < numberVoxelX; i++)
83 {
84 for(G4int j = 0; j < numberVoxelY; j++)
85 {
86 for(G4int k = 0; k < numberVoxelZ; k++)
87
88 matrix[Index(i,j,k)] = 0.;
89 }
90 }
91}
92
93void HadrontherapyMatrix::Fill(G4int i, G4int j, G4int k,
94 G4double energyDeposit)
95{
96 if (matrix)
97 matrix[Index(i,j,k)] += energyDeposit;
98
99 // Store the energy deposit in the matrix element corresponding
100 // to the phantom voxel i, j, k
101}
102
103void HadrontherapyMatrix::TotalEnergyDeposit()
104{
105 // Store the information of the matrix in a ntuple and in
106 // a 1D Histogram
107
108 G4int k;
109 G4int j;
110 G4int i;
111
112 if (matrix)
113 {
114 std::ofstream ofs;
115 ofs.open("DoseDistribution.out");
116
117 for(G4int l = 0; l < numberVoxelZ; l++)
118 {
119 k = l;
120
121 for(G4int m = 0; m < numberVoxelY; m++)
122 {
123 j = m * numberVoxelZ + k;
124
125 for(G4int n = 0; n < numberVoxelX; n++)
126 {
127 i = n* numberVoxelZ * numberVoxelY + j;
128 if(matrix[i] != 0)
129 {
130 ofs << n << '\t' << m << '\t' <<
131 k << '\t' << matrix[i] << G4endl;
132
133#ifdef ANALYSIS_USE
134 HadrontherapyAnalysisManager* analysis =
135 HadrontherapyAnalysisManager::getInstance();
136 analysis -> FillEnergyDeposit(n, m, k, matrix[i]);
137 analysis -> BraggPeak(n, matrix[i]);
138#endif
139 }
140}
141 }
142 }
143 }
144}
Note: See TracBrowser for help on using the repository browser.