Crombie Tools
PlotConfig.h
Go to the documentation of this file.
1 #ifndef CROMBIE_PLOTCONFIG_H
2 #define CROMBIE_PLOTCONFIG_H
3 
4 #include <vector>
5 #include <string>
6 #include <fstream>
7 #include <regex>
8 
9 #include "crombie/Hist.h"
10 #include "crombie/Parse.h"
11 
12 namespace crombie {
13  namespace PlotConfig {
14 
15  /// All of the information needed to make a plot
16  class Plot {
17  public:
18  Plot(const std::string& name, const unsigned nbins, const double low, const double max,
19  const std::string& label, const std::string& data_var = "", const std::string& mc_var = "")
20  : name{name}, nbins{nbins}, low{low}, max{max}, label{label},
21  data_var{data_var.size() ? data_var : name}, mc_var{mc_var.size() ? mc_var : name} {
22  Debug::Debug(__PRETTY_FUNCTION__, "New plot", name, nbins, low, max, label, data_var, mc_var);
23  };
24 
25  const std::string name;
26  private:
27  const unsigned nbins;
28  const double low;
29  const double max;
30  const std::string label;
31  public:
32  const std::string data_var;
33  const std::string mc_var;
34 
35  Hist::Hist get_hist() const; ///< Get a histogram that's properly formatted for this plot
36  };
37 
38 
39  std::vector<Plot> read(const std::string& config) {
40  std::vector<Plot> output;
41 
42  std::regex expr {"'([^']+)',\\s*(\\d+),\\s*(-?[\\d\\.]+),\\s*(-?[\\d\\.]+),\\s*'([^']+)'(,\\s*'([^']+)',\\s*'([^']+)')?"};
43  std::smatch matches;
44 
45  std::ifstream input {config};
46  for (auto& line : Parse::parse(input)) {
47  if (std::regex_match(line, matches, expr)) {
48  output.push_back({matches[1], static_cast<unsigned>(std::stoi(matches[2])),
49  std::stod(matches[3]), std::stod(matches[4]),
50  matches[5], matches[7], matches[8]});
51  }
52  }
53  return output;
54  }
55 
57  return Hist::Hist(label, nbins, low, max);
58  }
59 
60  }
61 }
62 
63 #endif