Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-24 08:14:00

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 F04PrimaryGeneratorAction.cc
0027 /// \brief Implementation of the F04PrimaryGeneratorAction class
0028 
0029 #include "F04PrimaryGeneratorAction.hh"
0030 
0031 #include "F04DetectorConstruction.hh"
0032 #include "F04PrimaryGeneratorMessenger.hh"
0033 
0034 #include "G4Event.hh"
0035 #include "G4GeometryManager.hh"
0036 #include "G4ParticleDefinition.hh"
0037 #include "G4ParticleGun.hh"
0038 #include "G4ParticleTable.hh"
0039 #include "G4PhysicalConstants.hh"
0040 #include "G4SystemOfUnits.hh"
0041 #include "G4TouchableHandle.hh"
0042 #include "G4ios.hh"
0043 #include "Randomize.hh"
0044 
0045 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0046 
0047 F04PrimaryGeneratorAction::F04PrimaryGeneratorAction(F04DetectorConstruction* detectorConstruction)
0048   : fDetector(detectorConstruction)
0049 {
0050   G4int n_particle = 1;
0051   fParticleGun = new G4ParticleGun(n_particle);
0052 
0053   fGunMessenger = new F04PrimaryGeneratorMessenger(this);
0054 
0055   G4String particleName;
0056   G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
0057 
0058   fParticleGun->SetParticleDefinition(particleTable->FindParticle(particleName = "proton"));
0059   fParticleGun->SetParticleEnergy(500. * MeV);
0060   fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0061 
0062   fZvertex = -0.5 * (fDetector->GetTargetThickness());
0063   fParticleGun->SetParticlePosition(G4ThreeVector(fXvertex, fYvertex, fZvertex));
0064 }
0065 
0066 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0067 
0068 F04PrimaryGeneratorAction::~F04PrimaryGeneratorAction()
0069 {
0070   delete fParticleGun;
0071   delete fGunMessenger;
0072 }
0073 
0074 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0075 
0076 void F04PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0077 {
0078   if (!fFirst) {
0079     fFirst = true;
0080     G4ThreeVector direction(0.0, 0.0, 1.0);
0081 
0082     G4Navigator* theNavigator =
0083       G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
0084     if (theNavigator->GetWorldVolume()) {
0085       auto aNavigator = new G4Navigator();
0086       aNavigator->SetWorldVolume(theNavigator->GetWorldVolume());
0087 
0088       G4ThreeVector center(0., 0., 0.);
0089       aNavigator->LocateGlobalPointAndSetup(center, nullptr, false);
0090 
0091       G4TouchableHandle touchable = aNavigator->CreateTouchableHistoryHandle();
0092 
0093       // set Global2local transform
0094       fGlobal2local = touchable->GetHistory()->GetTopTransform();
0095 
0096       direction = fGlobal2local.Inverse().TransformAxis(direction);
0097       delete aNavigator;
0098     }
0099 
0100     fParticleGun->SetParticleMomentumDirection(direction);
0101   }
0102 
0103   G4double x0, y0, z0;
0104 
0105   if (fVertexDefined) {
0106     x0 = fXvertex;
0107     y0 = fYvertex;
0108     z0 = fZvertex;
0109   }
0110   else {
0111     x0 = 0.;
0112     y0 = 0.;
0113     z0 = -0.5 * (fDetector->GetTargetThickness());
0114   }
0115 
0116   G4double r0, phi0;
0117 
0118   if (fRndmFlag == "on") {
0119     r0 = (fDetector->GetTargetRadius()) * std::sqrt(G4UniformRand());
0120     phi0 = twopi * G4UniformRand();
0121     x0 = r0 * std::cos(phi0);
0122     y0 = r0 * std::sin(phi0);
0123   }
0124 
0125   G4ThreeVector localPosition(x0, y0, z0);
0126   G4ThreeVector globalPosition = fGlobal2local.Inverse().TransformPoint(localPosition);
0127 
0128   fParticleGun->SetParticlePosition(globalPosition);
0129   fParticleGun->GeneratePrimaryVertex(anEvent);
0130 }
0131 
0132 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0133 
0134 void F04PrimaryGeneratorAction::SetXvertex(G4double x)
0135 {
0136   fVertexDefined = true;
0137   fXvertex = x;
0138   G4cout << " X coordinate of the primary vertex = " << fXvertex / mm << " mm." << G4endl;
0139 }
0140 
0141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0142 
0143 void F04PrimaryGeneratorAction::SetYvertex(G4double y)
0144 {
0145   fVertexDefined = true;
0146   fYvertex = y;
0147   G4cout << " Y coordinate of the primary vertex = " << fYvertex / mm << " mm." << G4endl;
0148 }
0149 
0150 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0151 
0152 void F04PrimaryGeneratorAction::SetZvertex(G4double z)
0153 {
0154   fVertexDefined = true;
0155   fZvertex = z;
0156   G4cout << " Z coordinate of the primary vertex = " << fZvertex / mm << " mm." << G4endl;
0157 }