1 #ifndef __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
2 #define __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
28 template <
class ParticleType>
37 std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators;
41 for (
auto& imva : vConfig) {
45 mvaEstimators.emplace_back(
50 <<
" failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
57 std::vector<std::string>
names;
58 for (
auto& imva : vConfig) {
86 template <
typename ValueType,
class HandleType>
89 const std::vector<ValueType>&
values,
91 auto valMap = std::make_unique<edm::ValueMap<ValueType>>();
93 filler.insert(handle, values.begin(), values.end());
98 template <
class ParticleType>
100 const bool tagGiven = !keysForValueMapsTag.
label().empty();
107 template <
class ParticleType>
110 keysForValueMapsToken_(getKeysForValueMapsToken<
ParticleType>(
111 iConfig.getParameter<edm::
InputTag>(
"keysForValueMaps"), consumesCollector())),
112 mvaEstimators_(getMVAEstimators(iConfig.getParameterSetVector(
"mvaConfigurations"))),
113 mvaValueMapNames_(getValueMapNames(iConfig.getParameterSetVector(
"mvaConfigurations"),
"Values")),
114 mvaRawValueMapNames_(getValueMapNames(iConfig.getParameterSetVector(
"mvaConfigurations"),
"RawValues")),
115 mvaCategoriesMapNames_(getValueMapNames(iConfig.getParameterSetVector(
"mvaConfigurations"),
"Categories")),
116 variableHelper_(consumesCollector()) {
118 produces<edm::ValueMap<float>>(
name);
120 produces<edm::ValueMap<float>>(
name);
122 produces<edm::ValueMap<int>>(
name);
125 template <
class ParticleType>
129 std::vector<float> auxVariables = variableHelper_.getAuxVariables(iEvent);
131 auto srcHandle = iEvent.
getHandle(srcToken_);
132 auto keysForValueMapsHandle =
133 keysForValueMapsToken_.isUninitialized() ? srcHandle : iEvent.
getHandle(keysForValueMapsToken_);
136 if (!validated_ && !srcHandle->empty()) {
142 for (
unsigned iEstimator = 0; iEstimator < mvaEstimators_.size(); iEstimator++) {
143 std::vector<float> mvaValues;
144 std::vector<float> mvaRawValues;
145 std::vector<int> mvaCategories;
148 for (
auto const& cand : *srcHandle) {
150 const float response = mvaEstimators_[iEstimator]->mvaValue(&cand, auxVariables, cat);
151 mvaRawValues.push_back(response);
152 mvaValues.push_back(2.0 / (1.0 +
exp(-2.0 * response)) - 1);
153 mvaCategories.push_back(cat);
156 writeValueMap(iEvent, keysForValueMapsHandle, mvaValues, mvaValueMapNames_[iEstimator]);
157 writeValueMap(iEvent, keysForValueMapsHandle, mvaRawValues, mvaRawValueMapNames_[iEstimator]);
158 writeValueMap(iEvent, keysForValueMapsHandle, mvaCategories, mvaCategoriesMapNames_[iEstimator]);
163 template <
class ParticleType>
173 desc.
addVPSet(
"mvaConfigurations", mvaConfigurations);
const edm::EDGetTokenT< edm::View< ParticleType > > keysForValueMapsToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
const std::vector< std::string > mvaRawValueMapNames_
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
std::vector< ParameterSet > VParameterSet
const MVAVariableHelper variableHelper_
Exp< T >::type exp(const T &t)
const std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
const std::string names[nVars_]
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void addDefault(ParameterSetDescription const &psetDescription)
const std::vector< std::string > mvaCategoriesMapNames_
const std::vector< std::string > mvaValueMapNames_
static std::vector< std::string > getValueMapNames(const edm::VParameterSet &vConfig, std::string &&suffix)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::atomic< bool > validated_
MVAValueMapProducer(const edm::ParameterSet &)
const edm::EDGetTokenT< edm::View< ParticleType > > srcToken_
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
static auto getMVAEstimators(const edm::VParameterSet &vConfig)
ParticleType
Definition of particle types.