Don't kill particles inside detector

Hi,

I want to perform a simulation of x-rays. I’m still at the beginning of my project where I’m using code snippets of other projects. So far, it’s working well, I guess but I’m a bit stuck now where I expect the x-rays to go through the detectors and not being stopped there. This is the code of the detector:

G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
{
G4Track *track = aStep->GetTrack();

track->SetTrackStatus(fStopAndKill);

G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
G4StepPoint *postStepPoint = aStep->GetPostStepPoint(); // leaves detector

G4ThreeVector posPhoton = preStepPoint->GetPosition();

const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();

G4int detector_number = touchable->GetCopyNumber();

G4VPhysicalVolume *physVol = touchable->GetVolume();
G4ThreeVector posDetector = physVol->GetTranslation();

G4int evt = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();

G4AnalysisManager *man = G4AnalysisManager::Instance();
man->FillNtupleIColumn(0, evt);
man->FillNtupleDColumn(1, posDetector[0]);
man->FillNtupleDColumn(2, posDetector[1]);
man->FillNtupleDColumn(3, posDetector[2]);
man->AddNtupleRow(0);

return true;
}

I have identified the line

track->SetTrackStatus(fStopAndKill);

which is responsible for doing so. However, the alternative options Geant4: G4TrackStatus.hh File Reference don’t tell me much which one I should use:

fAlive : Continue the tracking
fStopButAlive : Invoke active rest physics processes and kill the current track afterward
fStopAndKill : Kill the current track
fKillTrackAndSecondaries : Kill the current track and also associated secondaries. Suspend the current track.
fSuspend : Suspend the current track
fPostponeToNextEvent : Postpones the tracking of the current track to the next event

What are you trying to do? If you want the xray to travel through your detector, just comment out that line. You don’t have to replace it with anything

1 Like

Good point, actually, I want to measure the deposited energy at the detectors but x-rays are possible to go through matter, right? So I would expect that a part of the x-rays go through even if there’s a detector?

Yes, it depends on your detector efficiency, but its very possible that x-rays travel through your detector.

You can track the deposited energy, and use a StopandKill command if the x-ray leaves the detector, because you don’t care about it any more.

Or, you can just let the particle run out of energy or leave your world box, at which point it is no longer tracked anyway

1 Like

How do I let the particle run out of energy, I mean, with which command?

In the above case, it would be possible that a particle goes through three detectors sequentially and I would like to allow this.

You don’t have to do anything. That’s what Geant4 does. It will track a particle, with all of its interactions and energy losses, until the particle either reaches zero energy, or leaves the world volume.

1 Like

Thanks, ok. What would I enter in

track->SetTrackStatus()

then?

Why do you want to chage the track status? Your question was about measuring quantities (i.e., using a sensitive detector, a scorer, or a stepping action). If you want to let Geant4 do it’s normal thing, you shouldn’t interfere with that by changing the track status.

Means I can simply remove this command line?
What would be the default setting?

You can remove any line. There are very few required commands in GEANT4, namely specify your user action classes, and creating a minimal geometry, particle source, etc.

If you don’t have that line, Geant4 will handle the process and only record what you ask it to

1 Like

What does happen when I comment out this line (either be it track->SetTrackStatus(fStopAndKill); or track->SetTrackStatus(fAlive); ) ?
I noticed slight changes in the energy deposition but what is actually happening then?

when commenting out the line track->SetTrackStatus(fStopAndKill);, you don’t kill the particle in the current step, and thus the particle proceeds to be tracked through the geometry and can deposit additional energy or leave the detector volume etc…

when commenting out the line track->SetTrackStatus(fAlive);, in principle nothing should change. Without having looked very much into this: you could see differences in energy deposition due to different reasons: first, the random numbers generator is in a different state (if you seed e.g. with the current time at the start of the run / launch of application / …). Second, maybe the order in which the tracks are processed could change when you touch the TrackStatus (the order of random numbers stays the same, but different “decisions” are made for the particular step)? After all, it is a monte carlo simulation…

In any case: I would not touch stuff if not needed. Manually setting the trackStatus to alive is probably unneccessary…

1 Like

Thanks a lot for the information!

Am I safe when I simply uncomment the track->SetTrackStatus() in general? Is there any default which will be set then?
Btw, I can’t find any official information about these options…?

a general statement is difficult in this case. Your code snippet from the opening post suggests that you intend to detect the position where a particle enters the detector. In order to avoid counting the same particle as it travels through the detector in each step, the particle is killed at the first step within the SD.
If you don’t kill the particle, there will be at least 1 additional step inside the SD (namely, when it leaves the volume) and an additional N-tuple row will be added.

I would add

if (aStep->GetPreStepPoint()->GetStepStatus() == fGeomBoundary ) {
   ...
}

around the stuff that is supposed to sense the entrance position, and not mess with the TrackStatus.

I don’t belive there is a default that will be set anywhere, because it is simply not necessary to change this --except maybe some (physical)process happens that changes the status, but this is then part of Geant4 doing it’s normal thing :slight_smile:

You have posted documentation about the different options yourself:

1 Like

You are safe doing just about anything… you can always revert if you see unintended changes. You can do almost nothing in stepping action and Geant4 will create the particle, track it through all physics processes that occur, and stop tracking when the particle is destroyed. You don’t have to specify any changes to the track status unless you explicitly want to. For instance, as weller mentioned, if you only care about the location that the particle enters your detector, you could kill the particle after it has entered the detector. But if you also care about total energy deposited, then you will have to check to make sure the particle has just entered the detector (for the first time) and isn’t just interacting inside of the detector.

1 Like

Thanks both of you!

Sorry in case I’m repeating myself over and over. My attempt is rather simple: I only want to measure the energy particles (only photons) deposit within my detectors. This, on the other hand, as realistic as possible.

Means, from my side, the photons shouldn’t be killed within the detector because they could go through it and enter another detector(?).
That’s why I’m so concerned about this track status because I’m not really understanding it (w.r.t. to the simulation). But according to the deposited energies it seems there is (almost) no difference whether I use the track status or not (in case it is set to fStopAndKill).
From my guts I would set it to fAlive because “continuing the track” sounds reasonable to me. But whyever, this takes ages meaning it takes so long it is not usable…? But maybe I would need to change something for this (like Pre- or PostStep…?).
I’m also fine to ignore/uncomment this command in total. I just don’t understand it in either case :slight_smile:

if it does not take ages if you don’t set it to alive:
maybe you revive meant-to-be-dead particles by this? just speculating…

Ok,… weird? Why and how? :smiley: