CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/PhysicsTools/PatAlgos/plugins/PATTriggerMatchEmbedder.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    PatAlgos
00004 // Class:      pat::PATTriggerMatchEmbedder
00005 //
00015 //
00016 // $Id: PATTriggerMatchEmbedder.cc,v 1.6 2010/09/02 17:52:47 vadler Exp $
00017 //
00018 
00019 
00020 #include <vector>
00021 
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/EDProducer.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 #include "FWCore/Utilities/interface/InputTag.h"
00027 #include "DataFormats/Common/interface/Handle.h"
00028 #include "DataFormats/Common/interface/RefToBase.h"
00029 
00030 #include "DataFormats/PatCandidates/interface/Electron.h"
00031 #include "DataFormats/PatCandidates/interface/Jet.h"
00032 #include "DataFormats/PatCandidates/interface/MET.h"
00033 #include "DataFormats/PatCandidates/interface/Muon.h"
00034 #include "DataFormats/PatCandidates/interface/Photon.h"
00035 #include "DataFormats/PatCandidates/interface/Tau.h"
00036 
00037 
00038 namespace pat {
00039 
00040   template< class PATObjectType >
00041   class PATTriggerMatchEmbedder : public edm::EDProducer {
00042 
00043       edm::InputTag src_;
00044       std::vector< edm::InputTag > matches_;
00045 
00046     public:
00047 
00048       explicit PATTriggerMatchEmbedder( const edm::ParameterSet & iConfig );
00049       ~PATTriggerMatchEmbedder() {};
00050 
00051     private:
00052 
00053       virtual void produce( edm::Event & iEvent, const edm::EventSetup & iSetup );
00054 
00055   };
00056 
00057   typedef PATTriggerMatchEmbedder< Electron > PATTriggerMatchElectronEmbedder;
00058   typedef PATTriggerMatchEmbedder< Jet >      PATTriggerMatchJetEmbedder;
00059   typedef PATTriggerMatchEmbedder< MET >      PATTriggerMatchMETEmbedder;
00060   typedef PATTriggerMatchEmbedder< Muon >     PATTriggerMatchMuonEmbedder;
00061   typedef PATTriggerMatchEmbedder< Photon >   PATTriggerMatchPhotonEmbedder;
00062   typedef PATTriggerMatchEmbedder< Tau >      PATTriggerMatchTauEmbedder;
00063 
00064 }
00065 
00066 
00067 using namespace pat;
00068 
00069 
00070 template< class PATObjectType >
00071 PATTriggerMatchEmbedder< PATObjectType >::PATTriggerMatchEmbedder( const edm::ParameterSet & iConfig ) :
00072   src_( iConfig.getParameter< edm::InputTag >( "src" ) ),
00073   matches_( iConfig.getParameter< std::vector< edm::InputTag > >( "matches" ) )
00074 {
00075   produces< std::vector< PATObjectType > >();
00076 }
00077 
00078 template< class PATObjectType >
00079 void PATTriggerMatchEmbedder< PATObjectType >::produce( edm::Event & iEvent, const edm::EventSetup & iSetup )
00080 {
00081   std::auto_ptr< std::vector< PATObjectType > > output( new std::vector< PATObjectType >() );
00082 
00083   edm::Handle< edm::View< PATObjectType > > candidates;
00084   iEvent.getByLabel( src_, candidates );
00085   if ( ! candidates.isValid() ) {
00086     edm::LogError( "missingInputSource" ) << "Input source with InputTag " << src_.encode() << " not in event.";
00087     return;
00088   }
00089 
00090   for ( typename edm::View< PATObjectType >::const_iterator iCand = candidates->begin(); iCand != candidates->end(); ++iCand ) {
00091     const unsigned index( iCand - candidates->begin() );
00092     PATObjectType cand( candidates->at( index ) );
00093     std::set< TriggerObjectStandAloneRef > cachedRefs;
00094     for ( size_t iMatch = 0; iMatch < matches_.size(); ++iMatch ) {
00095       edm::Handle< TriggerObjectStandAloneMatch > match;
00096       iEvent.getByLabel( matches_.at( iMatch ), match );
00097       if ( ! match.isValid() ) {
00098         edm::LogError( "missingInputMatch" ) << "Input match with InputTag " << matches_.at( iMatch ).encode() << " not in event.";
00099         continue;
00100       }
00101       const TriggerObjectStandAloneRef trigRef( ( *match )[ candidates->refAt( index ) ] );
00102       if ( trigRef.isNonnull() && trigRef.isAvailable() ) {
00103         if ( cachedRefs.insert( trigRef ).second ) { // protection from multiple entries of the same trigger objects
00104           cand.addTriggerObjectMatch( *trigRef );
00105         }
00106       }
00107     }
00108     output->push_back( cand );
00109   }
00110 
00111   iEvent.put( output );
00112 }
00113 
00114 
00115 #include "FWCore/Framework/interface/MakerMacros.h"
00116 
00117 DEFINE_FWK_MODULE( PATTriggerMatchElectronEmbedder );
00118 DEFINE_FWK_MODULE( PATTriggerMatchJetEmbedder );
00119 DEFINE_FWK_MODULE( PATTriggerMatchMETEmbedder );
00120 DEFINE_FWK_MODULE( PATTriggerMatchMuonEmbedder );
00121 DEFINE_FWK_MODULE( PATTriggerMatchPhotonEmbedder );
00122 DEFINE_FWK_MODULE( PATTriggerMatchTauEmbedder );