CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
19 
20  private:
21  // produce
22  virtual void produce(edm::Event&, const edm::EventSetup&);
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 
42  std::string bTagAlgo_;
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_;
63 
64  std::vector<unsigned> constraints_;
65  double mW_;
66  double mTop_;
67 
69 
70  struct KinFitResult {
71  int Status;
72  double Chi2;
73  double Prob;
80  std::vector<int> JetCombi;
81  bool operator< (const KinFitResult& rhs) { return Chi2 < rhs.Chi2; };
82  };
83 };
84 
85 template<typename LeptonCollection>
87  jets_ (cfg.getParameter<edm::InputTag>("jets")),
88  leps_ (cfg.getParameter<edm::InputTag>("leps")),
89  mets_ (cfg.getParameter<edm::InputTag>("mets")),
90  match_ (cfg.getParameter<edm::InputTag>("match")),
91  useOnlyMatch_ (cfg.getParameter<bool> ("useOnlyMatch" )),
92  bTagAlgo_ (cfg.getParameter<std::string> ("bTagAlgo" )),
93  minBTagValueBJet_ (cfg.getParameter<double> ("minBDiscBJets" )),
94  maxBTagValueNonBJet_(cfg.getParameter<double> ("maxBDiscLightJets" )),
95  useBTag_ (cfg.getParameter<bool> ("useBTagging" )),
96  maxNJets_ (cfg.getParameter<int> ("maxNJets" )),
97  maxNComb_ (cfg.getParameter<int> ("maxNComb" )),
98  maxNrIter_ (cfg.getParameter<unsigned> ("maxNrIter" )),
99  maxDeltaS_ (cfg.getParameter<double> ("maxDeltaS" )),
100  maxF_ (cfg.getParameter<double> ("maxF" )),
101  jetParam_ (cfg.getParameter<unsigned> ("jetParametrisation")),
102  lepParam_ (cfg.getParameter<unsigned> ("lepParametrisation")),
103  metParam_ (cfg.getParameter<unsigned> ("metParametrisation")),
104  constraints_ (cfg.getParameter<std::vector<unsigned> >("constraints")),
105  mW_ (cfg.getParameter<double> ("mW" )),
106  mTop_ (cfg.getParameter<double> ("mTop" ))
107 {
110 
111  produces< std::vector<pat::Particle> >("PartonsHadP");
112  produces< std::vector<pat::Particle> >("PartonsHadQ");
113  produces< std::vector<pat::Particle> >("PartonsHadB");
114  produces< std::vector<pat::Particle> >("PartonsLepB");
115  produces< std::vector<pat::Particle> >("Leptons");
116  produces< std::vector<pat::Particle> >("Neutrinos");
117 
118  produces< std::vector<std::vector<int> > >();
119  produces< std::vector<double> >("Chi2");
120  produces< std::vector<double> >("Prob");
121  produces< std::vector<int> >("Status");
122 }
123 
124 template<typename LeptonCollection>
126 {
127  delete fitter;
128 }
129 
130 template<typename LeptonCollection>
131 bool TtSemiLepKinFitProducer<LeptonCollection>::doBTagging(bool& useBTag_, edm::Handle<std::vector<pat::Jet> >& jets, std::vector<int>& combi,
132  std::string& bTagAlgo_, double& minBTagValueBJet_, double& maxBTagValueNonBJet_){
133 
134  if( !useBTag_ ) {
135  return true;
136  }
137  if( useBTag_ &&
138  (*jets)[combi[TtSemiLepEvtPartons::HadB ]].bDiscriminator(bTagAlgo_) >= minBTagValueBJet_ &&
139  (*jets)[combi[TtSemiLepEvtPartons::LepB ]].bDiscriminator(bTagAlgo_) >= minBTagValueBJet_ &&
140  (*jets)[combi[TtSemiLepEvtPartons::LightQ ]].bDiscriminator(bTagAlgo_) < maxBTagValueNonBJet_ &&
141  (*jets)[combi[TtSemiLepEvtPartons::LightQBar]].bDiscriminator(bTagAlgo_) < maxBTagValueNonBJet_ ) {
142  return true;
143  }
144  else{
145  return false;
146  }
147 }
148 
149 template<typename LeptonCollection>
151 {
152  std::auto_ptr< std::vector<pat::Particle> > pPartonsHadP( new std::vector<pat::Particle> );
153  std::auto_ptr< std::vector<pat::Particle> > pPartonsHadQ( new std::vector<pat::Particle> );
154  std::auto_ptr< std::vector<pat::Particle> > pPartonsHadB( new std::vector<pat::Particle> );
155  std::auto_ptr< std::vector<pat::Particle> > pPartonsLepB( new std::vector<pat::Particle> );
156  std::auto_ptr< std::vector<pat::Particle> > pLeptons ( new std::vector<pat::Particle> );
157  std::auto_ptr< std::vector<pat::Particle> > pNeutrinos ( new std::vector<pat::Particle> );
158 
159  std::auto_ptr< std::vector<std::vector<int> > > pCombi ( new std::vector<std::vector<int> > );
160  std::auto_ptr< std::vector<double> > pChi2 ( new std::vector<double> );
161  std::auto_ptr< std::vector<double> > pProb ( new std::vector<double> );
162  std::auto_ptr< std::vector<int> > pStatus( new std::vector<int> );
163 
165  evt.getByLabel(jets_, jets);
166 
168  evt.getByLabel(mets_, mets);
169 
171  evt.getByLabel(leps_, leps);
172 
173  unsigned int nPartons = 4;
174 
175  std::vector<int> match;
176  bool invalidMatch = false;
177  if(useOnlyMatch_) {
179  evt.getByLabel(match_, matchHandle);
180  match = *(matchHandle->begin());
181  // check if match is valid
182  if(match.size()!=nPartons) invalidMatch=true;
183  else {
184  for(unsigned int idx=0; idx<match.size(); ++idx) {
185  if(match[idx]<0 || match[idx]>=(int)jets->size()) {
186  invalidMatch=true;
187  break;
188  }
189  }
190  }
191  }
192 
193  // -----------------------------------------------------
194  // skip events with no appropriate lepton candidate in
195  // or empty MET or less jets than partons or invalid match
196  // -----------------------------------------------------
197 
198  if( leps->empty() || mets->empty() || jets->size()<nPartons || invalidMatch ) {
199  // the kinFit getters return empty objects here
200  pPartonsHadP->push_back( fitter->fittedHadP() );
201  pPartonsHadQ->push_back( fitter->fittedHadQ() );
202  pPartonsHadB->push_back( fitter->fittedHadB() );
203  pPartonsLepB->push_back( fitter->fittedLepB() );
204  pLeptons ->push_back( fitter->fittedLepton() );
205  pNeutrinos ->push_back( fitter->fittedNeutrino() );
206  // indices referring to the jet combination
207  std::vector<int> invalidCombi;
208  for(unsigned int i = 0; i < nPartons; ++i)
209  invalidCombi.push_back( -1 );
210  pCombi->push_back( invalidCombi );
211  // chi2
212  pChi2->push_back( -1. );
213  // chi2 probability
214  pProb->push_back( -1. );
215  // status of the fitter
216  pStatus->push_back( -1 );
217  // feed out all products
218  evt.put(pCombi);
219  evt.put(pPartonsHadP, "PartonsHadP");
220  evt.put(pPartonsHadQ, "PartonsHadQ");
221  evt.put(pPartonsHadB, "PartonsHadB");
222  evt.put(pPartonsLepB, "PartonsLepB");
223  evt.put(pLeptons , "Leptons" );
224  evt.put(pNeutrinos , "Neutrinos" );
225  evt.put(pChi2 , "Chi2" );
226  evt.put(pProb , "Prob" );
227  evt.put(pStatus , "Status" );
228  return;
229  }
230 
231  // -----------------------------------------------------
232  // analyze different jet combinations using the KinFitter
233  // (or only a given jet combination if useOnlyMatch=true)
234  // -----------------------------------------------------
235 
236  std::vector<int> jetIndices;
237  if(!useOnlyMatch_) {
238  for(unsigned int i=0; i<jets->size(); ++i){
239  if(maxNJets_ >= (int) nPartons && maxNJets_ == (int) i) break;
240  jetIndices.push_back(i);
241  }
242  }
243 
244  std::vector<int> combi;
245  for(unsigned int i=0; i<nPartons; ++i) {
246  if(useOnlyMatch_) combi.push_back( match[i] );
247  else combi.push_back(i);
248  }
249 
250  std::list<KinFitResult> FitResultList;
251 
252  do{
253  for(int cnt = 0; cnt < TMath::Factorial( combi.size() ); ++cnt){
254  // take into account indistinguishability of the two jets from the hadr. W decay,
255  // reduces combinatorics by a factor of 2
257  || useOnlyMatch_ ) && doBTagging(useBTag_, jets, combi, bTagAlgo_, minBTagValueBJet_, maxBTagValueNonBJet_) ){
258 
259  std::vector<pat::Jet> jetCombi;
260  jetCombi.resize(nPartons);
261  jetCombi[TtSemiLepEvtPartons::LightQ ] = (*jets)[combi[TtSemiLepEvtPartons::LightQ ]];
263  jetCombi[TtSemiLepEvtPartons::HadB ] = (*jets)[combi[TtSemiLepEvtPartons::HadB ]];
264  jetCombi[TtSemiLepEvtPartons::LepB ] = (*jets)[combi[TtSemiLepEvtPartons::LepB ]];
265 
266  // do the kinematic fit
267  int status = fitter->fit(jetCombi, (*leps)[0], (*mets)[0]);
268 
269  if( status == 0 ) { // only take into account converged fits
271  result.Status = status;
272  result.Chi2 = fitter->fitS();
273  result.Prob = fitter->fitProb();
274  result.HadB = fitter->fittedHadB();
275  result.HadP = fitter->fittedHadP();
276  result.HadQ = fitter->fittedHadQ();
277  result.LepB = fitter->fittedLepB();
278  result.LepL = fitter->fittedLepton();
279  result.LepN = fitter->fittedNeutrino();
280  result.JetCombi = combi;
281 
282  FitResultList.push_back(result);
283  }
284 
285  }
286  if(useOnlyMatch_) break; // don't go through combinatorics if useOnlyMatch was chosen
287  next_permutation( combi.begin(), combi.end() );
288  }
289  if(useOnlyMatch_) break; // don't go through combinatorics if useOnlyMatch was chosen
290  }
291  while(stdcomb::next_combination( jetIndices.begin(), jetIndices.end(), combi.begin(), combi.end() ));
292 
293  // sort results w.r.t. chi2 values
294  FitResultList.sort();
295 
296  // -----------------------------------------------------
297  // feed out result
298  // starting with the JetComb having the smallest chi2
299  // -----------------------------------------------------
300 
301  if( FitResultList.size() < 1 ) { // in case no fit results were stored in the list (all fits aborted)
302  pPartonsHadP->push_back( fitter->fittedHadP() );
303  pPartonsHadQ->push_back( fitter->fittedHadQ() );
304  pPartonsHadB->push_back( fitter->fittedHadB() );
305  pPartonsLepB->push_back( fitter->fittedLepB() );
306  pLeptons ->push_back( fitter->fittedLepton() );
307  pNeutrinos ->push_back( fitter->fittedNeutrino() );
308  // indices referring to the jet combination
309  std::vector<int> invalidCombi;
310  for(unsigned int i = 0; i < nPartons; ++i)
311  invalidCombi.push_back( -1 );
312  pCombi->push_back( invalidCombi );
313  // chi2
314  pChi2->push_back( -1. );
315  // chi2 probability
316  pProb->push_back( -1. );
317  // status of the fitter
318  pStatus->push_back( -1 );
319  }
320  else {
321  unsigned int iComb = 0;
322  for(typename std::list<KinFitResult>::const_iterator result = FitResultList.begin(); result != FitResultList.end(); ++result) {
323  if(maxNComb_ >= 1 && iComb == (unsigned int) maxNComb_) break;
324  iComb++;
325  // partons
326  pPartonsHadP->push_back( result->HadP );
327  pPartonsHadQ->push_back( result->HadQ );
328  pPartonsHadB->push_back( result->HadB );
329  pPartonsLepB->push_back( result->LepB );
330  // lepton
331  pLeptons->push_back( result->LepL );
332  // neutrino
333  pNeutrinos->push_back( result->LepN );
334  // indices referring to the jet combination
335  pCombi->push_back( result->JetCombi );
336  // chi2
337  pChi2->push_back( result->Chi2 );
338  // chi2 probability
339  pProb->push_back( result->Prob );
340  // status of the fitter
341  pStatus->push_back( result->Status );
342  }
343  }
344  evt.put(pCombi);
345  evt.put(pPartonsHadP, "PartonsHadP");
346  evt.put(pPartonsHadQ, "PartonsHadQ");
347  evt.put(pPartonsHadB, "PartonsHadB");
348  evt.put(pPartonsLepB, "PartonsLepB");
349  evt.put(pLeptons , "Leptons" );
350  evt.put(pNeutrinos , "Neutrinos" );
351  evt.put(pChi2 , "Chi2" );
352  evt.put(pProb , "Prob" );
353  evt.put(pStatus , "Status" );
354 }
355 
356 template<typename LeptonCollection>
358 {
360  switch(val){
364  default:
365  throw cms::Exception("WrongConfig")
366  << "Chosen jet parametrization is not supported: " << val << "\n";
367  break;
368  }
369  return result;
370 }
371 
372 template<typename LeptonCollection>
374 {
376  switch(val){
383  default:
384  throw cms::Exception("WrongConfig")
385  << "Chosen fit constraint is not supported: " << val << "\n";
386  break;
387  }
388  return result;
389 }
390 
391 template<typename LeptonCollection>
392 std::vector<TtSemiLepKinFitter::Constraint> TtSemiLepKinFitProducer<LeptonCollection>::constraints(std::vector<unsigned>& val)
393 {
394  std::vector<TtSemiLepKinFitter::Constraint> result;
395  for(unsigned i=0; i<val.size(); ++i){
396  result.push_back(constraint(val[i]));
397  }
398  return result;
399 }
400 
401 #endif
double maxBTagValueNonBJet_
max value of bTag for a non-b-jet
int i
Definition: DBlmapReader.cc:9
std::string bTagAlgo_
input tag for b-tagging algorithm
static const unsigned int nPartons
double minBTagValueBJet_
min value of bTag for a b-jet
Param
supported parameterizations
Definition: TopKinFitter.h:22
std::vector< TtSemiLepKinFitter::Constraint > constraints(std::vector< unsigned > &)
virtual void produce(edm::Event &, const edm::EventSetup &)
int maxNJets_
maximal number of jets (-1 possible to indicate &#39;all&#39;)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
tuple result
Definition: query.py:137
std::vector< unsigned > constraints_
unsigned int maxNrIter_
maximal number of iterations to be performed for the fit
bool operator<(const KinFitResult &rhs)
bool doBTagging(bool &useBTag_, edm::Handle< std::vector< pat::Jet > > &jets, std::vector< int > &combi, std::string &bTagAlgo_, double &minBTagValueBJets_, double &maxBTagValueNonBJets_)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
int maxNComb_
maximal number of combinations to be written to the event
double maxF_
maximal deviation for contstraints
Analysis-level particle class.
Definition: Particle.h:34
TtSemiLepKinFitter::Constraint constraint(unsigned)
bool useOnlyMatch_
switch to use only a combination given by another hypothesis
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:6
TtSemiLepKinFitProducer(const edm::ParameterSet &)
double maxDeltaS_
maximal chi2 equivalent
tuple status
Definition: ntuplemaker.py:245
Definition: Chi2.h:17
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="")
Constraint
supported constraints