CMS 3D CMS Logo

TtSemiEvtSolutionMaker.cc
Go to the documentation of this file.
6 
8 
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
23 public:
24  explicit TtSemiEvtSolutionMaker(const edm::ParameterSet& iConfig);
25  ~TtSemiEvtSolutionMaker() override;
26 
27  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
28 
29  // convert unsigned to Param
31  // convert unsigned to Param
33  // convert unsigned to Param
34  std::vector<TtSemiLepKinFitter::Constraint> constraints(std::vector<unsigned>&);
35 
36 private:
37  // configurables
44  unsigned int nrCombJets_;
49  double maxDist_;
51  double maxDeltaS_, maxF_;
53  std::vector<int> lrSignalSelObs_, lrJetCombObs_;
54  std::vector<unsigned> constraints_;
56  // tools
63 };
64 
67  // configurables
68  electronSrcToken_ = mayConsume<std::vector<pat::Electron> >(iConfig.getParameter<edm::InputTag>("electronSource"));
69  muonSrcToken_ = mayConsume<std::vector<pat::Muon> >(iConfig.getParameter<edm::InputTag>("muonSource"));
70  metSrcToken_ = consumes<std::vector<pat::MET> >(iConfig.getParameter<edm::InputTag>("metSource"));
71  jetSrcToken_ = consumes<std::vector<pat::Jet> >(iConfig.getParameter<edm::InputTag>("jetSource"));
72  leptonFlavour_ = iConfig.getParameter<std::string>("leptonFlavour");
73  jetCorrScheme_ = iConfig.getParameter<int>("jetCorrectionScheme");
74  nrCombJets_ = iConfig.getParameter<unsigned int>("nrCombJets");
75  doKinFit_ = iConfig.getParameter<bool>("doKinFit");
76  addLRSignalSel_ = iConfig.getParameter<bool>("addLRSignalSel");
77  lrSignalSelObs_ = iConfig.getParameter<std::vector<int> >("lrSignalSelObs");
78  lrSignalSelFile_ = iConfig.getParameter<std::string>("lrSignalSelFile");
79  addLRJetComb_ = iConfig.getParameter<bool>("addLRJetComb");
80  lrJetCombObs_ = iConfig.getParameter<std::vector<int> >("lrJetCombObs");
81  lrJetCombFile_ = iConfig.getParameter<std::string>("lrJetCombFile");
82  maxNrIter_ = iConfig.getParameter<int>("maxNrIter");
83  maxDeltaS_ = iConfig.getParameter<double>("maxDeltaS");
84  maxF_ = iConfig.getParameter<double>("maxF");
85  jetParam_ = iConfig.getParameter<int>("jetParametrisation");
86  lepParam_ = iConfig.getParameter<int>("lepParametrisation");
87  metParam_ = iConfig.getParameter<int>("metParametrisation");
88  constraints_ = iConfig.getParameter<std::vector<unsigned> >("constraints");
89  matchToGenEvt_ = iConfig.getParameter<bool>("matchToGenEvt");
90  matchingAlgo_ = iConfig.getParameter<int>("matchingAlgorithm");
91  useMaxDist_ = iConfig.getParameter<bool>("useMaximalDistance");
92  useDeltaR_ = iConfig.getParameter<bool>("useDeltaR");
93  maxDist_ = iConfig.getParameter<double>("maximalDistance");
94  genEvtToken_ = mayConsume<TtGenEvent>(edm::InputTag("genEvt"));
95 
96  // define kinfitter
97  if (doKinFit_) {
100  }
101 
102  // define jet combinations related calculators
106  if (addLRJetComb_)
108 
109  // instantiate signal selection calculator
110  if (addLRSignalSel_)
112 
113  // define what will be produced
114  produces<std::vector<TtSemiEvtSolution> >();
115 }
116 
119  if (doKinFit_)
120  delete myKinFitter;
121  delete mySimpleBestJetComb;
123  delete myLRJetCombObservables;
124  if (addLRSignalSel_)
125  delete myLRSignalSelCalc;
126  if (addLRJetComb_)
127  delete myLRJetCombCalc;
128 }
129 
131  //
132  // TopObject Selection
133  //
134 
135  // select lepton (the TtLepton vectors are, for the moment, sorted on pT)
136  bool leptonFound = false;
138  if (leptonFlavour_ == "muon") {
139  iEvent.getByToken(muonSrcToken_, muons);
140  if (!muons->empty())
141  leptonFound = true;
142  }
144  if (leptonFlavour_ == "electron") {
145  iEvent.getByToken(electronSrcToken_, electrons);
146  if (!electrons->empty())
147  leptonFound = true;
148  }
149 
150  // select MET (TopMET vector is sorted on ET)
151  bool metFound = false;
153  iEvent.getByToken(metSrcToken_, mets);
154  if (!mets->empty())
155  metFound = true;
156 
157  // select Jets
158  bool jetsFound = false;
160  iEvent.getByToken(jetSrcToken_, jets);
161  if (jets->size() >= 4)
162  jetsFound = true;
163 
164  //
165  // Build Event solutions according to the ambiguity in the jet combination
166  //
167  std::vector<TtSemiEvtSolution>* evtsols = new std::vector<TtSemiEvtSolution>();
168  if (leptonFound && metFound && jetsFound) {
169  // protect against reading beyond array boundaries
170  unsigned int nrCombJets = nrCombJets_; // do not overwrite nrCombJets_
171  if (jets->size() < nrCombJets)
172  nrCombJets = jets->size();
173  // loop over all jets
174  for (unsigned int p = 0; p < nrCombJets; p++) {
175  for (unsigned int q = 0; q < nrCombJets; q++) {
176  for (unsigned int bh = 0; bh < nrCombJets; bh++) {
177  if (q > p && !(bh == p || bh == q)) {
178  for (unsigned int bl = 0; bl < nrCombJets; bl++) {
179  if (!(bl == p || bl == q || bl == bh)) {
180  TtSemiEvtSolution asol;
182  if (leptonFlavour_ == "muon")
183  asol.setMuon(muons, 0);
184  if (leptonFlavour_ == "electron")
185  asol.setElectron(electrons, 0);
186  asol.setNeutrino(mets, 0);
187  asol.setHadp(jets, p);
188  asol.setHadq(jets, q);
189  asol.setHadb(jets, bh);
190  asol.setLepb(jets, bl);
191  if (doKinFit_) {
192  asol = myKinFitter->addKinFitInfo(&asol);
193  // just to keep a record in the event (drop? -> present in provenance anyway...)
197  }
198  if (matchToGenEvt_) {
200  iEvent.getByToken(genEvtToken_, genEvt);
201  if (genEvt->numberOfBQuarks() ==
202  2 && // FIXME: in rare cases W->bc decay, resulting in a wrong filled genEvt leading to a segmentation fault
203  genEvt->numberOfLeptons() ==
204  1) { // FIXME: temporary solution to avoid crash in JetPartonMatching for non semi-leptonic events
205  asol.setGenEvt(genEvt);
206  }
207  }
208  // these lines calculate the observables to be used in the TtSemiSignalSelection LR
209  (*myLRSignalSelObservables)(asol, *jets);
210 
211  // if asked for, calculate with these observable values the LRvalue and
212  // (depending on the configuration) probability this event is signal
213  // FIXME: DO WE NEED TO DO THIS FOR EACH SOLUTION??? (S.L.20/8/07)
214  if (addLRSignalSel_)
215  (*myLRSignalSelCalc)(asol);
216 
217  // these lines calculate the observables to be used in the TtSemiJetCombination LR
218  //(*myLRJetCombObservables)(asol);
219 
220  (*myLRJetCombObservables)(asol, iEvent);
221 
222  // if asked for, calculate with these observable values the LRvalue and
223  // (depending on the configuration) probability a jet combination is correct
224  if (addLRJetComb_)
225  (*myLRJetCombCalc)(asol);
226 
227  //std::cout<<"SignalSelLRval = "<<asol.getLRSignalEvtLRval()<<" JetCombProb = "<<asol.getLRSignalEvtProb()<<std::endl;
228  //std::cout<<"JetCombLRval = "<<asol.getLRJetCombLRval()<<" JetCombProb = "<<asol.getLRJetCombProb()<<std::endl;
229 
230  // fill solution to vector
231  asol.setupHyp();
232  evtsols->push_back(asol);
233  }
234  }
235  }
236  }
237  }
238  }
239 
240  // if asked for, match the event solutions to the gen Event
241  if (matchToGenEvt_) {
242  int bestSolution = -999;
243  int bestSolutionChangeWQ = -999;
245  iEvent.getByToken(genEvtToken_, genEvt);
246  if (genEvt->numberOfBQuarks() ==
247  2 && // FIXME: in rare cases W->bc decay, resulting in a wrong filled genEvt leading to a segmentation fault
248  genEvt->numberOfLeptons() ==
249  1) { // FIXME: temporary solution to avoid crash in JetPartonMatching for non semi-leptonic events
250  std::vector<const reco::Candidate*> quarks;
251  const reco::Candidate& genp = *(genEvt->hadronicDecayQuark());
252  const reco::Candidate& genq = *(genEvt->hadronicDecayQuarkBar());
253  const reco::Candidate& genbh = *(genEvt->hadronicDecayB());
254  const reco::Candidate& genbl = *(genEvt->leptonicDecayB());
255  quarks.push_back(&genp);
256  quarks.push_back(&genq);
257  quarks.push_back(&genbh);
258  quarks.push_back(&genbl);
259  std::vector<const reco::Candidate*> recjets;
260  for (size_t s = 0; s < evtsols->size(); s++) {
261  recjets.clear();
262  const reco::Candidate& jetp = (*evtsols)[s].getRecHadp();
263  const reco::Candidate& jetq = (*evtsols)[s].getRecHadq();
264  const reco::Candidate& jetbh = (*evtsols)[s].getRecHadb();
265  const reco::Candidate& jetbl = (*evtsols)[s].getRecLepb();
266  recjets.push_back(&jetp);
267  recjets.push_back(&jetq);
268  recjets.push_back(&jetbh);
269  recjets.push_back(&jetbl);
270  JetPartonMatching aMatch(quarks, recjets, matchingAlgo_, useMaxDist_, useDeltaR_, maxDist_);
271  (*evtsols)[s].setGenEvt(genEvt);
272  (*evtsols)[s].setMCBestSumAngles(aMatch.getSumDistances());
273  (*evtsols)[s].setMCBestAngleHadp(aMatch.getDistanceForParton(0));
274  (*evtsols)[s].setMCBestAngleHadq(aMatch.getDistanceForParton(1));
275  (*evtsols)[s].setMCBestAngleHadb(aMatch.getDistanceForParton(2));
276  (*evtsols)[s].setMCBestAngleLepb(aMatch.getDistanceForParton(3));
277  if (aMatch.getMatchForParton(2) == 2 && aMatch.getMatchForParton(3) == 3) {
278  if (aMatch.getMatchForParton(0) == 0 && aMatch.getMatchForParton(1) == 1) {
279  bestSolution = s;
280  bestSolutionChangeWQ = 0;
281  } else if (aMatch.getMatchForParton(0) == 1 && aMatch.getMatchForParton(1) == 0) {
282  bestSolution = s;
283  bestSolutionChangeWQ = 1;
284  }
285  }
286  }
287  }
288  for (size_t s = 0; s < evtsols->size(); s++) {
289  (*evtsols)[s].setMCBestJetComb(bestSolution);
290  (*evtsols)[s].setMCChangeWQ(bestSolutionChangeWQ);
291  }
292  }
293 
294  // add TtSemiSimpleBestJetComb to solutions
295  int simpleBestJetComb = (*mySimpleBestJetComb)(*evtsols);
296  for (size_t s = 0; s < evtsols->size(); s++)
297  (*evtsols)[s].setSimpleBestJetComb(simpleBestJetComb);
298 
299  // choose the best jet combination according to LR value
300  if (addLRJetComb_ && !evtsols->empty()) {
301  float bestLRVal = -1000000;
302  int bestSol = (*evtsols)[0].getLRBestJetComb(); // duplicate the default
303  for (size_t s = 0; s < evtsols->size(); s++) {
304  if ((*evtsols)[s].getLRJetCombLRval() > bestLRVal) {
305  bestLRVal = (*evtsols)[s].getLRJetCombLRval();
306  bestSol = s;
307  }
308  }
309  for (size_t s = 0; s < evtsols->size(); s++) {
310  (*evtsols)[s].setLRBestJetComb(bestSol);
311  }
312  }
313 
314  //store the vector of solutions to the event
315  std::unique_ptr<std::vector<TtSemiEvtSolution> > pOut(evtsols);
316  iEvent.put(std::move(pOut));
317 
318  } else {
319  /*
320  std::cout<<"No calibrated solutions built, because: ";
321  if(jets->size()<4) std::cout<<"nr sel jets < 4"<<std::endl;
322  if(leptonFlavour_ == "muon" && muons->size() == 0) std::cout<<"no good muon candidate"<<std::endl;
323  if(leptonFlavour_ == "electron" && electrons->size() == 0) std::cout<<"no good electron candidate"<<std::endl;
324  if(mets->size() == 0) std::cout<<"no MET reconstruction"<<std::endl;
325  */
326  // TtSemiEvtSolution asol;
327  // evtsols->push_back(asol);
328  std::unique_ptr<std::vector<TtSemiEvtSolution> > pOut(evtsols);
329  iEvent.put(std::move(pOut));
330  }
331 }
332 
335  switch (val) {
338  break;
341  break;
344  break;
345  default:
346  throw cms::Exception("WrongConfig") << "Chosen jet parametrization is not supported: " << val << "\n";
347  break;
348  }
349  return result;
350 }
351 
354  switch (val) {
357  break;
360  break;
363  break;
366  break;
369  break;
370  default:
371  throw cms::Exception("WrongConfig") << "Chosen fit constraint is not supported: " << val << "\n";
372  break;
373  }
374  return result;
375 }
376 
377 std::vector<TtSemiLepKinFitter::Constraint> TtSemiEvtSolutionMaker::constraints(std::vector<unsigned>& val) {
378  std::vector<TtSemiLepKinFitter::Constraint> result;
379  for (unsigned i = 0; i < val.size(); ++i) {
380  result.push_back(constraint(val[i]));
381  }
382  return result;
383 }
384 
void setLepb(const edm::Handle< std::vector< pat::Jet > > &jet, int i)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
TtSemiLRJetCombCalc * myLRJetCombCalc
edm::EDGetTokenT< std::vector< pat::Electron > > electronSrcToken_
Param
supported parameterizations
Definition: TopKinFitter.h:22
genp
produce generated paricles in acceptance #
TtSemiLepKinFitter * myKinFitter
TtSemiLRJetCombObservables * myLRJetCombObservables
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TtSemiSimpleBestJetComb * mySimpleBestJetComb
std::vector< unsigned > constraints_
void setGenEvt(const edm::Handle< TtGenEvent > &aGenEvt)
TtSemiLepKinFitter::Param param(unsigned)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
edm::EDGetTokenT< std::vector< pat::Muon > > muonSrcToken_
Simple method to get the correct jet combination in semileptonic ttbar events.
void setHadp(const edm::Handle< std::vector< pat::Jet > > &jet, int i)
void setLeptonParametrisation(int lp)
void setMuon(const edm::Handle< std::vector< pat::Muon > > &muon, int i)
Class to calculate the jet combination LR value and purity from a root-file with fit functions...
~TtSemiEvtSolutionMaker() override
destructor
int iEvent
Definition: GenABIO.cc:224
void setJetCorrectionScheme(int scheme)
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
edm::EDGetTokenT< std::vector< pat::MET > > metSrcToken_
void setJetParametrisation(int jp)
Class to calculate the jet combination LR value and purity from a root-file with fit functions...
TtSemiEvtSolutionMaker(const edm::ParameterSet &iConfig)
constructor
void setElectron(const edm::Handle< std::vector< pat::Electron > > &elec, int i)
TtSemiEvtSolution addKinFitInfo(TtSemiEvtSolution *asol)
add kin fit information to the old event solution (in for legacy reasons)
Steering class for the overall top-lepton likelihood.
edm::EDGetTokenT< std::vector< pat::Jet > > jetSrcToken_
TtSemiLepKinFitter::Constraint constraint(unsigned)
int getMatchForParton(const unsigned int part, const unsigned int comb=0)
void setNeutrinoParametrisation(int mp)
edm::EDGetTokenT< TtGenEvent > genEvtToken_
void setNeutrino(const edm::Handle< std::vector< pat::MET > > &met, int i)
std::vector< int > lrSignalSelObs_
std::vector< TtSemiLepKinFitter::Constraint > constraints(std::vector< unsigned > &)
TtSemiLRSignalSelObservables * myLRSignalSelObservables
double getSumDistances(const unsigned int comb=0)
void setHadb(const edm::Handle< std::vector< pat::Jet > > &jet, int i)
def move(src, dest)
Definition: eostools.py:511
std::vector< int > lrJetCombObs_
void setHadq(const edm::Handle< std::vector< pat::Jet > > &jet, int i)
Constraint
supported constraints
TtSemiLRSignalSelCalc * myLRSignalSelCalc
double getDistanceForParton(const unsigned int part, const unsigned int comb=0)