File indexing completed on 2026-04-03 07:52:51
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
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 #include "MyPrimaryGeneratorActionFromFile.hh"
0045
0046 #include "DetectorConstruction.hh"
0047 #include "MyFileReader.hh"
0048
0049 #include "G4RandomDirection.hh"
0050 #include "G4RunManager.hh"
0051 #include "G4StateManager.hh"
0052 #include "G4SystemOfUnits.hh"
0053
0054 namespace
0055 {
0056 G4Mutex myPrimGenMutex = G4MUTEX_INITIALIZER;
0057 }
0058
0059 MyFileReader* MyPrimaryGeneratorActionFromFile::fileReader = 0;
0060
0061
0062
0063 MyPrimaryGeneratorActionFromFile::MyPrimaryGeneratorActionFromFile()
0064 : G4VUserPrimaryGeneratorAction(), G4VStateDependent(), fParticleGun(0), fDetector(0)
0065 {
0066 fDetector = dynamic_cast<const DetectorConstruction*>(
0067 G4RunManager::GetRunManager()->GetUserDetectorConstruction());
0068
0069 G4AutoLock lock(&myPrimGenMutex);
0070
0071 if (!fileReader) fileReader = new MyFileReader();
0072
0073 fParticleGun = new G4ParticleGun(1);
0074 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
0075 G4ParticleDefinition* particle = particleTable->FindParticle("e-");
0076 fParticleGun->SetParticleDefinition(particle);
0077 fParticleGun->SetParticleEnergy(10. * MeV);
0078 }
0079
0080
0081
0082 MyPrimaryGeneratorActionFromFile::~MyPrimaryGeneratorActionFromFile()
0083 {
0084 G4AutoLock lock(&myPrimGenMutex);
0085 if (fileReader) {
0086 delete fileReader;
0087 fileReader = 0;
0088 }
0089
0090 G4StateManager::GetStateManager()->DeregisterDependent(this);
0091 if (fParticleGun) delete fParticleGun;
0092 }
0093
0094
0095
0096 void MyPrimaryGeneratorActionFromFile::GeneratePrimaries(G4Event* anEvent)
0097 {
0098
0099
0100 G4double nrj = 0;
0101 if (fileReader) {
0102 G4AutoLock lock(&myPrimGenMutex);
0103 nrj = fileReader->GetAnEvent();
0104 }
0105 fParticleGun->SetParticleEnergy(nrj * eV);
0106
0107
0108 G4double thickness = fDetector->GetCytoThickness();
0109 G4double radius = fDetector->GetNuclRadius();
0110
0111 G4double rx = 1 * m;
0112 G4double ry = 1 * m;
0113 G4double rz = 1 * m;
0114 G4double myRadius = 0;
0115
0116 do {
0117 rx = (2 * G4UniformRand() - 1) * (radius + thickness) * 1.01;
0118 ry = (2 * G4UniformRand() - 1) * (radius + thickness) * 1.01;
0119 rz = (2 * G4UniformRand() - 1) * (radius + thickness) * 1.01;
0120 myRadius = std::sqrt(rx * rx + ry * ry + rz * rz);
0121
0122 } while (myRadius > radius + thickness || myRadius < radius);
0123
0124 fParticleGun->SetParticlePosition(G4ThreeVector(rx, ry, rz));
0125 fParticleGun->SetParticleMomentumDirection(G4RandomDirection());
0126
0127
0128
0129
0130 fParticleGun->GeneratePrimaryVertex(anEvent);
0131 }
0132
0133
0134
0135 G4bool MyPrimaryGeneratorActionFromFile::Notify(G4ApplicationState requestedState)
0136 {
0137 if (requestedState == G4State_Idle) {
0138 if (fParticleGun != 0) return true;
0139
0140 fParticleGun = new G4ParticleGun(1);
0141
0142
0143
0144 G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle("e-");
0145 fParticleGun->SetParticleDefinition(particle);
0146 fParticleGun->SetParticleEnergy(300 * eV);
0147 fParticleGun->SetParticlePosition(G4ThreeVector());
0148 fParticleGun->SetParticleMomentumDirection(G4ThreeVector(1, 0, 0));
0149 }
0150
0151 return true;
0152 }