ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HadrontherapyDetectorSD.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HadrontherapyDetectorSD.cc
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 
31 #include "G4Step.hh"
32 #include "G4VTouchable.hh"
33 #include "G4TouchableHistory.hh"
34 #include "G4SDManager.hh"
35 #include "HadrontherapyMatrix.hh"
36 #include "HadrontherapyLet.hh"
37 #include "G4Track.hh"
38 #include "G4SystemOfUnits.hh"
39 #include "HadrontherapyMatrix.hh"
40 
41 
42 #include "G4SteppingManager.hh"
43 #include "G4TrackVector.hh"
45 #include "G4ios.hh"
46 #include "G4SteppingManager.hh"
47 #include "G4Track.hh"
48 #include "G4Step.hh"
49 #include "G4StepPoint.hh"
50 #include "G4TrackStatus.hh"
51 #include "G4TrackVector.hh"
52 #include "G4ParticleDefinition.hh"
53 #include "G4ParticleTypes.hh"
54 #include "G4UserEventAction.hh"
56 #include "G4VSensitiveDetector.hh"
58 #include "G4SystemOfUnits.hh"
59 
60 
64 {
65  G4String HCname;
66  collectionName.insert(HCname="HadrontherapyDetectorHitsCollection");
67  HitsCollection = NULL;
69 
70 }
71 
74 {}
75 
78 {
79 
81  collectionName[0]);
82 }
83 
86 {
87 
88 
89  if (aStep -> GetPreStepPoint() -> GetPhysicalVolume() -> GetName() != "RODetectorZDivisionPhys") return false;
90 
91 
92  // Get kinetic energy
93  G4Track * theTrack = aStep -> GetTrack();
94 
95  G4ParticleDefinition *particleDef = theTrack -> GetDefinition();
96  //Get particle name
97  G4String particleName = particleDef -> GetParticleName();
98 
99  // Get particle PDG code
100  G4int pdg = particleDef ->GetPDGEncoding();
101 
102  // Get unique track_id (in an event)
103  G4int trackID = theTrack -> GetTrackID();
104 
105  G4double energyDeposit = aStep -> GetTotalEnergyDeposit();
106 
107  G4double DX = aStep -> GetStepLength();
108  G4int Z = particleDef-> GetAtomicNumber();
109  G4int A = particleDef-> GetAtomicMass();
110  G4StepPoint* PreStep = aStep->GetPreStepPoint();
111 
112  // Read voxel indexes: i is the x index, k is the z index
113  const G4VTouchable* touchable = aStep->GetPreStepPoint()->GetTouchable();
114  G4int k = touchable->GetReplicaNumber(0);
115  G4int i = touchable->GetReplicaNumber(2);
116  G4int j = touchable->GetReplicaNumber(1);
117 
118  G4TouchableHandle touchPreStep = PreStep->GetTouchableHandle();
119  G4VPhysicalVolume* volumePre = touchPreStep->GetVolume();
120  G4String namePre = volumePre->GetName();
121 
122 
123 
124 
125 
128 
129  G4int* hitTrack = matrix -> GetHitTrack(i,j,k);
130 
131 
132  // ******************** let ***************************
133  if (let)
134  {
135  if ( !(Z==0 && A==1) ) // All but not neutrons
136  {
137  if( energyDeposit>0. && DX >0. )// calculate only energy deposit
138  {
139  if (pdg !=22 && pdg !=11) // not gamma and electrons
140  {
141 
142  // Get the pre-step kinetic energy
143  G4double eKinPre = aStep -> GetPreStepPoint() -> GetKineticEnergy();
144  // Get the post-step kinetic energy
145  G4double eKinPost = aStep -> GetPostStepPoint() -> GetKineticEnergy();
146  // Get the step average kinetic energy
147  G4double eKinMean = (eKinPre + eKinPost) * 0.5;
148 
149  // get the material
150  G4Material * materialStep = aStep -> GetPreStepPoint() -> GetMaterial();
151 
152  // get the secondary paticles
153  G4Step fstep = *theTrack -> GetStep();
154  // store all the secondary partilce in current step
155  const std::vector<const G4Track*> * secondary = fstep.GetSecondaryInCurrentStep();
156 
157  size_t SecondarySize = (*secondary).size();
158  G4double EnergySecondary = 0.;
159 
160  // get secondary electrons energy deposited
161  if (SecondarySize) // calculate only secondary particles
162  {
163  for (size_t numsec = 0; numsec< SecondarySize ; numsec ++)
164  {
165  //Get the PDG code of every secondaty particles in current step
166  G4int PDGSecondary=(*secondary)[numsec]->GetDefinition()->GetPDGEncoding();
167 
168  if(PDGSecondary == 11) // calculate only secondary electrons
169  {
170  // calculate the energy deposit of secondary electrons in current step
171  EnergySecondary += (*secondary)[numsec]->GetKineticEnergy();
172  }
173  }
174 
175  }
176 
177  // call the let filldatas function to calculate let
178  let -> FillEnergySpectrum(trackID, particleDef, eKinMean, materialStep,
179  energyDeposit,EnergySecondary,DX, i, j, k);
180  }
181  }
182  }
183  }
184 
185 
186  if (matrix)
187  {
188 
189  // Increment Fluences & accumulate energy spectra
190  // Hit voxels are marked with track_id throught hitTrack matrix
191  //G4int* hitTrack = matrix -> GetHitTrack(i,j,k); // hitTrack MUST BE cleared at every eventAction!
192  if ( *hitTrack != trackID )
193  {
194  *hitTrack = trackID;
195  /*
196  * Fill FLUENCE data for every single nuclide
197  * Exclude e-, neutrons, gamma, ...
198  */
199  if ( Z >= 1) matrix -> Fill(trackID, particleDef, i, j, k, 0, true);
200 
201  }
202 
203  if(energyDeposit != 0)
204  {
205  /*
206  * This method will fill a dose matrix for every single nuclide.
207  * A method of the HadrontherapyMatrix class (StoreDoseFluenceAscii())
208  * is called automatically at the end of main (or via the macro command /analysis/writeDoseFile.
209  * It permits to store all dose/fluence data into a single plane ASCII file.
210  */
211  // if (A==1 && Z==1) // primary and sec. protons
212  if ( Z>=1 ) // exclude e-, neutrons, gamma, ...
213  matrix -> Fill(trackID, particleDef, i, j, k, energyDeposit);
214  /*
215  * Create a hit with the information of position is in the detector
216  */
218  detectorHit -> SetEdepAndPosition(i, j, k, energyDeposit);
219  HitsCollection -> insert(detectorHit);
220  }
221  }
222  return true;
223 }
224 
227 {
228 
229  static G4int HCID = -1;
230  if(HCID < 0)
231  {
232  HCID = GetCollectionID(0);
233  }
234 
235  HCE -> AddHitsCollection(HCID,HitsCollection);
236 }
237