Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-17 07:41:12

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 SingleParticleGun.cc
0027 /// \brief Implementation of the SingleParticleGun class
0028 ///
0029 /// \author J. Yarba; FNAL
0030 
0031 #include "SingleParticleGun.hh"
0032 
0033 #include "G4Event.hh"
0034 #include "G4ParticleDefinition.hh"
0035 #include "G4ParticleGun.hh"
0036 #include "G4ParticleTable.hh"
0037 #include "G4ThreeVector.hh"
0038 #include "Randomize.hh"
0039 
0040 // SPECIAL CASE - needed for treatment of polarization for tau's
0041 #include "G4TauMinus.hh"
0042 #include "G4TauPlus.hh"
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0045 
0046 SingleParticleGun::SingleParticleGun(const G4String& pname, const double pmom)
0047   : G4VUserPrimaryGeneratorAction(), fGun(nullptr), fMomentum(pmom)
0048 {
0049   int nparts = 1;
0050   fGun = new G4ParticleGun(nparts);
0051 
0052   G4ParticleTable* pdt = G4ParticleTable::GetParticleTable();
0053   G4ParticleDefinition* pd = pdt->FindParticle(pname);
0054   fGun->SetParticleDefinition(pd);
0055   fGun->SetParticlePosition(G4ThreeVector(0., 0., 0.));
0056   double mass = pd->GetPDGMass();
0057   double energy = std::sqrt(fMomentum * fMomentum + mass * mass);
0058   fGun->SetParticleEnergy(energy);
0059 
0060   // NOTE: do not set momentum directions
0061   //       as it'll be generated event-by-event
0062 }
0063 
0064 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0065 
0066 SingleParticleGun::~SingleParticleGun()
0067 {
0068   delete fGun;
0069 }
0070 
0071 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0072 
0073 void SingleParticleGun::GeneratePrimaries(G4Event* evt)
0074 {
0075   if (!fGun) {
0076     G4cout << " ParticleGun is NOT set up; BAIL OUT from this event " << G4endl;
0077     G4cout << " Check if you are using ctor "
0078            << "SinglePartGun(const G4string& partname, const double pmom ) " << G4endl;
0079     return;
0080   }
0081 
0082   double phi = -1. * CLHEP::pi + CLHEP::twopi * G4UniformRand();
0083   double theta = CLHEP::pi / 4. + CLHEP::halfpi * G4UniformRand();
0084 
0085   double x = std::sin(theta) * std::cos(phi);
0086   double y = std::sin(theta) * std::sin(phi);
0087   double z = std::cos(theta);
0088 
0089   fGun->SetParticleMomentumDirection(G4ThreeVector(x, y, z));
0090 
0091   // SPECIAL CASE: for particle such as tau polarization should be -1 * p3
0092   //               while for tau_bar it should be +1
0093   //
0094   // NOTE: one could probably compare by name but that'd mean comparing strings...
0095   //
0096   if (fGun->GetParticleDefinition() == G4TauMinus::TauMinus()) {
0097     fGun->SetParticlePolarization(G4ThreeVector(-1.0 * x, -1.0 * y, -1.0 * z));
0098   }
0099   else if (fGun->GetParticleDefinition() == G4TauPlus::TauPlus()) {
0100     fGun->SetParticlePolarization(G4ThreeVector(x, y, z));
0101   }
0102 
0103   fGun->GeneratePrimaryVertex(evt);
0104 
0105   return;
0106 }
0107 
0108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......