Hello, I’ve two targets and I want to get the secondary particles outgoing the second one. In this topic 2 circular targets@anna said me to read this one Last step in a volume so I understood that I have to use the G4Step::IsLastStepInVolume()
Moreover, I also found this topic Get secondaries so I copied, in my SteppingAction, the code written in it.
This is the section regarding of the second target
if (volume == fScoringVolume2) {
}
and I wrote
if (step->IsLastStepInVolume()){
auto secondary = step->GetSecondaryInCurrentStep();
size_t size_secondary = (*secondary).size();
if (size_secondary){
for (size_t i=0; i<(size_secondary);i++){
auto secstep = (*secondary)[i];
int secpdg = secstep->GetParticleDefinition()->GetPDGEncoding();
analysisManager->FillNtupleDColumn(3,0, secpdg);
double seckinEnergy = secstep->GetDynamicParticle()->GetKineticEnergy();
analysisManager->FillNtupleDColumn(3,1, seckinEnergy);
double SecMomDirx = secstep->GetMomentumDirection().x();
analysisManager->FillNtupleDColumn(3,2, SecMomDirx);
double SecMomDiry = secstep->GetMomentumDirection().y();
analysisManager->FillNtupleDColumn(3,3, SecMomDiry);
double SecMomDirz = secstep->GetMomentumDirection().z();
analysisManager->FillNtupleDColumn(3,3, SecMomDirz);
analysisManager->AddNtupleRow(3);
}
}
}
but even if today I tried to simulate 10000 primary particles, I get 0 secondaried, so I think there is an error in my code…
There’s no reason to expect that secondaries will be produced only on the very last step of your primary particle. Seconardaries are no different from any other particle in Geant4. In your stepping action, you’ll see steps from secondaries as well as primaries.
You can see that a particle is a “secondary” by looking at the G4Track::GetParentId() function. If that function returns 0, the particle is a primary (it has no parent); non-zero values refer to the track ID of the track that created those secondaries.
Thank you @mkelsey
Yes, I know about the G4Track::GetParentId() because @anna explained me this function but I’m interested not about all the produced secondary particles but just about the secondary particles (produced in both the 2 targets) outgoing the second target to see which particles can reach reach the dector. Is there a way using the G4Track::GetParentId() to select just the secondary particles (produced in the first or in the second target) outgoing the second target?
No. You select particles leaving the second target by looking for a step with the prestep point in the second target and the poststep point with fGeomBoundary. Use GetParentID() to eliminate primaries from this selection. Then you can look at the track vertex information to see where it was created.
I get points some events at z [-1;-0.15] even if my …
My source is at z= -1m anf my first target is centered at z=0 so its range is [-0.15 cm; z=0.15 cm]
You can also look up the mean free path (or interaction length) for your beam particles in standard air. Figure out how many such interactions you would expect between your source position and your first target. Google is your friend.