source: trunk/source/geometry/management/test/TestAssemblyVolume/src/TstVAPrimaryGeneratorAction.cc @ 1348

Last change on this file since 1348 was 1347, checked in by garnier, 14 years ago

geant4 tag 9.4

File size: 5.9 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// $Id: TstVAPrimaryGeneratorAction.cc,v 1.7 2006/06/29 18:34:43 gunter Exp $
28// GEANT4 tag $Name: geant4-09-04-ref-00 $
29//
30// --------------------------------------------------------------
31
32#include "globals.hh"
33#include "Randomize.hh"
34
35#include "TstVAPrimaryGeneratorAction.hh"
36
37#include "TstVAPrimaryGeneratorMessenger.hh"
38#include "G4Event.hh"
39#include "G4ParticleGun.hh"
40#include "G4ParticleTable.hh"
41#include "G4ParticleDefinition.hh"
42#include "G4TransportationManager.hh"
43
44TstVAPrimaryGeneratorAction::TstVAPrimaryGeneratorAction():
45  generatorAction (standardGun),
46  particleGun (0),
47  messenger (0),
48  worldVolume (0)
49{
50  G4int n_particle = 1;
51  particleGun = new G4ParticleGun(n_particle);
52
53  // default particle
54
55  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
56  G4String particleName;
57  G4ParticleDefinition* particle
58    = particleTable->FindParticle(particleName="e-");
59  particleGun->SetParticleDefinition(particle);
60  particleGun->SetParticleMomentumDirection(G4ThreeVector(1.,0.,0.));
61  particleGun->SetParticleEnergy(1.*GeV);
62  particleGun->SetParticlePosition(G4ThreeVector(0.*cm,0.*cm,0.*cm));
63
64  // messenger
65
66  messenger = new TstVAPrimaryGeneratorMessenger (this);
67
68  // world extent
69
70  worldVolume = G4TransportationManager::GetTransportationManager ()
71    -> GetNavigatorForTracking () -> GetWorldVolume ();
72  if (worldVolume) worldExtent = worldVolume -> GetLogicalVolume ()
73                     -> GetSolid () -> GetExtent ();
74
75}
76
77TstVAPrimaryGeneratorAction::~TstVAPrimaryGeneratorAction()
78{
79  delete particleGun;
80  delete messenger;
81}
82
83void TstVAPrimaryGeneratorAction::SelectPrimaryGeneratorAction
84(Action action)
85{
86  generatorAction = action;
87}
88
89void TstVAPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
90{
91  G4VPhysicalVolume* currentWorldVolume;
92
93  double costheta;
94  double sintheta;
95  double phi;
96  double cosphi;
97  double sinphi;
98
99  switch (generatorAction) {
100
101  case standardGun:
102
103    particleGun->GeneratePrimaryVertex(anEvent);
104    break;
105
106  case randomDirectionGun:
107
108    costheta = CLHEP::RandFlat::shoot (-1., 1.);
109    sintheta = std::sqrt (1. - costheta * costheta);
110    phi      = CLHEP::RandFlat::shoot (twopi);
111    cosphi   = std::cos (phi);
112    sinphi   = std::sin (phi);
113    particleGun->SetParticleMomentumDirection
114      (G4ThreeVector (sintheta * cosphi, sintheta * sinphi, costheta));
115
116    particleGun->GeneratePrimaryVertex(anEvent);
117    break;
118
119  case randomPositionGun:
120
121    // Check if world is in place or has changed.
122    currentWorldVolume = 
123      G4TransportationManager::GetTransportationManager ()
124      -> GetNavigatorForTracking () -> GetWorldVolume ();
125    if (!worldVolume || worldVolume != currentWorldVolume) {
126      worldVolume = currentWorldVolume;
127      if (worldVolume) worldExtent = worldVolume -> GetLogicalVolume ()
128                         -> GetSolid () -> GetExtent ();
129    }
130
131    particleGun->SetParticlePosition
132      (G4ThreeVector
133       (CLHEP::RandFlat::shoot (worldExtent.GetXmin (), worldExtent.GetXmax ()),
134        CLHEP::RandFlat::shoot (worldExtent.GetYmin (), worldExtent.GetYmax ()),
135        CLHEP::RandFlat::shoot (worldExtent.GetZmin (), worldExtent.GetZmax ())));
136
137    particleGun->GeneratePrimaryVertex(anEvent);
138    break;
139
140  case randomPositionAndDirectionGun:
141
142    // Check if world is in place or has changed.
143    currentWorldVolume =
144      G4TransportationManager::GetTransportationManager ()
145      -> GetNavigatorForTracking () -> GetWorldVolume ();
146    if (!worldVolume || worldVolume != currentWorldVolume) {
147      worldVolume = currentWorldVolume;
148      if (worldVolume) worldExtent = worldVolume -> GetLogicalVolume ()
149                         -> GetSolid () -> GetExtent ();
150    }
151
152    particleGun->SetParticlePosition
153      (G4ThreeVector
154       (CLHEP::RandFlat::shoot (worldExtent.GetXmin (), worldExtent.GetXmax ()),
155        CLHEP::RandFlat::shoot (worldExtent.GetYmin (), worldExtent.GetYmax ()),
156        CLHEP::RandFlat::shoot (worldExtent.GetZmin (), worldExtent.GetZmax ())));
157
158    costheta = CLHEP::RandFlat::shoot (-1., 1.);
159    sintheta = std::sqrt (1. - costheta * costheta);
160    phi      = CLHEP::RandFlat::shoot (twopi);
161    cosphi   = std::cos (phi);
162    sinphi   = std::sin (phi);
163    particleGun->SetParticleMomentumDirection
164      (G4ThreeVector (sintheta * cosphi, sintheta * sinphi, costheta));
165
166    particleGun->GeneratePrimaryVertex(anEvent);
167    break;
168
169  }
170}
Note: See TracBrowser for help on using the repository browser.