CMS 3D CMS Logo

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