CMS 3D CMS Logo

TtSemiLepJetCombMVATrainer.cc
Go to the documentation of this file.
1 #include "TMath.h"
2 #include <algorithm>
3 
7 
10 
13 
15  genEvtToken_ (consumes<TtGenEvent>(edm::InputTag("genEvt"))),
16  lepsToken_ (consumes< edm::View<reco::RecoCandidate> >(cfg.getParameter<edm::InputTag>("leps" ))),
17  jetsToken_ (consumes< std::vector<pat::Jet> >(cfg.getParameter<edm::InputTag>("jets" ))),
18  metsToken_ (consumes< std::vector<pat::MET> >(cfg.getParameter<edm::InputTag>("mets" ))),
19  matchingToken_ (mayConsume< std::vector< std::vector<int> > >(cfg.getParameter<edm::InputTag>("matching"))),
20  maxNJets_ (cfg.getParameter<int> ("maxNJets")),
21  leptonType_(readLeptonType(cfg.getParameter<std::string>("leptonType")))
22 {
23 }
24 
26 {
27 }
28 
29 void
31 {
32  for(unsigned int i = 0; i < 5; i++)
33  nEvents[i] = 0;
34 }
35 
36 void
38 {
39  mvaComputer.update<TtSemiLepJetCombMVARcd>("trainer", setup, "ttSemiLepJetCombMVA");
40 
41  // can occur in the last iteration when the
42  // MVATrainer is about to save the result
43  if(!mvaComputer) return;
44 
45  nEvents[0]++;
46 
48  evt.getByToken(genEvtToken_, genEvt);
49 
51  evt.getByToken(lepsToken_, leptons);
52 
53  // skip events with no appropriate lepton candidate
54  if( leptons->empty() ) return;
55 
56  nEvents[1]++;
57 
58  const math::XYZTLorentzVector lepton = leptons->begin()->p4();
59 
61  evt.getByToken(metsToken_, mets);
62 
63  // skip events with empty METs vector
64  if( mets->empty() ) return;
65 
66  nEvents[2]++;
67 
68  const pat::MET *met = &(*mets)[0];
69 
71  evt.getByToken(jetsToken_, jets);
72 
73  // skip events with less jets than partons
74  unsigned int nPartons = 4;
75  if( jets->size() < nPartons ) return;
76 
77  nEvents[3]++;
78 
80  std::vector<int> matching;
81  // get jet-parton matching if signal channel
82  if(genEvt->semiLeptonicChannel() == leptonType_) {
83  evt.getByToken(matchingToken_, matchingHandle);
84  if( matchingHandle->empty() )
85  throw cms::Exception("EmptyProduct")
86  << "Empty vector from jet-parton matching. This should not happen! \n";
87  matching = *(matchingHandle->begin());
88  if(matching.size() < nPartons) return;
89  // skip events that were affected by the outlier
90  // rejection in the jet-parton matching
91  for(unsigned int i = 0; i < matching.size(); ++i) {
92  if(matching[i] == -3) continue; // -3: parton was chosen to be excluded from jet-parton matching
93  if(matching[i] < 0 || matching[i] >= (int)jets->size())
94  return;
95  }
96  }
97  // use dummy for matching if not signal channel
98  else
99  for(unsigned int i = 0; i < nPartons; i++)
100  matching.push_back( -1 );
101 
102  nEvents[4]++;
103 
104  // take into account indistinguishability of the two jets from the hadr. W decay,
105  // reduces combinatorics by a factor of 2
107  int iTemp = matching[TtSemiLepEvtPartons::LightQ];
109  matching[TtSemiLepEvtPartons::LightQBar] = iTemp;
110  }
111 
112  std::vector<int> jetIndices;
113  for(unsigned int i=0; i<jets->size(); ++i) {
114  if(maxNJets_ >= (int) nPartons && i == (unsigned int) maxNJets_) break;
115  jetIndices.push_back(i);
116  }
117 
118  std::vector<int> combi;
119  for(unsigned int i = 0; i < nPartons; ++i)
120  combi.push_back(i);
121 
122  // use a ValueList to collect all variables for the MVAComputer
124 
125  // set target variable to false like for background
126  // this is necessary when passing all combinations at once
128 
129  do {
130  // number of possible combinations from number of partons: e.g. 4! = 24
131  for(unsigned int cnt = 0; cnt < TMath::Factorial( combi.size() ); ++cnt) {
132 
133  // take into account indistinguishability of the two jets from the hadr. W decay,
134  // reduces combinatorics by a factor of 2
135  if(combi[TtSemiLepEvtPartons::LightQ] < combi[TtSemiLepEvtPartons::LightQBar]) {
136 
137  TtSemiLepJetComb jetComb(*jets, combi, lepton, *met);
138 
139  bool trueCombi = false;
140  // true combination only if signal channel
141  // and in agreement with matching
142  if(genEvt->semiLeptonicChannel()==leptonType_) {
143  trueCombi = true;
144  for(unsigned int i = 0; i < matching.size(); ++i) {
145  if(combi[i]!=matching[i] && matching[i]!=-3) {
146  trueCombi = false;
147  break;
148  }
149  }
150  }
151 
152  // feed MVA input variables for this jetComb into the ValueList
153  values.add("target", trueCombi);
154  evaluateTtSemiLepJetComb(values, jetComb);
155 
156  }
157 
158  next_permutation( combi.begin() , combi.end() );
159  }
160  }
161  while(stdcomb::next_combination( jetIndices.begin(), jetIndices.end(), combi.begin(), combi.end() ));
162 
163  // pass MVA input variables for all jet combinations in this event
164  // to the MVAComputer for training
165  mvaComputer->eval( values );
166 }
167 
168 void
170 {
171  edm::LogInfo log("TtSemiLepJetCombMVATrainer");
172  log << "Number of events... \n"
173  << "...passed to the trainer : " << std::setw(7) << nEvents[0] << "\n"
174  << "...rejected since no lepton candidate : " << std::setw(7) << nEvents[0]-nEvents[1] << "\n"
175  << "...rejected since no MET object : " << std::setw(7) << nEvents[1]-nEvents[2] << "\n"
176  << "...rejected since not enough jets : " << std::setw(7) << nEvents[2]-nEvents[3] << "\n"
177  << "...rejected due to bad jet-parton matching : " << std::setw(7) << nEvents[3]-nEvents[4] << "\n"
178  << "...accepted for training : " << std::setw(7) << nEvents[4] << "\n";
179 }
180 
183 {
184  if (str == "kElec") return WDecay::kElec;
185  else if(str == "kMuon") return WDecay::kMuon;
186  else if(str == "kTau" ) return WDecay::kTau;
187  else throw cms::Exception("Configuration")
188  << "Chosen leptonType is not supported: " << str << "\n";
189 }
190 
191 // implement the plugins for the trainer
192 // -> defines TtSemiLepJetCombMVAContainerSaveCondDB
193 // -> defines TtSemiLepJetCombMVASaveFile
194 // -> defines TtSemiLepJetCombMVATrainerLooper
195 MVA_TRAINER_IMPLEMENT(TtSemiLepJetCombMVA);
PhysicsTools::MVAComputerCache mvaComputer
Analysis-level MET class.
Definition: MET.h:43
edm::EDGetTokenT< std::vector< pat::Jet > > jetsToken_
static const unsigned int nPartons
edm::EDGetTokenT< edm::View< reco::RecoCandidate > > lepsToken_
double eval(Iterator_t first, Iterator_t last) const
evaluate variables given by a range of iterators given by first and last
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
#define MVA_TRAINER_IMPLEMENT(N)
Definition: HelperMacros.h:40
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
WDecay::LeptonType readLeptonType(const std::string &str)
static const AtomicId kTargetId
Definition: MVATrainer.h:59
Definition: HeavyIon.h:7
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
Class derived from the TopGenEvent for ttbar events.
Definition: TtGenEvent.h:18
WDecay::LeptonType semiLeptonicChannel() const
return decay channel; all leptons including taus are allowed
Definition: TtGenEvent.cc:39
Definition: Jet.py:1
edm::EDGetTokenT< TtGenEvent > genEvtToken_
vector< PseudoJet > jets
edm::EDGetTokenT< std::vector< std::vector< int > > > matchingToken_
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:81
bool update(const Calibration::MVAComputer *computer)
met
===> hadronic RAZOR
Helper class that can contain an identifier-value pair.
Definition: Variable.h:51
void evaluateTtSemiLepJetComb(PhysicsTools::Variable::ValueList &values, const TtSemiLepJetComb &jetComb)
fixed size matrix
HLT enums.
Common calculator class to keep multivariate analysis variables for jet combinations in semi-leptonic...
TtSemiLepJetCombMVATrainer(const edm::ParameterSet &)
edm::EDGetTokenT< std::vector< pat::MET > > metsToken_
bool next_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end)
Definition: combination.h:22
void analyze(const edm::Event &evt, const edm::EventSetup &setup) override
void add(AtomicId id, double value)
Definition: Variable.h:106