PhotonIsoProducer

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 140 141 142 143 144 145 146 147 148 149 150 151 152
// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDFilter.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
#include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
#include "DataFormats/PatCandidates/interface/Electron.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "TrackingTools/Records/interface/TransientTrackRecord.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "EgammaAnalysis/ElectronTools/interface/PFIsolationEstimator.h"
//
// class declaration
//

class PhotonIsoProducer : public edm::stream::EDFilter<> {
public:
  explicit PhotonIsoProducer(const edm::ParameterSet&);
  ~PhotonIsoProducer() override;

private:
  bool filter(edm::Event&, const edm::EventSetup&) override;

  // ----------member data ---------------------------
  bool verbose_;
  edm::EDGetTokenT<reco::VertexCollection> vertexToken_;
  edm::EDGetTokenT<reco::PhotonCollection> photonToken_;
  edm::EDGetTokenT<reco::PFCandidateCollection> particleFlowToken_;
  std::string nameIsoCh_;
  std::string nameIsoPh_;
  std::string nameIsoNh_;

  PFIsolationEstimator isolator;
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
PhotonIsoProducer::PhotonIsoProducer(const edm::ParameterSet& iConfig) {
  verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
  vertexToken_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexTag"));
  photonToken_ = consumes<reco::PhotonCollection>(iConfig.getParameter<edm::InputTag>("photonTag"));
  particleFlowToken_ = consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("particleFlowTag"));

  nameIsoCh_ = iConfig.getParameter<std::string>("nameValueMapIsoCh");
  nameIsoPh_ = iConfig.getParameter<std::string>("nameValueMapIsoPh");
  nameIsoNh_ = iConfig.getParameter<std::string>("nameValueMapIsoNh");

  produces<edm::ValueMap<double> >(nameIsoCh_);
  produces<edm::ValueMap<double> >(nameIsoPh_);
  produces<edm::ValueMap<double> >(nameIsoNh_);

  isolator.initializePhotonIsolation(kTRUE);  //NOTE: this automatically set all the correct defaul veto values
  isolator.setConeSize(0.3);
}

PhotonIsoProducer::~PhotonIsoProducer() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
}

//
// member functions
//

// ------------ method called on each new Event  ------------
bool PhotonIsoProducer::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
  std::unique_ptr<edm::ValueMap<double> > chIsoMap(new edm::ValueMap<double>());
  edm::ValueMap<double>::Filler chFiller(*chIsoMap);

  std::unique_ptr<edm::ValueMap<double> > phIsoMap(new edm::ValueMap<double>());
  edm::ValueMap<double>::Filler phFiller(*phIsoMap);

  std::unique_ptr<edm::ValueMap<double> > nhIsoMap(new edm::ValueMap<double>());
  edm::ValueMap<double>::Filler nhFiller(*nhIsoMap);

  edm::Handle<reco::VertexCollection> vertexCollection;
  iEvent.getByToken(vertexToken_, vertexCollection);

  edm::Handle<reco::PhotonCollection> phoCollection;
  iEvent.getByToken(photonToken_, phoCollection);
  const reco::PhotonCollection* recoPho = phoCollection.product();

  // All PF Candidate for alternate isolation
  edm::Handle<reco::PFCandidateCollection> pfCandidatesH;
  iEvent.getByToken(particleFlowToken_, pfCandidatesH);
  const reco::PFCandidateCollection thePfColl = *(pfCandidatesH.product());

  std::vector<double> chIsoValues;
  std::vector<double> phIsoValues;
  std::vector<double> nhIsoValues;
  chIsoValues.reserve(phoCollection->size());
  phIsoValues.reserve(phoCollection->size());
  nhIsoValues.reserve(phoCollection->size());

  unsigned int ivtx = 0;
  reco::VertexRef myVtxRef(vertexCollection, ivtx);

  for (reco::PhotonCollection::const_iterator aPho = recoPho->begin(); aPho != recoPho->end(); ++aPho) {
    isolator.fGetIsolation(&*aPho, &thePfColl, myVtxRef, vertexCollection);

    if (verbose_) {
      edm::LogPrint("PhotonIsoProducer") << " run " << iEvent.id().run() << " lumi " << iEvent.id().luminosityBlock()
                                         << " event " << iEvent.id().event();
      edm::LogPrint("PhotonIsoProducer") << " pt " << aPho->pt() << " eta " << aPho->eta() << " phi " << aPho->phi()
                                         << " charge " << aPho->charge() << " : ";

      edm::LogPrint("PhotonIsoProducer") << " ChargedIso " << isolator.getIsolationCharged();
      edm::LogPrint("PhotonIsoProducer") << " PhotonIso " << isolator.getIsolationPhoton();
      edm::LogPrint("PhotonIsoProducer") << " NeutralHadron Iso " << isolator.getIsolationNeutral();
    }

    chIsoValues.push_back(isolator.getIsolationCharged());
    phIsoValues.push_back(isolator.getIsolationPhoton());
    nhIsoValues.push_back(isolator.getIsolationNeutral());
  }

  chFiller.insert(phoCollection, chIsoValues.begin(), chIsoValues.end());
  chFiller.fill();

  phFiller.insert(phoCollection, phIsoValues.begin(), phIsoValues.end());
  phFiller.fill();

  nhFiller.insert(phoCollection, nhIsoValues.begin(), nhIsoValues.end());
  nhFiller.fill();

  iEvent.put(std::move(chIsoMap), nameIsoCh_);
  iEvent.put(std::move(phIsoMap), nameIsoPh_);
  iEvent.put(std::move(nhIsoMap), nameIsoNh_);

  return true;
}

//define this as a plug-in
DEFINE_FWK_MODULE(PhotonIsoProducer);