CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NoPileUpPFMEtDataProducer.cc
Go to the documentation of this file.
2 
5 
6 #include <algorithm>
7 #include <cmath>
8 
9 const int flag_isWithinFakeJet = 1;
12 
13 const double dR2Min = 0.001 * 0.001;
14 
16  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
17  looseJetIdAlgo_(nullptr),
18  pfMEtSignInterface_(nullptr) {
19  srcJets_ = consumes<reco::PFJetCollection>(cfg.getParameter<edm::InputTag>("srcJets"));
20  srcJetIds_ = consumes<edm::ValueMap<int> >(cfg.getParameter<edm::InputTag>("srcJetIds"));
21  minJetPt_ = cfg.getParameter<double>("minJetPt");
22  std::string jetIdSelection_string = cfg.getParameter<std::string>("jetIdSelection");
23  if (jetIdSelection_string == "loose")
25  else if (jetIdSelection_string == "medium")
27  else if (jetIdSelection_string == "tight")
29  else
30  throw cms::Exception("NoPileUpPFMEtDataProducer")
31  << "Invalid Configuration Parameter 'jetIdSelection' = " << jetIdSelection_string << " !!\n";
32  jetEnOffsetCorrLabel_ = cfg.getParameter<std::string>("jetEnOffsetCorrLabel");
33 
34  srcPFCandidates_ = consumes<reco::PFCandidateCollection>(cfg.getParameter<edm::InputTag>("srcPFCandidates"));
35  srcPFCandidatesView_ = consumes<edm::View<reco::PFCandidate> >(cfg.getParameter<edm::InputTag>("srcPFCandidates"));
37  consumes<PFCandToVertexAssMap>(cfg.getParameter<edm::InputTag>("srcPFCandToVertexAssociations"));
38  srcJetsForMEtCov_ = mayConsume<reco::PFJetCollection>(cfg.getParameter<edm::InputTag>("srcJetsForMEtCov"));
39  minJetPtForMEtCov_ = cfg.getParameter<double>("minJetPtForMEtCov");
40  srcHardScatterVertex_ = consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>("srcHardScatterVertex"));
41  dZcut_ = cfg.getParameter<double>("dZcut");
42 
43  edm::ParameterSet cfgPFJetIdAlgo;
44  cfgPFJetIdAlgo.addParameter<std::string>("version", "FIRSTDATA");
45  cfgPFJetIdAlgo.addParameter<std::string>("quality", "LOOSE");
46  looseJetIdAlgo_ = new PFJetIDSelectionFunctor(cfgPFJetIdAlgo);
47 
49 
50  maxWarnings_ = (cfg.exists("maxWarnings")) ? cfg.getParameter<int>("maxWarnings") : 1;
51  numWarnings_ = 0;
52 
53  verbosity_ = (cfg.exists("verbosity")) ? cfg.getParameter<int>("verbosity") : 0;
54 
55  produces<reco::PUSubMETCandInfoCollection>("jetInfos");
56  produces<reco::PUSubMETCandInfoCollection>("pfCandInfos");
57 }
58 
60  delete looseJetIdAlgo_;
61  delete pfMEtSignInterface_;
62 }
63 
64 namespace {
65  void setPFCandidateFlag(const reco::PFJet& pfJet,
67  std::vector<int>& flags,
68  int value,
69  int& numWarnings,
70  int maxWarnings,
71  std::vector<const reco::PFJet*>* pfCandidateToJetAssociations = nullptr) {
72  edm::ProductID viewProductID;
73  if (!pfCandidateCollection.empty()) {
74  viewProductID = pfCandidateCollection.ptrAt(0).id();
75  }
76 
77  std::vector<reco::PFCandidatePtr> pfConsts = pfJet.getPFConstituents();
78  for (std::vector<reco::PFCandidatePtr>::const_iterator pfJetConstituent = pfConsts.begin();
79  pfJetConstituent != pfConsts.end();
80  ++pfJetConstituent) {
81  std::vector<int> idxs;
82  if (!pfCandidateCollection.empty() && pfJetConstituent->id() == viewProductID) {
83  idxs.push_back(pfJetConstituent->key());
84  } else {
85  bool isMatched_fast = false;
86  if (pfJetConstituent->key() < pfCandidateCollection.size()) {
87  edm::Ptr<reco::PFCandidate> pfCandidatePtr = pfCandidateCollection.ptrAt(pfJetConstituent->key());
88  double dR2 = deltaR2((*pfJetConstituent)->p4(), pfCandidatePtr->p4());
89  if (dR2 < dR2Min) {
90  idxs.push_back(pfCandidatePtr.key());
91  isMatched_fast = true;
92  }
93  }
94 
95  if (!isMatched_fast) {
96  size_t numPFCandidates = pfCandidateCollection.size();
97  for (size_t iPFCandidate = 0; iPFCandidate < numPFCandidates; ++iPFCandidate) {
98  edm::Ptr<reco::PFCandidate> pfCandidatePtr = pfCandidateCollection.ptrAt(iPFCandidate);
99  double dR2 = deltaR2((*pfJetConstituent)->p4(), pfCandidatePtr->p4());
100  if (dR2 < dR2Min) {
101  idxs.push_back(pfCandidatePtr.key());
102  }
103  }
104  if (numWarnings < maxWarnings) {
105  edm::LogWarning("setPFCandidateFlag")
106  << " The productIDs of PFJetConstituent and PFCandidateCollection passed as function arguments don't "
107  "match.\n"
108  << "NOTE: The return value will be unaffected, but the code will run MUCH slower !!";
109  ++numWarnings;
110  }
111  }
112  }
113  if (!idxs.empty()) {
114  for (std::vector<int>::const_iterator idx = idxs.begin(); idx != idxs.end(); ++idx) {
115  if ((*idx) >= (int)flags.size())
116  flags.resize(2 * flags.size());
117  flags[*idx] |= value;
118  if (pfCandidateToJetAssociations != nullptr)
119  (*pfCandidateToJetAssociations)[*idx] = &pfJet;
120  }
121  } else {
122  edm::LogError("setPFCandidateFlag")
123  << " Failed to associated PFJetConstituent with index = " << pfJetConstituent->key()
124  << " to any PFCandidate !!";
125  }
126  }
127  }
128 } // namespace
129 
131  LogDebug("produce") << "<NoPileUpPFMEtDataProducer::produce>:\n"
132  << " moduleLabel = " << moduleLabel_ << std::endl;
133 
134  // get jets
136  evt.getByToken(srcJets_, jets);
137 
138  typedef edm::ValueMap<int> jetIdMap;
139  edm::Handle<jetIdMap> jetIds;
140  evt.getByToken(srcJetIds_, jetIds);
141 
142  // get jets for computing contributions to PFMEt significance matrix
145  evt.getByToken(srcJetsForMEtCov_, jetsForMEtCov);
146 
147  // get PFCandidates
149  evt.getByToken(srcPFCandidatesView_, pfCandidates);
150 
151  std::vector<int> pfCandidateFlags(pfCandidates->size());
152  std::vector<const reco::PFJet*> pfCandidateToJetAssociations(pfCandidates->size());
153 
155  evt.getByToken(srcPFCandidates_, pfCandidateHandle);
156 
157  // get PFCandidate-to-vertex associations and "the" hard-scatter vertex
158  edm::Handle<PFCandToVertexAssMap> pfCandToVertexAssociations;
159  evt.getByToken(srcPFCandToVertexAssociations_, pfCandToVertexAssociations);
160 
161  noPuUtils::reversedPFCandToVertexAssMap pfCandToVertexAssociations_reversed =
162  noPuUtils::reversePFCandToVertexAssociation(*pfCandToVertexAssociations);
163 
164  edm::Handle<reco::VertexCollection> hardScatterVertex;
165  evt.getByToken(srcHardScatterVertex_, hardScatterVertex);
166 
167  auto jetInfos = std::make_unique<reco::PUSubMETCandInfoCollection>();
168  auto pfCandInfos = std::make_unique<reco::PUSubMETCandInfoCollection>();
169 
170  const JetCorrector* jetEnOffsetCorrector = nullptr;
171  if (!jetEnOffsetCorrLabel_.empty()) {
172  jetEnOffsetCorrector = JetCorrector::getJetCorrector(jetEnOffsetCorrLabel_, es);
173  if (!jetEnOffsetCorrector)
174  throw cms::Exception("NoPileUpPFMEtDataProducer::produce")
175  << "Failed to access Jet corrections for = " << jetEnOffsetCorrLabel_ << " !!\n";
176  }
177 
178  size_t numJets = jets->size();
179  for (size_t iJet = 0; iJet < numJets; ++iJet) {
180  reco::PFJetRef jet(jets, iJet);
181  if (!(jet->pt() > minJetPt_))
182  continue;
183 
184  bool passesLooseJetId = (*looseJetIdAlgo_)(*jet);
185  if (!passesLooseJetId) {
186  setPFCandidateFlag(*jet, *pfCandidates, pfCandidateFlags, flag_isWithinFakeJet, numWarnings_, maxWarnings_);
187  }
188  setPFCandidateFlag(*jet, *pfCandidates, pfCandidateFlags, flag_isWithinSelectedJet, numWarnings_, maxWarnings_);
189 
190  reco::PUSubMETCandInfo jetInfo;
191  jetInfo.setP4(jet->p4());
192  int jetId = (*jetIds)[jet];
193  bool jetIdSelection_passed = PileupJetIdentifier::passJetId(jetId, jetIdSelection_);
194  jetInfo.setType((jetIdSelection_passed) ? reco::PUSubMETCandInfo::kHS : reco::PUSubMETCandInfo::kPU);
195  jetInfo.setPassesLooseJetId(passesLooseJetId);
196  double jetEnergy_uncorrected = jet->chargedHadronEnergy() + jet->neutralHadronEnergy() + jet->photonEnergy() +
197  jet->electronEnergy() + jet->muonEnergy() + jet->HFHadronEnergy() +
198  jet->HFEMEnergy();
199  double jetPx_uncorrected = cos(jet->phi()) * sin(jet->theta()) * jetEnergy_uncorrected;
200  double jetPy_uncorrected = sin(jet->phi()) * sin(jet->theta()) * jetEnergy_uncorrected;
201  double jetPz_uncorrected = cos(jet->theta()) * jetEnergy_uncorrected;
203  jetPx_uncorrected, jetPy_uncorrected, jetPz_uncorrected, jetEnergy_uncorrected);
204  reco::PFJet rawJet(*jet);
205  rawJet.setP4(rawJetP4);
206  double jetNeutralEnFrac = (jetEnergy_uncorrected > 0.)
207  ? (jet->neutralEmEnergy() + jet->neutralHadronEnergy()) / jetEnergy_uncorrected
208  : -1.;
209  jetInfo.setChargedEnFrac((1 - jetNeutralEnFrac));
210  jetInfo.setOffsetEnCorr(
211  (jetEnOffsetCorrector) ? rawJet.energy() * (1. - jetEnOffsetCorrector->correction(rawJet, evt, es)) : 0.);
213 
214  jetInfos->push_back(jetInfo);
215  }
216  LogDebug("produce") << "#jetInfos = " << jetInfos->size() << std::endl;
217 
218  for (reco::PFJetCollection::const_iterator jet = jets->begin(); jet != jets->end(); ++jet) {
219  if (jet->pt() > minJetPtForMEtCov_) {
220  setPFCandidateFlag(*jet,
221  *pfCandidates,
222  pfCandidateFlags,
224  numWarnings_,
225  maxWarnings_,
226  &pfCandidateToJetAssociations);
227  }
228  }
229 
230  size_t numPFCandidates = pfCandidates->size();
231  for (size_t iPFCandidate = 0; iPFCandidate < numPFCandidates; ++iPFCandidate) {
232  reco::PFCandidatePtr pfCandidatePtr = pfCandidates->ptrAt(iPFCandidate);
233 
234  int idx = pfCandidatePtr.key();
235  reco::PUSubMETCandInfo pfCandInfo;
236  pfCandInfo.setP4(pfCandidatePtr->p4());
237  pfCandInfo.setCharge(pfCandidatePtr->charge());
238  pfCandInfo.setType(-1);
239  // CV: need to call isVertexAssociated_fast instead of isVertexAssociated function
240  // (makes run-time of MVAPFMEtDataProducer::produce decrease from ~1s per event to ~0.35s per event)
241  //int vtxAssociationType = isVertexAssociated(*pfCandidatePtr, *pfCandToVertexAssociations, *hardScatterVertex, dZcut_);
242  reco::PFCandidateRef pfCandidateRef(pfCandidateHandle, iPFCandidate);
243  int vtxAssociationType = noPuUtils::isVertexAssociated_fast(
244  pfCandidateRef, pfCandToVertexAssociations_reversed, *hardScatterVertex, dZcut_, numWarnings_, maxWarnings_);
245  bool isHardScatterVertex_associated = (vtxAssociationType == noPuUtils::kChHSAssoc);
246  if (pfCandidatePtr->charge() == 0)
248  else if (isHardScatterVertex_associated)
250  else
252  pfCandInfo.setIsWithinJet((pfCandidateFlags[idx] & flag_isWithinSelectedJet));
253  if (pfCandInfo.isWithinJet())
254  pfCandInfo.setPassesLooseJetId((pfCandidateFlags[idx] & flag_isWithinFakeJet));
255  else
256  pfCandInfo.setPassesLooseJetId(true);
257 
258  // CV: for PFCandidates that are within PFJets (of Pt between 'minJetPtForMEtCov' and 'minJetPt'),
259  // take contribution to PFMEt significance matrix from associated PFJet.
260  // (energy uncertainty scaled by ratio of PFCandidate/PFJet energy)
261  const reco::PFJet* jet_matched = pfCandidateToJetAssociations[idx];
262  if (jet_matched) {
263  metsig::SigInputObj pfCandResolution = pfMEtSignInterface_->compResolution(pfCandidatePtr.get());
264  metsig::SigInputObj jetResolution = pfMEtSignInterface_->compResolution(jet_matched);
265 
266  metsig::SigInputObj metSign;
267  metSign.set(pfCandResolution.get_type(),
268  pfCandResolution.get_energy(),
269  pfCandResolution.get_phi(),
270  jetResolution.get_sigma_e() * (pfCandidatePtr->energy() / jet_matched->energy()),
271  jetResolution.get_sigma_tan());
272  pfCandInfo.setMEtSignObj(metSign);
273  } else {
274  pfCandInfo.setMEtSignObj(pfMEtSignInterface_->compResolution(pfCandidatePtr.get()));
275  }
276 
277  pfCandInfos->push_back(pfCandInfo);
278  }
279 
280  LogDebug("produce") << "#pfCandInfos = " << pfCandInfos->size() << std::endl;
281 
282  evt.put(std::move(jetInfos), "jetInfos");
283  evt.put(std::move(pfCandInfos), "pfCandInfos");
284 }
285 
287 
double get_sigma_tan() const
Definition: SigInputObj.h:40
edm::EDGetTokenT< PFCandToVertexAssMap > srcPFCandToVertexAssociations_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
tuple cfg
Definition: looper.py:296
const int flag_isWithinSelectedJet
key_type key() const
Definition: Ptr.h:163
void produce(edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
Ptr< value_type > ptrAt(size_type i) const
double get_phi() const
Definition: SigInputObj.h:38
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:139
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:99
void setP4(const reco::Candidate::LorentzVector p4)
Definition: PUSubMETData.h:46
bool exists(std::string const &parameterName) const
checks if a parameter exists
size_type size() const
edm::EDGetTokenT< reco::PFJetCollection > srcJetsForMEtCov_
const int flag_isWithinFakeJet
noPuUtils::reversedPFCandToVertexAssMap reversePFCandToVertexAssociation(const PFCandToVertexAssMap &)
edm::EDGetTokenT< reco::VertexCollection > srcHardScatterVertex_
Log< level::Error, false > LogError
Jets made from PFObjects.
Definition: PFJet.h:20
void set(const std::string &m_type, const double &m_energy, const double &m_phi, const double &m_sigma_e, const double &m_sigma_tan)
Definition: SigInputObj.h:42
edm::EDGetTokenT< edm::View< reco::PFCandidate > > srcPFCandidatesView_
tuple pfCandidateCollection
static bool passJetId(int flag, Id level)
const int flag_isWithinJetForMEtCov
edm::EDGetTokenT< reco::PFCandidateCollection > srcPFCandidates_
bool isWithinJet() const
Definition: PUSubMETData.h:37
vector< PseudoJet > jets
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:135
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
def move
Definition: eostools.py:511
bool empty() const
void setIsWithinJet(bool isWJ)
Definition: PUSubMETData.h:53
edm::EDGetTokenT< reco::PFJetCollection > srcJets_
NoPileUpPFMEtDataProducer(const edm::ParameterSet &)
PFJetIDSelectionFunctor * looseJetIdAlgo_
PFMEtSignInterfaceBase * pfMEtSignInterface_
PF Jet selector for pat::Jets.
void setPassesLooseJetId(float jetId)
Definition: PUSubMETData.h:54
void setCharge(int charge)
Definition: PUSubMETData.h:50
edm::EDGetTokenT< edm::ValueMap< int > > srcJetIds_
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
void setChargedEnFrac(float chEnF)
Definition: PUSubMETData.h:57
const double dR2Min
double get_energy() const
Definition: SigInputObj.h:37
static const JetCorrector * getJetCorrector(const std::string &fName, const edm::EventSetup &fSetup)
retrieve corrector from the event setup. troughs exception if something is missing ...
Definition: JetCorrector.cc:48
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
void setType(int type)
Definition: PUSubMETData.h:49
int isVertexAssociated_fast(const reco::PFCandidateRef &, const noPuUtils::reversedPFCandToVertexAssMap &, const reco::VertexCollection &, double, int &, int)
virtual std::vector< reco::PFCandidatePtr > getPFConstituents() const
get all constituents
Definition: PFJet.cc:41
PileupJetIdentifier::Id jetIdSelection_
Log< level::Warning, false > LogWarning
std::string get_type() const
Definition: SigInputObj.h:36
metsig::SigInputObj compResolution(const T *particle) const
void setMEtSignObj(metsig::SigInputObj msig)
Definition: PUSubMETData.h:59
moduleLabel_(iConfig.getParameter< string >("@module_label"))
void setOffsetEnCorr(float offset)
Definition: PUSubMETData.h:55
void setP4(const LorentzVector &p4) final
set 4-momentum
double get_sigma_e() const
Definition: SigInputObj.h:39
#define LogDebug(id)
double energy() const final
energy