CMS 3D CMS Logo

METSignificance.cc
Go to the documentation of this file.
1 
2 // -*- C++ -*-
3 //
4 // Package: METAlgorithms
5 // Class: METSignificance
6 //
12 //
13 // Original Author: Nathan Mirman (Cornell University)
14 // Created: Thu May 30 16:39:52 CDT 2013
15 //
16 //
17 
19 #include <unordered_set>
20 
21 namespace {
22  struct ptr_hash {
23  std::size_t operator()(const reco::CandidatePtr& k) const
24  {
25  if(k.refCore().isTransient()) return (unsigned long)k.refCore().productPtr() ^ k.key();
26  else return k.refCore().id().processIndex() ^ k.refCore().id().productIndex() ^ k.key();
27  }
28  };
29 }
30 
32 
33  edm::ParameterSet cfgParams = iConfig.getParameter<edm::ParameterSet>("parameters");
34 
35  double dRmatch = cfgParams.getParameter<double>("dRMatch");
36  dR2match_ = dRmatch*dRmatch;
37 
38  jetThreshold_ = cfgParams.getParameter<double>("jetThreshold");
39  jetEtas_ = cfgParams.getParameter<std::vector<double> >("jeta");
40  jetParams_ = cfgParams.getParameter<std::vector<double> >("jpar");
41  pjetParams_ = cfgParams.getParameter<std::vector<double> >("pjpar");
42  useDeltaRforFootprint_ = cfgParams.getParameter<bool>("useDeltaRforFootprint");
43 
44 }
45 
47 
49  const std::vector<edm::Handle<reco::CandidateView> >& leptons,
50  const edm::Handle<edm::View<reco::Candidate> >& pfCandidatesH,
51  double rho,
52  JME::JetResolution& resPtObj,
53  JME::JetResolution& resPhiObj,
55  bool isRealData,
56  double& sumPtUnclustered) {
57  //pfcandidates
58  const edm::View<reco::Candidate>& pfCandidates = *pfCandidatesH;
59 
60  // metsig covariance
61  double cov_xx = 0;
62  double cov_xy = 0;
63  double cov_yy = 0;
64 
65  // for lepton and jet subtraction
66  std::unordered_set<reco::CandidatePtr,ptr_hash> footprint;
67 
68  // subtract leptons out of sumPtUnclustered
69  for ( const auto& lep_i : leptons ) {
70  for (const auto& lep : lep_i->ptrs()) {
71  if (lep->pt() > 10) {
72  for (unsigned int n = 0; n < lep->numberOfSourceCandidatePtrs(); n++)
73  footprint.insert(lep->sourceCandidatePtr(n));
74  }
75  }
76  }
77 
78  std::vector<bool> cleanedJets(jets.size(), false);
79  std::transform(jets.begin(), jets.end(), cleanedJets.begin(), [this, &leptons](auto const& jet) -> bool {
80  return cleanJet(jet, leptons);
81  });
82  // subtract jets out of sumPtUnclustered
83  auto iCleaned = cleanedJets.begin();
84  for (const auto& jet : jets) {
85  // disambiguate jets and leptons
86  if (!(*iCleaned++))
87  continue;
88  for (unsigned int n = 0; n < jet.numberOfSourceCandidatePtrs(); n++) {
89  footprint.insert(jet.sourceCandidatePtr(n));
90  }
91 
92  }
93 
94  // calculate sumPtUnclustered
95  for(size_t i = 0; i< pfCandidates.size(); ++i) {
96 
97  // check if candidate exists in a lepton or jet
98  bool cleancand = true;
99  if(footprint.find( pfCandidates.ptrAt(i) )==footprint.end()) {
100 
101  //dP4 recovery
102  for( const auto& it : footprint) {
103  // Special treatment for PUPPI with dR, since jet candidates (puppi) and MET candidates (puppiForMet)
104  // can't be matched through the sourceCandidatePtrs and may have different energy, but same direction.
105  if((it.isNonnull()) && (it.isAvailable()) &&
106  (((!useDeltaRforFootprint_) && ((it->p4()-pfCandidates[i].p4()).Et2()<0.000025)) ||
107  (( useDeltaRforFootprint_) && (reco::deltaR2(*it, pfCandidates[i])<0.00000025)))){
108  cleancand = false;
109  break;
110  }
111  }
112  // if not, add to sumPtUnclustered
113  if( cleancand ){
114  sumPtUnclustered += pfCandidates[i].pt();
115  }
116  }
117  }
118 
119  // add jets to metsig covariance matrix and subtract them from sumPtUnclustered
120  iCleaned = cleanedJets.begin();
121  for(const auto& jet : jets) {
122 
123  // disambiguate jets and leptons
124  if(!(*iCleaned++) ) continue;
125 
126  double jpt = jet.pt();
127  double jeta = jet.eta();
128  double feta = std::abs(jeta);
129  double c = jet.px()/jet.pt();
130  double s = jet.py()/jet.pt();
131 
133  parameters.setJetPt(jpt).setJetEta(jeta).setRho(rho);
134 
135  // jet energy resolutions
136  double sigmapt = resPtObj.getResolution(parameters);
137  double sigmaphi = resPhiObj.getResolution(parameters);
138  // SF not needed since is already embedded in the sigma in the dataGlobalTag
139  // double sigmaSF = isRealData ? resSFObj.getScaleFactor(parameters) : 1.0;
140 
141  // split into high-pt and low-pt sector
142  if( jpt > jetThreshold_ ){
143  // high-pt jets enter into the covariance matrix via JER
144 
145  double scale = 0;
146  if(feta<jetEtas_[0]) scale = jetParams_[0];
147  else if(feta<jetEtas_[1]) scale = jetParams_[1];
148  else if(feta<jetEtas_[2]) scale = jetParams_[2];
149  else if(feta<jetEtas_[3]) scale = jetParams_[3];
150  else scale = jetParams_[4];
151 
152  // double dpt = sigmaSF*scale*jpt*sigmapt;
153  double dpt = scale*jpt*sigmapt;
154  double dph = jpt*sigmaphi;
155 
156  cov_xx += dpt*dpt*c*c + dph*dph*s*s;
157  cov_xy += (dpt*dpt-dph*dph)*c*s;
158  cov_yy += dph*dph*c*c + dpt*dpt*s*s;
159 
160  } else {
161 
162  // add the (corrected) jet to the sumPtUnclustered
163  sumPtUnclustered += jpt;
164 
165  }
166 
167  }
168 
169  //protection against unphysical events
170  if(sumPtUnclustered<0) sumPtUnclustered=0;
171 
172  // add pseudo-jet to metsig covariance matrix
173  cov_xx += pjetParams_[0]*pjetParams_[0] + pjetParams_[1]*pjetParams_[1]*sumPtUnclustered;
174  cov_yy += pjetParams_[0]*pjetParams_[0] + pjetParams_[1]*pjetParams_[1]*sumPtUnclustered;
175 
176  reco::METCovMatrix cov;
177  cov(0,0) = cov_xx;
178  cov(1,0) = cov_xy;
179  cov(0,1) = cov_xy;
180  cov(1,1) = cov_yy;
181 
182  return cov;
183 }
184 
185 double
187 
188  // covariance matrix determinant
189  double det = cov(0,0)*cov(1,1) - cov(0,1)*cov(1,0);
190 
191  // invert matrix
192  double ncov_xx = cov(1,1) / det;
193  double ncov_xy = -cov(0,1) / det;
194  double ncov_yy = cov(0,0) / det;
195 
196  // product of met and inverse of covariance
197  double sig = met.px()*met.px()*ncov_xx + 2*met.px()*met.py()*ncov_xy + met.py()*met.py()*ncov_yy;
198 
199  return sig;
200 }
201 
202 bool
204  const std::vector< edm::Handle<reco::CandidateView> >& leptons ){
205 
206  for ( const auto& lep_i : leptons ) {
207  for( const auto& lep : *lep_i ) {
208  if ( reco::deltaR2(lep, jet) < dR2match_ ) {
209  return false;
210  }
211  }
212  }
213  return true;
214 }
T getParameter(std::string const &) const
bool cleanJet(const reco::Jet &jet, const std::vector< edm::Handle< reco::CandidateView > > &leptons)
float getResolution(const JetParameters &parameters) const
JetParameters & setJetEta(float eta)
static double getSignificance(const reco::METCovMatrix &cov, const reco::MET &met)
JetParameters & setRho(float rho)
key_type key() const
Definition: Ptr.h:185
Ptr< value_type > ptrAt(size_type i) const
Base class for all types of Jets.
Definition: Jet.h:20
double px() const final
x coordinate of momentum vector
ProcessIndex productIndex() const
Definition: ProductID.h:37
size_type size() const
ROOT::Math::SMatrix< double, 2 > METCovMatrix
Definition: MET.h:40
const_iterator begin() const
Definition: MET.h:42
vector< PseudoJet > jets
void const * productPtr() const
Definition: RefCore.h:52
RefCore const & refCore() const
Definition: Ptr.h:189
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
reco::METCovMatrix getCovariance(const edm::View< reco::Jet > &jets, const std::vector< edm::Handle< reco::CandidateView > > &leptons, const edm::Handle< edm::View< reco::Candidate > > &pfCandidates, double rho, JME::JetResolution &resPtObj, JME::JetResolution &resPhiObj, JME::JetResolutionScaleFactor &resSFObj, bool isRealData, double &sumPtUnclustered)
int k[5][pyjets_maxn]
METSignificance(const edm::ParameterSet &iConfig)
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
met
===> hadronic RAZOR
double py() const final
y coordinate of momentum vector
ProductID id() const
Definition: RefCore.h:49
bool isTransient() const
Definition: RefCore.h:106
sumPtUnclustered
Definition: met_cff.py:20
const_iterator end() const
JetParameters & setJetPt(float pt)
ProcessIndex processIndex() const
Definition: ProductID.h:36