HiggsAnalysis-KITHiggsToTauTau
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
MetCorrectors.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "Kappa/DataFormats/interface/Kappa.h"
5 
6 #include "Artus/Core/interface/ProducerBase.h"
7 #include "Artus/Utility/interface/Utility.h"
8 
9 #include "HiggsAnalysis/KITHiggsToTauTau/interface/HttTypes.h"
10 #include "HiggsAnalysis/KITHiggsToTauTau/interface/Utility/RecoilCorrector.h"
11 #include "HiggsAnalysis/KITHiggsToTauTau/interface/Utility/MEtSys.h"
12 
13 #include <boost/regex.hpp>
14 
15 
24 template<class TMet>
25 class MetCorrectorBase: public ProducerBase<HttTypes>
26 {
27 public:
28 
30 
31  MetCorrectorBase(TMet* product_type::*metMemberUncorrected,
32  TMet product_type::*metMemberCorrected,
33  std::vector<float> product_type::*metCorrections,
34  std::string (setting_type::*GetRecoilCorrectorFile)(void) const,
35  std::string (setting_type::*GetMetShiftCorrectorFile)(void) const,
36  bool (setting_type::*GetUpdateMetWithCorrectedLeptons)(void) const
37  ) :
38  ProducerBase<HttTypes>(),
39  m_metMemberUncorrected(metMemberUncorrected),
40  m_metMemberCorrected(metMemberCorrected),
41  m_metCorrections(metCorrections),
42  GetRecoilCorrectorFile(GetRecoilCorrectorFile),
43  GetMetShiftCorrectorFile(GetMetShiftCorrectorFile),
44  GetUpdateMetWithCorrectedLeptons(GetUpdateMetWithCorrectedLeptons)
45  {
46  }
47 
48  virtual void Init(setting_type const& settings, metadata_type& metadata) override
49  {
50  ProducerBase<HttTypes>::Init(settings, metadata);
51 
52  m_recoilCorrector = new RecoilCorrector((settings.*GetRecoilCorrectorFile)());
53 
54  if ((settings.GetMetSysType() != 0) || (settings.GetMetSysShift() != 0))
55  {
56  m_metShiftCorrector = new MEtSys((settings.*GetMetShiftCorrectorFile)());
57 
58  if (settings.GetMetSysType() == 1)
59  {
60  m_sysType = MEtSys::SysType::Response;
61  }
62  else if (settings.GetMetSysType() == 2)
63  {
64  m_sysType = MEtSys::SysType::Resolution;
65  }
66  else
67  {
68  m_sysType = MEtSys::SysType::NoType;
69  LOG(FATAL) << "Invalid HttSettings::MetSysType option";
70  }
71 
72  if (settings.GetMetSysShift() > 0)
73  {
74  m_sysShift = MEtSys::SysShift::Up;
75  }
76  else
77  {
78  m_sysShift = MEtSys::SysShift::Down;
79  }
80  }
81 
82  // determine process type, trigger several decisions later
83  if (boost::regex_search(settings.GetNickname(), boost::regex("DY.?JetsToLL|W.?JetsToLNu|HToTauTau", boost::regex::extended)))
84  {
85  m_processType = MEtSys::ProcessType::BOSON;
86  }
87  else if (boost::regex_search(settings.GetNickname(), boost::regex("TT", boost::regex::extended)))
88  {
89  m_processType = MEtSys::ProcessType::TOP;
90  }
91  else
92  {
93  m_processType = MEtSys::ProcessType::EWK;
94  }
95  m_isWJets = boost::regex_search(settings.GetNickname(), boost::regex("W.?JetsToLNu", boost::regex::icase | boost::regex::extended));
96 
97  m_doMetSys = ((settings.GetMetSysType() != 0) || (settings.GetMetSysShift() != 0));
98 
99  if(settings.GetMetCorrectionMethod() == "quantileMapping")
100  m_correctionMethod = MetCorrectorBase::CorrectionMethod::QUANTILE_MAPPING;
101  else if(settings.GetMetCorrectionMethod() == "meanResolution")
102  m_correctionMethod = MetCorrectorBase::CorrectionMethod::MEAN_RESOLUTION;
103  else
104  {
105  m_correctionMethod = MetCorrectorBase::CorrectionMethod::NONE;
106  LOG(FATAL) << "Invalid MetCorrectionMethod option. Available are 'quantileMapping' and 'meanResolution'";
107  }
108 
109  if (settings.GetMetUncertaintyShift())
110  {
111  m_metUncertaintyType = HttEnumTypes::ToMETUncertaintyType(settings.GetMetUncertaintyType());
112  }
113  }
114 
115  virtual void Produce(event_type const& event, product_type & product,
116  setting_type const& settings, metadata_type const& metadata) const override
117  {
118  assert(m_metMemberUncorrected != nullptr);
119 
120  // Retrieve the needed informations from the event content
121  // and replace nominal met four vector by one shifted by
122  // specific uncertainty in order to propagate it through
123  // entire analysis if required by configuration
124  float metX = settings.GetMetUncertaintyShift() ? (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].Px() : (product.*m_metMemberUncorrected)->p4.Px();
125  float metY = settings.GetMetUncertaintyShift() ? (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].Py() : (product.*m_metMemberUncorrected)->p4.Py();
126  float metEnergy = settings.GetMetUncertaintyShift() ? (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].energy() : (product.*m_metMemberUncorrected)->p4.energy();
127  float metResolution = std::sqrt(metEnergy * metEnergy - metX * metX - metY * metY);
128 
129  // Recalculate MET if lepton energies have been corrected:
130  // MetX' = MetX + Px - Px'
131  // MetY' = MetY + Py - Py'
132  // MET' = sqrt(MetX' * MetX' + MetY' * MetY')
133  if ((settings.*GetUpdateMetWithCorrectedLeptons)())
134  {
135  // Electrons
136  for (std::vector<std::shared_ptr<KElectron> >::iterator electron = product.m_correctedElectrons.begin();
137  electron != product.m_correctedElectrons.end(); ++electron)
138  {
139  // Only update MET if there actually was a correction applied
140  if (Utility::ApproxEqual(electron->get()->p4, const_cast<KLepton*>(product.m_originalLeptons[electron->get()])->p4))
141  continue;
142 
143  float eX = electron->get()->p4.Px() - const_cast<KLepton*>(product.m_originalLeptons[electron->get()])->p4.Px();
144  float eY = electron->get()->p4.Py() - const_cast<KLepton*>(product.m_originalLeptons[electron->get()])->p4.Py();
145 
146  metX -= eX;
147  metY -= eY;
148  }
149 
150  // Muons
151  for (std::vector<std::shared_ptr<KMuon> >::iterator muon = product.m_correctedMuons.begin();
152  muon != product.m_correctedMuons.end(); ++muon)
153  {
154  // Only update MET if there actually was a correction applied
155  if (Utility::ApproxEqual(muon->get()->p4, const_cast<KLepton*>(product.m_originalLeptons[muon->get()])->p4))
156  continue;
157 
158  float eX = muon->get()->p4.Px() - const_cast<KLepton*>(product.m_originalLeptons[muon->get()])->p4.Px();
159  float eY = muon->get()->p4.Py() - const_cast<KLepton*>(product.m_originalLeptons[muon->get()])->p4.Py();
160 
161  metX -= eX;
162  metY -= eY;
163  }
164 
165  // Taus
166  for (std::vector<std::shared_ptr<KTau> >::iterator tau = product.m_correctedTaus.begin();
167  tau != product.m_correctedTaus.end(); ++tau)
168  {
169  // Only update MET if there actually was a correction applied
170  if (Utility::ApproxEqual(tau->get()->p4, const_cast<KLepton*>(product.m_originalLeptons[tau->get()])->p4))
171  continue;
172 
173  float eX = tau->get()->p4.Px() - const_cast<KLepton*>(product.m_originalLeptons[tau->get()])->p4.Px();
174  float eY = tau->get()->p4.Py() - const_cast<KLepton*>(product.m_originalLeptons[tau->get()])->p4.Py();
175 
176  metX -= eX;
177  metY -= eY;
178  }
179  }
180 
181  // Recoil corrections follow
182  int nJets30 = product_type::GetNJetsAbovePtThreshold(product.m_validJets, 30.0);
183 
184  // In selected W+Jets events one of the leptons is faked by hadronic jet and this
185  // jet should be counted as a part of hadronic recoil to the W boson
186  if(m_isWJets)
187  {
188  nJets30 += 1;
189  }
190 
191  float genPx = 0.; // generator Z(W) px
192  float genPy = 0.; // generator Z(W) py
193  float visPx = 0.; // visible (generator) Z(W) px
194  float visPy = 0.; // visible (generator) Z(W) py
195 
196  for (KGenParticles::const_iterator genParticle = event.m_genParticles->begin();
197  genParticle != event.m_genParticles->end(); ++genParticle)
198  {
199  int pdgId = std::abs(genParticle->pdgId);
200 
201  if ( (pdgId >= DefaultValues::pdgIdElectron && pdgId <= DefaultValues::pdgIdNuTau && genParticle->fromHardProcessFinalState()) ||
202  (genParticle->isDirectHardProcessTauDecayProduct()) )
203  {
204  genPx += genParticle->p4.Px();
205  genPy += genParticle->p4.Py();
206 
207  if ( !(pdgId == DefaultValues::pdgIdNuE || pdgId == DefaultValues::pdgIdNuMu || pdgId == DefaultValues::pdgIdNuTau) )
208  {
209  visPx += genParticle->p4.Px();
210  visPy += genParticle->p4.Py();
211  }
212  }
213  }
214 
215  // Save the ingredients for the correction
216  (product.*m_metCorrections).push_back(genPx);
217  (product.*m_metCorrections).push_back(genPy);
218  (product.*m_metCorrections).push_back(visPx);
219  (product.*m_metCorrections).push_back(visPy);
220 
221  float correctedMetX, correctedMetY;
222 
223  if(m_correctionMethod == MetCorrectorBase::CorrectionMethod::QUANTILE_MAPPING)
225  metX,
226  metY,
227  genPx,
228  genPy,
229  visPx,
230  visPy,
231  nJets30,
232  correctedMetX,
233  correctedMetY);
234  else if(m_correctionMethod == MetCorrectorBase::CorrectionMethod::MEAN_RESOLUTION)
236  metX,
237  metY,
238  genPx,
239  genPy,
240  visPx,
241  visPy,
242  nJets30,
243  correctedMetX,
244  correctedMetY);
245 
246  (product.*m_metMemberCorrected) = *(product.*m_metMemberUncorrected);
247 
248  // Apply the recoil correction to the MET object (only for DY, W and Higgs samples)
249  if (m_processType == MEtSys::ProcessType::BOSON)
250  {
251  (product.*m_metMemberCorrected).p4.SetPxPyPzE(
252  correctedMetX,
253  correctedMetY,
254  0.,
255  std::sqrt(metResolution * metResolution + correctedMetX * correctedMetX + correctedMetY * correctedMetY));
256  if (m_correctGlobalMet)
257  {
258  product.m_met = product.*m_metMemberCorrected;
259  }
260  }
261  else if ((settings.*GetUpdateMetWithCorrectedLeptons)()) // Apply at least corrections from lepton adjustments
262  {
263  (product.*m_metMemberCorrected).p4.SetPxPyPzE(
264  metX,
265  metY,
266  0.,
267  std::sqrt(metResolution * metResolution + metX * metX + metY * metY));
268  if (m_correctGlobalMet)
269  {
270  product.m_met = product.*m_metMemberCorrected;
271  }
272  }
273  else if (settings.GetMetUncertaintyShift()) // If no other corrections are applied, use MET shifted by uncertainty if required by configuration
274  {
275  (product.*m_metMemberCorrected).p4.SetPxPyPzE(
276  (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].Px(),
277  (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].Py(),
278  (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].Pz(),
279  (product.*m_metMemberUncorrected)->p4_shiftedByUncertainties[m_metUncertaintyType].energy());
280  if (m_correctGlobalMet)
281  {
282  product.m_met = product.*m_metMemberCorrected;
283  }
284  }
285 
286  // Apply the correction to the MET object, if required (done for all the samples)
287  if (m_doMetSys)
288  {
289  float correctedMetShiftX, correctedMetShiftY;
290 
292  (product.*m_metMemberCorrected).p4.Px(), (product.*m_metMemberCorrected).p4.Py(),
293  genPx, genPy,
294  visPx, visPy,
295  nJets30,
297  m_sysType,
298  m_sysShift,
299  correctedMetShiftX,
300  correctedMetShiftY
301  );
302 
303  (product.*m_metMemberCorrected).p4.SetPxPyPzE(
304  correctedMetShiftX,
305  correctedMetShiftY,
306  0.,
307  std::sqrt(metResolution * metResolution + correctedMetShiftX * correctedMetShiftX + correctedMetShiftY * correctedMetShiftY));
308  if (m_correctGlobalMet)
309  {
310  product.m_met = product.*m_metMemberCorrected;
311  }
312  }
313  }
314 
315 protected:
316  TMet* product_type::*m_metMemberUncorrected;
317  TMet product_type::*m_metMemberCorrected;
318  std::vector<float> product_type::*m_metCorrections;
319  std::string (setting_type::*GetRecoilCorrectorFile)(void) const;
320  std::string (setting_type::*GetMetShiftCorrectorFile)(void) const;
326  bool m_isWJets;
330  bool (setting_type::*GetUpdateMetWithCorrectedLeptons)(void) const;
331  KMETUncertainty::Type m_metUncertaintyType;
332 };
333 
334 
335 
339 class MetCorrector: public MetCorrectorBase<KMET>
340 {
341 public:
342  MetCorrector();
343  virtual void Init(setting_type const& settings, metadata_type& metadata) override;
344  virtual std::string GetProducerId() const override;
345 };
346 
351 {
352 public:
353  MvaMetCorrector();
354  virtual void Init(setting_type const& settings, metadata_type& metadata) override;
355  virtual std::string GetProducerId() const override;
356 };
MEtSys * m_metShiftCorrector
Definition: MetCorrectors.h:322
CorrectionMethod m_correctionMethod
Definition: MetCorrectors.h:328
std::string(setting_type::* GetRecoilCorrectorFile)(void) const
Definition: MetCorrectors.h:319
Definition: MetCorrectors.h:29
Definition: MEtSys.h:13
virtual void Init(setting_type const &settings, metadata_type &metadata) override
Definition: MetCorrectors.cc:18
MetCorrector()
Definition: MetCorrectors.cc:7
virtual void Init(setting_type const &settings, metadata_type &metadata) override
Definition: MetCorrectors.h:48
int energy
Definition: samples_run2_2015.py:14
void Correct(float MetPx, float MetPy, float genZPx, float genZPy, float diLepPx, float diLepPy, int njets, float &MetCorrPx, float &MetCorrPy)
Definition: RecoilCorrector.cc:214
virtual std::string GetProducerId() const override
Definition: MetCorrectors.cc:44
virtual std::string GetProducerId() const override
Definition: MetCorrectors.cc:104
MetCorrectorBase(TMet *product_type::*metMemberUncorrected, TMet product_type::*metMemberCorrected, std::vector< float > product_type::*metCorrections, std::string(setting_type::*GetRecoilCorrectorFile)(void) const, std::string(setting_type::*GetMetShiftCorrectorFile)(void) const, bool(setting_type::*GetUpdateMetWithCorrectedLeptons)(void) const )
Definition: MetCorrectors.h:31
virtual void Produce(event_type const &event, product_type &product, setting_type const &settings, metadata_type const &metadata) const override
Definition: MetCorrectors.h:115
TMet product_type::* m_metMemberCorrected
Definition: MetCorrectors.h:317
bool(setting_type::* GetUpdateMetWithCorrectedLeptons)(void) const
Definition: MetCorrectors.h:330
Definition: MetCorrectors.h:29
bool m_doMetSys
Definition: MetCorrectors.h:327
TMet *product_type::* m_metMemberUncorrected
Definition: MetCorrectors.h:316
Corrector for (PF) MET.
Definition: MetCorrectors.h:339
ProcessType
Definition: MEtSys.h:70
RecoilCorrector * m_recoilCorrector
Definition: MetCorrectors.h:321
bool m_correctGlobalMet
Definition: MetCorrectors.h:329
MEtSys::SysShift m_sysShift
Definition: MetCorrectors.h:325
SysType
Definition: MEtSys.h:71
static KMETUncertainty::Type ToMETUncertaintyType(std::string const &metUncertainty)
Definition: HttEnumTypes.h:174
bool m_isWJets
Definition: MetCorrectors.h:326
MEtSys::SysType m_sysType
Definition: MetCorrectors.h:324
CorrectionMethod
Definition: MetCorrectors.h:29
MvaMetCorrector()
Definition: MetCorrectors.cc:50
SysShift
Definition: MEtSys.h:72
std::string(setting_type::* GetMetShiftCorrectorFile)(void) const
Definition: MetCorrectors.h:320
std::vector< float > product_type::* m_metCorrections
Definition: MetCorrectors.h:318
all data types which are used for Htt analyses
Definition: HttTypes.h:27
KMETUncertainty::Type m_metUncertaintyType
Definition: MetCorrectors.h:331
tuple regex
Definition: plot_overtraining.py:51
void CorrectByMeanResolution(float MetPx, float MetPy, float genZPx, float genZPy, float diLepPx, float diLepPy, int njets, float &MetCorrPx, float &MetCorrPy)
Definition: RecoilCorrector.cc:342
MEtSys::ProcessType m_processType
Definition: MetCorrectors.h:323
void ApplyMEtSys(float metPx, float metPy, float genVPx, float genVPy, float visVPx, float visVPy, int njets, int bkgdType, int sysType, int shiftType, float &metShiftPx, float &metShiftPy)
Definition: MEtSys.cc:269
Corrector for MVAMET.
Definition: MetCorrectors.h:350
virtual void Init(setting_type const &settings, metadata_type &metadata) override
Definition: MetCorrectors.cc:61
Definition: RecoilCorrector.h:12
Definition: MetCorrectors.h:29
Corrects the MET created by the MET producer.
Definition: MetCorrectors.h:25