Geant4  10.02.p01
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 //
26 // $Id: F04PrimaryGeneratorAction.cc 78002 2013-12-02 08:25:49Z gcosmo $
27 //
30 //
31 
32 #include "G4ios.hh"
33 #include "G4Event.hh"
34 #include "G4ParticleGun.hh"
35 #include "G4ParticleTable.hh"
36 #include "G4ParticleDefinition.hh"
37 
38 #include "G4GeometryManager.hh"
39 
40 #include "Randomize.hh"
41 #include "G4PhysicalConstants.hh"
42 #include "G4SystemOfUnits.hh"
43 
45 
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
53  : fDetector(detectorConstruction), fRndmFlag("off"), fFirst(false),
54  fXvertex(0.), fYvertex(0.), fZvertex(0.),
55  fVertexdefined(false)
56 {
57  G4int n_particle = 1;
58  fParticleGun = new G4ParticleGun(n_particle);
59 
61 
62  G4String particleName;
64 
65  fParticleGun->SetParticleDefinition(particleTable->
66  FindParticle(particleName="proton"));
69 
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  G4ThreeVector direction(0.0,0.0,1.0);
92 
93  G4Navigator* theNavigator =
95  GetNavigatorForTracking();
96  if ( theNavigator->GetWorldVolume() )
97  {
98  G4Navigator* aNavigator = new G4Navigator();
99  aNavigator->SetWorldVolume(theNavigator->GetWorldVolume());
100 
101  G4ThreeVector center(0.,0.,0.);
102  aNavigator->LocateGlobalPointAndSetup(center,0,false);
103 
104  G4TouchableHistoryHandle touchable = aNavigator->
105  CreateTouchableHistoryHandle();
106 
107  // set Global2local transform
108  fGlobal2local = touchable->GetHistory()->GetTopTransform();
109 
110  direction = fGlobal2local.Inverse().TransformAxis(direction);
111  delete aNavigator;
112  }
113 
115  }
116 
117  G4double x0,y0,z0 ;
118 
119  if(fVertexdefined)
120  {
121  x0 = fXvertex ;
122  y0 = fYvertex ;
123  z0 = fZvertex ;
124  }
125  else
126  {
127  x0 = 0. ;
128  y0 = 0. ;
129  z0 = -0.5*(fDetector->GetTargetThickness());
130  }
131 
132  G4double r0, phi0;
133 
134  if (fRndmFlag == "on")
135  {
136  r0 = (fDetector->GetTargetRadius())*std::sqrt(G4UniformRand());
137  phi0 = twopi*G4UniformRand();
138  x0 = r0*std::cos(phi0);
139  y0 = r0*std::sin(phi0);
140  }
141 
142  G4ThreeVector localPosition(x0,y0,z0);
143  G4ThreeVector globalPosition =
144  fGlobal2local.Inverse().TransformPoint(localPosition);
145 
146  fParticleGun->SetParticlePosition(globalPosition);
148 }
149 
150 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
151 
153 {
154  fVertexdefined = true;
155  fXvertex = x;
156  G4cout << " X coordinate of the primary vertex = " << fXvertex/mm <<
157  " mm." << G4endl;
158 }
159 
160 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
161 
163 {
164  fVertexdefined = true;
165  fYvertex = y;
166  G4cout << " Y coordinate of the primary vertex = " << fYvertex/mm <<
167  " mm." << G4endl;
168 }
169 
170 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
171 
173 {
174  fVertexdefined = true;
175  fZvertex = z;
176  G4cout << " Z coordinate of the primary vertex = " << fZvertex/mm <<
177  " mm." << G4endl;
178 }
Definition of the F04PrimaryGeneratorAction class.
static const double MeV
Definition: G4SIunits.hh:211
CLHEP::Hep3Vector G4ThreeVector
G4AffineTransform Inverse() const
G4double z
Definition: TRTMaterials.hh:39
F04PrimaryGeneratorMessenger * fGunMessenger
virtual void GeneratePrimaries(G4Event *)
Definition of the F04PrimaryGeneratorMessenger class.
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
int G4int
Definition: G4Types.hh:78
virtual void GeneratePrimaryVertex(G4Event *evt)
Definition of the F04DetectorConstruction class.
void SetParticlePosition(G4ThreeVector aPosition)
#define G4UniformRand()
Definition: Randomize.hh:97
G4GLOB_DLL std::ostream G4cout
static const double twopi
Definition: G4SIunits.hh:75
static G4TransportationManager * GetTransportationManager()
void SetParticleEnergy(G4double aKineticEnergy)
G4ThreeVector TransformPoint(const G4ThreeVector &vec) const
F04DetectorConstruction * fDetector
static G4ParticleTable * GetParticleTable()
F04PrimaryGeneratorAction(F04DetectorConstruction *)
G4ThreeVector TransformAxis(const G4ThreeVector &axis) const
const G4double x[NPOINTSGL]
void SetWorldVolume(G4VPhysicalVolume *pWorld)
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=0, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
Definition: G4Navigator.cc:122
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
static const double mm
Definition: G4SIunits.hh:114
G4VPhysicalVolume * GetWorldVolume() const
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
const G4double r0