Exercises and Solutions - Day 4

Exercises: Exercises of the day

Exercises and solutions - Part II

The solutions are indicated in blue.

Part II - Sensitive Detector and Hits

Exercise 4-2*
Extend the functionality of the hit class, in order that lateral coordinates (x and y) of hits can also be stored. Implement appropriate get and set functions. Moreover extend the copy constructor and the assignment operator accordingly.

In the header file containing the class definition of a detector hit, DetectorHit.hh, introduce the following functions and data members (in green):
class DetectorHit : public G4VHit {

public:
...
   inline void SetXCoordinate(G4double x) {
     xCoord = x;
   }
   inline void SetYCoordinate(G4double y) {
     yCoord = y;
   }
   inline G4double GetXCoordinate() {
     return xCoord;
   }
   inline G4double GetYCoordinate() {
     return yCoord;
   }
private:
...
   G4double xCoord;
   G4double yCoord;
};
You can find the DectectorHit.hh file here.
Don't forget to initialize the data members in the class constructor (in DetectorHit.cc):
DetectorHit::DetectorHit() :
   ...
   xCoord(0.),
   yCoord(0.) {

}
The copy constructor must be updated to take into account the new variables:
DetectorHit::DetectorHit(const DetectorHit& right) :
   ...
   xCoord(right.xCoord),
   yCoord(right.yCoord) {
}
The assignment operator also requires some additional code:
const DetectorHit& DetectorHit::operator=(const DetectorHit& right) {
  ...
  xCoord = right.xCoord;
  yCoord = right.yCoord;
  return *this;
}
Click here for the DectectorHit.cc file.

Exercise 4-3
This exercise provides a transition to part III (The topics covered in part II and III are closely linked, since the functionality of sensitive detectors allows one to keep track of information which is basically retrieved from objects of classes like G4Track, G4Step or G4DynamicParticle):
In the ProcessHits method of your sensitive detector, retrieve the lateral coordinates of the particle step, and store them in the modified Hit class by using the new get functions.

In ProcessHits method of your SensitiveDetector class add the following code in green:
G4bool SensitiveDetector::ProcessHits(G4Step* step, 
                                      G4TouchableHistory* hist) {

  if(step == 0) return false;

  G4double energyDeposit = step -> GetTotalEnergyDeposit();

  G4ThreeVector translation = hist -> GetTranslation();
  G4double binCenter = translation.z();

  DetectorHit* hit = new DetectorHit(); 
 
  hit -> SetEnergyDeposit(energyDeposit);
  hit -> SetBinCenter(binCenter);
  G4StepPoint* preStepPoint = step -> GetPreStepPoint();

  G4ThreeVector preStepPointPosition = preStepPoint -> GetPosition();

  G4double xCoord = preStepPointPosition.x();
  G4double yCoord = preStepPointPosition.y();

  hit -> SetXCoordinate(xCoord);
  hit -> SetYCoordinate(yCoord);
  hitsCollection -> insert(hit);

  return true;
}
Click here for the SensitiveDetector.cc file.