Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/stream/EDFilter.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 
0007 #include "DataFormats/Common/interface/ValueMap.h"
0008 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0009 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0010 #include "DataFormats/PatCandidates/interface/Electron.h"
0011 
0012 #include "EgammaAnalysis/ElectronTools/interface/ElectronEffectiveArea.h"
0013 
0014 class ElectronIsolatorFromEffectiveArea : public edm::stream::EDFilter<> {
0015 public:
0016   typedef edm::ValueMap<double> CandDoubleMap;
0017   typedef ElectronEffectiveArea EEA;
0018   explicit ElectronIsolatorFromEffectiveArea(const edm::ParameterSet&);
0019 
0020 private:
0021   bool filter(edm::Event&, const edm::EventSetup&) override;
0022   edm::InputTag gsfElectronTag;
0023   edm::InputTag pfElectronTag;
0024   edm::InputTag patElectronTag;
0025   edm::EDGetTokenT<reco::GsfElectronCollection> gsfElectronToken;
0026   edm::EDGetTokenT<reco::PFCandidateCollection> pfElectronToken;
0027   edm::EDGetTokenT<pat::ElectronCollection> patElectronToken;
0028   edm::EDGetTokenT<double> rhoIsoToken;
0029   const EEA::ElectronEffectiveAreaType modeEEA;
0030   const EEA::ElectronEffectiveAreaTarget targetEEA;
0031   static std::map<std::string, EEA::ElectronEffectiveAreaType> EEA_type();
0032   static std::map<std::string, EEA::ElectronEffectiveAreaTarget> EEA_target();
0033 };
0034 
0035 ElectronIsolatorFromEffectiveArea::ElectronIsolatorFromEffectiveArea(const edm::ParameterSet& config)
0036     : gsfElectronTag(edm::InputTag("")),
0037       pfElectronTag(edm::InputTag("")),
0038       patElectronTag(edm::InputTag("")),
0039       rhoIsoToken(consumes<double>(config.getParameter<edm::InputTag>("rhoIso"))),
0040       modeEEA(EEA_type()[config.getParameter<std::string>("EffectiveAreaType")]),
0041       targetEEA(EEA_target()[config.getParameter<std::string>("EffectiveAreaTarget")]) {
0042   if (config.existsAs<edm::InputTag>("gsfElectrons"))
0043     gsfElectronTag = config.getParameter<edm::InputTag>("gsfElectrons");
0044   if (config.existsAs<edm::InputTag>("pfElectrons"))
0045     pfElectronTag = config.getParameter<edm::InputTag>("pfElectrons");
0046   if (config.existsAs<edm::InputTag>("patElectrons"))
0047     patElectronTag = config.getParameter<edm::InputTag>("patElectrons");
0048   if (!gsfElectronTag.label().empty())
0049     gsfElectronToken = consumes<reco::GsfElectronCollection>(gsfElectronTag);
0050   if (!pfElectronTag.label().empty())
0051     pfElectronToken = consumes<reco::PFCandidateCollection>(pfElectronTag);
0052   if (!patElectronTag.label().empty())
0053     patElectronToken = consumes<pat::ElectronCollection>(patElectronTag);
0054   produces<CandDoubleMap>();
0055 }
0056 
0057 bool ElectronIsolatorFromEffectiveArea::filter(edm::Event& event, const edm::EventSetup&) {
0058   std::unique_ptr<CandDoubleMap> product(new CandDoubleMap());
0059   CandDoubleMap::Filler filler(*product);
0060 
0061   edm::Handle<double> rho;
0062   event.getByToken(rhoIsoToken, rho);
0063 
0064   if (!gsfElectronTag.label().empty()) {
0065     edm::Handle<reco::GsfElectronCollection> gsfElectrons;
0066     event.getByToken(gsfElectronToken, gsfElectrons);
0067     std::vector<double> gsfCorrectionsEA;
0068     if (gsfElectrons.isValid()) {
0069       for (reco::GsfElectronCollection::const_iterator it = gsfElectrons->begin(); it != gsfElectrons->end(); ++it)
0070         gsfCorrectionsEA.push_back((*rho) *
0071                                    EEA::GetElectronEffectiveArea(modeEEA, it->superCluster()->eta(), targetEEA));
0072       filler.insert(gsfElectrons, gsfCorrectionsEA.begin(), gsfCorrectionsEA.end());
0073     }
0074   }
0075 
0076   if (!pfElectronTag.label().empty()) {
0077     edm::Handle<reco::PFCandidateCollection> pfElectrons;
0078     event.getByToken(pfElectronToken, pfElectrons);
0079     std::vector<double> pfCorrectionsEA;
0080     if (pfElectrons.isValid()) {
0081       for (reco::PFCandidateCollection::const_iterator it = pfElectrons->begin(); it != pfElectrons->end(); ++it)
0082         pfCorrectionsEA.push_back(
0083             (*rho) * EEA::GetElectronEffectiveArea(modeEEA, it->gsfElectronRef()->superCluster()->eta(), targetEEA));
0084       filler.insert(pfElectrons, pfCorrectionsEA.begin(), pfCorrectionsEA.end());
0085     }
0086   }
0087 
0088   if (!patElectronTag.label().empty()) {
0089     edm::Handle<pat::ElectronCollection> patElectrons;
0090     event.getByToken(patElectronToken, patElectrons);
0091     std::vector<double> patCorrectionsEA;
0092     if (patElectrons.isValid()) {
0093       for (pat::ElectronCollection::const_iterator it = patElectrons->begin(); it != patElectrons->end(); ++it)
0094         patCorrectionsEA.push_back((*rho) *
0095                                    EEA::GetElectronEffectiveArea(modeEEA, it->superCluster()->eta(), targetEEA));
0096       filler.insert(patElectrons, patCorrectionsEA.begin(), patCorrectionsEA.end());
0097     }
0098   }
0099 
0100   filler.fill();
0101   event.put(std::move(product));
0102   return true;
0103 }
0104 
0105 // These maps should really be static const members of interface/ElectronEffectiveArea.h
0106 // Here mapping strings to only a subset of the enum
0107 std::map<std::string, ElectronEffectiveArea::ElectronEffectiveAreaType> ElectronIsolatorFromEffectiveArea::EEA_type() {
0108   std::map<std::string, EEA::ElectronEffectiveAreaType> m;
0109   m["kEleGammaAndNeutralHadronIso03"] = EEA::kEleGammaAndNeutralHadronIso03;
0110   m["kEleGammaAndNeutralHadronIso04"] = EEA::kEleGammaAndNeutralHadronIso04;
0111   return m;
0112 }
0113 
0114 std::map<std::string, ElectronEffectiveArea::ElectronEffectiveAreaTarget>
0115 ElectronIsolatorFromEffectiveArea::EEA_target() {
0116   std::map<std::string, EEA::ElectronEffectiveAreaTarget> m;
0117   m["kEleEANoCorr"] = EEA::kEleEANoCorr;
0118   m["kEleEAData2011"] = EEA::kEleEAData2011;
0119   m["kEleEASummer11MC"] = EEA::kEleEASummer11MC;
0120   m["kEleEAFall11MC"] = EEA::kEleEAFall11MC;
0121   m["kEleEAData2012"] = EEA::kEleEAData2012;
0122   return m;
0123 }
0124 
0125 DEFINE_FWK_MODULE(ElectronIsolatorFromEffectiveArea);