CMS 3D CMS Logo

MVAValueMapProducer.h
Go to the documentation of this file.
1 #ifndef __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
2 #define __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
3 
17 
18 #include <cmath>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 template <class ParticleType>
25 public:
27 
28  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
29 
30 private:
31  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
32 
33  // for AOD and MiniAOD case
35 
36  // MVA estimators
37  const std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators_;
38 
39  // Value map names
40  const std::vector<std::string> mvaValueMapNames_;
41  const std::vector<std::string> mvaRawValueMapNames_;
42  const std::vector<std::string> mvaCategoriesMapNames_;
43 
44  // To get the auxiliary MVA variables
46 };
47 
48 namespace {
49 
50  template <typename ValueType, class HandleType>
51  void writeValueMap(edm::Event& iEvent,
53  const std::vector<ValueType>& values,
54  const std::string& label) {
55  auto valMap = std::make_unique<edm::ValueMap<ValueType>>();
56  typename edm::ValueMap<ValueType>::Filler filler(*valMap);
57  filler.insert(handle, values.begin(), values.end());
58  filler.fill();
59  iEvent.put(std::move(valMap), label);
60  }
61 
62  auto getMVAEstimators(const edm::VParameterSet& vConfig) {
63  std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators;
64 
65  // Loop over the list of MVA configurations passed here from python and
66  // construct all requested MVA estimators.
67  for (auto& imva : vConfig) {
68  // The factory below constructs the MVA of the appropriate type based
69  // on the "mvaName" which is the name of the derived MVA class (plugin)
70  if (!imva.empty()) {
71  mvaEstimators.emplace_back(
72  AnyMVAEstimatorRun2Factory::get()->create(imva.getParameter<std::string>("mvaName"), imva));
73 
74  } else
75  throw cms::Exception(" MVA configuration not found: ")
76  << " failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
77  }
78 
79  return mvaEstimators;
80  }
81 
82  std::vector<std::string> getValueMapNames(const edm::VParameterSet& vConfig, std::string&& suffix) {
83  std::vector<std::string> names;
84  for (auto& imva : vConfig) {
85  names.push_back(imva.getParameter<std::string>("mvaName") + imva.getParameter<std::string>("mvaTag") + suffix);
86  }
87 
88  return names;
89  }
90 } // namespace
91 
92 template <class ParticleType>
94  : src_(consumes<edm::View<ParticleType>>(iConfig.getParameter<edm::InputTag>("src"))),
95  mvaEstimators_(getMVAEstimators(iConfig.getParameterSetVector("mvaConfigurations"))),
96  mvaValueMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "Values")),
97  mvaRawValueMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "RawValues")),
98  mvaCategoriesMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "Categories")),
100  for (auto const& name : mvaValueMapNames_)
101  produces<edm::ValueMap<float>>(name);
102  for (auto const& name : mvaRawValueMapNames_)
103  produces<edm::ValueMap<float>>(name);
104  for (auto const& name : mvaCategoriesMapNames_)
105  produces<edm::ValueMap<int>>(name);
106 }
107 
108 template <class ParticleType>
111  const edm::EventSetup& iSetup) const {
112  std::vector<float> auxVariables = variableHelper_.getAuxVariables(iEvent);
113 
114  auto src = iEvent.getHandle(src_);
115 
116  // Loop over MVA estimators
117  for (unsigned iEstimator = 0; iEstimator < mvaEstimators_.size(); iEstimator++) {
118  std::vector<float> mvaValues;
119  std::vector<float> mvaRawValues;
120  std::vector<int> mvaCategories;
121 
122  // Loop over particles
123  for (auto const& cand : src->ptrs()) {
124  int cat = -1; // Passed by reference to the mvaValue function to store the category
125  const float response = mvaEstimators_[iEstimator]->mvaValue(cand.get(), auxVariables, cat);
126  mvaRawValues.push_back(response); // The MVA score
127  mvaValues.push_back(2.0 / (1.0 + exp(-2.0 * response)) - 1); // MVA output between -1 and 1
128  mvaCategories.push_back(cat);
129  } // end loop over particles
130 
131  writeValueMap(iEvent, src, mvaValues, mvaValueMapNames_[iEstimator]);
132  writeValueMap(iEvent, src, mvaRawValues, mvaRawValueMapNames_[iEstimator]);
133  writeValueMap(iEvent, src, mvaCategories, mvaCategoriesMapNames_[iEstimator]);
134 
135  } // end loop over estimators
136 }
137 
138 template <class ParticleType>
140  //The following says we do not know what parameters are allowed so do no validation
141  // Please change this to state exactly what you do use, even if it is no parameters
143  desc.setUnknown();
144  descriptions.addDefault(desc);
145 }
146 
147 #endif
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
const std::vector< std::string > mvaRawValueMapNames_
def create(alignables, pedeDump, additionalData, outputFile, config)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
const MVAVariableHelper variableHelper_
const std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
const std::string names[nVars_]
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
char const * label
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:547
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:224
def cat(path)
Definition: eostools.py:401
void addDefault(ParameterSetDescription const &psetDescription)
const std::vector< std::string > mvaCategoriesMapNames_
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
const std::vector< std::string > mvaValueMapNames_
const edm::EDGetTokenT< edm::View< ParticleType > > src_
MVAValueMapProducer(const edm::ParameterSet &)
HLT enums.
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
const std::vector< float > getAuxVariables(const edm::Event &iEvent) const
def move(src, dest)
Definition: eostools.py:511
ParticleType
Definition of particle types.
Definition: ParticleCode.h:17