Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 08:38:14

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file PrimaryGeneratorAction.cc
0027 /// \brief Implementation of the PrimaryGeneratorAction class
0028 
0029 // This example is provided by the Geant4-DNA collaboration
0030 // Any report or published results obtained using the Geant4-DNA software
0031 // shall cite the following Geant4-DNA collaboration publication:
0032 // Med. Phys. 37 (2010) 4692-4708
0033 // and papers
0034 // M. Batmunkh et al. J Radiat Res Appl Sci 8 (2015) 498-507
0035 // O. Belov et al. Physica Medica 32 (2016) 1510-1520
0036 // The Geant4-DNA web site is available at http://geant4-dna.org
0037 //
0038 // -------------------------------------------------------------------
0039 // November 2016
0040 // -------------------------------------------------------------------
0041 //
0042 
0043 #include "PrimaryGeneratorAction.hh"
0044 
0045 #include "G4SystemOfUnits.hh"
0046 //
0047 #include "CommandLineParser.hh"
0048 
0049 #include "G4Box.hh"
0050 #include "G4Event.hh"
0051 #include "G4LogicalVolume.hh"
0052 #include "G4LogicalVolumeStore.hh"
0053 #include "G4Orb.hh"
0054 #include "G4ParticleDefinition.hh"
0055 #include "G4ParticleGun.hh"
0056 #include "G4PhysicalConstants.hh"
0057 #include "G4PhysicalVolumeStore.hh"
0058 #include "G4Proton.hh"
0059 #include "G4VPhysicalVolume.hh"
0060 #include "Randomize.hh"
0061 
0062 using namespace G4DNAPARSER;
0063 
0064 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0065 
0066 PrimaryGeneratorAction::PrimaryGeneratorAction() : G4VUserPrimaryGeneratorAction()
0067 {
0068   G4int n_particle = 1;
0069   fpParticleGun = new G4ParticleGun(n_particle);
0070   //
0071   G4ParticleDefinition* particle = G4Proton::Proton();
0072   fpParticleGun->SetParticleDefinition(particle);
0073   // default gun parameters
0074   fpParticleGun->SetParticleEnergy(10. * MeV);
0075   fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0076   fpParticleGun->SetParticlePosition(G4ThreeVector(0., 0., 0.));
0077 }
0078 
0079 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0080 
0081 PrimaryGeneratorAction::~PrimaryGeneratorAction()
0082 {
0083   delete fpParticleGun;
0084 }
0085 
0086 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0087 
0088 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0089 {
0090   // Initial kinetic energy of particles beam as Gaussion distribution!
0091 
0092   // G4double Ekin = 10.*MeV;
0093   // G4double deltaEkin = 9.0955e-5*MeV;
0094   // fpParticleGun->SetParticleEnergy(G4RandGauss::shoot(Ekin,deltaEkin));
0095 
0096   // In order to avoid dependence of PrimaryGeneratorAction
0097   // on DetectorConstruction class we get world volume
0098   // from G4LogicalVolumeStore
0099 
0100   // We included three options for particles direction:
0101 
0102   G4double mediumRadius = 0.;
0103   G4double boundingXHalfLength = 0.;
0104   G4double boundingYHalfLength = 0.;
0105   G4double boundingZHalfLength = 0.;
0106   G4LogicalVolume* mediumLV = G4LogicalVolumeStore::GetInstance()->GetVolume("Medium");
0107   G4LogicalVolume* boundingLV = G4LogicalVolumeStore::GetInstance()->GetVolume("BoundingSlice");
0108   G4Orb* mediumSphere = 0;
0109   G4Box* boundingSlice = 0;
0110   if (mediumLV && boundingLV) {
0111     mediumSphere = dynamic_cast<G4Orb*>(mediumLV->GetSolid());
0112     boundingSlice = dynamic_cast<G4Box*>(boundingLV->GetSolid());
0113   }
0114   if (mediumSphere && boundingSlice) {
0115     mediumRadius = mediumSphere->GetRadius();
0116     boundingXHalfLength = boundingSlice->GetXHalfLength();
0117     boundingYHalfLength = boundingSlice->GetYHalfLength();
0118     boundingZHalfLength = boundingSlice->GetZHalfLength();
0119 
0120     /// a) Partilces directed to "square" on the XY plane of bounding slice
0121     ///   (or YZ, XZ)
0122 
0123     if (CommandLineParser::GetParser()->GetCommandIfActive("-sXY")) {
0124       // INITIAL BEAM DIVERGENCE
0125       fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0126       // // INITIAL BEAM POSITION
0127       fpParticleGun->SetParticlePosition(G4ThreeVector(
0128         CLHEP::RandFlat::shoot(-boundingXHalfLength, boundingXHalfLength),
0129         CLHEP::RandFlat::shoot(-boundingYHalfLength, boundingYHalfLength), -mediumRadius));
0130     }
0131 
0132     /// b) Partilces directed to "disk" on the XY plane of
0133     ///    bounding slice (or YZ, XZ)
0134 
0135     else if (CommandLineParser::GetParser()->GetCommandIfActive("-dXY")) {
0136       fpParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0137       G4double x0, y0, z0;
0138       x0 = 100. * mm;
0139       y0 = 100. * mm;
0140       z0 = -mediumRadius;  // mediumRadius;
0141       while (!(std::sqrt(x0 * x0 + y0 * y0) <= mediumRadius)) {
0142         x0 = CLHEP::RandFlat::shoot(-mediumRadius, mediumRadius);
0143         y0 = CLHEP::RandFlat::shoot(-mediumRadius, mediumRadius);
0144       }
0145       fpParticleGun->SetParticlePosition(G4ThreeVector(x0, y0, z0));
0146     }
0147 
0148     /// c) Partilces directed towards the bounding slice (default option!)
0149     // Select a starting position on a sphere including the
0150     // target volume and neuron morphology
0151     else {
0152       G4double cosTheta = 2. * G4UniformRand() - 1;
0153       G4double sinTheta = std::sqrt(1. - cosTheta * cosTheta);
0154       G4double phi = twopi * G4UniformRand();
0155       G4ThreeVector positionStart(mediumRadius * sinTheta * std::cos(phi),
0156                                   mediumRadius * sinTheta * std::sin(phi), mediumRadius * cosTheta);
0157       fpParticleGun->SetParticlePosition(positionStart);
0158       // To compute the direction, select a point inside the target volume
0159       G4ThreeVector positionDir(boundingXHalfLength * (2. * G4UniformRand() - 1),
0160                                 boundingYHalfLength * (2. * G4UniformRand() - 1),
0161                                 boundingZHalfLength * (2. * G4UniformRand() - 1));
0162       fpParticleGun->SetParticleMomentumDirection((positionDir - positionStart).unit());
0163       // Surface area of sphere
0164       fGunArea = 4. * pi * mediumRadius * mediumRadius;
0165     }
0166   }
0167   else {
0168     G4cerr << "Bounding slice volume not found!" << G4endl;
0169     G4cerr << "Default particle kinematic used" << G4endl;
0170   }
0171 
0172   fpParticleGun->GeneratePrimaryVertex(anEvent);
0173 }