_Geant4 Version:_v11.3.0
_Operating System:_macOS Sequoia 15.2
_Compiler/Version:_v16.0.0
_CMake Version:_v3.31.2
Hello! I’ve been using Geant4 to model particle production from a 120 GeV proton beam incident on an iron beam dump, and have been attempting to record particle name and 4-momentum for any particles passing through 2 detectors at different places in the simulation geometry. I’ve recently noticed something a little odd that I was wondering if anyone had any clarifications on: the number of particles that appear to pass through each detector don’t seem to line up with the output on the visualizer, especially for the first detector. Here’s one example run below, with the visualizer and the corresponding detector hits, where there are significantly fewer particles being recorded than there should be according to the visualizer:
Below is the code that I use for both detectors. My aim was to have each detector record the particle name and 4-momentum of the particle when it first hit the detector and both print it out and save it to a .csv file, though it doesn’t seem as though that is the case based on the above image. What could be causing this discrepancy and how can I fix it? Any help would be greatly appreciated!
#include "detector.hh"
#include "G4ParticleDefinition.hh"
#include "G4AnalysisManager.hh"
MySensitiveDetector::MySensitiveDetector(G4String name) : G4VSensitiveDetector(name)
{}
MySensitiveDetector::~MySensitiveDetector()
{}
G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROHist)
{
//new KE, mom. stuff from G4 forums
//check to make sure track isn't leaving the worl vol. first (returns physical vol. as null ptr)
G4bool worldboundary = aStep->GetPostStepPoint()->GetStepStatus()==fWorldBoundary;
if (!worldboundary){
G4LogicalVolume* post_volume = aStep->GetPostStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume();
G4String vol_name = post_volume->GetName();
G4bool boundary = aStep->GetPreStepPoint()->GetStepStatus()==fGeomBoundary;
if ((vol_name == "Detector") && (boundary)){
//get kinetic energy, momenta, particle name
G4double KE = aStep->GetPostStepPoint()->GetKineticEnergy();
G4double p_x = aStep->GetPostStepPoint()->GetMomentum().x();
G4double p_y = aStep->GetPostStepPoint()->GetMomentum().y();
G4double p_z = aStep->GetPostStepPoint()->GetMomentum().z();
G4String particleName = aStep->GetTrack()->GetDynamicParticle()->GetDefinition()->GetParticleName();
//print out the 4-mom.
G4cout << "Particle Name, 4-mom.: " << vol_name << ", " << particleName << ", " << "(" << KE << ", " << p_x << ", " << p_y << ", " << p_z << ")" << G4endl;
//record the 4-mom.
G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
analysisManager->FillNtupleSColumn(0, 0, vol_name);
analysisManager->FillNtupleSColumn(0, 1, particleName);
analysisManager->FillNtupleDColumn(0, 2, KE);
analysisManager->FillNtupleDColumn(0, 3, p_x);
analysisManager->FillNtupleDColumn(0, 4, p_y);
analysisManager->FillNtupleDColumn(0, 5, p_z);
analysisManager->AddNtupleRow(0);
}
}
return 1;
}
#include "seconddetector.hh"
#include "G4ParticleDefinition.hh"
#include "G4AnalysisManager.hh"
MySecondSensitiveDetector::MySecondSensitiveDetector(G4String name) : G4VSensitiveDetector(name)
{}
MySecondSensitiveDetector::~MySecondSensitiveDetector()
{}
G4bool MySecondSensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROHist)
{
//new KE, mom. stuff from G4 forums
//check to make sure track isn't leaving the worl vol. first (returns physical vol. as null ptr)
G4bool worldboundary = aStep->GetPostStepPoint()->GetStepStatus()==fWorldBoundary;
if (!worldboundary){
G4LogicalVolume* post_volume = aStep->GetPostStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume();
G4String vol_name = post_volume->GetName();
G4bool boundary = aStep->GetPreStepPoint()->GetStepStatus()==fGeomBoundary;
if ((vol_name == "DetectorTwo") && (boundary)){
//get kinetic energy, momenta, particle name
G4double KE = aStep->GetPostStepPoint()->GetKineticEnergy();
G4double p_x = aStep->GetPostStepPoint()->GetMomentum().x();
G4double p_y = aStep->GetPostStepPoint()->GetMomentum().y();
G4double p_z = aStep->GetPostStepPoint()->GetMomentum().z();
G4String particleName = aStep->GetTrack()->GetDynamicParticle()->GetDefinition()->GetParticleName();
//print out the 4-mom.
G4cout << "Particle Name, 4-mom.: " << vol_name << ", " << particleName << ", " << "(" << KE << ", " << p_x << ", " << p_y << ", " << p_z << ")" << G4endl;
//record the 4-mom.
G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
analysisManager->FillNtupleSColumn(0, 0, vol_name);
analysisManager->FillNtupleSColumn(0, 1, particleName);
analysisManager->FillNtupleDColumn(0, 2, KE);
analysisManager->FillNtupleDColumn(0, 3, p_x);
analysisManager->FillNtupleDColumn(0, 4, p_y);
analysisManager->FillNtupleDColumn(0, 5, p_z);
analysisManager->AddNtupleRow(0);
}
}
return 1;
}