Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:22:01

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 PrimaryGeneratorAction.cc
0028 /// \brief Primary Generator for Molecular DNA simulation
0029 
0030 #include "PrimaryGeneratorAction.hh"
0031 
0032 #include "PrimaryGeneratorMessenger.hh"
0033 #include "PrimaryGeneratorSourceGRASCSV.hh"
0034 
0035 #include "G4GeneralParticleSource.hh"
0036 #include "G4ParticleGun.hh"
0037 
0038 // define a Mutex to avoid concurrent reading in multi-thread
0039 #include "G4AutoLock.hh"
0040 namespace
0041 {
0042 G4Mutex PrimaryGeneratorMutex = G4MUTEX_INITIALIZER;
0043 }
0044 
0045 // instance of PrimaryGeneratorSource
0046 PrimaryGeneratorSourceGRASCSV* PrimaryGeneratorAction::fPrimarySource = nullptr;
0047 
0048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 
0050 PrimaryGeneratorAction::PrimaryGeneratorAction()
0051 {
0052   G4AutoLock lock(&PrimaryGeneratorMutex);
0053   fParticleGun = new G4GeneralParticleSource();
0054 
0055   fFirstEvent = true;
0056   fGunMessenger = new PrimaryGeneratorMessenger(this);
0057 }
0058 
0059 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0060 
0061 PrimaryGeneratorAction::~PrimaryGeneratorAction()
0062 {
0063   G4AutoLock lock(&PrimaryGeneratorMutex);
0064   delete fParticleGun;
0065   if (fPrimarySource != nullptr) {
0066     delete fPrimarySource;
0067     fPrimarySource = nullptr;
0068   }
0069   delete fGunMessenger;
0070 }
0071 
0072 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0073 
0074 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0075 {
0076   if (fMyInputFileName != "") {
0077     G4AutoLock lock(&PrimaryGeneratorMutex);
0078     // Read primaries from file
0079     // Before first event, instantiate file reader if fInputFileName is not empty
0080     if (fFirstEvent) {
0081       fPrimarySource = new PrimaryGeneratorSourceGRASCSV(fMyInputFileName);
0082       fFirstEvent = false;
0083     }
0084 
0085     auto* fpParticleGun = new G4ParticleGun();
0086 
0087     // Get a new primary particle.
0088     Primary* primary = fPrimarySource->GetPrimary();
0089     if (primary != nullptr) {
0090       G4String particleName = primary->GetName();
0091       G4ThreeVector pos = primary->GetPosition();
0092       G4ThreeVector momdir = primary->GetMomentumDirection();
0093       G4double energy = primary->GetEnergy();
0094       G4ParticleDefinition* particle = primary->GetParticleDefinition();
0095       // primary->Print();  // print of the data of the primary particle
0096 
0097       fpParticleGun->SetParticleDefinition(particle);
0098       fpParticleGun->SetParticlePosition(pos);
0099       fpParticleGun->SetParticleEnergy(energy * CLHEP::MeV);
0100       fpParticleGun->SetParticleMomentumDirection(momdir);
0101       fpParticleGun->GeneratePrimaryVertex(anEvent);
0102     }
0103     else {
0104       // If primary is NULL, the end of file has been reached or the file format is not consistent
0105       // A Geantino placed at kInfinity will be fired in this case
0106       G4cout << "WARNING: The phase space source is ended. Maybe you reach the end of file, or "
0107                 "your file is broken. A Geantino will be generated."
0108              << G4endl;
0109       G4ParticleTable* pTable = G4ParticleTable::GetParticleTable();
0110       G4ParticleDefinition* particle = pTable->FindParticle("geantino");
0111       fpParticleGun->SetParticleDefinition(particle);
0112       G4ThreeVector pos = G4ThreeVector(kInfinity, kInfinity, kInfinity);
0113       fpParticleGun->SetParticlePosition(pos);
0114       fpParticleGun->SetParticleEnergy(0);
0115       fpParticleGun->SetParticleMomentumDirection(pos);
0116       fpParticleGun->GeneratePrimaryVertex(anEvent);
0117     }
0118   }
0119   else {
0120     fParticleGun->GeneratePrimaryVertex(anEvent);
0121   }
0122 }
0123 
0124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......