Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-05 07:05:22

0001 // system include files
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/stream/EDFilter.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0013 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0014 #include "DataFormats/PatCandidates/interface/Electron.h"
0015 #include "DataFormats/VertexReco/interface/Vertex.h"
0016 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0017 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0018 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0019 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0020 #include "EgammaAnalysis/ElectronTools/interface/PFIsolationEstimator.h"
0021 //
0022 // class declaration
0023 //
0024 
0025 class PhotonIsoProducer : public edm::stream::EDFilter<> {
0026 public:
0027   explicit PhotonIsoProducer(const edm::ParameterSet&);
0028   ~PhotonIsoProducer() override;
0029 
0030 private:
0031   bool filter(edm::Event&, const edm::EventSetup&) override;
0032 
0033   // ----------member data ---------------------------
0034   bool verbose_;
0035   edm::EDGetTokenT<reco::VertexCollection> vertexToken_;
0036   edm::EDGetTokenT<reco::PhotonCollection> photonToken_;
0037   edm::EDGetTokenT<reco::PFCandidateCollection> particleFlowToken_;
0038   std::string nameIsoCh_;
0039   std::string nameIsoPh_;
0040   std::string nameIsoNh_;
0041 
0042   PFIsolationEstimator isolator;
0043 };
0044 
0045 //
0046 // constants, enums and typedefs
0047 //
0048 
0049 //
0050 // static data member definitions
0051 //
0052 
0053 //
0054 // constructors and destructor
0055 //
0056 PhotonIsoProducer::PhotonIsoProducer(const edm::ParameterSet& iConfig) {
0057   verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
0058   vertexToken_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexTag"));
0059   photonToken_ = consumes<reco::PhotonCollection>(iConfig.getParameter<edm::InputTag>("photonTag"));
0060   particleFlowToken_ = consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("particleFlowTag"));
0061 
0062   nameIsoCh_ = iConfig.getParameter<std::string>("nameValueMapIsoCh");
0063   nameIsoPh_ = iConfig.getParameter<std::string>("nameValueMapIsoPh");
0064   nameIsoNh_ = iConfig.getParameter<std::string>("nameValueMapIsoNh");
0065 
0066   produces<edm::ValueMap<double> >(nameIsoCh_);
0067   produces<edm::ValueMap<double> >(nameIsoPh_);
0068   produces<edm::ValueMap<double> >(nameIsoNh_);
0069 
0070   isolator.initializePhotonIsolation(kTRUE);  //NOTE: this automatically set all the correct defaul veto values
0071   isolator.setConeSize(0.3);
0072 }
0073 
0074 PhotonIsoProducer::~PhotonIsoProducer() {
0075   // do anything here that needs to be done at desctruction time
0076   // (e.g. close files, deallocate resources etc.)
0077 }
0078 
0079 //
0080 // member functions
0081 //
0082 
0083 // ------------ method called on each new Event  ------------
0084 bool PhotonIsoProducer::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0085   std::unique_ptr<edm::ValueMap<double> > chIsoMap(new edm::ValueMap<double>());
0086   edm::ValueMap<double>::Filler chFiller(*chIsoMap);
0087 
0088   std::unique_ptr<edm::ValueMap<double> > phIsoMap(new edm::ValueMap<double>());
0089   edm::ValueMap<double>::Filler phFiller(*phIsoMap);
0090 
0091   std::unique_ptr<edm::ValueMap<double> > nhIsoMap(new edm::ValueMap<double>());
0092   edm::ValueMap<double>::Filler nhFiller(*nhIsoMap);
0093 
0094   edm::Handle<reco::VertexCollection> vertexCollection;
0095   iEvent.getByToken(vertexToken_, vertexCollection);
0096 
0097   edm::Handle<reco::PhotonCollection> phoCollection;
0098   iEvent.getByToken(photonToken_, phoCollection);
0099   const reco::PhotonCollection* recoPho = phoCollection.product();
0100 
0101   // All PF Candidate for alternate isolation
0102   edm::Handle<reco::PFCandidateCollection> pfCandidatesH;
0103   iEvent.getByToken(particleFlowToken_, pfCandidatesH);
0104   const reco::PFCandidateCollection thePfColl = *(pfCandidatesH.product());
0105 
0106   std::vector<double> chIsoValues;
0107   std::vector<double> phIsoValues;
0108   std::vector<double> nhIsoValues;
0109   chIsoValues.reserve(phoCollection->size());
0110   phIsoValues.reserve(phoCollection->size());
0111   nhIsoValues.reserve(phoCollection->size());
0112 
0113   unsigned int ivtx = 0;
0114   reco::VertexRef myVtxRef(vertexCollection, ivtx);
0115 
0116   for (reco::PhotonCollection::const_iterator aPho = recoPho->begin(); aPho != recoPho->end(); ++aPho) {
0117     isolator.fGetIsolation(&*aPho, &thePfColl, myVtxRef, vertexCollection);
0118 
0119     if (verbose_) {
0120       edm::LogPrint("PhotonIsoProducer") << " run " << iEvent.id().run() << " lumi " << iEvent.id().luminosityBlock()
0121                                          << " event " << iEvent.id().event();
0122       edm::LogPrint("PhotonIsoProducer") << " pt " << aPho->pt() << " eta " << aPho->eta() << " phi " << aPho->phi()
0123                                          << " charge " << aPho->charge() << " : ";
0124 
0125       edm::LogPrint("PhotonIsoProducer") << " ChargedIso " << isolator.getIsolationCharged();
0126       edm::LogPrint("PhotonIsoProducer") << " PhotonIso " << isolator.getIsolationPhoton();
0127       edm::LogPrint("PhotonIsoProducer") << " NeutralHadron Iso " << isolator.getIsolationNeutral();
0128     }
0129 
0130     chIsoValues.push_back(isolator.getIsolationCharged());
0131     phIsoValues.push_back(isolator.getIsolationPhoton());
0132     nhIsoValues.push_back(isolator.getIsolationNeutral());
0133   }
0134 
0135   chFiller.insert(phoCollection, chIsoValues.begin(), chIsoValues.end());
0136   chFiller.fill();
0137 
0138   phFiller.insert(phoCollection, phIsoValues.begin(), phIsoValues.end());
0139   phFiller.fill();
0140 
0141   nhFiller.insert(phoCollection, nhIsoValues.begin(), nhIsoValues.end());
0142   nhFiller.fill();
0143 
0144   iEvent.put(std::move(chIsoMap), nameIsoCh_);
0145   iEvent.put(std::move(phIsoMap), nameIsoPh_);
0146   iEvent.put(std::move(nhIsoMap), nameIsoNh_);
0147 
0148   return true;
0149 }
0150 
0151 //define this as a plug-in
0152 DEFINE_FWK_MODULE(PhotonIsoProducer);