Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICMetProducer.hh
Go to the documentation of this file.
1 #ifndef UserCode_ICHiggsTauTau_ICMetProducer_h
2 #define UserCode_ICHiggsTauTau_ICMetProducer_h
3 
4 #include <memory>
5 #include <vector>
6 #include <string>
7 #include "Math/Vector4D.h"
8 #include "Math/Vector4Dfwd.h"
9 #include "boost/functional/hash.hpp"
10 #include "FWCore/Framework/interface/EDProducer.h"
11 #include "FWCore/ParameterSet/interface/ParameterSet.h"
12 #include "FWCore/Framework/interface/Event.h"
13 #include "FWCore/Framework/interface/EventSetup.h"
14 #include "FWCore/Utilities/interface/InputTag.h"
15 #include "DataFormats/METReco/interface/MET.h"
16 #include "DataFormats/PatCandidates/interface/MET.h"
21 #include "FWCore/Utilities/interface/Exception.h"
22 
26 template <class T>
27 class ICMetProducer : public edm::EDProducer {
28  public:
29  explicit ICMetProducer(const edm::ParameterSet&);
31 
32  private:
33  virtual void beginJob();
34  virtual void produce(edm::Event&, const edm::EventSetup&);
35  virtual void endJob();
36  void constructSpecific(edm::Handle<edm::View<T> > const& mets_handle,
37  edm::Event& event, const edm::EventSetup& setup);
38  std::vector<ic::Met>* met_;
39  edm::InputTag input_;
40  std::string branch_;
41  boost::hash<T const*> met_hasher_;
42  bool do_custom_id_;
43  edm::InputTag inputID_;
44  bool store_id_from_user_cand_;
45 
46  struct SigTags {
47  edm::InputTag metsig;
48  edm::InputTag metsigcov00;
49  edm::InputTag metsigcov01;
50  edm::InputTag metsigcov10;
51  edm::InputTag metsigcov11;
52  explicit SigTags(edm::ParameterSet const& pset,edm::ConsumesCollector && collector);
53  };
54 
55  struct SigTagsMethod2 {
56  edm::InputTag metsig;
57  edm::InputTag metsigcov;
58  explicit SigTagsMethod2(edm::ParameterSet const& pset, edm::ConsumesCollector && collector);
59  };
60 
61  bool do_gen_met_;
62 
63  bool do_external_metsig_;
64  bool do_external_metsig_method2_;
65  SigTags metsig_;
66  SigTagsMethod2 metsig_method2_;
67 
68  bool do_metcorrections_;
69  std::vector<std::string> metcorrections_;
70  bool do_metuncertainties_;
71  std::vector<std::string> metuncertainties_;
72 };
73 
74 template <class T>
75 ICMetProducer<T>::SigTags::SigTags(edm::ParameterSet const& pset, edm::ConsumesCollector && collector)
76  : metsig(pset.getParameter<edm::InputTag>("metsig")),
77  metsigcov00(pset.getParameter<edm::InputTag>("metsigcov00")),
78  metsigcov01(pset.getParameter<edm::InputTag>("metsigcov01")),
79  metsigcov10(pset.getParameter<edm::InputTag>("metsigcov10")),
80  metsigcov11(pset.getParameter<edm::InputTag>("metsigcov11")) {
81  collector.consumes<double>(metsig);
82  collector.consumes<double>(metsigcov00);
83  collector.consumes<double>(metsigcov01);
84  collector.consumes<double>(metsigcov10);
85  collector.consumes<double>(metsigcov11);
86  }
87 
88 template <class T>
89 ICMetProducer<T>::SigTagsMethod2::SigTagsMethod2(edm::ParameterSet const& pset, edm::ConsumesCollector && collector)
90  : metsig(pset.getParameter<edm::InputTag>("metsig")),
91  metsigcov(pset.getParameter<edm::InputTag>("metsigcov")) {
92  collector.consumes<double>(metsig);
93  collector.consumes<math::Error<2>::type>(metsigcov);
94  }
95 
96 template <class T>
97 ICMetProducer<T>::ICMetProducer(const edm::ParameterSet& config)
98  : input_(config.getParameter<edm::InputTag>("input")),
99  branch_(config.getParameter<std::string>("branch")),
100  do_custom_id_(config.getParameter<bool>("includeCustomID")),
101  inputID_(config.getParameter<edm::InputTag>("inputCustomID")),
102  do_gen_met_(config.getParameter<bool>("doGenMet")),
103  do_external_metsig_(config.getParameter<bool>("includeExternalMetsig")),
104  do_external_metsig_method2_(config.getParameter<bool>("includeExternalMetsigMethod2")),
105  metsig_(config.getParameterSet("metsig"),consumesCollector()),
106  metsig_method2_(config.getParameterSet("metsig_method2"),consumesCollector()){
107  //do_metuncertainties_(config.getParameter<bool>("includeMetUncertainties")),
108  //metuncertainties_(config.getParameter<std::vector<std::string> >("metuncertainties")) {
109  consumes<edm::View<T>>(input_);
110  consumes<std::vector<std::size_t>>(inputID_);
111  met_ = new std::vector<ic::Met>();
112  PrintHeaderWithProduces(config, input_, branch_);
113  PrintOptional(1, do_custom_id_, "includeCustomID");
114 }
115 
116 template <>
117 ICMetProducer<pat::MET>::ICMetProducer(const edm::ParameterSet& config)
118  : input_(config.getParameter<edm::InputTag>("input")),
119  branch_(config.getParameter<std::string>("branch")),
120  do_custom_id_(config.getParameter<bool>("includeCustomID")),
121  inputID_(config.getParameter<edm::InputTag>("inputCustomID")),
122  store_id_from_user_cand_(config.getParameter<bool>("includeUserCandID")),
123  do_gen_met_(config.getParameter<bool>("doGenMet")),
124  do_external_metsig_(config.getParameter<bool>("includeExternalMetsig")),
125  do_external_metsig_method2_(config.getParameter<bool>("includeExternalMetsigMethod2")),
126  metsig_(config.getParameterSet("metsig"),consumesCollector()),
127  metsig_method2_(config.getParameterSet("metsig_method2"),consumesCollector()),
128  do_metcorrections_(config.getParameter<bool>("includeMetCorrections")),
129  metcorrections_(config.getParameter<std::vector<std::string> >("metcorrections")),
130  do_metuncertainties_(config.getParameter<bool>("includeMetUncertainties")),
131  metuncertainties_(config.getParameter<std::vector<std::string> >("metuncertainties")) {
132  consumes<edm::View<pat::MET>>(input_);
133  consumes<std::vector<std::size_t>>(inputID_);
134 
135  met_ = new std::vector<ic::Met>();
136  PrintHeaderWithProduces(config, input_, branch_);
137  PrintOptional(1, do_custom_id_, "includeCustomID");
138  PrintOptional(1, do_metuncertainties_, "includeMetUncertainties");
139  PrintOptional(1, do_metcorrections_, "includeMetCorrections");
140 }
141 
142 
143 template <class T>
145  delete met_;
146 }
147 
148 template <class T>
149 void ICMetProducer<T>::produce(edm::Event& event, const edm::EventSetup& setup) {
150  edm::Handle<edm::View<T> > mets_handle;
151  event.getByLabel(input_, mets_handle);
152  edm::Handle<std::vector<std::size_t> > id_handle;
153  if (do_custom_id_) {
154  event.getByLabel(inputID_, id_handle);
155  }
156  // event.getByLabel(merge_labels_[i], "MVAMetId", id_handle);
157 
158  met_->clear();
159  met_->resize(mets_handle->size(), ic::Met());
160 
161 
162  for (unsigned i = 0; i < mets_handle->size(); ++i) {
163  T const& src = mets_handle->at(i);
164  ic::Met& dest = met_->at(i);
165  if (do_custom_id_) {
166  dest.set_id(id_handle->at(i));
167  } else {
168  dest.set_id(met_hasher_(&src));
169  }
170  if (!do_gen_met_){
171 
172  if(!do_external_metsig_&&!do_external_metsig_method2_){
173  dest.set_et_sig(src.mEtSig());
174  dest.set_xx_sig(src.getSignificanceMatrix()(0, 0));
175  dest.set_xy_sig(src.getSignificanceMatrix()(0, 1));
176  dest.set_yx_sig(src.getSignificanceMatrix()(1, 0));
177  dest.set_yy_sig(src.getSignificanceMatrix()(1, 1));
178  }
179  else{
180  if(!do_external_metsig_method2_){
181  edm::Handle<double> metsig;
182  edm::Handle<double> metsigcov00;
183  edm::Handle<double> metsigcov01;
184  edm::Handle<double> metsigcov10;
185  edm::Handle<double> metsigcov11;
186  event.getByLabel(metsig_.metsig, metsig);
187  event.getByLabel(metsig_.metsigcov00, metsigcov00);
188  event.getByLabel(metsig_.metsigcov01, metsigcov01);
189  event.getByLabel(metsig_.metsigcov10, metsigcov10);
190  event.getByLabel(metsig_.metsigcov11, metsigcov11);
191  dest.set_et_sig(*metsig);
192  dest.set_xx_sig(*metsigcov00);
193  dest.set_xy_sig(*metsigcov01);
194  dest.set_yx_sig(*metsigcov10);
195  dest.set_yy_sig(*metsigcov11);
196  } else{
197  edm::Handle<double> metsig;
198  edm::Handle<math::Error<2>::type> metsigcov;
199  event.getByLabel(metsig_method2_.metsig, metsig);
200  event.getByLabel(metsig_method2_.metsigcov, metsigcov);
201  dest.set_et_sig(*metsig);
202  dest.set_xx_sig((*metsigcov)(0,0));
203  dest.set_xy_sig((*metsigcov)(0,1));
204  dest.set_yx_sig((*metsigcov)(1,0));
205  dest.set_yy_sig((*metsigcov)(1,1));
206  }
207  }
208  }
209  }
210  constructSpecific(mets_handle, event, setup);
211 }
212 
213 //Specific stuff:
214 template <class T>
216  edm::Handle<edm::View<T> > const& mets_handle, edm::Event& event,
217  const edm::EventSetup& setup) {}
218 
219 template <>
221  edm::Handle<edm::View<reco::MET> > const& mets_handle, edm::Event& event,
222  const edm::EventSetup& setup) {
223  for (unsigned i = 0; i < mets_handle->size(); ++i) {
224  reco::MET const& src = mets_handle->at(i);
225  ic::Met& dest = met_->at(i);
226  dest.set_pt(src.pt());
227  dest.set_eta(src.eta());
228  dest.set_phi(src.phi());
229  dest.set_energy(src.energy());
230  dest.set_sum_et(src.sumEt());
231  }
232  }
233 
234 template <>
236  edm::Handle<edm::View<pat::MET> > const& mets_handle, edm::Event& event,
237  const edm::EventSetup& setup) {
238  for (unsigned i = 0; i < mets_handle->size(); ++i) {
239  pat::MET const& src = mets_handle->at(i);
240  ic::Met& dest = met_->at(i);
241  if(store_id_from_user_cand_){
242  std::size_t id = 0;
243  std::vector<std::string> candidate_names = src.userCandNames();
244  for(unsigned j = 0; j < candidate_names.size(); ++j){
245  reco::CandidatePtr cand_ref = src.userCand(candidate_names.at(j));
246  boost::hash_combine(id, cand_ref.get());
247  }
248  dest.set_id(id);
249  }
250 
251 
252  if (do_gen_met_){
253  dest.set_pt(src.genMET()->pt());
254  dest.set_eta(src.genMET()->eta());
255  dest.set_phi(src.genMET()->phi());
256  dest.set_energy(src.genMET()->energy());
257  dest.set_sum_et(src.genMET()->sumEt());
258  return;
259  }
260 
261  dest.set_pt(src.pt());
262  dest.set_eta(src.eta());
263  dest.set_phi(src.phi());
264  dest.set_energy(src.energy());
265  dest.set_sum_et(src.sumEt());
266 
267 
268 #if CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION ==7 && CMSSW_MINOR_VERSION >=4)
269  // Only write correction into the output met if the user wants it
270 #if CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION == 7 && CMSSW_REVISION >= 12)
271  if (do_metcorrections_) {
272  if (metcorrections_.size() != pat::MET::METCorrectionLevel::METCorrectionLevelSize){
273  throw cms::Exception("MetCorrectionNotRecognised")<<__FILE__ << " line " << __LINE__ << ": size of expected met correction object is " << metcorrections_.size() << " but pat::MET::METCorrectionLevel enum contains " << pat::MET::METCorrectionLevel::METCorrectionLevelSize << " elements. Code needs updating.\n";
274  }
275 
276  for (unsigned j = 0; j < static_cast<unsigned>(pat::MET::METCorrectionLevel::METCorrectionLevelSize) ; ++j) {
277  ic::Met::BasicMet tmp;
278  tmp.px = src.corPx(static_cast<pat::MET::METCorrectionLevel>(j));
279  tmp.py = src.corPy(static_cast<pat::MET::METCorrectionLevel>(j));
280  tmp.sumet = src.corSumEt(static_cast<pat::MET::METCorrectionLevel>(j));
281  dest.SetCorrectedMet(metcorrections_[j],tmp);
282  }
283 
284  }
285 #endif
286  if(do_metuncertainties_){
287  if (metuncertainties_.size() != pat::MET::METUncertainty::METUncertaintySize){
288  throw cms::Exception("MetCorrectionNotRecognised")<<__FILE__ << " line " << __LINE__ << ": size of expected met uncertainties object is " << metuncertainties_.size() << " but pat::MET::METUncertainty enum contains " << pat::MET::METUncertainty::METUncertaintySize << " elements. Code needs updating.\n";
289  }
290 
291  for(unsigned iunc=0;iunc<static_cast<unsigned>(pat::MET::METUncertainty::METUncertaintySize);iunc++){
292  ic::Met::BasicMet tmp;
293  tmp.px = src.shiftedPx(static_cast<pat::MET::METUncertainty>(iunc));
294  tmp.py = src.shiftedPy(static_cast<pat::MET::METUncertainty>(iunc));
295  tmp.sumet = src.shiftedSumEt(static_cast<pat::MET::METUncertainty>(iunc));
296  dest.SetShiftedMet(metuncertainties_[iunc],tmp);
297 
298 
299  }
300 
301  }
302 #endif
303  }
304 }
305 
306 
307 
308 template <class T>
310  ic::StaticTree::tree_->Branch(branch_.c_str(), &met_);
311 }
312 
313 template <class T>
314 void ICMetProducer<T>::endJob() {}
315 
316 
317 
318 
319 #endif
Stores a missing transverse energy object and the corresponding significance and corrections.
Definition: Met.hh:15
double sumet
Definition: Met.hh:18
See documentation here.
void set_xx_sig(double const &xx_sig)
The component of the significance matrix.
Definition: Met.hh:86
void set_yy_sig(double const &yy_sig)
The component of the significance matrix.
Definition: Met.hh:95
void set_et_sig(double const &et_sig)
Signifiance of the missing transverse energy.
Definition: Met.hh:83
void set_phi(double const &phi)
Direct access to .
Definition: Candidate.hh:75
void set_id(std::size_t const &id)
Unique identifier.
Definition: Candidate.hh:66
static TTree * tree_
Definition: StaticTree.hh:13
double px
Definition: Met.hh:18
void SetCorrectedMet(std::string const &name, ic::Met::BasicMet const &value)
Store a corrected met, overwriting any existing value with label name
Definition: Met.cc:31
void set_energy(double const &energy)
Direct access to the energy.
Definition: Candidate.hh:78
void set_yx_sig(double const &yx_sig)
The component of the significance matrix.
Definition: Met.hh:92
void SetShiftedMet(std::string const &name, ic::Met::BasicMet const &value)
Store a shifted met, overwriting any existing value with label name
Definition: Met.cc:35
ICMetProducer(const edm::ParameterSet &)
double py
Definition: Met.hh:18
void PrintOptional(unsigned depth, bool value, std::string text)
Definition: Consumes.h:19
edm::ConsumesCollector consumesCollector()
Definition: Consumes.cc:4
void set_pt(double const &pt)
Direct access to the .
Definition: Candidate.hh:69
void set_eta(double const &eta)
Direct access to .
Definition: Candidate.hh:72
void set_sum_et(double const &sum_et)
Scalar sum of transverse energies for all input objects.
Definition: Met.hh:80
void PrintHeaderWithProduces(edm::ParameterSet const &config, edm::InputTag const &in, std::string branch)
void set_xy_sig(double const &xy_sig)
The component of the significance matrix.
Definition: Met.hh:89