File indexing completed on 2025-02-23 09:21:46
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 #include "Dicom2PrimaryGeneratorAction.hh"
0031
0032 #include "DicomDetectorConstruction.hh"
0033
0034 #include "G4Box.hh"
0035 #include "G4LogicalVolume.hh"
0036 #include "G4LogicalVolumeStore.hh"
0037 #include "G4ParticleDefinition.hh"
0038 #include "G4ParticleGun.hh"
0039 #include "G4ParticleTable.hh"
0040 #include "G4PhysicalVolumeStore.hh"
0041 #include "G4RandomDirection.hh"
0042 #include "G4RunManager.hh"
0043 #include "G4SystemOfUnits.hh"
0044 #include "G4UnitsTable.hh"
0045 #include "G4VPhysicalVolume.hh"
0046 #include "Randomize.hh"
0047
0048
0049
0050 Dicom2PrimaryGeneratorAction::Dicom2PrimaryGeneratorAction()
0051 : G4VUserPrimaryGeneratorAction(),
0052 fParticleGun(new G4ParticleGun(1)),
0053 fEnvelopeBox(nullptr),
0054 fEnvelopeVol(nullptr),
0055 fPosCenter(G4ThreeVector(0.0)),
0056 fPosDelta(G4ThreeVector(0.0)),
0057 fGeomFactor(0.8)
0058 {
0059
0060 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
0061 G4String particleName;
0062 G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "e-");
0063 fParticleGun->SetParticleDefinition(particle);
0064 fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0065 fParticleGun->SetParticleEnergy(10. * MeV);
0066 }
0067
0068
0069
0070 Dicom2PrimaryGeneratorAction::~Dicom2PrimaryGeneratorAction()
0071 {
0072 delete fParticleGun;
0073 }
0074
0075
0076
0077 void Dicom2PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0078 {
0079
0080
0081
0082
0083
0084
0085 if (!fEnvelopeBox) {
0086 G4LogicalVolume* envLV = G4LogicalVolumeStore::GetInstance()->GetVolume("phantomContainer");
0087 if (envLV) fEnvelopeBox = dynamic_cast<G4Box*>(envLV->GetSolid());
0088 }
0089
0090 if (!fEnvelopeVol) {
0091 fEnvelopeVol = G4PhysicalVolumeStore::GetInstance()->GetVolume("phantomContainer");
0092 }
0093
0094
0095 if (fEnvelopeVol) {
0096 fPosCenter = fEnvelopeVol->GetObjectTranslation();
0097 }
0098 else {
0099 G4ExceptionDescription msg;
0100 msg << "Envelope physical volume not found.\n";
0101 msg << "The gun will be place at the center.";
0102 G4Exception("Dicom2PrimaryGeneratorAction::GeneratePrimaries()", "DICOM20002", JustWarning,
0103 msg);
0104 }
0105
0106
0107 if (fEnvelopeBox) {
0108 fPosDelta =
0109 G4ThreeVector(2.0 * fEnvelopeBox->GetXHalfLength(), 2.0 * fEnvelopeBox->GetYHalfLength(),
0110 2.0 * fEnvelopeBox->GetZHalfLength());
0111 }
0112 else {
0113 G4ExceptionDescription msg;
0114 msg << "Envelope volume of box shape not found.\n";
0115 msg << "The gun will be place at the center.";
0116 G4Exception("Dicom2PrimaryGeneratorAction::GeneratePrimaries()", "DICOM20003", JustWarning,
0117 msg);
0118 }
0119
0120
0121 G4ThreeVector pos = fPosCenter;
0122
0123 G4ThreeVector delta(fPosDelta.x() * (G4UniformRand() - 0.5),
0124 fPosDelta.y() * (G4UniformRand() - 0.5),
0125 fPosDelta.z() * (G4UniformRand() - 0.5));
0126
0127 delta *= fGeomFactor;
0128
0129
0130
0131
0132
0133
0134
0135 fParticleGun->SetParticlePosition(pos + delta);
0136 fParticleGun->SetParticleMomentumDirection(G4RandomDirection());
0137 fParticleGun->GeneratePrimaryVertex(anEvent);
0138 }
0139
0140