CP3-llbb Framework
BinnedValuesJSONParser.h
1 #pragma once
2 
3 #include <cp3_llbb/Framework/interface/Histogram.h>
4 #include <cp3_llbb/Framework/interface/BinnedValues.h>
5 
6 #include <boost/property_tree/ptree.hpp>
7 
8 #include <memory>
9 
10 #include <TFormula.h>
11 
13 
14  public:
15  BinnedValuesJSONParser(const std::string& file) {
16  parse_file(file);
17  }
18 
19  virtual BinnedValues&& get_values() final {
20  return std::move(m_values);
21  }
22 
23  private:
24  void parse_file(const std::string& file);
25 
26  std::vector<float> get_array(boost::property_tree::ptree& ptree) {
27  std::vector<float> vector;
28  for (auto& value: ptree) {
29  vector.push_back(std::stof(value.second.data()));
30  }
31 
32  return vector;
33  }
34 
35  std::vector<std::string> get_string_array(boost::property_tree::ptree& ptree) {
36  std::vector<std::string> vector;
37  for (auto& value: ptree) {
38  vector.push_back(value.second.data());
39  }
40 
41  return vector;
42  }
43 
44  template <class T, typename _Value>
45  void fillHistogram(T& h, const std::vector<float>& bins, const _Value& value, const _Value& error_low, const _Value& error_high) {
46  std::size_t bin = h.findBin(bins);
47 
48  h.setBinContent(bin, value);
49  h.setBinErrorLow(bin, error_low);
50  h.setBinErrorHigh(bin, error_high);
51  }
52 
53  void fillHistogram(BinnedValues& val, const std::vector<float>& bins, const float& value, const float& error_low, const float& error_high) {
54  fillHistogram(*val.binned.get(), bins, value, error_low, error_high);
55  }
56 
57  void fillHistogram(BinnedValues& val, const std::vector<float>& bins, const std::string& value, const std::string& error_low, const std::string& error_high) {
58  std::shared_ptr<TFormula> value_formula(new TFormula("", value.c_str()));
59  std::shared_ptr<TFormula> error_low_formula(new TFormula("", error_low.c_str()));
60  std::shared_ptr<TFormula> error_high_formula(new TFormula("", error_high.c_str()));
61  fillHistogram(*val.formula.get(), bins, value_formula, error_low_formula, error_high_formula);
62  }
63 
64  template <typename _Content>
65  void parse_data(boost::property_tree::ptree& ptree, std::size_t dimension) {
66  for (auto& data_x: ptree.get_child("data")) {
67  std::vector<float> binning_x = get_array(data_x.second.get_child("bin"));
68  float mean_x = (binning_x[0] + binning_x[1]) / 2.;
69 
70  if (dimension > 1) {
71 
72  for (auto& data_y: data_x.second.get_child("values")) {
73  std::vector<float> binning_y = get_array(data_y.second.get_child("bin"));
74  float mean_y = (binning_y[0] + binning_y[1]) / 2.;
75 
76  if (dimension > 2) {
77 
78  for (auto& data_z: data_y.second.get_child("values")) {
79  std::vector<float> binning_z = get_array(data_z.second.get_child("bin"));
80  float mean_z = (binning_z[0] + binning_z[1]) / 2.;
81  _Content value = data_z.second.get<_Content>("value");
82  _Content error_low = data_z.second.get<_Content>("error_low");
83  _Content error_high = data_z.second.get<_Content>("error_high");
84 
85  fillHistogram(m_values, {mean_x, mean_y, mean_z}, value, error_low, error_high);
86  }
87 
88  } else {
89 
90  _Content value = data_y.second.get<_Content>("value");
91  _Content error_low = data_y.second.get<_Content>("error_low");
92  _Content error_high = data_y.second.get<_Content>("error_high");
93 
94  fillHistogram(m_values, {mean_x, mean_y}, value, error_low, error_high);
95  }
96 
97  }
98 
99  } else {
100 
101  _Content value = data_x.second.get<_Content>("value");
102  _Content error_low = data_x.second.get<_Content>("error_low");
103  _Content error_high = data_x.second.get<_Content>("error_high");
104 
105  fillHistogram(m_values, {mean_x}, value, error_low, error_high);
106  }
107  }
108  }
109 
110  BinnedValues m_values;
111 };
Definition: BinnedValuesJSONParser.h:12
Definition: BinnedValues.h:88