Simulating different particle gun energies in a single run

Hi Everyone,

I am totally new to Geant4 and I’d like to apologize in advance for the very basic question I am about to ask.

I am building my simulations from ExampleB1 and I’m at a point where I want to simulate different particle gun energies in a single run and create/save an n-tuple for each of the energy. I have been doing the simulations individually but this takes a lot of time.

How can I specify the different energies and loop through each of them. Can multithreading the run solve this problem and how can I implement it for about 10 different energies?

Thanks in advance for your kind response! :slight_smile:

You could use a loop:

"make a macro file e.g kapis.mac and add the following

/gun/energy {eKin} MeV
/run/beamOn 100000
/control/shell youroutput.dat output_{eKin}.dat

then in geant4 command line paste this:

/control/loop kapis.mac eKin 10 100 20

assuming the initial energy denoted by variable ekin is 10kev and final is 100kev in steps of 20kev or so as you wish."

Nice solution, but the original post had a clear typo: The description says “kev” [sic], but the loop macro is using MeV.

Thank you so much for your contribution @weller and @mkelsey Your help is very much appreciated.

How do I implement it so the next step should increment with double the current eKin value. I tried using eKin*2 but I got an error:
image

I also tried to specify all the values from 10 through to 10240 but it only ran a simulation for 10 keV.

How can I fix this problem?

Thanks once again for your assistance. Means a lot!

I am not sure if you can apply math operations within the parameters of a command.

try this for a quick solution:

/control/foreach kapis.mac eKin "10 20 40 80 160 320 640 1280 2560 5120 10240"

did you specify

/control/loop kapis.mac 10 10240 1

?

Thanks again @weller

The foreach loop works but I discovered that only the last n-tuple (i.e. 10240 keV in this case) has entries. I am guessing this may be due to an overwrite because of how my RunAction.cc is setup and needs to modified to create a distinct ntuple at each step. Here’s a snippet from the RunAction file:

some inspiration:

void myRunAction::BeginOfRunAction(const G4Run* aRun) {
    G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
    char fileName[65];
    sprintf(fileName, "Somethingsomething_run%04d",aRun->GetRunID());
    analysisManager->SetFileName(G4String(fileName));
    analysisManager->OpenFile();
}

Thank you SO much @weller That worked perfectly and I can’t really express how grateful I am. You have saved me a ton of time and I really appreciate it!

Future readers: Every response solves a specific problem but I could only mark one response as a solution.