Execute with an input file (containing particle informaton) as argument

Dear Experts,

I am interested to know if a file(a root file in my case) name can be passed as an input argument during the execution of Geant4 simulation.

This root file contains energy, momentum and vertex information (obtained from a generator simulating proton-proton collisions, e.g. pythia) to be fed to the Geant4ParticleGun.
I have been using such a file to set the properties of the particles to be shot using the particle gun by reading it in the PrimaryGeneratorAction class. However, this is hardcoded and prevents me from submitting jobs in parallel.

Basically, I am trying to submit my standalone simulation jobs on grid where I read the input files from a dataset and run Gean4 simulation for each of the files in this dataset.

It would be a great help if I can pass a string in the form of a file name while executing run.mac file.

Thank you in advance,

You would need to write your own subclass of G4VUserPrimaryGeneratorAction, and you would probably want to write a Messenger (UI command) class to go with it. You would define a UI command to take a string argument (the filename), and pass that argument back into the generator action.

The bulk of your generator action class would involve opening your ROOT file and traversing it to get to the data you need. Once you have the track kinematics, you could either pass them through to G4ParticleGun, or you could instantiate a G4PrimaryVertex and G4PrimaryParticles based on your data, and add them to G4Event in your own code.

Almost every example includes its own generator action, and a bunch of them also have associated Messengers. Probably the easiest way to get started is to use one of them as a template. Unless your ROOT file is extremely simple (plain N-tuple-style TTrees), you will have to write your own code to process it, and you’ll have to ensure in your application Makefile or CMakeFile that you link to ROOT.


Thank you for your prompt reply. This helps! I already have a working simulation with G4VUserPrimaryGeneratorAction class. Was just wondering if there was a command that could be added to the run.mac file.
I do the following to execute the run.mac file

// Get the pointer to the User Interface manager
G4UImanager * UImanager = G4UImanager::GetUIpointer();

if (argc!=1) // batch mode
G4String command = "/control/execute ";
G4String fileName = argv[1]; //.mac file
else // interactive mode : define UI session
// start visualization

As suggested by you, I would then similarly add G4String filename2 =argv[2] and then pass filename2 to G4VUserPrimaryGeneratorAction class directly. However, what role will the UImanager have here when passing filename2 to G4VUserPrimaryGeneratorAction?


Yes, you can definitely do it that way. I was thinking a bit differently. If you write a Messenger class which communicates with your PrimaryGeneratorAction, then you could put into the run.mac (or whatever) a line that might look like


Then you or your users don’t have to remember that the ROOT file must be given as the second command-line argument.

Also, once you have the Messenger class in place, you can imagine adding other commands to configure your generator at run time (maybe the position of your source, or beam width, or turning on debugging messages).

I use UI commands extensively in the executable for my experiment, so that we can provide pre-built executables which users can still configure for their needs.

1 Like