CMS 3D CMS Logo

HGCalTriggerClusterIdentificationBDT.cc
Go to the documentation of this file.
1 #include <limits>
7 
8 
9 
11 {
12 
13  public:
14  class Category
15  {
16  public:
17  Category(float pt_min, float pt_max, float eta_min, float eta_max):
18  pt_min_(pt_min), pt_max_(pt_max),
19  eta_min_(eta_min), eta_max_(eta_max)
20  {
21  }
23  bool contains(float pt, float eta) const
24  {
25  bool output = true;
26  if(pt<pt_min_ || pt>=pt_max_) output = false;
27  if(std::abs(eta)<eta_min_ || std::abs(eta)>=eta_max_) output = false;
28  return output;
29  }
30 
31  float pt_min() const {return pt_min_;}
32  float pt_max() const {return pt_max_;}
33  float eta_min() const {return eta_min_;}
34  float eta_max() const {return eta_max_;}
35 
36  private:
37  float pt_min_ = 0.;
39  float eta_min_ = 1.5;
40  float eta_max_ = 3.;
41  };
42 
43  public:
44 
47  void initialize(const edm::ParameterSet& conf) final;
48  float value(const l1t::HGCalMulticluster& cluster) const final;
49  bool decision(const l1t::HGCalMulticluster& cluster) const final;
50 
51  private:
52  enum class ClusterVariable
53  {
54  cl3d_showerlength,
55  cl3d_coreshowerlength,
56  cl3d_firstlayer,
57  cl3d_maxlayer,
58  cl3d_seetot,
59  cl3d_seemax,
60  cl3d_spptot,
61  cl3d_sppmax,
62  cl3d_szz,
63  cl3d_srrtot,
64  cl3d_srrmax,
65  cl3d_srrmean
66  };
67  std::vector<Category> categories_;
68  std::vector<std::unique_ptr<TMVAEvaluator>> bdts_;
69  std::vector<double> working_points_;
70  std::vector<std::string> input_variables_;
71  std::vector<ClusterVariable> input_variables_id_;
72 
74  int category(float pt, float eta) const;
75 
76 
77 };
78 
79 DEFINE_HGC_TPG_CLUSTER_ID(HGCalTriggerClusterIdentificationBDT,"HGCalTriggerClusterIdentificationBDT" );
80 
81 
84 {
85 }
86 
87 void
90 {
91  if(!bdts_.empty())
92  {
93  edm::LogWarning("HGCalTriggerClusterIdentificationBDT|Initialization")
94  << "BDTs already initialized.";
95  return;
96  }
97  input_variables_ = conf.getParameter< std::vector<std::string> >("Inputs");
98  std::vector<std::string> bdt_files = conf.getParameter< std::vector<std::string> >("Weights");
99  std::vector<double> categories_etamin = conf.getParameter<std::vector<double>>("CategoriesEtaMin");
100  std::vector<double> categories_etamax = conf.getParameter<std::vector<double>>("CategoriesEtaMax");
101  std::vector<double> categories_ptmin = conf.getParameter<std::vector<double>>("CategoriesPtMin");
102  std::vector<double> categories_ptmax = conf.getParameter<std::vector<double>>("CategoriesPtMax");
103  working_points_ = conf.getParameter<std::vector<double>>("WorkingPoints");
104 
105  if(bdt_files.size()!=categories_etamin.size() ||
106  categories_etamin.size()!=categories_etamax.size() ||
107  categories_etamax.size()!=categories_ptmin.size() ||
108  categories_ptmin.size()!=categories_ptmax.size() ||
109  categories_ptmax.size()!=working_points_.size()
110  )
111  {
112  throw cms::Exception("HGCalTriggerClusterIdentificationBDT|BadInitialization")
113  << "Inconsistent numbers of categories, BDT weight files and working points";
114  }
115  categories_.reserve(working_points_.size());
116  bdts_.reserve(working_points_.size());
117  for(unsigned cat=0; cat<categories_etamin.size(); cat++)
118  {
119  categories_.emplace_back(
120  categories_ptmin[cat],
121  categories_ptmax[cat],
122  categories_etamin[cat],
123  categories_etamax[cat]);
124  }
125  std::vector<std::string> spectators = {};
126  for (const auto& file : bdt_files)
127  {
128  bdts_.emplace_back(new TMVAEvaluator());
129  bdts_.back()->initialize(
130  "!Color:Silent:!Error",
131  "BDT::bdt",
132  edm::FileInPath(file).fullPath(),
134  spectators,
135  false, false);
136  }
137 
138  // Transform input variable strings to enum values for later comparisons
139  input_variables_id_.reserve(input_variables_.size());
140  for(const auto& variable : input_variables_)
141  {
142  if(variable=="cl3d_showerlength") input_variables_id_.push_back(ClusterVariable::cl3d_showerlength);
143  else if(variable=="cl3d_coreshowerlength") input_variables_id_.push_back(ClusterVariable::cl3d_coreshowerlength);
144  else if(variable=="cl3d_firstlayer") input_variables_id_.push_back(ClusterVariable::cl3d_firstlayer);
145  else if(variable=="cl3d_maxlayer") input_variables_id_.push_back(ClusterVariable::cl3d_maxlayer);
146  else if(variable=="cl3d_seetot") input_variables_id_.push_back(ClusterVariable::cl3d_seetot);
147  else if(variable=="cl3d_seemax") input_variables_id_.push_back(ClusterVariable::cl3d_seemax);
148  else if(variable=="cl3d_spptot") input_variables_id_.push_back(ClusterVariable::cl3d_spptot);
149  else if(variable=="cl3d_sppmax") input_variables_id_.push_back(ClusterVariable::cl3d_sppmax);
150  else if(variable=="cl3d_szz") input_variables_id_.push_back(ClusterVariable::cl3d_szz);
151  else if(variable=="cl3d_srrtot") input_variables_id_.push_back(ClusterVariable::cl3d_srrtot);
152  else if(variable=="cl3d_srrmax") input_variables_id_.push_back(ClusterVariable::cl3d_srrmax);
153  else if(variable=="cl3d_srrmean") input_variables_id_.push_back(ClusterVariable::cl3d_srrmean);
154  }
155 }
156 
157 float
159 value(const l1t::HGCalMulticluster& cluster) const
160 {
161  std::map<std::string, float> inputs;
162  for(unsigned i=0; i<input_variables_.size(); i++)
163  {
165  }
166  float pt = cluster.pt();
167  float eta = cluster.eta();
168  int cat = category(pt, eta);
169  return (cat!=-1 ? bdts_.at(cat)->evaluate(inputs) : -999.);
170 }
171 
172 
173 bool
175 decision(const l1t::HGCalMulticluster& cluster) const
176 {
177  float bdt_output = value(cluster);
178  float pt = cluster.pt();
179  float eta = cluster.eta();
180  int cat = category(pt, eta);
181  return (cat!=-1 ? bdt_output>working_points_.at(cat) : true);
182 }
183 
184 int
186 category(float pt, float eta) const
187 {
188  for(unsigned cat=0; cat<categories_.size(); cat++)
189  {
190  if(categories_[cat].contains(pt, eta)) return static_cast<int>(cat);
191  }
192  return -1;
193 }
194 
195 
196 
197 
198 float
201 {
202  switch(variable)
203  {
204  case ClusterVariable::cl3d_showerlength: return cluster.showerLength();
206  case ClusterVariable::cl3d_firstlayer: return cluster.firstLayer();
207  case ClusterVariable::cl3d_maxlayer: return cluster.maxLayer();
208  case ClusterVariable::cl3d_seetot: return cluster.sigmaEtaEtaTot();
209  case ClusterVariable::cl3d_seemax: return cluster.sigmaEtaEtaMax();
210  case ClusterVariable::cl3d_spptot: return cluster.sigmaPhiPhiTot();
211  case ClusterVariable::cl3d_sppmax: return cluster.sigmaPhiPhiMax();
212  case ClusterVariable::cl3d_szz: return cluster.sigmaZZ();
213  case ClusterVariable::cl3d_srrtot: return cluster.sigmaRRTot();
214  case ClusterVariable::cl3d_srrmax: return cluster.sigmaRRMax();
215  case ClusterVariable::cl3d_srrmean: return cluster.sigmaRRMean();
216  default: break;
217  }
218  return 0.;
219 }
T getParameter(std::string const &) const
int maxLayer() const
double eta() const final
momentum pseudorapidity
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:38
float sigmaPhiPhiTot() const
float sigmaEtaEtaMax() const
float sigmaRRTot() const
double pt() const final
transverse momentum
float sigmaZZ() const
def cat(path)
Definition: eostools.py:401
int showerLength() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_HGC_TPG_CLUSTER_ID(type, name)
void initialize(const edm::ParameterSet &conf) final
Category(float pt_min, float pt_max, float eta_min, float eta_max)
float clusterVariable(ClusterVariable, const l1t::HGCalMulticluster &) const
float sigmaEtaEtaTot() const
float value(const l1t::HGCalMulticluster &cluster) const final
int coreShowerLength() const
std::vector< std::unique_ptr< TMVAEvaluator > > bdts_
int firstLayer() const
float sigmaRRMean() const
bool decision(const l1t::HGCalMulticluster &cluster) const final
float sigmaRRMax() const
float sigmaPhiPhiMax() const