CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/PhysicsTools/PatExamples/plugins/PatTriggerAnalyzer.cc

Go to the documentation of this file.
00001 #include "TMath.h"
00002 #include "FWCore/ServiceRegistry/interface/Service.h"
00003 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00004 #include "PhysicsTools/PatUtils/interface/TriggerHelper.h"
00005 #include "PhysicsTools/PatExamples/plugins/PatTriggerAnalyzer.h"
00006 #include <iostream> // DEBUG
00007 
00008 
00009 using namespace pat;
00010 
00011 
00012 PatTriggerAnalyzer::PatTriggerAnalyzer( const edm::ParameterSet & iConfig ) :
00013   // pat::Trigger
00014   trigger_( iConfig.getParameter< edm::InputTag >( "trigger" ) ),
00015   // pat::TriggerEvent
00016   triggerEvent_( iConfig.getParameter< edm::InputTag >( "triggerEvent" ) ),
00017   // muon input collection
00018   muons_( iConfig.getParameter< edm::InputTag >( "muons" ) ),
00019   // muon match objects
00020   muonMatch_( iConfig.getParameter< std::string >( "muonMatch" ) ),
00021   // minimal id for of all trigger objects
00022   minID_( iConfig.getParameter< unsigned >( "minID" ) ),
00023   // maximal id for of all trigger objects
00024   maxID_( iConfig.getParameter< unsigned >( "maxID" ) ),
00025   histos1D_(), histos2D_()
00026 {
00027 }
00028 
00029 PatTriggerAnalyzer::~PatTriggerAnalyzer()
00030 {
00031 }
00032 
00033 void PatTriggerAnalyzer::beginJob()
00034 {
00035   edm::Service< TFileService > fileService;
00036 
00037   // pt correlation plot
00038   histos2D_[ "ptTrigCand" ] = fileService->make< TH2D >( "ptTrigCand", "Object vs. candidate p_{T} (GeV)", 60, 0., 300., 60, 0., 300. );
00039   histos2D_[ "ptTrigCand" ]->SetXTitle( "candidate p_{T} (GeV)" );
00040   histos2D_[ "ptTrigCand" ]->SetYTitle( "object p_{T} (GeV)" );
00041   // eta correlation plot
00042   histos2D_[ "etaTrigCand" ] = fileService->make< TH2D >( "etaTrigCand", "Object vs. candidate #eta", 50, -2.5, 2.5, 50, -2.5, 2.5 );
00043   histos2D_[ "etaTrigCand" ]->SetXTitle( "candidate #eta" );
00044   histos2D_[ "etaTrigCand" ]->SetYTitle( "object #eta" );
00045   // phi correlation plot
00046   histos2D_[ "phiTrigCand" ] = fileService->make< TH2D >( "phiTrigCand", "Object vs. candidate #phi", 60, -TMath::Pi(), TMath::Pi(), 60, -TMath::Pi(), TMath::Pi() );
00047   histos2D_[ "phiTrigCand" ]->SetXTitle( "candidate #phi" );
00048   histos2D_[ "phiTrigCand" ]->SetYTitle( "object #phi" );
00049   // turn-on curves
00050   histos1D_[ "turnOn" ] = fileService->make< TH1D >( "turnOn", "p_{T} (GeV) of matched candidate", 10, 0., 50.);
00051   histos1D_[ "turnOn" ]->SetXTitle( "candidate p_{T} (GeV)" );
00052   histos1D_[ "turnOn" ]->SetYTitle( "# of objects" );
00053   // mean pt for all trigger objects
00054   histos1D_[ "ptMean" ] = fileService->make< TH1D >( "ptMean", "Mean p_{T} (GeV) per filter ID", maxID_ - minID_ + 1, minID_ - 0.5, maxID_ + 0.5);
00055   histos1D_[ "ptMean" ]->SetXTitle( "filter ID" );
00056   histos1D_[ "ptMean" ]->SetYTitle( "mean p_{T} (GeV)" );
00057 
00058   // initialize counters for mean pt calculation
00059   for( unsigned id = minID_; id <= maxID_; ++id ){
00060     sumN_ [ id ] = 0;
00061     sumPt_[ id ] = 0.;
00062   }
00063 }
00064 
00065 void PatTriggerAnalyzer::analyze( const edm::Event & iEvent, const edm::EventSetup & iSetup )
00066 {
00067   // PAT trigger event
00068   edm::Handle< TriggerEvent > triggerEvent;
00069   iEvent.getByLabel( triggerEvent_, triggerEvent );
00070 
00071   // PAT object collection
00072   edm::Handle< MuonCollection > muons;
00073   iEvent.getByLabel( muons_, muons );
00074 
00075   // PAT trigger helper for trigger matching information
00076   const helper::TriggerMatchHelper matchHelper;
00077 
00078   /*
00079     kinematics comparison
00080   */
00081 
00082   // loop over muon references (PAT muons have been used in the matcher in task 3)
00083   for( size_t iMuon = 0; iMuon < muons->size(); ++iMuon ) {
00084     // we need all these ingedients to recieve matched trigger object from the matchHelper
00085     const TriggerObjectRef trigRef( matchHelper.triggerMatchObject( muons, iMuon, muonMatch_, iEvent, *triggerEvent ) );
00086     // finally we can fill the histograms
00087     if ( trigRef.isAvailable() && trigRef.isNonnull() ) { // check references (necessary!)
00088       histos2D_[ "ptTrigCand" ]->Fill( muons->at( iMuon ).pt(), trigRef->pt() );
00089       histos2D_[ "etaTrigCand" ]->Fill( muons->at( iMuon ).eta(), trigRef->eta() );
00090       histos2D_[ "phiTrigCand" ]->Fill( muons->at( iMuon ).phi(), trigRef->phi() );
00091     }
00092   }
00093 
00094   /*
00095     turn-on curve
00096   */
00097 
00098   // get the trigger objects corresponding to the used matching (HLT muons)
00099   const TriggerObjectRefVector trigRefs( triggerEvent->objects( trigger::TriggerMuon ) );
00100   // loop over selected trigger objects
00101   for ( TriggerObjectRefVector::const_iterator iTrig = trigRefs.begin(); iTrig != trigRefs.end(); ++iTrig ) {
00102     // get all matched candidates for the trigger object
00103     const reco::CandidateBaseRefVector candRefs( matchHelper.triggerMatchCandidates( ( *iTrig), muonMatch_, iEvent, *triggerEvent ) );
00104     if ( candRefs.empty() ) continue;
00105     // fill the histogram...
00106     // (only for the first match, since we resolved ambiguities in the matching configuration,
00107     // so that we have one at maximum per trigger object)
00108     reco::CandidateBaseRef muonRef( candRefs.at( 0 ) );
00109     if ( muonRef.isAvailable() && muonRef.isNonnull() ) {
00110       histos1D_[ "turnOn" ]->Fill( muonRef->pt() );
00111     }
00112   }
00113 
00114   /*
00115     mean pt
00116   */
00117 
00118   // loop over all trigger match objects from minID to maxID; have
00119   // a look to DataFormats/HLTReco/interface/TriggerTypeDefs.h to
00120   // know more about the available trrigger object id's
00121   for ( unsigned id=minID_; id<=maxID_; ++id ) {
00122     // vector of all objects for a given object id
00123     const TriggerObjectRefVector objRefs( triggerEvent->objects( id ) );
00124     // buffer the number of objects
00125     sumN_[ id ] += objRefs.size();
00126     // iterate the objects and buffer the pt of the objects
00127     for ( TriggerObjectRefVector::const_iterator iRef = objRefs.begin(); iRef != objRefs.end(); ++iRef ) {
00128       sumPt_[ id ] += ( *iRef )->pt();
00129     }
00130   }
00131 }
00132 
00133 void PatTriggerAnalyzer::endJob()
00134 {
00135   // normalize the entries for the mean pt plot
00136   for(unsigned id=minID_; id<=maxID_; ++id){
00137     if( sumN_[ id ]!=0 ) histos1D_[ "ptMean" ]->Fill( id, sumPt_[ id ]/sumN_[ id ] );
00138   }
00139 }
00140 
00141 
00142 #include "FWCore/Framework/interface/MakerMacros.h"
00143 DEFINE_FWK_MODULE( PatTriggerAnalyzer );