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=std::cos(-1);
19 
21 {
22  if(input=="sumet") return "particleFlow_SumET";
23  if(input=="npv") return "nPV";
24  if(input=="pfu") return "particleFlow_U";
25  if(input=="pfuphi") return "particleFlow_UPhi";
26  if(input=="tksumet") return "track_SumET";
27  if(input=="tku") return "track_U";
28  if(input=="tkuphi") return "track_UPhi";
29  if(input=="nopusumet") return "noPileUp_SumET";
30  if(input=="nopuu") return "noPileUp_U";
31  if(input=="nopuuphi") return "noPileUp_UPhi";
32  if(input=="pusumet") return "pileUp_SumET";
33  if(input=="pumet") return "pileUp_MET";
34  if(input=="pumetphi") return "pileUp_METPhi";
35  if(input=="pucsumet") return "pileUpCorrected_SumET";
36  if(input=="pucu") return "pileUpCorrected_U";
37  if(input=="pucuphi") return "pileUpCorrected_UPhi";
38  if(input=="jetpt1") return "jet1_pT";
39  if(input=="jeteta1") return "jet1_eta";
40  if(input=="jetphi1") return "jet1_Phi";
41  if(input=="jetpt2") return "jet2_pT";
42  if(input=="jeteta2") return "jet2_eta";
43  if(input=="jetphi2") return "jet2_Phi";
44  if(input=="nalljet") return "nJets";
45  if(input=="njet") return "numJetsPtGt30";
46  if(input=="uphi_mva") return "PhiCor_UPhi";
47  if(input=="uphix_mva") return "PhiCor_UPhi";
48  if(input=="ux_mva") return "RecoilCor_U";
49  return input;
50 }
51 
53 {
54  if ( inputFileName.location()==edm::FileInPath::Unknown ) throw cms::Exception("PFMETAlgorithmMVA::loadMVA")
55  << " Failed to find File = " << inputFileName << " !!\n";
56  std::unique_ptr<TFile> inputFile(new TFile(inputFileName.fullPath().data()) );
57 
58  std::vector<std::string> *lVec = (std::vector<std::string>*)inputFile->Get("varlist");
59 
60  if(lVec==nullptr) {
61  throw cms::Exception("PFMETAlgorithmMVA::loadMVA")
62  << " Failed to load mva file : " << inputFileName.fullPath().data() << " is not a proper file !!\n";
63  }
64 
65  std::vector<std::string> variableNames;
66  for(unsigned int i=0; i< lVec->size();++i)
67  {
68  variableNames.push_back(updateVariableNames(lVec->at(i)));
69  }
70 
71  if(mvaName.find(mvaNameU_) != std::string::npos) varForU_ = variableNames;
72  else if(mvaName.find(mvaNameDPhi_) != std::string::npos) varForDPhi_ = variableNames;
73  else if(mvaName.find(mvaNameCovU1_) != std::string::npos) varForCovU1_ = variableNames;
74  else if(mvaName.find(mvaNameCovU2_) != std::string::npos) varForCovU2_ = variableNames;
75  else throw cms::Exception("PFMETAlgorithmMVA::loadMVA") << "MVA MET weight file tree names do not match specified inputs" << std::endl;
76 
77 
78  const GBRForest* mva = (GBRForest*)inputFile->Get(mvaName.data());
79  if ( !mva )
80  throw cms::Exception("PFMETAlgorithmMVA::loadMVA")
81  << " Failed to load MVA = " << mvaName.data() << " from file = " << inputFileName.fullPath().data() << " !!\n";
82 
83  return mva;
84 }
85 
87 {
89  es.get<GBRWrapperRcd>().get(mvaName, mva);
90  return mva.product();
91 }
92 
94  : utils_(cfg),
95  mvaReaderU_(nullptr),
96  mvaReaderDPhi_(nullptr),
97  mvaReaderCovU1_(nullptr),
98  mvaReaderCovU2_(nullptr),
99  cfg_(cfg)
100 {
102 
103  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
104 
105 }
106 
108 {
109  if ( !loadMVAfromDB_ ) {
110  delete mvaReaderU_;
111  delete mvaReaderDPhi_;
112  delete mvaReaderCovU1_;
113  delete mvaReaderCovU2_;
114  }
115 }
116 
117 //-------------------------------------------------------------------------------
119 {
120  edm::ParameterSet cfgInputRecords = cfg_.getParameter<edm::ParameterSet>("inputRecords");
121  mvaNameU_ = cfgInputRecords.getParameter<std::string>("U");
122  mvaNameDPhi_ = cfgInputRecords.getParameter<std::string>("DPhi");
123  mvaNameCovU1_ = cfgInputRecords.getParameter<std::string>("CovU1");
124  mvaNameCovU2_ = cfgInputRecords.getParameter<std::string>("CovU2");
125 
126  if ( loadMVAfromDB_ ) {
127  mvaReaderU_ = loadMVAfromDB(es, mvaNameU_);
128  mvaReaderDPhi_ = loadMVAfromDB(es, mvaNameDPhi_);
129  mvaReaderCovU1_ = loadMVAfromDB(es, mvaNameCovU1_);
130  mvaReaderCovU2_ = loadMVAfromDB(es, mvaNameCovU2_);
131  } else {
132  edm::ParameterSet cfgInputFileNames = cfg_.getParameter<edm::ParameterSet>("inputFileNames");
133 
134  edm::FileInPath inputFileNameU = cfgInputFileNames.getParameter<edm::FileInPath>("U");
135  mvaReaderU_ = loadMVAfromFile(inputFileNameU, mvaNameU_);
136  edm::FileInPath inputFileNameDPhi = cfgInputFileNames.getParameter<edm::FileInPath>("DPhi");
137  mvaReaderDPhi_ = loadMVAfromFile(inputFileNameDPhi, mvaNameDPhi_);
138  edm::FileInPath inputFileNameCovU1 = cfgInputFileNames.getParameter<edm::FileInPath>("CovU1");
139  mvaReaderCovU1_ = loadMVAfromFile(inputFileNameCovU1, mvaNameCovU1_);
140  edm::FileInPath inputFileNameCovU2 = cfgInputFileNames.getParameter<edm::FileInPath>("CovU2");
141  mvaReaderCovU2_ = loadMVAfromFile(inputFileNameCovU2, mvaNameCovU2_);
142  }
143 }
144 
145 //-------------------------------------------------------------------------------
146 void PFMETAlgorithmMVA::setInput(const std::vector<reco::PUSubMETCandInfo>& leptons,
147  const std::vector<reco::PUSubMETCandInfo>& jets,
148  const std::vector<reco::PUSubMETCandInfo>& pfCandidates,
149  const std::vector<reco::Vertex::Point>& vertices)
150 {
151 
152 
153  utils_.computeAllSums( jets, leptons, pfCandidates);
154 
157 
160 
161  const std::vector<reco::PUSubMETCandInfo> jets_cleaned = utils_.getCleanedJets();
162 
168 
169  reco::Candidate::LorentzVector jet1P4 = utils_.leadJetP4(jets_cleaned);
170  reco::Candidate::LorentzVector jet2P4 = utils_.subleadJetP4(jets_cleaned);
171 
172 
173  var_["particleFlow_U"] = pfRecoil_data.met;
174  var_["particleFlow_SumET"] = pfRecoil_data.sumet;
175  var_["particleFlow_UPhi"] = pfRecoil_data.phi;
176 
177  var_["track_SumET"] = chHSRecoil_data.sumet/var_["particleFlow_SumET"];
178  var_["track_U"] = chHSRecoil_data.met;
179  var_["track_UPhi"] = chHSRecoil_data.phi;
180 
181  var_["noPileUp_SumET"] = hsRecoil_data.sumet/var_["particleFlow_SumET"];
182  var_["noPileUp_U"] = hsRecoil_data.met;
183  var_["noPileUp_UPhi"] = hsRecoil_data.phi;
184 
185  var_["pileUp_SumET"] = puRecoil_data.sumet/var_["particleFlow_SumET"];
186  var_["pileUp_MET"] = puRecoil_data.met;
187  var_["pileUp_METPhi"] = puRecoil_data.phi;
188 
189  var_["pileUpCorrected_SumET"] = hsMinusNeutralPUMEt_data.sumet/var_["particleFlow_SumET"];
190  var_["pileUpCorrected_U"] = hsMinusNeutralPUMEt_data.met;
191  var_["pileUpCorrected_UPhi"] = hsMinusNeutralPUMEt_data.phi;
192 
193  var_["jet1_pT"] = jet1P4.pt();
194  var_["jet1_eta"] = jet1P4.eta();
195  var_["jet1_Phi"] = jet1P4.phi();
196  var_["jet2_pT"] = jet2P4.pt();
197  var_["jet2_eta"] = jet2P4.eta();
198  var_["jet2_Phi"] = jet2P4.phi();
199 
200  var_["numJetsPtGt30"] = utils_.numJetsAboveThreshold(jets_cleaned, 30.);
201  var_["nJets"] = jets_cleaned.size();
202  var_["nPV"] = vertices.size();
203 }
204 
205 //-------------------------------------------------------------------------------
206 std::unique_ptr<float[]> PFMETAlgorithmMVA::createFloatVector(std::vector<std::string> variableNames)
207 {
208  std::unique_ptr<float[]> floatVector(new float[variableNames.size()]);
209  int i = 0;
210  for(auto variableName: variableNames)
211  {
212  floatVector[i++] = var_[variableName];
213  }
214  return floatVector;
215 }
216 
217 //-------------------------------------------------------------------------------
219 {
220  // CV: MVAs needs to be evaluated in order { DPhi, U1, CovU1, CovU2 }
221  // as MVA for U1 (CovU1, CovU2) uses output of DPhi (DPhi and U1) MVA
223  var_["PhiCor_UPhi"] = var_["particleFlow_UPhi"] + mvaOutputDPhi_;
225  var_["RecoilCor_U"] = var_["particleFlow_U"] * mvaOutputU_;
226  var_["RecoilCor_UPhi"] = var_["PhiCor_UPhi"];
227  mvaOutputCovU1_ = GetResponse(mvaReaderCovU1_, varForCovU1_)* mvaOutputU_ * var_["particleFlow_U"];
228  mvaOutputCovU2_ = GetResponse(mvaReaderCovU2_, varForCovU2_)* mvaOutputU_ * var_["particleFlow_U"];
229 
230 
231  // compute MET(Photon check)
232  if(hasPhotons_) {
233  //Fix events with unphysical properties
235  if(var_["track_U"]/sumLeptonPt < 0.1 || var_["noPileUp_U"]/sumLeptonPt < 0.1 ) {
236  mvaOutputU_ = 1.;
237  mvaOutputDPhi_ = 0.;
238  }
239  }
240  computeMET();
241 }
242 //-------------------------------------------------------------------------------
243 
245 {
246  double U = var_["RecoilCor_U"];
247  double Phi = var_["PhiCor_UPhi"];
248  if ( U < 0. ) Phi += Pi; //RF: No sign flip for U necessary in that case?
249  double cosPhi = std::cos(Phi);
250  double sinPhi = std::sin(Phi);
251  double metPx = U*cosPhi - sumLeptonPx_;
252  double metPy = U*sinPhi - sumLeptonPy_;
253  double metPt = sqrt(metPx*metPx + metPy*metPy);
254  mvaMEt_.SetCoordinates(metPx, metPy, 0., metPt);
255  // compute MET uncertainties in dirrections parallel and perpendicular to hadronic recoil
256  // (neglecting uncertainties on lepton momenta)
257  mvaMEtCov_(0, 0) = mvaOutputCovU1_*cosPhi*cosPhi + mvaOutputCovU2_*sinPhi*sinPhi;
258  mvaMEtCov_(0, 1) = -mvaOutputCovU1_*sinPhi*cosPhi + mvaOutputCovU2_*sinPhi*cosPhi;
259  mvaMEtCov_(1, 0) = mvaMEtCov_(0, 1);
260  mvaMEtCov_(1, 1) = mvaOutputCovU1_*sinPhi*sinPhi + mvaOutputCovU2_*cosPhi*cosPhi;
261 }
262 
263 //-------------------------------------------------------------------------------
264 const float PFMETAlgorithmMVA::GetResponse(const GBRForest * Reader, std::vector<std::string> &variableNames )
265 {
266  std::unique_ptr<float[]> mvaInputVector = createFloatVector(variableNames);
267  double result = Reader->GetResponse( mvaInputVector.get() );
268  return result;
269 }
270 
271 //-------------------------------------------------------------------------------
272 void PFMETAlgorithmMVA::print(std::ostream& stream) const
273 {
274  stream << "<PFMETAlgorithmMVA::print>:" << std::endl;
275  for(auto entry: var_)
276  stream << entry.first << " = " << entry.second << std::endl;
277  stream << " covU1 = " << mvaOutputCovU1_ << ", covU2 = " << mvaOutputCovU2_ << std::endl;
278  stream << " sum(leptons): Pt = " << sqrt(sumLeptonPx_*sumLeptonPx_ + sumLeptonPy_*sumLeptonPy_) << ","
279  << " phi = " << atan2(sumLeptonPy_, sumLeptonPx_) << " "
280  << "(Px = " << sumLeptonPx_ << ", Py = " << sumLeptonPy_ << ")" ;
281  stream << " MVA output: U = " << mvaOutputU_ << ", dPhi = " << mvaOutputDPhi_ << "," << " covU1 = " << mvaOutputCovU1_ << ", covU2 = " << mvaOutputCovU2_ << std::endl;
282  stream << std::endl;
283 }
284 
double GetResponse(const float *vector) const
Definition: GBRForest.h:59
const double Pi
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< std::string > varForCovU2_
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 * loadMVAfromFile(const edm::FileInPath &inputFileName, const std::string &mvaName)
std::vector< std::string > varForU_
const GBRForest * mvaReaderCovU1_
const float GetResponse(const GBRForest *Reader, std::vector< std::string > &variableNames)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
const GBRForest * mvaReaderCovU2_
const std::string updateVariableNames(std::string input)
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 > &)
static std::string const input
Definition: EdmProvDump.cc:43
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
std::unique_ptr< float[]> createFloatVector(std::vector< std::string > variableNames)
vector< PseudoJet > jets
tuple result
Definition: query.py:137
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)
const GBRForest * loadMVAfromDB(const edm::EventSetup &es, const std::string &mvaName)
void initialize(const edm::EventSetup &)
reco::METCovMatrix mvaMEtCov_
std::vector< std::string > varForCovU1_
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
std::vector< std::string > varForDPhi_
std::map< std::string, float > var_
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