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