No get secondary particles

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?

Thank you

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.

Thank you @mkelsey Maybe I solved in this way

G4VPhysicalVolume* ThisVol = step->GetPreStepPoint()->GetTouchableHandle()->GetVolume();
	G4VPhysicalVolume* NextVol = step->GetPostStepPoint()->GetTouchableHandle()->GetVolume();

if(NextVol && ThisVol->GetName()=="Envelope2" && NextVol->GetName()=="World" && step->GetTrack()->GetParentID()!=00 ) { 
			 		    int secpdg = step->GetTrack()->GetParticleDefinition()->GetPDGEncoding();
			 			analysisManager->FillNtupleDColumn(3,0, secpdg);
			 			double seckinEnergy = step->GetTrack()->GetDynamicParticle()->GetKineticEnergy();
			 			analysisManager->FillNtupleDColumn(3,1, seckinEnergy);
			 			double SecMomDirx = step->GetTrack()->GetMomentumDirection().x();
			 			analysisManager->FillNtupleDColumn(3,2, SecMomDirx);
			 			double SecMomDiry = step->GetTrack()->GetMomentumDirection().y();
			 			analysisManager->FillNtupleDColumn(3,3, SecMomDiry);
			 			double SecMomDirz = step->GetTrack()->GetMomentumDirection().z();
			 			analysisManager->FillNtupleDColumn(3,3, SecMomDirz);
			 			double SecVertx = step->GetTrack()->GetVertexPosition().x();
			 			analysisManager->FillNtupleDColumn(3,4, SecVertx);
						double SecVerty = step->GetTrack()->GetVertexPosition().y();
						analysisManager->FillNtupleDColumn(3,4, SecVerty);
						double SecVertz = step->GetTrack()->GetVertexPosition().z();
						analysisManager->FillNtupleDColumn(3,4, SecVertz);
			   			analysisManager->AddNtupleRow(3);  
		}

and now I get the secondaries! Just I don’t understand the reason beacause of in the plot of the vertex on the z axis

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]

What material did you assign to your world volume? G4_AIR? Or G4_Galactic?

Yes @mkelsey it’s air…do you think there are particles that interacted in the world before to reach the target?

Absolutely. You can see them in your plot.

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.

1 Like

Thank you @mkelsey !