File indexing completed on 2025-02-05 23:51:09
0001 #ifndef CommonTools_ParticleFlow_ElectronIDPFCandidateSelectorDefinition
0002 #define CommonTools_ParticleFlow_ElectronIDPFCandidateSelectorDefinition
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/ConsumesCollector.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0016 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0017 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0018 #include "DataFormats/Common/interface/ValueMap.h"
0019 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0020 #include "CommonTools/ParticleFlow/interface/PFCandidateSelectorDefinition.h"
0021 #include <algorithm>
0022
0023 namespace pf2pat {
0024
0025 struct ElectronIDPFCandidateSelectorDefinition : public PFCandidateSelectorDefinition {
0026 ElectronIDPFCandidateSelectorDefinition(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0027 : electronsToken_(
0028 iC.consumes<reco::GsfElectronCollection>(cfg.getParameter<edm::InputTag>("recoGsfElectrons"))),
0029 electronIdToken_(iC.consumes<edm::ValueMap<float> >(cfg.getParameter<edm::InputTag>("electronIdMap"))) {
0030 if (cfg.exists("bitsToCheck")) {
0031 isBitMap_ = true;
0032 mask_ = 0;
0033 if (cfg.existsAs<std::vector<std::string> >("bitsToCheck")) {
0034 std::vector<std::string> strbits = cfg.getParameter<std::vector<std::string> >("bitsToCheck");
0035 for (std::vector<std::string>::const_iterator istrbit = strbits.begin(), estrbit = strbits.end();
0036 istrbit != estrbit;
0037 ++istrbit) {
0038 if (*istrbit == "id") {
0039 mask_ |= 1;
0040 } else if (*istrbit == "iso") {
0041 mask_ |= 2;
0042 } else if (*istrbit == "conv") {
0043 mask_ |= 4;
0044 } else if (*istrbit == "ip") {
0045 mask_ |= 8;
0046 } else
0047 throw cms::Exception("Configuration")
0048 << "ElectronIDPFCandidateSelector: "
0049 << "bitsToCheck allowed string values are only id(0), iso(1), conv(2), ip(3).\n"
0050 << "Otherwise, use uint32_t bitmask).\n";
0051 }
0052 } else if (cfg.existsAs<uint32_t>("bitsToCheck")) {
0053 mask_ = cfg.getParameter<uint32_t>("bitsToCheck");
0054 } else {
0055 throw cms::Exception("Configuration")
0056 << "ElectronIDPFCandidateSelector: "
0057 << "bitsToCheck must be either a vector of strings, or a uint32 bitmask.\n";
0058 }
0059 } else {
0060 isBitMap_ = false;
0061 value_ = cfg.getParameter<double>("electronIdCut");
0062 }
0063 }
0064
0065 static void fillPSetDescription(edm::ParameterSetDescription& desc) {
0066 desc.add<edm::InputTag>("recoGsfElectrons", edm::InputTag(""));
0067 desc.add<edm::InputTag>("electronIdMap", edm::InputTag(""));
0068
0069
0070 desc.add<double>("electronIdCut", 0.);
0071 }
0072
0073 void select(const HandleToCollection& hc, const edm::Event& e, const edm::EventSetup& s) {
0074 selected_.clear();
0075
0076 edm::Handle<reco::GsfElectronCollection> electrons;
0077 e.getByToken(electronsToken_, electrons);
0078
0079 edm::Handle<edm::ValueMap<float> > electronId;
0080 e.getByToken(electronIdToken_, electronId);
0081
0082 unsigned key = 0;
0083 for (collection::const_iterator pfc = hc->begin(); pfc != hc->end(); ++pfc, ++key) {
0084
0085 reco::GsfTrackRef PfTk = pfc->gsfTrackRef();
0086
0087
0088 if (PfTk.isNull())
0089 continue;
0090
0091 int match = -1;
0092
0093 for (auto it = electrons->begin(), ed = electrons->end(); it != ed; ++it) {
0094 if (it->gsfTrack() == PfTk) {
0095 match = it - electrons->begin();
0096 break;
0097 }
0098 }
0099
0100 if (match == -1) {
0101 for (auto it = electrons->begin(), ed = electrons->end(); it != ed; ++it) {
0102 if (std::count(it->ambiguousGsfTracks().begin(), it->ambiguousGsfTracks().end(), PfTk) > 0) {
0103 match = it - electrons->begin();
0104 break;
0105 }
0106 }
0107 }
0108
0109 if (match != -1) {
0110 reco::GsfElectronRef ref(electrons, match);
0111 float eleId = (*electronId)[ref];
0112 bool pass = false;
0113 if (isBitMap_) {
0114 uint32_t thisval = eleId;
0115 pass = ((thisval & mask_) == mask_);
0116 } else {
0117 pass = (eleId > value_);
0118 }
0119 if (pass) {
0120 selected_.push_back(reco::PFCandidate(*pfc));
0121 reco::PFCandidatePtr ptrToMother(hc, key);
0122 selected_.back().setSourceCandidatePtr(ptrToMother);
0123 }
0124 }
0125 }
0126 }
0127
0128 private:
0129 edm::EDGetTokenT<reco::GsfElectronCollection> electronsToken_;
0130 edm::EDGetTokenT<edm::ValueMap<float> > electronIdToken_;
0131 bool isBitMap_;
0132 uint32_t mask_;
0133 double value_;
0134 };
0135 }
0136
0137 #endif