Skip to content

Commit

Permalink
Refactor messengers (#69)
Browse files Browse the repository at this point in the history
* Refactor out a helper function to initialize an interactive session.
  This allows to do a late intitialization of an interactive session.
  Before that change, using the command to use an interactive session
  just segfaulted with a null ptr deref.
* There is an upstream bug with bools in G4GenericMessenger, only
  numeric values work, other values like `true` will be silently ignored.
* Some commands in `RMGPhysics` have been restricted to the PreInit state.
  see `G4DeexPrecoParameters::IsLocked()` which just _silently_ discards
  any changes to the `G4DeexPrecoParameters` if the state is not PreInit.
* Some guidance and parameter name changes.
  • Loading branch information
ManuelHu authored Apr 11, 2024
1 parent 2f0b743 commit 0691f18
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 32 deletions.
2 changes: 0 additions & 2 deletions include/RMGEventAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class RMGEventAction : public G4UserEventAction {

private:

std::unique_ptr<G4GenericMessenger> fMessenger;
void DefineCommands();
RMGRunAction* fRunAction = nullptr;
};

Expand Down
4 changes: 3 additions & 1 deletion include/RMGManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
class G4VUserPhysicsList;
class RMGHardware;
class RMGUserAction;
class RMGManagerMessenger;
class G4GenericMessenger;
class G4UIExecutive;
class RMGManager {

public:
Expand Down Expand Up @@ -110,6 +110,8 @@ class RMGManager {
void SetUpDefaultProcessesList();
void SetUpDefaultUserAction();

std::unique_ptr<G4UIExecutive> StartInteractiveSession();

std::string fApplicationName;
int fArgc;
char** fArgv;
Expand Down
1 change: 0 additions & 1 deletion include/RMGPhysics.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "G4VModularPhysicsList.hh"
#include "globals.hh"

class RMGProcessesMessenger;
class RMGPhysics : public G4VModularPhysicsList {

public:
Expand Down
11 changes: 1 addition & 10 deletions src/RMGEventAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
#include "fmt/chrono.h"
#include "magic_enum/magic_enum.hpp"

RMGEventAction::RMGEventAction(RMGRunAction* run_action) : fRunAction(run_action) {

this->DefineCommands();
}
RMGEventAction::RMGEventAction(RMGRunAction* run_action) : fRunAction(run_action) {}

void RMGEventAction::BeginOfEventAction(const G4Event* event) {

Expand Down Expand Up @@ -79,10 +76,4 @@ void RMGEventAction::EndOfEventAction(const G4Event* event) {
// NOTE: G4analysisManager::AddNtupleRow() must be called here for event-oriented output
}

void RMGEventAction::DefineCommands() {

fMessenger = std::make_unique<G4GenericMessenger>(this, "/RMG/Output/",
"Commands for controlling the event actions");
}

// vim: tabstop=2 shiftwidth=2 expandtab
2 changes: 1 addition & 1 deletion src/RMGHardware.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ G4VPhysicalVolume* RMGHardware::Construct() {
fWorld = this->DefineGeometry();
if (!fWorld)
RMGLog::Out(RMGLog::fatal, "DefineGeometry() returned nullptr. ",
"Did you forget to reimplement the base class method?");
"Did you forget to reimplement the base class method, or to specify a GDML file?");
}

// TODO: build and return world volume?
Expand Down
1 change: 0 additions & 1 deletion src/RMGHardwareMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
RMGHardwareMessenger::RMGHardwareMessenger(RMGHardware* hw) : fHardware(hw) {
fRegisterCmd = new G4UIcommand("/RMG/Geometry/RegisterDetector", this);
fRegisterCmd->SetGuidance("register a sensitive detector");
fRegisterCmd->SetGuidance("[usage] /RMG/Geometry/RegisterDetector T PV ID [C]");

auto p_type = new G4UIparameter("type", 's', false);
p_type->SetParameterCandidates(RMGTools::GetCandidates<RMGHardware::DetectorType>().c_str());
Expand Down
25 changes: 15 additions & 10 deletions src/RMGManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,22 @@ void RMGManager::Initialize() {
}
}

std::unique_ptr<G4UIExecutive> RMGManager::StartInteractiveSession() {

RMGLog::Out(RMGLog::summary, "Entering interactive mode");
auto cval = std::getenv("DISPLAY");
auto val = cval == nullptr ? std::string("") : std::string(cval);
if (val.empty()) RMGLog::Out(RMGLog::warning, "DISPLAY not set, forcing G4UI_USE_TCSH=1");
return std::make_unique<G4UIExecutive>(fArgc, fArgv, val.empty() ? "tcsh" : "");
}

void RMGManager::Run() {

// desired behavior
// - by default (nothing is specified), open an interactive session
// - if macro is specified, run and quit
// - if macro is specified and fInteractive is true, do not quit afterwards
// - a macro can request interactive mode

// FIXME: logic here does not work. There is no way to do interactive visualization

Expand All @@ -122,13 +132,7 @@ void RMGManager::Run() {

// configure UI
std::unique_ptr<G4UIExecutive> session = nullptr;
if (fInteractive) {
RMGLog::Out(RMGLog::summary, "Entering interactive mode");
auto cval = std::getenv("DISPLAY");
auto val = cval == nullptr ? std::string("") : std::string(cval);
if (val.empty()) RMGLog::Out(RMGLog::warning, "DISPLAY not set, forcing G4UI_USE_TCSH=1");
session = std::make_unique<G4UIExecutive>(fArgc, fArgv, val.empty() ? "tcsh" : "");
}
if (fInteractive) { session = StartInteractiveSession(); }

// eventually execute macros
auto UI = G4UImanager::GetUIpointer();
Expand All @@ -139,6 +143,7 @@ void RMGManager::Run() {

// if interactive mode is requested, do not quit and start a session
if (fInteractive) {
if (!session) session = StartInteractiveSession();
session->SetPrompt(RMGLog::Colorize<RMGLog::Ansi::unspecified>("remage> ", G4cout, true));
session->SessionStart();
}
Expand Down Expand Up @@ -259,9 +264,9 @@ void RMGManager::DefineCommands() {
fMessenger = std::make_unique<G4GenericMessenger>(this, "/RMG/Manager/",
"General commands for controlling the application");

fMessenger->DeclareMethod("Interactive", &RMGManager::SetInteractive)
fMessenger->DeclareProperty("Interactive", fInteractive)
.SetGuidance("Enable interactive mode")
.SetParameterName("flag", true)
.SetParameterName("interactive", true)
.SetDefaultValue("true")
.SetStates(G4State_PreInit, G4State_Idle);

Expand All @@ -275,7 +280,7 @@ void RMGManager::DefineCommands() {
"Commands for controlling application logging");

fLogMessenger->DeclareMethod("LogLevel", &RMGManager::SetLogLevel)
.SetGuidance("Set verbosity level on screen")
.SetGuidance("Set verbosity level of application log")
.SetParameterName("level", false)
.SetCandidates(RMGTools::GetCandidates<RMGLog::LogLevel>())
.SetStates(G4State_PreInit, G4State_Idle);
Expand Down
16 changes: 10 additions & 6 deletions src/RMGPhysics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,23 @@ void RMGPhysics::DefineCommands() {
.SetCandidates(RMGTools::GetCandidates<RMGPhysics::LowEnergyEMOption>())
.SetStates(G4State_PreInit);

// TODO: upstream bug with bools in G4GenericMessenger (only numeric values work).
fMessenger->DeclareMethod("EnableGammaAngularCorrelation", &RMGPhysics::SetUseGammaAngCorr)
.SetGuidance("")
.SetStates(G4State_PreInit, G4State_Idle);
.SetGuidance("Set correlated gamma emission flag")
.SetCandidates("0 1")
.SetStates(G4State_PreInit);

fMessenger->DeclareMethod("GammaTwoJMAX", &RMGPhysics::SetGammaTwoJMAX)
.SetGuidance("")
.SetGuidance("Set max 2J for sampling of angular correlations")
.SetParameterName("x", false)
.SetRange("x > 0")
.SetStates(G4State_PreInit, G4State_Idle);
.SetStates(G4State_PreInit);

// TODO: upstream bug with bools in G4GenericMessenger (only numeric values work).
fMessenger->DeclareMethod("StoreICLevelData", &RMGPhysics::SetStoreICLevelData)
.SetGuidance("")
.SetStates(G4State_PreInit, G4State_Idle);
.SetGuidance("Store e- internal conversion data")
.SetCandidates("0 1")
.SetStates(G4State_PreInit);
}

// vim: shiftwidth=2 tabstop=2 expandtab

0 comments on commit 0691f18

Please sign in to comment.