CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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 overlap = false;
00144   
00145       // Trigger
00146       Handle<TriggerResults> triggerResults;
00147       TriggerNames trigNames;
00148       if (!ev.getByLabel(trigTag_, triggerResults)) {
00149         LogWarning("") << ">>> TRIGGER collection does not exist !!!";
00150         return;
00151       }
00152       ev.getByLabel(trigTag_, triggerResults); 
00153       // trigNames.init(*triggerResults);
00154       trigNames_ = &ev.triggerNames(*triggerResults);
00155       //bool trigger_fired = false;
00156       for (unsigned int i=0; i<triggerResults->size(); i++) {
00157         
00158 
00159         std::string trigName = trigNames_->triggerName(i);
00160         if ( trigName == hltPath_ && triggerResults->accept(i)) {
00161           //trigger_fired = true;
00162         }
00163       }   
00164       edm::Handle< trigger::TriggerEvent > handleTriggerEvent;
00165       //  LogTrace("") << ">>> Trigger bit: " << trigger_fired << " (" << hltPath_ << ")";
00166           if ( ! ev.getByLabel( trigEv_, handleTriggerEvent ))  {
00167         LogWarning( "errorTriggerEventValid" ) << "trigger::TriggerEvent product with InputTag " << trigEv_.encode() << " not in event";
00168         return;
00169       }
00170       ev.getByLabel( trigEv_, handleTriggerEvent );
00171       const trigger::TriggerObjectCollection & toc(handleTriggerEvent->getObjects());
00172       size_t nMuHLT =0;
00173       std::vector<reco::Particle>  HLTMuMatched; 
00174       for ( size_t ia = 0; ia < handleTriggerEvent->sizeFilters(); ++ ia) {
00175         std::string fullname = handleTriggerEvent->filterTag(ia).encode();
00176         std::string name;
00177         size_t p = fullname.find_first_of(':');
00178         if ( p != std::string::npos) {
00179           name = fullname.substr(0, p);
00180         }
00181         else {
00182           name = fullname;
00183         }
00184         if ( &toc !=0 ) {
00185           const trigger::Keys & k = handleTriggerEvent->filterKeys(ia);
00186           for (trigger::Keys::const_iterator ki = k.begin(); ki !=k.end(); ++ki ) {
00187             if (name == L3FilterName_  ) { 
00188               HLTMuMatched.push_back(toc[*ki].particle());
00189               nMuHLT++;     
00190             }
00191           }    
00192         }
00193       }
00194       
00195         //  looping on muon....
00196       Handle<View<Muon> >   muons;     
00197       if (!ev.getByLabel(muonTag_, muons)) {           
00198         LogError("") << ">>> muon collection does not exist !!!";     
00199         return;     
00200       }
00201       
00202       ev.getByLabel(muonTag_, muons);  
00203       //saving only muons with pt> ptMuCut and eta<etaMuCut  
00204       std::vector<reco::Muon>  highPtGlbMuons; 
00205     
00206       for (unsigned int i=0; i<muons->size(); i++ ){    
00207         const reco::Muon & mu = muons->at(i);
00208         double pt = mu.pt();
00209         double eta = mu.eta();
00210         if (pt> ptMuCut_ && fabs(eta)< etaMuCut_) {
00211           if (mu.isGlobalMuon()) highPtGlbMuons.push_back(mu);    
00212         }
00213       }
00214       unsigned int nHighPtGlbMu = highPtGlbMuons.size();
00215       std::cout << "I've got " << nHighPtGlbMu << " nHighPtGlbMu" << std::endl;
00216       // unsigned int nHighPtStaMu = highPtStaMuons.size();
00217 
00218         // stop the loop after 10 cicles....  
00219         (nHighPtGlbMu> 10)?   nHighPtGlbMu=10 : 1; 
00220 
00221         if (nHighPtGlbMu>0 ){
00222           
00223            for(unsigned int i =0 ; i < nHighPtGlbMu ; i++) {
00224             reco::Muon muon1 = highPtGlbMuons[i];
00225             math::XYZTLorentzVector mu1(muon1.p4());
00226             //      double pt1= muon1.pt();
00227         
00228             /* bool singleTrigFlag1 =*/ IsMuMatchedToHLTMu ( muon1,  HLTMuMatched ,maxDeltaR_, maxDPtRel_ );
00229             
00230           }
00231           
00232         }      
00233       
00234 }
00235 
00236 
00237 #include "FWCore/Framework/interface/MakerMacros.h"
00238 
00239 DEFINE_FWK_MODULE( MuTriggerAnalyzer );
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251