Exercises and Solutions - Day 2 (May 20th)

Exercises: Exercises of the day

Exercises and solutions - Part IV

The solutions are indicated in red.

Part IV - Primary generator

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

  1. for each event, draw a random number x in (0,1) using G4UniformRand(), and shoot an electron if x<0.5 and a positron (G4Positron) if x>0.5
  2. what does it happen if you generate a positron at rest (namely zero kinetic energy)?

#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
If one shoots a positron with zero kinetic energy, it is captured in the material after a certain time, and it produces two annihilation photons. In fact the positron annihilation physics process implements AtRest and PostStep actions (see class tomorrow).

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 (notice: what does "isotropic mean"? How should theta and phi be sampled?)

#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