The solutions are indicated in blue.
Exercise 2-6
In the default case, the primary particle is a 1-MeV electron which impinges perpendicularly on a side of the detector
box. The impinging angle can be changed interactively by the command /source/incidentAngle
#include "G4Positron.hh" ... void PrimaryGenerator::GeneratePrimaries(G4Event* event) { .. G4double randomNumber = G4UniformRand(); if (randomNumber > 0.5) particleGun->SetParticleDefinition(G4Electron::ElectronDefinition()); else particleGun->SetParticleDefinition(G4Positron::PositronDefinition()); }Source code
Exercise 2-7
Replace the "pencil beam" defined by default with a isotropic point source. The source should be
placed 1 cm above the centre of the detector (on z axis). It emits 662-keV gamma-rays
with isotropic angular distribution.
#include "G4Gamma.hh" ... void PrimaryGenerator::GeneratePrimaries(G4Event* event) { ... kineticEnergy = 662.0*keV; particleGun->SetParticleEnergy(kineticEnergy); particleGun->SetParticleDefinition(G4Gamma::GammaDefinition()); // the detector has z coordinate going from 0 to 4 cm (0 to boxLength), // therefore we place it at (0.,0.,5.0*cm) G4double zOfSource = 5.0*cm; particleGun->SetParticlePosition(G4ThreeVector(0.,0.,zOfSource)); G4double cosTheta = -1.0 + 2.0*G4UniformRand(); G4double phi = twopi*G4UniformRand(); G4double sinTheta = sqrt(1. - cosTheta*cosTheta); // these are the cosines for an isotropic direction particleGun -> SetParticleMomentumDirection(G4ThreeVector(sinTheta*cos(phi), sinTheta*sin(phi), cosTheta)); particleGun -> GeneratePrimaryVertex(event); }Source code
Exercise 2-8
Generate a point source, as before, emitting gamma-rays of 122 keV (branching ratio: 86%) and 136 keV
(branching ratio: 14%)
void PrimaryGenerator::GeneratePrimaries(G4Event* event) { ... G4double kineticEnergy1 = 122*keV; G4double kineticEnergy2 = 136*keV; G4double branchingRatio = 0.86; particleGun ->SetParticleDefinition(G4Gamma::GammaDefinition()); if (G4UniformRand() > branchingRatio) kineticEnergy = kineticEnergy1; else kineticEnergy = kineticEnergy2; particleGun -> SetParticleEnergy(kineticEnergy); // the detector has z coordinate going from 0 to 4 cm (0 to boxLength), // therefore we place it at (0,0,5.0*cm) G4double zOfSource = 5.0*cm; particleGun -> SetParticlePosition(G4ThreeVector(0.,0.,zOfSource)); G4double cosTheta = -1.0 + 2.0 * G4UniformRand(); G4double phi = twopi*G4UniformRand(); G4double sinTheta = sqrt(1. - cosTheta*cosTheta); // these are the cosines for an isotropic direction particleGun -> SetParticleMomentumDirection(G4ThreeVector(sinTheta*cos(phi), sinTheta*sin(phi), cosTheta)); particleGun -> GeneratePrimaryVertex(event); }Source code