MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
MaCh3Modes.cpp
Go to the documentation of this file.
1//MaCh3 includes
2#include "MaCh3Modes.h"
3
4// *******************
5MaCh3Modes::MaCh3Modes(std::string const &filename) {
6// *******************
7 // Load config
8 YAML::Node config = M3OpenConfig(filename);
9
10 std::string GetMaCh3ModeName(const int Index);
11 NModes = 0;
12 nCCModes = 0;
13
14 Title = config["Title"].as<std::string>();
15 Generator = config["GeneratorName"].as<std::string>();
16
17 auto names = Get<std::vector<std::string>>(config["MaCh3Modes"], __FILE__, __LINE__);
18
19 for(size_t i = 0; i < names.size(); i++)
20 {
21 DeclareNewMode(names[i],
22 Get<std::string>(config[names[i]]["Name"], __FILE__, __LINE__),
23 Get<int>(config[names[i]]["PlotColor"], __FILE__, __LINE__),
24 Get<std::vector<int>>(config[names[i]]["GeneratorMaping"], __FILE__, __LINE__),
25 Get<bool>(config[names[i]]["IsNC"], __FILE__, __LINE__),
26 Get<std::string>(config[names[i]]["SplineSuffix"], __FILE__, __LINE__));
27
28 if (!Get<bool>(config[names[i]]["IsNC"], __FILE__, __LINE__)) {
29 nCCModes += 1;
30 }
31 }
32 // Add unknown category, it's better to have garbage category where all undefined modes will go rather than get random crashes
33 DeclareNewMode("UNKNOWN_BAD",
34 "UNKNOWN_BAD",
35 kBlack,
36 {},
37 false,
38 "UNKNOWN_BAD");
39 // This is hack to not have bad mode
40 NModes--;
41
42 PrepareMap();
43
44 Print();
45}
46
47// *******************
49// *******************
50 MACH3LOG_INFO("Printing MaCh3 Modes Called: {}", Title);
51
52 MACH3LOG_INFO("========================================================================");
53 MACH3LOG_INFO("{:<5} {:2} {:<20} {:2} {:<20} {:2} {:<30}", "#", "|", "Name", "|", "FancyName", "|", Generator+" Modes");
54 MACH3LOG_INFO("------------------------------------------------------------------------");
55 for(int i = 0; i < NModes; ++i) {
56 auto Name = fMode[i].Name;
57 auto FancyName = fMode[i].FancyName;
58 auto Values = fMode[i].GeneratorMaping;
59
60 std::string generatorModes;
61 for (const int& element : Values) {
62 generatorModes += std::to_string(element) + " ";
63 }
64 MACH3LOG_INFO("{:<5} {:2} {:<20} {:2} {:<20} {:2} {:<30}", i, "|", Name, "|", FancyName, "|", generatorModes);
65 }
66 MACH3LOG_INFO("========================================================================");
67
68 MACH3LOG_INFO("==========================");
69 MACH3LOG_INFO("{:<10} {:2} {:<30}", Generator + " Modes", "|", "Name");
70 MACH3LOG_INFO("--------------------------");
71 for (size_t i = 0; i < ModeMap.size(); ++i) {
72 MACH3LOG_INFO("{:<10} {:2} {:<30}", i, "|", fMode[ModeMap[i]].Name);
73 }
74 MACH3LOG_INFO("==========================");
75}
76
77// *******************
79// *******************
80 if (Mode.count(name)) {
81 return Mode[name];
82 }
83 MaCh3Modes_t index = MaCh3Modes_t(Mode.size());
84 Mode[name] = index;
85 return index;
86}
87
88// *******************
89void MaCh3Modes::DeclareNewMode(std::string const &name,
90 std::string const &fancyname,
91 int PlotColor,
92 std::vector<int> const &GenMap,
93 bool IsNC,
94 std::string SplineSuffix) {
95// *******************
96 MaCh3ModeInfo newinfo;
97 newinfo.GeneratorMaping = GenMap;
98 newinfo.FancyName = fancyname;
99 newinfo.PlotColor = PlotColor;
100 newinfo.Name = name;
101 newinfo.IsNC = IsNC;
102 newinfo.SplineSuffix = SplineSuffix;
103
105
106 fMode.emplace(index, newinfo);
107 NModes++;
108}
109
110// *******************
112// *******************
113 int maxElement = 0;
114 for(int i = 0; i < NModes; ++i) {
115 for(size_t j = 0; j < fMode[i].GeneratorMaping.size(); j++) {
116 maxElement = std::max(maxElement, fMode[i].GeneratorMaping[j]);
117 }
118 }
119 ModeMap.resize(maxElement+1, Mode["UNKNOWN_BAD"]);
120
121 for(int m = 0; m < NModes; ++m) {
122 for(size_t i = 0; i < fMode[m].GeneratorMaping.size(); ++i) {
123 if(fMode[m].GeneratorMaping[i] < 0) {
124 MACH3LOG_ERROR("Negative value of Mode {} for mode {}", fMode[m].GeneratorMaping[i], fMode[m].Name);
125 throw MaCh3Exception(__FILE__ , __LINE__ );
126 }
127 if(ModeMap[fMode[m].GeneratorMaping[i]] != Mode["UNKNOWN_BAD"])
128 {
129 MACH3LOG_ERROR("Generator mode {} already defined in mode {} can't do this for mode {}", fMode[m].GeneratorMaping[i], fMode[ModeMap[fMode[m].GeneratorMaping[i]]].Name, fMode[m].Name);
130 throw MaCh3Exception(__FILE__ , __LINE__ );
131 }
132 ModeMap[fMode[m].GeneratorMaping[i]] = m;
133 }
134 }
135}
136
137// *******************
138std::string MaCh3Modes::GetMaCh3ModeName(const int Index) {
139// *******************
140 if(Index < 0)
141 MACH3LOG_CRITICAL("Mode you look for is smaller than 0 and equal to {}", Index);
142
143 // return UNKNOWN_BAD if out of boundary
144 if(Index > NModes)
145 {
146 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", Index, NModes, fMode[NModes].Name);
147 return fMode[NModes].Name;
148 }
149 return fMode[Index].Name;
150}
151
152// *******************
153bool MaCh3Modes::IsMaCh3ModeNC(const int Index) {
154 if(Index < 0)
155 MACH3LOG_CRITICAL("Mode you look for is smaller than 0 and equal to {}", Index);
156
157 // return UNKNOWN_BAD if out of boundary
158 if(Index > NModes)
159 {
160 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", Index, NModes, fMode[NModes].Name);
161 return fMode[NModes].IsNC;
162 }
163
164 return fMode[Index].IsNC;
165}
166// *******************
167
168// *******************
169std::string MaCh3Modes::GetMaCh3ModeFancyName(const int Index) {
170// *******************
171 // return UNKNOWN_BAD if out of boundary
172 if(Index < 0)
173 MACH3LOG_CRITICAL("Mode you look for is smaller than 0 and equal to {}", Index);
174
175 if(Index > NModes)
176 {
177 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", Index, NModes, fMode[NModes].Name);
178 return fMode[NModes].FancyName;
179 }
180 return fMode[Index].FancyName;
181}
182
183// *******************
184MaCh3Modes_t MaCh3Modes::GetMode(const std::string& name) {
185// *******************
186 if (Mode.count(name)) {
187 return Mode[name];
188 }
189 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", name, NModes, fMode[NModes].Name);
190
191 // return UNKNOWN_BAD
192 return NModes;
193}
194
195// *******************
197// *******************
198 if(Index < 0)
199 MACH3LOG_CRITICAL("Mode you look for is smaller than 0 and equal to {}", Index);
200
201 if(Index >= static_cast<int>(ModeMap.size()))
202 {
203 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", Index, NModes, fMode[NModes].Name);
204 return NModes;
205 }
206
207 return ModeMap[Index];
208}
209
210// *******************
212// *******************
213 // return UNKNOWN_BAD if out of boundary
214 if(Index < 0)
215 MACH3LOG_CRITICAL("Mode you look for is smaller than 0 and equal to {}", Index);
216
217 if(Index > NModes)
218 {
219 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", Index, NModes, fMode[NModes].PlotColor);
220 return fMode[NModes].PlotColor;
221 }
222 return fMode[Index].PlotColor;
223}
224
225// *******************
226std::string MaCh3Modes::GetSplineSuffixFromMaCh3Mode(const int Index) {
227 // *******************
228 // return UNKNOWN_BAD if out of boundary
229 if(Index < 0)
230 MACH3LOG_CRITICAL("Mode you look for is smaller than 0 and equal to {}", Index);
231
232 if(Index > NModes) {
233 MACH3LOG_DEBUG("Asking for mode {}, while I only have {}, returning {} mode", Index, NModes, fMode[NModes].SplineSuffix);
234 return fMode[NModes].SplineSuffix;
235 }
236
237 return fMode[Index].SplineSuffix;
238}
Color_t PlotColor[]
#define MACH3LOG_CRITICAL
Definition: MaCh3Logger.h:26
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:22
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:25
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:23
int MaCh3Modes_t
Enumerator of MaCh3Mode.
Definition: MaCh3Modes.h:14
std::string config
Definition: ProcessMCMC.cpp:30
Type Get(const YAML::Node &node, const std::string File, const int Line)
Get content of config file.
Definition: YamlHelper.h:265
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:560
Custom exception class for MaCh3 errors.
std::map< std::string, MaCh3Modes_t > Mode
KS: Handy map which helps find mode number based on string.
Definition: MaCh3Modes.h:88
int nCCModes
DB: Number of CC modes.
Definition: MaCh3Modes.h:101
void DeclareNewMode(std::string const &name, std::string const &fancyname, int PlotColor, std::vector< int > const &GenMap, bool IsNC, std::string SplineSuffix)
KS: Add new mode.
Definition: MaCh3Modes.cpp:89
std::string GetMaCh3ModeFancyName(const int Index)
KS: Get fancy name of mode, if mode not known you will get UNKNOWN_BAD.
Definition: MaCh3Modes.cpp:169
MaCh3Modes(std::string const &filename)
KS: Initialise MaCh3 modes.
Definition: MaCh3Modes.cpp:5
MaCh3Modes_t GetMode(const std::string &name)
KS: Get mode number based on name, if mode not known you will get UNKNOWN_BAD.
Definition: MaCh3Modes.cpp:184
int NModes
KS: Number of modes, keep in mind actual number is +1 greater due to unknown category.
Definition: MaCh3Modes.h:99
std::vector< int > ModeMap
KS: Handy map helping us find MaCh3 mode based on Generator mode value.
Definition: MaCh3Modes.h:92
void Print()
KS: Print info about initialised modes.
Definition: MaCh3Modes.cpp:48
std::string Generator
KS: Name of generator like NEUT, NuWro etc. this is to make stuff fancy.
Definition: MaCh3Modes.h:97
std::string GetSplineSuffixFromMaCh3Mode(const int Index)
DB: Get binned spline mode suffic from MaCh3 Mode.
Definition: MaCh3Modes.cpp:226
std::string Title
KS: Name of loaded modes.
Definition: MaCh3Modes.h:95
MaCh3Modes_t EnsureModeNameRegistered(std::string const &name)
KS: Make sure we don't have two modes with the same name.
Definition: MaCh3Modes.cpp:78
int GetMaCh3ModePlotColor(const int Index)
KS: Get normal name of mode, if mode not known you will get UNKNOWN_BAD.
Definition: MaCh3Modes.cpp:211
std::string GetMaCh3ModeName(const int Index)
KS: Get normal name of mode, if mode not known you will get UNKNOWN_BAD.
Definition: MaCh3Modes.cpp:138
bool IsMaCh3ModeNC(const int Index)
DB: Get IsNC (a check whether the given MaCh3 corresponds to a Neutral Current mode)
Definition: MaCh3Modes.cpp:153
void PrepareMap()
KS: Fill ModeMap.
Definition: MaCh3Modes.cpp:111
MaCh3Modes_t GetModeFromGenerator(const int Index)
KS: Get MaCh3 mode from generator mode.
Definition: MaCh3Modes.cpp:196
std::map< MaCh3Modes_t, MaCh3ModeInfo > fMode
KS: Main map storing info about used modes.
Definition: MaCh3Modes.h:90
KS: Class containing information for a single MaCh3Mode.
Definition: MaCh3Modes.h:17
std::vector< int > GeneratorMaping
Mapping between mode and generator integers.
Definition: MaCh3Modes.h:25
std::string SplineSuffix
Spline suffix.
Definition: MaCh3Modes.h:29
std::string FancyName
Mode fancy name.
Definition: MaCh3Modes.h:21
std::string Name
Mode name.
Definition: MaCh3Modes.h:19
bool IsNC
IsNC check.
Definition: MaCh3Modes.h:27
int PlotColor
Mode color for plotting purposes.
Definition: MaCh3Modes.h:23