MaCh3  2.6.0
Reference Guide
Functions
MaCh3Factory.cpp File Reference
#include "Fitters/MaCh3Factory.h"
Include dependency graph for MaCh3Factory.cpp:

Go to the source code of this file.

Functions

std::unique_ptr< FitterBaseMaCh3FitterFactory (Manager *fitMan)
 MaCh3 Factory initiates one of implemented fitting algorithms. More...
 
std::unique_ptr< ManagerMaCh3ManagerFactory (int argc, char **argv)
 Initializes the config Manager class and allows overriding settings via command-line arguments. More...
 

Function Documentation

◆ MaCh3ManagerFactory()

std::unique_ptr<Manager> MaCh3ManagerFactory ( int  argc,
char **  argv 
)

Initializes the config Manager class and allows overriding settings via command-line arguments.

Parameters
argcnumber of arguments
argvname of arguments
Returns
A unique pointer to the initialized Manager instance with optional overrides applied.
Note
Usage examples:
./bin/MCMCTutorial Inputs/FitterConfig.yaml General:OutputFile:blarb.root
./bin/MCMCTutorial Inputs/FitterConfig.yaml General:OutputFile:blarb.root General:MCMC:NSteps:50000
Todo:
DL: Should probably replace this with something that doesn't require modifying core code
Todo:
KS: May need some recursive magic to reduce amount of hardcoding

Definition at line 41 of file MaCh3Factory.cpp.

41  {
42 // ********************************************
43  if (argc < 2) {
44  MACH3LOG_ERROR("Wrong usage of MaCh3 executable!");
45  MACH3LOG_ERROR("Syntax is $: {} config.yaml", argv[0]);
46  MACH3LOG_ERROR("Where config.yaml is a valid config file, compatible with the Manager class (Manager/Manager.cpp/h)");
47  throw MaCh3Exception(__FILE__, __LINE__);
48  }
49 
50  // Check if we are using --override mode
51  if (argc >= 4 && std::string(argv[2]) == "--override") {
52  const std::string overrideFile = argv[3];
53  MACH3LOG_INFO("Merging configuration files: base config '{}', override config '{}'. "
54  "Options in '{}' will take precedence over '{}'.",
55  argv[1], overrideFile, overrideFile, argv[1]);
56 
57  if(argc > 4) {
58  MACH3LOG_ERROR("Too many arguments provided when using '--override'. "
59  "Expected only two config files. "
60  "If using override feature, you cannot provide any additional arguments.");
61  throw MaCh3Exception(__FILE__, __LINE__);
62  }
63  // Load the two YAML files
64  YAML::Node config1 = M3OpenConfig(argv[1]);
65  YAML::Node config2 = M3OpenConfig(overrideFile);
66 
67  // Merge them
68  YAML::Node merged = MergeNodes(config1, config2);
69  auto FitManager = std::make_unique<Manager>(merged);
70 
71  return FitManager;
72  }
73 
74  // Initialise manger responsible for config handling
75  auto FitManager = std::make_unique<Manager>(argv[1]);
76 
77  //KS: Lambda to make sure we are not overwriting setting which should be committed
78  auto SanityOverwrite = [](const std::string& Name) {
79  if (Name.find("Systematics") != std::string::npos ||
80  Name.find("Samples") != std::string::npos)
81  {
82  MACH3LOG_CRITICAL("You are overwriting settings ({}) that are highly likely intended to be committed.", Name);
84  MACH3LOG_CRITICAL("If you're sure you want to do this, e.g. for testing or step size tuning, you can remove the throw that lives here:");
85  throw MaCh3Exception(__FILE__ , __LINE__ );
86  }
87  };
88 
89  for (int i = 2; i < argc; ++i)
90  {
91  const std::string arg = argv[i];
92  const size_t colonCount = std::count(arg.begin(), arg.end(), ':');
93 
95  if (colonCount == 1) {
96  const size_t firstColon = arg.find(':');
97  const std::string section = arg.substr(0, firstColon);
98  const std::string value = arg.substr(firstColon + 1);
99 
100  MACH3LOG_INFO("Overriding setting: Section={}, Value={}", section, value);
101  SanityOverwrite(section);
102  FitManager->OverrideSettings(section, value);
103  } else if (colonCount == 2) {
104  const size_t firstColon = arg.find(':');
105  const size_t secondColon = arg.find(':', firstColon + 1);
106 
107  const std::string section = arg.substr(0, firstColon);
108  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
109  const std::string value = arg.substr(secondColon + 1);
110 
111  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Value={}", section, key, value);
112  SanityOverwrite(section);
113  SanityOverwrite(key);
114  FitManager->OverrideSettings(section, key, value);
115  } else if (colonCount == 3) {
116  const size_t firstColon = arg.find(':');
117  const size_t secondColon = arg.find(':', firstColon + 1);
118  const size_t thridColon = arg.find(':', secondColon + 1);
119 
120  const std::string section = arg.substr(0, firstColon);
121  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
122  const std::string key2 = arg.substr(secondColon + 1, thridColon - secondColon - 1);
123  const std::string value = arg.substr(thridColon + 1);
124 
125  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Key={}, Value={}", section, key, key2, value);
126  SanityOverwrite(section);
127  SanityOverwrite(key);
128  SanityOverwrite(key2);
129  FitManager->OverrideSettings(section, key, key2, value);
130  } else if (colonCount == 4) {
131  const size_t firstColon = arg.find(':');
132  const size_t secondColon = arg.find(':', firstColon + 1);
133  const size_t thirdColon = arg.find(':', secondColon + 1);
134  const size_t fourthColon = arg.find(':', thirdColon + 1);
135 
136  const std::string section = arg.substr(0, firstColon);
137  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
138  const std::string key2 = arg.substr(secondColon + 1, thirdColon - secondColon - 1);
139  const std::string key3 = arg.substr(thirdColon + 1, fourthColon - thirdColon - 1);
140  const std::string value = arg.substr(fourthColon + 1);
141 
143  "Overriding setting: Section={}, Key={}, Key={}, Key={}, Value={}",
144  section, key, key2, key3, value);
145 
146  SanityOverwrite(section);
147  SanityOverwrite(key);
148  SanityOverwrite(key2);
149  SanityOverwrite(key3);
150 
151  FitManager->OverrideSettings(section, key, key2, key3, value);
152  } else {
153  MACH3LOG_ERROR("Invalid override argument format: {}", arg);
154  MACH3LOG_ERROR("Expected format:Section:Key:Key:Value, Section:Key:Value or Section:Value");
155  throw MaCh3Exception(__FILE__, __LINE__);
156  }
157  }
158  return FitManager;
159 }
#define MACH3LOG_CRITICAL
Definition: MaCh3Logger.h:38
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
YAML::Node MergeNodes(const YAML::Node &a, const YAML::Node &b)
KS: Recursively merges two YAML nodes.
Definition: YamlHelper.h:434
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:589
Custom exception class used throughout MaCh3.