CMS 3D CMS Logo

TtSemiLepKinFitProducer.h
Go to the documentation of this file.
1 #ifndef TtSemiLepKinFitProducer_h
2 #define TtSemiLepKinFitProducer_h
3 
7 
11 
12 template <typename LeptonCollection>
14 public:
16  ~TtSemiLepKinFitProducer() override;
17 
18 private:
19  // produce
20  void produce(edm::Event&, const edm::EventSetup&) override;
21 
22  // convert unsigned to Param
24  // convert unsigned to Param
26  // convert unsigned to Param
27  std::vector<TtSemiLepKinFitter::Constraint> constraints(std::vector<unsigned>&);
28  // helper function for b-tagging
29  bool doBTagging(bool& useBTag_,
30  edm::Handle<std::vector<pat::Jet>>& jets,
31  std::vector<int>& combi,
33  double& minBTagValueBJets_,
34  double& maxBTagValueNonBJets_);
35 
39 
50  bool useBTag_;
52  int maxNJets_;
54  int maxNComb_;
55 
57  unsigned int maxNrIter_;
59  double maxDeltaS_;
61  double maxF_;
62  unsigned int jetParam_;
63  unsigned int lepParam_;
64  unsigned int metParam_;
66  std::vector<unsigned> constraints_;
67  double mW_;
68  double mTop_;
70  std::vector<double> jetEnergyResolutionScaleFactors_;
71  std::vector<double> jetEnergyResolutionEtaBinning_;
73  std::vector<edm::ParameterSet> udscResolutions_;
74  std::vector<edm::ParameterSet> bResolutions_;
75  std::vector<edm::ParameterSet> lepResolutions_;
76  std::vector<edm::ParameterSet> metResolutions_;
77 
79 
80  struct KinFitResult {
81  int Status;
82  double Chi2;
83  double Prob;
90  std::vector<int> JetCombi;
91  bool operator<(const KinFitResult& rhs) { return Chi2 < rhs.Chi2; };
92  };
93 };
94 
95 template <typename LeptonCollection>
97  : jetsToken_(consumes<std::vector<pat::Jet>>(cfg.getParameter<edm::InputTag>("jets"))),
98  lepsToken_(consumes<LeptonCollection>(cfg.getParameter<edm::InputTag>("leps"))),
99  metsToken_(consumes<std::vector<pat::MET>>(cfg.getParameter<edm::InputTag>("mets"))),
100  matchToken_(mayConsume<std::vector<std::vector<int>>>(cfg.getParameter<edm::InputTag>("match"))),
101  useOnlyMatch_(cfg.getParameter<bool>("useOnlyMatch")),
102  bTagAlgo_(cfg.getParameter<std::string>("bTagAlgo")),
103  minBTagValueBJet_(cfg.getParameter<double>("minBDiscBJets")),
104  maxBTagValueNonBJet_(cfg.getParameter<double>("maxBDiscLightJets")),
105  useBTag_(cfg.getParameter<bool>("useBTagging")),
106  maxNJets_(cfg.getParameter<int>("maxNJets")),
107  maxNComb_(cfg.getParameter<int>("maxNComb")),
108  maxNrIter_(cfg.getParameter<unsigned>("maxNrIter")),
109  maxDeltaS_(cfg.getParameter<double>("maxDeltaS")),
110  maxF_(cfg.getParameter<double>("maxF")),
111  jetParam_(cfg.getParameter<unsigned>("jetParametrisation")),
112  lepParam_(cfg.getParameter<unsigned>("lepParametrisation")),
113  metParam_(cfg.getParameter<unsigned>("metParametrisation")),
114  constraints_(cfg.getParameter<std::vector<unsigned>>("constraints")),
115  mW_(cfg.getParameter<double>("mW")),
116  mTop_(cfg.getParameter<double>("mTop")),
117  jetEnergyResolutionScaleFactors_(cfg.getParameter<std::vector<double>>("jetEnergyResolutionScaleFactors")),
118  jetEnergyResolutionEtaBinning_(cfg.getParameter<std::vector<double>>("jetEnergyResolutionEtaBinning")),
119  udscResolutions_(0),
120  bResolutions_(0),
121  lepResolutions_(0),
122  metResolutions_(0) {
123  if (cfg.exists("udscResolutions") && cfg.exists("bResolutions") && cfg.exists("lepResolutions") &&
124  cfg.exists("metResolutions")) {
125  udscResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet>>("udscResolutions");
126  bResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet>>("bResolutions");
127  lepResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet>>("lepResolutions");
128  metResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet>>("metResolutions");
129  } else if (cfg.exists("udscResolutions") || cfg.exists("bResolutions") || cfg.exists("lepResolutions") ||
130  cfg.exists("metResolutions")) {
131  throw cms::Exception("Configuration") << "Parameters 'udscResolutions', 'bResolutions', 'lepResolutions', "
132  "'metResolutions' should be used together.\n";
133  }
134 
136  param(lepParam_),
137  param(metParam_),
138  maxNrIter_,
139  maxDeltaS_,
140  maxF_,
142  mW_,
143  mTop_,
145  &bResolutions_,
150 
151  produces<std::vector<pat::Particle>>("PartonsHadP");
152  produces<std::vector<pat::Particle>>("PartonsHadQ");
153  produces<std::vector<pat::Particle>>("PartonsHadB");
154  produces<std::vector<pat::Particle>>("PartonsLepB");
155  produces<std::vector<pat::Particle>>("Leptons");
156  produces<std::vector<pat::Particle>>("Neutrinos");
157 
158  produces<std::vector<std::vector<int>>>();
159  produces<std::vector<double>>("Chi2");
160  produces<std::vector<double>>("Prob");
161  produces<std::vector<int>>("Status");
162 
163  produces<int>("NumberOfConsideredJets");
164 }
165 
166 template <typename LeptonCollection>
168  delete fitter;
169 }
170 
171 template <typename LeptonCollection>
173  edm::Handle<std::vector<pat::Jet>>& jets,
174  std::vector<int>& combi,
176  double& minBTagValueBJet_,
177  double& maxBTagValueNonBJet_) {
178  if (!useBTag_) {
179  return true;
180  }
181  if (useBTag_ && (*jets)[combi[TtSemiLepEvtPartons::HadB]].bDiscriminator(bTagAlgo_) >= minBTagValueBJet_ &&
182  (*jets)[combi[TtSemiLepEvtPartons::LepB]].bDiscriminator(bTagAlgo_) >= minBTagValueBJet_ &&
183  (*jets)[combi[TtSemiLepEvtPartons::LightQ]].bDiscriminator(bTagAlgo_) < maxBTagValueNonBJet_ &&
184  (*jets)[combi[TtSemiLepEvtPartons::LightQBar]].bDiscriminator(bTagAlgo_) < maxBTagValueNonBJet_) {
185  return true;
186  } else {
187  return false;
188  }
189 }
190 
191 template <typename LeptonCollection>
193  std::unique_ptr<std::vector<pat::Particle>> pPartonsHadP(new std::vector<pat::Particle>);
194  std::unique_ptr<std::vector<pat::Particle>> pPartonsHadQ(new std::vector<pat::Particle>);
195  std::unique_ptr<std::vector<pat::Particle>> pPartonsHadB(new std::vector<pat::Particle>);
196  std::unique_ptr<std::vector<pat::Particle>> pPartonsLepB(new std::vector<pat::Particle>);
197  std::unique_ptr<std::vector<pat::Particle>> pLeptons(new std::vector<pat::Particle>);
198  std::unique_ptr<std::vector<pat::Particle>> pNeutrinos(new std::vector<pat::Particle>);
199 
200  std::unique_ptr<std::vector<std::vector<int>>> pCombi(new std::vector<std::vector<int>>);
201  std::unique_ptr<std::vector<double>> pChi2(new std::vector<double>);
202  std::unique_ptr<std::vector<double>> pProb(new std::vector<double>);
203  std::unique_ptr<std::vector<int>> pStatus(new std::vector<int>);
204 
205  std::unique_ptr<int> pJetsConsidered(new int);
206 
208  evt.getByToken(jetsToken_, jets);
209 
211  evt.getByToken(metsToken_, mets);
212 
214  evt.getByToken(lepsToken_, leps);
215 
216  const unsigned int nPartons = 4;
217 
218  std::vector<int> match;
219  bool invalidMatch = false;
220  if (useOnlyMatch_) {
221  *pJetsConsidered = nPartons;
223  evt.getByToken(matchToken_, matchHandle);
224  match = *(matchHandle->begin());
225  // check if match is valid
226  if (match.size() != nPartons)
227  invalidMatch = true;
228  else {
229  for (unsigned int idx = 0; idx < match.size(); ++idx) {
230  if (match[idx] < 0 || match[idx] >= (int)jets->size()) {
231  invalidMatch = true;
232  break;
233  }
234  }
235  }
236  }
237 
238  // -----------------------------------------------------
239  // skip events with no appropriate lepton candidate in
240  // or empty MET or less jets than partons or invalid match
241  // -----------------------------------------------------
242 
243  if (leps->empty() || mets->empty() || jets->size() < nPartons || invalidMatch) {
244  // the kinFit getters return empty objects here
245  pPartonsHadP->push_back(fitter->fittedHadP());
246  pPartonsHadQ->push_back(fitter->fittedHadQ());
247  pPartonsHadB->push_back(fitter->fittedHadB());
248  pPartonsLepB->push_back(fitter->fittedLepB());
249  pLeptons->push_back(fitter->fittedLepton());
250  pNeutrinos->push_back(fitter->fittedNeutrino());
251  // indices referring to the jet combination
252  std::vector<int> invalidCombi;
253  for (unsigned int i = 0; i < nPartons; ++i)
254  invalidCombi.push_back(-1);
255  pCombi->push_back(invalidCombi);
256  // chi2
257  pChi2->push_back(-1.);
258  // chi2 probability
259  pProb->push_back(-1.);
260  // status of the fitter
261  pStatus->push_back(-1);
262  // number of jets
263  *pJetsConsidered = jets->size();
264  // feed out all products
265  evt.put(std::move(pCombi));
266  evt.put(std::move(pPartonsHadP), "PartonsHadP");
267  evt.put(std::move(pPartonsHadQ), "PartonsHadQ");
268  evt.put(std::move(pPartonsHadB), "PartonsHadB");
269  evt.put(std::move(pPartonsLepB), "PartonsLepB");
270  evt.put(std::move(pLeptons), "Leptons");
271  evt.put(std::move(pNeutrinos), "Neutrinos");
272  evt.put(std::move(pChi2), "Chi2");
273  evt.put(std::move(pProb), "Prob");
274  evt.put(std::move(pStatus), "Status");
275  evt.put(std::move(pJetsConsidered), "NumberOfConsideredJets");
276  return;
277  }
278 
279  // -----------------------------------------------------
280  // analyze different jet combinations using the KinFitter
281  // (or only a given jet combination if useOnlyMatch=true)
282  // -----------------------------------------------------
283 
284  std::vector<int> jetIndices;
285  if (!useOnlyMatch_) {
286  for (unsigned int i = 0; i < jets->size(); ++i) {
287  if (maxNJets_ >= (int)nPartons && maxNJets_ == (int)i) {
288  *pJetsConsidered = i;
289  break;
290  }
291  jetIndices.push_back(i);
292  }
293  }
294 
295  std::vector<int> combi;
296  for (unsigned int i = 0; i < nPartons; ++i) {
297  if (useOnlyMatch_)
298  combi.push_back(match[i]);
299  else
300  combi.push_back(i);
301  }
302 
303  std::list<KinFitResult> FitResultList;
304 
305  do {
306  for (int cnt = 0; cnt < TMath::Factorial(combi.size()); ++cnt) {
307  // take into account indistinguishability of the two jets from the hadr. W decay,
308  // reduces combinatorics by a factor of 2
311  std::vector<pat::Jet> jetCombi;
312  jetCombi.resize(nPartons);
313  jetCombi[TtSemiLepEvtPartons::LightQ] = (*jets)[combi[TtSemiLepEvtPartons::LightQ]];
315  jetCombi[TtSemiLepEvtPartons::HadB] = (*jets)[combi[TtSemiLepEvtPartons::HadB]];
316  jetCombi[TtSemiLepEvtPartons::LepB] = (*jets)[combi[TtSemiLepEvtPartons::LepB]];
317 
318  // do the kinematic fit
319  const int status = fitter->fit(jetCombi, (*leps)[0], (*mets)[0]);
320 
321  if (status == 0) { // only take into account converged fits
323  result.Status = status;
324  result.Chi2 = fitter->fitS();
325  result.Prob = fitter->fitProb();
326  result.HadB = fitter->fittedHadB();
327  result.HadP = fitter->fittedHadP();
328  result.HadQ = fitter->fittedHadQ();
329  result.LepB = fitter->fittedLepB();
330  result.LepL = fitter->fittedLepton();
331  result.LepN = fitter->fittedNeutrino();
332  result.JetCombi = combi;
333 
334  FitResultList.push_back(result);
335  }
336  }
337  if (useOnlyMatch_)
338  break; // don't go through combinatorics if useOnlyMatch was chosen
339  next_permutation(combi.begin(), combi.end());
340  }
341  if (useOnlyMatch_)
342  break; // don't go through combinatorics if useOnlyMatch was chosen
343  } while (stdcomb::next_combination(jetIndices.begin(), jetIndices.end(), combi.begin(), combi.end()));
344 
345  // sort results w.r.t. chi2 values
346  FitResultList.sort();
347 
348  // -----------------------------------------------------
349  // feed out result
350  // starting with the JetComb having the smallest chi2
351  // -----------------------------------------------------
352 
353  if ((unsigned)FitResultList.size() < 1) { // in case no fit results were stored in the list (all fits aborted)
354  pPartonsHadP->push_back(fitter->fittedHadP());
355  pPartonsHadQ->push_back(fitter->fittedHadQ());
356  pPartonsHadB->push_back(fitter->fittedHadB());
357  pPartonsLepB->push_back(fitter->fittedLepB());
358  pLeptons->push_back(fitter->fittedLepton());
359  pNeutrinos->push_back(fitter->fittedNeutrino());
360  // indices referring to the jet combination
361  std::vector<int> invalidCombi;
362  for (unsigned int i = 0; i < nPartons; ++i)
363  invalidCombi.push_back(-1);
364  pCombi->push_back(invalidCombi);
365  // chi2
366  pChi2->push_back(-1.);
367  // chi2 probability
368  pProb->push_back(-1.);
369  // status of the fitter
370  pStatus->push_back(-1);
371  } else {
372  unsigned int iComb = 0;
373  for (typename std::list<KinFitResult>::const_iterator result = FitResultList.begin(); result != FitResultList.end();
374  ++result) {
375  if (maxNComb_ >= 1 && iComb == (unsigned int)maxNComb_)
376  break;
377  iComb++;
378  // partons
379  pPartonsHadP->push_back(result->HadP);
380  pPartonsHadQ->push_back(result->HadQ);
381  pPartonsHadB->push_back(result->HadB);
382  pPartonsLepB->push_back(result->LepB);
383  // lepton
384  pLeptons->push_back(result->LepL);
385  // neutrino
386  pNeutrinos->push_back(result->LepN);
387  // indices referring to the jet combination
388  pCombi->push_back(result->JetCombi);
389  // chi2
390  pChi2->push_back(result->Chi2);
391  // chi2 probability
392  pProb->push_back(result->Prob);
393  // status of the fitter
394  pStatus->push_back(result->Status);
395  }
396  }
397  evt.put(std::move(pCombi));
398  evt.put(std::move(pPartonsHadP), "PartonsHadP");
399  evt.put(std::move(pPartonsHadQ), "PartonsHadQ");
400  evt.put(std::move(pPartonsHadB), "PartonsHadB");
401  evt.put(std::move(pPartonsLepB), "PartonsLepB");
402  evt.put(std::move(pLeptons), "Leptons");
403  evt.put(std::move(pNeutrinos), "Neutrinos");
404  evt.put(std::move(pChi2), "Chi2");
405  evt.put(std::move(pProb), "Prob");
406  evt.put(std::move(pStatus), "Status");
407  evt.put(std::move(pJetsConsidered), "NumberOfConsideredJets");
408 }
409 
410 template <typename LeptonCollection>
413  switch (val) {
415  result = TtSemiLepKinFitter::kEMom;
416  break;
419  break;
422  break;
423  default:
424  throw cms::Exception("Configuration") << "Chosen jet parametrization is not supported: " << val << "\n";
425  break;
426  }
427  return result;
428 }
429 
430 template <typename LeptonCollection>
433  switch (val) {
436  break;
439  break;
442  break;
445  break;
448  break;
451  break;
454  break;
455  default:
456  throw cms::Exception("Configuration") << "Chosen fit constraint is not supported: " << val << "\n";
457  break;
458  }
459  return result;
460 }
461 
462 template <typename LeptonCollection>
463 std::vector<TtSemiLepKinFitter::Constraint> TtSemiLepKinFitProducer<LeptonCollection>::constraints(
464  std::vector<unsigned>& val) {
465  std::vector<TtSemiLepKinFitter::Constraint> result;
466  for (unsigned i = 0; i < val.size(); ++i) {
467  result.push_back(constraint(val[i]));
468  }
469  return result;
470 }
471 
472 #endif
std::vector< edm::ParameterSet > bResolutions_
std::vector< double > jetEnergyResolutionScaleFactors_
scale factors for jet energy resolution
T getParameter(std::string const &) const
double maxBTagValueNonBJet_
max value of bTag for a non-b-jet
const pat::Particle fittedHadP() const
return hadronic light quark candidate
edm::EDGetTokenT< std::vector< pat::Jet > > jetsToken_
std::string bTagAlgo_
input tag for b-tagging algorithm
edm::EDGetTokenT< std::vector< pat::MET > > metsToken_
static const unsigned int nPartons
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
double minBTagValueBJet_
min value of bTag for a b-jet
Param
supported parameterizations
Definition: TopKinFitter.h:20
const pat::Particle fittedNeutrino() const
return neutrino candidate
const pat::Particle fittedLepB() const
return leptonic b quark candidate
std::vector< TtSemiLepKinFitter::Constraint > constraints(std::vector< unsigned > &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
bool exists(std::string const &parameterName) const
checks if a parameter exists
double fitProb() const
return fit probability
Definition: TopKinFitter.h:37
int maxNJets_
maximal number of jets (-1 possible to indicate &#39;all&#39;)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
bool doBTagging(bool &useBTag_, edm::Handle< std::vector< pat::Jet >> &jets, std::vector< int > &combi, std::string &bTagAlgo_, double &minBTagValueBJets_, double &maxBTagValueNonBJets_)
int fit(const std::vector< pat::Jet > &jets, const pat::Lepton< LeptonType > &leps, const pat::MET &met)
kinematic fit interface for PAT objects
std::vector< double > jetEnergyResolutionEtaBinning_
Definition: Jet.py:1
edm::EDGetTokenT< std::vector< std::vector< int > > > matchToken_
std::vector< unsigned > constraints_
constrains
unsigned int maxNrIter_
maximal number of iterations to be performed for the fit
bool operator<(const KinFitResult &rhs)
const pat::Particle fittedHadB() const
return hadronic b quark candidate
std::vector< edm::ParameterSet > udscResolutions_
config-file-based object resolutions
int maxNComb_
maximal number of combinations to be written to the event
double maxF_
maximal deviation for contstraints
edm::EDGetTokenT< LeptonCollection > lepsToken_
std::vector< edm::ParameterSet > metResolutions_
Analysis-level particle class.
Definition: Particle.h:30
void produce(edm::Event &, const edm::EventSetup &) override
const pat::Particle fittedHadQ() const
return hadronic light quark candidate
TtSemiLepKinFitter::Constraint constraint(unsigned)
const pat::Particle fittedLepton() const
return lepton candidate
HLT enums.
bool useOnlyMatch_
switch to use only a combination given by another hypothesis
double fitS() const
return chi2 of fit (not normalized to degrees of freedom)
Definition: TopKinFitter.h:33
bool useBTag_
switch to tell whether to use b-tagging or not
TtSemiLepKinFitter::Param param(unsigned)
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
TtSemiLepKinFitProducer(const edm::ParameterSet &)
double maxDeltaS_
maximal chi2 equivalent
bool next_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end)
Definition: combination.h:19
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
std::vector< edm::ParameterSet > lepResolutions_
def move(src, dest)
Definition: eostools.py:511
Constraint
supported constraints