Back to home page

EIC code displayed by LXR

 
 

    


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

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