MaCh3  2.2.3
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)
 
std::unique_ptr< managerMaCh3ManagerFactory (int argc, char **argv)
 

Function Documentation

◆ MaCh3FitterFactory()

std::unique_ptr<FitterBase> MaCh3FitterFactory ( manager fitMan)

Definition at line 5 of file MaCh3Factory.cpp.

5  {
6 // ********************************************
7  std::unique_ptr<FitterBase> MaCh3Fitter = nullptr;
8 
9  auto Algorithm = GetFromManager<std::string>(fitMan->raw()["General"]["FittingAlgorithm"], "MCMC");
10 
11  if(Algorithm == "MCMC" || Algorithm == "MR2T2")
12  {
13  MaCh3Fitter = std::make_unique<MR2T2>(fitMan);
14  }
15  else if (Algorithm == "DelayedMCMC" || Algorithm == "DelayedMR2T2")
16  {
17  MaCh3Fitter = std::make_unique<DelayedMR2T2>(fitMan);
18  }
19  else if (Algorithm == "PSO")
20  {
21  MaCh3Fitter = std::make_unique<PSO>(fitMan);
22  }
23  else if (Algorithm == "Minuit2")
24  {
25  #ifdef MaCh3_MINUIT2
26  MaCh3Fitter = std::make_unique<MinuitFit>(fitMan);
27  #else
28  MACH3LOG_ERROR("Trying to use Minuit2 however MaCh3 was compiled without Minuit2 support");
29  throw MaCh3Exception(__FILE__ , __LINE__ );
30  #endif
31  }
32  else
33  {
34  MACH3LOG_ERROR("You want to use algorithm {}, I don't recognize it, sry", Algorithm);
35  throw MaCh3Exception(__FILE__ , __LINE__ );
36  }
37  return MaCh3Fitter;
38 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:27
Custom exception class for MaCh3 errors.
YAML::Node const & raw()
Return config.
Definition: Manager.h:41

◆ MaCh3ManagerFactory()

std::unique_ptr<manager> MaCh3ManagerFactory ( int  argc,
char **  argv 
)
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);
83  throw MaCh3Exception(__FILE__ , __LINE__ );
84  }
85  };
86 
87  for (int i = 2; i < argc; ++i)
88  {
89  const std::string arg = argv[i];
90  const size_t colonCount = std::count(arg.begin(), arg.end(), ':');
91 
93  if (colonCount == 1) {
94  const size_t firstColon = arg.find(':');
95  const std::string section = arg.substr(0, firstColon);
96  const std::string value = arg.substr(firstColon + 1);
97 
98  MACH3LOG_INFO("Overriding setting: Section={}, Value={}", section, value);
99  SanityOverwrite(section);
100  FitManager->OverrideSettings(section, value);
101  } else if (colonCount == 2) {
102  const size_t firstColon = arg.find(':');
103  const size_t secondColon = arg.find(':', firstColon + 1);
104 
105  const std::string section = arg.substr(0, firstColon);
106  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
107  const std::string value = arg.substr(secondColon + 1);
108 
109  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Value={}", section, key, value);
110  SanityOverwrite(section);
111  SanityOverwrite(key);
112  FitManager->OverrideSettings(section, key, value);
113  } else if (colonCount == 3) {
114  const size_t firstColon = arg.find(':');
115  const size_t secondColon = arg.find(':', firstColon + 1);
116  const size_t thridColon = arg.find(':', secondColon + 1);
117 
118  const std::string section = arg.substr(0, firstColon);
119  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
120  const std::string key2 = arg.substr(secondColon + 1, thridColon - secondColon - 1);
121  const std::string value = arg.substr(thridColon + 1);
122 
123  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Key={}, Value={}", section, key, key2, value);
124  SanityOverwrite(section);
125  SanityOverwrite(key);
126  SanityOverwrite(key2);
127  FitManager->OverrideSettings(section, key, key2, value);
128  } else {
129  MACH3LOG_ERROR("Invalid override argument format: {}", arg);
130  MACH3LOG_ERROR("Expected format:Section:Key:Key:Value, Section:Key:Value or Section:Value");
131  throw MaCh3Exception(__FILE__, __LINE__);
132  }
133  }
134  return FitManager;
135 }
#define MACH3LOG_CRITICAL
Definition: MaCh3Logger.h:28
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:25
YAML::Node MergeNodes(YAML::Node a, YAML::Node b)
KS: Recursively merges two YAML nodes.
Definition: YamlHelper.h:406
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:561