MaCh3  2.2.3
Reference Guide
plottingManager.cpp
Go to the documentation of this file.
1 #include "plottingManager.h"
2 
3 namespace MaCh3Plotting {
4 // this is the constructor using the default plotting config file
6  // set config file name
8 }
9 
10 // this is the constructor with user specified config
11 PlottingManager::PlottingManager(const std::string &PlottingConfigName) {
12  // parse the config file
13  _configFileName = PlottingConfigName;
14 }
15 
31 
32  MACH3LOG_DEBUG("Initialising PlottingManager with plotting congif {}", _configFileName);
33  // read options from the config
34  YAML::Node managerOptions = _plottingConfig["ManagerOptions"];
35 
36  std::string translationConfig = managerOptions["translationConfig"].as<std::string>();
37  if (translationConfig == "")
38  {
39  translationConfig = DEFAULT_TRANSLATION_CONFIG;
40  MACH3LOG_DEBUG("PlottingManager: Using default translation config file name");
41  }
42 
43  MACH3LOG_DEBUG("PlottingManager: Using translation config file: {}", translationConfig);
44 
45  std::string styleConfig = managerOptions["styleConfig"].as<std::string>();
46  if (styleConfig == "")
47  {
48  styleConfig = DEFAULT_STYLE_CONFIG;
49  MACH3LOG_DEBUG("PlottingManager: Using default style config file name");
50  }
51 
52  MACH3LOG_DEBUG("PlottingManager: Using style config file: {}", styleConfig);
53 
54  // create the StyleManager
55  _styleMan = std::make_unique<StyleManager>(styleConfig);
56 
57  // create the InputManager and add all the files to it
58  _inputMan = std::make_unique<InputManager>(translationConfig);
59  for (std::string fileName : _fileNames)
60  {
61  _inputMan->addFile(fileName);
62  }
63 }
64 
92 void PlottingManager::parseInputs(int argc, char * const *argv) {
93  // parse the inputs
94  int c;
95  while ((c = getopt(argc, argv, "o:l:d:c:srgh")) != -1)
96  {
97  if (c < 0)
98  break;
99  switch (c)
100  {
101  case 'o': {
102  setOutFileName(optarg);
103  break;
104  }
105  case 's': {
106  _splitBySample = true;
107  break;
108  }
109  case 'r': {
110  _plotRatios = true;
111  break;
112  }
113  case 'g': {
114  _drawGrid = true;
115  break;
116  }
117  case 'h': {
118  usage();
119  throw MaCh3Exception(__FILE__ , __LINE__ );
120  }
121  case 'l': {
122  parseFileLabels(optarg, _fileLabels);
123  MACH3LOG_INFO("Specified file labels: ");
124  for (std::string label : _fileLabels)
125  MACH3LOG_INFO(" {}", label);
126  break;
127  }
128  case 'c': {
129  _configFileName = optarg;
130  break;
131  }
132  case 'd': {
133  _extraDrawOptions = optarg;
134  break;
135  }
136  }
137  }
138 
139  MACH3LOG_INFO("Input files provided");
140  // optind is for the extra arguments that are not parsed by the program
141  for (; optind < argc; optind++)
142  {
143  _fileNames.push_back(argv[optind]);
144  MACH3LOG_INFO(argv[optind]);
145  _defaultFileLabels.push_back(argv[optind]);
146  }
147 
148  if (_splitBySample)
149  MACH3LOG_INFO("Splitting by sample");
150 
151  if (_plotRatios && _fileNames.size() == 0)
152  {
153  MACH3LOG_ERROR("you specified -r <_plotRatios> = true but didnt specify any files to compare against, was this a mistake?");
154  }
155 
156  if (_fileLabels.size() == 0)
157  {
158  MACH3LOG_INFO("No file labels specified, will just use the file names");
160  }
161 
162  if ((_fileLabels.size() != 0) && (_fileLabels.size() != _fileNames.size()))
163  {
164  MACH3LOG_ERROR("hmmm, you gave me {} labels but {} files", _fileLabels.size(), _fileNames.size());
165  }
166 
167  initialise();
168 }
169 
178 }
179 
180 std::string PlottingManager::getUserOption(std::string option) {
182  (void) option;
183  return "";
184 }
185 
191 }
192 
197 void PlottingManager::setOutFileName(const std::string& saveName) {
198  if (saveName.find(".") == std::string::npos)
199  {
200  _outputName = saveName + ".pdf";
201  return;
202  }
203 
204  std::string ext = saveName;
205  // if there are .'s then remove everything before them until we only have the file extension left
206  while (ext.find(".") != std::string::npos)
207  ext.erase(0, ext.find(".") + 1);
208  if (ext == "pdf" || ext == "ps" || ext == "eps")
209  {
210  _outputName = saveName;
211  return;
212  }
213 
214  MACH3LOG_WARN("file extension '{}' that you provided doesnt support multiple plots in one file", ext);
215  MACH3LOG_WARN("should be one of .pdf, .eps .ps, will use pdf");
216  _outputName = saveName + ".pdf";
217 }
218 
223 const std::string PlottingManager::getOutputName(const std::string &suffix) {
224  std::string ext = _outputName;
225  std::string name = _outputName;
226 
227  size_t dotPos = 0;
228  while (ext.find(".") != std::string::npos)
229  {
230  dotPos += ext.find(".");
231  ext.erase(0, ext.find(".") + 1);
232  }
233 
234  name.erase(dotPos, ext.size() + 1);
235  return name + suffix + "." + ext;
236 }
237 
240 void PlottingManager::setExec(const std::string& execName) {
241  MACH3LOG_DEBUG("Setting internal exec name to {}", execName);
242  _execOptions = _plottingConfig[execName];
243  if (_outputName == "Plot.pdf")
244  setOutFileName(getOption<std::string>("defaultOutputName"));
245 }
246 
247 void PlottingManager::parseFileLabels(std::string labelString, std::vector<std::string> &labelVec) {
248  // take in a string defining labels of the form "label1;label2;...;labelN" and parse it into a
249  // vector containing the labels
250 
251  size_t end = labelString.find(";");
252  while (end != std::string::npos)
253  { // Loop until no delimiter is left in the string.
254  labelVec.push_back(labelString.substr(0, end));
255  labelString.erase(labelString.begin(), labelString.begin() + end + 1);
256  end = labelString.find(";");
257  }
258  labelVec.push_back(labelString.substr(0, end));
259 }
260 } // namespace MaCh3Plotting
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:24
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:27
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:25
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:26
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:561
Custom exception class for MaCh3 errors.
void setOutFileName(const std::string &fileName)
Parse and set the output file name, if extension specified, check its one root supports,...
const std::string DEFAULT_PLOTTING_CONFIG
void initialise()
initalise this PlottingManager.
const std::string DEFAULT_STYLE_CONFIG
void setExec(const std::string &execName)
Internally set the name of the executable that manager is being used in.
std::vector< std::string > _defaultFileLabels
std::vector< std::string > _fileLabels
const std::string DEFAULT_TRANSLATION_CONFIG
std::vector< std::string > _fileNames
const std::string getOutputName() const
Get the straight up output file name with no bells or whistles, just the file extension.
std::unique_ptr< InputManager > _inputMan
void parseInputs(int argc, char *const *argv)
Parse command line arguments.
void usage()
Print a usage message for the current executable.
PlottingManager()
Construct a new PlottingManager using default plottingConfig config.
void parseFileLabels(std::string labelString, std::vector< std::string > &labelVec)
Parse string of labels into a vector of strings.
std::unique_ptr< StyleManager > _styleMan
void addUserOption()
Describe an option you want to add to the PlottingManager which can be read in from the command line ...
std::string getUserOption(std::string option)
Retrieve a command line option you specified using addOption.