CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/ElectroWeakAnalysis/ZMuMu/plugins/DimuonStatistics.cc

Go to the documentation of this file.
00001 #include "FWCore/Framework/interface/EDAnalyzer.h"
00002 #include "FWCore/Utilities/interface/InputTag.h"
00003 #include <iostream>
00004 
00005 class DimuonStatistics : public edm::EDAnalyzer {
00006 public:
00007   DimuonStatistics(const edm::ParameterSet & cfg);
00008   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00009   virtual void endJob();
00010 private:
00011   edm::InputTag src_;
00012   std::vector<unsigned int> matched_, unMatched_;
00013   double  ptMin_, massMin_,massMax_,  etaMin_,  etaMax_,  trkIso_;
00014 };
00015 
00016 #include "DataFormats/Candidate/interface/Candidate.h"
00017 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00018 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00019 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "DataFormats/Common/interface/Handle.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "DataFormats/PatCandidates/interface/Muon.h"
00024 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
00025 using namespace std;
00026 using namespace reco;
00027 using namespace edm;
00028 const unsigned int maxEntries = 10;
00029 
00030 DimuonStatistics::DimuonStatistics(const edm::ParameterSet & cfg) : 
00031   src_(cfg.getParameter<InputTag>("src")), 
00032   matched_(maxEntries + 1, 0), 
00033   unMatched_(maxEntries + 1, 0), 
00034   ptMin_(cfg.getUntrackedParameter<double>("ptMin")),
00035   massMin_(cfg.getUntrackedParameter<double>("massMin")),
00036   massMax_(cfg.getUntrackedParameter<double>("massMax")),
00037   etaMin_(cfg.getUntrackedParameter<double>("etaMin")),
00038   etaMax_(cfg.getUntrackedParameter<double>("etaMax")),
00039   trkIso_(cfg.getUntrackedParameter<double>("trkIso")){
00040 }
00041 
00042 void DimuonStatistics::endJob() {
00043   cout << src_.encode() << endl ;
00044   cout << " == Matched == " << endl;
00045   for(unsigned int i = 0; i <= maxEntries; ++i) {
00046     cout << i << ": " << matched_[i];
00047     if (i < maxEntries) cout << ", ";
00048   }
00049   cout << endl;
00050   cout << " == unMatched == " << endl;
00051   for(unsigned int i = 0; i <= maxEntries; ++i) {
00052     cout << i << ": " << unMatched_[i];
00053     if (i < maxEntries) cout << ", ";
00054 }
00055   cout << endl;
00056 }
00057 
00058 void DimuonStatistics::analyze(const edm::Event& evt, const edm::EventSetup&) {
00059   Handle<CandidateView> src;
00060   evt.getByLabel(src_, src);
00061   double trackIso1=-1;
00062   double trackIso2=-1;
00063   int j=0;
00064   unsigned int matched = 0, unMatched = 0;
00065   cout << ">> entries in " << src_ << ": " << src->size() << endl;
00066   for(CandidateView::const_iterator i = src->begin(); i != src->end(); ++i) {
00067     j++;
00068     const Candidate * dau1 = i->daughter(0);
00069     const Candidate * dau2 = i->daughter(1);
00070     if(dau1 == 0|| dau2 == 0) 
00071       throw Exception(errors::InvalidReference) <<
00072         "one of the two daughter does not exist\n";
00073     const Candidate * c1 = dau1->masterClone().get();
00074     GenParticleRef mc1;
00075     const pat::Muon * mu1 = dynamic_cast<const pat::Muon*>(c1);
00076     if(mu1 != 0) {
00077       mc1 = mu1->genParticleRef();
00078       //     if (mc1.isNonnull()) cout << "DimuonStatistics> genParticleRef1 " << mc1->pdgId() << endl;
00079       trackIso1=mu1->trackIso();
00080     } else {
00081       const pat::GenericParticle * gp1 = dynamic_cast<const pat::GenericParticle*>(c1);
00082       if(gp1 == 0) 
00083         throw Exception(errors::InvalidReference) <<
00084           "first of two daughter is neither a pat::Muon not pat::GenericParticle\n";
00085       mc1 = gp1->genParticleRef();
00086     }
00087     const Candidate * c2 = dau2->masterClone().get();
00088     GenParticleRef mc2;
00089     const pat::Muon * mu2 = dynamic_cast<const pat::Muon*>(c2);
00090     if(mu2 != 0) {
00091       mc2 = mu2->genParticleRef();
00092       //      if (mc2.isNonnull()) cout << "DimuonStatistics> genParticleRef2 " << mc2->pdgId() << endl;
00093      trackIso2=mu2->trackIso();
00094     } else {
00095       const pat::GenericParticle * gp2 = dynamic_cast<const pat::GenericParticle*>(c2);
00096       if(gp2 == 0) 
00097         throw Exception(errors::InvalidReference) <<
00098           "first of two daughter is neither a pat::Muon not pat::GenericParticle\n";
00099       mc2 = gp2->genParticleRef();
00100     }
00101     GenParticleRef dimuonMatch;
00102     if(mc1.isNonnull() && mc2.isNonnull()) {
00103       cout << "DimuonStatistics> mc1: " << mc1->pdgId() << ", mc2: " << mc2->pdgId() << endl;
00104       int k=0;
00105       do {
00106         k++;
00107         mc1 = mc1->numberOfMothers() > 0 ? mc1->motherRef() : GenParticleRef();
00108         mc2 = mc2->numberOfMothers() > 0 ? mc2->motherRef() : GenParticleRef();
00109         //      cout << "DimuonStatistics> do loop: " << k << "  id1 " << mc1->pdgId() << "    id2 " << mc2->pdgId() << endl;
00110       } while(mc1 != mc2 && mc1.isNonnull() && mc2.isNonnull());
00111       if(mc1.isNonnull() && mc2.isNonnull() && mc1->pdgId()==23) {
00112         dimuonMatch = mc1;
00113       }
00114     }
00115     //    cout << "DimuonMatcher> dimuonMatch " << dimuonMatch.isNonnull() << endl;
00116     if( (fabs(dau1->eta()) > etaMin_  && fabs(dau1->eta()) < etaMax_)   && dau1->pt()>ptMin_ &&
00117      ( (fabs(dau2->eta()) > etaMin_ ) && (fabs(dau2->eta()) < etaMax_) ) && dau2->pt()>ptMin_ &&        
00118   trackIso1 < trkIso_ &&  trackIso2 < trkIso_ &&                           i->mass() > massMin_ && i->mass() < massMax_ 
00119         )  {
00120       cout << "dimuon mass " << i->mass() << endl;    
00121       if(dimuonMatch.isNonnull())
00122         ++matched;
00123       else
00124         ++unMatched;
00125          
00126          
00127     }
00128   }
00129   cout << "matched: " << matched << ", unmatched: " << unMatched << endl;
00130   
00131   if(matched > maxEntries) matched = maxEntries;
00132   if(unMatched > maxEntries) unMatched = maxEntries;
00133   ++matched_[matched];
00134   ++unMatched_[unMatched];
00135 }
00136 
00137 
00138 #include "FWCore/Framework/interface/MakerMacros.h"
00139 
00140 DEFINE_FWK_MODULE(DimuonStatistics);