CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SoftElectronMVAEstimator.cc
Go to the documentation of this file.
7 #include "TMVA/Reader.h"
8 #include "TMVA/MethodBDT.h"
9 
11  std::vector<std::string> weightsfiles;
12  std::string path_mvaWeightFileEleID;
13  for(unsigned ifile=0 ; ifile < cfg_.vweightsfiles.size() ; ++ifile) {
14  path_mvaWeightFileEleID = edm::FileInPath ( cfg_.vweightsfiles[ifile].c_str() ).fullPath();
15  weightsfiles.push_back(path_mvaWeightFileEleID);
16  }
17 
18  //initialize
19  //Define expected number of bins
20 
21  //Check number of weight files given
22  if (ExpectedNBins != cfg_.vweightsfiles.size() ) {
23  std::cout << "Error: Expected Number of bins = " << ExpectedNBins << " does not equal to weightsfiles.size() = "
24  << cfg_.vweightsfiles.size() << std::endl;
25 
27  }
28 
29 
30  for (unsigned int i=0;i<ExpectedNBins; ++i) {
31  TMVA::Reader tmvaReader("!Color:Silent");
32  tmvaReader.AddVariable("fbrem", &fbrem);
33  tmvaReader.AddVariable("EtotOvePin", &EtotOvePin);
34  tmvaReader.AddVariable("EClusOverPout", &eleEoPout);
35  tmvaReader.AddVariable("EBremOverDeltaP", &EBremOverDeltaP);
36  tmvaReader.AddVariable("logSigmaEtaEta", &logSigmaEtaEta);
37  tmvaReader.AddVariable("DeltaEtaTrackEcalSeed", &DeltaEtaTrackEcalSeed);
38  tmvaReader.AddVariable("HoE", &HoE);
39  tmvaReader.AddVariable("gsfchi2", &gsfchi2);
40  tmvaReader.AddVariable("kfchi2", &kfchi2);
41  tmvaReader.AddVariable("kfhits", &kfhits);
42  tmvaReader.AddVariable("SigmaPtOverPt", &SigmaPtOverPt);
43  tmvaReader.AddVariable("deta", &deta);
44  tmvaReader.AddVariable("dphi", &dphi);
45  tmvaReader.AddVariable("detacalo", &detacalo);
46  tmvaReader.AddVariable("see", &see);
47  tmvaReader.AddVariable("spp", &spp);
48  tmvaReader.AddVariable("R9", &R9);
49  tmvaReader.AddVariable("etawidth", &etawidth);
50  tmvaReader.AddVariable("phiwidth", &phiwidth);
51  tmvaReader.AddVariable("e1x5e5x5", &OneMinusE1x5E5x5);
52  tmvaReader.AddVariable("IoEmIoP", &IoEmIoP);
53  tmvaReader.AddVariable("PreShowerOverRaw", &PreShowerOverRaw);
54  tmvaReader.AddVariable("nPV", &nPV);
55 
56  tmvaReader.AddVariable( "pt", &pt);
57  tmvaReader.AddVariable( "eta", &eta);
58 
59  tmvaReader.AddSpectator( "pt", &pt);
60  tmvaReader.AddSpectator( "eta", &eta);
61 
62  // Taken from Daniele (his mail from the 30/11)
63  // training of the 7/12 with Nvtx added
64  std::unique_ptr<TMVA::IMethod> temp( tmvaReader.BookMVA("BDT",weightsfiles[i]) );
65  gbr[i].reset( new GBRForest( dynamic_cast<TMVA::MethodBDT*>( tmvaReader.FindMVA("BDT") ) ) );
66  }
67 }
68 
69 
71 { }
72 
73 
74 UInt_t SoftElectronMVAEstimator::GetMVABin(int pu, double eta, double pt) const {
75 
76  //Default is to return the first bin
77  unsigned int bin = 0;
78 
79  bool ptrange[3],etarange[3],purange[2];
80  ptrange[0]=pt > 2 && pt < 5;
81  ptrange[1]=pt > 5 && pt < 10;
82  ptrange[2]=pt > 10;
83  etarange[0]=fabs(eta) < 0.8;
84  etarange[1]=fabs(eta) > 0.8 && fabs(eta) <1.4;
85  etarange[2]=fabs(eta) > 1.4;
86  purange[0]=nPV<=20;
87  purange[1]=nPV>20;
88 
89  int index=0;
90  for(int kPU=0;kPU<2;kPU++)
91  for(int kETA=0;kETA<3;kETA++)
92  for(int kPT=0;kPT<3;kPT++){
93  if (purange[kPU] && ptrange[kPT] && etarange[kETA]) bin=index;
94  index++;
95  }
96  return bin;
97 }
98 
99 
100 
102  const reco::VertexCollection& pvc) const {
103  float vars[25];
104 
105  vars[0] = myElectron.fbrem();// fbrem
106  vars[1] = myElectron.eSuperClusterOverP(); //EtotOvePin
107  vars[2] = myElectron.eEleClusterOverPout(); //eleEoPout
108 
109  float etot = myElectron.eSuperClusterOverP()*myElectron.trackMomentumAtVtx().R();
110  float eEcal = myElectron.eEleClusterOverPout()*myElectron.trackMomentumAtEleClus().R();
111  float dP = myElectron.trackMomentumAtVtx().R()-myElectron.trackMomentumAtEleClus().R();
112  vars[3] = (etot-eEcal)/dP; //EBremOverDeltaP
113  vars[4] = std::log(myElectron.sigmaEtaEta()); //logSigmaEtaEta
114  vars[5] = myElectron.deltaEtaEleClusterTrackAtCalo(); //DeltaEtaTrackEcalSeed
115  vars[6] = myElectron.hcalOverEcalBc(); //HoE
116 
117  bool validKF= false;
118  reco::TrackRef myTrackRef = myElectron.closestCtfTrackRef();
119  validKF = (myTrackRef.isAvailable() && myTrackRef.isNonnull());
120  vars[7] = myElectron.gsfTrack()->normalizedChi2(); //gsfchi2
121  vars[8] = (validKF) ? myTrackRef->normalizedChi2() : 0 ; //kfchi2
122  vars[9] = (validKF) ? myTrackRef->hitPattern().trackerLayersWithMeasurement() : -1. ; //kfhits
123 
124  vars[10] = myElectron.gsfTrack().get()->ptModeError()/myElectron.gsfTrack().get()->ptMode() ; //SigmaPtOverPt
125  vars[11] = myElectron.deltaEtaSuperClusterTrackAtVtx(); //deta
126  vars[12] = myElectron.deltaPhiSuperClusterTrackAtVtx(); //dphi
127  vars[13] = myElectron.deltaEtaSeedClusterTrackAtCalo(); //detacalo
128  vars[14] = myElectron.sigmaIetaIeta(); //see
129  vars[15] = myElectron.sigmaIphiIphi(); //spp
130  vars[16] = myElectron.r9(); //R9
131  vars[17] = myElectron.superCluster()->etaWidth(); //etawidth
132  vars[18] = myElectron.superCluster()->phiWidth(); //phiwidth
133  vars[19] = (myElectron.e5x5()) !=0. ? 1.-(myElectron.e1x5()/myElectron.e5x5()) : -1. ; //OneMinusE1x5E5x5
134  vars[20] = (1.0/myElectron.ecalEnergy()) - (1.0 / myElectron.p()); // IoEmIoP
135  vars[21] = myElectron.superCluster()->preshowerEnergy() / myElectron.superCluster()->rawEnergy(); //PreShowerOverRaw
136  vars[22] = pvc.size(); // nPV
137  vars[23] = myElectron.pt(); //pt
138  vars[24] = myElectron.eta(); //eta
139 
140 /*
141  std::cout<<"fbrem "<<fbrem<<std::endl;
142  std::cout<<"EtotOvePin "<<EtotOvePin<<std::endl;
143  std::cout<<"eleEoPout "<<eleEoPout<<std::endl;
144  std::cout<<"EBremOverDeltaP "<<EBremOverDeltaP<<std::endl;
145  std::cout<<"logSigmaEtaEta "<<logSigmaEtaEta<<std::endl;
146  std::cout<<"DeltaEtaTrackEcalSeed "<<DeltaEtaTrackEcalSeed<<std::endl;
147  std::cout<<"HoE "<<HoE<<std::endl;
148  std::cout<<"kfchi2 "<<kfchi2<<std::endl;
149  std::cout<<"kfhits "<<kfhits<<std::endl;
150  std::cout<<"gsfchi2 "<<gsfchi2<<std::endl;
151  std::cout<<"SigmaPtOverPt "<<SigmaPtOverPt<<std::endl;
152  std::cout<<"deta "<<deta<<std::endl;
153  std::cout<<"dphi "<<dphi<<std::endl;
154  std::cout<<"detacalo "<<detacalo<<std::endl;
155  std::cout<<"see "<<see<<std::endl;
156  std::cout<< "spp " << spp<< std::endl;
157  std::cout<< "R9 " << R9<< std::endl;
158  std::cout<< "IoEmIoP " << IoEmIoP<< std::endl;
159  std::cout<<"etawidth "<<etawidth<<std::endl;
160  std::cout<<"phiwidth "<<phiwidth<<std::endl;
161  std::cout<<"OneMinusE1x5E5x5 "<<OneMinusE1x5E5x5<<std::endl;
162  std::cout<<"PreShowerOverRaw "<<PreShowerOverRaw<<std::endl;
163 */
164  bindVariables(vars);
165 
166  double result= gbr[0]->GetClassifier(vars);
167 
168  return result;
169 }
170 
171 
172 void SoftElectronMVAEstimator::bindVariables(float vars[25]) const {
173  if( vars[0] < -1.) //fbrem
174  vars[0] = -1.;
175 
176  vars[11] = std::abs(vars[11]); // deta
177  if(vars[11] > 0.06)
178  vars[11] = 0.06;
179 
180  vars[12] = std::abs(vars[12]);
181  if(vars[12] > 0.6)
182  vars[12] = 0.6;
183 
184  //if(EoP > 20.)
185  // EoP = 20.;
186 
187  if(vars[2] > 20.) //eleEoPout
188  vars[2] = 20.;
189 
190  vars[13] = std::abs(vars[13]); //detacalo
191  if(vars[13] > 0.2)
192  vars[13] = 0.2;
193 
194  if( vars[19] < -1.) //OneMinusE1x5E5x5
195  vars[19] = -1;
196 
197  if( vars[19] > 2.) //OneMinusE1x5E5x5
198  vars[19] = 2.;
199 
200  if( vars[7] > 200.) //gsfchi2
201  vars[7] = 200;
202 
203  if( vars[8] > 10.) //kfchi2
204  vars[8] = 10.;
205 
206 }
float sigmaIphiIphi() const
Definition: GsfElectron.h:417
bool isAvailable() const
Definition: Ref.h:576
int i
Definition: DBlmapReader.cc:9
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
tuple cfg
Definition: looper.py:293
float eSuperClusterOverP() const
Definition: GsfElectron.h:243
SoftElectronMVAEstimator(const Configuration &)
math::XYZVectorF trackMomentumAtVtx() const
Definition: GsfElectron.h:289
assert(m_qm.get())
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
float fbrem() const
Definition: GsfElectron.h:698
TrackRef closestCtfTrackRef() const
Definition: GsfElectron.h:199
tuple result
Definition: mps_fire.py:83
float deltaEtaSuperClusterTrackAtVtx() const
Definition: GsfElectron.h:247
float sigmaIetaIeta() const
Definition: GsfElectron.h:416
UInt_t GetMVABin(int pu, double eta, double pt) const
virtual SuperClusterRef superCluster() const
reference to a SuperCluster
Definition: GsfElectron.h:182
float deltaPhiSuperClusterTrackAtVtx() const
Definition: GsfElectron.h:250
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float eEleClusterOverPout() const
Definition: GsfElectron.h:246
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:244
float hcalOverEcalBc() const
Definition: GsfElectron.h:428
double mva(const reco::GsfElectron &myElectron, const reco::VertexCollection &) const
float deltaEtaEleClusterTrackAtCalo() const
Definition: GsfElectron.h:249
float e1x5() const
Definition: GsfElectron.h:418
void bindVariables(float vars[25]) const
float ecalEnergy() const
Definition: GsfElectron.h:785
math::XYZVectorF trackMomentumAtEleClus() const
Definition: GsfElectron.h:292
virtual double p() const final
magnitude of momentum vector
std::array< std::unique_ptr< const GBRForest >, ExpectedNBins > gbr
float e5x5() const
Definition: GsfElectron.h:420
float r9() const
Definition: GsfElectron.h:421
float deltaEtaSeedClusterTrackAtCalo() const
Definition: GsfElectron.h:248
tuple cout
Definition: gather_cfg.py:145
virtual double eta() const final
momentum pseudorapidity
float sigmaEtaEta() const
Definition: GsfElectron.h:415
virtual GsfTrackRef gsfTrack() const
reference to a GsfTrack
Definition: GsfElectron.h:183
virtual double pt() const final
transverse momentum