Example showing how to retrieve particle properties

Hi,

For people doing analysis or preparing input files for Geant4 this code may be useful. It shows how to retrieve static properties, such as mass, half life or table with decay modes, of particles known to Geant4.

// Example showing how to retrieve Geant4 particle properties
// Alvaro Tolosa-Delgado, May 2024, CERN
//
// To compile:  g++ example_show_particle_table.cc `geant4-config --libs --cflags` -o example_show_particle_table 
// To run: ./example_show_particle_table

// include the following headers to instatiate all particle definitions
#include "FTFP_BERT.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "G4DecayTable.hh"

// or include this header to access definition of Kaon+
// there is a G4xxxx.hh header file for each particle
#include "G4KaonPlus.hh"
int main()
{

  // Initialize a builtin physics list, any would work in this case
  G4VModularPhysicsList* physicsList = new FTFP_BERT; // from FTFP_BERT_.hh
  // Initialize the definition of all the particles known to Geant4
  physicsList->ConstructParticle();
  
  // now the Particle table is ready to be used
  G4ParticleDefinition* particle = G4ParticleTable::GetParticleTable()->FindParticle("kaon+");
  // show all information about particle properties and decay modes
  particle->DumpTable();
  // dump only decay modes
  if(particle->GetDecayTable()) particle->GetDecayTable()->DumpInfo();
   
  // The previous steps can be replaced by accesing the particle definition directly
  //G4ParticleDefinition* particle = G4KaonPlus::Definition(); // from G4KaonPlus.hh
  
  // retrieve the mass according to PDG
  G4double particle_mass = particle->GetPDGMass();
  // remember to use unit conversion ALWAYS!!
  std::cout << particle_mass / CLHEP::keV << std::endl;
  
  // retrieve condition if particle is going to be tracked (that is, if its halflife is >0.01ps)
  // G4bool particle_stable   = particle->GetPDGStable();

  // dump all particles 
  G4ParticleTable::G4PTblDicIterator *piter = G4ParticleTable::GetParticleTable()->GetIterator(); 
  piter -> reset();
  while( (*piter)() ){ (piter->value())->DumpTable(); }

}

Dear admins, if you think this is not the place to post this kind of things, please delete the thread.

Best,
Alvaro