The solutions are indicated in blue.
Exercise 4-4
Introduce a conditional statement in the ProcessHits method of the sensitive detector, in order that hits are only created, filled and stored in the hits collection for
primary electron tracks:
if(step -> GetTrack() -> GetTrackID() == 1) {
DetectorHit* hit = new DetectorHit(); hit -> SetEnergyDeposit(energyDeposit); hit -> SetBinCenter(binCenter); hitsCollection -> insert(hit); }Click here for the source file.
Exercise 4-5
Modify the conditional statement of the previous example, in order that hits are only created for
photons (the primary electrons produce secondary photons):
if(step -> GetTrack() -> GetDefinition() == G4Gamma::Definition()) {
DetectorHit* hit = new DetectorHit(); hit -> SetEnergyDeposit(energyDeposit); hit -> SetBinCenter(binCenter); hitsCollection -> insert(hit); }Don't forget to include the header for the gamma particle definition. Click here for the source file.
Exercise 4-6
You can find here the header file for a user tracking action class (the Geant4 exercise example does not yet included a user tracking action). The aim is to complete the class implementation and to integrate the class into the existing application:
UserTrackingAction::UserTrackingAction() : counter(0) { }The user tracking action class holds the pointer to the tracking manager (fpTrackingManager). From the tracking manager we can retrieve the secondary track vector (i.e. a G4TrackVector object), which is a container class for tracks. To count the number of secondary electrons, we need to loop over all entries and check the particle type. Note that G4TrackVector is defined as:
typedef std::vector< G4Track* > G4TrackVector;You can thus use the standard template library (STL) functionality of the vector container class (you may also use iterators). The implementation of UserPostTrackingAction can be as follows:
void UserTrackingAction::UserPostTrackingAction(const G4Track*) { G4TrackVector* secTracks = fpTrackingManager -> GimmeSecondaries(); if(secTracks) { size_t nmbSecTracks = (*secTracks).size(); for(size_t i = 0; i < nmbSecTracks; i++) { if((*secTracks)[i] -> GetDefinition() == G4Electron::Definition()) counter++; } } }Click here for the source file.
int main(int argc,char** argv) { G4RunManager* runManager = new G4RunManager; ... // instantiate the mandatory classes here and assign them to the run manager
UserTrackingAction* trackingAction = new UserTrackingAction(); runManager -> SetUserAction(trackingAction);
... }Don't forget to include the header file of the tracking action. Click here for the main function.