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:
error

Hi, you can do this two ways, by editing the source code of PrimaryGeneratorAction.cc 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 PrimaryGeneratorAction.cc 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
in PrimaryGeneratorAction.cc,

how to specicy ion source like carbon 12 using class G4ParticleGun
in PrimaryGeneratorAction.cc?
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);
G4IonTable::GetIonTable()->CreateAllIon();
G4ParticleDefinition* ion = G4IonTable::GetIonTable()->GetIon(6, 12, 0.);

fParticleGun->SetParticleCharge(0);
fParticleGun->SetParticleDefinition(ion);

but the error occurs the information in the picture.

What version of Geant4 are you using?

In PrimaryGeneratorAction.cc, add in B1PrimaryGeneratorAction::GeneratePrimaries:

fParticleGun->SetParticlePosition(G4ThreeVector(x0,y0,z0));
.
//Set definition of ION beam for C12:
G4int Z=6;
G4int A=12;
G4double ionCharge = 0;
G4double excitEnergy = 0keV;
.
G4ParticleDefinition
ion = G4IonTable::GetIonTable()->GetIon(Z,A,excitEnergy);
fParticleGun->SetParticleDefinition(ion);
fParticleGun->SetParticleCharge(ionCharge);
.
fParticleGun->GeneratePrimaryVertex(anEvent);

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 PrimaryGeneratorAction.cc file from example B1.
B1PrimaryGeneratorAction.cc (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->SetParticleCharge(0.);
fParticleGun->SetParticleDefinition(ion);

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

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

thanks anyway!