HaarigerHarald / geant4_pybind

Alternative Python bindings for Geant4
The Unlicense
35 stars 6 forks source link

Killing a track in SteppingAction? #21

Closed shayan0373n closed 10 months ago

shayan0373n commented 10 months ago

In Geant4, you can delete a track in SteppingAction like this:

G4Track* track = step->GetTrack();
track->SetTrackStatus(fStopAndKill);

I've noticed that SetTrackStatus (and seemingly many other setter methods) are not available in Python bindings. Is this intentional? Do you have any suggestions on how to kill a track, for example, when the energy has dropped below a certain threshold?

HaarigerHarald commented 10 months ago

It's not intentional some things just weren't bound to python yet. Can you verify that they are available now with 54a420faf3dc378e1594d8988f3f8fc947231ac1 and work correctly?

shayan0373n commented 10 months ago

Thanks for the quick update. Unfortunately, I currently don't have the required build setup to test the changes. I will try over the weekend if it is left untested.

To test SetTrackStatus we can use examble B1 and change the UserSteppingAction method in B1SteppingAction class to:

def UserSteppingAction(self, aStep):
    if self.fScoringVolume == None:
        detectorConstruction = G4RunManager.GetRunManager().GetUserDetectorConstruction()
        self.fScoringVolume = detectorConstruction.fScoringVolume

    volume = aStep.GetPreStepPoint().GetTouchable().GetVolume().GetLogicalVolume()

    # check if we are in scoring volume
    if volume != self.fScoringVolume:
        return
    else:
        # collect energy deposited in this step
        edepStep = aStep.GetTotalEnergyDeposit()
        self.fEventAction.AddEdep(edepStep)
        # kill the track !TEST!
        track = aStep.GetTrack()
        track.SetTrackStatus(fStopAndKill)

Edit: The expected result should show a significantly lower (but not zero) deposited energy, since we are killing the particles as soon as they reach the scoring volume of the detector.

HaarigerHarald commented 10 months ago

With this modification both C++ and Python yield the same result.

 Cumulated dose per run, in scoring volume : 677.265 picoGy  rms = 18.1942 picoGy 

I'll close this for now.