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.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 //
 27 /// \file PrimaryGeneratorAction.cc
 28 /// \brief Implementation of the PrimaryGeneratorAction class
 29 
 30 #include "PrimaryGeneratorAction.hh"
 31 
 32 #include "G4Event.hh"
 33 #include "G4ParticleGun.hh"
 34 #include "G4PhysicalConstants.hh"
 35 #include "G4SystemOfUnits.hh"
 36 #include "Randomize.hh"
 37 
 38 #include <fstream>
 39 
 40 PrimaryGeneratorAction::PrimaryGeneratorAction(DetectorConstruction* detector)
 41   : G4VUserPrimaryGeneratorAction(), fDetector(detector)
 42 {
 43   fpParticleGun = std::make_unique<G4ParticleGun>();
 44   fpParticleGun->SetParticleEnergy(-1);  // default value - can be overridden in the macro file
 45 }
 46 
 47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 48 
 49 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
 50 {
 51   // user can set the energy in the macro file
 52   G4double energy = fpParticleGun->GetParticleEnergy();
 53 
 54   // only first check is important as only user can set energy to -1,
 55   // hopefully...
 56   if (energy == -1) {  // if energy is larger than zero, then the beam is
 57                        // mono-energetic if the energy is set to zero, then the
 58                        // energy is randomized, based on spectrum file
 59     fMonoEnergetic = false;
 60   }
 61 
 62   if (!fMonoEnergetic) {
 63     energy = GenerateParticleEnergy();
 64   }
 65 
 66   fpParticleGun->SetParticleEnergy(energy);
 67   fpParticleGun->SetParticlePosition(GenerateParticlePosition());  // point of emission
 68   fpParticleGun->SetParticleMomentumDirection(
 69     GenerateParticleDirection());  // direction of emission
 70 
 71   fpParticleGun->GeneratePrimaryVertex(anEvent);  // sending the particle
 72 }
 73 
 74 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 75 
 76 G4double PrimaryGeneratorAction::GenerateParticleEnergy()
 77 {
 78   if (fEnergySpectrum_length == 0) {  // reading the spectrum file if not loaded yet
 79     std::ifstream fin(fEnergySpectrumFilename);
 80     G4String len, gain, offset, counts;
 81     fin >> len >> gain >> offset;
 82     fEnergySpectrum_length = std::stoi(len);
 83     fEnergySpectrum_gain = std::stod(gain);
 84     fEnergySpectrum_offset = std::stod(offset);
 85 
 86     fEnergySpectrum_counts.resize(fEnergySpectrum_length);
 87     for (G4int i = 0; i < fEnergySpectrum_length; i++) {
 88       fin >> counts;
 89       fEnergySpectrum_counts[i] = std::stoi(counts);
 90     }
 91   }
 92 
 93   auto max_en_counter = fEnergySpectrum_counts[fEnergySpectrum_length - 1] - 1;
 94   auto rand_count = 1 + G4int(G4UniformRand() * max_en_counter);
 95 
 96   // primitive search for lowest en_id that gives higher count value than
 97   // rand_count:
 98   G4int en_id = 0;
 99   for (; rand_count > fEnergySpectrum_counts[en_id]; en_id++)
100     ;
101 
102   G4int left = fEnergySpectrum_counts[en_id - 1];
103   G4int right = fEnergySpectrum_counts[en_id];
104 
105   G4double en_left = (en_id - 1) * fEnergySpectrum_gain + fEnergySpectrum_offset;
106   G4double en_right = en_id * fEnergySpectrum_gain + fEnergySpectrum_offset;
107 
108   // linear interpolation:
109   G4double slope = (en_right - en_left) / (right - left);
110   return (en_left + slope * (rand_count - left)) * MeV;
111 }
112 
113 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
114 
115 G4ThreeVector PrimaryGeneratorAction::GenerateParticlePosition()
116 {
117   // the source is an infinitely thin disk of radius r
118   G4double r = std::sqrt(G4UniformRand()) * fDetector->GetCollDiameter() / 2.;
119   G4double phi = G4UniformRand() * twopi;
120 
121   G4double x = fDetector->GetCollExitPosistion() - fDetector->GetCollLength();
122   G4double y = r * std::cos(phi);
123   G4double z = r * std::sin(phi);
124 
125   return {x, y, z};
126 }
127 
128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
129 
130 G4ThreeVector PrimaryGeneratorAction::GenerateParticleDirection()
131 {
132   G4double phi, theta;
133   G4double px, py, pz;
134 
135   phi = G4UniformRand() * twopi;
136 
137   // For convenience theta is measured along the beam axis, which is x-axis.
138   // To reduce the number of events, when the projectile hits the collimator,
139   // only forward angles in the range [0, max_theta] are considered.
140   // If theta is larger, then the projectile will hit the collimator anyway.
141   // Even with this restriction only 1 in 4 projectiles pass through the
142   // collimator.
143   G4double cos_max_theta =
144     std::cos(std::atan(fDetector->GetCollDiameter() / fDetector->GetCollLength()));
145   theta = std::acos(cos_max_theta + G4UniformRand() * (1 - cos_max_theta));
146 
147   px = std::cos(theta);
148   py = std::sin(theta) * std::sin(phi);
149   pz = std::sin(theta) * std::cos(phi);
150 
151   return {px, py, pz};
152 }
153 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
154