Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-15 08:29:04

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 #include "PrimaryGeneratorAction.hh"
0030 
0031 #include "DetectorConstruction.hh"
0032 
0033 #include "G4Electron.hh"
0034 #include "G4Event.hh"
0035 #include "G4Gamma.hh"
0036 #include "G4ParticleDefinition.hh"
0037 #include "G4ParticleGun.hh"
0038 #include "G4ParticleTable.hh"
0039 #include "G4PhysicalConstants.hh"
0040 #include "G4RandomDirection.hh"
0041 #include "G4RunManager.hh"
0042 #include "G4StateManager.hh"
0043 #include "G4SystemOfUnits.hh"
0044 #include "Randomize.hh"
0045 
0046 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0047 
0048 PrimaryGeneratorAction::PrimaryGeneratorAction() {
0049   fDetector = dynamic_cast<const DetectorConstruction *>(
0050     G4RunManager::GetRunManager()->GetUserDetectorConstruction());
0051   fParticleGun = std::make_unique<G4ParticleGun>(1);
0052 }
0053 
0054 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0055 
0056 PrimaryGeneratorAction::~PrimaryGeneratorAction() {
0057   G4StateManager::GetStateManager()->DeregisterDependent(this);
0058 }
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0061 
0062 void PrimaryGeneratorAction::GeneratePrimaries(G4Event *anEvent) {
0063   const G4double R = fDetector->GetNPRadius();
0064   G4double Ryz = 9999;
0065 
0066   // G4ParticleDefinition *particle = G4Electron::ElectronDefinition();
0067   // fParticleGun->SetParticleDefinition(particle);
0068 
0069   constexpr G4double scale = 1.;
0070 
0071   G4double xpos = 0, ypos = 0, zpos = 0;
0072   while (!(Ryz < R)) {
0073     ypos = (2. * G4UniformRand() - 1.) * R;
0074     zpos = (2. * G4UniformRand() - 1.) * R;
0075     Ryz = std::sqrt(ypos * ypos + zpos * zpos);
0076   }
0077   xpos = std::sqrt(R * R - (ypos * ypos + zpos * zpos));
0078   fParticleGun->SetParticlePosition(G4ThreeVector(-xpos * scale,
0079                                                   ypos * scale,
0080                                                   zpos * scale));
0081   fParticleGun->SetParticleMomentumDirection(G4ThreeVector(1, 0, 0));
0082 
0083   fParticleGun->GeneratePrimaryVertex(anEvent);
0084 }
0085 
0086 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0087 
0088 G4bool PrimaryGeneratorAction::Notify(G4ApplicationState requestedState) {
0089   if (requestedState == G4State_Idle) {
0090     if (fParticleGun != nullptr) return true;
0091     const G4double R = fDetector->GetNPRadius();
0092     G4double Ryz = 9999;
0093 
0094     // G4ParticleDefinition *particle = G4Electron::ElectronDefinition();
0095     // fParticleGun->SetParticleDefinition(particle);
0096 
0097     fParticleGun->SetParticleMomentumDirection(G4ThreeVector(-1, 0, 0));
0098 
0099     while (!(Ryz < R)) {
0100       const G4double ypos = (2. * G4UniformRand() - 1.) * R;
0101       const G4double zpos = (2. * G4UniformRand() - 1.) * R;
0102       Ryz = std::sqrt(ypos * ypos + zpos * zpos);
0103       const G4double xpos = std::sqrt(R * R - (ypos * ypos + zpos * zpos));
0104       fParticleGun->SetParticlePosition(G4ThreeVector(-xpos, ypos, zpos));
0105     }
0106     // fParticleGun->SetParticlePosition(G4ThreeVector(-Xposition,0,0));
0107     // fParticleGun->GeneratePrimaryVertex(anEvent);
0108   }
0109 
0110   return true;
0111 }