How to get the level energy of the radioactive products?

I’ve been looking for ways to get the energy of the nuclear level of the radioactive products after activation from a beam made of protons or photons. I have tried to get the level energy in the SteppingAction by iterating through the particle name strings obtained from G4Track using something like this:

std::regex pattern(“\[([-+]?[0-9]*\.?[0-9]+)\]”);
// Initialize a std::sregex_iterator to iterate through matches in the text
std::sregex_iterator it(text.begin(), text.end(), pattern);
std::sregex_iterator end;
double number=0;
// Loop through matches and extract the floating-point numbers
while (it != end) {
std::smatch match = *it;
std::string number_str = match[1]; // Captured group (number) is at index 1
number = std::stod(number_str); // Convert the string to a double
//std::cout << "Extracted floating-point number: " << number << std::endl;

However, this significantly slow down my program because the iteration takes a large amount of time. I have surveyed the functions of G4Track and also G4ParticleDefinition, trying to find a smarter way, but no function is available for getting the level of energy. If anyone happens to know any way to solve this problem, that would be great. Thank you in advance. :slight_smile:
_Geant4 Version: 11.2
_Operating System: Ubuntu 22
_Compiler/Version: gcc
_CMake Version: 3.18.4

Why do string parsing at all? You imply above that you have a G4Track and know how to get G4ParticleDefinition. From the latter, you can use G4ParticleDefinition::IsGeneralIon() to check if you’re dealing with a nucleus. If the latter returns true, then you can do a dynamic cast to G4Ions.

At that point, you can call G4Ions::GetExcitationEnergy() to get the actual energy, or G4Ions::GetIsomerLevel() if you think the excitation is a defined meta-stable state.

Dear @HowYoungLan ,

long time ago I myself had the need to do something like what you do. I did it at the SteppingAction for

  • First step of a track
  • Only of the creator process was “RadioactiveDecay”
        G4cout << "\t" << setiosflags(std::ios::right) << std::setw(15)
               << particleName << "[ " << std::setw(9)
               << G4BestUnit(currentE, "Energy") << " ] created at "
               << std::setw(8) << G4BestUnit(time, "Time")
               << "( dt = " << std::setw(8)
               << G4BestUnit(time - isotope_time, "Time") << ")";
        if (particleType != "nucleus" || particleName == "alpha")
          G4cout << G4endl;
          G4double excitationEnergy =
              ((const G4Ions*)(fTrack->GetDefinition()))
          // 24 November 2013  Dennis Wright radioactive_decay-V09-06-21
          //	NOT VALID ANYMORE   G4bool      stability =
          // fTrack->GetDefinition()->GetPDGStable();
          G4bool stability = halfLife == -1 ? true : false;
          if (!stability)
            if (excitationEnergy > 0.)
              G4cout << "  Excited, it should decay.";
              if (halfLife > 0)
                G4cout << " HalfLife = " << G4BestUnit(halfLife, "Time");
              G4cout << G4endl;
              G4cout << " Unstable, it should decay. HalfLife = "
                     << G4BestUnit(halfLife, "Time") << G4endl;
            G4cout << "\tStable, end of chain." << G4endl;

But this may or may not work anymore, it may also be inefficient with string comparisons and whatnot . But please use the snippet as an inspiration.