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