test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pfjetMVAtools.cc
Go to the documentation of this file.
1 #include <vector>
2 #include "Math/VectorUtil.h"
4 
5 using namespace std;
6 
7 namespace HWWFunctions {
8 
9  bool sortByPFJetPt (const std::pair <LorentzVector, Int_t> &pfjet1, const std::pair<LorentzVector, Int_t> &pfjet2)
10  {
11  return pfjet1.first.pt() > pfjet2.first.pt();
12  }
13 
14  bool getGoodMVAs(HWW& hww, vector <float> &goodmvas, string variable)
15  {
16 
17  vector <float> mva_variable;
18  if( variable == "mvavalue" ){ mva_variable = hww.pfjets_mvavalue();
19  }
20  else{
21  edm::LogError("InvalidInput") <<"variable not found. Check input. Exiting.";
22  exit(99);
23  }
24 
25  //if no bug is detected, returns the original collection of the mvas stored in the cms2 ntuple.
26  if(hww.pfjets_p4().size() == mva_variable.size() ) {
27 
28  goodmvas = mva_variable;
29  return false;
30 
31  }else{
32 
33  vector <bool> isgoodindex;
34  vector <std::pair <LorentzVector, Int_t> > cjets;
35  double deta = 0.0;
36  double dphi = 0.0;
37  double dr = 0.0;
38 
39  if( hww.evt_isRealData() ){
40  for( size_t cjeti = 0; cjeti < hww.pfjets_p4().size(); cjeti++) { // corrected jets collection
41  LorentzVector corrjet = hww.pfjets_p4().at(cjeti);
42  pair <LorentzVector, Int_t> cjetpair = make_pair( corrjet, (Int_t)cjeti );
43  cjets.push_back(cjetpair);
44  }
45 
46  }else{
47  for( size_t cjeti = 0; cjeti < hww.pfjets_p4().size(); cjeti++) { // corrected jets collection
48  LorentzVector corrjet = hww.pfjets_p4().at(cjeti);
49  pair <LorentzVector, Int_t> cjetpair = make_pair( corrjet, (Int_t)cjeti );
50  cjets.push_back(cjetpair);
51  }
52  }
53 
54  sort(cjets.begin(), cjets.end(), sortByPFJetPt);
55 
56  for( size_t ucjeti = 0; ucjeti < hww.pfjets_p4().size(); ucjeti++) { // uncorrected jets collection
57  for( size_t cjeti = 0; cjeti < hww.pfjets_p4().size(); cjeti++) { // corrected jets collection
58 
59  //buggy method
60  if( hww.evt_isRealData() ){
61  if( abs( hww.pfjets_area().at(ucjeti) - hww.pfjets_area().at(cjets.at(cjeti).second)) > numeric_limits<float>::epsilon() ) continue;
62  if( fabs( hww.pfjets_p4().at(ucjeti).eta() - (hww.pfjets_p4().at(cjets.at(cjeti).second)).eta()) > 0.01 ) continue;
63  }else{
64  if( abs( hww.pfjets_area().at(ucjeti) - hww.pfjets_area().at(cjets.at(cjeti).second)) > numeric_limits<float>::epsilon() ) continue;
65  if( fabs( hww.pfjets_p4().at(ucjeti).eta() - (hww.pfjets_p4().at(cjets.at(cjeti).second)).eta()) > 0.01 ) continue;
66  }
67 
68  //fix
69  if( hww.evt_isRealData() ){
70  deta = hww.pfjets_p4().at(ucjeti).eta() - (hww.pfjets_p4().at(cjets.at(cjeti).second)).eta();
71  dphi = acos(cos(hww.pfjets_p4().at(ucjeti).phi() - (hww.pfjets_p4().at(cjets.at(cjeti).second)).phi()));
72  dr = sqrt(deta*deta + dphi*dphi);
73  }else{
74  deta = hww.pfjets_p4().at(ucjeti).eta() - (hww.pfjets_p4().at(cjets.at(cjeti).second)).eta();
75  dphi = acos(cos(hww.pfjets_p4().at(ucjeti).phi() - (hww.pfjets_p4().at(cjets.at(cjeti).second)).phi()));
76  dr = sqrt(deta*deta + dphi*dphi);
77  }
78 
79  if (dr > 0.01){
80  isgoodindex.push_back(false);
81  }else{
82  isgoodindex.push_back(true);
83  }
84  }
85  }
86 
87  if( isgoodindex.size() >= mva_variable.size() ){
88  for( size_t mvai = 0; mvai < mva_variable.size(); mvai++ ){
89  if( isgoodindex.at(mvai) ) goodmvas.push_back(mva_variable.at(mvai));
90  }
91  }
92 
93  //still possible that the fix picks up less events than the fix in cmssw
94  //This behavior was not seen by me, but just in case this line here will
95  // prevent the code from crashing and return the original mva collection.
96  if( goodmvas.size() == hww.pfjets_p4().size() ){
97  //fill the new mva values
98  return true;
99  }else{
100  //cout<<"new mva values vector size "<<goodmvas.size()<<" different to pfjets collection size "<<hww.pfjets_p4().size()<<endl;
101  //cout<<"returning old mva collection: "<<variable<<endl;
102  goodmvas.clear();
103  goodmvas = mva_variable;
104  return false;
105  }
106  }
107  }
108 
109 }
T sqrt(T t)
Definition: SSEVec.h:48
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Definition: HWW.h:11
std::vector< float > & pfjets_mvavalue()
Definition: HWW.cc:785
bool getGoodMVAs(HWW &, std::vector< float > &goodmvas, std::string variable)
int & evt_isRealData()
Definition: HWW.cc:619
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< float > > LorentzVector
Definition: pfjetMVAtools.h:10
bool sortByPFJetPt(const std::pair< LorentzVector, Int_t > &pfjet1, const std::pair< LorentzVector, Int_t > &pfjet2)
Definition: pfjetMVAtools.cc:9
std::vector< LorentzVector > & pfjets_p4()
Definition: HWW.cc:769
const double epsilon
std::vector< float > & pfjets_area()
Definition: HWW.cc:777