CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/PhysicsTools/PatExamples/plugins/PatTriggerAnalyzer.cc

Go to the documentation of this file.
00001 #include <map>
00002 #include <string>
00003 
00004 #include "TH1D.h"
00005 #include "TH2D.h"
00006 
00007 #include "FWCore/Framework/interface/Event.h"
00008 #include "FWCore/Utilities/interface/InputTag.h"
00009 #include "FWCore/Framework/interface/EDAnalyzer.h"
00010 #include "FWCore/Framework/interface/Frameworkfwd.h"
00011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00012 #include "DataFormats/PatCandidates/interface/Muon.h"
00013 #include "DataFormats/PatCandidates/interface/TriggerEvent.h"
00014 
00015 
00016 class PatTriggerAnalyzer : public edm::EDAnalyzer {
00017 
00018  public:
00020   explicit PatTriggerAnalyzer( const edm::ParameterSet & iConfig );
00022   ~PatTriggerAnalyzer();
00023 
00024  private:
00026   virtual void beginJob();
00028   virtual void analyze( const edm::Event & iEvent, const edm::EventSetup & iSetup );
00030   virtual void endJob();
00031 
00033   edm::InputTag trigger_;
00035   edm::InputTag triggerEvent_;
00037   edm::InputTag muons_;
00039   std::string   muonMatch_;
00041   unsigned nBins_;
00042   double   binWidth_;
00044   unsigned minID_;
00046   unsigned maxID_;
00047 
00049   std::map< std::string, TH1D* > histos1D_;
00050   std::map< std::string, TH2D* > histos2D_;
00051 
00053   std::map< unsigned, unsigned > sumN_;
00054   std::map< unsigned, double >   sumPt_;
00055 };
00056 
00057 
00058 #include "TMath.h"
00059 #include "FWCore/ServiceRegistry/interface/Service.h"
00060 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00061 #include "PhysicsTools/PatUtils/interface/TriggerHelper.h"
00062 
00063 
00064 using namespace pat;
00065 
00066 
00067 PatTriggerAnalyzer::PatTriggerAnalyzer( const edm::ParameterSet & iConfig ) :
00068   // pat::Trigger
00069   trigger_( iConfig.getParameter< edm::InputTag >( "trigger" ) ),
00070   // pat::TriggerEvent
00071   triggerEvent_( iConfig.getParameter< edm::InputTag >( "triggerEvent" ) ),
00072   // muon input collection
00073   muons_( iConfig.getParameter< edm::InputTag >( "muons" ) ),
00074   // muon match objects
00075   muonMatch_( iConfig.getParameter< std::string >( "muonMatch" ) ),
00076   // binning for turn-on curve
00077   nBins_( iConfig.getParameter< unsigned >( "nBins" ) ),
00078   binWidth_( iConfig.getParameter< double >( "binWidth" ) ),
00079   // minimal id for of all trigger objects
00080   minID_( iConfig.getParameter< unsigned >( "minID" ) ),
00081   // maximal id for of all trigger objects
00082   maxID_( iConfig.getParameter< unsigned >( "maxID" ) ),
00083   histos1D_(), histos2D_()
00084 {
00085 }
00086 
00087 PatTriggerAnalyzer::~PatTriggerAnalyzer()
00088 {
00089 }
00090 
00091 void PatTriggerAnalyzer::beginJob()
00092 {
00093   edm::Service< TFileService > fileService;
00094 
00095   // pt correlation plot
00096   histos2D_[ "ptTrigCand" ] = fileService->make< TH2D >( "ptTrigCand", "Object vs. candidate p_{T} (GeV)", 60, 0., 300., 60, 0., 300. );
00097   histos2D_[ "ptTrigCand" ]->SetXTitle( "candidate p_{T} (GeV)" );
00098   histos2D_[ "ptTrigCand" ]->SetYTitle( "object p_{T} (GeV)" );
00099   // eta correlation plot
00100   histos2D_[ "etaTrigCand" ] = fileService->make< TH2D >( "etaTrigCand", "Object vs. candidate #eta", 50, -2.5, 2.5, 50, -2.5, 2.5 );
00101   histos2D_[ "etaTrigCand" ]->SetXTitle( "candidate #eta" );
00102   histos2D_[ "etaTrigCand" ]->SetYTitle( "object #eta" );
00103   // phi correlation plot
00104   histos2D_[ "phiTrigCand" ] = fileService->make< TH2D >( "phiTrigCand", "Object vs. candidate #phi", 60, -TMath::Pi(), TMath::Pi(), 60, -TMath::Pi(), TMath::Pi() );
00105   histos2D_[ "phiTrigCand" ]->SetXTitle( "candidate #phi" );
00106   histos2D_[ "phiTrigCand" ]->SetYTitle( "object #phi" );
00107   // candidate counter for turn-on curve
00108   histos1D_[ "countCand" ] = fileService->make< TH1D >( "countCand", "p_{T} (GeV) of candidate", nBins_, 20., 20. + nBins_ * binWidth_ );
00109   histos1D_[ "countCand" ]->SetXTitle( "candidate p_{T} (GeV)" );
00110   histos1D_[ "countCand" ]->SetYTitle( "# of candidates" );
00111   // turn-on curve
00112   histos1D_[ "turnOn" ] = fileService->make< TH1D >( "turnOn", "p_{T} (GeV) of candidate", nBins_, 20., 20. + nBins_ * binWidth_ );
00113   histos1D_[ "turnOn" ]->SetXTitle( "candidate p_{T} (GeV)" );
00114   histos1D_[ "turnOn" ]->SetYTitle( "efficiency" );
00115   // mean pt for all trigger objects
00116   histos1D_[ "ptMean" ] = fileService->make< TH1D >( "ptMean", "Mean p_{T} (GeV) per trigger object type", maxID_ - minID_ + 1, minID_ - 0.5, maxID_ + 0.5);
00117   histos1D_[ "ptMean" ]->SetXTitle( "trigger object type" );
00118   histos1D_[ "ptMean" ]->SetYTitle( "mean p_{T} (GeV)" );
00119 
00120   // initialize counters for mean pt calculation
00121   for( unsigned id = minID_; id <= maxID_; ++id ) {
00122     sumN_ [ id ] = 0;
00123     sumPt_[ id ] = 0.;
00124   }
00125 }
00126 
00127 void PatTriggerAnalyzer::analyze( const edm::Event & iEvent, const edm::EventSetup & iSetup )
00128 {
00129   // PAT trigger event
00130   edm::Handle< TriggerEvent > triggerEvent;
00131   iEvent.getByLabel( triggerEvent_, triggerEvent );
00132 
00133   // PAT object collection
00134   edm::Handle< MuonCollection > muons;
00135   iEvent.getByLabel( muons_, muons );
00136 
00137   // PAT trigger helper for trigger matching information
00138   const helper::TriggerMatchHelper matchHelper;
00139 
00140   /*
00141     kinematics comparison
00142   */
00143 
00144   // loop over muon references (PAT muons have been used in the matcher in task 3)
00145   for( size_t iMuon = 0; iMuon < muons->size(); ++iMuon ) {
00146     // we need all these ingedients to recieve matched trigger objects from the matchHelper
00147     const TriggerObjectRef trigRef( matchHelper.triggerMatchObject( muons, iMuon, muonMatch_, iEvent, *triggerEvent ) );
00148     // finally we can fill the histograms
00149     if ( trigRef.isAvailable() && trigRef.isNonnull() ) { // check references (necessary!)
00150       histos2D_[ "ptTrigCand" ]->Fill( muons->at( iMuon ).pt(), trigRef->pt() );
00151       histos2D_[ "etaTrigCand" ]->Fill( muons->at( iMuon ).eta(), trigRef->eta() );
00152       histos2D_[ "phiTrigCand" ]->Fill( muons->at( iMuon ).phi(), trigRef->phi() );
00153     }
00154   }
00155 
00156   /*
00157     turn-on curve
00158   */
00159 
00160   // loop over muon references again
00161   for( size_t iMuon = 0; iMuon < muons->size(); ++iMuon ) {
00162     // fill the counting histogram...
00163     histos1D_[ "countCand" ]->Fill( muons->at( iMuon ).pt() );
00164   }
00165 
00166   // get the trigger objects corresponding to the used matching (HLT muons)
00167   const TriggerObjectRefVector trigRefs( triggerEvent->objects( trigger::TriggerMuon ) );
00168   // loop over selected trigger objects
00169   for ( TriggerObjectRefVector::const_iterator iTrig = trigRefs.begin(); iTrig != trigRefs.end(); ++iTrig ) {
00170     // get all matched candidates for the trigger object
00171     const reco::CandidateBaseRefVector candRefs( matchHelper.triggerMatchCandidates( ( *iTrig ), muonMatch_, iEvent, *triggerEvent ) );
00172     if ( candRefs.empty() ) continue;
00173     // fill the histogram...
00174     // (only for the first match, since we resolved ambiguities in the matching configuration,
00175     // so that we have one at maximum per trigger object)
00176     reco::CandidateBaseRef muonRef( candRefs.at( 0 ) );
00177     if ( muonRef.isAvailable() && muonRef.isNonnull() ) {
00178       histos1D_[ "turnOn" ]->Fill( muonRef->pt() );
00179     }
00180   }
00181 
00182   /*
00183     mean pt
00184   */
00185 
00186   // loop over all trigger objects from minID to maxID; have
00187   // a look to DataFormats/HLTReco/interface/TriggerTypeDefs.h to
00188   // know more about the available trrigger object IDs
00189   for ( unsigned id = minID_; id <= maxID_; ++id ) {
00190     // vector of all objects for a given object id
00191     const TriggerObjectRefVector objRefs( triggerEvent->objects( id ) );
00192     // buffer the number of objects
00193     sumN_[ id ] += objRefs.size();
00194     // iterate the objects and buffer the pt of the objects
00195     for ( TriggerObjectRefVector::const_iterator iRef = objRefs.begin(); iRef != objRefs.end(); ++iRef ) {
00196       sumPt_[ id ] += ( *iRef )->pt();
00197     }
00198   }
00199 }
00200 
00201 void PatTriggerAnalyzer::endJob()
00202 {
00203   /*
00204     turn-on curve
00205   */
00206 
00207   // normalise bins for turn-on based with counter
00208   histos1D_[ "turnOn" ]->Divide( histos1D_[ "countCand" ] );
00209 
00210   /*
00211     mean pt
00212   */
00213 
00214   // normalize the entries for the mean pt plot
00215   for(unsigned id=minID_; id<=maxID_; ++id){
00216     if( sumN_[ id ] != 0 ) histos1D_[ "ptMean" ]->Fill( id, sumPt_[ id ] / sumN_[ id ] );
00217   }
00218 }
00219 
00220 
00221 #include "FWCore/Framework/interface/MakerMacros.h"
00222 DEFINE_FWK_MODULE( PatTriggerAnalyzer );