CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PhoMVACut.cc
Go to the documentation of this file.
3 
5 public:
7 
8  result_type operator()(const reco::PhotonPtr&) const final;
9 
11  void getEventContent(const edm::EventBase&) final;
12 
13  double value(const reco::CandidatePtr& cand) const final;
14 
16 
17 private:
18  // Cut values
19  const std::vector<double> _mvaCutValues;
20 
21  // Pre-computed MVA value map
24 };
25 
27 
29  : CutApplicatorWithEventContentBase(c), _mvaCutValues(c.getParameter<std::vector<double> >("mvaCuts")) {
30  edm::InputTag mvaValTag = c.getParameter<edm::InputTag>("mvaValueMapName");
31  contentTags_.emplace("mvaVal", mvaValTag);
32 
33  edm::InputTag mvaCatTag = c.getParameter<edm::InputTag>("mvaCategoriesMapName");
34  contentTags_.emplace("mvaCat", mvaCatTag);
35 }
36 
38  auto mvaVal = cc.consumes<edm::ValueMap<float> >(contentTags_["mvaVal"]);
39  contentTokens_.emplace("mvaVal", mvaVal);
40 
41  auto mvaCat = cc.consumes<edm::ValueMap<int> >(contentTags_["mvaCat"]);
42  contentTokens_.emplace("mvaCat", mvaCat);
43 }
44 
46  ev.getByLabel(contentTags_["mvaVal"], _mvaValueMap);
48 }
49 
51  // in case we are by-value
52  const std::string& val_name = contentTags_.find("mvaVal")->second.instance();
53  const std::string& cat_name = contentTags_.find("mvaCat")->second.instance();
54  edm::Ptr<pat::Photon> pat(cand);
55  float val = -1.0;
56  int cat = -1;
58  _mvaValueMap->contains(cand.id())) {
59  cat = (*_mvaCategoriesMap)[cand];
60  val = (*_mvaValueMap)[cand];
62  _mvaValueMap->idSize() == 1 && cand.id() == edm::ProductID()) {
63  // in case we have spoofed a ptr
64  //note this must be a 1:1 valuemap (only one product input)
65  cat = _mvaCategoriesMap->begin()[cand.key()];
66  val = _mvaValueMap->begin()[cand.key()];
67  } else if (_mvaCategoriesMap.isValid() && _mvaValueMap.isValid()) { // throw an exception
68  cat = (*_mvaCategoriesMap)[cand];
69  val = (*_mvaValueMap)[cand];
70  }
71 
72  // Find the cut value
73  const int iCategory = _mvaCategoriesMap.isValid() ? cat : pat->userInt(cat_name);
74  if (iCategory >= (int)(_mvaCutValues.size()))
75  throw cms::Exception(" Error in MVA categories: ")
76  << " found a particle with a category larger than max configured " << std::endl;
77  const float cutValue = _mvaCutValues[iCategory];
78 
79  // Look up the MVA value for this particle
80  const float mvaValue = _mvaValueMap.isValid() ? val : pat->userFloat(val_name);
81 
82  // Apply the cut and return the result
83  return mvaValue > cutValue;
84 }
85 
86 double PhoMVACut::value(const reco::CandidatePtr& cand) const {
87  // in case we are by-value
88  const std::string& val_name = contentTags_.find("mvaVal")->second.instance();
89  edm::Ptr<pat::Photon> pat(cand);
90  float val = 0.0;
92  _mvaValueMap->contains(cand.id())) {
93  val = (*_mvaValueMap)[cand];
95  _mvaValueMap->idSize() == 1 && cand.id() == edm::ProductID()) {
96  // in case we have spoofed a ptr
97  //note this must be a 1:1 valuemap (only one product input)
98  val = _mvaValueMap->begin()[cand.key()];
99  } else if (_mvaCategoriesMap.isValid() && _mvaValueMap.isValid()) {
100  val = (*_mvaValueMap)[cand];
101  }
102  const float mvaValue = _mvaValueMap.isValid() ? val : pat->userFloat(val_name);
103  return mvaValue;
104 }
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
const edm::EventSetup & c
std::unordered_map< std::string, edm::InputTag > contentTags_
key_type key() const
Definition: Ptr.h:163
PhoMVACut(const edm::ParameterSet &c)
Definition: PhoMVACut.cc:28
CandidateType candidateType() const final
Definition: PhoMVACut.cc:15
bool ev
edm::Handle< edm::ValueMap< float > > _mvaValueMap
Definition: PhoMVACut.cc:22
void getEventContent(const edm::EventBase &) final
Definition: PhoMVACut.cc:45
result_type operator()(const reco::PhotonPtr &) const final
Definition: PhoMVACut.cc:50
bool contains(ProductID id) const
Definition: ValueMap.h:155
double value(const reco::CandidatePtr &cand) const final
Definition: PhoMVACut.cc:86
def cat
Definition: eostools.py:401
bool isValid() const
Definition: HandleBase.h:70
const std::vector< double > _mvaCutValues
Definition: PhoMVACut.cc:19
void setConsumes(edm::ConsumesCollector &) final
Definition: PhoMVACut.cc:37
size_t idSize() const
Definition: ValueMap.h:157
std::unordered_map< std::string, edm::EDGetToken > contentTokens_
const_iterator begin() const
Definition: ValueMap.h:229
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:92
#define DEFINE_EDM_PLUGIN(factory, type, name)
edm::Handle< edm::ValueMap< int > > _mvaCategoriesMap
Definition: PhoMVACut.cc:23