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 
15  public:
16 
18  ~TtSemiLepKinFitProducer() override;
19 
20  private:
21  // produce
22  void produce(edm::Event&, const edm::EventSetup&) override;
23 
24  // convert unsigned to Param
26  // convert unsigned to Param
28  // convert unsigned to Param
29  std::vector<TtSemiLepKinFitter::Constraint> constraints(std::vector<unsigned>&);
30  // helper function for b-tagging
31  bool doBTagging(bool& useBTag_, edm::Handle<std::vector<pat::Jet> >& jets, std::vector<int>& combi,
32  std::string& bTagAlgo_, double& minBTagValueBJets_, double& maxBTagValueNonBJets_);
33 
37 
48  bool useBTag_;
50  int maxNJets_;
52  int maxNComb_;
53 
55  unsigned int maxNrIter_;
57  double maxDeltaS_;
59  double maxF_;
60  unsigned int jetParam_;
61  unsigned int lepParam_;
62  unsigned int metParam_;
64  std::vector<unsigned> constraints_;
65  double mW_;
66  double mTop_;
68  std::vector<double> jetEnergyResolutionScaleFactors_;
69  std::vector<double> jetEnergyResolutionEtaBinning_;
71  std::vector<edm::ParameterSet> udscResolutions_;
72  std::vector<edm::ParameterSet> bResolutions_;
73  std::vector<edm::ParameterSet> lepResolutions_;
74  std::vector<edm::ParameterSet> metResolutions_;
75 
77 
78  struct KinFitResult {
79  int Status;
80  double Chi2;
81  double Prob;
88  std::vector<int> JetCombi;
89  bool operator< (const KinFitResult& rhs) { return Chi2 < rhs.Chi2; };
90  };
91 };
92 
93 template<typename LeptonCollection>
95  jetsToken_ (consumes<std::vector<pat::Jet> >(cfg.getParameter<edm::InputTag>("jets"))),
96  lepsToken_ (consumes<LeptonCollection>(cfg.getParameter<edm::InputTag>("leps"))),
97  metsToken_ (consumes<std::vector<pat::MET> >(cfg.getParameter<edm::InputTag>("mets"))),
98  matchToken_ (mayConsume<std::vector<std::vector<int> > >(cfg.getParameter<edm::InputTag>("match"))),
99  useOnlyMatch_ (cfg.getParameter<bool> ("useOnlyMatch" )),
100  bTagAlgo_ (cfg.getParameter<std::string> ("bTagAlgo" )),
101  minBTagValueBJet_ (cfg.getParameter<double> ("minBDiscBJets" )),
102  maxBTagValueNonBJet_ (cfg.getParameter<double> ("maxBDiscLightJets" )),
103  useBTag_ (cfg.getParameter<bool> ("useBTagging" )),
104  maxNJets_ (cfg.getParameter<int> ("maxNJets" )),
105  maxNComb_ (cfg.getParameter<int> ("maxNComb" )),
106  maxNrIter_ (cfg.getParameter<unsigned> ("maxNrIter" )),
107  maxDeltaS_ (cfg.getParameter<double> ("maxDeltaS" )),
108  maxF_ (cfg.getParameter<double> ("maxF" )),
109  jetParam_ (cfg.getParameter<unsigned> ("jetParametrisation" )),
110  lepParam_ (cfg.getParameter<unsigned> ("lepParametrisation" )),
111  metParam_ (cfg.getParameter<unsigned> ("metParametrisation" )),
112  constraints_ (cfg.getParameter<std::vector<unsigned> >("constraints")),
113  mW_ (cfg.getParameter<double> ("mW" )),
114  mTop_ (cfg.getParameter<double> ("mTop" )),
115  jetEnergyResolutionScaleFactors_(cfg.getParameter<std::vector<double> >("jetEnergyResolutionScaleFactors")),
116  jetEnergyResolutionEtaBinning_ (cfg.getParameter<std::vector<double> >("jetEnergyResolutionEtaBinning")),
118 {
119  if(cfg.exists("udscResolutions") && cfg.exists("bResolutions") && cfg.exists("lepResolutions") && cfg.exists("metResolutions")){
120  udscResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet> >("udscResolutions");
121  bResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet> >("bResolutions" );
122  lepResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet> >("lepResolutions" );
123  metResolutions_ = cfg.getParameter<std::vector<edm::ParameterSet> >("metResolutions" );
124  }
125  else if(cfg.exists("udscResolutions") || cfg.exists("bResolutions") || cfg.exists("lepResolutions") || cfg.exists("metResolutions") ){
126  throw cms::Exception("Configuration") << "Parameters 'udscResolutions', 'bResolutions', 'lepResolutions', 'metResolutions' should be used together.\n";
127  }
128 
132 
133  produces< std::vector<pat::Particle> >("PartonsHadP");
134  produces< std::vector<pat::Particle> >("PartonsHadQ");
135  produces< std::vector<pat::Particle> >("PartonsHadB");
136  produces< std::vector<pat::Particle> >("PartonsLepB");
137  produces< std::vector<pat::Particle> >("Leptons");
138  produces< std::vector<pat::Particle> >("Neutrinos");
139 
140  produces< std::vector<std::vector<int> > >();
141  produces< std::vector<double> >("Chi2");
142  produces< std::vector<double> >("Prob");
143  produces< std::vector<int> >("Status");
144 
145  produces<int>("NumberOfConsideredJets");
146 }
147 
148 template<typename LeptonCollection>
150 {
151  delete fitter;
152 }
153 
154 template<typename LeptonCollection>
155 bool TtSemiLepKinFitProducer<LeptonCollection>::doBTagging(bool& useBTag_, edm::Handle<std::vector<pat::Jet> >& jets, std::vector<int>& combi,
157 
158  if( !useBTag_ ) {
159  return true;
160  }
161  if( useBTag_ &&
162  (*jets)[combi[TtSemiLepEvtPartons::HadB ]].bDiscriminator(bTagAlgo_) >= minBTagValueBJet_ &&
163  (*jets)[combi[TtSemiLepEvtPartons::LepB ]].bDiscriminator(bTagAlgo_) >= minBTagValueBJet_ &&
164  (*jets)[combi[TtSemiLepEvtPartons::LightQ ]].bDiscriminator(bTagAlgo_) < maxBTagValueNonBJet_ &&
165  (*jets)[combi[TtSemiLepEvtPartons::LightQBar]].bDiscriminator(bTagAlgo_) < maxBTagValueNonBJet_ ) {
166  return true;
167  }
168  else{
169  return false;
170  }
171 }
172 
173 template<typename LeptonCollection>
175 {
176  std::unique_ptr< std::vector<pat::Particle> > pPartonsHadP( new std::vector<pat::Particle> );
177  std::unique_ptr< std::vector<pat::Particle> > pPartonsHadQ( new std::vector<pat::Particle> );
178  std::unique_ptr< std::vector<pat::Particle> > pPartonsHadB( new std::vector<pat::Particle> );
179  std::unique_ptr< std::vector<pat::Particle> > pPartonsLepB( new std::vector<pat::Particle> );
180  std::unique_ptr< std::vector<pat::Particle> > pLeptons ( new std::vector<pat::Particle> );
181  std::unique_ptr< std::vector<pat::Particle> > pNeutrinos ( new std::vector<pat::Particle> );
182 
183  std::unique_ptr< std::vector<std::vector<int> > > pCombi ( new std::vector<std::vector<int> > );
184  std::unique_ptr< std::vector<double> > pChi2 ( new std::vector<double> );
185  std::unique_ptr< std::vector<double> > pProb ( new std::vector<double> );
186  std::unique_ptr< std::vector<int> > pStatus( new std::vector<int> );
187 
188  std::unique_ptr<int> pJetsConsidered(new int);
189 
191  evt.getByToken(jetsToken_, jets);
192 
194  evt.getByToken(metsToken_, mets);
195 
197  evt.getByToken(lepsToken_, leps);
198 
199  const unsigned int nPartons = 4;
200 
201  std::vector<int> match;
202  bool invalidMatch = false;
203  if(useOnlyMatch_) {
204  *pJetsConsidered = nPartons;
206  evt.getByToken(matchToken_, matchHandle);
207  match = *(matchHandle->begin());
208  // check if match is valid
209  if(match.size()!=nPartons) invalidMatch=true;
210  else {
211  for(unsigned int idx=0; idx<match.size(); ++idx) {
212  if(match[idx]<0 || match[idx]>=(int)jets->size()) {
213  invalidMatch=true;
214  break;
215  }
216  }
217  }
218  }
219 
220  // -----------------------------------------------------
221  // skip events with no appropriate lepton candidate in
222  // or empty MET or less jets than partons or invalid match
223  // -----------------------------------------------------
224 
225  if( leps->empty() || mets->empty() || jets->size()<nPartons || invalidMatch ) {
226  // the kinFit getters return empty objects here
227  pPartonsHadP->push_back( fitter->fittedHadP() );
228  pPartonsHadQ->push_back( fitter->fittedHadQ() );
229  pPartonsHadB->push_back( fitter->fittedHadB() );
230  pPartonsLepB->push_back( fitter->fittedLepB() );
231  pLeptons ->push_back( fitter->fittedLepton() );
232  pNeutrinos ->push_back( fitter->fittedNeutrino() );
233  // indices referring to the jet combination
234  std::vector<int> invalidCombi;
235  for(unsigned int i = 0; i < nPartons; ++i)
236  invalidCombi.push_back( -1 );
237  pCombi->push_back( invalidCombi );
238  // chi2
239  pChi2->push_back( -1. );
240  // chi2 probability
241  pProb->push_back( -1. );
242  // status of the fitter
243  pStatus->push_back( -1 );
244  // number of jets
245  *pJetsConsidered = jets->size();
246  // feed out all products
247  evt.put(std::move(pCombi));
248  evt.put(std::move(pPartonsHadP), "PartonsHadP");
249  evt.put(std::move(pPartonsHadQ), "PartonsHadQ");
250  evt.put(std::move(pPartonsHadB), "PartonsHadB");
251  evt.put(std::move(pPartonsLepB), "PartonsLepB");
252  evt.put(std::move(pLeptons ), "Leptons" );
253  evt.put(std::move(pNeutrinos ), "Neutrinos" );
254  evt.put(std::move(pChi2 ), "Chi2" );
255  evt.put(std::move(pProb ), "Prob" );
256  evt.put(std::move(pStatus ), "Status" );
257  evt.put(std::move(pJetsConsidered), "NumberOfConsideredJets");
258  return;
259  }
260 
261  // -----------------------------------------------------
262  // analyze different jet combinations using the KinFitter
263  // (or only a given jet combination if useOnlyMatch=true)
264  // -----------------------------------------------------
265 
266  std::vector<int> jetIndices;
267  if(!useOnlyMatch_) {
268  for(unsigned int i=0; i<jets->size(); ++i){
269  if(maxNJets_ >= (int) nPartons && maxNJets_ == (int) i) {
270  *pJetsConsidered = i;
271  break;
272  }
273  jetIndices.push_back(i);
274  }
275  }
276 
277  std::vector<int> combi;
278  for(unsigned int i=0; i<nPartons; ++i) {
279  if(useOnlyMatch_) combi.push_back( match[i] );
280  else combi.push_back(i);
281  }
282 
283  std::list<KinFitResult> FitResultList;
284 
285  do{
286  for(int cnt = 0; cnt < TMath::Factorial( combi.size() ); ++cnt){
287  // take into account indistinguishability of the two jets from the hadr. W decay,
288  // reduces combinatorics by a factor of 2
291 
292  std::vector<pat::Jet> jetCombi;
293  jetCombi.resize(nPartons);
294  jetCombi[TtSemiLepEvtPartons::LightQ ] = (*jets)[combi[TtSemiLepEvtPartons::LightQ ]];
296  jetCombi[TtSemiLepEvtPartons::HadB ] = (*jets)[combi[TtSemiLepEvtPartons::HadB ]];
297  jetCombi[TtSemiLepEvtPartons::LepB ] = (*jets)[combi[TtSemiLepEvtPartons::LepB ]];
298 
299  // do the kinematic fit
300  const int status = fitter->fit(jetCombi, (*leps)[0], (*mets)[0]);
301 
302  if( status == 0 ) { // only take into account converged fits
304  result.Status = status;
305  result.Chi2 = fitter->fitS();
306  result.Prob = fitter->fitProb();
307  result.HadB = fitter->fittedHadB();
308  result.HadP = fitter->fittedHadP();
309  result.HadQ = fitter->fittedHadQ();
310  result.LepB = fitter->fittedLepB();
311  result.LepL = fitter->fittedLepton();
312  result.LepN = fitter->fittedNeutrino();
313  result.JetCombi = combi;
314 
315  FitResultList.push_back(result);
316  }
317 
318  }
319  if(useOnlyMatch_) break; // don't go through combinatorics if useOnlyMatch was chosen
320  next_permutation( combi.begin(), combi.end() );
321  }
322  if(useOnlyMatch_) break; // don't go through combinatorics if useOnlyMatch was chosen
323  }
324  while(stdcomb::next_combination( jetIndices.begin(), jetIndices.end(), combi.begin(), combi.end() ));
325 
326  // sort results w.r.t. chi2 values
327  FitResultList.sort();
328 
329  // -----------------------------------------------------
330  // feed out result
331  // starting with the JetComb having the smallest chi2
332  // -----------------------------------------------------
333 
334  if( (unsigned)FitResultList.size() < 1 ) { // in case no fit results were stored in the list (all fits aborted)
335  pPartonsHadP->push_back( fitter->fittedHadP() );
336  pPartonsHadQ->push_back( fitter->fittedHadQ() );
337  pPartonsHadB->push_back( fitter->fittedHadB() );
338  pPartonsLepB->push_back( fitter->fittedLepB() );
339  pLeptons ->push_back( fitter->fittedLepton() );
340  pNeutrinos ->push_back( fitter->fittedNeutrino() );
341  // indices referring to the jet combination
342  std::vector<int> invalidCombi;
343  for(unsigned int i = 0; i < nPartons; ++i)
344  invalidCombi.push_back( -1 );
345  pCombi->push_back( invalidCombi );
346  // chi2
347  pChi2->push_back( -1. );
348  // chi2 probability
349  pProb->push_back( -1. );
350  // status of the fitter
351  pStatus->push_back( -1 );
352  }
353  else {
354  unsigned int iComb = 0;
355  for(typename std::list<KinFitResult>::const_iterator result = FitResultList.begin(); result != FitResultList.end(); ++result) {
356  if(maxNComb_ >= 1 && iComb == (unsigned int) maxNComb_) break;
357  iComb++;
358  // partons
359  pPartonsHadP->push_back( result->HadP );
360  pPartonsHadQ->push_back( result->HadQ );
361  pPartonsHadB->push_back( result->HadB );
362  pPartonsLepB->push_back( result->LepB );
363  // lepton
364  pLeptons->push_back( result->LepL );
365  // neutrino
366  pNeutrinos->push_back( result->LepN );
367  // indices referring to the jet combination
368  pCombi->push_back( result->JetCombi );
369  // chi2
370  pChi2->push_back( result->Chi2 );
371  // chi2 probability
372  pProb->push_back( result->Prob );
373  // status of the fitter
374  pStatus->push_back( result->Status );
375  }
376  }
377  evt.put(std::move(pCombi));
378  evt.put(std::move(pPartonsHadP), "PartonsHadP");
379  evt.put(std::move(pPartonsHadQ), "PartonsHadQ");
380  evt.put(std::move(pPartonsHadB), "PartonsHadB");
381  evt.put(std::move(pPartonsLepB), "PartonsLepB");
382  evt.put(std::move(pLeptons ), "Leptons" );
383  evt.put(std::move(pNeutrinos ), "Neutrinos" );
384  evt.put(std::move(pChi2 ), "Chi2" );
385  evt.put(std::move(pProb ), "Prob" );
386  evt.put(std::move(pStatus ), "Status" );
387  evt.put(std::move(pJetsConsidered), "NumberOfConsideredJets");
388 }
389 
390 template<typename LeptonCollection>
392 {
394  switch(val){
398  default:
399  throw cms::Exception("Configuration")
400  << "Chosen jet parametrization is not supported: " << val << "\n";
401  break;
402  }
403  return result;
404 }
405 
406 template<typename LeptonCollection>
408 {
410  switch(val){
418  default:
419  throw cms::Exception("Configuration")
420  << "Chosen fit constraint is not supported: " << val << "\n";
421  break;
422  }
423  return result;
424 }
425 
426 template<typename LeptonCollection>
427 std::vector<TtSemiLepKinFitter::Constraint> TtSemiLepKinFitProducer<LeptonCollection>::constraints(std::vector<unsigned>& val)
428 {
429  std::vector<TtSemiLepKinFitter::Constraint> result;
430  for(unsigned i=0; i<val.size(); ++i){
431  result.push_back(constraint(val[i]));
432  }
433  return result;
434 }
435 
436 #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:125
double minBTagValueBJet_
min value of bTag for a b-jet
Param
supported parameterizations
Definition: TopKinFitter.h:22
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:517
bool exists(std::string const &parameterName) const
checks if a parameter exists
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
double fitProb() const
return fit probability
Definition: TopKinFitter.h:36
int maxNJets_
maximal number of jets (-1 possible to indicate &#39;all&#39;)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
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_
vector< PseudoJet > jets
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
bool doBTagging(bool &useBTag_, edm::Handle< std::vector< pat::Jet > > &jets, std::vector< int > &combi, std::string &bTagAlgo_, double &minBTagValueBJets_, double &maxBTagValueNonBJets_)
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:32
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:32
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:22
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
std::vector< edm::ParameterSet > lepResolutions_
def move(src, dest)
Definition: eostools.py:511
Constraint
supported constraints