MaCh3  2.6.1
Reference Guide
PlottingManager.cpp
Go to the documentation of this file.
1 #include <filesystem>
2 #include "PlottingManager.h"
3 
4 namespace M3 {
5 namespace Plotting {
6 // this is the constructor using the default plotting config file
8  // set config file name
10 }
11 
12 // this is the constructor with user specified config
13 PlottingManager::PlottingManager(const std::string &PlottingConfigName) {
14  // parse the config file
15  _configFileName = PlottingConfigName;
16 }
17 
33 
34  MACH3LOG_DEBUG("Initialising PlottingManager with plotting congif {}", _configFileName);
35  // read options from the config
36  YAML::Node managerOptions = _plottingConfig["ManagerOptions"];
37 
38  std::string translationConfig = managerOptions["translationConfig"].as<std::string>();
39  if (translationConfig == "")
40  {
41  translationConfig = DEFAULT_TRANSLATION_CONFIG;
42  MACH3LOG_DEBUG("PlottingManager: Using default translation config file name");
43  }
44 
45  MACH3LOG_DEBUG("PlottingManager: Using translation config file: {}", translationConfig);
46 
47  std::string styleConfig = managerOptions["styleConfig"].as<std::string>();
48  if (styleConfig == "")
49  {
50  styleConfig = DEFAULT_STYLE_CONFIG;
51  MACH3LOG_DEBUG("PlottingManager: Using default style config file name");
52  }
53 
54  MACH3LOG_DEBUG("PlottingManager: Using style config file: {}", styleConfig);
55 
56  // create the StyleManager
57  _styleMan = std::make_unique<StyleManager>(styleConfig);
58 
59  // create the InputManager and add all the files to it
60  _inputMan = std::make_unique<InputManager>(translationConfig, _fileNames);
61 }
62 
90 void PlottingManager::parseInputs(int argc, char * const *argv) {
91  if (argc < 2)
92  {
93  usage();
94  MACH3LOG_ERROR("no arguments were specified :(");
95  throw MaCh3Exception(__FILE__, __LINE__);
96  }
97 
98  // parse the inputs
99  int c;
100  while ((c = getopt(argc, argv, "o:l:d:c:srgh")) != -1)
101  {
102  if (c < 0)
103  break;
104  switch (c)
105  {
106  case 'o': {
107  setOutFileName(optarg);
108  break;
109  }
110  case 's': {
111  _splitBySample = true;
112  break;
113  }
114  case 'r': {
115  _plotRatios = true;
116  break;
117  }
118  case 'g': {
119  _drawGrid = true;
120  break;
121  }
122  case 'h': {
123  usage();
124  break;
125  }
126  case 'l': {
127  parseFileLabels(optarg, _fileLabels);
128  MACH3LOG_INFO("Specified file labels: ");
129  for (std::string label : _fileLabels)
130  MACH3LOG_INFO(" {}", label);
131  break;
132  }
133  case 'c': {
134  _configFileName = optarg;
135  break;
136  }
137  case 'd': {
138  _extraDrawOptions = optarg;
139  break;
140  }
141  case '?':
142  default:
143  {
144  MACH3LOG_ERROR("Unknown or malformed option");
145  usage();
146  throw MaCh3Exception(__FILE__, __LINE__);
147  }
148  }
149  }
150 
151  MACH3LOG_INFO("Input files provided");
152  // optind is for the extra arguments that are not parsed by the program
153  for (; optind < argc; optind++)
154  {
155  _fileNames.push_back(argv[optind]);
156  MACH3LOG_INFO(argv[optind]);
157  _defaultFileLabels.push_back(argv[optind]);
158  }
159 
160  if (_splitBySample)
161  MACH3LOG_INFO("Splitting by sample");
162 
163  if (_plotRatios && _fileNames.size() == 0)
164  {
165  MACH3LOG_ERROR("you specified -r <_plotRatios> = true but didn't specify any files to compare against, was this a mistake?");
166  }
167 
168  if (_fileLabels.size() == 0)
169  {
170  MACH3LOG_INFO("No file labels specified, will just use the file names");
172  }
173 
174  if ((_fileLabels.size() != 0) && (_fileLabels.size() != _fileNames.size()))
175  {
176  MACH3LOG_ERROR("hmmm, you gave me {} labels but {} files", _fileLabels.size(), _fileNames.size());
177  }
178 
179  initialise();
180 }
181 
190 }
191 
192 std::string PlottingManager::getUserOption(const std::string& option) {
194  (void) option;
195  return "";
196 }
197 
202  MACH3LOG_INFO("========================================");
203  MACH3LOG_INFO("PlottingManager usage:");
204  MACH3LOG_INFO("========================================");
205 
206  MACH3LOG_INFO("Required:");
207  MACH3LOG_INFO(" <input files> One or more input files (positional args)");
208 
209  MACH3LOG_INFO("");
210  MACH3LOG_INFO("Options:");
211  MACH3LOG_INFO(" -o <file> Output file name");
212  MACH3LOG_INFO(" -l <labels> Comma/space-separated file labels");
213  MACH3LOG_INFO(" -c <config> Configuration file name");
214  //MACH3LOG_INFO(" -d <options> Extra draw options");
215  //MACH3LOG_INFO(" -s Split by sample");
216  //MACH3LOG_INFO(" -r Plot ratios (requires multiple input files)");
217  MACH3LOG_INFO(" -g Draw grid");
218  MACH3LOG_INFO(" -h Show this help message");
219 
220  MACH3LOG_INFO("");
221  MACH3LOG_INFO("Examples:");
222  MACH3LOG_INFO(" ./plot file1.root -f FancyName");
223  MACH3LOG_INFO("========================================");
224 }
225 
230 void PlottingManager::setOutFileName(const std::string& saveName) {
231  std::filesystem::path path(saveName);
232  if (!path.has_extension()) {
233  path.replace_extension(".pdf");
234  _outputName = path.string();
235  return;
236  }
237 
238  std::string ext = path.extension().string();
239  if (ext == ".pdf" || ext == ".ps" || ext == ".eps") {
240  _outputName = saveName;
241  return;
242  }
243 
244  MACH3LOG_WARN("file extension '{}' that you provided doesnt support multiple plots in one file", ext);
245  MACH3LOG_WARN("should be one of .pdf, .eps .ps, will use pdf");
246  _outputName = saveName + ".pdf";
247 }
248 
252 const std::string PlottingManager::getOutputName(const std::string &suffix) {
253  std::filesystem::path path(_outputName);
254  // ".root", ".pdf", etc.
255  std::string ext = path.extension().string();
256  // filename without extension
257  std::string stem = path.stem().string();
258 
259  return stem + suffix + ext;
260 }
261 
264 void PlottingManager::setExec(const std::string& execName) {
265  MACH3LOG_DEBUG("Setting internal exec name to {}", execName);
266  _execOptions = _plottingConfig[execName];
267  if (_outputName == "Plot.pdf")
268  setOutFileName(getOption<std::string>("defaultOutputName"));
269 }
270 
271 void PlottingManager::parseFileLabels(std::string labelString, std::vector<std::string> &labelVec) {
272  // take in a string defining labels of the form "label1;label2;...;labelN" and parse it into a
273  // vector containing the labels
274 
275  size_t end = labelString.find(";");
276  while (end != std::string::npos)
277  { // Loop until no delimiter is left in the string.
278  labelVec.push_back(labelString.substr(0, end));
279  labelString.erase(labelString.begin(), labelString.begin() + end + 1);
280  end = labelString.find(";");
281  }
282  labelVec.push_back(labelString.substr(0, end));
283 }
284 } // namespace Plotting
285 } // namespace M3
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:34
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:36
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:590
void setExec(const std::string &execName)
Internally set the name of the executable that manager is being used in.
void initialise()
initalise this PlottingManager.
std::vector< std::string > _fileNames
const std::string getOutputName()
Get the straight up output file name with no bells or whistles, just the file extension.
std::vector< std::string > _defaultFileLabels
const std::string DEFAULT_PLOTTING_CONFIG
const std::string DEFAULT_TRANSLATION_CONFIG
std::string getUserOption(const std::string &option)
Retrieve a command line option you specified using addOption.
PlottingManager()
Construct a new PlottingManager using default plottingConfig config.
void addUserOption()
Describe an option you want to add to the PlottingManager which can be read in from the command line ...
const std::string DEFAULT_STYLE_CONFIG
std::unique_ptr< InputManager > _inputMan
std::vector< std::string > _fileLabels
void parseInputs(int argc, char *const *argv)
Parse command line arguments.
void parseFileLabels(std::string labelString, std::vector< std::string > &labelVec)
Parse string of labels into a vector of strings.
void setOutFileName(const std::string &fileName)
Parse and set the output file name, if extension specified, check its one root supports,...
void usage() const
Print a usage message for the current executable.
std::unique_ptr< StyleManager > _styleMan
Custom exception class used throughout MaCh3.
Main namespace for MaCh3 software.