MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
ParameterTunes.cpp
Go to the documentation of this file.
2
3// ********************************************
4ParameterTunes::ParameterTunes(const YAML::Node& Settings) {
5// ********************************************
6 std::map<std::string, std::vector<double>> orderedTunes;
7
8 // Loop over all systematics
9 for (const auto& sysNode : Settings) {
10 const auto& values = sysNode["Systematic"]["ParameterValues"];
11
12 for (const auto& tuneNode : values) {
13 std::string key = tuneNode.first.as<std::string>();
14 if (key == "PreFitValue") continue;
15
16 double val = tuneNode.second.as<double>();
17 orderedTunes[key].push_back(val);
18 }
19 }
20
21 size_t expectedSize = 0;
22 bool hasMismatch = false;
23 std::ostringstream errorMsg;
24 errorMsg << "Inconsistent number of parameter values across tunes:\n";
25
26 for (const auto& kv : orderedTunes) {
27 const auto& vals = kv.second;
28 if (expectedSize == 0) {
29 expectedSize = vals.size();
30 } else if (vals.size() != expectedSize) {
31 hasMismatch = true;
32 }
33 errorMsg << " - Tune '" << kv.first << "': " << vals.size() << " values\n";
34 }
35
36 if (hasMismatch) {
37 errorMsg << "Expected all tunes to have " << expectedSize << " values.";
38 MACH3LOG_ERROR("{}", errorMsg.str());
39 throw MaCh3Exception(__FILE__, __LINE__);
40 }
41
42 int idx = 0;
43 for (const auto& kv : orderedTunes) {
44 TuneNames.push_back(kv.first);
45 TuneValues.push_back(kv.second);
46 TuneMap[kv.first] = idx++;
47 }
48
49 // KS: Log final summary of found tunes
50 MACH3LOG_INFO("Found {} tunes:", TuneNames.size());
51 for (size_t i = 0; i < TuneNames.size(); ++i) {
52 MACH3LOG_INFO(" Tune {} {}", i, TuneNames[i]);
53 }
54 #ifdef DEBUG
55 PrintTunes();
56 #endif
57}
58
59// ********************************************
61// ********************************************
62
63}
64
65// ********************************************
66std::vector<double> ParameterTunes::ParameterTunes::GetTune(const int TuneNumber) const {
67// ********************************************
68 if (TuneNumber >= static_cast<int>(TuneValues.size())) {
69 MACH3LOG_ERROR("I only have {} tune(s), but you asked for index {}", TuneValues.size(), TuneNumber);
70 throw MaCh3Exception(__FILE__, __LINE__);
71 }
72
73 return TuneValues[TuneNumber];
74}
75
76// ********************************************
77std::vector<double> ParameterTunes::ParameterTunes::GetTune(const std::string& TuneName) const {
78// ********************************************
79 auto it = TuneMap.find(TuneName);
80 if (it == TuneMap.end()) {
81 MACH3LOG_ERROR("Tune '{}' not found. Available tunes: {}", TuneName, fmt::join(TuneNames, ", "));
82 throw MaCh3Exception(__FILE__, __LINE__);
83 }
84
85 return GetTune(it->second);
86}
87
88// ********************************************
89void ParameterTunes::ParameterTunes::PrintTunes() const {
90// ********************************************
91 MACH3LOG_INFO("Available tunes:");
92 for (size_t i = 0; i < TuneNames.size(); ++i) {
93 MACH3LOG_INFO(" Tune {} {}", i, TuneNames[i]);
94 for (size_t j = 0; j < TuneValues[i].size(); ++j) {
95 MACH3LOG_INFO(" Value {} = {}", j, TuneValues[i][j]);
96 }
97 }
98}
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:25
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:23
Custom exception class for MaCh3 errors.
ParameterTunes(const YAML::Node &Settings)
Constructor.
std::vector< std::string > TuneNames
Name of each Tun.
void PrintTunes() const
Simply print all tunes and associated values.
virtual ~ParameterTunes()
std::unordered_map< std::string, int > TuneMap
Map between tune name and value.
std::vector< std::vector< double > > TuneValues
Values for each Tune and Parameter.