Back to home page

EIC code displayed by LXR

 
 

    


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

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 Dicom2PrimaryGeneratorAction.cc
0028 /// \brief Implementation of the Dicom2PrimaryGeneratorAction class
0029 
0030 #include "Dicom2PrimaryGeneratorAction.hh"
0031 
0032 #include "DicomDetectorConstruction.hh"
0033 
0034 #include "G4Box.hh"
0035 #include "G4LogicalVolume.hh"
0036 #include "G4LogicalVolumeStore.hh"
0037 #include "G4ParticleDefinition.hh"
0038 #include "G4ParticleGun.hh"
0039 #include "G4ParticleTable.hh"
0040 #include "G4PhysicalVolumeStore.hh"
0041 #include "G4RandomDirection.hh"
0042 #include "G4RunManager.hh"
0043 #include "G4SystemOfUnits.hh"
0044 #include "G4UnitsTable.hh"
0045 #include "G4VPhysicalVolume.hh"
0046 #include "Randomize.hh"
0047 
0048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 
0050 Dicom2PrimaryGeneratorAction::Dicom2PrimaryGeneratorAction()
0051   : G4VUserPrimaryGeneratorAction(),
0052     fParticleGun(new G4ParticleGun(1)),
0053     fEnvelopeBox(nullptr),
0054     fEnvelopeVol(nullptr),
0055     fPosCenter(G4ThreeVector(0.0)),
0056     fPosDelta(G4ThreeVector(0.0)),
0057     fGeomFactor(0.8)
0058 {
0059   // default particle kinematic
0060   G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
0061   G4String particleName;
0062   G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "e-");
0063   fParticleGun->SetParticleDefinition(particle);
0064   fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
0065   fParticleGun->SetParticleEnergy(10. * MeV);
0066 }
0067 
0068 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0069 
0070 Dicom2PrimaryGeneratorAction::~Dicom2PrimaryGeneratorAction()
0071 {
0072   delete fParticleGun;
0073 }
0074 
0075 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0076 
0077 void Dicom2PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
0078 {
0079   // this function is called at the begining of ecah event
0080   //
0081 
0082   // In order to avoid dependence of PrimaryGeneratorAction
0083   // on DetectorConstruction class we get phantomContainer volume
0084   // from G4LogicalVolumeStore.
0085   if (!fEnvelopeBox) {
0086     G4LogicalVolume* envLV = G4LogicalVolumeStore::GetInstance()->GetVolume("phantomContainer");
0087     if (envLV) fEnvelopeBox = dynamic_cast<G4Box*>(envLV->GetSolid());
0088   }
0089 
0090   if (!fEnvelopeVol) {
0091     fEnvelopeVol = G4PhysicalVolumeStore::GetInstance()->GetVolume("phantomContainer");
0092   }
0093 
0094   // get the center of the phantom
0095   if (fEnvelopeVol) {
0096     fPosCenter = fEnvelopeVol->GetObjectTranslation();
0097   }
0098   else {
0099     G4ExceptionDescription msg;
0100     msg << "Envelope physical volume not found.\n";
0101     msg << "The gun will be place at the center.";
0102     G4Exception("Dicom2PrimaryGeneratorAction::GeneratePrimaries()", "DICOM20002", JustWarning,
0103                 msg);
0104   }
0105 
0106   // get the size of the phantom
0107   if (fEnvelopeBox) {
0108     fPosDelta =
0109       G4ThreeVector(2.0 * fEnvelopeBox->GetXHalfLength(), 2.0 * fEnvelopeBox->GetYHalfLength(),
0110                     2.0 * fEnvelopeBox->GetZHalfLength());
0111   }
0112   else {
0113     G4ExceptionDescription msg;
0114     msg << "Envelope volume of box shape not found.\n";
0115     msg << "The gun will be place at the center.";
0116     G4Exception("Dicom2PrimaryGeneratorAction::GeneratePrimaries()", "DICOM20003", JustWarning,
0117                 msg);
0118   }
0119 
0120   // start at center
0121   G4ThreeVector pos = fPosCenter;
0122   // get a random displacement
0123   G4ThreeVector delta(fPosDelta.x() * (G4UniformRand() - 0.5),
0124                       fPosDelta.y() * (G4UniformRand() - 0.5),
0125                       fPosDelta.z() * (G4UniformRand() - 0.5));
0126   // scale by geom factor
0127   delta *= fGeomFactor;
0128 
0129   // G4cout << "Starting position: " << G4BestUnit(pos + delta, "Length")
0130   //        << ", pos = " << G4BestUnit(fPosCenter, "Length")
0131   //        << ", size = " << G4BestUnit(fPosDelta, "Length")
0132   //        << ", delta = " << G4BestUnit(delta, "Length")
0133   //        << G4endl;
0134 
0135   fParticleGun->SetParticlePosition(pos + delta);
0136   fParticleGun->SetParticleMomentumDirection(G4RandomDirection());
0137   fParticleGun->GeneratePrimaryVertex(anEvent);
0138 }
0139 
0140 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......