MatchedProbeMaker

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#ifndef PhysicsTools_TagAndProbe_MatchedProbeMaker_H
#define PhysicsTools_TagAndProbe_MatchedProbeMaker_H

// system include files
#include <memory>
#include <vector>

// user include files
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Candidate/interface/Candidate.h"
#include "DataFormats/Candidate/interface/CandidateFwd.h"
#include "DataFormats/Candidate/interface/OverlapChecker.h"
#include "DataFormats/Math/interface/deltaR.h"
#include "DataFormats/Common/interface/Ref.h"
#include "DataFormats/Common/interface/RefVector.h"
#include "DataFormats/Candidate/interface/Candidate.h"
#include "DataFormats/Candidate/interface/CandMatchMap.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"

//
// class decleration
//

template <typename T>
class MatchedProbeMaker : public edm::one::EDProducer<> {
public:
  typedef std::vector<T> collection;

  explicit MatchedProbeMaker(const edm::ParameterSet& iConfig);

  ~MatchedProbeMaker() override;

private:
  void beginJob() override;
  void produce(edm::Event&, const edm::EventSetup&) override;
  void endJob() override;

  // ----------member data ---------------------------
  edm::InputTag m_candidateSource;
  edm::InputTag m_referenceSource;
  edm::InputTag m_resMatchMapSource;

  bool matched_;
};

template <typename T>
MatchedProbeMaker<T>::MatchedProbeMaker(const edm::ParameterSet& iConfig)
    : m_candidateSource(iConfig.getUntrackedParameter<edm::InputTag>("CandidateSource")),
      m_referenceSource(iConfig.getUntrackedParameter<edm::InputTag>("ReferenceSource")),
      m_resMatchMapSource(iConfig.getUntrackedParameter<edm::InputTag>("ResMatchMapSource", edm::InputTag("Dummy"))),
      matched_(iConfig.getUntrackedParameter<bool>("Matched", true)) {
  //register your products
  produces<edm::RefVector<collection> >();
}

template <typename T>
MatchedProbeMaker<T>::~MatchedProbeMaker() {}

template <typename T>
void MatchedProbeMaker<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
  LogDebug("MatchedProbeMaker");

  using namespace edm;
  using namespace reco;

  std::unique_ptr<edm::RefVector<collection> > outputCollection_matched(new edm::RefVector<collection>);
  std::unique_ptr<edm::RefVector<collection> > outputCollection_unmatched(new edm::RefVector<collection>);

  // Get the candidates from the event
  edm::Handle<edm::RefVector<collection> > Cands;
  iEvent.getByLabel(m_candidateSource, Cands);

  edm::Handle<reco::CandidateView> Refs;
  iEvent.getByLabel(m_referenceSource, Refs);

  // Get the resolution matching map from the event
  edm::Handle<reco::CandViewMatchMap> ResMatchMap;

  if (iEvent.getByLabel(m_resMatchMapSource, ResMatchMap)) {
    // Loop over the candidates looking for a match
    for (unsigned i = 0; i < Cands->size(); i++) {
      const edm::Ref<collection> CandRef = (*Cands)[i];
      reco::CandidateBaseRef candBaseRef(CandRef);

      // Loop over match map
      reco::CandViewMatchMap::const_iterator f = ResMatchMap->find(candBaseRef);
      if (f != ResMatchMap->end()) {
        outputCollection_matched->push_back(CandRef);
      } else {
        outputCollection_unmatched->push_back(CandRef);
      }
    }
  } else {
    OverlapChecker overlap;

    // Loop over the candidates looking for a match
    for (unsigned i = 0; i < Cands->size(); i++) {
      const edm::Ref<collection> CandRef = (*Cands)[i];
      //RefToBase<Candidate> CandRef(Cands, i);
      reco::CandidateBaseRef candBaseRef(CandRef);

      bool ppass = false;

      for (unsigned j = 0; j < Refs->size(); j++) {
        //const edm::Ref< collection > RefRef = (*Refs)[j];
        RefToBase<Candidate> RefRef(Refs, j);

        if (overlap(*CandRef, *RefRef)) {
          ppass = true;
        }
      }

      if (ppass)
        outputCollection_matched->push_back(CandRef);
      else
        outputCollection_unmatched->push_back(CandRef);
    }
  }

  if (matched_)
    iEvent.put(std::move(outputCollection_matched));
  else
    iEvent.put(std::move(outputCollection_unmatched));
}

// ------------ method called once each job just before starting event loop  ------------
template <typename T>
void MatchedProbeMaker<T>::beginJob() {}

// ------------ method called once each job just after ending the event loop  ------------
template <typename T>
void MatchedProbeMaker<T>::endJob() {}

#endif