How to specify a few ion source such as 12C,28Si using class G4ParticleGun?

I want to use ion source of 12C and 28Si each 50% to do a simulation in G4,
How shauld i do if i use class G4ParticleGun?

Get 2 primary ParticleDefinition, sampling in the primary action with 50% probability.

how to specify ion source 12C? how to write source code?

if i specify 12C source like this:
“G4ParticleDefinition* particle
= particleTable->FindParticle(particleName=“12c”);”

the error occurs the information in the figure

Carbon-12 should be “C12”, similar, “Si28”.

sorry,Carbon-12 written as “C12” or “c12” is no correct, the same error occors as below:

Hi, you can do this two ways, by editing the source code of or simply by adding the command into your run.mac macro:

For Carbon-12:
/gun/particle ion
/gun/ion 6 12 0 0
/gun/energy 120 MeV #10MeV per nucleon

For Silicon-28:
/gun/particle ion
/gun/ion 14 28 0 0
/gun/energy 280 MeV #10MeV per nucleon

Otherwise you can add into by following instructions on slide 13 at:

I would recommend just using the macro approach so you don’t need to recompile each time.

If you want to have a mixed source of 50% each, you will need to use GeneralParticleSource (GPS) instead. GPS has much more more functions.

I can use a mixed source do a simulation using fortran in fluka and c++ in g4,
i only do not know how to specify ion source like carbon 12 using class G4ParticleGun

how to specicy ion source like carbon 12 using class G4ParticleGun
please help me thanks!

I have learn how to specify ion source from the cource uploaded by jamesv, thanks all!

I write carbon-12 ion source follow the cource uploaded by jamesv like this below:
G4int n_particle = 1;
fParticleGun = new G4ParticleGun(n_particle);
G4ParticleDefinition* ion = G4IonTable::GetIonTable()->GetIon(6, 12, 0.);


but the error occurs the information in the picture.

What version of Geant4 are you using?

In, add in B1PrimaryGeneratorAction::GeneratePrimaries:

//Set definition of ION beam for C12:
G4int Z=6;
G4int A=12;
G4double ionCharge = 0;
G4double excitEnergy = 0keV;
ion = G4IonTable::GetIonTable()->GetIon(Z,A,excitEnergy);

And then you don’t need to use the macro command in run.mac because it will be replaced by this for each event. I included my file from example B1. (4.5 KB)

If you want to define the source in C++ code rather than using macro command that is fine for using G4ParticleGun. If you want to make the mixed 50% C12 and Si28 primary beam, you could use a random number generator when you define the ion source to randomly pick either C12 or Si28.

the Geant4 version i use is 10.6.

when i move the code below:
G4ParticleDefinition* ion = G4IonTable::GetIonTable()->GetIon(6, 12, 0.);


fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0., 0., 1.));
fParticleGun->SetParticleEnergy(6. * MeV);

from constructor section to GeneratePrimaries(G4Event* anEvent) section:
the program works well.

thanks anyway!