source: trunk/examples/extended/field/field04/src/F04PrimaryGeneratorAction.cc @ 1309

Last change on this file since 1309 was 807, checked in by garnier, 16 years ago

update

File size: 5.3 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//
27//
28
29#include "G4ios.hh"
30#include "G4Event.hh"
31#include "G4ParticleGun.hh"
32#include "G4ParticleTable.hh"
33#include "G4ParticleDefinition.hh"
34
35#include "G4GeometryManager.hh"
36
37#include "Randomize.hh"
38
39#include "F04PrimaryGeneratorAction.hh"
40
41#include "F04DetectorConstruction.hh"
42#include "F04PrimaryGeneratorMessenger.hh"
43
44G4bool F04PrimaryGeneratorAction::first = false;
45
46F04PrimaryGeneratorAction::F04PrimaryGeneratorAction(F04DetectorConstruction* DC)
47  : Detector(DC), rndmFlag("off"),
48    xvertex(0.), yvertex(0.), zvertex(0.),
49    vertexdefined(false)
50{
51  G4int n_particle = 1;
52  particleGun  = new G4ParticleGun(n_particle);
53 
54  gunMessenger = new F04PrimaryGeneratorMessenger(this);
55
56  G4String particleName;
57  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
58
59  particleGun->SetParticleDefinition(particleTable->
60                                        FindParticle(particleName="proton"));
61  particleGun->SetParticleEnergy(500.*MeV);
62  particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
63
64  zvertex = -0.5*(Detector->GetTargetThickness());
65  particleGun->SetParticlePosition(G4ThreeVector(xvertex,yvertex,zvertex));
66
67}
68
69F04PrimaryGeneratorAction::~F04PrimaryGeneratorAction()
70{
71  delete particleGun;
72  delete gunMessenger;
73}
74
75void F04PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
76{
77
78  if (!first) {
79
80     first = true;
81
82     G4Navigator* theNavigator =
83                    G4TransportationManager::GetTransportationManager()->
84                                                 GetNavigatorForTracking();
85     G4Navigator* aNavigator = new G4Navigator();
86     if ( theNavigator->GetWorldVolume() )
87               aNavigator->SetWorldVolume(theNavigator->GetWorldVolume());
88
89     G4GeometryManager* geomManager = G4GeometryManager::GetInstance();
90
91     if (!geomManager->IsGeometryClosed()) {
92        geomManager->OpenGeometry();
93        geomManager->CloseGeometry(true);
94     }
95
96     G4ThreeVector center(0.,0.,0.);
97     aNavigator->LocateGlobalPointAndSetup(center,0,false);
98
99     G4TouchableHistoryHandle fTouchable = aNavigator->
100                                         CreateTouchableHistoryHandle();
101
102    // set global2local transform
103    global2local = fTouchable->GetHistory()->GetTopTransform();
104
105    G4ThreeVector direction(0.0,0.0,1.0);
106    direction = global2local.Inverse().TransformAxis(direction);
107
108    particleGun->SetParticleMomentumDirection(direction);
109  }
110
111  G4double x0,y0,z0 ;
112
113  if(vertexdefined)
114  {
115    x0 = xvertex ;
116    y0 = yvertex ;
117    z0 = zvertex ;
118  }
119  else
120  {
121    x0 = 0. ;
122    y0 = 0. ;
123    z0 = -0.5*(Detector->GetTargetThickness());
124  }
125
126  G4double r0,phi0 ;
127
128  if (rndmFlag == "on")
129  {
130      r0 = (Detector->GetTargetRadius())*std::sqrt(G4UniformRand());
131      phi0 = twopi*G4UniformRand();
132      x0 = r0*std::cos(phi0);
133      y0 = r0*std::sin(phi0);
134  } 
135
136  G4ThreeVector localPosition(x0,y0,z0);
137  G4ThreeVector globalPosition =
138                        global2local.Inverse().TransformPoint(localPosition);
139
140  particleGun->SetParticlePosition(globalPosition);
141  particleGun->GeneratePrimaryVertex(anEvent);
142}
143
144void F04PrimaryGeneratorAction::Setxvertex(G4double x)
145{
146  vertexdefined = true ;
147  xvertex = x ;
148  G4cout << " X coordinate of the primary vertex = " << xvertex/mm <<
149            " mm." << G4endl;
150}
151
152void F04PrimaryGeneratorAction::Setyvertex(G4double y)
153{
154  vertexdefined = true ;
155  yvertex = y ;
156  G4cout << " Y coordinate of the primary vertex = " << yvertex/mm <<
157            " mm." << G4endl;
158}
159
160void F04PrimaryGeneratorAction::Setzvertex(G4double z)
161{
162  vertexdefined = true ;
163  zvertex = z ;
164  G4cout << " Z coordinate of the primary vertex = " << zvertex/mm <<
165            " mm." << G4endl;
166}
Note: See TracBrowser for help on using the repository browser.