/ElectroWeakAnalysis/ZMuMu/plugins/DimuonMCMatcher.cc

https://github.com/aivanov-cern/cmssw · C++ · 94 lines · 81 code · 8 blank · 5 comment · 24 complexity · 9bae54ca72631a5f21f4b83593aafde4 MD5 · raw file

  1. #include "FWCore/Framework/interface/EDProducer.h"
  2. #include "FWCore/Utilities/interface/InputTag.h"
  3. #include <iostream>
  4. class DimuonMCMatcher : public edm::EDProducer {
  5. public:
  6. DimuonMCMatcher(const edm::ParameterSet & cfg);
  7. virtual void produce(edm::Event&, const edm::EventSetup&) override;
  8. private:
  9. edm::InputTag src_;
  10. };
  11. #include "DataFormats/Candidate/interface/Candidate.h"
  12. #include "DataFormats/Candidate/interface/CandidateFwd.h"
  13. #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
  14. #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
  15. #include "FWCore/Framework/interface/Event.h"
  16. #include "DataFormats/Common/interface/Handle.h"
  17. #include "FWCore/ParameterSet/interface/ParameterSet.h"
  18. #include "DataFormats/PatCandidates/interface/Muon.h"
  19. #include "DataFormats/PatCandidates/interface/GenericParticle.h"
  20. using namespace std;
  21. using namespace reco;
  22. using namespace edm;
  23. DimuonMCMatcher::DimuonMCMatcher(const edm::ParameterSet & cfg) :
  24. src_(cfg.getParameter<InputTag>("src")) {
  25. produces<vector<GenParticleRef> >();
  26. }
  27. void DimuonMCMatcher::produce(edm::Event& evt, const edm::EventSetup&) {
  28. Handle<CandidateView> src;
  29. evt.getByLabel(src_, src);
  30. auto_ptr<vector<GenParticleRef> > matched(new vector<GenParticleRef>);
  31. matched->reserve(src->size());
  32. int j=0;
  33. for(CandidateView::const_iterator i = src->begin(); i != src->end(); ++i) {
  34. j++;
  35. const Candidate * dau1 = i->daughter(0);
  36. const Candidate * dau2 = i->daughter(1);
  37. if(dau1 == 0|| dau2 == 0)
  38. throw Exception(errors::InvalidReference) <<
  39. "one of the two daughter does not exist\n";
  40. const Candidate * c1 = dau1->masterClone().get();
  41. GenParticleRef mc1;
  42. const pat::Muon * mu1 = dynamic_cast<const pat::Muon*>(c1);
  43. if(mu1 != 0) {
  44. mc1 = mu1->genParticleRef();
  45. // if (mc1.isNonnull()) cout << "DimuonMCMatcher> genParticleRef1 " << mc1->pdgId() << endl;
  46. } else {
  47. const pat::GenericParticle * gp1 = dynamic_cast<const pat::GenericParticle*>(c1);
  48. if(gp1 == 0)
  49. throw Exception(errors::InvalidReference) <<
  50. "first of two daughter is neither a pat::Muon not pat::GenericParticle\n";
  51. mc1 = gp1->genParticleRef();
  52. }
  53. const Candidate * c2 = dau2->masterClone().get();
  54. GenParticleRef mc2;
  55. const pat::Muon * mu2 = dynamic_cast<const pat::Muon*>(c2);
  56. if(mu2 != 0) {
  57. mc2 = mu2->genParticleRef();
  58. // if (mc2.isNonnull()) cout << "DimuonMCMatcher> genParticleRef2 " << mc2->pdgId() << endl;
  59. } else {
  60. const pat::GenericParticle * gp2 = dynamic_cast<const pat::GenericParticle*>(c2);
  61. if(gp2 == 0)
  62. throw Exception(errors::InvalidReference) <<
  63. "first of two daughter is neither a pat::Muon not pat::GenericParticle\n";
  64. mc2 = gp2->genParticleRef();
  65. }
  66. GenParticleRef dimuonMatch;
  67. // cout << "DimuonMatcher> mc1 " << mc1.isNonnull() << " mc2 " << mc2.isNonnull() << endl;
  68. if(mc1.isNonnull() && mc2.isNonnull()) {
  69. int k=0;
  70. do {
  71. k++;
  72. mc1 = mc1->numberOfMothers() > 0 ? mc1->motherRef() : GenParticleRef();
  73. mc2 = mc2->numberOfMothers() > 0 ? mc2->motherRef() : GenParticleRef();
  74. // cout << "DimuonMCMatcher> do loop: " << k << " id1 " << mc1->pdgId() << " id2 " << mc2->pdgId() << endl;
  75. } while(mc1 != mc2 && mc1.isNonnull() && mc2.isNonnull());
  76. if(mc1.isNonnull() && mc2.isNonnull() && mc1->pdgId()==23) {
  77. dimuonMatch = mc1;
  78. }
  79. }
  80. // cout << "DimuonMatcher> dimuonMatch " << dimuonMatch.isNonnull() << endl;
  81. matched->push_back(dimuonMatch);
  82. }
  83. evt.put(matched);
  84. }
  85. #include "FWCore/Framework/interface/MakerMacros.h"
  86. DEFINE_FWK_MODULE(DimuonMCMatcher);