CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MVAValueMapProducer.h
Go to the documentation of this file.
1 #ifndef __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
2 #define __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
3 
20 
21 #include <atomic>
22 #include <cmath>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 template <class ParticleType>
29 public:
31 
32  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
33 
34 private:
35  static auto getMVAEstimators(const edm::VParameterSet& vConfig) {
36  std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators;
37 
38  // Loop over the list of MVA configurations passed here from python and
39  // construct all requested MVA estimators.
40  for (auto& imva : vConfig) {
41  // The factory below constructs the MVA of the appropriate type based
42  // on the "mvaName" which is the name of the derived MVA class (plugin)
43  if (!imva.empty()) {
44  mvaEstimators.emplace_back(
45  AnyMVAEstimatorRun2Factory::get()->create(imva.getParameter<std::string>("mvaName"), imva));
46 
47  } else
48  throw cms::Exception(" MVA configuration not found: ")
49  << " failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
50  }
51 
52  return mvaEstimators;
53  }
54 
55  static std::vector<std::string> getValueMapNames(const edm::VParameterSet& vConfig, std::string&& suffix) {
56  std::vector<std::string> names;
57  for (auto& imva : vConfig) {
58  names.push_back(imva.getParameter<std::string>("mvaName") + imva.getParameter<std::string>("mvaTag") + suffix);
59  }
60 
61  return names;
62  }
63 
64  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
65 
68 
69  // MVA estimators
70  const std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators_;
71 
72  // Value map names
73  const std::vector<std::string> mvaValueMapNames_;
74  const std::vector<std::string> mvaRawValueMapNames_;
75  const std::vector<std::string> mvaCategoriesMapNames_;
76 
77  // To get the auxiliary MVA variables
79 
80  CMS_THREAD_SAFE mutable std::atomic<bool> validated_ = false;
81 };
82 
83 namespace {
84 
85  template <typename ValueType, class HandleType>
86  void writeValueMap(edm::Event& iEvent,
88  const std::vector<ValueType>& values,
89  const std::string& label) {
90  auto valMap = std::make_unique<edm::ValueMap<ValueType>>();
91  typename edm::ValueMap<ValueType>::Filler filler(*valMap);
92  filler.insert(handle, values.begin(), values.end());
93  filler.fill();
94  iEvent.put(std::move(valMap), label);
95  }
96 
97  template <class ParticleType>
98  auto getKeysForValueMapsToken(edm::InputTag const& keysForValueMapsTag, edm::ConsumesCollector&& cc) {
99  const bool tagGiven = !keysForValueMapsTag.label().empty();
100  return tagGiven ? cc.consumes<edm::View<ParticleType>>(keysForValueMapsTag)
102  }
103 
104 } // namespace
105 
106 template <class ParticleType>
108  : srcToken_(consumes<edm::View<ParticleType>>(iConfig.getParameter<edm::InputTag>("src"))),
109  keysForValueMapsToken_(getKeysForValueMapsToken<ParticleType>(
110  iConfig.getParameter<edm::InputTag>("keysForValueMaps"), consumesCollector())),
111  mvaEstimators_(getMVAEstimators(iConfig.getParameterSetVector("mvaConfigurations"))),
112  mvaValueMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "Values")),
113  mvaRawValueMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "RawValues")),
114  mvaCategoriesMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "Categories")),
115  variableHelper_(consumesCollector()) {
116  for (auto const& name : mvaValueMapNames_)
117  produces<edm::ValueMap<float>>(name);
118  for (auto const& name : mvaRawValueMapNames_)
119  produces<edm::ValueMap<float>>(name);
120  for (auto const& name : mvaCategoriesMapNames_)
121  produces<edm::ValueMap<int>>(name);
122 }
123 
124 template <class ParticleType>
126  edm::Event& iEvent,
127  const edm::EventSetup& iSetup) const {
128  std::vector<float> auxVariables = variableHelper_.getAuxVariables(iEvent);
129 
130  auto srcHandle = iEvent.getHandle(srcToken_);
131  auto keysForValueMapsHandle =
132  keysForValueMapsToken_.isUninitialized() ? srcHandle : iEvent.getHandle(keysForValueMapsToken_);
133 
134  // check if nothing is wrong with the data format of the candidates
135  if (!validated_ && !srcHandle->empty()) {
136  egammaTools::validateEgammaCandidate((*srcHandle)[0]);
137  validated_ = true;
138  }
139 
140  // Loop over MVA estimators
141  for (unsigned iEstimator = 0; iEstimator < mvaEstimators_.size(); iEstimator++) {
142  std::vector<float> mvaValues;
143  std::vector<float> mvaRawValues;
144  std::vector<int> mvaCategories;
145 
146  // Loop over particles
147  for (auto const& cand : *srcHandle) {
148  int cat = -1; // Passed by reference to the mvaValue function to store the category
149  const float response = mvaEstimators_[iEstimator]->mvaValue(&cand, auxVariables, cat);
150  mvaRawValues.push_back(response); // The MVA score
151  mvaValues.push_back(2.0 / (1.0 + exp(-2.0 * response)) - 1); // MVA output between -1 and 1
152  mvaCategories.push_back(cat);
153  } // end loop over particles
154 
155  writeValueMap(iEvent, keysForValueMapsHandle, mvaValues, mvaValueMapNames_[iEstimator]);
156  writeValueMap(iEvent, keysForValueMapsHandle, mvaRawValues, mvaRawValueMapNames_[iEstimator]);
157  writeValueMap(iEvent, keysForValueMapsHandle, mvaCategories, mvaCategoriesMapNames_[iEstimator]);
158 
159  } // end loop over estimators
160 }
161 
162 template <class ParticleType>
165  desc.add<edm::InputTag>("src", {});
166  desc.add<edm::InputTag>("keysForValueMaps", {});
167  {
168  //The following says we do not know what parameters are allowed so do no validation
169  // Please change this to state exactly what you do use, even if it is no parameters
170  edm::ParameterSetDescription mvaConfigurations;
171  mvaConfigurations.setUnknown();
172  desc.addVPSet("mvaConfigurations", mvaConfigurations);
173  }
174  descriptions.addDefault(desc);
175 }
176 
177 #endif
const edm::EDGetTokenT< edm::View< ParticleType > > keysForValueMapsToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
const std::vector< std::string > mvaRawValueMapNames_
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:34
void validateEgammaCandidate(Candidate const &candidate)
const MVAVariableHelper variableHelper_
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
const std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
const std::string names[nVars_]
char const * label
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:563
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
const std::vector< std::string > mvaCategoriesMapNames_
def cat
Definition: eostools.py:401
const std::vector< std::string > mvaValueMapNames_
def move
Definition: eostools.py:511
tuple handle
Definition: patZpeak.py:23
static std::vector< std::string > getValueMapNames(const edm::VParameterSet &vConfig, std::string &&suffix)
#define CMS_THREAD_SAFE
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::atomic< bool > validated_
MVAValueMapProducer(const edm::ParameterSet &)
const edm::EDGetTokenT< edm::View< ParticleType > > srcToken_
std::string const & label() const
Definition: InputTag.h:36
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
#define get
static auto getMVAEstimators(const edm::VParameterSet &vConfig)
ParticleType
Definition of particle types.
Definition: ParticleCode.h:17