source: trunk/examples/advanced/gammaray_telescope/src/GammaRayTelPrimaryGeneratorAction.cc@ 1163

Last change on this file since 1163 was 807, checked in by garnier, 17 years ago

update

File size: 7.5 KB
RevLine 
[807]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//
27// $Id: GammaRayTelPrimaryGeneratorAction.cc,v 1.10 2006/06/29 15:56:58 gunter Exp $
28// GEANT4 tag $Name: geant4-09-01-patch-02 $
29// ------------------------------------------------------------
30// GEANT 4 class implementation file
31// CERN Geneva Switzerland
32//
33//
34// ------------ GammaRayTelPrimaryGeneratorAction ------
35// by G.Santin, F.Longo & R.Giannitrapani (13 nov 2000)
36//
37// ************************************************************
38
39//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
40//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
41
42#include "G4RunManager.hh"
43#include "GammaRayTelPrimaryGeneratorAction.hh"
44
45#include "GammaRayTelDetectorConstruction.hh"
46#include "GammaRayTelPrimaryGeneratorMessenger.hh"
47
48#include "G4Event.hh"
49#include "G4ParticleGun.hh"
50#include "G4GeneralParticleSource.hh"
51#include "G4ParticleTable.hh"
52#include "G4ParticleDefinition.hh"
53#include "Randomize.hh"
54
55//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
56
57GammaRayTelPrimaryGeneratorAction::GammaRayTelPrimaryGeneratorAction()
58 :rndmFlag("off"),nSourceType(0),nSpectrumType(0)
59{
60 G4RunManager* runManager = G4RunManager::GetRunManager();
61 GammaRayTelDetector =
62 (GammaRayTelDetectorConstruction*)(runManager->GetUserDetectorConstruction());
63
64
65 //create a messenger for this class
66
67 gunMessenger = new GammaRayTelPrimaryGeneratorMessenger(this);
68
69 G4int n_particle = 1;
70
71 if (sourceGun)
72 {
73 particleGun = new G4ParticleGun(n_particle);
74 // default particle kinematic
75
76 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
77 G4String particleName;
78 G4ParticleDefinition* particle
79 = particleTable->FindParticle(particleName="e-");
80 particleGun->SetParticleDefinition(particle);
81 particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,-1.));
82 particleGun->SetParticleEnergy(30.*MeV);
83 G4double position = 0.5*(GammaRayTelDetector->GetWorldSizeZ());
84 particleGun->SetParticlePosition(G4ThreeVector(0.*cm,0.*cm,position));
85 }
86 else
87 {
88 particleSource = new G4GeneralParticleSource();
89 }
90
91}
92
93//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
94
95GammaRayTelPrimaryGeneratorAction::~GammaRayTelPrimaryGeneratorAction()
96{
97 if (sourceGun)
98 delete particleGun;
99 else
100 delete particleSource;
101
102 delete gunMessenger;
103}
104
105//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
106
107void GammaRayTelPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
108{
109 if (sourceGun)
110 {
111
112 //this function is called at the begining of event
113 //
114 G4double z0 = 0.5*(GammaRayTelDetector->GetWorldSizeZ());
115 G4double x0 = 0.*cm, y0 = 0.*cm;
116
117 G4ThreeVector pos0;
118 G4ThreeVector dir0;
119 G4ThreeVector vertex0 = G4ThreeVector(x0,y0,z0);
120
121 dir0 = G4ThreeVector(0.,0.,-1.);
122
123 G4double theta, phi, y, f;
124 G4double theta0=0.;
125 G4double phi0=0.;
126
127 switch(nSourceType) {
128 case 0:
129 particleGun->SetParticlePosition(vertex0);
130 particleGun->SetParticleMomentumDirection(dir0);
131 break;
132 case 1:
133 // GS: Generate random position on the 4PIsphere to create a unif. distrib.
134 // GS: on the sphere
135 phi = G4UniformRand() * twopi;
136 do {
137 y = G4UniformRand()*1.0;
138 theta = G4UniformRand() * pi;
139 f = std::sin(theta);
140 } while (y > f);
141 vertex0 = G4ThreeVector(1.,0.,0.);
142 vertex0.setMag(dVertexRadius);
143 vertex0.setTheta(theta);
144 vertex0.setPhi(phi);
145 particleGun->SetParticlePosition(vertex0);
146
147 dir0 = G4ThreeVector(1.,0.,0.);
148 do {
149 phi = G4UniformRand() * twopi;
150 do {
151 y = G4UniformRand()*1.0;
152 theta = G4UniformRand() * pi;
153 f = std::sin(theta);
154 } while (y > f);
155 dir0.setPhi(phi);
156 dir0.setTheta(theta);
157 } while (vertex0.dot(dir0) >= -0.7 * vertex0.mag());
158 particleGun->SetParticleMomentumDirection((G4ParticleMomentum)dir0);
159
160 break;
161 case 2:
162 // GS: Generate random position on the upper semi-sphere z>0 to create a unif. distrib.
163 // GS: on a plane
164 phi = G4UniformRand() * twopi;
165 do {
166 y = G4UniformRand()*1.0;
167 theta = G4UniformRand() * halfpi;
168 f = std::sin(theta) * std::cos(theta);
169 } while (y > f);
170 vertex0 = G4ThreeVector(1.,0.,0.);
171
172 G4double xy = GammaRayTelDetector->GetWorldSizeXY();
173 G4double z = GammaRayTelDetector->GetWorldSizeZ();
174
175 if (dVertexRadius > xy*0.5)
176 {
177 G4cout << "vertexRadius too big " << G4endl;
178 G4cout << "vertexRadius setted to " << xy*0.45 << G4endl;
179 dVertexRadius = xy*0.45;
180 }
181
182 if (dVertexRadius > z*0.5)
183 {
184 G4cout << "vertexRadius too high " << G4endl;
185 G4cout << "vertexRadius setted to " << z*0.45 << G4endl;
186 dVertexRadius = z*0.45;
187 }
188
189
190 vertex0.setMag(dVertexRadius);
191 vertex0.setTheta(theta);
192 vertex0.setPhi(phi);
193
194 // GS: Get the user defined direction for the primaries and
195 // GS: Rotate the random position according to the user defined direction for the particle
196
197 dir0 = particleGun->GetParticleMomentumDirection();
198 if (dir0.mag() > 0.001)
199 {
200 theta0 = dir0.theta();
201 phi0 = dir0.phi();
202 }
203
204 if (theta0!=0.)
205 {
206 G4ThreeVector rotationAxis(1.,0.,0.);
207 rotationAxis.setPhi(phi0+halfpi);
208 vertex0.rotate(theta0+pi,rotationAxis);
209 }
210 particleGun->SetParticlePosition(vertex0);
211 break;
212 }
213
214
215 G4double pEnergy;
216
217 switch(nSpectrumType) {
218 case 0:
219 break;
220 case 1:
221 break;
222 case 2:
223 do {
224 y = G4UniformRand()*100000.0;
225 pEnergy = G4UniformRand() * 10. * GeV;
226 f = std::pow(pEnergy * (1/GeV), -4.);
227 } while (y > f);
228
229 particleGun->SetParticleEnergy(pEnergy);
230
231 break;
232 case 3:
233 break;
234 }
235
236 particleGun->GeneratePrimaryVertex(anEvent);
237 }
238 else
239 {
240 particleSource->GeneratePrimaryVertex(anEvent);
241 }
242
243}
244
245//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
246
247
248
249
250
251
252
253
Note: See TracBrowser for help on using the repository browser.