Instantiating ion in GeneratePrimaries

Initial example is TestmEm0. If I use macro

/gun/particle ion
/gun/ion 6 12

and simply

fParticleGun->GeneratePrimaryVertex(anEvent);

in PrimaryGeneratorAction everything works fine.

But if I comment lines in macro file and want to use ion directly (pretty the same way what the macro file do looking the Messenger code):

fParticleGun->SetParticleDefinition(G4IonTable::GetIonTable()->GetIon(6, 12, 0));
fParticleGun->SetParticleCharge(6);

than it fails and uses e- as default particle (e- is defined in SetDefaultKinematic of TestEm0).

What is the problem with setting ions directly in GeneratePrimaries?

I’m using Geant4.10.6-p02.

in example radioactivedecay/rdecay01, you will find :

G4int Z = 10, A = 24;
G4double ionCharge   = 0.*eplus;
G4double excitEnergy = 0.*keV;

G4ParticleDefinition* ion
   = G4IonTable::GetIonTable()->GetIon(Z,A,excitEnergy);
fParticleGun->SetParticleDefinition(ion);
fParticleGun->SetParticleCharge(ionCharge);

//create vertex
//
fParticleGun->GeneratePrimaryVertex(anEvent);

I’ve just coppied this entire part into PrimaryGeneratorAction.cc of TestEm0 and it still don’t work (it sees e-).

As said in Readme, TestEm0 is a special exercice : there is no tracking. In particular, the function GeneratePrimaries( ) is never called.
A solution is to put commands in the main via the function ApplyCommand( ). For example, at end of TestEm0.cc :
UImanager->ApplyCommand("/gun/particle ion");
UImanager->ApplyCommand("/gun/ion 6 12");
UImanager->ApplyCommand("/run/beamOn 1");

I got the idea. Thank you for the explaination.