1 #ifndef __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
2 #define __RecoEgamma_EgammaTools_MVAValueMapProducer_H__
27 template <
class ParticleType>
36 std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators;
40 for (
auto& imva : vConfig) {
44 mvaEstimators.emplace_back(
49 <<
" failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
56 std::vector<std::string>
names;
57 for (
auto& imva : vConfig) {
85 template <
typename ValueType,
class HandleType>
88 const std::vector<ValueType>&
values,
90 auto valMap = std::make_unique<edm::ValueMap<ValueType>>();
92 filler.insert(handle, values.begin(), values.end());
97 template <
class ParticleType>
99 const bool tagGiven = !keysForValueMapsTag.
label().empty();
106 template <
class ParticleType>
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()) {
117 produces<edm::ValueMap<float>>(
name);
119 produces<edm::ValueMap<float>>(
name);
121 produces<edm::ValueMap<int>>(
name);
124 template <
class ParticleType>
128 std::vector<float> auxVariables = variableHelper_.getAuxVariables(iEvent);
130 auto srcHandle = iEvent.
getHandle(srcToken_);
131 auto keysForValueMapsHandle =
132 keysForValueMapsToken_.isUninitialized() ? srcHandle : iEvent.
getHandle(keysForValueMapsToken_);
135 if (!validated_ && !srcHandle->empty()) {
141 for (
unsigned iEstimator = 0; iEstimator < mvaEstimators_.size(); iEstimator++) {
142 std::vector<float> mvaValues;
143 std::vector<float> mvaRawValues;
144 std::vector<int> mvaCategories;
147 for (
auto const& cand : *srcHandle) {
149 const float response = mvaEstimators_[iEstimator]->mvaValue(&cand, auxVariables, cat);
150 mvaRawValues.push_back(response);
151 mvaValues.push_back(2.0 / (1.0 +
exp(-2.0 * response)) - 1);
152 mvaCategories.push_back(cat);
155 writeValueMap(iEvent, keysForValueMapsHandle, mvaValues, mvaValueMapNames_[iEstimator]);
156 writeValueMap(iEvent, keysForValueMapsHandle, mvaRawValues, mvaRawValueMapNames_[iEstimator]);
157 writeValueMap(iEvent, keysForValueMapsHandle, mvaCategories, mvaCategoriesMapNames_[iEstimator]);
162 template <
class ParticleType>
172 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.