source: trunk/examples/advanced/hadrontherapy/src/HadrontherapyPrimaryGeneratorAction.cc @ 1332

Last change on this file since 1332 was 1313, checked in by garnier, 14 years ago

geant4.9.4 beta rc0

File size: 7.2 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// HadrontherapyPrimarygeneratorAction.cc;
27// See more at: http://g4advancedexamples.lngs.infn.it/Examples/hadrontherapy
28// ----------------------------------------------------------------------------
29//                 GEANT 4 - Hadrontherapy example
30// ----------------------------------------------------------------------------
31// Code developed by:
32//
33// G.A.P. Cirrone(a)*, F.Romano(a)
34//
35// (a) Laboratori Nazionali del Sud
36//     of the INFN, Catania, Italy
37//
38// * cirrone@lns.infn.it
39//
40// ------------------------------------------------------------------------------
41
42#include "HadrontherapyPrimaryGeneratorAction.hh"
43#include "HadrontherapyPrimaryGeneratorMessenger.hh"
44#include "G4Event.hh"
45#include "G4ParticleGun.hh"
46#include "G4ParticleTable.hh"
47#include "G4ParticleDefinition.hh"
48#include "Randomize.hh"
49#include "HadrontherapyAnalysisManager.hh"
50
51HadrontherapyPrimaryGeneratorAction::HadrontherapyPrimaryGeneratorAction()
52{
53  // Define the messenger
54  gunMessenger = new HadrontherapyPrimaryGeneratorMessenger(this);
55
56  particleGun  = new G4ParticleGun();
57
58  SetDefaultPrimaryParticle(); 
59} 
60
61HadrontherapyPrimaryGeneratorAction::~HadrontherapyPrimaryGeneratorAction()
62{
63  delete particleGun;
64
65  delete gunMessenger;
66}
67 
68void HadrontherapyPrimaryGeneratorAction::SetDefaultPrimaryParticle()
69{   
70  // ****************************
71  // Default primary particle
72  // ****************************
73 
74  // Define primary particles: protons
75  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
76  G4ParticleDefinition* particle = particleTable -> FindParticle("proton");
77  particleGun -> SetParticleDefinition(particle); 
78
79  // Define the energy of primary particles:
80  // gaussian distribution with mean energy = 62.0 *MeV
81  // and sigma = 400.0 *keV
82  G4double defaultMeanKineticEnergy = 62.0 *MeV;
83  meanKineticEnergy = defaultMeanKineticEnergy;
84
85  G4double defaultsigmaEnergy = 400.0 *keV;
86  sigmaEnergy = defaultsigmaEnergy;
87 
88#ifdef G4ANALYSIS_USE_ROOT
89  // Write these values into the analysis if needed. Have to be written separately on change.
90  HadrontherapyAnalysisManager::GetInstance()->setBeamMetaData(meanKineticEnergy, sigmaEnergy);
91#endif
92
93  // Define the parameters of the initial position:
94  // the y, z coordinates have a gaussian distribution
95 
96  G4double defaultX0 = -2700.0 *mm;
97  X0 = defaultX0;
98
99  G4double defaultY0 = 0.0 *mm; 
100  Y0 = defaultY0;
101
102  G4double defaultZ0 = 0.0 *mm; 
103  Z0 = defaultZ0;
104
105  G4double defaultsigmaY = 1. *mm; 
106  sigmaY = defaultsigmaY;
107
108  G4double defaultsigmaZ = 1. *mm; 
109  sigmaZ = defaultsigmaZ;
110
111  // Define the parameters of the momentum of primary particles:
112  // The momentum along the y and z axis has a gaussian distribution
113  G4double defaultsigmaMomentumY = 0.0; 
114  sigmaMomentumY = defaultsigmaMomentumY;
115
116  G4double defaultsigmaMomentumZ = 0.0; 
117  sigmaMomentumZ = defaultsigmaMomentumZ;
118}
119
120void HadrontherapyPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
121{
122#ifdef G4ANALYSIS_USE_ROOT
123  // Increment the event counter
124  HadrontherapyAnalysisManager::GetInstance()->startNewEvent();
125#endif
126
127  // ****************************************
128  // Set the beam angular apread
129  // and spot size
130  // beam spot size
131  // ****************************************
132
133  // Set the position of the primary particles
134  G4double x = X0;
135  G4double y = Y0;
136  G4double z = Z0;
137
138  if ( sigmaY > 0.0 )
139    {
140      y += G4RandGauss::shoot( Y0, sigmaY );
141    }
142 
143  if ( sigmaZ > 0.0 )
144    {
145      z += G4RandGauss::shoot( Z0, sigmaZ );
146    }
147
148  particleGun -> SetParticlePosition(G4ThreeVector( x , y , z ) );
149 
150  // ********************************************
151  // Set the beam energy and energy spread
152  // ********************************************
153
154  G4double kineticEnergy = G4RandGauss::shoot( meanKineticEnergy, sigmaEnergy );
155  particleGun -> SetParticleEnergy ( kineticEnergy );
156
157  // Set the direction of the primary particles
158  G4double momentumX = 1.0;
159  G4double momentumY = 0.0;
160  G4double momentumZ = 0.0;
161
162  if ( sigmaMomentumY  > 0.0 )
163    {
164      momentumY += G4RandGauss::shoot( 0., sigmaMomentumY );
165    }
166  if ( sigmaMomentumZ  > 0.0 )
167    {
168      momentumZ += G4RandGauss::shoot( 0., sigmaMomentumZ );
169    }
170 
171  particleGun -> SetParticleMomentumDirection( G4ThreeVector(momentumX,momentumY,momentumZ) );
172
173  // Generate a primary particle
174  particleGun -> GeneratePrimaryVertex( anEvent ); 
175} 
176
177void HadrontherapyPrimaryGeneratorAction::SetmeanKineticEnergy (G4double val ) 
178{
179        meanKineticEnergy = val;
180#ifdef G4ANALYSIS_USE_ROOT
181  // Update the beam-data in the analysis manager
182  HadrontherapyAnalysisManager::GetInstance()->setBeamMetaData(meanKineticEnergy, sigmaEnergy);
183#endif
184
185} 
186
187void HadrontherapyPrimaryGeneratorAction::SetsigmaEnergy (G4double val ) 
188{ 
189        sigmaEnergy = val;
190#ifdef G4ANALYSIS_USE_ROOT
191  // Update the sigmaenergy in the metadata.
192  HadrontherapyAnalysisManager::GetInstance()->setBeamMetaData(meanKineticEnergy, sigmaEnergy);
193#endif
194}
195
196void HadrontherapyPrimaryGeneratorAction::SetXposition (G4double val ) 
197{ X0 = val;}
198
199void HadrontherapyPrimaryGeneratorAction::SetYposition (G4double val ) 
200{ Y0 = val;}
201
202void HadrontherapyPrimaryGeneratorAction::SetZposition (G4double val ) 
203{ Z0 = val;}
204
205void HadrontherapyPrimaryGeneratorAction::SetsigmaY (G4double val ) 
206{ sigmaY = val;}
207
208void HadrontherapyPrimaryGeneratorAction::SetsigmaZ (G4double val ) 
209{ sigmaZ = val;}
210
211void HadrontherapyPrimaryGeneratorAction::SetsigmaMomentumY (G4double val ) 
212{ sigmaMomentumY = val;}
213
214void HadrontherapyPrimaryGeneratorAction::SetsigmaMomentumZ (G4double val ) 
215{ sigmaMomentumZ = val;}
216
217G4double HadrontherapyPrimaryGeneratorAction::GetmeanKineticEnergy(void)
218{ return meanKineticEnergy;}
219
Note: See TracBrowser for help on using the repository browser.