CMS 3D CMS Logo

PFJetMETcorrInputProducerT.h
Go to the documentation of this file.
1 #ifndef JetMETCorrections_Type1MET_PFJetMETcorrInputProducerT_h
2 #define JetMETCorrections_Type1MET_PFJetMETcorrInputProducerT_h
3 
27 
35 
38 
40 
42 
43 #include <string>
44 #include <type_traits>
45 
47 {
48  template <typename T, typename Textractor>
50  {
51  public:
52 
53  void operator()(const T&) const {} // no type-checking needed for reco::PFJet input
54  bool isPatJet(const T&) const {
56  }
57  };
58 
59  template <typename T>
60  class RawJetExtractorT // this template is neccessary to support pat::Jets
61  // (because pat::Jet->p4() returns the JES corrected, not the raw, jet momentum)
62  // But it does not handle the muon removal!!!!! MM
63  {
64  public:
65 
68  {
69  return jet.p4();
70  }
71  };
72 
73 }
74 
75 template <typename T, typename Textractor>
77 {
78  public:
79 
81  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
82  offsetCorrLabel_(""),
83  skipMuonSelection_(nullptr)
84  {
85  token_ = consumes<std::vector<T> >(cfg.getParameter<edm::InputTag>("src"));
86 
87  if ( cfg.exists("offsetCorrLabel") ) {
88  offsetCorrLabel_ = cfg.getParameter<edm::InputTag>("offsetCorrLabel");
89  offsetCorrToken_ = consumes<reco::JetCorrector>(offsetCorrLabel_);
90  }
91  jetCorrLabel_ = cfg.getParameter<edm::InputTag>("jetCorrLabel"); //for MC
92  jetCorrLabelRes_ = cfg.getParameter<edm::InputTag>("jetCorrLabelRes"); //for data
93  jetCorrToken_ = mayConsume<reco::JetCorrector>(jetCorrLabel_);
94  jetCorrResToken_ = mayConsume<reco::JetCorrector>(jetCorrLabelRes_);
95 
96  jetCorrEtaMax_ = ( cfg.exists("jetCorrEtaMax") ) ?
97  cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
98 
99  type1JetPtThreshold_ = cfg.getParameter<double>("type1JetPtThreshold");
100 
101  skipEM_ = cfg.getParameter<bool>("skipEM");
102  if ( skipEM_ ) {
103  skipEMfractionThreshold_ = cfg.getParameter<double>("skipEMfractionThreshold");
104  }
105 
106  skipMuons_ = cfg.getParameter<bool>("skipMuons");
107  if ( skipMuons_ ) {
108  std::string skipMuonSelection_string = cfg.getParameter<std::string>("skipMuonSelection");
109  skipMuonSelection_ = new StringCutObjectSelector<reco::Candidate>(skipMuonSelection_string,true);
110  }
111 
112  if ( cfg.exists("type2Binning") ) {
113  typedef std::vector<edm::ParameterSet> vParameterSet;
114  vParameterSet cfgType2Binning = cfg.getParameter<vParameterSet>("type2Binning");
115  for ( vParameterSet::const_iterator cfgType2BinningEntry = cfgType2Binning.begin();
116  cfgType2BinningEntry != cfgType2Binning.end(); ++cfgType2BinningEntry ) {
117  type2Binning_.push_back(new type2BinningEntryType(*cfgType2BinningEntry));
118  }
119  } else {
120  type2Binning_.push_back(new type2BinningEntryType());
121  }
122 
123  produces<CorrMETData>("type1");
124  for ( typename std::vector<type2BinningEntryType*>::const_iterator type2BinningEntry = type2Binning_.begin();
125  type2BinningEntry != type2Binning_.end(); ++type2BinningEntry ) {
126  produces<CorrMETData>((*type2BinningEntry)->getInstanceLabel_full("type2"));
127  produces<CorrMETData>((*type2BinningEntry)->getInstanceLabel_full("offset"));
128  }
129  }
131  {
132  delete skipMuonSelection_;
133 
134  for ( typename std::vector<type2BinningEntryType*>::const_iterator it = type2Binning_.begin();
135  it != type2Binning_.end(); ++it ) {
136  delete (*it);
137  }
138  }
139 
142  desc.add<edm::InputTag>("src",edm::InputTag("ak4PFJetsCHS"));
143  desc.add<edm::InputTag>("offsetCorrLabel",edm::InputTag("ak4PFCHSL1FastjetCorrector"));
144  desc.add<edm::InputTag>("jetCorrLabel",edm::InputTag("ak4PFCHSL1FastL2L3Corrector"));
145  desc.add<edm::InputTag>("jetCorrLabelRes",edm::InputTag("ak4PFCHSL1FastL2L3ResidualCorrector"));
146  desc.add<double>("jetCorrEtaMax",9.9);
147  desc.add<double>("type1JetPtThreshold",15);
148  desc.add<bool>("skipEM",true);
149  desc.add<double>("skipEMfractionThreshold",0.90);
150  desc.add<bool>("skipMuons",true);
151  desc.add<std::string>("skipMuonSelection","isGlobalMuon | isStandAloneMuon");
153  }
154 
155 
156  private:
157 
158  void produce(edm::Event& evt, const edm::EventSetup& es) override
159  {
160 
161  std::unique_ptr<CorrMETData> type1Correction(new CorrMETData());
162  for ( typename std::vector<type2BinningEntryType*>::iterator type2BinningEntry = type2Binning_.begin();
163  type2BinningEntry != type2Binning_.end(); ++type2BinningEntry ) {
164  (*type2BinningEntry)->binUnclEnergySum_ = CorrMETData();
165  (*type2BinningEntry)->binOffsetEnergySum_ = CorrMETData();
166  }
167 
169  //automatic switch for residual corrections
170  if(evt.isRealData() ) {
171  jetCorrLabel_ = jetCorrLabelRes_;
172  evt.getByToken(jetCorrResToken_, jetCorr);
173  } else {
174  evt.getByToken(jetCorrToken_, jetCorr);
175  }
176 
177  typedef std::vector<T> JetCollection;
179  evt.getByToken(token_, jets);
180 
181  int numJets = jets->size();
182  for ( int jetIndex = 0; jetIndex < numJets; ++jetIndex ) {
183  const T& jet = jets->at(jetIndex);
184 
186  checkInputType(jet);
187 
188  double emEnergyFraction = jet.chargedEmEnergyFraction() + jet.neutralEmEnergyFraction();
189  if ( skipEM_ && emEnergyFraction > skipEMfractionThreshold_ ) continue;
190 
191  const static PFJetMETcorrInputProducer_namespace::RawJetExtractorT<T> rawJetExtractor {};
192  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(jet);
193 
194  if ( skipMuons_ ) {
195  const std::vector<reco::CandidatePtr> & cands = jet.daughterPtrVector();
196  for ( std::vector<reco::CandidatePtr>::const_iterator cand = cands.begin();
197  cand != cands.end(); ++cand ) {
198  const reco::PFCandidate *pfcand = dynamic_cast<const reco::PFCandidate *>(cand->get());
199  const reco::Candidate *mu = (pfcand != nullptr ? ( pfcand->muonRef().isNonnull() ? pfcand->muonRef().get() : nullptr) : cand->get());
200  if ( mu != nullptr && (*skipMuonSelection_)(*mu) ) {
201  reco::Candidate::LorentzVector muonP4 = (*cand)->p4();
202  rawJetP4 -= muonP4;
203  }
204  }
205  }
206 
208  if ( checkInputType.isPatJet(jet) )
209  corrJetP4 = jetCorrExtractor_(jet, jetCorrLabel_.label(), jetCorrEtaMax_, &rawJetP4);
210  else
211  corrJetP4 = jetCorrExtractor_(jet, jetCorr.product(), jetCorrEtaMax_, &rawJetP4);
212 
213  if ( corrJetP4.pt() > type1JetPtThreshold_ ) {
214 
215  reco::Candidate::LorentzVector rawJetP4offsetCorr = rawJetP4;
216  if ( !offsetCorrLabel_.label().empty() ) {
218  evt.getByToken(offsetCorrToken_, offsetCorr);
219  if ( checkInputType.isPatJet(jet) )
220  rawJetP4offsetCorr = jetCorrExtractor_(jet, offsetCorrLabel_.label(), jetCorrEtaMax_, &rawJetP4);
221  else
222  rawJetP4offsetCorr = jetCorrExtractor_(jet, offsetCorr.product(), jetCorrEtaMax_, &rawJetP4);
223 
224  for ( typename std::vector<type2BinningEntryType*>::iterator type2BinningEntry = type2Binning_.begin();
225  type2BinningEntry != type2Binning_.end(); ++type2BinningEntry ) {
226  if ( !(*type2BinningEntry)->binSelection_ || (*(*type2BinningEntry)->binSelection_)(corrJetP4) ) {
227  (*type2BinningEntry)->binOffsetEnergySum_.mex += (rawJetP4.px() - rawJetP4offsetCorr.px());
228  (*type2BinningEntry)->binOffsetEnergySum_.mey += (rawJetP4.py() - rawJetP4offsetCorr.py());
229  (*type2BinningEntry)->binOffsetEnergySum_.sumet += (rawJetP4.Et() - rawJetP4offsetCorr.Et());
230  }
231  }
232  }
233 
234 //--- MET balances momentum of reconstructed particles,
235 // hence correction to jets and corresponding Type 1 MET correction are of opposite sign
236  type1Correction->mex -= (corrJetP4.px() - rawJetP4offsetCorr.px());
237  type1Correction->mey -= (corrJetP4.py() - rawJetP4offsetCorr.py());
238  type1Correction->sumet += (corrJetP4.Et() - rawJetP4offsetCorr.Et());
239  } else {
240  for ( typename std::vector<type2BinningEntryType*>::iterator type2BinningEntry = type2Binning_.begin();
241  type2BinningEntry != type2Binning_.end(); ++type2BinningEntry ) {
242  if ( !(*type2BinningEntry)->binSelection_ || (*(*type2BinningEntry)->binSelection_)(corrJetP4) ) {
243  (*type2BinningEntry)->binUnclEnergySum_.mex += rawJetP4.px();
244  (*type2BinningEntry)->binUnclEnergySum_.mey += rawJetP4.py();
245  (*type2BinningEntry)->binUnclEnergySum_.sumet += rawJetP4.Et();
246  }
247  }
248  }
249  }
250 
251 //--- add
252 // o Type 1 MET correction (difference corrected-uncorrected jet energy for jets of (corrected) Pt > 10 GeV)
253 // o momentum sum of "unclustered energy" (jets of (corrected) Pt < 10 GeV)
254 // o momentum sum of "offset energy" (sum of energy attributed to pile-up/underlying event)
255 // to the event
256  evt.put(std::move(type1Correction), "type1");
257  for ( typename std::vector<type2BinningEntryType*>::const_iterator type2BinningEntry = type2Binning_.begin();
258  type2BinningEntry != type2Binning_.end(); ++type2BinningEntry ) {
259  evt.put(std::unique_ptr<CorrMETData>(new CorrMETData((*type2BinningEntry)->binUnclEnergySum_)), (*type2BinningEntry)->getInstanceLabel_full("type2"));
260  evt.put(std::unique_ptr<CorrMETData>(new CorrMETData((*type2BinningEntry)->binOffsetEnergySum_)), (*type2BinningEntry)->getInstanceLabel_full("offset"));
261  }
262  }
263 
265 
267 
272  edm::EDGetTokenT<reco::JetCorrector> jetCorrToken_; // e.g. 'ak5CaloJetL1FastL2L3' (MC) / 'ak5CaloJetL1FastL2L3Residual' (Data)
273  edm::EDGetTokenT<reco::JetCorrector> jetCorrResToken_; // e.g. 'ak5CaloJetL1FastL2L3' (MC) / 'ak5CaloJetL1FastL2L3Residual' (Data)
274  Textractor jetCorrExtractor_;
275 
276  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold
277 
278  double type1JetPtThreshold_; // threshold to distinguish between jets entering Type 1 MET correction
279  // and jets entering "unclustered energy" sum
280  // NOTE: threshold is applied on **corrected** jet energy (recommended default = 10 GeV)
281 
282  bool skipEM_; // flag to exclude jets with large fraction of electromagnetic energy (electrons/photons)
283  // from Type 1 + 2 MET corrections
285 
286  bool skipMuons_; // flag to subtract momentum of muons (provided muons pass selection cuts) which are within jets
287  // from jet energy before compute JECs/propagating JECs to Type 1 + 2 MET corrections
289 
291  {
293  : binLabel_(""),
294  binSelection_(nullptr)
295  {}
297  : binLabel_(cfg.getParameter<std::string>("binLabel")),
298  binSelection_(new StringCutObjectSelector<reco::Candidate::LorentzVector>(cfg.getParameter<std::string>("binSelection")))
299  {}
301  {
302  delete binSelection_;
303  }
305  {
306  std::string retVal = instanceLabel;
307  if ( instanceLabel != "" && binLabel_ != "" ) retVal.append("#");
308  retVal.append(binLabel_);
309  return retVal;
310  }
315  };
316  std::vector<type2BinningEntryType*> type2Binning_;
317 };
318 
319 #endif
320 
321 
322 
323 
T getParameter(std::string const &) const
reco::Candidate::LorentzVector operator()(const T &jet) const
edm::EDGetTokenT< reco::JetCorrector > jetCorrResToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
std::vector< Jet > JetCollection
Definition: Jet.h:55
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< reco::JetCorrector > jetCorrToken_
#define nullptr
StringCutObjectSelector< reco::Candidate > * skipMuonSelection_
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::EDGetTokenT< reco::JetCorrector > offsetCorrToken_
std::string defaultModuleLabel()
bool isRealData() const
Definition: EventBase.h:62
std::vector< type2BinningEntryType * > type2Binning_
edm::EDGetTokenT< std::vector< T > > token_
vector< PseudoJet > jets
StringCutObjectSelector< reco::Candidate::LorentzVector > * binSelection_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
math::XYZTLorentzVector LorentzVector
const int mu
Definition: Constants.h:22
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:243
ParameterDescriptionBase * add(U const &iLabel, T const &value)
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:459
T const * product() const
Definition: Handle.h:74
PFJetMETcorrInputProducerT(const edm::ParameterSet &cfg)
std::string getInstanceLabel_full(const std::string &instanceLabel)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
a MET correction term
Definition: CorrMETData.h:14
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
long double T
def move(src, dest)
Definition: eostools.py:511
void produce(edm::Event &evt, const edm::EventSetup &es) override