File indexing completed on 2024-04-06 12:25:09
0001 #include "PhysicsTools/SelectorUtils/interface/CutApplicatorWithEventContentBase.h"
0002 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0003
0004 class PhoMVACut : public CutApplicatorWithEventContentBase {
0005 public:
0006 PhoMVACut(const edm::ParameterSet& c);
0007
0008 result_type operator()(const reco::PhotonPtr&) const final;
0009
0010 void setConsumes(edm::ConsumesCollector&) final;
0011 void getEventContent(const edm::EventBase&) final;
0012
0013 double value(const reco::CandidatePtr& cand) const final;
0014
0015 CandidateType candidateType() const final { return PHOTON; }
0016
0017 private:
0018
0019 const std::vector<double> _mvaCutValues;
0020
0021
0022 edm::Handle<edm::ValueMap<float> > _mvaValueMap;
0023 edm::Handle<edm::ValueMap<int> > _mvaCategoriesMap;
0024 };
0025
0026 DEFINE_EDM_PLUGIN(CutApplicatorFactory, PhoMVACut, "PhoMVACut");
0027
0028 PhoMVACut::PhoMVACut(const edm::ParameterSet& c)
0029 : CutApplicatorWithEventContentBase(c), _mvaCutValues(c.getParameter<std::vector<double> >("mvaCuts")) {
0030 edm::InputTag mvaValTag = c.getParameter<edm::InputTag>("mvaValueMapName");
0031 contentTags_.emplace("mvaVal", mvaValTag);
0032
0033 edm::InputTag mvaCatTag = c.getParameter<edm::InputTag>("mvaCategoriesMapName");
0034 contentTags_.emplace("mvaCat", mvaCatTag);
0035 }
0036
0037 void PhoMVACut::setConsumes(edm::ConsumesCollector& cc) {
0038 auto mvaVal = cc.consumes<edm::ValueMap<float> >(contentTags_["mvaVal"]);
0039 contentTokens_.emplace("mvaVal", mvaVal);
0040
0041 auto mvaCat = cc.consumes<edm::ValueMap<int> >(contentTags_["mvaCat"]);
0042 contentTokens_.emplace("mvaCat", mvaCat);
0043 }
0044
0045 void PhoMVACut::getEventContent(const edm::EventBase& ev) {
0046 ev.getByLabel(contentTags_["mvaVal"], _mvaValueMap);
0047 ev.getByLabel(contentTags_["mvaCat"], _mvaCategoriesMap);
0048 }
0049
0050 CutApplicatorBase::result_type PhoMVACut::operator()(const reco::PhotonPtr& cand) const {
0051
0052 const std::string& val_name = contentTags_.find("mvaVal")->second.instance();
0053 const std::string& cat_name = contentTags_.find("mvaCat")->second.instance();
0054 edm::Ptr<pat::Photon> pat(cand);
0055 float val = -1.0;
0056 int cat = -1;
0057 if (_mvaCategoriesMap.isValid() && _mvaCategoriesMap->contains(cand.id()) && _mvaValueMap.isValid() &&
0058 _mvaValueMap->contains(cand.id())) {
0059 cat = (*_mvaCategoriesMap)[cand];
0060 val = (*_mvaValueMap)[cand];
0061 } else if (_mvaCategoriesMap.isValid() && _mvaValueMap.isValid() && _mvaCategoriesMap->idSize() == 1 &&
0062 _mvaValueMap->idSize() == 1 && cand.id() == edm::ProductID()) {
0063
0064
0065 cat = _mvaCategoriesMap->begin()[cand.key()];
0066 val = _mvaValueMap->begin()[cand.key()];
0067 } else if (_mvaCategoriesMap.isValid() && _mvaValueMap.isValid()) {
0068 cat = (*_mvaCategoriesMap)[cand];
0069 val = (*_mvaValueMap)[cand];
0070 }
0071
0072
0073 const int iCategory = _mvaCategoriesMap.isValid() ? cat : pat->userInt(cat_name);
0074 if (iCategory >= (int)(_mvaCutValues.size()))
0075 throw cms::Exception(" Error in MVA categories: ")
0076 << " found a particle with a category larger than max configured " << std::endl;
0077 const float cutValue = _mvaCutValues[iCategory];
0078
0079
0080 const float mvaValue = _mvaValueMap.isValid() ? val : pat->userFloat(val_name);
0081
0082
0083 return mvaValue > cutValue;
0084 }
0085
0086 double PhoMVACut::value(const reco::CandidatePtr& cand) const {
0087
0088 const std::string& val_name = contentTags_.find("mvaVal")->second.instance();
0089 edm::Ptr<pat::Photon> pat(cand);
0090 float val = 0.0;
0091 if (_mvaCategoriesMap.isValid() && _mvaCategoriesMap->contains(cand.id()) && _mvaValueMap.isValid() &&
0092 _mvaValueMap->contains(cand.id())) {
0093 val = (*_mvaValueMap)[cand];
0094 } else if (_mvaCategoriesMap.isValid() && _mvaValueMap.isValid() && _mvaCategoriesMap->idSize() == 1 &&
0095 _mvaValueMap->idSize() == 1 && cand.id() == edm::ProductID()) {
0096
0097
0098 val = _mvaValueMap->begin()[cand.key()];
0099 } else if (_mvaCategoriesMap.isValid() && _mvaValueMap.isValid()) {
0100 val = (*_mvaValueMap)[cand];
0101 }
0102 const float mvaValue = _mvaValueMap.isValid() ? val : pat->userFloat(val_name);
0103 return mvaValue;
0104 }