// // ******************************************************************** // * License and Disclaimer * // * * // * The Geant4 software is copyright of the Copyright Holders of * // * the Geant4 Collaboration. It is provided under the terms and * // * conditions of the Geant4 Software License, included in the file * // * LICENSE and available at http://cern.ch/geant4/license . These * // * include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GEANT4 collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the Geant4 Software license. * // ******************************************************************** // // // //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... #include "PrimaryGeneratorAction.hh" #include "FileReader.hh" #include "G4RunManager.hh" #include "G4Event.hh" #include "G4AutoLock.hh" #include "G4ParticleTable.hh" #include "G4ParticleDefinition.hh" #include "Randomize.hh" #include "G4GeneralParticleSource.hh" #include "G4ParticleGun.hh" #include "G4PhysicalConstants.hh" #include "G4SystemOfUnits.hh" #include "globals.hh" namespace {G4Mutex PrimaryGeneratorActionMutex = G4MUTEX_INITIALIZER;} FileReader* PrimaryGeneratorAction::fFileReader = 0; //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... PrimaryGeneratorAction::PrimaryGeneratorAction(): fReadFromFile(0) { G4cout << "### PrimaryGeneratorAction instantiated ###" << G4endl; //Primary particles G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle("gamma"); //defining a GPS (it is used in batch mode if fReadFromFile=0) fGPS = new G4GeneralParticleSource(); fGPS->SetParticleDefinition(particle); //Spatial distribution G4SPSPosDistribution* vPosDist = fGPS->GetCurrentSource()->GetPosDist(); vPosDist->SetPosDisType("Plane"); vPosDist->SetPosDisShape("Circle"); vPosDist->SetRadius(50.*mm); vPosDist->SetCentreCoords(G4ThreeVector(0.,0.,0.)); //Angular distribution G4SPSAngDistribution* vAngDist = fGPS->GetCurrentSource()->GetAngDist(); vAngDist->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.)); //Energy distribution G4SPSEneDistribution* vEneDist = fGPS->GetCurrentSource()->GetEneDist(); vEneDist->SetEnergyDisType("Mono"); vEneDist->SetMonoEnergy(20.*keV); //defining a Particle Gun (to be used with file reader) fGun = new G4ParticleGun(); fGun->SetParticleDefinition(particle); //options fReadFromFile = false; fFileName = "input/rect_source_1E7_2.dat"; IWantGPS = false; hLx = 50.; //mm hLy = 50.; //mm Nx = 100; Ny = 100; //root code for histogram of the generated points (it works only in single core) if (!fReadFromFile && !IWantGPS) { RGfile = new TFile("RGstats.root","recreate"); RGSpatialDistrib = new TH2D("RGSpatialDistrib","RGSpatialDistrib", Nx,-hLx,hLx,Ny,-hLy,hLy); } } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... PrimaryGeneratorAction::~PrimaryGeneratorAction() { G4AutoLock lock(&PrimaryGeneratorActionMutex); if (fFileReader) { delete fFileReader; fFileReader = 0; } delete fGPS; delete fGun; if (!fReadFromFile && !IWantGPS) { RGfile->Write(); RGfile->Close(); } } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... void PrimaryGeneratorAction::SetFileName(G4String vFileName) {fFileName = vFileName;} //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) { G4AutoLock lock(&PrimaryGeneratorActionMutex); if (!fFileReader) { fFileReader = new FileReader(fFileName); if (fReadFromFile) { G4cout << G4endl << "Read file ..." << G4endl; fFileReader->StoreEvents(); G4cout << "... file read" << G4endl << G4endl; } } if (fReadFromFile) { fGun->SetParticlePosition(fFileReader->GetAnEventPosition(anEvent->GetEventID())); //fGun->SetParticleMomentumDirection(fFileReader->GetAnEventMomentum(anEvent->GetEventID())); //fGun->SetParticleEnergy(fFileReader->GetAnEventEnergy(anEvent->GetEventID())); fGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.)); fGun->SetParticleEnergy(20.*keV); fGun->GeneratePrimaryVertex(anEvent); } else { if (IWantGPS) { fGPS->GeneratePrimaryVertex(anEvent); } else { G4double xs = -hLx + 2*hLx*G4UniformRand(); G4double ys = -hLy + 2*hLy*G4UniformRand(); fGun->SetParticlePosition(G4ThreeVector(xs, ys, 0.)*mm); fGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.)); fGun->SetParticleEnergy(20.*keV); RGSpatialDistrib->Fill(xs,ys,1); //fill the histogram fGun->GeneratePrimaryVertex(anEvent); } } } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......