Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Manager Factory #290

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions manager/Monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ void MaCh3Welcome() {
// KS: Get version of MaCh3
std::string GetMaCh3Version() {
// ************************

//KS: Find MaCh3 version based on header file. There could be better way to just include version.h but as long as we don't have to hardcode version I am content
std::string MaCh3_VERSION = "";

Expand Down Expand Up @@ -87,7 +86,6 @@ std::string GetMaCh3Version() {
// KS: Find out more about operational system
void GetOSInfo() {
// ************************

MACH3LOG_INFO("Operating System Information:");

// Distribution and version
Expand All @@ -99,7 +97,6 @@ void GetOSInfo() {
//KS: Simple function retrieving CPU info
void GetCPUInfo() {
// ************************

//KS: Use -m 1 to limit to only one grep because in one computing node there is a lot of CPU which are the same
MACH3LOG_INFO("Using following CPU:");

Expand All @@ -121,7 +118,6 @@ void GetCPUInfo() {
//KS: Simple function retrieving GPU info
void GetGPUInfo(){
// ************************

#ifdef CUDA
MACH3LOG_INFO("Using following GPU:");
// Print GPU name
Expand Down Expand Up @@ -170,7 +166,6 @@ std::string TerminalToString(std::string cmd) {
//KS: Simple to retrieve speed of get entry inspired by
void EstimateDataTransferRate(TChain* chain, const Long64_t entry){
// ************************

TStopwatch timer;

timer.Start();
Expand All @@ -188,7 +183,6 @@ void EstimateDataTransferRate(TChain* chain, const Long64_t entry){
//KS: Simply print progress bar
void PrintProgressBar(const Long64_t Done, const Long64_t All){
// ************************

double progress = double(Done)/double(All);
const int barWidth = 20;
std::ostringstream progressBar;
Expand Down Expand Up @@ -254,7 +248,6 @@ int getValue(const std::string& Type){ //Note: this value is in KB!
MACH3LOG_ERROR("Not supported getValue: {}", Type);
throw MaCh3Exception(__FILE__, __LINE__);
}

return result;
}

Expand Down
73 changes: 73 additions & 0 deletions mcmc/MaCh3Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,76 @@ covarianceXsec* MaCh3CovarianceFactory(manager *FitManager, const std::string& P
// ********************************************
return MaCh3CovarianceFactory<covarianceXsec>(FitManager, PreFix);
}

// ********************************************
std::unique_ptr<manager> MaCh3ManagerFactory(int argc, char **argv) {
// ********************************************
if (argc < 2) {
MACH3LOG_ERROR("Wrong usage of MaCh3 executable!");
MACH3LOG_ERROR("Syntax is $: {} config.yaml", argv[0]);
MACH3LOG_ERROR("Where config.yaml is a valid config file, compatible with the manager class (manager/manager.cpp/h)");
throw MaCh3Exception(__FILE__, __LINE__);
}

// Initialise manger responsible for config handling
auto FitManager = std::make_unique<manager>(argv[1]);

//KS: Lambda to make sure we are not overwriting setting which should be committed
auto SanityOverwrite = [](const std::string& Name) {
if (Name.find("Systematics") != std::string::npos ||
Name.find("Samples") != std::string::npos)
{
MACH3LOG_CRITICAL("You are overwriting settings ({}) that are highly likely intended to be committed.", Name);
throw MaCh3Exception(__FILE__ , __LINE__ );
}
};

for (int i = 2; i < argc; ++i)
{
const std::string arg = argv[i];
const size_t colonCount = std::count(arg.begin(), arg.end(), ':');

/// @todo KS: May need some recursive magic to reduce amount of hardcoding
if (colonCount == 1) {
const size_t firstColon = arg.find(':');
const std::string section = arg.substr(0, firstColon);
const std::string value = arg.substr(firstColon + 1);

MACH3LOG_INFO("Overriding setting: Section={}, Value={}", section, value);
SanityOverwrite(section);
FitManager->OverrideSettings(section, value);
} else if (colonCount == 2) {
const size_t firstColon = arg.find(':');
const size_t secondColon = arg.find(':', firstColon + 1);

const std::string section = arg.substr(0, firstColon);
const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
const std::string value = arg.substr(secondColon + 1);

MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Value={}", section, key, value);
SanityOverwrite(section);
SanityOverwrite(key);
FitManager->OverrideSettings(section, key, value);
} else if (colonCount == 3) {
const size_t firstColon = arg.find(':');
const size_t secondColon = arg.find(':', firstColon + 1);
const size_t thridColon = arg.find(':', secondColon + 1);

const std::string section = arg.substr(0, firstColon);
const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
const std::string key2 = arg.substr(secondColon + 1, thridColon - secondColon - 1);
const std::string value = arg.substr(thridColon + 1);

MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Key={}, Value={}", section, key, key2, value);
SanityOverwrite(section);
SanityOverwrite(key);
SanityOverwrite(key2);
FitManager->OverrideSettings(section, key, key2, value);
} else {
MACH3LOG_ERROR("Invalid override argument format: {}", arg);
MACH3LOG_ERROR("Expected format:Section:Key:Key:Valu, Section:Key:Value or Section:Value");
throw MaCh3Exception(__FILE__, __LINE__);
}
}
return FitManager;
}
14 changes: 14 additions & 0 deletions mcmc/MaCh3Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "covariance/covarianceXsec.h"
#include "covariance/covarianceOsc.h"

/// @file MaCh3Factory.h
/// @brief Factory methods for MaCh3 software which streamline initialisation of different objects
/// @author Kamil Skwarczynski

/// @brief MaCh3 Factory initiates one of implemented fitting algorithms
/// @param fitMan pointer to Manager class
Expand All @@ -24,6 +27,17 @@
/// FittingAlgorithm: ["MCMC"]
std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan);

/// @brief Initializes the config manager class and allows overriding settings via command-line arguments.
/// @param argc number of arguments
/// @param argv name of arguments
/// @return A unique pointer to the initialized `manager` instance with optional overrides applied.
/// @example Usage examples:
/// ```
/// ./bin/MCMCTutorial Inputs/FitterConfig.yaml General:OutputFile:blarb.root
/// ./bin/MCMCTutorial Inputs/FitterConfig.yaml General:OutputFile:blarb.root General:MCMC:NSteps:50000
/// ```
std::unique_ptr<manager> MaCh3ManagerFactory(int argc, char **argv);

/// @brief Factory function for creating a covariance class for systematic handling.
covarianceXsec* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix);

Expand Down
Loading