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