CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TtSemiLepSignalSelMVATrainer.cc
Go to the documentation of this file.
1 #include "TMath.h"
2 #include <algorithm>
3 
6 
10 
17 
18 
20  muons_ (cfg.getParameter<edm::InputTag>("muons")),
21  electrons_ (cfg.getParameter<edm::InputTag>("elecs")),
22  jets_ (cfg.getParameter<edm::InputTag>("jets")),
23  METs_ (cfg.getParameter<edm::InputTag>("mets")),
24  lepChannel_(cfg.getParameter<int>("lepChannel")),
25  whatData_ (cfg.getParameter<int>("whatData")),
26  maxEv_ (cfg.getParameter<int>("maxEv"))
27 {
28 }
29 
31 {
32 }
33 
34 void
36 {
37  //communication with CMSSW CondDB
38  mvaComputer.update<TtSemiLepSignalSelMVARcd>("trainer", setup, "traintreeSaver");
39 
40  // can occur in the last iteration when the
41  // MVATrainer is about to save the result
42  if(!mvaComputer) return;
43 
44 
45  //make your preselection here!!
46  //the following code is for the default example
48  evt.getByLabel(METs_,MET_handle);
49  if(!MET_handle.isValid()) return;
50  const edm::View<pat::MET> MET = *MET_handle;
51 
53  evt.getByLabel(jets_, jet_handle);
54  if(!jet_handle.isValid()) return;
55  const std::vector<pat::Jet> jets = *jet_handle;
56  unsigned int nJets = 0;
57  std::vector<pat::Jet> seljets;
58  //std::cout<<"number of jets: "<<jets.size()<<std::endl;
59  for(std::vector<pat::Jet>::const_iterator it = jets.begin(); it != jets.end(); it++) {
60  //std::cout<<"Jet Et: "<<it->et()<<" Eta: "<<fabs(it->eta())<<std::endl;
62  if(it->et()>30. && fabs(it->eta())<2.4) {
63  seljets.push_back(*it);
64  nJets++;
65  }
66  }
67  //std::cout<<"selected Jets: "<<nJets<<std::endl;
68  if(nJets<4) return;
69 
70  //sort by Pt
71  sort(seljets.begin(),seljets.end(),JetwithHigherPt());
72 
73  edm::Handle< edm::View<pat::Muon> > muon_handle;
74  evt.getByLabel(muons_, muon_handle);
75  if(!muon_handle.isValid()) return;
76  const edm::View<pat::Muon> muons = *muon_handle;
77  int nmuons = 0;
78  std::vector<pat::Muon> selMuons;
79  for(edm::View<pat::Muon>::const_iterator it = muons.begin(); it!=muons.end(); it++) {
80  reco::TrackRef gltr = it->track(); // global track
81  reco::TrackRef trtr = it->innerTrack(); // tracker track
82  if(it->pt()>30 && fabs(it->eta())<2.1 && (it->pt()/(it->pt()+it->trackIso()+it->caloIso()))>0.95 && it->isGlobalMuon()){
83  if(gltr.isNull()) continue; //temporary problems with dead trackrefs
84  if((gltr->chi2()/gltr->ndof())<10 && trtr->numberOfValidHits()>=11) {
85 
86  double dRmin = 9999.;
87  for(std::vector<pat::Jet>::const_iterator ajet = seljets.begin(); ajet != seljets.end(); ajet++) {
88  math::XYZTLorentzVector jet = ajet->p4();
89  math::XYZTLorentzVector muon = it->p4();
90  double tmpdR = DeltaR(muon,jet);
91  if(tmpdR<dRmin) dRmin = tmpdR;
92  }
93  reco::TrackRef trtr = it->track(); // tracker track
94  if(dRmin>0.3) { //temporary problems with muon isolation
95  nmuons++;
96  selMuons.push_back(*it);
97  }
98  }
99  }
100  }
101  //std::cout<<"selected Muons: "<<nleptons<<std::endl;
102  if(nmuons!=1) return;
103 
104  edm::Handle< edm::View<pat::Electron> > electron_handle;
105  evt.getByLabel(electrons_, electron_handle);
106  if(!electron_handle.isValid()) return;
107  const edm::View<pat::Electron> electrons = *electron_handle;
108  int nelectrons = 0;
109  for(edm::View<pat::Electron>::const_iterator it = electrons.begin(); it!=electrons.end(); it++) {
110  if(it->pt()>30 && fabs(it->eta())<2.4 && (it->pt()/(it->pt()+it->trackIso()+it->caloIso()))>0.95 && it->isElectronIDAvailable("eidTight"))
111  {
112  if(it->electronID("eidTight")==1) nelectrons++;
113  }
114  }
115  if(nelectrons>0) return;
116  //end of the preselection
117 
118 
119  math::XYZTLorentzVector muon = selMuons.begin()->p4();
120 
121  //count the number of selected events
122  selEv++;
123  //skip event if enough events are already selected
124  if(selEv>maxEv_ && maxEv_!=-1) return;
125 
126  //calculation of InputVariables
127  //see TopQuarkAnalysis/TopTools/interface/TtSemiLepSignalSel.h
128  // /src/TtSemiLepSignalSel.cc
129  //all objects i.e. jets, muons, electrons... which are needed for the calculation
130  //of the input-variables have to be passed to this class
131 
132  TtSemiLepSignalSel selection(seljets,muon,MET);
133 
134  //this is only needed for the default example
136  evt.getByLabel("genEvt", genEvt);
137 
138  double weight = 1.0; //standard no weight, i.e. weight=1.0, set this to the corresponding weight if
139  //different weights for different events are available
140  if(whatData_==-1) { //your training-file contains both, signal and background events
141  bool isSignal;
142  isSignal = true;//true for signal, false for background this has to be derived in some way
143  evaluateTtSemiLepSignalSel(mvaComputer, selection, weight, true, isSignal);
144  }
145  else {
146 
147  if(whatData_==1){ //your tree contains only signal events
148  //if needed do a special signal selection here
149  //the following code is for the default example
150  if(genEvt->isSemiLeptonic() && genEvt->semiLeptonicChannel() == lepChannel_) {
151  //std::cout<<"a tt_semlep muon event"<<std::endl;
152  evaluateTtSemiLepSignalSel(mvaComputer, selection, weight, true, true);
153  }
154  else selEv--;
155  }
156  else if(whatData_==0){
157  //std::cout<<"a Wjets event"<<std::endl;
158  evaluateTtSemiLepSignalSel(mvaComputer, selection, weight, true, false);
159  }
160  else std::cout<<"Config File Error!! Please check <whatData> in TtSemiLepSignalSelMVATrainer.cfi";
161  }
162 }
163 
165  selEv = 0;
166  if(whatData_!=-1 && whatData_!=0 && whatData_!=1){
167  std::cout<<"Config File Error!! Please check <whatData> in TtSemiLepSignalSelMVATrainer.cfi"<<std::endl;;
168  return;
169  }
170 }
171 
173 {
174  double dPhi = fabs(v1.Phi() - v2.Phi());
175  if (dPhi > TMath::Pi()) dPhi = 2*TMath::Pi() - dPhi;
176  return dPhi;
177 }
178 
180 {
181  double dPhi = DeltaPhi(v1,v2);
182  double dR = TMath::Sqrt((v1.Eta()-v2.Eta())*(v1.Eta()-v2.Eta())+dPhi*dPhi);
183  return dR;
184 }
185 
186 // implement the plugins for the trainer
187 // -> defines TtSemiLepSignalSelMVAContainerSaveCondDB
188 // -> defines TtSemiLepSignalSelMVASaveFile
189 // -> defines TtSemiLepSignalSelMVATrainerLooper
190 MVA_TRAINER_IMPLEMENT(TtSemiLepSignalSelMVA);
const double Pi
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
double evaluateTtSemiLepSignalSel(PhysicsTools::MVAComputerCache &mvaComputer, const TtSemiLepSignalSel &sigsel, float weight=1., const bool training=false, const bool isSignal=false)
PhysicsTools::MVAComputerCache mvaComputer
static bool test(uint32_t val, uint32_t mask)
Definition: Flags.h:30
#define MVA_TRAINER_IMPLEMENT(N)
Definition: HelperMacros.h:40
selection
main part
Definition: corrVsCorr.py:98
TtSemiLepSignalSelMVATrainer(const edm::ParameterSet &)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:30
double DeltaR(math::XYZTLorentzVector v1, math::XYZTLorentzVector v2)
bool isNull() const
Checks for null.
Definition: Ref.h:247
double dPhi(double phi1, double phi2)
Definition: JetUtil.h:30
virtual void analyze(const edm::Event &evt, const edm::EventSetup &setup)
vector< PseudoJet > jets
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
bool update(const Calibration::MVAComputer *computer)
tuple muons
Definition: patZpeak.py:38
double DeltaPhi(math::XYZTLorentzVector v1, math::XYZTLorentzVector v2)
const_iterator begin() const
tuple cout
Definition: gather_cfg.py:121
const_iterator end() const
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")