CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EmbeddingKineReweightNtupleProducer.cc
Go to the documentation of this file.
2 
5 
8 
17 
19 
20 #include <TMath.h>
21 #include <TString.h>
22 
25 
27 
29  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
30  ntuple_(0)
31 {
32  srcGenDiTaus_ = cfg.getParameter<edm::InputTag>("srcGenDiTaus");
33 
34  srcGenParticles_ = cfg.getParameter<edm::InputTag>("srcGenParticles");
35  srcSelectedMuons_ = cfg.getParameter<edm::InputTag>("srcSelectedMuons");
36 
37  srcRecLeg1_ = cfg.getParameter<edm::InputTag>("srcRecLeg1");
38  srcRecLeg2_ = cfg.getParameter<edm::InputTag>("srcRecLeg2");
39 
40  srcRecJets_ = cfg.getParameter<edm::InputTag>("srcRecJets");
41 
42  srcGenCaloMEt_ = cfg.getParameter<edm::InputTag>("srcGenCaloMEt");
43  srcRecCaloMEt_ = cfg.getParameter<edm::InputTag>("srcRecCaloMEt");
44  srcGenPFMEt_ = cfg.getParameter<edm::InputTag>("srcGenPFMEt");
45  srcRecPFMEt_ = cfg.getParameter<edm::InputTag>("srcRecPFMEt");
46 
47  srcWeights_ = cfg.getParameter<vInputTag>("srcWeights");
48  srcGenFilterInfo_ = cfg.getParameter<edm::InputTag>("srcGenFilterInfo");
49 }
50 
52 {
53 // nothing to be done yet...
54 }
55 
57 {
58 //--- create TTree
60  ntuple_ = fs->make<TTree>("embeddingKineReweightNtuple", "embeddingKineReweightNtuple");
61 
62 //--- add branches storing quantities for computing embeddingKineReweight LUTs
63 // 1.) for embeddingKineReweightGENtoEmbedded:
64  addBranch_EnPxPyPz("genDiTau");
65  addBranch_EnPxPyPz("genTau1");
66  addBranch_EnPxPyPz("genTau2");
67 // 2.) for embeddingKineReweightGENtoREC:
68  addBranch_EnPxPyPz("genDiMuon");
69  addBranch_EnPxPyPz("genMuonPlus");
70  addBranch_EnPxPyPz("genMuonMinus");
71  addBranch_EnPxPyPz("recDiMuon");
72  addBranch_EnPxPyPz("recMuonPlus");
73  addBranch_EnPxPyPz("recMuonMinus");
74 
75  addBranch_EnPxPyPz("recLeg1");
76  addBranch_EnPxPyPz("recLeg2");
77 
78  addBranchI("numJetsRawPtGt20");
79  addBranchI("numJetsCorrPtGt20");
80  addBranchI("numJetsRawPtGt30");
81  addBranchI("numJetsCorrPtGt30");
82 
83  addBranch_EnPxPyPz("genCaloMEt");
84  addBranch_EnPxPyPz("recCaloMEt");
85  addBranch_MEtResProjections("recMinusGenCaloMEt");
86  addBranch_EnPxPyPz("genPFMEt");
87  addBranch_EnPxPyPz("recPFMEt");
88  addBranch_MEtResProjections("recMinusGenPFMEt");
89 
90  for ( vInputTag::const_iterator srcWeight = srcWeights_.begin();
91  srcWeight != srcWeights_.end(); ++srcWeight ) {
92  std::string branchName = Form("%s_%s", srcWeight->label().data(), srcWeight->instance().data());
93  addBranchF(branchName.data());
94  }
95  if ( srcGenFilterInfo_.label() != "" ) {
96  addBranchF("genFilterInfo");
97  addBranchI("genFilterInfoIsValid");
98  }
99 }
100 
101 namespace
102 {
103  void findDaughters(const reco::GenParticle* mother, std::vector<const reco::GenParticle*>& daughters, int status)
104  {
105  unsigned numDaughters = mother->numberOfDaughters();
106  for ( unsigned iDaughter = 0; iDaughter < numDaughters; ++iDaughter ) {
107  const reco::GenParticle* daughter = mother->daughterRef(iDaughter).get();
108 
109  if ( status == -1 || daughter->status() == status ) daughters.push_back(daughter);
110 
111  findDaughters(daughter, daughters, status);
112  }
113  }
114 }
115 
117 {
118 //--- reset all branches
119  for ( branchMap::iterator branch = branches_.begin();
120  branch != branches_.end(); ++branch ) {
121  branch->second.valueF_ = 0.;
122  branch->second.valueI_ = 0;
123  }
124 
125 //--- set branches containing generator level Z->tautau information
126  const reco::Candidate* genDiTau_ref = 0;
127  if ( srcGenDiTaus_.label() != "" ) {
128  edm::Handle<CandidateView> genDiTaus;
129  evt.getByLabel(srcGenDiTaus_, genDiTaus);
130 
131  for ( CandidateView::const_iterator genDiTau = genDiTaus->begin();
132  genDiTau != genDiTaus->end(); ++genDiTau ) {
133  const reco::CompositeCandidate* genDiTau_composite = dynamic_cast<const reco::CompositeCandidate*>(&(*genDiTau));
134  if ( !(genDiTau_composite && genDiTau_composite->numberOfDaughters() == 2) ) {
135  std::cerr << "genDiTau has " << genDiTau_composite->numberOfDaughters() << " daughters --> skipping !!" << std::endl;
136  continue;
137  }
138 
139  const reco::Candidate* genTau1 = genDiTau_composite->daughter(0);
140  const reco::Candidate* genTau2 = genDiTau_composite->daughter(1);
141  if ( !(genTau1 && genTau2) ) {
142  std::cerr << "Failed to find daughters of genDiTau --> skipping !!" << std::endl;
143  continue;
144  }
145  const reco::Candidate* genTauPlus = 0;
146  const reco::Candidate* genTauMinus = 0;
147  if ( genTau1->charge() > +0.5 && genTau2->charge() < -0.5 ) {
148  genTauPlus = genTau1;
149  genTauMinus = genTau2;
150  } else if ( genTau1->charge() < -0.5 && genTau2->charge() > +0.5 ) {
151  genTauPlus = genTau2;
152  genTauMinus = genTau1;
153  } else {
154  std::cerr << "Failed to find daughters of genDiTau --> skipping !!" << std::endl;
155  continue;
156  }
157 
158  setValue_EnPxPyPz("genDiTau", genDiTau->p4());
159  setValue_EnPxPyPz("genTauPlus", genTauPlus->p4());
160  setValue_EnPxPyPz("genTauMinus", genTauMinus->p4());
161  genDiTau_ref = &(*genDiTau);
162  }
163  }
164 
165  if ( !genDiTau_ref ) {
166  std::cerr << "Failed to find genDiTau --> skipping !!" << std::endl;
167  return;
168  }
169 
170 //--- set branches containing generator level Z->mumu information
171  if ( srcGenParticles_.label() != "" ) {
173  evt.getByLabel(srcGenParticles_, genParticles);
174 
175  for ( reco::GenParticleCollection::const_iterator genParticle = genParticles->begin();
176  genParticle != genParticles->end(); ++genParticle ) {
177  if ( TMath::Abs(genParticle->pdgId()) == 23 ) {
178  std::vector<const reco::GenParticle*> daughters;
179  findDaughters(&(*genParticle), daughters, -1);
180  const reco::GenParticle* genMuPlus = 0;
181  const reco::GenParticle* genMuMinus = 0;
182  for ( std::vector<const reco::GenParticle*>::const_iterator daughter = daughters.begin();
183  daughter != daughters.end(); ++daughter ) {
184  if ( (*daughter)->pdgId() == -13 ) genMuPlus = (*daughter);
185  if ( (*daughter)->pdgId() == +13 ) genMuMinus = (*daughter);
186  }
187  if ( genMuPlus && genMuMinus ) {
188  setValue_EnPxPyPz("genDiMuon", genParticle->p4());
189  setValue_EnPxPyPz("genMuonPlus", genMuPlus->p4());
190  setValue_EnPxPyPz("genMuonMinus", genMuMinus->p4());
191 
192 //--- set branches containing information on reconstructed Z->mumu decays
193  if ( srcSelectedMuons_.label() != "" ) {
194  std::vector<reco::CandidateBaseRef> selMuons = getSelMuons(evt, srcSelectedMuons_);
195  const reco::CandidateBaseRef recMuPlus = getTheMuPlus(selMuons);
196  const reco::CandidateBaseRef recMuMinus = getTheMuMinus(selMuons);
197  if ( recMuPlus.isNonnull() && recMuMinus.isNonnull() ) {
198  setValue_EnPxPyPz("recDiMuon", recMuPlus->p4() + recMuMinus->p4());
199  setValue_EnPxPyPz("recMuonPlus", recMuPlus->p4());
200  setValue_EnPxPyPz("recMuonMinus", recMuMinus->p4());
201  }
202  }
203  }
204  }
205  }
206  }
207 
209  evt.getByLabel(srcRecLeg1_, recLeg1);
210  if ( recLeg1->size() >= 1 ) {
211  setValue_EnPxPyPz("recLeg1", recLeg1->at(0).p4());
212  }
214  evt.getByLabel(srcRecLeg2_, recLeg2);
215  if ( srcRecLeg2_ == srcRecLeg1_ && recLeg2->size() >= 2 ) {
216  setValue_EnPxPyPz("recLeg2", recLeg1->at(1).p4());
217  } else if ( recLeg2->size() >= 1 ) {
218  setValue_EnPxPyPz("recLeg2", recLeg2->at(0).p4());
219  }
220 
222  evt.getByLabel(srcRecJets_, recJets);
223  int numJetsRawPtGt20 = 0;
224  int numJetsCorrPtGt20 = 0;
225  int numJetsRawPtGt30 = 0;
226  int numJetsCorrPtGt30 = 0;
227  for ( pat::JetCollection::const_iterator recJet = recJets->begin();
228  recJet != recJets->end(); ++recJet ) {
229 
230  reco::Candidate::LorentzVector rawJetP4 = recJet->correctedP4("Uncorrected");
231  double rawJetPt = rawJetP4.pt();
232  double rawJetAbsEta = TMath::Abs(rawJetP4.eta());
233  if ( rawJetAbsEta < 4.5 ) {
234  if ( rawJetPt > 20. ) ++numJetsRawPtGt20;
235  if ( rawJetPt > 30. ) ++numJetsRawPtGt30;
236  }
237 
238  reco::Candidate::LorentzVector corrJetP4 = recJet->p4();
239  double corrJetPt = corrJetP4.pt();
240  double corrJetAbsEta = TMath::Abs(corrJetP4.eta());
241  if ( corrJetAbsEta < 4.5 ) {
242  if ( corrJetPt > 20. ) ++numJetsCorrPtGt20;
243  if ( corrJetPt > 30. ) ++numJetsCorrPtGt30;
244  }
245  }
246  setValueI("numJetsRawPtGt20", numJetsRawPtGt20);
247  setValueI("numJetsCorrPtGt20", numJetsCorrPtGt20);
248  setValueI("numJetsRawPtGt30", numJetsRawPtGt30);
249  setValueI("numJetsCorrPtGt30", numJetsCorrPtGt30);
250 
251  if ( genDiTau_ref ) {
252  edm::Handle<METView> genCaloMET;
253  evt.getByLabel(srcGenCaloMEt_, genCaloMET);
254  const reco::Candidate::LorentzVector& genCaloMEtP4 = genCaloMET->front().p4();
255  setValue_EnPxPyPz("genCaloMEt", genCaloMEtP4);
256  edm::Handle<METView> recCaloMET;
257  evt.getByLabel(srcRecCaloMEt_, recCaloMET);
258  const reco::Candidate::LorentzVector& recCaloMEtP4 = recCaloMET->front().p4();
259  //std::cout << "<EmbeddingKineReweightNtupleProducer>:" << std::endl;
260  //std::cout << " recCaloMEt(" << srcRecCaloMEt_.label() << "): Pt = " << recCaloMEtP4.pt() << ", phi = " << recCaloMEtP4.phi()
261  // << " (Px = " << recCaloMEtP4.px() << ", Py = " << recCaloMEtP4.py() << ")" << std::endl;
262  setValue_EnPxPyPz("recCaloMEt", recCaloMEtP4);
263  setValue_MEtResProjections("recMinusGenCaloMEt", genCaloMEtP4, recCaloMEtP4, genDiTau_ref->p4());
264 
265  edm::Handle<METView> genPFMET;
266  evt.getByLabel(srcGenPFMEt_, genPFMET);
267  const reco::Candidate::LorentzVector& genPFMEtP4 = genPFMET->front().p4();
268  setValue_EnPxPyPz("genPFMEt", genPFMEtP4);
269  edm::Handle<METView> recPFMET;
270  evt.getByLabel(srcRecPFMEt_, recPFMET);
271  const reco::Candidate::LorentzVector& recPFMEtP4 = recPFMET->front().p4();
272  setValue_EnPxPyPz("recPFMEt", recPFMEtP4);
273  setValue_MEtResProjections("recMinusGenPFMEt", genPFMEtP4, recPFMEtP4, genDiTau_ref->p4());
274  }
275 
276 //--- set branches containing event weight information
277  for ( vInputTag::const_iterator srcWeight = srcWeights_.begin();
278  srcWeight != srcWeights_.end(); ++srcWeight ) {
280  evt.getByLabel(*srcWeight, weight);
281  std::string branchName = Form("%s_%s", srcWeight->label().data(), srcWeight->instance().data());
282  setValueF(branchName.data(), *weight);
283  }
284  if ( srcGenFilterInfo_.label() != "" ) {
285  edm::Handle<GenFilterInfo> genFilterInfo;
286  evt.getByLabel(srcGenFilterInfo_, genFilterInfo);
287  double genFilterInfo_weight = 1.;
288  bool genFilterInfo_isValid = false;
289  if ( genFilterInfo->numEventsTried() > 0 ) {
290  genFilterInfo_weight = genFilterInfo->filterEfficiency();
291  genFilterInfo_isValid = true;
292  }
293  setValueF("genFilterInfo", genFilterInfo_weight);
294  setValueI("genFilterInfoIsValid", genFilterInfo_isValid);
295  }
296 
297 //--- finally, fill all computed quantities into TTree
298  assert(ntuple_);
299  ntuple_->Fill();
300 }
301 
303 {
304  //std::cout << "<EmbeddingKineReweightNtupleProducer::addBranchF>:" << std::endl;
305  //std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
306  //std::cout << " branchName = " << name << std::endl;
307  assert(branches_.count(name) == 0);
308  std::string name_and_format = name + "/F";
309  ntuple_->Branch(name.c_str(), &branches_[name].valueF_, name_and_format.c_str());
310 }
311 
313 {
314  //std::cout << "<EmbeddingKineReweightNtupleProducer::addBranchI>:" << std::endl;
315  //std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
316  //std::cout << " branchName = " << name << std::endl;
317  assert(branches_.count(name) == 0);
318  std::string name_and_format = name + "/I";
319  ntuple_->Branch(name.c_str(), &branches_[name].valueI_, name_and_format.c_str());
320 }
321 
323 {
324  stream << "<EmbeddingKineReweightNtupleProducer::printBranches>:" << std::endl;
325  stream << " registered branches for module = " << moduleLabel_ << std::endl;
326  for ( branchMap::const_iterator branch = branches_.begin();
327  branch != branches_.end(); ++branch ) {
328  stream << " " << branch->first << std::endl;
329  }
330  stream << std::endl;
331 }
332 
334 {
335  if ( verbosity_ ) std::cout << "branch = " << name << ": value = " << value << std::endl;
336  branchMap::iterator branch = branches_.find(name);
337  if ( branch != branches_.end() ) {
338  branch->second.valueF_ = value;
339  } else {
340  throw cms::Exception("InvalidParameter")
341  << "No branch with name = " << name << " defined !!\n";
342  }
343 }
344 
346 {
347  if ( verbosity_ ) std::cout << "branch = " << name << ": value = " << value << std::endl;
348  branchMap::iterator branch = branches_.find(name);
349  if ( branch != branches_.end() ) {
350  branch->second.valueI_ = value;
351  } else {
352  throw cms::Exception("InvalidParameter")
353  << "No branch with name = " << name << " defined !!\n";
354  }
355 }
356 
357 //
358 //-------------------------------------------------------------------------------
359 //
360 
362 {
363  addBranchF(std::string(name).append("En"));
364  addBranchF(std::string(name).append("Px"));
365  addBranchF(std::string(name).append("Py"));
366  addBranchF(std::string(name).append("Pz"));
367  addBranchF(std::string(name).append("Pt"));
368  addBranchF(std::string(name).append("Eta"));
369  addBranchF(std::string(name).append("Phi"));
370  addBranchF(std::string(name).append("M"));
371  addBranchI(std::string(name).append("IsValid"));
372 }
373 
375 {
376  addBranchF(std::string(name).append("ParlZ"));
377  addBranchF(std::string(name).append("PerpZ"));
378 }
379 
380 //
381 //-------------------------------------------------------------------------------
382 //
383 
385 {
386  setValueF(std::string(name).append("En"), p4.E());
387  setValueF(std::string(name).append("Px"), p4.px());
388  setValueF(std::string(name).append("Py"), p4.py());
389  setValueF(std::string(name).append("Pz"), p4.pz());
390  setValueF(std::string(name).append("Pt"), p4.pt());
391  setValueF(std::string(name).append("Eta"), p4.eta());
392  setValueF(std::string(name).append("Phi"), p4.phi());
393  setValueF(std::string(name).append("M"), p4.M());
394  setValueI(std::string(name).append("IsValid"), true);
395 }
396 
398  const reco::Candidate::LorentzVector& genMEtP4, const reco::Candidate::LorentzVector& recMEtP4,
400 {
401  if ( zP4.pt() > 0. ) {
402  double qX = zP4.px();
403  double qY = zP4.py();
404  double qT = TMath::Sqrt(qX*qX + qY*qY);
405  double dX = recMEtP4.px() - genMEtP4.px();
406  double dY = recMEtP4.py() - genMEtP4.py();
407  double dParl = (dX*qX + dY*qY)/qT;
408  double dPerp = (dX*qY - dY*qX)/qT;
409  setValueF(std::string(name).append("ParlZ"), dParl);
410  setValueF(std::string(name).append("PerpZ"), dPerp);
411  }
412 }
413 
415 
T getParameter(std::string const &) const
reco::CandidateBaseRef getTheMuMinus(const std::vector< reco::CandidateBaseRef > &)
tuple cfg
Definition: looper.py:259
daughters::value_type daughterRef(size_type i) const
reference to daughter at given position
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual int status() const
status word
assert(m_qm.get())
const reco::GenParticle * mother(const reco::GenParticle &p, unsigned int imoth=0)
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:279
virtual size_type numberOfDaughters() const
number of daughters
void analyze(const edm::Event &, const edm::EventSetup &)
edm::View< reco::Candidate > CandidateView
double p4[4]
Definition: TauolaWrapper.h:92
const_iterator end() const
last daughter const_iterator
Definition: Candidate.h:146
virtual size_t numberOfDaughters() const
number of daughters
T Abs(T a)
Definition: MathUtil.h:49
edm::View< reco::MET > METView
const reco::GenParticle * daughter(const reco::GenParticle &p, unsigned int idau)
virtual int charge() const =0
electric charge
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
void setValue_EnPxPyPz(const std::string &, const reco::Candidate::LorentzVector &)
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
std::string const & label() const
Definition: InputTag.h:42
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
T get() const
get a component
Definition: Candidate.h:217
const_iterator begin() const
first daughter const_iterator
Definition: Candidate.h:144
tuple cout
Definition: gather_cfg.py:121
std::vector< reco::CandidateBaseRef > getSelMuons(const edm::Event &, const edm::InputTag &)
int weight
Definition: histoStyle.py:50
tuple status
Definition: ntuplemaker.py:245
reco::CandidateBaseRef getTheMuPlus(const std::vector< reco::CandidateBaseRef > &)
moduleLabel_(iConfig.getParameter< string >("@module_label"))
void setValue_MEtResProjections(const std::string &, const reco::Candidate::LorentzVector &, const reco::Candidate::LorentzVector &, const reco::Candidate::LorentzVector &)
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector