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")),
99  variableHelper_(consumesCollector()) {
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
AnyMVAEstimatorRun2Base.h
edm::StreamID
Definition: StreamID.h:30
Handle.h
MVAValueMapProducer::MVAValueMapProducer
MVAValueMapProducer(const edm::ParameterSet &)
Definition: MVAValueMapProducer.h:93
MVAValueMapProducer::src_
const edm::EDGetTokenT< edm::View< ParticleType > > src_
Definition: MVAValueMapProducer.h:34
sistrip::View
View
Definition: ConstantsForView.h:26
patZpeak.handle
handle
Definition: patZpeak.py:23
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
eostools.cat
def cat(path)
Definition: eostools.py:401
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
edm::VParameterSet
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
edm::Handle
Definition: AssociativeIterator.h:50
MVAVariableHelper.h
createPayload.suffix
suffix
Definition: createPayload.py:281
MakerMacros.h
MVAValueMapProducer
Definition: MVAValueMapProducer.h:24
names
const std::string names[nVars_]
Definition: PhotonIDValueMapProducer.cc:122
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
MVAValueMapProducer::mvaValueMapNames_
const std::vector< std::string > mvaValueMapNames_
Definition: MVAValueMapProducer.h:40
EDGetToken.h
MVAValueMapProducer::mvaCategoriesMapNames_
const std::vector< std::string > mvaCategoriesMapNames_
Definition: MVAValueMapProducer.h:42
edm::global::EDProducer
Definition: EDProducer.h:32
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
MVAValueMapProducer::mvaRawValueMapNames_
const std::vector< std::string > mvaRawValueMapNames_
Definition: MVAValueMapProducer.h:41
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
Event.h
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
cand
Definition: decayParser.h:34
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::ParameterSetDescription::setUnknown
void setUnknown()
Definition: ParameterSetDescription.cc:39
edm::EventSetup
Definition: EventSetup.h:57
pat::ParticleType
ParticleType
Definition of particle types.
Definition: ParticleCode.h:17
MVAValueMapProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: MVAValueMapProducer.h:139
get
#define get
ValueMap.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
MVAValueMapProducer::mvaEstimators_
const std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
Definition: MVAValueMapProducer.h:37
Frameworkfwd.h
Exception
Definition: hltDiff.cc:246
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::helper::Filler
Definition: ValueMap.h:22
View.h
ParameterSet.h
EDProducer.h
JetChargeProducer_cfi.exp
exp
Definition: JetChargeProducer_cfi.py:6
edm::Event
Definition: Event.h:73
MVAValueMapProducer::variableHelper_
const MVAVariableHelper variableHelper_
Definition: MVAValueMapProducer.h:45
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
MVAVariableHelper
Definition: MVAVariableHelper.h:23
label
const char * label
Definition: PFTauDecayModeTools.cc:11
MVAValueMapProducer::produce
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: MVAValueMapProducer.h:109