Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrimaryGeneratorAction.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 // This example is provided by the Geant4-DNA collaboration
27 // Any report or published results obtained using the Geant4-DNA software
28 // shall cite the following Geant4-DNA collaboration publication:
29 // Med. Phys. 37 (2010) 4692-4708
30 // Delage et al. PDB4DNA: implementation of DNA geometry from the Protein Data
31 // Bank (PDB) description for Geant4-DNA Monte-Carlo
32 // simulations (submitted to Comput. Phys. Commun.)
33 // The Geant4-DNA web site is available at http://geant4-dna.org
34 //
35 // $Id$
36 //
39 
40 #include "PrimaryGeneratorAction.hh"
41 
42 #include "G4Box.hh"
43 #include "G4Event.hh"
44 #include "G4LogicalVolume.hh"
45 #include "G4LogicalVolumeStore.hh"
46 #include "G4ParticleDefinition.hh"
47 #include "G4ParticleGun.hh"
48 #include "G4ParticleTable.hh"
49 #include "G4PhysicalConstants.hh"
50 #include "G4PhysicalVolumeStore.hh"
51 #include "G4SystemOfUnits.hh"
52 
53 #include "Randomize.hh"
54 
55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
56 
59 {
60  G4int n_particle = 1;
61  fpParticleGun = new G4ParticleGun(n_particle);
62 
63  // default particle kinematic
64 
65  G4ParticleDefinition* particle
67  fpParticleGun->SetParticleDefinition(particle);
68  fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
69  fpParticleGun->SetParticleEnergy(0.1*MeV);
70  fpParticleGun->SetParticlePosition(G4ThreeVector(0.*nm,0.*nm,0.*nm));
71 }
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
74 
76 {
77  delete fpParticleGun;
78 }
79 
80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
81 
83 {
84  // In order to avoid dependence of PrimaryGeneratorAction
85  // on DetectorConstruction class we get world volume
86  // from G4LogicalVolumeStore
87  //
88  G4double boundXHalfLength = 0;
89  G4double boundYHalfLength = 0;
90  G4double boundZHalfLength = 0;
91 
92  G4VPhysicalVolume* boundPV
94 
95  G4ThreeVector boundPos;
96  if ( boundPV )
97  {
98  boundPos = boundPV->GetTranslation();
99  }
100 
101  G4LogicalVolume* boundLV
102  = G4LogicalVolumeStore::GetInstance()->GetVolume("BoundingLV");
103 
104  G4Box* boundBox = 0;
105  if ( boundLV )
106  {
107  boundBox = dynamic_cast< G4Box*>(boundLV->GetSolid());
108  }
109 
110  if ( boundBox )
111  {
112  boundXHalfLength = boundBox->GetXHalfLength();
113  boundYHalfLength = boundBox->GetYHalfLength();
114  boundZHalfLength = boundBox->GetZHalfLength();
115 
116  // Set gun position
117  // Select a starting position on a sphere including the target volume
118  //
119  G4double radius = std::sqrt(boundXHalfLength*boundXHalfLength+
120  boundYHalfLength*boundYHalfLength+
121  boundZHalfLength*boundZHalfLength);
122  G4double cosTheta = 2*G4UniformRand()-1;
123  G4double sinTheta = std::sqrt(1.-cosTheta*cosTheta);
124  G4double phi = twopi*G4UniformRand();
125 
126  G4ThreeVector positionStart(boundPos.x()+radius*sinTheta*std::cos(phi),
127  boundPos.y()+radius*sinTheta*std::sin(phi),
128  boundPos.z()+radius*cosTheta);
129 
130  fpParticleGun->SetParticlePosition(positionStart);
131 
132  // Set gun direction
133  // To compute the direction, select a point inside the target volume
134  //
135  G4ThreeVector positionDir(
136  boundPos.x()+boundXHalfLength*(2*G4UniformRand()-1),
137  boundPos.y()+boundYHalfLength*(2*G4UniformRand()-1),
138  boundPos.z()+boundZHalfLength*(2*G4UniformRand()-1));
139 
140  fpParticleGun->SetParticleMomentumDirection(
141  (positionDir-positionStart).unit());
142  }
143  else
144  {
145  G4cerr << "Bounding volume not found." << G4endl;
146  G4cerr << "Default particle kinematic used" << G4endl;
147  }
148 
149  fpParticleGun->GeneratePrimaryVertex(anEvent);
150 }
G4double GetXHalfLength() const
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
CLHEP::Hep3Vector G4ThreeVector
double x() const
Definition: G4Box.hh:64
G4VSolid * GetSolid() const
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
int G4int
Definition: G4Types.hh:78
virtual void GeneratePrimaryVertex(G4Event *evt)
G4double GetZHalfLength() const
G4LogicalVolume * GetVolume(const G4String &name, G4bool verbose=true) const
double z() const
static constexpr double twopi
Definition: G4SIunits.hh:76
static G4PhysicalVolumeStore * GetInstance()
void SetParticlePosition(G4ThreeVector aPosition)
#define G4UniformRand()
Definition: Randomize.hh:97
static G4LogicalVolumeStore * GetInstance()
G4double GetYHalfLength() const
const G4ThreeVector & GetTranslation() const
void SetParticleEnergy(G4double aKineticEnergy)
static G4ParticleTable * GetParticleTable()
static constexpr double nm
Definition: G4SIunits.hh:112
double y() const
#define G4endl
Definition: G4ios.hh:61
static constexpr double MeV
Definition: G4SIunits.hh:214
double G4double
Definition: G4Types.hh:76
G4VPhysicalVolume * GetVolume(const G4String &name, G4bool verbose=true) const
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
G4GLOB_DLL std::ostream G4cerr