CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DPGAnalysis/Skims/interface/MatchedProbeMaker.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_TagAndProbe_MatchedProbeMaker_H
00002 #define PhysicsTools_TagAndProbe_MatchedProbeMaker_H
00003 
00004 // system include files
00005 #include <memory>
00006 #include <vector>
00007 
00008 // user include files
00009 #include "DataFormats/Common/interface/Handle.h"
00010 #include "DataFormats/Candidate/interface/Candidate.h"
00011 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00012 #include "DataFormats/Candidate/interface/OverlapChecker.h"
00013 #include "DataFormats/Math/interface/deltaR.h"
00014 #include "DataFormats/Common/interface/Ref.h"
00015 #include "DataFormats/Common/interface/RefVector.h"
00016 #include "DataFormats/Candidate/interface/Candidate.h"
00017 #include "DataFormats/Candidate/interface/CandMatchMap.h"
00018 #include "FWCore/Framework/interface/Frameworkfwd.h"
00019 #include "FWCore/Framework/interface/EDProducer.h"
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "FWCore/Utilities/interface/InputTag.h"
00024 
00025 //
00026 // class decleration
00027 //
00028 
00029 template< typename T >
00030 class MatchedProbeMaker : public edm::EDProducer 
00031 {
00032    public:
00033       typedef std::vector< T > collection;
00034 
00035       explicit MatchedProbeMaker(const edm::ParameterSet& iConfig);
00036 
00037       ~MatchedProbeMaker();
00038       
00039    private:
00040       virtual void beginJob() ;
00041       virtual void produce(edm::Event&, const edm::EventSetup&);
00042       virtual void endJob() ;
00043       
00044       // ----------member data ---------------------------
00045       edm::InputTag m_candidateSource;
00046       edm::InputTag m_referenceSource;
00047       edm::InputTag m_resMatchMapSource;
00048 
00049       bool matched_;    
00050 
00051 };
00052 
00053 template< typename T >
00054 MatchedProbeMaker<T>::MatchedProbeMaker(const edm::ParameterSet& iConfig) :
00055    m_candidateSource(iConfig.getUntrackedParameter<edm::InputTag>("CandidateSource")),
00056    m_referenceSource(iConfig.getUntrackedParameter<edm::InputTag>("ReferenceSource")),
00057    m_resMatchMapSource(iConfig.getUntrackedParameter<edm::InputTag>("ResMatchMapSource",edm::InputTag("Dummy"))),
00058    matched_(iConfig.getUntrackedParameter< bool >("Matched",true))
00059 {
00060    //register your products
00061    produces< edm::RefVector< collection > >();
00062 }
00063 
00064 
00065 template< typename T >
00066 MatchedProbeMaker<T>::~MatchedProbeMaker(){}
00067 
00068 
00069 template< typename T >
00070 void MatchedProbeMaker<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00071 {
00072   LogDebug("MatchedProbeMaker");
00073  
00074   using namespace edm;
00075   using namespace reco;
00076 
00077   std::auto_ptr< edm::RefVector< collection > > outputCollection_matched( new edm::RefVector< collection > );
00078   std::auto_ptr< edm::RefVector< collection > > outputCollection_unmatched(new edm::RefVector< collection > );
00079   
00080   // Get the candidates from the event
00081   edm::Handle< edm::RefVector< collection > > Cands;
00082   iEvent.getByLabel(m_candidateSource,Cands);
00083 
00084   edm::Handle< reco::CandidateView > Refs;
00085   iEvent.getByLabel(m_referenceSource,Refs);
00086 
00087   // Get the resolution matching map from the event
00088   edm::Handle<reco::CandViewMatchMap> ResMatchMap;
00089 
00090   if(iEvent.getByLabel(m_resMatchMapSource,ResMatchMap)){
00091     // Loop over the candidates looking for a match
00092     for (unsigned i=0; i<Cands->size(); i++) {
00093       const edm::Ref< collection > CandRef = (*Cands)[i];      
00094       reco::CandidateBaseRef candBaseRef( CandRef );
00095       
00096       // Loop over match map
00097       reco::CandViewMatchMap::const_iterator f = ResMatchMap->find( candBaseRef );
00098       if( f!=ResMatchMap->end() ) {
00099         outputCollection_matched->push_back(CandRef);      
00100       } else {
00101         outputCollection_unmatched->push_back(CandRef);      
00102       }
00103     }
00104   } else {
00105     OverlapChecker overlap;
00106     
00107     // Loop over the candidates looking for a match
00108     for (unsigned i=0; i<Cands->size(); i++) {      
00109       const edm::Ref< collection > CandRef = (*Cands)[i];
00110       //RefToBase<Candidate> CandRef(Cands, i);
00111       reco::CandidateBaseRef candBaseRef( CandRef );
00112       
00113       bool ppass = false;
00114       
00115       for (unsigned j=0; j<Refs->size(); j++) {
00116         //const edm::Ref< collection > RefRef = (*Refs)[j];
00117         RefToBase<Candidate> RefRef(Refs, j);
00118         reco::CandidateBaseRef refBaseRef( RefRef );
00119         
00120         if(overlap(*CandRef,*RefRef)) {
00121            ppass = true; 
00122         }
00123       }
00124       
00125       if( ppass ) outputCollection_matched->push_back(CandRef);
00126       else outputCollection_unmatched->push_back(CandRef);
00127     }  
00128   }
00129   
00130   if( matched_ ) iEvent.put( outputCollection_matched );
00131   else           iEvent.put( outputCollection_unmatched );
00132   
00133 }
00134 
00135 // ------------ method called once each job just before starting event loop  ------------
00136 template< typename T >
00137 void MatchedProbeMaker<T>::beginJob()
00138 {
00139 }
00140 
00141 // ------------ method called once each job just after ending the event loop  ------------
00142 template< typename T >
00143 void MatchedProbeMaker<T>::endJob() 
00144 {
00145 }
00146 
00147 #endif
00148 
00149