CMS 3D CMS Logo

GsfEleMVACut.cc
Go to the documentation of this file.
4 
5 
7 public:
9 
10  result_type operator()(const reco::GsfElectronPtr&) const final;
11 
13  void getEventContent(const edm::EventBase&) final;
14 
15  CandidateType candidateType() const final {
16  return ELECTRON;
17  }
18 
19 private:
20 
21  double value(const reco::CandidatePtr& cand) const final;
22 
23  // Cut formulas
24  const std::vector<std::string> mvaCutStrings_;
25  std::vector<StringObjectFunction<reco::GsfElectron>> cutFormula_;
26 
27  const int nCuts_;
28 
29  // Pre-computed MVA value map
32 
33 };
34 
37  "GsfEleMVACut");
38 
41  mvaCutStrings_(c.getParameter<std::vector<std::string> >("mvaCuts")),
43 {
44  edm::InputTag mvaValTag = c.getParameter<edm::InputTag>("mvaValueMapName");
45  contentTags_.emplace("mvaVal",mvaValTag);
46 
47  edm::InputTag mvaCatTag = c.getParameter<edm::InputTag>("mvaCategoriesMapName");
48  contentTags_.emplace("mvaCat",mvaCatTag);
49 
50  for (auto &cutString : mvaCutStrings_) {
52  }
53 }
54 
56 
57  auto mvaVal =
59  contentTokens_.emplace("mvaVal",mvaVal);
60 
61  auto mvaCat =
63  contentTokens_.emplace("mvaCat",mvaCat);
64 }
65 
67 
68  ev.getByLabel(contentTags_["mvaVal"], mvaValueMap_);
70 }
71 
75 
76  // in case we are by-value
77  const std::string& val_name = contentTags_.find("mvaVal")->second.instance();
78  const std::string& cat_name = contentTags_.find("mvaCat")->second.instance();
80  float val = -1.0;
81  int cat = -1;
83  mvaValueMap_.isValid() && mvaValueMap_->contains( cand.id() ) ) {
84  cat = (*mvaCategoriesMap_)[cand];
85  val = (*mvaValueMap_)[cand];
86  } else if ( mvaCategoriesMap_.isValid() && mvaValueMap_.isValid() &&
87  mvaCategoriesMap_->idSize() == 1 && mvaValueMap_->idSize() == 1 &&
88  cand.id() == edm::ProductID() ) {
89  // in case we have spoofed a ptr
90  //note this must be a 1:1 valuemap (only one product input)
91  cat = mvaCategoriesMap_->begin()[cand.key()];
92  val = mvaValueMap_->begin()[cand.key()];
93  } else if ( mvaCategoriesMap_.isValid() && mvaValueMap_.isValid() ){ // throw an exception
94  cat = (*mvaCategoriesMap_)[cand];
95  val = (*mvaValueMap_)[cand];
96  }
97 
98  // Find the cut formula
99  const int iCategory = mvaCategoriesMap_.isValid() ? cat : pat->userInt( cat_name );
100  if( iCategory >= nCuts_ )
101  throw cms::Exception(" Error in MVA categories: ")
102  << " found a particle with a category larger than max configured " << std::endl;
103 
104  // Look up the MVA value for this particle
105  const float mvaValue = mvaValueMap_.isValid() ? val : pat->userFloat( val_name );
106 
107  // Apply the cut and return the result
108  return mvaValue > cutFormula_[iCategory](*cand);
109 }
110 
112 
113  // in case we are by-value
114  const std::string& val_name =contentTags_.find("mvaVal")->second.instance();
116  float val = 0.0;
117  if( mvaCategoriesMap_.isValid() && mvaCategoriesMap_->contains( cand.id() ) &&
118  mvaValueMap_.isValid() && mvaValueMap_->contains( cand.id() ) ) {
119  val = (*mvaValueMap_)[cand];
120  } else if ( mvaCategoriesMap_.isValid() && mvaValueMap_.isValid() &&
121  mvaCategoriesMap_->idSize() == 1 && mvaValueMap_->idSize() == 1 &&
122  cand.id() == edm::ProductID() ) {
123  // in case we have spoofed a ptr
124  //note this must be a 1:1 valuemap (only one product input)
125  val = mvaValueMap_->begin()[cand.key()];
126  } else if ( mvaCategoriesMap_.isValid() && mvaValueMap_.isValid() ){ // throw an exception
127  val = (*mvaValueMap_)[cand];
128  }
129 
130  const float mvaValue = mvaValueMap_.isValid() ? val : pat->userFloat( val_name );
131  return mvaValue;
132 }
size
Write out results.
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
result_type operator()(const reco::GsfElectronPtr &) const final
Definition: GsfEleMVACut.cc:74
edm::Handle< edm::ValueMap< int > > mvaCategoriesMap_
Definition: GsfEleMVACut.cc:31
key_type key() const
Definition: Ptr.h:185
double value(const reco::CandidatePtr &cand) const final
std::unordered_map< std::string, edm::EDGetToken > contentTokens_
GsfEleMVACut(const edm::ParameterSet &c)
Definition: GsfEleMVACut.cc:39
void setConsumes(edm::ConsumesCollector &) final
Definition: GsfEleMVACut.cc:55
const int nCuts_
Definition: GsfEleMVACut.cc:27
bool ev
std::vector< StringObjectFunction< reco::GsfElectron > > cutFormula_
Definition: GsfEleMVACut.cc:25
std::unordered_map< std::string, edm::InputTag > contentTags_
float userFloat(const std::string &key) const
Definition: PATObject.h:791
Definition: HeavyIon.h:7
bool contains(ProductID id) const
Definition: ValueMap.h:155
def cat(path)
Definition: eostools.py:401
void getEventContent(const edm::EventBase &) final
Definition: GsfEleMVACut.cc:66
bool isValid() const
Definition: HandleBase.h:74
size_t idSize() const
Definition: ValueMap.h:159
int32_t userInt(const std::string &key) const
Definition: PATObject.h:833
const_iterator begin() const
Definition: ValueMap.h:208
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:180
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:92
CandidateType candidateType() const final
Definition: GsfEleMVACut.cc:15
#define DEFINE_EDM_PLUGIN(factory, type, name)
edm::Handle< edm::ValueMap< float > > mvaValueMap_
Definition: GsfEleMVACut.cc:30
const std::vector< std::string > mvaCutStrings_
Definition: GsfEleMVACut.cc:24