CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TtSemiEvtSolutionMaker.cc
Go to the documentation of this file.
1 //
2 //
3 
5 
7 
9 
17 
18 #include <memory>
19 
22  // configurables
23  electronSrcToken_ = mayConsume<std::vector<pat::Electron> >(iConfig.getParameter<edm::InputTag>("electronSource"));
24  muonSrcToken_ = mayConsume<std::vector<pat::Muon> >(iConfig.getParameter<edm::InputTag>("muonSource"));
25  metSrcToken_ = consumes<std::vector<pat::MET> >(iConfig.getParameter<edm::InputTag>("metSource"));
26  jetSrcToken_ = consumes<std::vector<pat::Jet> >(iConfig.getParameter<edm::InputTag>("jetSource"));
27  leptonFlavour_ = iConfig.getParameter<std::string>("leptonFlavour");
28  jetCorrScheme_ = iConfig.getParameter<int>("jetCorrectionScheme");
29  nrCombJets_ = iConfig.getParameter<unsigned int>("nrCombJets");
30  doKinFit_ = iConfig.getParameter<bool>("doKinFit");
31  addLRSignalSel_ = iConfig.getParameter<bool>("addLRSignalSel");
32  lrSignalSelObs_ = iConfig.getParameter<std::vector<int> >("lrSignalSelObs");
33  lrSignalSelFile_ = iConfig.getParameter<std::string>("lrSignalSelFile");
34  addLRJetComb_ = iConfig.getParameter<bool>("addLRJetComb");
35  lrJetCombObs_ = iConfig.getParameter<std::vector<int> >("lrJetCombObs");
36  lrJetCombFile_ = iConfig.getParameter<std::string>("lrJetCombFile");
37  maxNrIter_ = iConfig.getParameter<int>("maxNrIter");
38  maxDeltaS_ = iConfig.getParameter<double>("maxDeltaS");
39  maxF_ = iConfig.getParameter<double>("maxF");
40  jetParam_ = iConfig.getParameter<int>("jetParametrisation");
41  lepParam_ = iConfig.getParameter<int>("lepParametrisation");
42  metParam_ = iConfig.getParameter<int>("metParametrisation");
43  constraints_ = iConfig.getParameter<std::vector<unsigned> >("constraints");
44  matchToGenEvt_ = iConfig.getParameter<bool>("matchToGenEvt");
45  matchingAlgo_ = iConfig.getParameter<int>("matchingAlgorithm");
46  useMaxDist_ = iConfig.getParameter<bool>("useMaximalDistance");
47  useDeltaR_ = iConfig.getParameter<bool>("useDeltaR");
48  maxDist_ = iConfig.getParameter<double>("maximalDistance");
49  genEvtToken_ = mayConsume<TtGenEvent>(edm::InputTag("genEvt"));
50 
51  // define kinfitter
52  if (doKinFit_) {
54  param(jetParam_), param(lepParam_), param(metParam_), maxNrIter_, maxDeltaS_, maxF_, constraints(constraints_));
55  }
56 
57  // define jet combinations related calculators
61  if (addLRJetComb_)
62  myLRJetCombCalc = new TtSemiLRJetCombCalc(edm::FileInPath(lrJetCombFile_).fullPath(), lrJetCombObs_);
63 
64  // instantiate signal selection calculator
65  if (addLRSignalSel_)
66  myLRSignalSelCalc = new TtSemiLRSignalSelCalc(edm::FileInPath(lrSignalSelFile_).fullPath(), lrSignalSelObs_);
67 
68  // define what will be produced
69  produces<std::vector<TtSemiEvtSolution> >();
70 }
71 
74  if (doKinFit_)
75  delete myKinFitter;
76  delete mySimpleBestJetComb;
79  if (addLRSignalSel_)
80  delete myLRSignalSelCalc;
81  if (addLRJetComb_)
82  delete myLRJetCombCalc;
83 }
84 
86  //
87  // TopObject Selection
88  //
89 
90  // select lepton (the TtLepton vectors are, for the moment, sorted on pT)
91  bool leptonFound = false;
93  if (leptonFlavour_ == "muon") {
94  iEvent.getByToken(muonSrcToken_, muons);
95  if (!muons->empty())
96  leptonFound = true;
97  }
99  if (leptonFlavour_ == "electron") {
100  iEvent.getByToken(electronSrcToken_, electrons);
101  if (!electrons->empty())
102  leptonFound = true;
103  }
104 
105  // select MET (TopMET vector is sorted on ET)
106  bool metFound = false;
108  iEvent.getByToken(metSrcToken_, mets);
109  if (!mets->empty())
110  metFound = true;
111 
112  // select Jets
113  bool jetsFound = false;
115  iEvent.getByToken(jetSrcToken_, jets);
116  if (jets->size() >= 4)
117  jetsFound = true;
118 
119  //
120  // Build Event solutions according to the ambiguity in the jet combination
121  //
122  std::vector<TtSemiEvtSolution>* evtsols = new std::vector<TtSemiEvtSolution>();
123  if (leptonFound && metFound && jetsFound) {
124  // protect against reading beyond array boundaries
125  unsigned int nrCombJets = nrCombJets_; // do not overwrite nrCombJets_
126  if (jets->size() < nrCombJets)
127  nrCombJets = jets->size();
128  // loop over all jets
129  for (unsigned int p = 0; p < nrCombJets; p++) {
130  for (unsigned int q = 0; q < nrCombJets; q++) {
131  for (unsigned int bh = 0; bh < nrCombJets; bh++) {
132  if (q > p && !(bh == p || bh == q)) {
133  for (unsigned int bl = 0; bl < nrCombJets; bl++) {
134  if (!(bl == p || bl == q || bl == bh)) {
135  TtSemiEvtSolution asol;
137  if (leptonFlavour_ == "muon")
138  asol.setMuon(muons, 0);
139  if (leptonFlavour_ == "electron")
140  asol.setElectron(electrons, 0);
141  asol.setNeutrino(mets, 0);
142  asol.setHadp(jets, p);
143  asol.setHadq(jets, q);
144  asol.setHadb(jets, bh);
145  asol.setLepb(jets, bl);
146  if (doKinFit_) {
147  asol = myKinFitter->addKinFitInfo(&asol);
148  // just to keep a record in the event (drop? -> present in provenance anyway...)
152  }
153  if (matchToGenEvt_) {
155  iEvent.getByToken(genEvtToken_, genEvt);
156  if (genEvt->numberOfBQuarks() ==
157  2 && // FIXME: in rare cases W->bc decay, resulting in a wrong filled genEvt leading to a segmentation fault
158  genEvt->numberOfLeptons() ==
159  1) { // FIXME: temporary solution to avoid crash in JetPartonMatching for non semi-leptonic events
160  asol.setGenEvt(genEvt);
161  }
162  }
163  // these lines calculate the observables to be used in the TtSemiSignalSelection LR
164  (*myLRSignalSelObservables)(asol, *jets);
165 
166  // if asked for, calculate with these observable values the LRvalue and
167  // (depending on the configuration) probability this event is signal
168  // FIXME: DO WE NEED TO DO THIS FOR EACH SOLUTION??? (S.L.20/8/07)
169  if (addLRSignalSel_)
170  (*myLRSignalSelCalc)(asol);
171 
172  // these lines calculate the observables to be used in the TtSemiJetCombination LR
173  //(*myLRJetCombObservables)(asol);
174 
175  (*myLRJetCombObservables)(asol, iEvent);
176 
177  // if asked for, calculate with these observable values the LRvalue and
178  // (depending on the configuration) probability a jet combination is correct
179  if (addLRJetComb_)
180  (*myLRJetCombCalc)(asol);
181 
182  //std::cout<<"SignalSelLRval = "<<asol.getLRSignalEvtLRval()<<" JetCombProb = "<<asol.getLRSignalEvtProb()<<std::endl;
183  //std::cout<<"JetCombLRval = "<<asol.getLRJetCombLRval()<<" JetCombProb = "<<asol.getLRJetCombProb()<<std::endl;
184 
185  // fill solution to vector
186  asol.setupHyp();
187  evtsols->push_back(asol);
188  }
189  }
190  }
191  }
192  }
193  }
194 
195  // if asked for, match the event solutions to the gen Event
196  if (matchToGenEvt_) {
197  int bestSolution = -999;
198  int bestSolutionChangeWQ = -999;
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  std::vector<const reco::Candidate*> quarks;
206  const reco::Candidate& genp = *(genEvt->hadronicDecayQuark());
207  const reco::Candidate& genq = *(genEvt->hadronicDecayQuarkBar());
208  const reco::Candidate& genbh = *(genEvt->hadronicDecayB());
209  const reco::Candidate& genbl = *(genEvt->leptonicDecayB());
210  quarks.push_back(&genp);
211  quarks.push_back(&genq);
212  quarks.push_back(&genbh);
213  quarks.push_back(&genbl);
214  std::vector<const reco::Candidate*> recjets;
215  for (size_t s = 0; s < evtsols->size(); s++) {
216  recjets.clear();
217  const reco::Candidate& jetp = (*evtsols)[s].getRecHadp();
218  const reco::Candidate& jetq = (*evtsols)[s].getRecHadq();
219  const reco::Candidate& jetbh = (*evtsols)[s].getRecHadb();
220  const reco::Candidate& jetbl = (*evtsols)[s].getRecLepb();
221  recjets.push_back(&jetp);
222  recjets.push_back(&jetq);
223  recjets.push_back(&jetbh);
224  recjets.push_back(&jetbl);
225  JetPartonMatching aMatch(quarks, recjets, matchingAlgo_, useMaxDist_, useDeltaR_, maxDist_);
226  (*evtsols)[s].setGenEvt(genEvt);
227  (*evtsols)[s].setMCBestSumAngles(aMatch.getSumDistances());
228  (*evtsols)[s].setMCBestAngleHadp(aMatch.getDistanceForParton(0));
229  (*evtsols)[s].setMCBestAngleHadq(aMatch.getDistanceForParton(1));
230  (*evtsols)[s].setMCBestAngleHadb(aMatch.getDistanceForParton(2));
231  (*evtsols)[s].setMCBestAngleLepb(aMatch.getDistanceForParton(3));
232  if (aMatch.getMatchForParton(2) == 2 && aMatch.getMatchForParton(3) == 3) {
233  if (aMatch.getMatchForParton(0) == 0 && aMatch.getMatchForParton(1) == 1) {
234  bestSolution = s;
235  bestSolutionChangeWQ = 0;
236  } else if (aMatch.getMatchForParton(0) == 1 && aMatch.getMatchForParton(1) == 0) {
237  bestSolution = s;
238  bestSolutionChangeWQ = 1;
239  }
240  }
241  }
242  }
243  for (size_t s = 0; s < evtsols->size(); s++) {
244  (*evtsols)[s].setMCBestJetComb(bestSolution);
245  (*evtsols)[s].setMCChangeWQ(bestSolutionChangeWQ);
246  }
247  }
248 
249  // add TtSemiSimpleBestJetComb to solutions
250  int simpleBestJetComb = (*mySimpleBestJetComb)(*evtsols);
251  for (size_t s = 0; s < evtsols->size(); s++)
252  (*evtsols)[s].setSimpleBestJetComb(simpleBestJetComb);
253 
254  // choose the best jet combination according to LR value
255  if (addLRJetComb_ && !evtsols->empty()) {
256  float bestLRVal = -1000000;
257  int bestSol = (*evtsols)[0].getLRBestJetComb(); // duplicate the default
258  for (size_t s = 0; s < evtsols->size(); s++) {
259  if ((*evtsols)[s].getLRJetCombLRval() > bestLRVal) {
260  bestLRVal = (*evtsols)[s].getLRJetCombLRval();
261  bestSol = s;
262  }
263  }
264  for (size_t s = 0; s < evtsols->size(); s++) {
265  (*evtsols)[s].setLRBestJetComb(bestSol);
266  }
267  }
268 
269  //store the vector of solutions to the event
270  std::unique_ptr<std::vector<TtSemiEvtSolution> > pOut(evtsols);
271  iEvent.put(std::move(pOut));
272 
273  } else {
274  /*
275  std::cout<<"No calibrated solutions built, because: ";
276  if(jets->size()<4) std::cout<<"nr sel jets < 4"<<std::endl;
277  if(leptonFlavour_ == "muon" && muons->size() == 0) std::cout<<"no good muon candidate"<<std::endl;
278  if(leptonFlavour_ == "electron" && electrons->size() == 0) std::cout<<"no good electron candidate"<<std::endl;
279  if(mets->size() == 0) std::cout<<"no MET reconstruction"<<std::endl;
280  */
281  // TtSemiEvtSolution asol;
282  // evtsols->push_back(asol);
283  std::unique_ptr<std::vector<TtSemiEvtSolution> > pOut(evtsols);
284  iEvent.put(std::move(pOut));
285  }
286 }
287 
290  switch (val) {
292  result = TtSemiLepKinFitter::kEMom;
293  break;
296  break;
299  break;
300  default:
301  throw cms::Exception("WrongConfig") << "Chosen jet parametrization is not supported: " << val << "\n";
302  break;
303  }
304  return result;
305 }
306 
309  switch (val) {
312  break;
315  break;
318  break;
321  break;
324  break;
325  default:
326  throw cms::Exception("WrongConfig") << "Chosen fit constraint is not supported: " << val << "\n";
327  break;
328  }
329  return result;
330 }
331 
332 std::vector<TtSemiLepKinFitter::Constraint> TtSemiEvtSolutionMaker::constraints(std::vector<unsigned>& val) {
333  std::vector<TtSemiLepKinFitter::Constraint> result;
334  for (unsigned i = 0; i < val.size(); ++i) {
335  result.push_back(constraint(val[i]));
336  }
337  return result;
338 }
void setLepb(const edm::Handle< std::vector< pat::Jet > > &jet, int i)
TtSemiLRJetCombCalc * myLRJetCombCalc
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::EDGetTokenT< std::vector< pat::Electron > > electronSrcToken_
Param
supported parameterizations
Definition: TopKinFitter.h:20
TtSemiLepKinFitter * myKinFitter
TtSemiLRJetCombObservables * myLRJetCombObservables
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
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
tuple result
Definition: mps_fire.py:311
int iEvent
Definition: GenABIO.cc:224
void setJetCorrectionScheme(int scheme)
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
vector< PseudoJet > jets
def move
Definition: eostools.py:511
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...
tuple genp
produce generated paricles in acceptance #
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_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
TtSemiLepKinFitter::Constraint constraint(unsigned)
int getMatchForParton(const unsigned int part, const unsigned int comb=0)
void setNeutrinoParametrisation(int mp)
tuple muons
Definition: patZpeak.py:39
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)
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)