Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:18

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