CMS 3D CMS Logo

L1EGammaEECalibrator.cc
Go to the documentation of this file.
4 #include "boost/property_tree/ptree.hpp"
5 #include "boost/property_tree/json_parser.hpp"
6 #include <iterator>
7 
8 namespace l1tp2 {
9  std::vector<float> as_vector(boost::property_tree::ptree const& pt,
10  boost::property_tree::ptree::key_type const& key) {
11  std::vector<float> ret;
12  for (const auto& item : pt.get_child(key))
13  ret.push_back(item.second.get_value<float>());
14  return ret;
15  }
16 }; // namespace l1tp2
17 
19  //read the JSON file and populate the eta - pt bins and the value container
20  boost::property_tree::ptree calibration_map;
21  read_json(pset.getParameter<edm::FileInPath>("calibrationFile").fullPath(), calibration_map);
22 
23  auto eta_l = l1tp2::as_vector(calibration_map, "eta_l");
24  std::copy(eta_l.begin(), eta_l.end(), std::inserter(eta_bins, eta_bins.end()));
25  auto eta_h = l1tp2::as_vector(calibration_map, "eta_h");
26  eta_bins.insert(eta_h.back());
27 
28  auto pt_l = l1tp2::as_vector(calibration_map, "pt_l");
29  std::copy(pt_l.begin(), pt_l.end(), std::inserter(pt_bins, pt_bins.end()));
30  auto pt_h = l1tp2::as_vector(calibration_map, "pt_h");
31  pt_bins.insert(pt_h.back());
32 
33  auto calib_data = l1tp2::as_vector(calibration_map, "calib");
34  auto n_bins_eta = eta_bins.size();
35  auto n_bins_pt = pt_bins.size();
36  calib_factors.reserve(n_bins_eta * n_bins_pt);
37  for (auto calib_f = calib_data.begin(); calib_f != calib_data.end(); ++calib_f) {
38  auto index = calib_f - calib_data.begin();
39  int eta_bin = etaBin(eta_l[index]);
40  int pt_bin = ptBin(pt_l[index]);
41  calib_factors[(eta_bin * n_bins_pt) + pt_bin] = *calib_f;
42  }
43 }
44 
45 int L1EGammaEECalibrator::bin(const std::set<float>& container, float value) const {
46  auto bin_l = container.upper_bound(value);
47  if (bin_l == container.end()) {
48  // value not mapped to any bin
49  return -1;
50  }
51  return std::distance(container.begin(), bin_l) - 1;
52 }
53 
54 float L1EGammaEECalibrator::calibrationFactor(const float& pt, const float& eta) const {
55  int bin_eta = etaBin(eta);
56  int bin_pt = ptBin(pt);
57  if (bin_eta == -1 || bin_pt == -1)
58  return 1.;
59  auto n_bins_pt = pt_bins.size();
60  return calib_factors[(bin_eta * n_bins_pt) + bin_pt];
61 }
int etaBin(float eta) const
std::set< float > pt_bins
std::string fullPath() const
Definition: FileInPath.cc:161
ret
prodAgent to be discontinued
int ptBin(float pt) const
std::set< float > eta_bins
std::vector< float > as_vector(boost::property_tree::ptree const &pt, boost::property_tree::ptree::key_type const &key)
Definition: value.py:1
L1EGammaEECalibrator(const edm::ParameterSet &)
std::vector< float > calib_factors
float calibrationFactor(const float &pt, const float &eta) const
int bin(const std::set< float > &container, float value) const