MaCh3  2.8.0
Reference Guide
Functions
MaCh3Factory.h File Reference

Factory methods for MaCh3 software which streamline initialisation of different objects. More...

#include "Algorithms/FitterBase.h"
#include "Algorithms/MR2T2.h"
#include "Algorithms/DelayedMR2T2.h"
#include "Algorithms/PSO.h"
#include "Algorithms/LikelihoodFit.h"
#include "Parameters/ParameterHandlerGeneric.h"
Include dependency graph for MaCh3Factory.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::unique_ptr< FitterBase > MaCh3FitterFactory (Manager *FitManager)
 MaCh3 Factory initiates one of implemented fitting algorithms. More...
 
std::unique_ptr< Manager > MaCh3ManagerFactory (int argc, char **argv)
 Initializes the config Manager class and allows overriding settings via command-line arguments. More...
 
template<typename CovType >
std::unique_ptr< CovType > MaCh3CovarianceFactory (Manager *FitManager, const std::string &PreFix)
 Factory function for creating a covariance class for systematic handling. More...
 
template<typename SampleType >
std::vector< SampleType * > MaCh3SampleHandlerFactory (const std::vector< std::string > &SampleConfig, ParameterHandlerGeneric *xsec)
 Factory function for creating SampleHandler and initialisation with systematic. More...
 

Detailed Description

Factory methods for MaCh3 software which streamline initialisation of different objects.

Author
Kamil Skwarczynski

Definition in file MaCh3Factory.h.

Function Documentation

◆ MaCh3CovarianceFactory()

template<typename CovType >
std::unique_ptr<CovType> MaCh3CovarianceFactory ( Manager *  FitManager,
const std::string &  PreFix 
)

Factory function for creating a covariance class for systematic handling.

Parameters
FitManagerPointer to the Manager class that holds the configuration settings.
PreFixPrefix, for example Xsec, then code will look for XsecCovFile
Returns
Pointer to the initialized covarianceXsec matrix object.
Note
Example YAML configuration:
General:
Systematics:
XsecCovName: "xsec_cov"
XsecCovFile: ["inputs/blarb1.yaml",
"inputs/blarb2.yaml"]
XsecFix: ["Param_0",
"Param_1"]
XsecPCAThreshold: -1
#XsecPCAThreshold: 0.00001
XsecPCAParams: [-999, -999]
XsecStepScale: 0.0075
std::unique_ptr< CovType > MaCh3CovarianceFactory(Manager *FitManager, const std::string &PreFix)
Factory function for creating a covariance class for systematic handling.
Definition: MaCh3Factory.h:69
Todo:
add adaptive stuff

Definition at line 69 of file MaCh3Factory.h.

69  {
70 // ********************************************
71  // config for our matrix
72  YAML::Node Settings = FitManager->raw()["General"]["Systematics"];
73  auto CovMatrixName = Get<std::string>(Settings[std::string(PreFix) + "CovName"], __FILE__, __LINE__);
74  MACH3LOG_INFO("Initialising {} matrix", CovMatrixName);
75 
76  // yaml files initialising out matrix
77  auto CovMatrixFile = Get<std::vector<std::string>>(Settings[std::string(PreFix) + "CovFile"], __FILE__, __LINE__);
78 
79  // PCA threshold, -1 means no pca
80  auto PCAThreshold = GetFromManager<int>(Settings[std::string(PreFix) + "PCAThreshold"], -1, __FILE__ , __LINE__);
81  // do we pca whole matrix or only submatrix
82  auto PCAParamRegion = GetFromManager<std::vector<int>>(Settings[std::string(PreFix) + "PCAParams"], {-999, -999}, __FILE__ , __LINE__);
83 
84  auto CovObject = std::make_unique<CovType>(CovMatrixFile, CovMatrixName, PCAThreshold, PCAParamRegion[0], PCAParamRegion[1]);
85 
86  // Fill the parameter values with their nominal values
87  // should _ALWAYS_ be done before overriding with fix or flat
88  CovObject->SetParameters();
89 
90  auto FixParams = GetFromManager<std::vector<std::string>>(Settings[std::string(PreFix) + "Fix"], {}, __FILE__ , __LINE__);
91 
92  // Fixed CovObject parameters loop
93  if (FixParams.size() == 1 && FixParams.at(0) == "All") {
94  CovObject->SetFixAllParameters();
95  } else {
96  for (unsigned int j = 0; j < FixParams.size(); j++) {
97  CovObject->SetFixParameter(FixParams.at(j));
98  }
99  }
100 
101  if (CheckNodeExists(Settings, std::string(PreFix) + "Tune"))
102  {
103  CovObject->SetTune(Get<std::string>(Settings[std::string(PreFix) + "Tune"], __FILE__, __LINE__));
104  }
105 
106  //Global step scale for matrix
107  auto StepScale = Get<double>(Settings[std::string(PreFix) + "StepScale"], __FILE__, __LINE__);
108 
109  CovObject->SetStepScale(StepScale);
110 
111  // Adaptive MCMC stuff
112  if (CheckNodeExists(FitManager->raw(), "AdaptionOptions")) {
113  CovObject->InitialiseAdaption(FitManager->raw());
114  }
115 
116  return CovObject;
117 }
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
bool CheckNodeExists(const YAML::Node &node, Args... args)
KS: Wrapper function to call the recursive helper.
Definition: YamlHelper.h:59
YAML::Node const & raw() const
Return config.
Definition: Manager.h:48

◆ 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  std::string arg2;
51  if (argc > 2) {
52  arg2 = argv[2];
53  }
54  // Check if we are using --override mode, or for some CLI whether we simply passed .yaml
55  if ((argc == 4 && arg2 == "--override") ||
56  (argc == 3 && arg2.size() >= 5 && arg2.compare(arg2.size() - 5, 5, ".yaml") == 0)) {
57  std::string overrideFile;
58 
59  if (arg2 == "--override") {
60  overrideFile = argv[3];
61  } else {
62  overrideFile = arg2;
63  }
64  MACH3LOG_INFO("Merging configuration files: base config '{}', override config '{}'. "
65  "Options in '{}' will take precedence over '{}'.",
66  argv[1], overrideFile, overrideFile, argv[1]);
67 
68  // Load the two YAML files
69  YAML::Node config1 = M3OpenConfig(argv[1]);
70  YAML::Node config2 = M3OpenConfig(overrideFile);
71 
72  // Merge them
73  YAML::Node merged = MergeNodes(config1, config2);
74  auto FitManager = std::make_unique<Manager>(merged);
75 
76  return FitManager;
77  }
78 
79  // Initialise manger responsible for config handling
80  auto FitManager = std::make_unique<Manager>(argv[1]);
81 
82  //KS: Lambda to make sure we are not overwriting setting which should be committed
83  auto SanityOverwrite = [](const std::string& Name) {
84  if (Name.find("Systematics") != std::string::npos ||
85  Name.find("Samples") != std::string::npos)
86  {
87  MACH3LOG_CRITICAL("You are overwriting settings ({}) that are highly likely intended to be committed.", Name);
89  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:");
90  throw MaCh3Exception(__FILE__ , __LINE__ );
91  }
92  };
93 
94  for (int i = 2; i < argc; ++i)
95  {
96  const std::string arg = argv[i];
97  const size_t colonCount = std::count(arg.begin(), arg.end(), ':');
98 
100  if (colonCount == 1) {
101  const size_t firstColon = arg.find(':');
102  const std::string section = arg.substr(0, firstColon);
103  const std::string value = arg.substr(firstColon + 1);
104 
105  MACH3LOG_INFO("Overriding setting: Section={}, Value={}", section, value);
106  SanityOverwrite(section);
107  FitManager->OverrideSettings(section, value);
108  } else if (colonCount == 2) {
109  const size_t firstColon = arg.find(':');
110  const size_t secondColon = arg.find(':', firstColon + 1);
111 
112  const std::string section = arg.substr(0, firstColon);
113  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
114  const std::string value = arg.substr(secondColon + 1);
115 
116  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Value={}", section, key, value);
117  SanityOverwrite(section);
118  SanityOverwrite(key);
119  FitManager->OverrideSettings(section, key, value);
120  } else if (colonCount == 3) {
121  const size_t firstColon = arg.find(':');
122  const size_t secondColon = arg.find(':', firstColon + 1);
123  const size_t thridColon = arg.find(':', secondColon + 1);
124 
125  const std::string section = arg.substr(0, firstColon);
126  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
127  const std::string key2 = arg.substr(secondColon + 1, thridColon - secondColon - 1);
128  const std::string value = arg.substr(thridColon + 1);
129 
130  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Key={}, Value={}", section, key, key2, value);
131  SanityOverwrite(section);
132  SanityOverwrite(key);
133  SanityOverwrite(key2);
134  FitManager->OverrideSettings(section, key, key2, value);
135  } else if (colonCount == 4) {
136  const size_t firstColon = arg.find(':');
137  const size_t secondColon = arg.find(':', firstColon + 1);
138  const size_t thirdColon = arg.find(':', secondColon + 1);
139  const size_t fourthColon = arg.find(':', thirdColon + 1);
140 
141  const std::string section = arg.substr(0, firstColon);
142  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
143  const std::string key2 = arg.substr(secondColon + 1, thirdColon - secondColon - 1);
144  const std::string key3 = arg.substr(thirdColon + 1, fourthColon - thirdColon - 1);
145  const std::string value = arg.substr(fourthColon + 1);
146 
147  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Key={}, Key={}, Value={}",
148  section, key, key2, key3, value);
149 
150  SanityOverwrite(section);
151  SanityOverwrite(key);
152  SanityOverwrite(key2);
153  SanityOverwrite(key3);
154 
155  FitManager->OverrideSettings(section, key, key2, key3, value);
156  } else if (colonCount == 5) {
157  const size_t firstColon = arg.find(':');
158  const size_t secondColon = arg.find(':', firstColon + 1);
159  const size_t thirdColon = arg.find(':', secondColon + 1);
160  const size_t fourthColon = arg.find(':', thirdColon + 1);
161  const size_t fifthColon = arg.find(':', fourthColon + 1);
162 
163  const std::string section = arg.substr(0, firstColon);
164  const std::string key = arg.substr(firstColon + 1, secondColon - firstColon - 1);
165  const std::string key2 = arg.substr(secondColon + 1, thirdColon - secondColon - 1);
166  const std::string key3 = arg.substr(thirdColon + 1, fourthColon - thirdColon - 1);
167  const std::string key4 = arg.substr(fourthColon + 1, fifthColon - fourthColon - 1);
168  const std::string value = arg.substr(fifthColon + 1);
169 
170  MACH3LOG_INFO("Overriding setting: Section={}, Key={}, Key={}, Key={}, Key={}, Value={}",
171  section, key, key2, key3, key4, value);
172 
173  SanityOverwrite(section);
174  SanityOverwrite(key);
175  SanityOverwrite(key2);
176  SanityOverwrite(key3);
177  SanityOverwrite(key4);
178 
179  FitManager->OverrideSettings(section, key, key2, key3, key4, value);
180  } else {
181  MACH3LOG_ERROR("Invalid override argument format: {}", arg);
182  MACH3LOG_ERROR("Expected format:Section:Key:Key:Value, Section:Key:Value or Section:Value");
183  throw MaCh3Exception(__FILE__, __LINE__);
184  }
185  }
186  return FitManager;
187 }
#define MACH3LOG_CRITICAL
Definition: MaCh3Logger.h:38
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
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.

◆ MaCh3SampleHandlerFactory()

template<typename SampleType >
std::vector<SampleType*> MaCh3SampleHandlerFactory ( const std::vector< std::string > &  SampleConfig,
ParameterHandlerGeneric *  xsec 
)

Factory function for creating SampleHandler and initialisation with systematic.

Template Parameters
SampleTypeThe class type of the sample to create, e.g., SampleHandlerTutorial.
Parameters
SampleConfigPath to sample config.
xsecA pointer to a ParameterHandlerGeneric object for cross-section systematic settings.
Returns
Vector of SampleType object, initialized and ready for use.
Note
Example
auto mySamples = MaCh3SampleHandlerFactory<SampleHandlerTutorial>(SampleConfig, xsec);

Definition at line 132 of file MaCh3Factory.h.

133  {
134 // ********************************************
135  std::vector<SampleType*> Handlers(SampleConfig.size());
136  for (size_t i = 0; i < SampleConfig.size(); ++i)
137  {
138  // Instantiate the sample using the specified class type
139  SampleType* Sample = new SampleType(SampleConfig[i], xsec);
140  Handlers[i] = Sample;
141  }
142  return Handlers;
143 }