CMS 3D CMS Logo

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