CMS 3D CMS Logo

HGCalTriggerClusterIdentificationBDT.cc
Go to the documentation of this file.
1 #include <limits>
7 
9 public:
10  class Category {
11  public:
12  Category(float pt_min, float pt_max, float eta_min, float eta_max)
14  ~Category() {}
15  bool contains(float pt, float eta) const {
16  bool output = true;
17  if (pt < pt_min_ || pt >= pt_max_)
18  output = false;
19  if (std::abs(eta) < eta_min_ || std::abs(eta) >= eta_max_)
20  output = false;
21  return output;
22  }
23 
24  float pt_min() const { return pt_min_; }
25  float pt_max() const { return pt_max_; }
26  float eta_min() const { return eta_min_; }
27  float eta_max() const { return eta_max_; }
28 
29  private:
30  float pt_min_ = 0.;
32  float eta_min_ = 1.5;
33  float eta_max_ = 3.;
34  };
35 
36  class WorkingPoint {
37  public:
38  WorkingPoint(const std::string& name, double wp) : name_(name), wp_(wp) {}
39  ~WorkingPoint() = default;
40 
41  const std::string& name() const { return name_; }
42  double working_point() const { return wp_; }
43 
44  private:
46  double wp_;
47  };
48 
49 public:
52  void initialize(const edm::ParameterSet& conf) final;
53  float value(const l1t::HGCalMulticluster& cluster) const final;
54  bool decision(const l1t::HGCalMulticluster& cluster, unsigned wp = 0) const final;
55  const std::vector<std::string>& working_points() const final { return working_points_names_; }
56 
57 private:
58  enum class ClusterVariable {
67  cl3d_szz,
71  };
72  std::vector<Category> categories_;
73  std::vector<std::unique_ptr<TMVAEvaluator>> bdts_;
74  std::vector<std::vector<WorkingPoint>> working_points_;
75  std::vector<std::string> input_variables_;
76  std::vector<ClusterVariable> input_variables_id_;
77  std::vector<std::string> working_points_names_;
78 
80  int category(float pt, float eta) const;
81 };
82 
83 DEFINE_HGC_TPG_CLUSTER_ID(HGCalTriggerClusterIdentificationBDT, "HGCalTriggerClusterIdentificationBDT");
84 
87 
89  if (!bdts_.empty()) {
90  edm::LogWarning("HGCalTriggerClusterIdentificationBDT|Initialization") << "BDTs already initialized.";
91  return;
92  }
93  input_variables_ = conf.getParameter<std::vector<std::string>>("Inputs");
94  std::vector<std::string> bdt_files = conf.getParameter<std::vector<std::string>>("Weights");
95  std::vector<double> categories_etamin = conf.getParameter<std::vector<double>>("CategoriesEtaMin");
96  std::vector<double> categories_etamax = conf.getParameter<std::vector<double>>("CategoriesEtaMax");
97  std::vector<double> categories_ptmin = conf.getParameter<std::vector<double>>("CategoriesPtMin");
98  std::vector<double> categories_ptmax = conf.getParameter<std::vector<double>>("CategoriesPtMax");
99 
100  if (bdt_files.size() != categories_etamin.size() || categories_etamin.size() != categories_etamax.size() ||
101  categories_etamax.size() != categories_ptmin.size() || categories_ptmin.size() != categories_ptmax.size()) {
102  throw cms::Exception("HGCalTriggerClusterIdentificationBDT|BadInitialization")
103  << "Inconsistent numbers of categories, BDT weight files and working points";
104  }
105  size_t categories_size = categories_etamin.size();
106 
107  const auto wps_conf = conf.getParameter<std::vector<edm::ParameterSet>>("WorkingPoints");
108  working_points_.resize(categories_size);
109  for (const auto& wp_conf : wps_conf) {
110  std::string wp_name = wp_conf.getParameter<std::string>("Name");
111  std::vector<double> wps = wp_conf.getParameter<std::vector<double>>("WorkingPoint");
112  working_points_names_.emplace_back(wp_name);
113  if (wps.size() != categories_size) {
114  throw cms::Exception("HGCalTriggerClusterIdentificationBDT|BadInitialization")
115  << "Inconsistent number of categories in working point '" << wp_name << "'";
116  }
117  for (size_t cat = 0; cat < categories_size; cat++) {
118  working_points_[cat].emplace_back(wp_name, wps[cat]);
119  }
120  }
121 
122  categories_.reserve(categories_size);
123  bdts_.reserve(categories_size);
124  for (size_t cat = 0; cat < categories_size; cat++) {
125  categories_.emplace_back(
126  categories_ptmin[cat], categories_ptmax[cat], categories_etamin[cat], categories_etamax[cat]);
127  }
128  std::vector<std::string> spectators = {};
129  for (const auto& file : bdt_files) {
130  bdts_.emplace_back(new TMVAEvaluator());
131  bdts_.back()->initialize("!Color:Silent:!Error",
132  "BDT::bdt",
135  spectators,
136  false,
137  false);
138  }
139 
140  // Transform input variable strings to enum values for later comparisons
141  input_variables_id_.reserve(input_variables_.size());
142  for (const auto& variable : input_variables_) {
143  if (variable == "cl3d_showerlength")
145  else if (variable == "cl3d_coreshowerlength")
147  else if (variable == "cl3d_firstlayer")
149  else if (variable == "cl3d_maxlayer")
151  else if (variable == "cl3d_seetot")
153  else if (variable == "cl3d_seemax")
155  else if (variable == "cl3d_spptot")
157  else if (variable == "cl3d_sppmax")
159  else if (variable == "cl3d_szz")
161  else if (variable == "cl3d_srrtot")
163  else if (variable == "cl3d_srrmax")
165  else if (variable == "cl3d_srrmean")
167  }
168 }
169 
171  std::map<std::string, float> inputs;
172  for (unsigned i = 0; i < input_variables_.size(); i++) {
174  }
175  float pt = cluster.pt();
176  float eta = cluster.eta();
177  int cat = category(pt, eta);
178  return (cat != -1 ? bdts_.at(cat)->evaluate(inputs) : -999.);
179 }
180 
182  float bdt_output = value(cluster);
183  float pt = cluster.pt();
184  float eta = cluster.eta();
185  int cat = category(pt, eta);
186  return (cat != -1 ? bdt_output > working_points_.at(cat).at(wp).working_point() : true);
187 }
188 
190  for (unsigned cat = 0; cat < categories_.size(); cat++) {
191  if (categories_[cat].contains(pt, eta))
192  return static_cast<int>(cat);
193  }
194  return -1;
195 }
196 
198  const l1t::HGCalMulticluster& cluster) const {
199  switch (variable) {
201  return cluster.showerLength();
203  return cluster.coreShowerLength();
205  return cluster.firstLayer();
207  return cluster.maxLayer();
209  return cluster.sigmaEtaEtaTot();
211  return cluster.sigmaEtaEtaMax();
213  return cluster.sigmaPhiPhiTot();
215  return cluster.sigmaPhiPhiMax();
217  return cluster.sigmaZZ();
219  return cluster.sigmaRRTot();
221  return cluster.sigmaRRMax();
223  return cluster.sigmaRRMean();
224  default:
225  break;
226  }
227  return 0.;
228 }
float sigmaEtaEtaTot() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:37
double pt() const final
transverse momentum
int coreShowerLength() const
float sigmaPhiPhiTot() const
bool decision(const l1t::HGCalMulticluster &cluster, unsigned wp=0) const final
def cat(path)
Definition: eostools.py:401
float sigmaPhiPhiMax() const
int firstLayer() const
float clusterVariable(ClusterVariable, const l1t::HGCalMulticluster &) const
std::vector< std::vector< WorkingPoint > > working_points_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_HGC_TPG_CLUSTER_ID(type, name)
const std::vector< std::string > & working_points() const final
void initialize(const edm::ParameterSet &conf) final
Category(float pt_min, float pt_max, float eta_min, float eta_max)
float sigmaRRMax() const
int showerLength() const
int maxLayer() const
std::vector< std::unique_ptr< TMVAEvaluator > > bdts_
float value(const l1t::HGCalMulticluster &cluster) const final
float sigmaRRTot() const
float sigmaRRMean() const
float sigmaZZ() const
Log< level::Warning, false > LogWarning
float sigmaEtaEtaMax() const
double eta() const final
momentum pseudorapidity