Hi,
I am working on a Geant4 project where I want to pass an argument from the mac file to the RunAction.
The project is based on ExampleB1 and is kept
here
.
This is what I am trying to do:
Step 1: I create a parameter store in B1RunConfig.hh
// B1RunConfig.hh
#ifndef B1_RUN_CONFIG_HH
#define B1_RUN_CONFIG_HH
class B1RunConfig
{
public:
static B1RunConfig *Instance(); // singleton
void SetRunID(int val) { runID = val; }
int GetRunID() const { return runID; }
private:
B1RunConfig() = default;
int runID = 0;
};
#endif
and B1RunConfig.cc
// B1RunConfig.cc
#include "B1RunConfig.hh"
B1RunConfig *B1RunConfig::Instance()
{
static B1RunConfig instance;
return &instance;
}
Step 2: I made a messenger:
B1RunMessenger.hh
// B1RunMessenger.hh
#ifndef B1_RUN_MESSENGER_HH
#define B1_RUN_MESSENGER_HH
#include "G4UImessenger.hh"
#include "G4UIcmdWithAnInteger.hh"
#include "B1RunConfig.hh"
class B1RunMessenger : public G4UImessenger
{
public:
B1RunMessenger();
~B1RunMessenger();
void SetNewValue(G4UIcommand *command, G4String value) override;
private:
G4UIcmdWithAnInteger *runIDCmd;
};
#endif
B1RunMessenger.cc
// B1RunMessenger.cc
#include "B1RunMessenger.hh"
#include "G4UIdirectory.hh"
B1RunMessenger::B1RunMessenger()
{
auto dir = new G4UIdirectory("/myapp/");
dir->SetGuidance("Custom app controls.");
runIDCmd = new G4UIcmdWithAnInteger("/myapp/setRunID", this);
runIDCmd->SetGuidance("Set custom run ID.");
runIDCmd->SetParameterName("runID", false);
}
B1RunMessenger::~B1RunMessenger()
{
delete runIDCmd;
}
void B1RunMessenger::SetNewValue(G4UIcommand *command, G4String value)
{
if (command == runIDCmd)
{
B1RunConfig::Instance()->SetRunID(runIDCmd->GetNewIntValue(value));
}
}
Step 3: I use B1RunConfig in B1RunAction:
G4int id = B1RunConfig::Instance()->GetRunID();
G4cout << "B1RunAction: Received run ID = " << id << G4endl;
Step 4: In my exampleB1.cc, I instantiate the messenger:
// Instantiate messenger
static B1RunMessenger messenger; // static keeps it alive
Step 5: In the mac file, I pass the required argument:
/myapp/setRunID 42
While the setup is compiling fine, I see the argument is not passed from the mac file to the B1EventAction.
I see the required argument runID is always 0, which is the default value.
May I know what I am doing wrong here?
How should I pass an argument from the mac file to the RunAction?
Any help will be much appreciated.
Thank you,
Arka
Geant4 Version: 11.3.2
_Operating System:_x86_64-apple-darwin24.5.0
_Compiler/Version:_Apple clang version 17.0.0 (clang-1700.0.13.5)
_CMake Version:_4.0.3