CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/ElectroWeakAnalysis/ZMuMu/plugins/MC_Efficiency_Analyzer.cc

Go to the documentation of this file.
00001 /* \class MCEfficiencyAnalyzer
00002  *
00003  * Muon reconstruction efficiency from MC truth, 
00004  * for global muons, tracks and standalone. Take as input the output of the
00005  * standard EWK skim: zToMuMu
00006  *
00007  * Produces in output the efficency number
00008  *
00009  * \author Michele de Gruttola, INFN Naples
00010 */
00011 
00012 #include "FWCore/Framework/interface/EDAnalyzer.h"
00013 #include "DataFormats/Math/interface/deltaR.h"
00014 #include "FWCore/Utilities/interface/InputTag.h"
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/Framework/interface/MakerMacros.h"
00017 #include "DataFormats/Candidate/interface/Particle.h"
00018 #include "DataFormats/Candidate/interface/Candidate.h"
00019 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00020 #include "DataFormats/Candidate/interface/CandMatchMap.h"
00021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00022 #include "DataFormats/Common/interface/Ref.h"
00023 #include <iostream>
00024 #include <iterator>
00025 using namespace edm;
00026 using namespace std;
00027 using namespace reco;
00028 
00029 class MCEfficiencyAnalyzer : public edm::EDAnalyzer 
00030 {
00031 public:
00032   MCEfficiencyAnalyzer(const edm::ParameterSet& pset) : 
00033     zMuMu_( pset.getParameter<InputTag>( "zMuMu" ) ),
00034     Muons_( pset.getParameter<InputTag>( "Muons" ) ),
00035     MuonsMap_( pset.getParameter<InputTag>( "MuonsMap" ) ),
00036     Tracks_( pset.getParameter<InputTag>( "Tracks" ) ),
00037     TracksMap_( pset.getParameter<InputTag>( "TracksMap" ) ),
00038     genParticles_( pset.getParameter<InputTag>( "genParticles" ) ),
00039     StandAlone_( pset.getParameter<InputTag>( "StandAlone" ) ),
00040     StandAloneMap_( pset.getParameter<InputTag>( "StandAloneMap" ) ),
00041     etacut_( pset.getParameter<double>( "etacut" ) ),
00042     ptcut_( pset.getParameter<double>( "ptcut" ) ),    
00043     deltaRStacut_( pset.getParameter<double>( "deltaRStacut" ) )
00044 
00045   { 
00046     nMuMC =0; nMureco =0; nTrk=0; nSta=0; nNotMuMatching =0 ;  
00047   }
00048   
00049   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup) 
00050   {
00051     
00052     Handle<CandidateCollection> zMuMu;
00053     event.getByLabel(zMuMu_, zMuMu);
00054     
00055     
00056     Handle<CandidateCollection> Muons;
00057     event.getByLabel(Muons_, Muons);
00058     
00059     Handle<CandidateCollection> Tracks;
00060     event.getByLabel(Tracks_, Tracks);
00061     
00062     Handle<CandidateCollection> StandAlone;
00063     event.getByLabel(StandAlone_, StandAlone);
00064     
00065     Handle<CandMatchMap> MuonsMap;
00066     event.getByLabel(MuonsMap_, MuonsMap);
00067     
00068     Handle<CandMatchMap> TracksMap;
00069     event.getByLabel(TracksMap_, TracksMap);
00070     
00071     Handle<CandMatchMap> StandAloneMap;
00072     event.getByLabel(StandAloneMap_, StandAloneMap);
00073     
00074     Handle<GenParticleCollection> genParticles;
00075     event.getByLabel(genParticles_, genParticles);
00076 
00077     //Getting muons from Z MC  
00078     for( unsigned int k = 0; k < genParticles->size(); k++ ) 
00079       {
00080         const Candidate & ZCand = (*genParticles)[ k ];
00081         int status = ZCand.status(); 
00082         
00083         if (ZCand.pdgId()==23&& status==3 )
00084           {
00085             // positive muons 
00086             const Candidate  *  muCand1 =   ZCand.daughter(0);
00087             if (muCand1->status()==3)   
00088               {
00089                 for (unsigned int d =0 ; d< muCand1->numberOfDaughters(); d++)
00090                   { 
00091                     const Candidate * muCandidate = muCand1->daughter(d);
00092                     if (muCandidate->pdgId() == muCand1->pdgId() )
00093                       {
00094                         muCand1 = muCand1->daughter(d);
00095                       }
00096                   } 
00097               }
00098             // negative muons
00099             const Candidate  *  muCand2 =   ZCand.daughter(1);
00100             if (muCand2->status()==3) 
00101               {         
00102                 for (unsigned int e =0 ; e< muCand2->numberOfDaughters(); e++)
00103                   { 
00104                     const Candidate * muCandidate = muCand2->daughter(e);
00105                     if (muCandidate->pdgId() == muCand2->pdgId() )
00106                       {
00107                         muCand2 = muCand2->daughter(e);
00108                       }
00109                   }
00110               }
00111             
00112             double deltaR_Mu_Sta =0;
00113             int nMurecoTemp = nMureco; 
00114             // getting mu matched
00115             CandMatchMap::const_iterator i;  
00116             for(i = MuonsMap->begin(); i != MuonsMap->end(); i++ )  
00117               {
00118                 const Candidate/* & reco = * i -> key,*/ &  mc =  * i -> val;
00119                 if ((muCand1 == &mc) && (mc.pt()>ptcut_) && (abs(mc.eta()<etacut_)))
00120                   {
00121                     nMuMC++;
00122                     nMureco++;
00123                     break;
00124                   }
00125               }
00126             if (nMureco == nMurecoTemp ) // I.E. MU RECO NOT FOUND!!!!  
00127               {
00128                 int nTrkTemp = nTrk;                
00129                 int nStaTemp = nSta;
00130  
00131                 // getting tracks matched and doing the same, CONTROLLING IF MU IS RECONSTRUCTED AS A TRACK AND NOT AS A MU
00132                 CandMatchMap::const_iterator l;                 
00133                 for(l = TracksMap->begin(); l != TracksMap->end(); l++ )
00134                   {
00135                     const Candidate /* & Trkreco = * l -> key, */ &  Trkmc =  * l -> val;
00136                     if (( muCand1 == & Trkmc) && (Trkmc.pt()>ptcut_) && (abs(Trkmc.eta()<etacut_))) 
00137                       {
00138                         nMuMC++;
00139                         nTrk++;  
00140                         break;
00141                       }  
00142                   }
00143                 // the same for standalone  
00144                 CandMatchMap::const_iterator n;  
00145                 for(n = StandAloneMap->begin(); n != StandAloneMap->end(); n++ )                  
00146                   {
00147                     const Candidate & Stareco = * n -> key, &  Stamc =  * n -> val;
00148                     if ((muCand1 == &Stamc ) && (Stamc.pt()>ptcut_) && (abs(Stamc.eta()<etacut_)))  
00149                       {
00150                         nMuMC++;
00151                         nSta++;
00152                         deltaR_Mu_Sta = deltaR(Stareco, *muCand1);
00153                         
00154                         //      cout<<"Ho trovato un sta reco "<<endl;
00155                         break;
00156                       }
00157                   }
00158                 // controlling if sta and trk are reconstrucetd both, and if so the get the deltaR beetween muon MC and reco sta, to controll correlation to this happening  
00159                 if ((nSta == nStaTemp + 1) && (nTrk == nTrkTemp + 1 ) ) 
00160                   { 
00161                     nNotMuMatching ++; 
00162                     if ((deltaR_Mu_Sta< deltaRStacut_))
00163                       {
00164                     v_.push_back(deltaR_Mu_Sta) ;
00165                    cout << "Not matching from trk and sta matched to MC mu, to reconstruct a recoMU" << endl;
00166                       }
00167                   }
00168               }
00169           }
00170       }
00171   }
00172 
00173   
00174   virtual void endJob() 
00175   {
00176     
00177     
00178     cout <<"--- nMuMC == "<<nMuMC<<endl;
00179     cout <<"--- nMureco == "<<nMureco<<endl;
00180     cout <<"--- nSta == "<<nSta<<endl;
00181     cout <<"--- nTrk == "<<nTrk<<endl;
00182     cout <<"--- nNotMuMatching from a trk and sta matched to a Mu MC == "<<nNotMuMatching<<endl; 
00183     if (nMuMC!=0 )
00184       {
00185         cout<<" effMu == "<<(double) nMureco/nMuMC<<endl;
00186         cout<<" effTrk == "<< (double)(nTrk + nMureco) /nMuMC<<endl;  
00187         cout<<" effSta == "<< (double)(nSta + nMureco) / nMuMC<<endl;
00188       }   
00189      
00190 vector< int >::const_iterator p2;
00191     for (unsigned int i =0 ; i < v_.size(); ++i ) 
00192       {
00193         cout<<" delta R Mu Sta == "<< v_[i]<<endl;
00194         }
00195   }
00196   
00197   
00198   
00199   
00200   
00201 private:
00202   
00203   InputTag zMuMu_, Muons_, MuonsMap_, Tracks_, TracksMap_, genParticles_, StandAlone_,StandAloneMap_;
00204   double etacut_, ptcut_, deltaRStacut_;
00205   int nMuMC, nMureco, nTrk, nSta, nNotMuMatching;
00206   vector<double> v_;
00207 };
00208 
00209 
00210 DEFINE_FWK_MODULE(MCEfficiencyAnalyzer);