CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/ElectroWeakAnalysis/ZMuMu/plugins/MuTriggerAnalyzer.cc

Go to the documentation of this file.
00001 #include "FWCore/Framework/interface/EDAnalyzer.h"
00002 #include "FWCore/Utilities/interface/InputTag.h"
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "DataFormats/Common/interface/Handle.h"
00005 #include "FWCore/Framework/interface/MakerMacros.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "DataFormats/Candidate/interface/Candidate.h"
00008 #include "FWCore/Common/interface/TriggerNames.h"
00009 #include "DataFormats/Common/interface/TriggerResults.h"
00010 #include "DataFormats/HLTReco/interface/TriggerObject.h"
00011 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
00012 #include "DataFormats/Math/interface/deltaR.h"
00013 #include "DataFormats/MuonReco/interface/Muon.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00016 #include "PhysicsTools/RooStatsCms/interface/ClopperPearsonBinomialInterval.h"
00017 #include "TGraphAsymmErrors.h" 
00018 #include "TH1.h"
00019 #include <numeric>
00020 #include <algorithm>
00021 #include <string>
00022 using namespace std;
00023 using namespace reco;
00024 using namespace edm;
00025 
00026 /*
00027 bool IsMuMatchedToHLTMu ( const reco::Candidate * dau, std::vector<reco::Particle> HLTMu , double DR, double DPtRel ) {
00028   unsigned int dim =  HLTMu.size();
00029   unsigned int nPass=0;
00030   if (dim==0) return false;
00031   for (unsigned int k =0; k< dim; k++ ) {
00032    if (  (deltaR(HLTMu[k], *dau) < DR)   && (fabs(HLTMu[k].pt() - dau->pt())/ HLTMu[k].pt()<DPtRel)){     nPass++ ;
00033     }
00034   }
00035   return (nPass>0);
00036 }
00037 
00038 bool IsMuMatchedToHLTSingleMu ( const reco::Candidate * dau, reco::Particle HLTMu , double DR, double DPtRel ) {
00039   unsigned int nPass=0;
00040   if (  (deltaR(HLTMu, *dau) < DR)   && (fabs(HLTMu.pt() - dau->pt())/ HLTMu.pt()<DPtRel)) {
00041     nPass++;
00042   }
00043   return (nPass>0);
00044 }
00045 
00046 */
00047 
00048 
00049 class MuTriggerAnalyzer : public edm::EDAnalyzer {
00050 public:
00051   MuTriggerAnalyzer(const edm::ParameterSet& pset );
00052 
00053 private:
00054   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup);
00055   virtual void endJob();
00056   bool IsMuMatchedToHLTMu ( const reco::Muon & , std::vector<reco::Particle> ,double ,double );
00057   
00058   edm::InputTag trigTag_;   
00059   edm::InputTag  trigEv_;
00060   edm::InputTag muonTag_;
00061   double ptMuCut_, etaMuCut_;
00062   std::string hltPath_;
00063   std::string L3FilterName_;
00064   edm::Handle<edm::TriggerResults> triggerResults_;
00065   edm::TriggerNames const* trigNames_;
00066   edm::Handle< trigger::TriggerEvent > handleTriggerEvent_;
00067 
00068   double maxDPtRel_, maxDeltaR_ ;
00069   const int nbins_;
00070   const double ptMax_;
00071   TH1D * hTrigMuonPtNumS_;
00072   TH1D * hTrigMuonPtDenS_;
00073   TH1D * deltaR_;
00074   TH1D * deltaPtOverPt_;
00075 
00076 
00077 }; 
00078 
00079 bool MuTriggerAnalyzer::IsMuMatchedToHLTMu ( const reco::Muon & mu, std::vector<reco::Particle> HLTMu , double DR, double DPtRel ) {
00080   size_t dim =  HLTMu.size();
00081   size_t nPass=0;
00082   
00083   // filling the denumerator;
00084   double muRecoPt= mu.pt();
00085   hTrigMuonPtDenS_-> Fill(muRecoPt);         
00086  
00087   if (dim==0) return false;
00088   for (size_t k =0; k< dim; k++ ) {
00089     if (  (deltaR(HLTMu[k], mu) < DR)   && (fabs(HLTMu[k].pt() - mu.pt())/ HLTMu[k].pt()<DPtRel)){     
00090       nPass++ ;
00091       std::cout<< " matching a muon " << std::endl;  
00092       std::cout  << "muon reco  pt : " << muRecoPt<< std::endl;     
00093       std::cout  << "muon reco  eta " <<  mu.eta() << std::endl;   
00094       std::cout << "muon trigger  pt "<< HLTMu[k].pt() << std::endl; 
00095       // filling the numerator, at the same bin as the denum..... 
00096       hTrigMuonPtNumS_-> Fill(muRecoPt);
00097       deltaR_-> Fill(deltaR(HLTMu[k], mu));
00098       deltaPtOverPt_-> Fill(fabs(HLTMu[k].pt() - mu.pt())/ HLTMu[k].pt());
00099 
00100          
00101       std::cout << "muon trigger  eta : "<< HLTMu[k].eta() << std::endl;   
00102       std::cout  <<"deltaR((HLTMu[k], mu)): "<< deltaR(HLTMu[k], mu) << std::endl;
00103       std::cout  <<"deltaPtOverPt: "<< fabs(HLTMu[k].pt() - mu.pt())/ HLTMu[k].pt() << std::endl;
00104     }
00105   }
00106   
00107   return (nPass>0);
00108 }
00109 
00110 
00111 
00112 MuTriggerAnalyzer::MuTriggerAnalyzer(const edm::ParameterSet& cfg ) :
00113   trigTag_(cfg.getParameter<edm::InputTag> ("TrigTag")),
00114   trigEv_(cfg.getParameter<edm::InputTag> ("triggerEvent")),
00115   muonTag_(cfg.getUntrackedParameter<edm::InputTag>("muons")),
00116   ptMuCut_(cfg.getUntrackedParameter<double>("ptMuCut")),
00117   etaMuCut_(cfg.getUntrackedParameter<double>("etaMuCut")),
00118   hltPath_(cfg.getParameter<std::string >("hltPath")),
00119   L3FilterName_(cfg.getParameter<std::string >("L3FilterName")),
00120   maxDPtRel_(cfg.getParameter<double>("maxDPtRel")),
00121   maxDeltaR_(cfg.getParameter<double>("maxDeltaR")),
00122   nbins_(cfg.getParameter<double>("ptMax_")), 
00123   ptMax_(cfg.getParameter<double>("ptMax_")){
00124    Service<TFileService> fs;
00125  
00126  hTrigMuonPtNumS_ = fs->make<TH1D>("hTrigMuonPtNumS", "hTrigMuonPtNumS", nbins_ + 1, 0, ptMax_); 
00127   hTrigMuonPtDenS_ = fs->make<TH1D>("hTrigMuonPtDenS", "hTrigMuonPtDenS", nbins_ +1 , 0, ptMax_); 
00128   deltaR_ = fs->make<TH1D>("deltaR", "deltaR", nbins_+1, 0, maxDeltaR_); 
00129   deltaPtOverPt_ = fs->make<TH1D>("deltaPtOverPt", "deltaPtOverPt", nbins_ + 1, 0, maxDPtRel_); 
00130 
00131 }
00132 
00133 void MuTriggerAnalyzer::endJob() {
00134   for (int i = 0; i < nbins_+1; ++i){
00135     std::cout << "number of reco muon in bin " << i << " = " << hTrigMuonPtDenS_->GetBinContent(i) << std::endl;
00136     std::cout << "number of hlt muon in bin " << i << " = " << hTrigMuonPtNumS_->GetBinContent(i) << std::endl;
00137 
00138  
00139   }
00140 }
00141 
00142 void MuTriggerAnalyzer::analyze (const Event & ev, const EventSetup &) {
00143       bool singleTrigFlag1 = false;
00144           //      bool overlap = false;
00145   
00146       // Trigger
00147       Handle<TriggerResults> triggerResults;
00148       TriggerNames trigNames;
00149       if (!ev.getByLabel(trigTag_, triggerResults)) {
00150         LogWarning("") << ">>> TRIGGER collection does not exist !!!";
00151         return;
00152       }
00153       ev.getByLabel(trigTag_, triggerResults); 
00154       // trigNames.init(*triggerResults);
00155       trigNames_ = &ev.triggerNames(*triggerResults);
00156       bool trigger_fired = false;
00157       for (unsigned int i=0; i<triggerResults->size(); i++) {
00158         
00159 
00160         std::string trigName = trigNames_->triggerName(i);
00161         if ( trigName == hltPath_ && triggerResults->accept(i)) {
00162           trigger_fired = true;
00163         }
00164       }   
00165       edm::Handle< trigger::TriggerEvent > handleTriggerEvent;
00166       //  LogTrace("") << ">>> Trigger bit: " << trigger_fired << " (" << hltPath_ << ")";
00167           if ( ! ev.getByLabel( trigEv_, handleTriggerEvent ))  {
00168         LogWarning( "errorTriggerEventValid" ) << "trigger::TriggerEvent product with InputTag " << trigEv_.encode() << " not in event";
00169         return;
00170       }
00171       ev.getByLabel( trigEv_, handleTriggerEvent );
00172       const trigger::TriggerObjectCollection & toc(handleTriggerEvent->getObjects());
00173       size_t nMuHLT =0;
00174       std::vector<reco::Particle>  HLTMuMatched; 
00175       for ( size_t ia = 0; ia < handleTriggerEvent->sizeFilters(); ++ ia) {
00176         std::string fullname = handleTriggerEvent->filterTag(ia).encode();
00177         std::string name;
00178         size_t p = fullname.find_first_of(':');
00179         if ( p != std::string::npos) {
00180           name = fullname.substr(0, p);
00181         }
00182         else {
00183           name = fullname;
00184         }
00185         if ( &toc !=0 ) {
00186           const trigger::Keys & k = handleTriggerEvent->filterKeys(ia);
00187           for (trigger::Keys::const_iterator ki = k.begin(); ki !=k.end(); ++ki ) {
00188             if (name == L3FilterName_  ) { 
00189               HLTMuMatched.push_back(toc[*ki].particle());
00190               nMuHLT++;     
00191             }
00192           }    
00193         }
00194       }
00195       
00196         //  looping on muon....
00197       Handle<View<Muon> >   muons;     
00198       if (!ev.getByLabel(muonTag_, muons)) {           
00199         LogError("") << ">>> muon collection does not exist !!!";     
00200         return;     
00201       }
00202       
00203       ev.getByLabel(muonTag_, muons);  
00204       //saving only muons with pt> ptMuCut and eta<etaMuCut  
00205       std::vector<reco::Muon>  highPtGlbMuons; 
00206     
00207       for (unsigned int i=0; i<muons->size(); i++ ){    
00208         const reco::Muon & mu = muons->at(i);
00209         double pt = mu.pt();
00210         double eta = mu.eta();
00211         if (pt> ptMuCut_ && fabs(eta)< etaMuCut_) {
00212           if (mu.isGlobalMuon()) highPtGlbMuons.push_back(mu);    
00213         }
00214       }
00215       unsigned int nHighPtGlbMu = highPtGlbMuons.size();
00216       std::cout << "I've got " << nHighPtGlbMu << " nHighPtGlbMu" << std::endl;
00217       // unsigned int nHighPtStaMu = highPtStaMuons.size();
00218 
00219         // stop the loop after 10 cicles....  
00220         (nHighPtGlbMu> 10)?   nHighPtGlbMu=10 : 1; 
00221 
00222         if (nHighPtGlbMu>0 ){
00223           
00224            for(unsigned int i =0 ; i < nHighPtGlbMu ; i++) {
00225             reco::Muon muon1 = highPtGlbMuons[i];
00226             math::XYZTLorentzVector mu1(muon1.p4());
00227             //      double pt1= muon1.pt();
00228         
00229             singleTrigFlag1 = IsMuMatchedToHLTMu ( muon1,  HLTMuMatched ,maxDeltaR_, maxDPtRel_ );
00230             
00231           }
00232           
00233         }      
00234       
00235 }
00236 
00237 
00238 #include "FWCore/Framework/interface/MakerMacros.h"
00239 
00240 DEFINE_FWK_MODULE( MuTriggerAnalyzer );
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252