CMS 3D CMS Logo

TtSemiLepJetCombMVAComputer.cc
Go to the documentation of this file.
2 
4 
6  : mvaToken_(esConsumes()),
7  lepsToken_(consumes<edm::View<reco::RecoCandidate>>(cfg.getParameter<edm::InputTag>("leps"))),
8  jetsToken_(consumes<std::vector<pat::Jet>>(cfg.getParameter<edm::InputTag>("jets"))),
9  metsToken_(consumes<std::vector<pat::MET>>(cfg.getParameter<edm::InputTag>("mets"))),
10  maxNJets_(cfg.getParameter<int>("maxNJets")),
11  maxNComb_(cfg.getParameter<int>("maxNComb")) {
12  produces<std::vector<std::vector<int>>>();
13  produces<std::vector<double>>("Discriminators");
14  produces<std::string>("Method");
15  produces<int>("NumberOfConsideredJets");
16 }
17 
19  std::unique_ptr<std::vector<std::vector<int>>> pOut(new std::vector<std::vector<int>>);
20  std::unique_ptr<std::vector<double>> pOutDisc(new std::vector<double>);
21  std::unique_ptr<std::string> pOutMeth(new std::string);
22  std::unique_ptr<int> pJetsConsidered(new int);
23 
24  const auto& calibContainer = setup.getData(mvaToken_);
25  mvaComputer.update(&calibContainer, "ttSemiLepJetCombMVA");
26 
27  // read name of the processor that provides the MVA discriminator
28  // (to be used as meta information)
29  std::vector<const PhysicsTools::Calibration::VarProcessor*> processors =
30  (calibContainer.find("ttSemiLepJetCombMVA")).getProcessors();
31  *pOutMeth = (processors[processors.size() - 3])->getInstanceName();
32  evt.put(std::move(pOutMeth), "Method");
33 
34  // get lepton, jets and mets
37 
40 
43 
44  const unsigned int nPartons = 4;
45 
46  // skip events with no appropriate lepton candidate,
47  // empty METs vector or less jets than partons
48  if (leptons->empty() || mets->empty() || jets->size() < nPartons) {
49  std::vector<int> invalidCombi;
50  for (unsigned int i = 0; i < nPartons; ++i)
51  invalidCombi.push_back(-1);
52  pOut->push_back(invalidCombi);
53  evt.put(std::move(pOut));
54  pOutDisc->push_back(0.);
55  evt.put(std::move(pOutDisc), "Discriminators");
56  *pJetsConsidered = jets->size();
57  evt.put(std::move(pJetsConsidered), "NumberOfConsideredJets");
58  return;
59  }
60 
61  const math::XYZTLorentzVector lepton = leptons->begin()->p4();
62 
63  const pat::MET* met = &(*mets)[0];
64 
65  // analyze jet combinations
66  std::vector<int> jetIndices;
67  for (unsigned int i = 0; i < jets->size(); ++i) {
68  if (maxNJets_ >= (int)nPartons && maxNJets_ == (int)i) {
69  *pJetsConsidered = i;
70  break;
71  }
72  jetIndices.push_back(i);
73  }
74 
75  std::vector<int> combi;
76  for (unsigned int i = 0; i < nPartons; ++i)
77  combi.push_back(i);
78 
79  typedef std::pair<double, std::vector<int>> discCombPair;
80  std::list<discCombPair> discCombList;
81 
82  do {
83  for (int cnt = 0; cnt < TMath::Factorial(combi.size()); ++cnt) {
84  // take into account indistinguishability of the two jets from the hadr. W decay,
85  // reduces combinatorics by a factor of 2
87  TtSemiLepJetComb jetComb(*jets, combi, lepton, *met);
88 
89  // feed MVA input variables into a ValueList
92 
93  // get discriminator from the MVAComputer
94  double discrim = mvaComputer->eval(values);
95 
96  discCombList.push_back(std::make_pair(discrim, combi));
97  }
98  next_permutation(combi.begin(), combi.end());
99  }
100  } while (stdcomb::next_combination(jetIndices.begin(), jetIndices.end(), combi.begin(), combi.end()));
101 
102  // sort results w.r.t. discriminator values
103  discCombList.sort();
104 
105  // write result into the event
106  // (starting with the JetComb having the highest discriminator value -> reverse iterator)
107  unsigned int iDiscComb = 0;
108  typedef std::list<discCombPair>::reverse_iterator discCombIterator;
109  for (discCombIterator discCombPair = discCombList.rbegin(); discCombPair != discCombList.rend(); ++discCombPair) {
110  if (maxNComb_ >= 1 && iDiscComb == (unsigned int)maxNComb_)
111  break;
112  pOut->push_back(discCombPair->second);
113  pOutDisc->push_back(discCombPair->first);
114  iDiscComb++;
115  }
116  evt.put(std::move(pOut));
117  evt.put(std::move(pOutDisc), "Discriminators");
118  evt.put(std::move(pJetsConsidered), "NumberOfConsideredJets");
119 }
120 
121 // implement the plugins for the computer container
122 // -> register TtSemiLepJetCombMVARcd
123 // -> define TtSemiLepJetCombMVAFileSource
124 MVA_COMPUTER_CONTAINER_IMPLEMENT(TtSemiLepJetCombMVA);
Analysis-level MET class.
Definition: MET.h:40
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
static const unsigned int nPartons
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
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:526
edm::EDGetTokenT< std::vector< pat::Jet > > jetsToken_
edm::EDGetTokenT< std::vector< pat::MET > > metsToken_
Definition: HeavyIon.h:7
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
edm::ESGetToken< PhysicsTools::Calibration::MVAComputerContainer, TtSemiLepJetCombMVARcd > mvaToken_
Definition: Jet.py:1
PhysicsTools::MVAComputerCache mvaComputer
void produce(edm::Event &evt, const edm::EventSetup &setup) override
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:77
bool update(const Calibration::MVAComputer *computer)
void evaluateTtSemiLepJetComb(PhysicsTools::Variable::ValueList &values, const TtSemiLepJetComb &jetComb)
TtSemiLepJetCombMVAComputer(const edm::ParameterSet &)
fixed size matrix
HLT enums.
Common calculator class to keep multivariate analysis variables for jet combinations in semi-leptonic...
edm::EDGetTokenT< edm::View< reco::RecoCandidate > > lepsToken_
bool next_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end)
Definition: combination.h:19
def move(src, dest)
Definition: eostools.py:511
#define MVA_COMPUTER_CONTAINER_IMPLEMENT(N)
Definition: HelperMacros.h:53