I’ve to call it in the generatePrimaries of the PrimaryGeneratorAction
void B1PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
// this function is called at the begining of event
fGeneralParticleSource->SetParticlePosition(G4ThreeVector(0.,0.,-50*cm));
fGeneralParticleSource->GeneratePrimaryVertex(anEvent);
}
That’s not what you do. The detector construction is called by the Run Manager at the appropriate time during the job. The primary generator action is called, separately, by the Run Manager at the completely different appropriate times during the job.
You may, if you wish, register the pointer to your primary generator action with your detector construction (add a data member), and provide the primary generator action with a data member and a Set function for the position.
A better approach would be to write a separate Messenger class for your primary generator action (don’t mix up non-geometry stuff in your geometry Messenger). Then you can use a macro command to set the source position directly.
Hello @mkelsey can you write a piece of code please? Because I didn’t understand so much by world.
Anyway, i just need to pass the value of sampleLabPos that is calculated in the detector construction to the primary generator. I need to pass this value because i the primary gemerator i need to set the position of particles source using the sampleLabPos coordinates.
Ie. I need to write
I see your issue. Your sampleLabPos is not a user-set quantity. You compute it while the geometry is being built. Is “sampleLabPos” a G4ThreeVector? Is it the placement coordinate of a specific volume in your geometry?
If so, then you can add code to your PrimaryGeneratorAction to access that volume from the geometry store, get its placement coordinate, and just use that.
but unfortunately, the position is setted by the macro.
I.e.
in the macro I wrote
########## Particles ##########
/gps/particle gamma
/gps/energy 1808.65 keV
/gps/pos/type Volume
/gps/pos/shape Sphere
#/gps/pos/centre .0 .0 3.67 cm
/gps/pos/radius 7.25 cm
/gps/pos/inner_radius .0 mm
/gps/pos/halfz 16.4 cm
/gps/ang/type iso
/gps/source/intensity 1
/gps/pos/confine sample
then I commented the line #/gps/pos/centre .0 .0 3.67 cm
but the simulation centers the source in 0,0,0…ie. it looks like that if /gps/pos/centre isn’t called, the simulation center in 0,0,0, instead of fGeneralParticleSource->SetParticlePosition(samplePVLabPos);
I mean that it ignores my default parameters setted in primary generator action and it use as default parameters the one of the gps
Do you know how to solve please? Or, maybe, the General particle source doesn’t allow to set a default position in this way: