CMS 3D CMS Logo

MvaMEtUtilities.cc
Go to the documentation of this file.
2 
4 
5 #include <algorithm>
6 #include <cmath>
7 
9 {
10  // jet id
11  /* ===> this code parses an xml it uses parameter set
12  edm::ParameterSet jetConfig = iConfig.getParameter<edm::ParameterSet>("JetIdParams");
13  for(int i0 = 0; i0 < 3; i0++) {
14  std::string lCutType = "Tight";
15  if(i0 == PileupJetIdentifier::kMedium) lCutType = "Medium";
16  if(i0 == PileupJetIdentifier::kLoose) lCutType = "Loose";
17  std::vector<double> pt010 = jetConfig.getParameter<std::vector<double> >(("Pt010_" +lCutType).c_str());
18  std::vector<double> pt1020 = jetConfig.getParameter<std::vector<double> >(("Pt1020_"+lCutType).c_str());
19  std::vector<double> pt2030 = jetConfig.getParameter<std::vector<double> >(("Pt2030_"+lCutType).c_str());
20  std::vector<double> pt3050 = jetConfig.getParameter<std::vector<double> >(("Pt3050_"+lCutType).c_str());
21  for(int i1 = 0; i1 < 4; i1++) mvacut_[i0][0][i1] = pt010 [i1];
22  for(int i1 = 0; i1 < 4; i1++) mvacut_[i0][1][i1] = pt1020[i1];
23  for(int i1 = 0; i1 < 4; i1++) mvacut_[i0][2][i1] = pt2030[i1];
24  for(int i1 = 0; i1 < 4; i1++) mvacut_[i0][3][i1] = pt3050[i1];
25  }
26  */
27  //Tight Id => not used
28  mvaCut_[0][0][0] = 0.5; mvaCut_[0][0][1] = 0.6; mvaCut_[0][0][2] = 0.6; mvaCut_[0][0][3] = 0.9;
29  mvaCut_[0][1][0] = -0.2; mvaCut_[0][1][1] = 0.2; mvaCut_[0][1][2] = 0.2; mvaCut_[0][1][3] = 0.6;
30  mvaCut_[0][2][0] = 0.3; mvaCut_[0][2][1] = 0.4; mvaCut_[0][2][2] = 0.7; mvaCut_[0][2][3] = 0.8;
31  mvaCut_[0][3][0] = 0.5; mvaCut_[0][3][1] = 0.4; mvaCut_[0][3][2] = 0.8; mvaCut_[0][3][3] = 0.9;
32  //Medium id => not used
33  mvaCut_[1][0][0] = 0.2; mvaCut_[1][0][1] = 0.4; mvaCut_[1][0][2] = 0.2; mvaCut_[1][0][3] = 0.6;
34  mvaCut_[1][1][0] = -0.3; mvaCut_[1][1][1] = 0. ; mvaCut_[1][1][2] = 0. ; mvaCut_[1][1][3] = 0.5;
35  mvaCut_[1][2][0] = 0.2; mvaCut_[1][2][1] = 0.2; mvaCut_[1][2][2] = 0.5; mvaCut_[1][2][3] = 0.7;
36  mvaCut_[1][3][0] = 0.3; mvaCut_[1][3][1] = 0.2; mvaCut_[1][3][2] = 0.7; mvaCut_[1][3][3] = 0.8;
37  //Met Id => used
38  mvaCut_[2][0][0] = -0.2; mvaCut_[2][0][1] = -0.3; mvaCut_[2][0][2] = -0.5; mvaCut_[2][0][3] = -0.5;
39  mvaCut_[2][1][0] = -0.2; mvaCut_[2][1][1] = -0.2; mvaCut_[2][1][2] = -0.5; mvaCut_[2][1][3] = -0.3;
40  mvaCut_[2][2][0] = -0.2; mvaCut_[2][2][1] = -0.2; mvaCut_[2][2][2] = -0.2; mvaCut_[2][2][3] = 0.1;
41  mvaCut_[2][3][0] = -0.2; mvaCut_[2][3][1] = -0.2; mvaCut_[2][3][2] = 0. ; mvaCut_[2][3][3] = 0.2;
42 
43  dzCut_ = cfg.getParameter<double>("dZcut");
44  ptThreshold_ = ( cfg.exists("ptThreshold") ) ?
45  cfg.getParameter<int>("ptThreshold") : -1000;
46 }
47 
49 {
50 // nothing to be done yet...
51 }
52 
54 {
55  int ptBin = 0;
56  if ( jetP4.pt() >= 10. && jetP4.pt() < 20. ) ptBin = 1;
57  if ( jetP4.pt() >= 20. && jetP4.pt() < 30. ) ptBin = 2;
58  if ( jetP4.pt() >= 30. ) ptBin = 3;
59 
60  int etaBin = 0;
61  if ( std::abs(jetP4.eta()) >= 2.5 && std::abs(jetP4.eta()) < 2.75) etaBin = 1;
62  if ( std::abs(jetP4.eta()) >= 2.75 && std::abs(jetP4.eta()) < 3.0 ) etaBin = 2;
63  if ( std::abs(jetP4.eta()) >= 3.0 && std::abs(jetP4.eta()) < 5.0 ) etaBin = 3;
64 
65  return ( mvaJetId > mvaCut_[2][ptBin][etaBin] );
66 }
67 
68 reco::Candidate::LorentzVector MvaMEtUtilities::leadJetP4(const std::vector<reco::PUSubMETCandInfo>& jets)
69 {
70  return jetP4(jets, 0);
71 }
72 
73 reco::Candidate::LorentzVector MvaMEtUtilities::subleadJetP4(const std::vector<reco::PUSubMETCandInfo>& jets)
74 {
75  return jetP4(jets, 1);
76 }
77 
78 reco::Candidate::LorentzVector MvaMEtUtilities::jetP4(const std::vector<reco::PUSubMETCandInfo>& jets, unsigned idx)
79 {
80  reco::Candidate::LorentzVector retVal(0.,0.,0.,0.);
81  if ( idx < jets.size() ) {
82  std::vector<reco::PUSubMETCandInfo> jets_sorted = jets;
83  std::sort(jets_sorted.rbegin(), jets_sorted.rend());
84  retVal = jets_sorted[idx].p4();
85  }
86  return retVal;
87 }
88 unsigned MvaMEtUtilities::numJetsAboveThreshold(const std::vector<reco::PUSubMETCandInfo>& jets, double ptThreshold)
89 {
90  unsigned retVal = 0;
91  for ( std::vector<reco::PUSubMETCandInfo>::const_iterator jet = jets.begin();
92  jet != jets.end(); ++jet ) {
93  if ( jet->p4().pt() > ptThreshold ) ++retVal;
94  }
95  return retVal;
96 }
97 std::vector<reco::PUSubMETCandInfo> MvaMEtUtilities::cleanJets(const std::vector<reco::PUSubMETCandInfo>& jets,
98  const std::vector<reco::PUSubMETCandInfo>& leptons,
99  double ptThreshold, double dRmatch)
100 {
101 
102  double dR2match = dRmatch*dRmatch;
103  std::vector<reco::PUSubMETCandInfo> retVal;
104  for ( std::vector<reco::PUSubMETCandInfo>::const_iterator jet = jets.begin();
105  jet != jets.end(); ++jet ) {
106  bool isOverlap = false;
107  for ( std::vector<reco::PUSubMETCandInfo>::const_iterator lepton = leptons.begin();
108  lepton != leptons.end(); ++lepton ) {
109  if ( deltaR2(jet->p4(), lepton->p4()) < dR2match ) isOverlap = true;
110  }
111  if ( jet->p4().pt() > ptThreshold && !isOverlap ) retVal.push_back(*jet);
112  }
113  return retVal;
114 }
115 
116 std::vector<reco::PUSubMETCandInfo> MvaMEtUtilities::cleanPFCands(const std::vector<reco::PUSubMETCandInfo>& pfCandidates,
117  const std::vector<reco::PUSubMETCandInfo>& leptons,
118  double dRmatch, bool invert)
119 {
120 
121  double dR2match = dRmatch*dRmatch;
122  std::vector<reco::PUSubMETCandInfo> retVal;
123  for ( std::vector<reco::PUSubMETCandInfo>::const_iterator pfCandidate = pfCandidates.begin();
124  pfCandidate != pfCandidates.end(); ++pfCandidate ) {
125  bool isOverlap = false;
126  for ( std::vector<reco::PUSubMETCandInfo>::const_iterator lepton = leptons.begin();
127  lepton != leptons.end(); ++lepton ) {
128  if ( deltaR2(pfCandidate->p4(), lepton->p4()) < dR2match ) isOverlap = true;
129  }
130  if ( (!isOverlap && !invert) || (isOverlap && invert) ) retVal.push_back(*pfCandidate);
131  }
132  return retVal;
133 }
135 {
136  metData.met = sqrt(metData.mex*metData.mex + metData.mey*metData.mey);
137  metData.mez = 0.;
138  metData.phi = atan2(metData.mey, metData.mex);
139 }
140 
142 MvaMEtUtilities::computeCandSum( int compKey, double dZmax, int dZflag,
143  bool iCharged, bool mvaPassFlag,
144  const std::vector<reco::PUSubMETCandInfo>& objects ) {
145 
146  CommonMETData retVal;
147  retVal.mex = 0.;
148  retVal.mey = 0.;
149  retVal.sumet = 0.;
150 
151  for ( std::vector<reco::PUSubMETCandInfo>::const_iterator object = objects.begin();
152  object != objects.end(); ++object ) {
153 
154  double pFrac = 1;
155 
156  //pf candidates
157  // dZcut
158  // maximum distance within which tracks are
159  //considered to be associated to hard scatter vertex
160  // dZflag
161  // 0 : select charged PFCandidates originating from hard scatter vertex
162  // 1 : select charged PFCandidates originating from pile-up vertices
163  // 2 : select all PFCandidates
164  if( compKey==MvaMEtUtilities::kPFCands ) {
165  if ( object->dZ() < 0. && dZflag != 2 ) continue;
166  if ( object->dZ() > dZmax && dZflag == 0 ) continue;
167  if ( object->dZ() < dZmax && dZflag == 1 ) continue;
168  }
169 
170  //leptons
171  if( compKey==MvaMEtUtilities::kLeptons) {
172  if(iCharged) pFrac = object->chargedEnFrac();
173  }
174 
175  //jets
176  if( compKey==MvaMEtUtilities::kJets) {
177  bool passesMVAjetId = passesMVA(object->p4(), object->mva() );
178 
179  if ( passesMVAjetId && !mvaPassFlag ) continue;
180  if ( !passesMVAjetId && mvaPassFlag ) continue;
181 
182  pFrac = 1-object->chargedEnFrac();//neutral energy fraction
183  }
184 
185  retVal.mex += object->p4().px()*pFrac;
186  retVal.mey += object->p4().py()*pFrac;
187  retVal.sumet += object->p4().pt()*pFrac;
188  }
189 
190  finalize(retVal);
191  return retVal;
192 }
193 
194 
197 
198  CommonMETData retVal;
199 
200  if(metType == MvaMEtUtilities::kPF ) {
201  //MET = pfMET = - all candidates
202  // MET (1) in JME-13-003
203  retVal.mex = leptonsSum_.mex - pfCandSum_.mex;
204  retVal.mey = leptonsSum_.mey - pfCandSum_.mey;
206  }
207  if(metType == MvaMEtUtilities::kChHS ) {
208  //MET = - charged HS
209  // MET (2) in JME-13-003
213  }
214  if(metType == MvaMEtUtilities::kHS ) {
215  //MET = - charged HS - neutral HS in jets
216  // MET (3) in JME-13-003
220  }
221  if(metType == MvaMEtUtilities::kPU ) {
222  //MET = - charged PU - neutral PU in jets
223  //MET = -recoil in that particular case, - sign not useful for the MVA and then discarded
224  //motivated as PU IS its own recoil
225  // MET (4) in JME-13-003
226  retVal.mex = -(pfCandChPUSum_.mex + neutralJetPUSum_.mex);
227  retVal.mey = -(pfCandChPUSum_.mey + neutralJetPUSum_.mey);
229  }
230  if(metType == MvaMEtUtilities::kHSMinusNeutralPU ) {
231  //MET = all candidates - charged PU - neutral PU in jets
232  // = all charged HS + all neutrals - neutral PU in jets
233  // MET (5) in JME-13-003
237  }
238 
239  finalize(retVal);
240  return retVal;
241 }
242 
243 void
244 MvaMEtUtilities::computeAllSums(const std::vector<reco::PUSubMETCandInfo>& jets,
245  const std::vector<reco::PUSubMETCandInfo>& leptons,
246  const std::vector<reco::PUSubMETCandInfo>& pfCandidates ) {
247 
248  cleanedJets_ = cleanJets(jets, leptons, ptThreshold_, 0.5);
249 
250  leptonsSum_ = computeCandSum( kLeptons, 0., 0, false , false, leptons );
251  leptonsChSum_ = computeCandSum( kLeptons, 0., 0, true , false, leptons);
252  pfCandSum_ = computeCandSum( kPFCands, dzCut_, 2, false , false, pfCandidates);
253  pfCandChHSSum_ = computeCandSum( kPFCands, dzCut_, 0, false , false, pfCandidates);
254  pfCandChPUSum_ = computeCandSum( kPFCands, dzCut_, 1, false , false, pfCandidates);
255  neutralJetHSSum_ = computeCandSum( kJets, 0., 0, false , true, jets );
256  neutralJetPUSum_ = computeCandSum( kJets, 0., 0, false , false, jets );
257 
258 }
259 
260 double
262  return leptonsSum_.mex;
263 }
264 
265 double
267  return leptonsSum_.mey;
268 }
269 
270 double
272  return leptonsChSum_.mex;
273 }
274 
275 double
277  return leptonsChSum_.mey;
278 }
279 
280 
281 const std::vector<reco::PUSubMETCandInfo>&
283  return cleanedJets_;
284 }
T getParameter(std::string const &) const
virtual ~MvaMEtUtilities()
CommonMETData leptonsChSum_
const std::vector< reco::PUSubMETCandInfo > & getCleanedJets() const
double getLeptonsChSumMEX() const
double getLeptonsChSumMEY() const
void finalize(CommonMETData &metData)
unsigned numJetsAboveThreshold(const std::vector< reco::PUSubMETCandInfo > &, double)
std::vector< reco::PUSubMETCandInfo > cleanedJets_
CommonMETData neutralJetHSSum_
bool passesMVA(const reco::Candidate::LorentzVector &, double)
CommonMETData leptonsSum_
bool exists(std::string const &parameterName) const
checks if a parameter exists
CommonMETData neutralJetPUSum_
CommonMETData pfCandChPUSum_
reco::Candidate::LorentzVector subleadJetP4(const std::vector< reco::PUSubMETCandInfo > &)
double mvaCut_[3][4][4]
double getLeptonsSumMEX() const
T sqrt(T t)
Definition: SSEVec.h:18
vector< PseudoJet > jets
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Structure containing data common to all types of MET.
Definition: CommonMETData.h:12
void computeAllSums(const std::vector< reco::PUSubMETCandInfo > &jets, const std::vector< reco::PUSubMETCandInfo > &leptons, const std::vector< reco::PUSubMETCandInfo > &pfCandidates)
MvaMEtUtilities(const edm::ParameterSet &cfg)
reco::Candidate::LorentzVector leadJetP4(const std::vector< reco::PUSubMETCandInfo > &)
std::vector< reco::PUSubMETCandInfo > cleanPFCands(const std::vector< reco::PUSubMETCandInfo > &, const std::vector< reco::PUSubMETCandInfo > &, double, bool)
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
double getLeptonsSumMEY() const
CommonMETData computeRecoil(int metType)
CommonMETData computeCandSum(int compKey, double dZmax, int dZflag, bool iCharged, bool mvaPassFlag, const std::vector< reco::PUSubMETCandInfo > &objects)
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
std::vector< reco::PUSubMETCandInfo > cleanJets(const std::vector< reco::PUSubMETCandInfo > &, const std::vector< reco::PUSubMETCandInfo > &, double, double)
reco::Candidate::LorentzVector jetP4(const std::vector< reco::PUSubMETCandInfo > &, unsigned)
CommonMETData pfCandSum_
CommonMETData pfCandChHSSum_