CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFMETAlgorithmMVA.cc
Go to the documentation of this file.
2 
5 
9 
11 
12 #include <TFile.h>
13 
14 #include <iomanip>
15 
16 enum MVAType { kBaseline = 0 };
17 
18 const double Pi=cos(-1);
19 
20 namespace
21 {
22  const GBRForest* loadMVAfromFile(const edm::FileInPath& inputFileName, const std::string& mvaName)
23  {
24  if ( inputFileName.location()==edm::FileInPath::Unknown ) throw cms::Exception("PFMETAlgorithmMVA::loadMVA")
25  << " Failed to find File = " << inputFileName << " !!\n";
26  TFile* inputFile = new TFile(inputFileName.fullPath().data());
27 
28  //const GBRForest* mva = dynamic_cast<GBRForest*>(inputFile->Get(mvaName.data())); // CV: dynamic_cast<GBRForest*> fails for some reason ?!
29  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
30  if ( !mva )
31  throw cms::Exception("PFMETAlgorithmMVA::loadMVA")
32  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
33 
34  delete inputFile;
35 
36  return mva;
37  }
38 
39  const GBRForest* loadMVAfromDB(const edm::EventSetup& es, const std::string& mvaName)
40  {
42  es.get<GBRWrapperRcd>().get(mvaName, mva);
43  return mva.product();
44  }
45 }
46 
48  : utils_(cfg),
49  mvaInputU_(nullptr),
50  mvaInputDPhi_(nullptr),
51  mvaInputCovU1_(nullptr),
52  mvaInputCovU2_(nullptr),
53  mvaReaderU_(nullptr),
54  mvaReaderDPhi_(nullptr),
55  mvaReaderCovU1_(nullptr),
56  mvaReaderCovU2_(nullptr),
57  cfg_(cfg)
58 {
60 
61  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
62 
63  mvaInputU_ = new Float_t[25];
64  mvaInputDPhi_ = new Float_t[23];
65  mvaInputCovU1_ = new Float_t[26];
66  mvaInputCovU2_ = new Float_t[26];
67 }
68 
70 {
71  delete mvaInputU_;
72  delete mvaInputDPhi_;
73  delete mvaInputCovU1_;
74  delete mvaInputCovU2_;
75 
76  if ( !loadMVAfromDB_ ) {
77  delete mvaReaderU_;
78  delete mvaReaderDPhi_;
79  delete mvaReaderCovU1_;
80  delete mvaReaderCovU2_;
81  }
82 }
83 
85 {
86  if ( loadMVAfromDB_ ) {
87  edm::ParameterSet cfgInputRecords = cfg_.getParameter<edm::ParameterSet>("inputRecords");
88  mvaNameU_ = cfgInputRecords.getParameter<std::string>("U");
89  mvaReaderU_ = loadMVAfromDB(es, mvaNameU_);
90  mvaNameDPhi_ = cfgInputRecords.getParameter<std::string>("DPhi");
92  mvaNameCovU1_ = cfgInputRecords.getParameter<std::string>("CovU1");
94  mvaNameCovU2_ = cfgInputRecords.getParameter<std::string>("CovU2");
96  } else {
97  edm::ParameterSet cfgInputFileNames = cfg_.getParameter<edm::ParameterSet>("inputFileNames");
98 
99  mvaNameU_ = "U1Correction";
100  mvaNameDPhi_ = "PhiCorrection";
101  mvaNameCovU1_ = "CovU1";
102  mvaNameCovU2_ = "CovU2";
103 
104  edm::FileInPath inputFileNameU = cfgInputFileNames.getParameter<edm::FileInPath>("U");
105  mvaReaderU_ = loadMVAfromFile(inputFileNameU, mvaNameU_);
106  edm::FileInPath inputFileNameDPhi = cfgInputFileNames.getParameter<edm::FileInPath>("DPhi");
107  mvaReaderDPhi_ = loadMVAfromFile(inputFileNameDPhi, mvaNameDPhi_);
108  edm::FileInPath inputFileNameCovU1 = cfgInputFileNames.getParameter<edm::FileInPath>("CovU1");
109  mvaReaderCovU1_ = loadMVAfromFile(inputFileNameCovU1, mvaNameCovU1_);
110  edm::FileInPath inputFileNameCovU2 = cfgInputFileNames.getParameter<edm::FileInPath>("CovU2");
111  mvaReaderCovU2_ = loadMVAfromFile(inputFileNameCovU2, mvaNameCovU2_);
112  }
113 }
114 
115 //-------------------------------------------------------------------------------
116 void PFMETAlgorithmMVA::setInput(const std::vector<reco::PUSubMETCandInfo>& leptons,
117  const std::vector<reco::PUSubMETCandInfo>& jets,
118  const std::vector<reco::PUSubMETCandInfo>& pfCandidates,
119  const std::vector<reco::Vertex::Point>& vertices)
120 {
121 
122 
123  utils_.computeAllSums( jets, leptons, pfCandidates);
124 
127 
130 
131  const std::vector<reco::PUSubMETCandInfo> jets_cleaned = utils_.getCleanedJets();
132 
138 
139  reco::Candidate::LorentzVector jet1P4 = utils_.leadJetP4(jets_cleaned);
140  reco::Candidate::LorentzVector jet2P4 = utils_.subleadJetP4(jets_cleaned);
141 
142  double pfSumEt = pfRecoil_data.sumet;
143  double pfU = pfRecoil_data.met;
144  double pfPhi = pfRecoil_data.phi;
145  double tkSumEt = chHSRecoil_data.sumet;
146  double tkU = chHSRecoil_data.met;
147  double tkPhi = chHSRecoil_data.phi;
148  double npuSumEt = hsRecoil_data.sumet;
149  double npuU = hsRecoil_data.met;
150  double npuPhi = hsRecoil_data.phi;
151  double puSumEt = puRecoil_data.sumet;
152  double puMEt = puRecoil_data.met;
153  double puPhi = puRecoil_data.phi;
154  double pucSumEt = hsMinusNeutralPUMEt_data.sumet;
155  double pucU = hsMinusNeutralPUMEt_data.met;
156  double pucPhi = hsMinusNeutralPUMEt_data.phi;
157  double jet1Pt = jet1P4.pt();
158  double jet1Eta = jet1P4.eta();
159  double jet1Phi = jet1P4.phi();
160  double jet2Pt = jet2P4.pt();
161  double jet2Eta = jet2P4.eta();
162  double jet2Phi = jet2P4.phi();
163  double numJetsPtGt30 = utils_.numJetsAboveThreshold(jets_cleaned, 30.);
164  double numJets = jets_cleaned.size();
165  double numVertices = vertices.size();
166 
167  setInput(pfSumEt, pfU, pfPhi,
168  tkSumEt, tkU, tkPhi,
169  npuSumEt, npuU, npuPhi,
170  puSumEt, puMEt, puPhi,
171  pucSumEt, pucU, pucPhi,
172  jet1Pt, jet1Eta, jet1Phi,
173  jet2Pt, jet2Eta, jet2Phi,
174  numJetsPtGt30, numJets,
175  numVertices);
176 
177 }
178 
179 void PFMETAlgorithmMVA::setInput(double pfSumEt, double pfU, double pfPhi,
180  double tkSumEt, double tkU, double tkPhi,
181  double npuSumEt, double npuU, double npuPhi,
182  double puSumEt, double puMEt, double puPhi,
183  double pucSumEt, double pucU, double pucPhi,
184  double jet1Pt, double jet1Eta, double jet1Phi,
185  double jet2Pt, double jet2Eta, double jet2Phi,
186  double numJetsPtGt30, double numJets,
187  double numVertices)
188 {
189  // protection against "empty events"
190  if ( pfSumEt < 1. ) pfSumEt = 1.;
191 
192  pfSumEt_ = pfSumEt;
193  pfU_ = pfU;
194  pfPhi_ = pfPhi;
195  tkSumEt_ = tkSumEt/pfSumEt_;
196  tkU_ = tkU;
197  tkPhi_ = tkPhi;
198  npuSumEt_ = npuSumEt/pfSumEt_;
199  npuU_ = npuU;
200  npuPhi_ = npuPhi;
201  puSumEt_ = puSumEt/pfSumEt_;
202  puMEt_ = puMEt;
203  puPhi_ = puPhi;
204  pucSumEt_ = pucSumEt/pfSumEt_;
205  pucU_ = pucU;
206  pucPhi_ = pucPhi;
207  jet1Pt_ = jet1Pt;
208  jet1Eta_ = jet1Eta;
209  jet1Phi_ = jet1Phi;
210  jet2Pt_ = jet2Pt;
211  jet2Eta_ = jet2Eta;
212  jet2Phi_ = jet2Phi;
213  numJetsPtGt30_ = numJetsPtGt30;
214  numJets_ = numJets;
215  numVertices_ = numVertices;
216 }
217 //-------------------------------------------------------------------------------
218 
219 //-------------------------------------------------------------------------------
221 {
222  // CV: MVAs needs to be evaluated in order { DPhi, U1, CovU1, CovU2 }
223  // as MVA for U1 (CovU1, CovU2) uses output of DPhi (DPhi and U1) MVA
224  evaluateDPhi();
225  evaluateU();
226  evaluateCovU1();
227  evaluateCovU2();
228 
229  // compute MET(Photon check)
230  if(hasPhotons_) {
231  //Fix events with unphysical properties
233  if(tkU_/sumLeptonPt < 0.1 || npuU_/sumLeptonPt < 0.1 ) mvaOutputU_ = 1.;
234  if(tkU_/sumLeptonPt < 0.1 || npuU_/sumLeptonPt < 0.1 ) mvaOutputDPhi_ = 0.;
235  }
236  double U = pfU_*mvaOutputU_;
237  double Phi = pfPhi_ + mvaOutputDPhi_;
238  if ( U < 0. ) Phi += Pi;
239  double cosPhi = cos(Phi);
240  double sinPhi = sin(Phi);
241  double metPx = U*cosPhi - sumLeptonPx_;
242  double metPy = U*sinPhi - sumLeptonPy_;
243  double metPt = sqrt(metPx*metPx + metPy*metPy);
244  mvaMEt_.SetCoordinates(metPx, metPy, 0., metPt);
245 
246  // compute MET uncertainties in dirrections parallel and perpendicular to hadronic recoil
247  // (neglecting uncertainties on lepton momenta)
248  mvaMEtCov_(0, 0) = mvaOutputCovU1_*cosPhi*cosPhi + mvaOutputCovU2_*sinPhi*sinPhi;
249  mvaMEtCov_(0, 1) = -mvaOutputCovU1_*sinPhi*cosPhi + mvaOutputCovU2_*sinPhi*cosPhi;
250  mvaMEtCov_(1, 0) = mvaMEtCov_(0, 1);
251  mvaMEtCov_(1, 1) = mvaOutputCovU1_*sinPhi*sinPhi + mvaOutputCovU2_*cosPhi*cosPhi;
252 }
253 //-------------------------------------------------------------------------------
254 
255 //-------------------------------------------------------------------------------
257 {
258  mvaInputU_[0] = pfSumEt_; // PH: helps flattens response vs. Nvtx
260  mvaInputU_[2] = pfU_;
261  mvaInputU_[3] = pfPhi_;
262  mvaInputU_[4] = tkSumEt_;
263  mvaInputU_[5] = tkU_;
264  mvaInputU_[6] = tkPhi_;
265  mvaInputU_[7] = npuSumEt_;
266  mvaInputU_[8] = npuU_;
267  mvaInputU_[9] = npuPhi_;
268  mvaInputU_[10] = puSumEt_;
269  mvaInputU_[11] = puMEt_;
270  mvaInputU_[12] = puPhi_;
271  mvaInputU_[13] = pucSumEt_;
272  mvaInputU_[14] = pucU_;
273  mvaInputU_[15] = pucPhi_;
274  mvaInputU_[16] = jet1Pt_;
275  mvaInputU_[17] = jet1Eta_;
276  mvaInputU_[18] = jet1Phi_;
277  mvaInputU_[19] = jet2Pt_;
278  mvaInputU_[20] = jet2Eta_;
279  mvaInputU_[21] = jet2Phi_;
280  mvaInputU_[22] = numJets_;
284 }
285 
287 {
289  mvaInputDPhi_[1] = pfU_;
290  mvaInputDPhi_[2] = pfPhi_;
291  mvaInputDPhi_[3] = tkSumEt_;
292  mvaInputDPhi_[4] = tkU_;
293  mvaInputDPhi_[5] = tkPhi_;
295  mvaInputDPhi_[7] = npuU_;
296  mvaInputDPhi_[8] = npuPhi_;
297  mvaInputDPhi_[9] = puSumEt_;
298  mvaInputDPhi_[10] = puMEt_;
299  mvaInputDPhi_[11] = puPhi_;
300  mvaInputDPhi_[12] = pucSumEt_;
301  mvaInputDPhi_[13] = pucU_;
302  mvaInputDPhi_[14] = pucPhi_;
303  mvaInputDPhi_[15] = jet1Pt_;
304  mvaInputDPhi_[16] = jet1Eta_;
305  mvaInputDPhi_[17] = jet1Phi_;
306  mvaInputDPhi_[18] = jet2Pt_;
307  mvaInputDPhi_[19] = jet2Eta_;
308  mvaInputDPhi_[20] = jet2Phi_;
309  mvaInputDPhi_[21] = numJets_;
312 }
313 
315 {
316  mvaInputCovU1_[0] = pfSumEt_; // PH: helps flattens response vs. Nvtx
318  mvaInputCovU1_[2] = pfU_;
319  mvaInputCovU1_[3] = pfPhi_;
321  mvaInputCovU1_[5] = tkU_;
322  mvaInputCovU1_[6] = tkPhi_;
324  mvaInputCovU1_[8] = npuU_;
325  mvaInputCovU1_[9] = npuPhi_;
326  mvaInputCovU1_[10] = puSumEt_;
327  mvaInputCovU1_[11] = puMEt_;
328  mvaInputCovU1_[12] = puPhi_;
330  mvaInputCovU1_[14] = pucU_;
331  mvaInputCovU1_[15] = pucPhi_;
332  mvaInputCovU1_[16] = jet1Pt_;
333  mvaInputCovU1_[17] = jet1Eta_;
334  mvaInputCovU1_[18] = jet1Phi_;
335  mvaInputCovU1_[19] = jet2Pt_;
336  mvaInputCovU1_[20] = jet2Eta_;
337  mvaInputCovU1_[21] = jet2Phi_;
338  mvaInputCovU1_[22] = numJets_;
343 }
344 
346 {
347  mvaInputCovU2_[0] = pfSumEt_; // PH: helps flattens response vs. Nvtx
349  mvaInputCovU2_[2] = pfU_;
350  mvaInputCovU2_[3] = pfPhi_;
352  mvaInputCovU2_[5] = tkU_;
353  mvaInputCovU2_[6] = tkPhi_;
355  mvaInputCovU2_[8] = npuU_;
356  mvaInputCovU2_[9] = npuPhi_;
357  mvaInputCovU2_[10] = puSumEt_;
358  mvaInputCovU2_[11] = puMEt_;
359  mvaInputCovU2_[12] = puPhi_;
361  mvaInputCovU2_[14] = pucU_;
362  mvaInputCovU2_[15] = pucPhi_;
363  mvaInputCovU2_[16] = jet1Pt_;
364  mvaInputCovU2_[17] = jet1Eta_;
365  mvaInputCovU2_[18] = jet1Phi_;
366  mvaInputCovU2_[19] = jet2Pt_;
367  mvaInputCovU2_[20] = jet2Eta_;
368  mvaInputCovU2_[21] = jet2Phi_;
369  mvaInputCovU2_[22] = numJets_;
374 }
375 void PFMETAlgorithmMVA::print(std::ostream& stream) const
376 {
377  stream << "<PFMETAlgorithmMVA::print>:" << std::endl;
378  stream << " PF: sumEt = " << pfSumEt_ << ", U = " << pfU_ << ", phi = " << pfPhi_ << std::endl;
379  stream << " TK: sumEt = " << tkSumEt_ << ", U = " << tkU_ << ", phi = " << tkPhi_ << std::endl;
380  stream << " NPU: sumEt = " << npuSumEt_ << ", U = " << npuU_ << ", phi = " << npuPhi_ << std::endl;
381  stream << " PU: sumEt = " << puSumEt_ << ", MEt = " << puMEt_ << ", phi = " << puPhi_ << std::endl;
382  stream << " PUC: sumEt = " << pucSumEt_ << ", U = " << pucU_ << ", phi = " << pucPhi_ << std::endl;
383  stream << " jet1: Pt = " << jet1Pt_ << ", eta = " << jet1Eta_ << ", phi = " << jet1Phi_ << std::endl;
384  stream << " jet2: Pt = " << jet2Pt_ << ", eta = " << jet2Eta_ << ", phi = " << jet2Phi_ << std::endl;
385  stream << " num. jets = " << numJets_ << " (" << numJetsPtGt30_ << " with Pt > 30 GeV)" << std::endl;
386  stream << " num. vertices = " << numVertices_ << std::endl;
387  stream << " MVA output: U = " << mvaOutputU_ << ", dPhi = " << mvaOutputDPhi_ << ","
388  << " covU1 = " << mvaOutputCovU1_ << ", covU2 = " << mvaOutputCovU2_ << std::endl;
389  stream << " sum(leptons): Pt = " << sqrt(sumLeptonPx_*sumLeptonPx_ + sumLeptonPy_*sumLeptonPy_) << ","
390  << " phi = " << atan2(sumLeptonPy_, sumLeptonPx_) << " "
391  << "(Px = " << sumLeptonPx_ << ", Py = " << sumLeptonPy_ << ")" << std::endl;
392 }
393 
double GetResponse(const float *vector) const
Definition: GBRForest.h:55
const double Pi
T getParameter(std::string const &) const
const std::vector< reco::PUSubMETCandInfo > & getCleanedJets() const
double getLeptonsChSumMEX() const
tuple cfg
Definition: looper.py:259
double getLeptonsChSumMEY() const
unsigned numJetsAboveThreshold(const std::vector< reco::PUSubMETCandInfo > &, double)
MvaMEtUtilities utils_
void print(std::ostream &) const
const GBRForest * mvaReaderCovU1_
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
const GBRForest * mvaReaderCovU2_
edm::ParameterSet cfg_
reco::Candidate::LorentzVector subleadJetP4(const std::vector< reco::PUSubMETCandInfo > &)
#define nullptr
std::string mvaNameCovU1_
void setInput(const std::vector< reco::PUSubMETCandInfo > &, const std::vector< reco::PUSubMETCandInfo > &, const std::vector< reco::PUSubMETCandInfo > &, const std::vector< reco::Vertex::Point > &)
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
const GBRForest * mvaReaderDPhi_
const GBRForest * mvaReaderU_
PFMETAlgorithmMVA(const edm::ParameterSet &cfg)
double getLeptonsSumMEX() const
T sqrt(T t)
Definition: SSEVec.h:48
vector< PseudoJet > jets
Cos< T >::type cos(const T &t)
Definition: Cos.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)
void initialize(const edm::EventSetup &)
reco::METCovMatrix mvaMEtCov_
reco::Candidate::LorentzVector leadJetP4(const std::vector< reco::PUSubMETCandInfo > &)
double getLeptonsSumMEY() const
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
CommonMETData computeRecoil(int metType)
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:86
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
std::string mvaNameDPhi_
reco::Candidate::LorentzVector mvaMEt_
std::string mvaNameCovU2_
std::string fullPath() const
Definition: FileInPath.cc:165
tuple loadMVAfromDB
Definition: mvaPFMET_cff.py:80