Instantiate object of Detector construction to call a function in Generator action

Hello, I’ve function in the detector construction

The function is defined in B1detectorconstruction.hh
void PassSamplePosition();

and in

void B1DetectorConstruction::PassSamplePosition(){

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



How to instantiate the object to call the funcion?? (64.8 KB)
B1DetectorConstruction.hh (14.3 KB) (2.9 KB)

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


Then I need a method to pass the sampleLabPos value from detector construction to primary generator action

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.

Yes, it is a G4ThreeVector. It is defined in the detectorconstruction.hh

G4ThreeVector sampleLabPos;
  G4ThreeVector sampleRelPos;
	G4ThreeVector NfilledchamberRelPos;

then in the I define the relative positions of the volumes in mother volumes

sampleRelPos=G4ThreeVector(0, 0, (-4.55*cm-1.175*cm+fillingheight/2.-fsmarinelli_height/2.));
NfilledchamberRelPos=G4ThreeVector(0., 0., 11.95);

then I’ve to calculate the position of the sample in laboratory system i.e.


Lastly, I’ve to center the source using the sampleLabPos coordinates in primary generator action, i.e. something like this:


my problem is that I don’t know how to pass the sampleLabPos value from the detector construction to the primary generator action.

how to do that? is there an example of code or can you write a piece please?

Hello @mkelsey
Maybe I accessed both to the logical and physical volume from the PrimaryGeneratorAction and I got the coordinates

fGeneralParticleSource  = new G4GeneralParticleSource();
   sampleLV = G4LogicalVolumeStore::GetInstance()->GetVolume("sample");
   samplePV = G4PhysicalVolumeStore::GetInstance()->GetVolume("sample");
   NfilledchamberPV = G4PhysicalVolumeStore::GetInstance()->GetVolume("Nfilledchamber");
    samplePVRelPos = samplePV->GetObjectTranslation();
    NfilledchamberPVRelPos = NfilledchamberPV->GetObjectTranslation();

but unfortunately, the position is setted by the macro.


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

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:


but it exclusively allows by command line ?