File indexing completed on 2026-04-24 08:14:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "F04PrimaryGeneratorAction.hh"
0030
0031 #include "F04DetectorConstruction.hh"
0032 #include "F04PrimaryGeneratorMessenger.hh"
0033
0034 #include "G4Event.hh"
0035 #include "G4GeometryManager.hh"
0036 #include "G4ParticleDefinition.hh"
0037 #include "G4ParticleGun.hh"
0038 #include "G4ParticleTable.hh"
0039 #include "G4PhysicalConstants.hh"
0040 #include "G4SystemOfUnits.hh"
0041 #include "G4TouchableHandle.hh"
0042 #include "G4ios.hh"
0043 #include "Randomize.hh"
0044
0045
0046
0047 F04PrimaryGeneratorAction::F04PrimaryGeneratorAction(F04DetectorConstruction* detectorConstruction)
0048 : fDetector(detectorConstruction)
0049 {
0050 G4int n_particle = 1;
0051 fParticleGun = new G4ParticleGun(n_particle);
0052
0053 fGunMessenger = new F04PrimaryGeneratorMessenger(this);
0054
0055 G4String particleName;
0056 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
0057
0058 fParticleGun->SetParticleDefinition(particleTable->FindParticle(particleName = "proton"));
0059 fParticleGun->SetParticleEnergy(500. * MeV);
0060 fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0061
0062 fZvertex = -0.5 * (fDetector->GetTargetThickness());
0063 fParticleGun->SetParticlePosition(G4ThreeVector(fXvertex, fYvertex, fZvertex));
0064 }
0065
0066
0067
0068 F04PrimaryGeneratorAction::~F04PrimaryGeneratorAction()
0069 {
0070 delete fParticleGun;
0071 delete fGunMessenger;
0072 }
0073
0074
0075
0076 void F04PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0077 {
0078 if (!fFirst) {
0079 fFirst = true;
0080 G4ThreeVector direction(0.0, 0.0, 1.0);
0081
0082 G4Navigator* theNavigator =
0083 G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
0084 if (theNavigator->GetWorldVolume()) {
0085 auto aNavigator = new G4Navigator();
0086 aNavigator->SetWorldVolume(theNavigator->GetWorldVolume());
0087
0088 G4ThreeVector center(0., 0., 0.);
0089 aNavigator->LocateGlobalPointAndSetup(center, nullptr, false);
0090
0091 G4TouchableHandle touchable = aNavigator->CreateTouchableHistoryHandle();
0092
0093
0094 fGlobal2local = touchable->GetHistory()->GetTopTransform();
0095
0096 direction = fGlobal2local.Inverse().TransformAxis(direction);
0097 delete aNavigator;
0098 }
0099
0100 fParticleGun->SetParticleMomentumDirection(direction);
0101 }
0102
0103 G4double x0, y0, z0;
0104
0105 if (fVertexDefined) {
0106 x0 = fXvertex;
0107 y0 = fYvertex;
0108 z0 = fZvertex;
0109 }
0110 else {
0111 x0 = 0.;
0112 y0 = 0.;
0113 z0 = -0.5 * (fDetector->GetTargetThickness());
0114 }
0115
0116 G4double r0, phi0;
0117
0118 if (fRndmFlag == "on") {
0119 r0 = (fDetector->GetTargetRadius()) * std::sqrt(G4UniformRand());
0120 phi0 = twopi * G4UniformRand();
0121 x0 = r0 * std::cos(phi0);
0122 y0 = r0 * std::sin(phi0);
0123 }
0124
0125 G4ThreeVector localPosition(x0, y0, z0);
0126 G4ThreeVector globalPosition = fGlobal2local.Inverse().TransformPoint(localPosition);
0127
0128 fParticleGun->SetParticlePosition(globalPosition);
0129 fParticleGun->GeneratePrimaryVertex(anEvent);
0130 }
0131
0132
0133
0134 void F04PrimaryGeneratorAction::SetXvertex(G4double x)
0135 {
0136 fVertexDefined = true;
0137 fXvertex = x;
0138 G4cout << " X coordinate of the primary vertex = " << fXvertex / mm << " mm." << G4endl;
0139 }
0140
0141
0142
0143 void F04PrimaryGeneratorAction::SetYvertex(G4double y)
0144 {
0145 fVertexDefined = true;
0146 fYvertex = y;
0147 G4cout << " Y coordinate of the primary vertex = " << fYvertex / mm << " mm." << G4endl;
0148 }
0149
0150
0151
0152 void F04PrimaryGeneratorAction::SetZvertex(G4double z)
0153 {
0154 fVertexDefined = true;
0155 fZvertex = z;
0156 G4cout << " Z coordinate of the primary vertex = " << fZvertex / mm << " mm." << G4endl;
0157 }