CMS 3D CMS Logo

JetCleanerForType1METT.h
Go to the documentation of this file.
1 #ifndef JetMETCorrections_Type1MET_JetCleanerForType1METT_h
2 #define JetMETCorrections_Type1MET_JetCleanerForType1METT_h
3 
25 
33 
36 
38 
39 #include <string>
40 #include <type_traits>
41 
42 
44 {
45  template <typename T, typename Textractor>
47  {
48  public:
49 
50  void operator()(const T&) const {} // no type-checking needed for reco::PFJet input
51  bool isPatJet(const T&) const {
53  }
54  };
55 
56  template <typename T>
57  class RawJetExtractorT // this template is neccessary to support pat::Jets
58  // (because pat::Jet->p4() returns the JES corrected, not the raw, jet momentum)
59  // But it does not handle the muon removal!!!!! MM
60  {
61  public:
62 
65  {
66  return jet.p4();
67  }
68  };
69 }
70 
71 
72 template <typename T, typename Textractor>
74 {
75  public:
76 
78  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
79  offsetCorrLabel_(""),
80  skipMuonSelection_(nullptr)
81  {
82  token_ = consumes<std::vector<T> >(cfg.getParameter<edm::InputTag>("src"));
83 
84  offsetCorrLabel_ = cfg.getParameter<edm::InputTag>("offsetCorrLabel");
85  offsetCorrToken_ = consumes<reco::JetCorrector>(offsetCorrLabel_);
86 
87  jetCorrLabel_ = cfg.getParameter<edm::InputTag>("jetCorrLabel"); //for MC
88  jetCorrLabelRes_ = cfg.getParameter<edm::InputTag>("jetCorrLabelRes"); //for data
89  jetCorrToken_ = mayConsume<reco::JetCorrector>(jetCorrLabel_);
90  jetCorrResToken_ = mayConsume<reco::JetCorrector>(jetCorrLabelRes_);
91 
92  jetCorrEtaMax_ = cfg.getParameter<double>("jetCorrEtaMax");
93 
94  type1JetPtThreshold_ = cfg.getParameter<double>("type1JetPtThreshold");
95 
96  skipEM_ = cfg.getParameter<bool>("skipEM");
97  if ( skipEM_ ) {
98  skipEMfractionThreshold_ = cfg.getParameter<double>("skipEMfractionThreshold");
99  }
100 
101  skipMuons_ = cfg.getParameter<bool>("skipMuons");
102  if ( skipMuons_ ) {
103  std::string skipMuonSelection_string = cfg.getParameter<std::string>("skipMuonSelection");
104  skipMuonSelection_.reset( new StringCutObjectSelector<reco::Candidate>(skipMuonSelection_string,true) ) ;
105  }
106 
107  calcMuonSubtrRawPtAsValueMap_ = cfg.getParameter<bool>("calcMuonSubtrRawPtAsValueMap");
108 
109  produces<std::vector<T> >();
110  if (calcMuonSubtrRawPtAsValueMap_) produces<edm::ValueMap<float>>("MuonSubtrRawPt");
111  }
112 
115  desc.add<std::string>("@module_label");
116  desc.add<edm::InputTag>("src");
117  desc.add<edm::InputTag>("offsetCorrLabel");
118  desc.add<edm::InputTag>("jetCorrLabel");
119  desc.add<edm::InputTag>("jetCorrLabelRes");
120  desc.add<double>("jetCorrEtaMax",9.9);
121  desc.add<double>("type1JetPtThreshold");
122  desc.add<bool>("skipEM");
123  desc.add<double>("skipEMfractionThreshold");
124  desc.add<bool>("skipMuons");
125  desc.add<std::string>("skipMuonSelection");
126  desc.add<bool>("calcMuonSubtrRawPtAsValueMap",false)->setComment("calculate muon-subtracted raw pt as a ValueMap for the input collection (only for selected jets, zero for others)");
127  descriptions.addDefault(desc);
128  }
129 
130  private:
131 
132  void produce(edm::Event& evt, const edm::EventSetup& es) override
133  {
134 
136  //automatic switch for residual corrections
137  if(evt.isRealData() ) {
138  jetCorrLabel_ = jetCorrLabelRes_;
139  evt.getByToken(jetCorrResToken_, jetCorr);
140  } else {
141  evt.getByToken(jetCorrToken_, jetCorr);
142  }
143 
144  typedef std::vector<T> JetCollection;
146  evt.getByToken(token_, jets);
147 
148  //new collection
149  std::unique_ptr< std::vector<T> > cleanedJets( new std::vector<T>() );
150 
151  int numJets = jets->size();
152  std::vector<float> muonSubtrRawPt(numJets,0);
153 
154  for(int jetIndex=0;jetIndex<numJets; ++jetIndex ) {
155  const T& jet = jets->at(jetIndex);
156 
158  checkInputType(jet);
159 
160  double emEnergyFraction = jet.chargedEmEnergyFraction() + jet.neutralEmEnergyFraction();
161  if(skipEM_&&emEnergyFraction>skipEMfractionThreshold_ ) continue;
162 
163  const static JetCleanerForType1MET_namespace::RawJetExtractorT<T> rawJetExtractor {};
164  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(jet);
165 
166  if ( skipMuons_ ) {
167  const std::vector<reco::CandidatePtr> & cands = jet.daughterPtrVector();
168  for ( std::vector<reco::CandidatePtr>::const_iterator cand = cands.begin();
169  cand != cands.end(); ++cand ) {
170  const reco::PFCandidate *pfcand = dynamic_cast<const reco::PFCandidate *>(cand->get());
171  const reco::Candidate *mu = (pfcand != nullptr ? ( pfcand->muonRef().isNonnull() ? pfcand->muonRef().get() : nullptr) : cand->get());
172  if ( mu != nullptr && (*skipMuonSelection_)(*mu) ) {
173  reco::Candidate::LorentzVector muonP4 = (*cand)->p4();
174  rawJetP4 -= muonP4;
175  }
176  }
177  }
178 
180  if ( checkInputType.isPatJet(jet) ) {
181  corrJetP4 = jetCorrExtractor_(jet, jetCorrLabel_.label(), jetCorrEtaMax_, &rawJetP4);
182  }
183  else {
184  corrJetP4 = jetCorrExtractor_(jet, jetCorr.product(), jetCorrEtaMax_, &rawJetP4);
185  if(corrJetP4.pt()<type1JetPtThreshold_) continue;
186  }
187 
188  if(corrJetP4.pt()<type1JetPtThreshold_) continue;
189 
190  cleanedJets->push_back(jet);
191  if (calcMuonSubtrRawPtAsValueMap_) muonSubtrRawPt[jetIndex]=rawJetP4.Pt();
192 
193  }
194 
195  evt.put(std::move(cleanedJets));
196 
197  if (calcMuonSubtrRawPtAsValueMap_) {
198  std::unique_ptr<edm::ValueMap<float>> muonSubtrRawPtV(new edm::ValueMap<float>());
199  edm::ValueMap<float>::Filler fillerMuonSubtrRawPt(*muonSubtrRawPtV);
200  fillerMuonSubtrRawPt.insert(jets,muonSubtrRawPt.begin(),muonSubtrRawPt.end());
201  fillerMuonSubtrRawPt.fill();
202  evt.put(std::move(muonSubtrRawPtV),"MuonSubtrRawPt");
203  }
204 
205  }
206 
207 
208 
210 
212 
217  edm::EDGetTokenT<reco::JetCorrector> jetCorrToken_; // e.g. 'ak5CaloJetL1FastL2L3' (MC) / 'ak5CaloJetL1FastL2L3Residual' (Data)
218  edm::EDGetTokenT<reco::JetCorrector> jetCorrResToken_; // e.g. 'ak5CaloJetL1FastL2L3' (MC) / 'ak5CaloJetL1FastL2L3Residual' (Data)
219  Textractor jetCorrExtractor_;
220 
221  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold
222 
223  double type1JetPtThreshold_; // threshold to distinguish between jets entering Type 1 MET correction
224  // and jets entering "unclustered energy" sum
225  // NOTE: threshold is applied on **corrected** jet energy (recommended default = 10 GeV)
226 
227  bool skipEM_; // flag to exclude jets with large fraction of electromagnetic energy (electrons/photons)
228  // from Type 1 + 2 MET corrections
230 
231  bool skipMuons_; // flag to subtract momentum of muons (provided muons pass selection cuts) which are within jets
232  // from jet energy before compute JECs/propagating JECs to Type 1 + 2 MET corrections
233  std::unique_ptr<StringCutObjectSelector< reco::Candidate> > skipMuonSelection_;
234 
235  bool calcMuonSubtrRawPtAsValueMap_; // calculate muon-subtracted raw pt as a ValueMap for the input collection (only for selected jets, zero for others)
236 };
237 
238 #endif
239 
T getParameter(std::string const &) const
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
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< Jet > JetCollection
Definition: Jet.h:55
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
#define nullptr
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
reco::Candidate::LorentzVector operator()(const T &jet) const
bool isRealData() const
Definition: EventBase.h:62
void addDefault(ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< reco::JetCorrector > jetCorrToken_
vector< PseudoJet > jets
JetCleanerForType1METT(const edm::ParameterSet &cfg)
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)
void produce(edm::Event &evt, const edm::EventSetup &es) override
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:459
T const * product() const
Definition: Handle.h:74
edm::EDGetTokenT< reco::JetCorrector > offsetCorrToken_
edm::EDGetTokenT< std::vector< T > > token_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
std::unique_ptr< StringCutObjectSelector< reco::Candidate > > skipMuonSelection_
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
long double T
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< reco::JetCorrector > jetCorrResToken_