Changing detector position from a macro

Dear Geant4 experts,

I want to run multiple simulations by changing the detector position along xyz coordinates for each simulation. I searched online and came across a post here Can geant4 define a moving detector? that uses G4GeometryManager.

Can someone please explain how the detector is called within the macro and how the coordinates are specified in the macro?

I will also be happy to know if there is a more efficient way to achieve this.

Thank you

You would need to write your own messenger class (or use G4GenericMessenger) to define your own macro commands that set parameters in your geometry builder. You would need, in the messenger code, to ensure that when you change those parameters, you also set the “geometry has changed” flag so that the RunManager will rebuild the new geometry.

Many of the examples include the code to do this.

1 Like

Thank you very much mkelsey. I really appreciate your response.

Hi mkelsey,

I did try to implement the G4GenericMessenger as you suggested. However, I came across an error which I am struggling to solve. Below is a screenshot of the error and a part my my detector construction where its implemented. Any further advise will be appreciated.

Thank you

do you find the line

#include "G4GenericMessenger.hh"

in your file?

Thank you very much. I actually missed that.

Hi again,

I am following B5 example to use G4GenericMessenger. Attached is a portion of my Detector Construction that tries to mimic the class. Please correct me if am wrong, line 198 checks if cryst_phys is created and then proceed to modify the geometry. Otherwise print the message “Detector has not yet been constructed.” Is that correct?

When I run my simulation, I get the message “Detector has not yet been constructed.” printed, so am assuming the code those not proceed beyond this stage. However, if I change thetaAngle from my macro using /nvisage/detector/thetaAngle it’s recognized.

I also attempted to ReinitialGeometry() after the BeginOfRunAction() as described in the UserGuide, but that also didn’t prevent the message from been printed.

Could you please look at the screenshot and advise what you think is wrong with my code?

Thank you


correct. return makes the programm exit this function immediately.

Is there maybe a /run/initialize issued in between?

The message is assumingly printed because you try to alter the detector somewhere in macros that are run during program start!? After you make a (first) run, the message does not appear ever again during this session, only when you (re)start the simulation, correct?

I have tried with just one /run/initialize at the begining as well as after each run, but the same message is printed. Attached are my macro and the terminal display.

Thank you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.