Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:45

0001 #ifndef PhysicsTools_TagAndProbe_MatchedProbeMaker_H
0002 #define PhysicsTools_TagAndProbe_MatchedProbeMaker_H
0003 
0004 // system include files
0005 #include <memory>
0006 #include <vector>
0007 
0008 // user include files
0009 #include "DataFormats/Common/interface/Handle.h"
0010 #include "DataFormats/Candidate/interface/Candidate.h"
0011 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0012 #include "DataFormats/Candidate/interface/OverlapChecker.h"
0013 #include "DataFormats/Math/interface/deltaR.h"
0014 #include "DataFormats/Common/interface/Ref.h"
0015 #include "DataFormats/Common/interface/RefVector.h"
0016 #include "DataFormats/Candidate/interface/Candidate.h"
0017 #include "DataFormats/Candidate/interface/CandMatchMap.h"
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/one/EDProducer.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/Utilities/interface/InputTag.h"
0024 
0025 //
0026 // class decleration
0027 //
0028 
0029 template <typename T>
0030 class MatchedProbeMaker : public edm::one::EDProducer<> {
0031 public:
0032   typedef std::vector<T> collection;
0033 
0034   explicit MatchedProbeMaker(const edm::ParameterSet& iConfig);
0035 
0036   ~MatchedProbeMaker() override;
0037 
0038 private:
0039   void beginJob() override;
0040   void produce(edm::Event&, const edm::EventSetup&) override;
0041   void endJob() override;
0042 
0043   // ----------member data ---------------------------
0044   edm::InputTag m_candidateSource;
0045   edm::InputTag m_referenceSource;
0046   edm::InputTag m_resMatchMapSource;
0047 
0048   bool matched_;
0049 };
0050 
0051 template <typename T>
0052 MatchedProbeMaker<T>::MatchedProbeMaker(const edm::ParameterSet& iConfig)
0053     : m_candidateSource(iConfig.getUntrackedParameter<edm::InputTag>("CandidateSource")),
0054       m_referenceSource(iConfig.getUntrackedParameter<edm::InputTag>("ReferenceSource")),
0055       m_resMatchMapSource(iConfig.getUntrackedParameter<edm::InputTag>("ResMatchMapSource", edm::InputTag("Dummy"))),
0056       matched_(iConfig.getUntrackedParameter<bool>("Matched", true)) {
0057   //register your products
0058   produces<edm::RefVector<collection> >();
0059 }
0060 
0061 template <typename T>
0062 MatchedProbeMaker<T>::~MatchedProbeMaker() {}
0063 
0064 template <typename T>
0065 void MatchedProbeMaker<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0066   LogDebug("MatchedProbeMaker");
0067 
0068   using namespace edm;
0069   using namespace reco;
0070 
0071   std::unique_ptr<edm::RefVector<collection> > outputCollection_matched(new edm::RefVector<collection>);
0072   std::unique_ptr<edm::RefVector<collection> > outputCollection_unmatched(new edm::RefVector<collection>);
0073 
0074   // Get the candidates from the event
0075   edm::Handle<edm::RefVector<collection> > Cands;
0076   iEvent.getByLabel(m_candidateSource, Cands);
0077 
0078   edm::Handle<reco::CandidateView> Refs;
0079   iEvent.getByLabel(m_referenceSource, Refs);
0080 
0081   // Get the resolution matching map from the event
0082   edm::Handle<reco::CandViewMatchMap> ResMatchMap;
0083 
0084   if (iEvent.getByLabel(m_resMatchMapSource, ResMatchMap)) {
0085     // Loop over the candidates looking for a match
0086     for (unsigned i = 0; i < Cands->size(); i++) {
0087       const edm::Ref<collection> CandRef = (*Cands)[i];
0088       reco::CandidateBaseRef candBaseRef(CandRef);
0089 
0090       // Loop over match map
0091       reco::CandViewMatchMap::const_iterator f = ResMatchMap->find(candBaseRef);
0092       if (f != ResMatchMap->end()) {
0093         outputCollection_matched->push_back(CandRef);
0094       } else {
0095         outputCollection_unmatched->push_back(CandRef);
0096       }
0097     }
0098   } else {
0099     OverlapChecker overlap;
0100 
0101     // Loop over the candidates looking for a match
0102     for (unsigned i = 0; i < Cands->size(); i++) {
0103       const edm::Ref<collection> CandRef = (*Cands)[i];
0104       //RefToBase<Candidate> CandRef(Cands, i);
0105       reco::CandidateBaseRef candBaseRef(CandRef);
0106 
0107       bool ppass = false;
0108 
0109       for (unsigned j = 0; j < Refs->size(); j++) {
0110         //const edm::Ref< collection > RefRef = (*Refs)[j];
0111         RefToBase<Candidate> RefRef(Refs, j);
0112 
0113         if (overlap(*CandRef, *RefRef)) {
0114           ppass = true;
0115         }
0116       }
0117 
0118       if (ppass)
0119         outputCollection_matched->push_back(CandRef);
0120       else
0121         outputCollection_unmatched->push_back(CandRef);
0122     }
0123   }
0124 
0125   if (matched_)
0126     iEvent.put(std::move(outputCollection_matched));
0127   else
0128     iEvent.put(std::move(outputCollection_unmatched));
0129 }
0130 
0131 // ------------ method called once each job just before starting event loop  ------------
0132 template <typename T>
0133 void MatchedProbeMaker<T>::beginJob() {}
0134 
0135 // ------------ method called once each job just after ending the event loop  ------------
0136 template <typename T>
0137 void MatchedProbeMaker<T>::endJob() {}
0138 
0139 #endif