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