Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-03 07:52:51

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 MyPrimaryGeneratorActionFromFile.cc
0027 /// \brief Implementation of the MyPrimaryGeneratorActionFromFile class
0028 
0029 // Derived from
0030 //  https://twiki.cern.ch/twiki/bin/view/Geant4/QuickMigrationGuideForGeant4V10
0031 // Courtesy of A. Dotti
0032 //
0033 // This example is provided by the Geant4-DNA collaboration
0034 // Any report or published results obtained using the Geant4-DNA software
0035 // shall cite the following Geant4-DNA collaboration publications:
0036 // Med. Phys. 45 (2018) e722-e739
0037 // Phys. Med. 31 (2015) 861-874
0038 // Med. Phys. 37 (2010) 4692-4708
0039 // Int. J. Model. Simul. Sci. Comput. 1 (2010) 157–178
0040 //
0041 // The Geant4-DNA web site is available at http://geant4-dna.org
0042 //
0043 
0044 #include "MyPrimaryGeneratorActionFromFile.hh"
0045 
0046 #include "DetectorConstruction.hh"
0047 #include "MyFileReader.hh"
0048 
0049 #include "G4RandomDirection.hh"
0050 #include "G4RunManager.hh"
0051 #include "G4StateManager.hh"
0052 #include "G4SystemOfUnits.hh"
0053 
0054 namespace
0055 {
0056 G4Mutex myPrimGenMutex = G4MUTEX_INITIALIZER;
0057 }
0058 
0059 MyFileReader* MyPrimaryGeneratorActionFromFile::fileReader = 0;
0060 
0061 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0062 
0063 MyPrimaryGeneratorActionFromFile::MyPrimaryGeneratorActionFromFile()
0064   : G4VUserPrimaryGeneratorAction(), G4VStateDependent(), fParticleGun(0), fDetector(0)
0065 {
0066   fDetector = dynamic_cast<const DetectorConstruction*>(
0067     G4RunManager::GetRunManager()->GetUserDetectorConstruction());
0068 
0069   G4AutoLock lock(&myPrimGenMutex);
0070 
0071   if (!fileReader) fileReader = new MyFileReader();
0072 
0073   fParticleGun = new G4ParticleGun(1);
0074   G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
0075   G4ParticleDefinition* particle = particleTable->FindParticle("e-");
0076   fParticleGun->SetParticleDefinition(particle);
0077   fParticleGun->SetParticleEnergy(10. * MeV);
0078 }
0079 
0080 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0081 
0082 MyPrimaryGeneratorActionFromFile::~MyPrimaryGeneratorActionFromFile()
0083 {
0084   G4AutoLock lock(&myPrimGenMutex);
0085   if (fileReader) {
0086     delete fileReader;
0087     fileReader = 0;
0088   }
0089 
0090   G4StateManager::GetStateManager()->DeregisterDependent(this);
0091   if (fParticleGun) delete fParticleGun;
0092 }
0093 
0094 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0095 
0096 void MyPrimaryGeneratorActionFromFile::GeneratePrimaries(G4Event* anEvent)
0097 {
0098   // Energy is read from file
0099 
0100   G4double nrj = 0;
0101   if (fileReader) {
0102     G4AutoLock lock(&myPrimGenMutex);
0103     nrj = fileReader->GetAnEvent();
0104   }
0105   fParticleGun->SetParticleEnergy(nrj * eV);
0106 
0107   //
0108   G4double thickness = fDetector->GetCytoThickness();
0109   G4double radius = fDetector->GetNuclRadius();
0110 
0111   G4double rx = 1 * m;
0112   G4double ry = 1 * m;
0113   G4double rz = 1 * m;
0114   G4double myRadius = 0;
0115 
0116   do {
0117     rx = (2 * G4UniformRand() - 1) * (radius + thickness) * 1.01;
0118     ry = (2 * G4UniformRand() - 1) * (radius + thickness) * 1.01;
0119     rz = (2 * G4UniformRand() - 1) * (radius + thickness) * 1.01;
0120     myRadius = std::sqrt(rx * rx + ry * ry + rz * rz);
0121 
0122   } while (myRadius > radius + thickness || myRadius < radius);
0123 
0124   fParticleGun->SetParticlePosition(G4ThreeVector(rx, ry, rz));
0125   fParticleGun->SetParticleMomentumDirection(G4RandomDirection());
0126 
0127   // G4cout << "---> EVENT ID=" << anEvent->GetEventID()+1 << G4endl;
0128   // G4cout << "---> energy/eV=" << nrj << G4endl;
0129 
0130   fParticleGun->GeneratePrimaryVertex(anEvent);
0131 }
0132 
0133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0134 
0135 G4bool MyPrimaryGeneratorActionFromFile::Notify(G4ApplicationState requestedState)
0136 {
0137   if (requestedState == G4State_Idle) {
0138     if (fParticleGun != 0) return true;
0139 
0140     fParticleGun = new G4ParticleGun(1);
0141 
0142     // Define default primary particle
0143 
0144     G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle("e-");
0145     fParticleGun->SetParticleDefinition(particle);
0146     fParticleGun->SetParticleEnergy(300 * eV);
0147     fParticleGun->SetParticlePosition(G4ThreeVector());
0148     fParticleGun->SetParticleMomentumDirection(G4ThreeVector(1, 0, 0));
0149   }
0150 
0151   return true;
0152 }