Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
F04PrimaryGeneratorAction.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 //
28 //
29 //
30 #include "G4ios.hh"
31 #include "G4Event.hh"
32 #include "G4ParticleGun.hh"
33 #include "G4ParticleTable.hh"
34 #include "G4ParticleDefinition.hh"
35 
36 #include "G4GeometryManager.hh"
37 
38 #include "Randomize.hh"
39 #include "G4PhysicalConstants.hh"
40 #include "G4SystemOfUnits.hh"
41 
43 
46 
47 G4bool F04PrimaryGeneratorAction::fFirst = false;
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
53  : fDetector(detectorConstruction), fRndmFlag("off"),
54  fXvertex(0.), fYvertex(0.), fZvertex(0.),
55  fVertexdefined(false)
56 {
57  G4int n_particle = 1;
58  fParticleGun = new G4ParticleGun(n_particle);
59 
60  fGunMessenger = new F04PrimaryGeneratorMessenger(this);
61 
62  G4String particleName;
64 
65  fParticleGun->SetParticleDefinition(particleTable->
66  FindParticle(particleName="proton"));
67  fParticleGun->SetParticleEnergy(500.*MeV);
68  fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
69 
70  fZvertex = -0.5*(fDetector->GetTargetThickness());
71  fParticleGun->SetParticlePosition(G4ThreeVector(fXvertex,fYvertex,fZvertex));
72 
73 }
74 
75 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
76 
78 {
79  delete fParticleGun;
80  delete fGunMessenger;
81 }
82 
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
86 {
87 
88  if (!fFirst) {
89 
90  fFirst = true;
91 
92  G4Navigator* theNavigator =
94  GetNavigatorForTracking();
95  G4Navigator* aNavigator = new G4Navigator();
96  if ( theNavigator->GetWorldVolume() )
97  aNavigator->SetWorldVolume(theNavigator->GetWorldVolume());
98 
100 
101  if (!geomManager->IsGeometryClosed()) {
102  geomManager->OpenGeometry();
103  geomManager->CloseGeometry(true);
104  }
105 
106  G4ThreeVector center(0.,0.,0.);
107  aNavigator->LocateGlobalPointAndSetup(center,0,false);
108 
109  G4TouchableHistoryHandle touchable = aNavigator->
110  CreateTouchableHistoryHandle();
111 
112  // set Global2local transform
113  fGlobal2local = touchable->GetHistory()->GetTopTransform();
114 
115  G4ThreeVector direction(0.0,0.0,1.0);
116  direction = fGlobal2local.Inverse().TransformAxis(direction);
117 
118  fParticleGun->SetParticleMomentumDirection(direction);
119  }
120 
121  G4double x0,y0,z0 ;
122 
123  if(fVertexdefined)
124  {
125  x0 = fXvertex ;
126  y0 = fYvertex ;
127  z0 = fZvertex ;
128  }
129  else
130  {
131  x0 = 0. ;
132  y0 = 0. ;
133  z0 = -0.5*(fDetector->GetTargetThickness());
134  }
135 
136  G4double r0, phi0;
137 
138  if (fRndmFlag == "on")
139  {
140  r0 = (fDetector->GetTargetRadius())*std::sqrt(G4UniformRand());
141  phi0 = twopi*G4UniformRand();
142  x0 = r0*std::cos(phi0);
143  y0 = r0*std::sin(phi0);
144  }
145 
146  G4ThreeVector localPosition(x0,y0,z0);
147  G4ThreeVector globalPosition =
148  fGlobal2local.Inverse().TransformPoint(localPosition);
149 
150  fParticleGun->SetParticlePosition(globalPosition);
151  fParticleGun->GeneratePrimaryVertex(anEvent);
152 }
153 
154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
155 
157 {
158  fVertexdefined = true;
159  fXvertex = x;
160  G4cout << " X coordinate of the primary vertex = " << fXvertex/mm <<
161  " mm." << G4endl;
162 }
163 
164 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
165 
167 {
168  fVertexdefined = true;
169  fYvertex = y;
170  G4cout << " Y coordinate of the primary vertex = " << fYvertex/mm <<
171  " mm." << G4endl;
172 }
173 
174 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
175 
177 {
178  fVertexdefined = true;
179  fZvertex = z;
180  G4cout << " Z coordinate of the primary vertex = " << fZvertex/mm <<
181  " mm." << G4endl;
182 }