CMS 3D CMS Logo

TtSemiLepJetCombMVATrainer.cc

Go to the documentation of this file.
00001 #include "TMath.h"
00002 #include <algorithm>
00003 
00004 #include "DataFormats/PatCandidates/interface/Jet.h"
00005 #include "DataFormats/PatCandidates/interface/MET.h"
00006 #include "DataFormats/RecoCandidate/interface/RecoCandidate.h"
00007 #include "AnalysisDataFormats/TopObjects/interface/TtGenEvent.h"
00008 #include "TopQuarkAnalysis/TopJetCombination/interface/TtSemiLepJetCombEval.h"
00009 #include "TopQuarkAnalysis/TopJetCombination/plugins/TtSemiLepJetCombMVATrainer.h"
00010 
00011 #include "PhysicsTools/JetMCUtils/interface/combination.h"
00012 #include "PhysicsTools/MVATrainer/interface/HelperMacros.h"
00013 
00014 TtSemiLepJetCombMVATrainer::TtSemiLepJetCombMVATrainer(const edm::ParameterSet& cfg):
00015   leptons_   (cfg.getParameter<edm::InputTag>("leptons")),
00016   jets_      (cfg.getParameter<edm::InputTag>("jets")),
00017   mets_      (cfg.getParameter<edm::InputTag>("mets")),
00018   matching_  (cfg.getParameter<edm::InputTag>("matching")),
00019   maxNJets_  (cfg.getParameter<int>("maxNJets")),
00020   lepChannel_(cfg.getParameter<int>("lepChannel"))
00021 {
00022 }
00023 
00024 TtSemiLepJetCombMVATrainer::~TtSemiLepJetCombMVATrainer()
00025 {
00026 }
00027 
00028 void 
00029 TtSemiLepJetCombMVATrainer::beginJob(const edm::EventSetup&)
00030 {
00031   for(unsigned int i = 0; i < 5; i++)
00032     nEvents[i] = 0;
00033 }
00034 
00035 void
00036 TtSemiLepJetCombMVATrainer::analyze(const edm::Event& evt, const edm::EventSetup& setup)
00037 {
00038   mvaComputer.update<TtSemiLepJetCombMVARcd>("trainer", setup, "ttSemiLepJetCombMVA");
00039 
00040   // can occur in the last iteration when the 
00041   // MVATrainer is about to save the result
00042   if(!mvaComputer) return;
00043 
00044   nEvents[0]++;
00045 
00046   edm::Handle<TtGenEvent> genEvt;
00047   evt.getByLabel("genEvt", genEvt);
00048 
00049   edm::Handle< edm::View<reco::RecoCandidate> > leptons; 
00050   evt.getByLabel(leptons_, leptons);
00051 
00052   // skip events with no appropriate lepton candidate
00053   if( leptons->empty() ) return;
00054 
00055   nEvents[1]++;
00056 
00057   const math::XYZTLorentzVector lepton = leptons->begin()->p4();
00058 
00059   edm::Handle< std::vector<pat::MET> > mets;
00060   evt.getByLabel(mets_, mets);
00061 
00062   // skip events with empty METs vector
00063   if( mets->empty() ) return;
00064 
00065   nEvents[2]++;
00066 
00067   const pat::MET *met = &(*mets)[0];
00068 
00069   edm::Handle< std::vector<pat::Jet> > jets;
00070   evt.getByLabel(jets_, jets);
00071 
00072   // skip events with less jets than partons
00073   unsigned int nPartons = 4;
00074   if( jets->size() < nPartons ) return;
00075 
00076   nEvents[3]++;
00077 
00078   edm::Handle< std::vector< std::vector<int> > > matchingHandle;
00079   std::vector<int> matching;
00080   // get jet-parton matching if signal channel
00081   if(genEvt->semiLeptonicChannel() == lepChannel_) {
00082     evt.getByLabel(matching_, matchingHandle);
00083     matching = *(matchingHandle->begin());
00084     // skip events that were affected by the outlier 
00085     // rejection in the jet-parton matching
00086     for(unsigned int i = 0; i < matching.size(); ++i)
00087       if(matching[i] < 0 || matching[i] >= (int)jets->size())
00088         return;
00089   }
00090 
00091   nEvents[4]++;
00092 
00093   // take into account indistinguishability of the two jets from the hadr. W decay,
00094   // reduces combinatorics by a factor of 2
00095   if(matching[TtSemiLepEvtPartons::LightQ] > matching[TtSemiLepEvtPartons::LightQBar]) {
00096     int iTemp = matching[TtSemiLepEvtPartons::LightQ];
00097     matching[TtSemiLepEvtPartons::LightQ] = matching[TtSemiLepEvtPartons::LightQBar];
00098     matching[TtSemiLepEvtPartons::LightQBar] = iTemp;
00099   }
00100 
00101   std::vector<int> jetIndices;
00102   for(unsigned int i=0; i<jets->size(); ++i) {
00103     if(maxNJets_ >= nPartons && i == (unsigned int) maxNJets_) break;
00104     jetIndices.push_back(i);
00105   }
00106 
00107   std::vector<int> combi;
00108   for(unsigned int i = 0; i < nPartons; ++i) 
00109     combi.push_back(i);
00110 
00111   // use a ValueList to collect all variables for the MVAComputer
00112   PhysicsTools::Variable::ValueList values;
00113 
00114   // set target variable to false like for background
00115   // this is necessary when passing all combinations at once
00116   values.add( PhysicsTools::Variable::Value(PhysicsTools::MVATrainer::kTargetId, false) );
00117 
00118   do {
00119     // number of possible combinations from number of partons: e.g. 4! = 24
00120     for(unsigned int cnt = 0; cnt < TMath::Factorial( combi.size() ); ++cnt) {
00121 
00122       // take into account indistinguishability of the two jets from the hadr. W decay,
00123       // reduces combinatorics by a factor of 2
00124       if(combi[TtSemiLepEvtPartons::LightQ] < combi[TtSemiLepEvtPartons::LightQBar]) {
00125         
00126         TtSemiLepJetComb jetComb(*jets, combi, lepton, *met);
00127 
00128         bool trueCombi = false;
00129         // true combination only if signal channel
00130         // and in agreement with matching
00131         if(genEvt->semiLeptonicChannel()==lepChannel_ && combi==matching)
00132           trueCombi = true;
00133 
00134         // feed MVA input variables for this jetComb into the ValueList
00135         values.add("target", trueCombi);
00136         evaluateTtSemiLepJetComb(values, jetComb);
00137 
00138       }
00139 
00140       next_permutation( combi.begin() , combi.end() );
00141     }
00142   }
00143   while(stdcomb::next_combination( jetIndices.begin(), jetIndices.end(), combi.begin(), combi.end() ));
00144 
00145   // pass MVA input variables for all jet combinations in this event
00146   // to the MVAComputer for training
00147   mvaComputer->eval( values );
00148 }
00149 
00150 void
00151 TtSemiLepJetCombMVATrainer::endJob() 
00152 {
00153   edm::LogInfo log("TtSemiLepJetCombMVATrainer");
00154   log << "Number of events... \n"
00155       << "...passed to the trainer                   : " << std::setw(7) << nEvents[0] << "\n"
00156       << "...rejected since no lepton candidate      : " << std::setw(7) << nEvents[0]-nEvents[1] << "\n"
00157       << "...rejected since no MET object            : " << std::setw(7) << nEvents[1]-nEvents[2] << "\n"
00158       << "...rejected since not enough jets          : " << std::setw(7) << nEvents[2]-nEvents[3] << "\n"
00159       << "...rejected due to bad jet-parton matching : " << std::setw(7) << nEvents[3]-nEvents[4] << "\n"
00160       << "...accepted for training                   : " << std::setw(7) << nEvents[4] << "\n";
00161 }
00162 
00163 // implement the plugins for the trainer
00164 // -> defines TtSemiLepJetCombMVAContainerSaveCondDB
00165 // -> defines TtSemiLepJetCombMVASaveFile
00166 // -> defines TtSemiLepJetCombMVATrainerLooper
00167 MVA_TRAINER_IMPLEMENT(TtSemiLepJetCombMVA);

Generated on Tue Jun 9 17:48:13 2009 for CMSSW by  doxygen 1.5.4