CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/ElectroWeakAnalysis/ZMuMu/plugins/ZMuMuEfficiency.cc

Go to the documentation of this file.
00001 /* \class ZMuMuEfficiency
00002  * 
00003  * author: Pasquale Noli
00004  * revised by Salvatore di Guida
00005  * revised for CSA08 by Davide Piccolo
00006  *
00007  * Efficiency of reconstruction tracker and muon Chamber
00008  *
00009  */
00010 
00011 #include "DataFormats/Common/interface/AssociationVector.h"
00012 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00013 #include "DataFormats/Candidate/interface/CandMatchMap.h" 
00014 #include "FWCore/Framework/interface/EDAnalyzer.h"
00015 #include "DataFormats/Candidate/interface/Candidate.h"
00016 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00018 #include "FWCore/Framework/interface/Event.h"
00019 #include "FWCore/Framework/interface/EventSetup.h"
00020 #include "FWCore/Utilities/interface/InputTag.h"
00021 #include "DataFormats/Candidate/interface/OverlapChecker.h"
00022 #include "TH1.h"
00023 #include <vector>
00024 using namespace edm;
00025 using namespace std;
00026 using namespace reco;
00027 
00028 class ZMuMuEfficiency : public edm::EDAnalyzer {
00029 public:
00030   ZMuMuEfficiency(const edm::ParameterSet& pset);
00031 private:
00032   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup);
00033   bool check_ifZmumu(const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2); 
00034   float getParticlePt(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2); 
00035   float getParticleEta(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2); 
00036   float getParticlePhi(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2); 
00037   Particle::LorentzVector getParticleP4(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2); 
00038   virtual void endJob();
00039 
00040   edm::InputTag zMuMu_, zMuMuMatchMap_; 
00041   edm::InputTag zMuTrack_, zMuTrackMatchMap_; 
00042   edm::InputTag zMuStandAlone_, zMuStandAloneMatchMap_;
00043   edm::InputTag muons_, muonMatchMap_, muonIso_;
00044   edm::InputTag tracks_, trackIso_;
00045   edm::InputTag standAlone_, standAloneIso_;
00046   edm::InputTag genParticles_;
00047 
00048   double zMassMin_, zMassMax_, ptmin_, etamax_, isomax_;
00049   unsigned int nbinsPt_, nbinsEta_;
00050   reco::CandidateBaseRef globalMuonCandRef_, trackMuonCandRef_, standAloneMuonCandRef_;
00051   OverlapChecker overlap_;
00052 
00053   //histograms for measuring tracker efficiency
00054   TH1D *h_etaStandAlone_, *h_etaMuonOverlappedToStandAlone_; 
00055   TH1D *h_ptStandAlone_, *h_ptMuonOverlappedToStandAlone_; 
00056 
00057   //histograms for measuring standalone efficiency
00058   TH1D *h_etaTrack_, *h_etaMuonOverlappedToTrack_;
00059   TH1D *h_ptTrack_, *h_ptMuonOverlappedToTrack_;
00060 
00061   //histograms for MC acceptance
00062   TH1D *h_nZMCfound_;
00063   TH1D *h_ZetaGen_, *h_ZptGen_, *h_ZmassGen_;
00064   TH1D *h_muetaGen_, *h_muptGen_, *h_muIsoGen_;
00065   TH1D *h_dimuonPtGen_, *h_dimuonMassGen_, *h_dimuonEtaGen_;
00066   TH1D *h_ZetaGenPassed_, *h_ZptGenPassed_, *h_ZmassGenPassed_;
00067   TH1D *h_muetaGenPassed_, *h_muptGenPassed_, *h_muIsoGenPassed_;
00068   TH1D *h_dimuonPtGenPassed_, *h_dimuonMassGenPassed_, *h_dimuonEtaGenPassed_;
00069   //histograms for invarian mass resolution
00070   TH1D *h_DELTA_ZMuMuMassReco_dimuonMassGen_, *h_DELTA_ZMuStaMassReco_dimuonMassGen_, *h_DELTA_ZMuTrackMassReco_dimuonMassGen_;
00071 
00072   int numberOfEventsWithZMuMufound, numberOfEventsWithZMuStafound; 
00073   int numberOfMatchedZMuSta_,numberOfMatchedSelectedZMuSta_; 
00074   int numberOfMatchedZMuMu_, numberOfMatchedSelectedZMuMu_;
00075   int numberOfOverlappedStandAlone_, numberOfOverlappedTracks_, numberOfMatchedZMuTrack_notOverlapped;
00076   int numberOfMatchedZMuTrack_exclusive ,numberOfMatchedSelectedZMuTrack_exclusive;
00077   int numberOfMatchedZMuTrack_matchedZMuMu, numberOfMatchedZMuTrack_matchedSelectedZMuMu ;
00078   int totalNumberOfevents, totalNumberOfZfound, totalNumberOfZPassed;
00079   int noMCmatching, ZMuTrack_exclusive_1match, ZMuTrack_exclusive_morematch;
00080   int ZMuTrackselected_exclusive_1match, ZMuTrackselected_exclusive_morematch;
00081   int ZMuTrack_ZMuMu_1match, ZMuTrack_ZMuMu_2match, ZMuTrack_ZMuMu_morematch;
00082 
00083   int n_zMuMufound_genZsele, n_zMuStafound_genZsele, n_zMuTrkfound_genZsele;
00084 };
00085 
00086 #include "FWCore/ServiceRegistry/interface/Service.h"
00087 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00088 #include "DataFormats/Common/interface/Handle.h"
00089 #include "DataFormats/Candidate/interface/Particle.h"
00090 #include "DataFormats/Candidate/interface/Candidate.h"
00091 #include "DataFormats/Common/interface/ValueMap.h"
00092 #include "DataFormats/Candidate/interface/CandAssociation.h"
00093 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00094 #include <iostream>
00095 #include <iterator>
00096 #include <cmath>
00097 using namespace std;
00098 using namespace reco;
00099 using namespace edm;
00100 
00101 
00102 typedef edm::ValueMap<float> IsolationCollection;
00103 
00104 ZMuMuEfficiency::ZMuMuEfficiency(const ParameterSet& pset) : 
00105   zMuMu_(pset.getParameter<InputTag>("zMuMu")), 
00106   zMuMuMatchMap_(pset.getParameter<InputTag>("zMuMuMatchMap")), 
00107   zMuTrack_(pset.getParameter<InputTag>("zMuTrack")), 
00108   zMuTrackMatchMap_(pset.getParameter<InputTag>("zMuTrackMatchMap")), 
00109   zMuStandAlone_(pset.getParameter<InputTag>("zMuStandAlone")), 
00110   zMuStandAloneMatchMap_(pset.getParameter<InputTag>("zMuStandAloneMatchMap")), 
00111   muons_(pset.getParameter<InputTag>("muons")), 
00112   muonMatchMap_(pset.getParameter<InputTag>("muonMatchMap")), 
00113   muonIso_(pset.getParameter<InputTag>("muonIso")), 
00114   tracks_(pset.getParameter<InputTag>("tracks")), 
00115   trackIso_(pset.getParameter<InputTag>("trackIso")), 
00116   standAlone_(pset.getParameter<InputTag>("standAlone")), 
00117   standAloneIso_(pset.getParameter<InputTag>("standAloneIso")), 
00118   genParticles_(pset.getParameter<InputTag>( "genParticles" ) ),
00119 
00120   zMassMin_(pset.getUntrackedParameter<double>("zMassMin")), 
00121   zMassMax_(pset.getUntrackedParameter<double>("zMassMax")), 
00122   ptmin_(pset.getUntrackedParameter<double>("ptmin")), 
00123   etamax_(pset.getUntrackedParameter<double>("etamax")),  
00124   isomax_(pset.getUntrackedParameter<double>("isomax")), 
00125   nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")), 
00126   nbinsEta_(pset.getUntrackedParameter<unsigned int>("nbinsEta")) {
00127   Service<TFileService> fs;
00128   TFileDirectory trackEffDir = fs->mkdir("TrackEfficiency");
00129 
00130   // tracker efficiency distributions
00131   h_etaStandAlone_ = trackEffDir.make<TH1D>("StandAloneMuonEta", 
00132                                             "StandAlone #eta for Z -> #mu + standalone", 
00133                                             nbinsEta_, -etamax_, etamax_);
00134   h_etaMuonOverlappedToStandAlone_ = trackEffDir.make<TH1D>("MuonOverlappedToStandAloneEta", 
00135                                                             "Global muon overlapped to standAlone #eta for Z -> #mu + sa", 
00136                                                             nbinsEta_, -etamax_, etamax_);
00137   h_ptStandAlone_ = trackEffDir.make<TH1D>("StandAloneMuonPt", 
00138                                            "StandAlone p_{t} for Z -> #mu + standalone", 
00139                                            nbinsPt_, ptmin_, 100);
00140   h_ptMuonOverlappedToStandAlone_ = trackEffDir.make<TH1D>("MuonOverlappedToStandAlonePt", 
00141                                                            "Global muon overlapped to standAlone p_{t} for Z -> #mu + sa", 
00142                                                            nbinsPt_, ptmin_, 100);
00143   
00144   
00145   // StandAlone efficiency distributions
00146   TFileDirectory standaloneEffDir = fs->mkdir("StandaloneEfficiency");
00147   h_etaTrack_ = standaloneEffDir.make<TH1D>("TrackMuonEta", 
00148                                             "Track #eta for Z -> #mu + track", 
00149                                             nbinsEta_, -etamax_, etamax_);
00150   h_etaMuonOverlappedToTrack_ = standaloneEffDir.make<TH1D>("MuonOverlappedToTrackEta", 
00151                                                             "Global muon overlapped to track #eta for Z -> #mu + tk", 
00152                                                             nbinsEta_, -etamax_, etamax_);
00153   h_ptTrack_ = standaloneEffDir.make<TH1D>("TrackMuonPt", 
00154                                            "Track p_{t} for Z -> #mu + track", 
00155                                            nbinsPt_, ptmin_, 100);
00156   h_ptMuonOverlappedToTrack_ = standaloneEffDir.make<TH1D>("MuonOverlappedToTrackPt", 
00157                                                            "Global muon overlapped to track p_{t} for Z -> #mu + tk", 
00158                                                            nbinsPt_, ptmin_, 100);
00159 
00160 
00161   // inv. mass resolution studies
00162   TFileDirectory invMassResolutionDir = fs->mkdir("invriantMassResolution");
00163   h_DELTA_ZMuMuMassReco_dimuonMassGen_ = invMassResolutionDir.make<TH1D>("zMuMu_invMassResolution","zMuMu invariant Mass Resolution",50,-25,25);
00164   h_DELTA_ZMuStaMassReco_dimuonMassGen_ = invMassResolutionDir.make<TH1D>("zMuSta_invMassResolution","zMuSta invariant Mass Resolution",50,-25,25);
00165   h_DELTA_ZMuTrackMassReco_dimuonMassGen_ = invMassResolutionDir.make<TH1D>("zMuTrack_invMassResolution","zMuTrack invariant Mass Resolution",50,-25,25);
00166 
00167 
00168   // generator level histograms
00169   TFileDirectory genParticleDir = fs->mkdir("genParticle");
00170   h_nZMCfound_ = genParticleDir.make<TH1D>("NumberOfgeneratedZeta","n. of generated Z per event",4,-.5,3.5); 
00171   h_ZetaGen_ = genParticleDir.make<TH1D>("generatedZeta","#eta of generated Z",100,-5.,5.); 
00172   h_ZptGen_ = genParticleDir.make<TH1D>("generatedZpt","pt of generated Z",100,0.,200.); 
00173   h_ZmassGen_ = genParticleDir.make<TH1D>("generatedZmass","mass of generated Z",100,0.,200.); 
00174   h_muetaGen_ = genParticleDir.make<TH1D>("generatedMuonEta","#eta of generated muons from Z decay",100,-5.,5.); 
00175   h_muptGen_ = genParticleDir.make<TH1D>("generatedMuonpt","pt of generated muons from Z decay",100,0.,200.); 
00176   h_dimuonEtaGen_ = genParticleDir.make<TH1D>("generatedDimuonEta","#eta of generated dimuon",100,-5.,5.); 
00177   h_dimuonPtGen_ = genParticleDir.make<TH1D>("generatedDimuonPt","pt of generated dimuon",100,0.,200.); 
00178   h_dimuonMassGen_ = genParticleDir.make<TH1D>("generatedDimuonMass","mass of generated dimuon",100,0.,200.); 
00179   h_ZetaGenPassed_ = genParticleDir.make<TH1D>("generatedZeta_passed","#eta of generated Z after cuts",100,-5.,5.); 
00180   h_ZptGenPassed_ = genParticleDir.make<TH1D>("generatedZpt_passed","pt of generated Z after cuts",100,0.,200.); 
00181   h_ZmassGenPassed_ = genParticleDir.make<TH1D>("generatedZmass_passed","mass of generated Z after cuts",100,0.,200.); 
00182   h_muetaGenPassed_ = genParticleDir.make<TH1D>("generatedMuonEta_passed","#eta of generated muons from Z decay after cuts",100,-5.,5.); 
00183   h_muptGenPassed_ = genParticleDir.make<TH1D>("generatedMuonpt_passed","pt of generated muons from Z decay after cuts",100,0.,200.); 
00184   h_dimuonEtaGenPassed_ = genParticleDir.make<TH1D>("generatedDimuonEta_passed","#eta of generated dimuon after cuts",100,-5.,5.); 
00185   h_dimuonPtGenPassed_ = genParticleDir.make<TH1D>("generatedDimuonPt_passed","pt of generated dimuon after cuts",100,0.,200.); 
00186   h_dimuonMassGenPassed_ = genParticleDir.make<TH1D>("generatedDimuonMass_passed","mass of generated dimuon after cuts",100,0.,200.); 
00187   // to insert isolation histograms  ..............
00188 
00189   numberOfEventsWithZMuMufound = 0;
00190   numberOfEventsWithZMuStafound = 0;
00191   numberOfMatchedZMuMu_ = 0;
00192   numberOfMatchedSelectedZMuMu_ = 0;
00193   numberOfMatchedZMuSta_ = 0;
00194   numberOfMatchedSelectedZMuSta_ = 0;
00195   numberOfMatchedZMuTrack_matchedZMuMu = 0;
00196   numberOfMatchedZMuTrack_matchedSelectedZMuMu = 0;
00197   numberOfMatchedZMuTrack_exclusive = 0;
00198   numberOfMatchedSelectedZMuTrack_exclusive = 0;
00199   numberOfOverlappedStandAlone_ = 0;
00200   numberOfOverlappedTracks_ = 0;
00201   numberOfMatchedZMuTrack_notOverlapped = 0;
00202   noMCmatching = 0;
00203   ZMuTrack_exclusive_1match = 0;
00204   ZMuTrack_exclusive_morematch = 0;
00205   ZMuTrackselected_exclusive_1match = 0;
00206   ZMuTrackselected_exclusive_morematch = 0;
00207   ZMuTrack_ZMuMu_1match = 0;
00208   ZMuTrack_ZMuMu_2match = 0;
00209   ZMuTrack_ZMuMu_morematch = 0;
00210 
00211   n_zMuMufound_genZsele = 0;
00212   n_zMuStafound_genZsele = 0;
00213   n_zMuTrkfound_genZsele = 0;
00214 
00215   // generator counters
00216   totalNumberOfevents = 0;
00217   totalNumberOfZfound = 0;
00218   totalNumberOfZPassed = 0;
00219   
00220 }
00221 
00222 void ZMuMuEfficiency::analyze(const Event& event, const EventSetup& setup) {
00223   Handle<CandidateView> zMuMu;  
00224   Handle<GenParticleMatch> zMuMuMatchMap; //Map of Z made by Mu global + Mu global
00225   Handle<CandidateView> zMuTrack;  
00226   Handle<GenParticleMatch> zMuTrackMatchMap; //Map of Z made by Mu + Track
00227   Handle<CandidateView> zMuStandAlone; 
00228   Handle<GenParticleMatch> zMuStandAloneMatchMap; //Map of Z made by Mu + StandAlone
00229   Handle<CandidateView> muons; //Collection of Muons
00230   Handle<GenParticleMatch> muonMatchMap; 
00231   Handle<IsolationCollection> muonIso; 
00232   Handle<CandidateView> tracks; //Collection of Tracks
00233   Handle<IsolationCollection> trackIso; 
00234   Handle<CandidateView> standAlone; //Collection of StandAlone
00235   Handle<IsolationCollection> standAloneIso; 
00236   Handle<GenParticleCollection> genParticles;  // Collection of Generatd Particles
00237   
00238   event.getByLabel(zMuMu_, zMuMu); 
00239   event.getByLabel(zMuTrack_, zMuTrack); 
00240   event.getByLabel(zMuStandAlone_, zMuStandAlone); 
00241   event.getByLabel(muons_, muons); 
00242   event.getByLabel(tracks_, tracks); 
00243   event.getByLabel(standAlone_, standAlone); 
00244   event.getByLabel(genParticles_, genParticles);
00245 
00246   cout << "*********  zMuMu         size : " << zMuMu->size() << endl;
00247   cout << "*********  zMuStandAlone size : " << zMuStandAlone->size() << endl;
00248   cout << "*********  zMuTrack      size : " << zMuTrack->size() << endl;
00249   cout << "*********  muons         size : " << muons->size()<< endl;
00250   cout << "*********  standAlone    size : " << standAlone->size()<< endl;
00251   cout << "*********  tracks        size : " << tracks->size()<< endl;
00252   cout << "*********  generated     size : " << genParticles->size()<< endl;
00253 
00254 
00255   // generator level distributions
00256 
00257   int nZMCfound = 0;
00258   totalNumberOfevents++;
00259   int ngen = genParticles->size();
00260   bool ZMuMuMatchedfound = false;
00261   bool ZMuMuMatchedSelectedfound = false;
00262   bool ZMuStaMatchedfound = false;
00263   //bool ZMuStaMatchedSelectedfound = false;
00264   int ZMuTrackMatchedfound = 0;
00265   int ZMuTrackMatchedSelected_exclusivefound = 0;
00266 
00267   double dimuonMassGen = 0;
00268 
00269   for (int i=0; i<ngen; i++) {
00270     const Candidate &genCand = (*genParticles)[i];
00271 
00272     //   if((genCand.pdgId() == 23) && (genCand.status() == 2)) //this is an intermediate Z0
00273       //      cout << ">>> intermediate Z0 found, with " << genCand.numberOfDaughters() << " daughters" << endl;
00274     if((genCand.pdgId() == 23)&&(genCand.status() == 3)) { //this is a Z0
00275       if(genCand.numberOfDaughters() == 3) {                    // possible Z0 decays in mu+ mu-, the 3rd daughter is the same Z0
00276         const Candidate * dauGen0 = genCand.daughter(0);
00277         const Candidate * dauGen1 = genCand.daughter(1);
00278         const Candidate * dauGen2 = genCand.daughter(2);
00279         if (check_ifZmumu(dauGen0, dauGen1, dauGen2)) {         // Z0 in mu+ mu-
00280           totalNumberOfZfound++;
00281           nZMCfound++;
00282           bool checkpt = false;
00283           bool checketa = false;
00284           bool checkmass = false;
00285           float mupluspt, muminuspt, mupluseta, muminuseta;
00286           mupluspt = getParticlePt(-13,dauGen0,dauGen1,dauGen2);
00287           muminuspt = getParticlePt(13,dauGen0,dauGen1,dauGen2);
00288           mupluseta = getParticleEta(-13,dauGen0,dauGen1,dauGen2);
00289           muminuseta = getParticleEta(13,dauGen0,dauGen1,dauGen2);
00290           //float muplusphi = getParticlePhi(-13,dauGen0,dauGen1,dauGen2);
00291           //float muminusphi = getParticlePhi(13,dauGen0,dauGen1,dauGen2);
00292           Particle::LorentzVector pZ(0, 0, 0, 0);
00293           Particle::LorentzVector muplusp4 = getParticleP4(-13,dauGen0,dauGen1,dauGen2);
00294           Particle::LorentzVector muminusp4 = getParticleP4(13,dauGen0,dauGen1,dauGen2);
00295           pZ = muplusp4 + muminusp4;
00296           double dimuon_pt = sqrt(pZ.x()*pZ.x()+pZ.y()*pZ.y());
00297           double tan_theta_half = tan(atan(dimuon_pt/pZ.z())/2.);
00298           double dimuon_eta = 0.;
00299           if (tan_theta_half>0) dimuon_eta = -log(tan(tan_theta_half));
00300           if (tan_theta_half<=0) dimuon_eta = log(tan(-tan_theta_half));
00301 
00302           dimuonMassGen = pZ.mass();  // dimuon invariant Mass at Generator Level
00303 
00304           h_ZmassGen_->Fill(genCand.mass());
00305           h_ZetaGen_->Fill(genCand.eta());
00306           h_ZptGen_->Fill(genCand.pt());
00307           h_dimuonMassGen_->Fill(pZ.mass());
00308           h_dimuonEtaGen_->Fill(dimuon_eta);
00309           h_dimuonPtGen_->Fill(dimuon_pt);
00310           h_muetaGen_->Fill(mupluseta);
00311           h_muetaGen_->Fill(muminuseta);
00312           h_muptGen_->Fill(mupluspt);
00313           h_muptGen_->Fill(muminuspt);
00314                              // dimuon 4-momentum
00315           //      h_mDimuonMC->Fill(pZ.mass());
00316           //      h_ZminusDimuonMassMC->Fill(genCand.mass()-pZ.mass());
00317           //      h_DeltaPhiMC->Fill(deltaPhi(muplusphi,muminusphi));
00318           //      if (dauGen2==23) float z_eta = dauGen2->eta();
00319           //      if (dauGen2==23) float Zpt = dauGen2->pt();
00320           //      h_DeltaPhivsZPtMC->Fill(DeltaPhi(muplusphi,muminusphi),ZPt);
00321 
00322           if (mupluspt > ptmin_ && muminuspt > ptmin_) checkpt = true;
00323           if (mupluseta < etamax_ && muminuseta < etamax_) checketa = true;
00324           if (genCand.mass()>zMassMin_ && genCand.mass()<zMassMax_) checkmass = true;
00325           if (checkpt && checketa && checkmass) {
00326             totalNumberOfZPassed++;
00327             h_ZmassGenPassed_->Fill(genCand.mass());
00328             h_ZetaGenPassed_->Fill(genCand.eta());
00329             h_ZptGenPassed_->Fill(genCand.pt());
00330             h_dimuonMassGenPassed_->Fill(pZ.mass());
00331             h_dimuonEtaGenPassed_->Fill(dimuon_eta);
00332             h_dimuonPtGenPassed_->Fill(dimuon_pt);
00333             h_muetaGenPassed_->Fill(mupluseta);
00334             h_muetaGenPassed_->Fill(muminuseta);
00335             h_muptGenPassed_->Fill(mupluspt);
00336             h_muptGenPassed_->Fill(muminuspt);
00337 
00338             if (zMuMu->size() > 0 ) {
00339               n_zMuMufound_genZsele++; 
00340             }
00341             else if (zMuStandAlone->size() > 0 ) {
00342                 n_zMuStafound_genZsele++;
00343             }
00344             else {
00345               n_zMuTrkfound_genZsele++;       
00346             }
00347 
00348           }
00349         }
00350 
00351       }
00352     }
00353   }
00354   h_nZMCfound_->Fill(nZMCfound);                  // number of Z found in the event at generator level  
00355 
00356   //TRACK efficiency (conto numero di eventi Zmumu global e ZmuSta (ricorda che sono due campioni esclusivi)
00357 
00358   if (zMuMu->size() > 0 ) {
00359     numberOfEventsWithZMuMufound++;
00360     event.getByLabel(zMuMuMatchMap_, zMuMuMatchMap); 
00361     event.getByLabel(muonIso_, muonIso); 
00362     event.getByLabel(standAloneIso_, standAloneIso); 
00363     event.getByLabel(muonMatchMap_, muonMatchMap); 
00364     for(unsigned int i = 0; i < zMuMu->size(); ++i) { //loop on candidates
00365       const Candidate & zMuMuCand = (*zMuMu)[i]; //the candidate
00366       CandidateBaseRef zMuMuCandRef = zMuMu->refAt(i);
00367       bool isMatched = false;
00368       GenParticleRef zMuMuMatch = (*zMuMuMatchMap)[zMuMuCandRef];
00369 
00370       if(zMuMuMatch.isNonnull()) {  // ZMuMu matched
00371         isMatched = true;
00372         numberOfMatchedZMuMu_++;
00373       }
00374       CandidateBaseRef dau0 = zMuMuCand.daughter(0)->masterClone();
00375       CandidateBaseRef dau1 = zMuMuCand.daughter(1)->masterClone();
00376       if (isMatched) ZMuMuMatchedfound = true;
00377 
00378       // Cuts
00379       if((dau0->pt() > ptmin_) && (dau1->pt() > ptmin_) && 
00380          (fabs(dau0->eta()) < etamax_) && (fabs(dau1->eta()) < etamax_) && 
00381          (zMuMuCand.mass() > zMassMin_) && (zMuMuCand.mass() < zMassMax_) && 
00382          (isMatched)) { 
00383         //The Z daughters are already matched!
00384         const double globalMuonIsolation0 = (*muonIso)[dau0];
00385         const double globalMuonIsolation1 = (*muonIso)[dau1];
00386         if((globalMuonIsolation0 < isomax_) && (globalMuonIsolation1 < isomax_)) {      // ZMuMu matched and selected by cuts
00387           ZMuMuMatchedSelectedfound = true;
00388           numberOfMatchedSelectedZMuMu_++;
00389           h_etaStandAlone_->Fill(dau0->eta());            // StandAlone found dau0, eta
00390           h_etaStandAlone_->Fill(dau1->eta());            // StandAlone found dau1, eta
00391           h_etaMuonOverlappedToStandAlone_->Fill(dau0->eta());  // is global muon so dau0 is also found as a track, eta
00392           h_etaMuonOverlappedToStandAlone_->Fill(dau1->eta());  // is global muon so dau1 is also found as a track, eta
00393           h_ptStandAlone_->Fill(dau0->pt());            // StandAlone found dau0, pt
00394           h_ptStandAlone_->Fill(dau1->pt());            // StandAlone found dau1, pt
00395           h_ptMuonOverlappedToStandAlone_->Fill(dau0->pt());  // is global muon so dau0 is also found as a track, pt
00396           h_ptMuonOverlappedToStandAlone_->Fill(dau1->pt());  // is global muon so dau1 is also found as a track, pt
00397 
00398           h_etaTrack_->Fill(dau0->eta());            // Track found dau0, eta
00399           h_etaTrack_->Fill(dau1->eta());            // Track found dau1, eta
00400           h_etaMuonOverlappedToTrack_->Fill(dau0->eta());  // is global muon so dau0 is also found as a StandAlone, eta
00401           h_etaMuonOverlappedToTrack_->Fill(dau1->eta());  // is global muon so dau1 is also found as a StandAlone, eta
00402           h_ptTrack_->Fill(dau0->pt());            // Track found dau0, pt
00403           h_ptTrack_->Fill(dau1->pt());            // Track found dau1, pt
00404           h_ptMuonOverlappedToTrack_->Fill(dau0->pt());  // is global muon so dau0 is also found as a StandAlone, pt
00405           h_ptMuonOverlappedToTrack_->Fill(dau1->pt());  // is global muon so dau1 is also found as a StandAlone, pt
00406 
00407           h_DELTA_ZMuMuMassReco_dimuonMassGen_->Fill(zMuMuCand.mass()-dimuonMassGen);
00408           // check that the two muons are matched . .per ora è solo un mio controllo
00409           for(unsigned int j = 0; j < muons->size() ; ++j) {
00410             CandidateBaseRef muCandRef = muons->refAt(j); 
00411             GenParticleRef muonMatch = (*muonMatchMap)[muCandRef]; 
00412             //      if (muonMatch.isNonnull()) cout << "mu match n. " << j << endl;
00413           }
00414         }
00415       }
00416     }     
00417   }
00418 
00419   if (zMuStandAlone->size() > 0) {
00420     numberOfEventsWithZMuStafound++;
00421     event.getByLabel(zMuStandAloneMatchMap_, zMuStandAloneMatchMap); 
00422     event.getByLabel(muonIso_, muonIso); 
00423     event.getByLabel(standAloneIso_, standAloneIso); 
00424     event.getByLabel(muonMatchMap_, muonMatchMap); 
00425     for(unsigned int i = 0; i < zMuStandAlone->size(); ++i) { //loop on candidates
00426       const Candidate & zMuStaCand = (*zMuStandAlone)[i]; //the candidate
00427       CandidateBaseRef zMuStaCandRef = zMuStandAlone->refAt(i);
00428       bool isMatched = false;
00429       GenParticleRef zMuStaMatch = (*zMuStandAloneMatchMap)[zMuStaCandRef];
00430       if(zMuStaMatch.isNonnull()) {        // ZMuSta Macthed
00431         isMatched = true;
00432         ZMuStaMatchedfound = true;
00433         numberOfMatchedZMuSta_++;
00434       }
00435       CandidateBaseRef dau0 = zMuStaCand.daughter(0)->masterClone();
00436       CandidateBaseRef dau1 = zMuStaCand.daughter(1)->masterClone();
00437       
00438       // Cuts
00439       if((dau0->pt() > ptmin_) && (dau1->pt() > ptmin_) && 
00440          (fabs(dau0->eta()) < etamax_) && (fabs(dau1->eta()) < etamax_) && 
00441          (zMuStaCand.mass() > zMassMin_) && (zMuStaCand.mass() < zMassMax_) && 
00442          (isMatched)) {
00443         CandidateBaseRef standAloneMuonCandRef_, globalMuonCandRef_;
00444         if(dau0->isGlobalMuon()) {
00445           standAloneMuonCandRef_ = dau1;
00446           globalMuonCandRef_ = dau0;
00447         }
00448         if(dau1->isGlobalMuon()) {
00449           standAloneMuonCandRef_ = dau0;
00450           globalMuonCandRef_ = dau1;
00451         }
00452 
00453         const double globalMuonIsolation = (*muonIso)[globalMuonCandRef_];
00454         const double standAloneMuonIsolation = (*standAloneIso)[standAloneMuonCandRef_];
00455         
00456         if((globalMuonIsolation < isomax_) && (standAloneMuonIsolation < isomax_)) {   // ZMuSta matched and selected
00457           //ZMuStaMatchedSelectedfound = true;
00458           numberOfMatchedSelectedZMuSta_++;
00459           h_etaStandAlone_->Fill(standAloneMuonCandRef_->eta()); //Denominator eta for measuring track efficiency
00460           h_ptStandAlone_->Fill(standAloneMuonCandRef_->pt());   //Denominator pt for measuring track eff
00461           h_DELTA_ZMuStaMassReco_dimuonMassGen_->Fill(zMuStaCand.mass()-dimuonMassGen); // differnce between ZMuSta reco and dimuon mass gen
00462 
00463         }
00464       }
00465     }
00466   } //end loop on Candidate
00467   
00468   //STANDALONE efficiency
00469 
00470   if (zMuTrack->size() > 0) {
00471     event.getByLabel(zMuTrackMatchMap_, zMuTrackMatchMap); 
00472     event.getByLabel(muonIso_, muonIso); 
00473     event.getByLabel(trackIso_, trackIso); 
00474     event.getByLabel(muonMatchMap_, muonMatchMap); 
00475     for(unsigned int i = 0; i < zMuTrack->size(); ++i) { //loop on candidates
00476       const Candidate & zMuTrkCand = (*zMuTrack)[i]; //the candidate
00477       CandidateBaseRef zMuTrkCandRef = zMuTrack->refAt(i);
00478       bool isMatched = false;
00479       GenParticleRef zMuTrkMatch = (*zMuTrackMatchMap)[zMuTrkCandRef];
00480       if(zMuTrkMatch.isNonnull()) {
00481         isMatched = true;
00482       }
00483       CandidateBaseRef dau0 = zMuTrkCand.daughter(0)->masterClone();
00484       CandidateBaseRef dau1 = zMuTrkCand.daughter(1)->masterClone();
00485  
00486       if (isMatched) {
00487         ZMuTrackMatchedfound++;
00488         if (ZMuMuMatchedfound) numberOfMatchedZMuTrack_matchedZMuMu++;
00489         if (ZMuMuMatchedSelectedfound) numberOfMatchedZMuTrack_matchedSelectedZMuMu++;
00490         if (!ZMuMuMatchedfound) numberOfMatchedZMuTrack_exclusive++;
00491       }
00492       // Cuts
00493       if ((dau0->pt() > ptmin_) && (dau1->pt() > ptmin_) && 
00494           (fabs(dau0->eta()) < etamax_) && (fabs(dau1->eta())< etamax_) && 
00495           (zMuTrkCand.mass() > zMassMin_) && (zMuTrkCand.mass() < zMassMax_) && 
00496           (isMatched) && !ZMuMuMatchedfound && !ZMuStaMatchedfound ) {         
00497 
00498         // dau0 is always the global muon, dau1 is the track for ZMuTrack collection
00499         const double globalMuonIsolation = (*muonIso)[dau0];
00500         const double trackMuonIsolation = (*trackIso)[dau1];
00501         if((globalMuonIsolation < isomax_) && (trackMuonIsolation < isomax_)) { // ZMuTRack matched - selected without ZMuMu found (exclusive)
00502           numberOfMatchedSelectedZMuTrack_exclusive++;
00503           ZMuTrackMatchedSelected_exclusivefound++;
00504           h_etaTrack_->Fill(dau1->eta()); //Denominator eta Sta
00505           h_ptTrack_->Fill(dau1->pt());   //Denominator pt Sta
00506           h_DELTA_ZMuTrackMassReco_dimuonMassGen_->Fill(zMuTrkCand.mass()-dimuonMassGen);
00507         }
00508 
00509       }
00510     }
00511   } //end loop on Candidate  
00512 
00513   if (!ZMuMuMatchedfound && !ZMuStaMatchedfound && ZMuTrackMatchedfound == 0) noMCmatching++;
00514   if (!ZMuMuMatchedfound && ZMuTrackMatchedfound == 1) ZMuTrack_exclusive_1match++;
00515   if (!ZMuMuMatchedfound && ZMuTrackMatchedfound > 1) ZMuTrack_exclusive_morematch++;
00516   if (!ZMuMuMatchedfound && ZMuTrackMatchedSelected_exclusivefound == 1) ZMuTrackselected_exclusive_1match++;
00517   if (!ZMuMuMatchedfound && ZMuTrackMatchedSelected_exclusivefound > 1) ZMuTrackselected_exclusive_morematch++;
00518   if (ZMuMuMatchedfound && ZMuTrackMatchedfound == 1) ZMuTrack_ZMuMu_1match++;
00519   if (ZMuMuMatchedfound && ZMuTrackMatchedfound == 2) ZMuTrack_ZMuMu_2match++;
00520   if (ZMuMuMatchedfound && ZMuTrackMatchedfound > 2) ZMuTrack_ZMuMu_morematch++;
00521  
00522 }
00523 
00524 bool ZMuMuEfficiency::check_ifZmumu(const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2)
00525 {
00526   int partId0 = dauGen0->pdgId();
00527   int partId1 = dauGen1->pdgId();
00528   int partId2 = dauGen2->pdgId();
00529   bool muplusFound=false;
00530   bool muminusFound=false;
00531   bool ZFound=false;
00532   if (partId0==13 || partId1==13 || partId2==13) muminusFound=true;
00533   if (partId0==-13 || partId1==-13 || partId2==-13) muplusFound=true;
00534   if (partId0==23 || partId1==23 || partId2==23) ZFound=true;
00535   return muplusFound*muminusFound*ZFound;   
00536 }
00537  
00538 float ZMuMuEfficiency::getParticlePt(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2)
00539 {
00540   int partId0 = dauGen0->pdgId();
00541   int partId1 = dauGen1->pdgId();
00542   int partId2 = dauGen2->pdgId();
00543   float ptpart=0.;
00544   if (partId0 == ipart) {
00545     for(unsigned int k = 0; k < dauGen0->numberOfDaughters(); ++k) {
00546       const Candidate * dauMuGen = dauGen0->daughter(k);
00547       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00548         ptpart = dauMuGen->pt();
00549       }
00550     }
00551   }
00552   if (partId1 == ipart) {
00553     for(unsigned int k = 0; k < dauGen1->numberOfDaughters(); ++k) {
00554       const Candidate * dauMuGen = dauGen1->daughter(k);
00555       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00556         ptpart = dauMuGen->pt();
00557       }
00558     }
00559   }
00560   if (partId2 == ipart) {
00561     for(unsigned int k = 0; k < dauGen2->numberOfDaughters(); ++k) {
00562       const Candidate * dauMuGen = dauGen2->daughter(k);
00563       if(abs(dauMuGen->pdgId()) == ipart && dauMuGen->status() ==1) {
00564         ptpart = dauMuGen->pt();
00565       }
00566     }
00567   }
00568   return ptpart;
00569 }
00570  
00571 float ZMuMuEfficiency::getParticleEta(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2)
00572 {
00573   int partId0 = dauGen0->pdgId();
00574   int partId1 = dauGen1->pdgId();
00575   int partId2 = dauGen2->pdgId();
00576   float etapart=0.;
00577   if (partId0 == ipart) {
00578     for(unsigned int k = 0; k < dauGen0->numberOfDaughters(); ++k) {
00579       const Candidate * dauMuGen = dauGen0->daughter(k);
00580       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00581         etapart = dauMuGen->eta();
00582       }
00583     }
00584   }
00585   if (partId1 == ipart) {
00586     for(unsigned int k = 0; k < dauGen1->numberOfDaughters(); ++k) {
00587       const Candidate * dauMuGen = dauGen1->daughter(k);
00588       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00589         etapart = dauMuGen->eta();
00590       }
00591     }
00592   }
00593   if (partId2 == ipart) {
00594     for(unsigned int k = 0; k < dauGen2->numberOfDaughters(); ++k) {
00595       const Candidate * dauMuGen = dauGen2->daughter(k);
00596       if(abs(dauMuGen->pdgId()) == ipart && dauMuGen->status() ==1) {
00597         etapart = dauMuGen->eta();
00598       }
00599     }
00600   }
00601   return etapart;
00602 }
00603 
00604 float ZMuMuEfficiency::getParticlePhi(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2)
00605 {
00606   int partId0 = dauGen0->pdgId();
00607   int partId1 = dauGen1->pdgId();
00608   int partId2 = dauGen2->pdgId();
00609   float phipart=0.;
00610   if (partId0 == ipart) {
00611     for(unsigned int k = 0; k < dauGen0->numberOfDaughters(); ++k) {
00612       const Candidate * dauMuGen = dauGen0->daughter(k);
00613       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00614         phipart = dauMuGen->phi();
00615       }
00616     }
00617   }
00618   if (partId1 == ipart) {
00619     for(unsigned int k = 0; k < dauGen1->numberOfDaughters(); ++k) {
00620       const Candidate * dauMuGen = dauGen1->daughter(k);
00621       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00622         phipart = dauMuGen->phi();
00623       }
00624     }
00625   }
00626   if (partId2 == ipart) {
00627     for(unsigned int k = 0; k < dauGen2->numberOfDaughters(); ++k) {
00628       const Candidate * dauMuGen = dauGen2->daughter(k);
00629       if(abs(dauMuGen->pdgId()) == ipart && dauMuGen->status() ==1) {
00630         phipart = dauMuGen->phi();
00631       }
00632     }
00633   }
00634   return phipart;
00635 }
00636 
00637 Particle::LorentzVector ZMuMuEfficiency::getParticleP4(const int ipart, const Candidate * dauGen0, const Candidate * dauGen1, const Candidate * dauGen2)
00638 {
00639   int partId0 = dauGen0->pdgId();
00640   int partId1 = dauGen1->pdgId();
00641   int partId2 = dauGen2->pdgId();
00642   Particle::LorentzVector p4part(0.,0.,0.,0.);
00643   if (partId0 == ipart) {
00644     for(unsigned int k = 0; k < dauGen0->numberOfDaughters(); ++k) {
00645       const Candidate * dauMuGen = dauGen0->daughter(k);
00646       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00647         p4part = dauMuGen->p4();
00648       }
00649     }
00650   }
00651   if (partId1 == ipart) {
00652     for(unsigned int k = 0; k < dauGen1->numberOfDaughters(); ++k) {
00653       const Candidate * dauMuGen = dauGen1->daughter(k);
00654       if(dauMuGen->pdgId() == ipart && dauMuGen->status() ==1) {
00655         p4part = dauMuGen->p4();
00656       }
00657     }
00658   }
00659   if (partId2 == ipart) {
00660     for(unsigned int k = 0; k < dauGen2->numberOfDaughters(); ++k) {
00661       const Candidate * dauMuGen = dauGen2->daughter(k);
00662       if(abs(dauMuGen->pdgId()) == ipart && dauMuGen->status() ==1) {
00663         p4part = dauMuGen->p4();
00664       }
00665     }
00666   }
00667   return p4part;
00668 }
00669  
00670 
00671 
00672 void ZMuMuEfficiency::endJob() {
00673   //  double efficiencySTA =(double)numberOfOverlappedStandAlone_/(double)numberOfMatchedZMuTrack_;
00674   //  double errorEff_STA = sqrt( efficiencySTA*(1 - efficiencySTA)/(double)numberOfMatchedZMuTrack_);
00675 
00676   double myTrackEff = 2.*numberOfMatchedSelectedZMuMu_/(2.*numberOfMatchedSelectedZMuMu_+(double)numberOfMatchedSelectedZMuSta_);
00677   double myErrTrackEff = sqrt(myTrackEff*(1-myTrackEff)/(2.*numberOfMatchedSelectedZMuMu_+(double)numberOfMatchedSelectedZMuSta_));
00678 
00679   double myStaEff = 2.*numberOfMatchedSelectedZMuMu_/(2.*numberOfMatchedSelectedZMuMu_+(double)numberOfMatchedSelectedZMuTrack_exclusive);
00680   double myErrStaEff = sqrt(myTrackEff*(1-myTrackEff)/(2.*numberOfMatchedSelectedZMuMu_+(double)numberOfMatchedSelectedZMuTrack_exclusive));
00681 
00682   //  double efficiencyTRACK =(double)numberOfOverlappedTracks_/(double)numberOfMatchedZMuSta_;
00683   //  double errorEff_TRACK = sqrt( efficiencyTRACK*(1 - efficiencyTRACK)/(double)numberOfMatchedZMuSta_);
00684 
00685   cout << "------------------------------------  Counters for MC acceptance --------------------------------" << endl;
00686   cout << "totalNumberOfevents = " << totalNumberOfevents << endl;
00687   cout << "totalNumberOfZfound = " << totalNumberOfZfound << endl;
00688   cout << "totalNumberOfZpassed = " << totalNumberOfZPassed << endl;
00689   cout << "n. of events zMuMu found (gen level selected)" <<   n_zMuMufound_genZsele << endl;
00690   cout << "n. of events zMuSta found (gen level selected)" <<   n_zMuStafound_genZsele << endl;
00691   cout << "n. of events zMuTrk found (gen level selected)" <<   n_zMuTrkfound_genZsele << endl;
00692 
00693   cout << "----------------------------   Counter for MC truth efficiency calculation--------------------- " << endl;
00694 
00695   cout << "number of events with ZMuMu found = " << numberOfEventsWithZMuMufound << endl; 
00696   cout << "number of events with ZMuSta found = " << numberOfEventsWithZMuStafound << endl; 
00697   cout << "-------------------------------------------------------------------------------------- " << endl;
00698 
00699   cout << "number of events without MC maching = " << noMCmatching << endl; 
00700   cout << "number of ZMuTrack exclsive 1 match = " << ZMuTrack_exclusive_1match << endl; 
00701   cout << "number of ZMuTrack exclsive more match = " << ZMuTrack_exclusive_morematch << endl; 
00702   cout << "number of ZMuTrack selected exclusive 1 match = " << ZMuTrackselected_exclusive_1match << endl; 
00703   cout << "number of ZMuTrack selected exclusive more match = " << ZMuTrackselected_exclusive_morematch << endl; 
00704   cout << "number of ZMuTrack ZMuMu 1 match = " << ZMuTrack_ZMuMu_1match << endl; 
00705   cout << "number of ZMuTrack ZMuMu 2 match = " << ZMuTrack_ZMuMu_2match << endl; 
00706   cout << "number of ZMuTrack ZMuMu more match = " << ZMuTrack_ZMuMu_morematch << endl; 
00707   cout << "numberOfMatchedZMuMu = " << numberOfMatchedZMuMu_ << endl; 
00708   cout << "numberOfMatchedSelectdZMuMu = " << numberOfMatchedSelectedZMuMu_ << endl; 
00709   cout << "numberOfMatchedZMuSta = " << numberOfMatchedZMuSta_ << endl; 
00710   cout << "numberOfMatchedSelectedZMuSta = " << numberOfMatchedSelectedZMuSta_ << endl; 
00711   cout << "numberOfMatchedZMuTrack_matchedZMuMu = " << numberOfMatchedZMuTrack_matchedZMuMu << endl; 
00712   cout << "numberOfMatchedZMuTrack_matchedSelectedZMuMu = " << numberOfMatchedZMuTrack_matchedSelectedZMuMu << endl; 
00713   cout << "numberOfMatchedZMuTrack exclusive = " << numberOfMatchedZMuTrack_exclusive << endl; 
00714   cout << "numberOfMatchedSelectedZMuTrack exclusive = " << numberOfMatchedSelectedZMuTrack_exclusive << endl; 
00715   cout << " ----------------------------- Efficiency --------------------------------- " << endl;
00716   cout << "Efficiency StandAlone = " << myStaEff << " +/- " << myErrStaEff << endl;
00717   cout << "Efficiency Track      = " << myTrackEff << " +/- " << myErrTrackEff << endl;
00718 }
00719   
00720 #include "FWCore/Framework/interface/MakerMacros.h"
00721 
00722 DEFINE_FWK_MODULE(ZMuMuEfficiency);
00723