Skip to content

Commit

Permalink
Remove unnecessary std::move and add custom messenger in RMGGrabmayrG…
Browse files Browse the repository at this point in the history
…CReader
  • Loading branch information
EricMEsch committed Sep 12, 2024
1 parent e5e983d commit 83eafe1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 55 deletions.
30 changes: 19 additions & 11 deletions docs/rmg-commands.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/06-NeutronCapture/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.8)
project(TestNeutronCapture)
project(06-NeutronCapture)

set(CMAKE_BUILD_TYPE RelWithDebInfo)

find_package(remage)

add_executable(TestNeutronCapture main.cc HPGeTestStand.hh HPGeTestStand.cc IsotopeOutputScheme.cc
IsotopeOutputScheme.hh)
target_link_libraries(TestNeutronCapture PUBLIC RMG::remage)
add_executable(06-NeutronCapture main.cc HPGeTestStand.hh HPGeTestStand.cc IsotopeOutputScheme.cc
IsotopeOutputScheme.hh)
target_link_libraries(06-NeutronCapture PUBLIC RMG::remage)
2 changes: 1 addition & 1 deletion examples/06-NeutronCapture/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main(int argc, char** argv) {

// RMGLog::SetLogLevel(RMGLog::debug);

RMGManager manager("TestNeutronCapture", argc, argv);
RMGManager manager("06-NeutronCapture", argc, argv);
manager.SetUserInit(new HPGeTestStand());

auto user_init = manager.GetUserInit();
Expand Down
4 changes: 2 additions & 2 deletions examples/06-NeutronCapture/runRMGnCapture.mac
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

/RMG/Output/ActivateOutputScheme IsotopeOutputScheme

/RMG/GrabmayrGammaCascades/SetGammaCascadeFile 64,155,GammaCascades/156Gd-100Entries.txt
/RMG/GrabmayrGammaCascades/SetGammaCascadeFile 64,157,GammaCascades/158Gd-100Entries.txt
/RMG/GrabmayrGammaCascades/SetGammaCascadeFile 64 155 GammaCascades/156Gd-100Entries.txt
/RMG/GrabmayrGammaCascades/SetGammaCascadeFile 64 157 GammaCascades/158Gd-100Entries.txt
/RMG/GrabmayrGammaCascades/SetGammaCascadeRandomStartLocation 1
/run/initialize

Expand Down
25 changes: 22 additions & 3 deletions include/RMGGrabmayrGCReader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,34 @@ class RMGGrabmayrGCReader {
// std::vector<std::unique_ptr<std::ifstream>> files;
// map holding the corresponding file for each isotope
std::map<std::pair<G4int, G4int>, std::unique_ptr<std::ifstream>> fCascadeFiles;
std::unique_ptr<G4GenericMessenger> fMessenger;
std::unique_ptr<G4GenericMessenger> fGenericMessenger;
G4int fGammaCascadeRandomStartLocation = 0;

void SetGammaCascadeFile(const G4String& params);
void SetGammaCascadeFile(const G4int z, const G4int a, const G4String file_name);
void SetGammaCascadeRandomStartLocation(const int answer);
std::unique_ptr<std::ifstream> SetStartLocation(std::unique_ptr<std::ifstream> file);
void SetStartLocation(std::ifstream& file);

void RandomizeFiles();
void DefineCommands();

// Nested messenger class
class GCMessenger : public G4UImessenger {
public:

GCMessenger(RMGGrabmayrGCReader* reader);
~GCMessenger();

void SetNewValue(G4UIcommand* command, G4String newValues) override;

private:

RMGGrabmayrGCReader* fReader;
G4UIcommand* fGammaFileCmd;

void GammaFileCmd(const std::string& parameters);
};

std::unique_ptr<GCMessenger> fUIMessenger;
};


Expand Down
90 changes: 56 additions & 34 deletions src/RMGGrabmayrGCReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,64 +70,54 @@ GammaCascadeLine RMGGrabmayrGCReader::GetNextEntry(G4int z, G4int a) {
}


// Is there a better way dealing with the unique pointer?
std::unique_ptr<std::ifstream> RMGGrabmayrGCReader::SetStartLocation(
std::unique_ptr<std::ifstream> file) {
if (!file || !file->is_open())
void RMGGrabmayrGCReader::SetStartLocation(std::ifstream& file) {
if (!file.is_open())
RMGLog::Out(RMGLog::fatal, "The file is not open to set start location! Exit.");
file->clear(); // clear EOF flag
file->seekg(0, std::ios::beg); // move to beginning of file
file.clear(); // clear EOF flag
file.seekg(0, std::ios::beg); // move to beginning of file
// Skip Header
std::string line;
int header_length = 0;
do {
std::getline(*file, line);
std::getline(file, line);
header_length++;
} while (line[0] == '%' || (line.find("version") != std::string::npos));

// In case the Random start location macro is set
if (fGammaCascadeRandomStartLocation) {
int n_entries_in_file = 0;
// Seems excessiv to read through the entire file, there has to be a quicker way?
while (std::getline(*file, line)) n_entries_in_file++;
while (std::getline(file, line)) n_entries_in_file++;

file->clear(); // clear EOF flag
file->seekg(0, std::ios::beg); // move to beginning of file
file.clear(); // clear EOF flag
file.seekg(0, std::ios::beg); // move to beginning of file

int start_location = (int)(n_entries_in_file * G4UniformRand() + header_length);

RMGLog::Out(RMGLog::detail, "Random start location: ", start_location);
for (int j = 0; j < start_location; j++) std::getline(*file, line);
for (int j = 0; j < start_location; j++) std::getline(file, line);
}
return std::move(file);
}

void RMGGrabmayrGCReader::SetGammaCascadeFile(const G4String& params) {
// Convert token to Z, A and path/to/file
G4Tokenizer tokenizer(params);
G4String zStr = tokenizer(",");
G4String aStr = tokenizer(",");
G4String file_name = tokenizer(" \t\n");
G4int z = G4UIcommand::ConvertToInt(zStr);
G4int a = G4UIcommand::ConvertToInt(aStr);
void RMGGrabmayrGCReader::SetGammaCascadeFile(const G4int z, const G4int a, const G4String file_name) {

RMGLog::Out(RMGLog::detail, "Opening file ", file_name);
std::unique_ptr<std::ifstream> file = std::make_unique<std::ifstream>(file_name);

if (z == 0 || a == 0)
RMGLog::Out(RMGLog::fatal, "Isotope Z:" + std::to_string(z) + " A: " + std::to_string(a) +
" does not exist. Are you sure you used the correct format?");
RMGLog::Out(RMGLog::fatal,
"Isotope Z:" + std::to_string(z) + " A: " + std::to_string(a) + " does not exist.");
if (!file || !file->is_open())
RMGLog::Out(RMGLog::fatal, "Gamma cascade file: " + file_name + " not found! Exit.");

file = SetStartLocation(std::move(file));
SetStartLocation(*file);

fCascadeFiles.insert({{z, a}, std::move(file)});
}

void RMGGrabmayrGCReader::RandomizeFiles() {
RMGLog::Out(RMGLog::detail, "(Un)-Randomizing start locations");
for (auto& el : fCascadeFiles) { el.second = SetStartLocation(std::move(el.second)); }
for (auto& el : fCascadeFiles) { SetStartLocation(*el.second); }
}

void RMGGrabmayrGCReader::SetGammaCascadeRandomStartLocation(const int answer) {
Expand All @@ -138,18 +128,10 @@ void RMGGrabmayrGCReader::SetGammaCascadeRandomStartLocation(const int answer) {
}

void RMGGrabmayrGCReader::DefineCommands() {
fMessenger = std::make_unique<G4GenericMessenger>(this, "/RMG/GrabmayrGammaCascades/",
fGenericMessenger = std::make_unique<G4GenericMessenger>(this, "/RMG/GrabmayrGammaCascades/",
"Control Peters gamma cascade model");

fMessenger->DeclareMethod("SetGammaCascadeFile", &RMGGrabmayrGCReader::SetGammaCascadeFile)
.SetGuidance("Set the Z, A and /path/to/file for the gamma cascade upon neutron capture on "
"Isotope Z, A Format: Z,A,/path/to/file")
.SetParameterName("Z,A,/path/to/file", false)
.SetDefaultValue("64,155,/path/to/file.txt")
.SetStates(G4State_PreInit, G4State_Idle);


fMessenger
fGenericMessenger
->DeclareMethod("SetGammaCascadeRandomStartLocation",
&RMGGrabmayrGCReader::SetGammaCascadeRandomStartLocation)
.SetGuidance("Set the whether the start location in the gamma cascade file is random or not")
Expand All @@ -158,4 +140,44 @@ void RMGGrabmayrGCReader::DefineCommands() {
.SetCandidates("0 1")
.SetDefaultValue("0")
.SetStates(G4State_PreInit, G4State_Idle);

// SetGammaCascadeFile cannot be defined with the G4GenericMessenger (it has to many parameters).
fUIMessenger = std::make_unique<GCMessenger>(this);
}


RMGGrabmayrGCReader::GCMessenger::GCMessenger(RMGGrabmayrGCReader* reader) : fReader(reader) {
fGammaFileCmd = new G4UIcommand("/RMG/GrabmayrGammaCascades/SetGammaCascadeFile", this);
fGammaFileCmd->SetGuidance("Set the Z, A and /path/to/file for the gamma cascade employed upon "
"neutron capture on said isotope");

auto p_Z = new G4UIparameter("Z", 'i', false);
p_Z->SetGuidance("Z of isotope");
fGammaFileCmd->SetParameter(p_Z);

auto p_A = new G4UIparameter("A", 'i', false);
p_A->SetGuidance("A of isotope");
fGammaFileCmd->SetParameter(p_A);

auto p_file = new G4UIparameter("file", 's', false);
p_file->SetGuidance("/path/to/file of gamma cascade");
fGammaFileCmd->SetParameter(p_file);

fGammaFileCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
}

RMGGrabmayrGCReader::GCMessenger::~GCMessenger() { delete fGammaFileCmd; }

void RMGGrabmayrGCReader::GCMessenger::SetNewValue(G4UIcommand* command, G4String newValues) {
if (command == fGammaFileCmd) GammaFileCmd(newValues);
}

void RMGGrabmayrGCReader::GCMessenger::GammaFileCmd(const std::string& parameters) {
G4Tokenizer next(parameters);

auto Z = std::stoi(next());
auto A = std::stoi(next());
auto file = next();

fReader->SetGammaCascadeFile(Z, A, file);
}

0 comments on commit 83eafe1

Please sign in to comment.