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 
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>
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
171  mvaConfigurations.setUnknown();
172  desc.addVPSet("mvaConfigurations", mvaConfigurations);
173  }
174  descriptions.addDefault(desc);
175 }
176 
177 #endif
AnyMVAEstimatorRun2Base.h
edm::StreamID
Definition: StreamID.h:30
Handle.h
MVAValueMapProducer::MVAValueMapProducer
MVAValueMapProducer(const edm::ParameterSet &)
Definition: MVAValueMapProducer.h:107
MVAValueMapProducer::getValueMapNames
static std::vector< std::string > getValueMapNames(const edm::VParameterSet &vConfig, std::string &&suffix)
Definition: MVAValueMapProducer.h:55
MVAValueMapProducer::srcToken_
const edm::EDGetTokenT< edm::View< ParticleType > > srcToken_
Definition: MVAValueMapProducer.h:66
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
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89301
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
edm::VParameterSet
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:34
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
MVAValueMapProducer::validated_
std::atomic< bool > validated_
Definition: MVAValueMapProducer.h:80
edm::Handle
Definition: AssociativeIterator.h:50
MVAVariableHelper.h
createPayload.suffix
suffix
Definition: createPayload.py:281
edm::InputTag::label
std::string const & label() const
Definition: InputTag.h:36
MakerMacros.h
MVAValueMapProducer
Definition: MVAValueMapProducer.h:28
names
const std::string names[nVars_]
Definition: PhotonIDValueMapProducer.cc:124
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
ElectronMVAValueMapProducer_cfi.mvaConfigurations
mvaConfigurations
Definition: ElectronMVAValueMapProducer_cfi.py:46
MVAValueMapProducer::mvaValueMapNames_
const std::vector< std::string > mvaValueMapNames_
Definition: MVAValueMapProducer.h:73
MVAValueMapProducer::getMVAEstimators
static auto getMVAEstimators(const edm::VParameterSet &vConfig)
Definition: MVAValueMapProducer.h:35
egammaTools::validateEgammaCandidate
void validateEgammaCandidate(Candidate const &candidate)
Definition: validateEgammaCandidate.h:11
CMS_THREAD_SAFE
#define CMS_THREAD_SAFE
Definition: thread_safety_macros.h:4
EDGetToken.h
MVAValueMapProducer::mvaCategoriesMapNames_
const std::vector< std::string > mvaCategoriesMapNames_
Definition: MVAValueMapProducer.h:75
edm::global::EDProducer
Definition: EDProducer.h:32
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
edm::View
Definition: CaloClusterFwd.h:14
MVAValueMapProducer::mvaRawValueMapNames_
const std::vector< std::string > mvaRawValueMapNames_
Definition: MVAValueMapProducer.h:74
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
thread_safety_macros.h
cand
Definition: decayParser.h:32
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::EventSetup
Definition: EventSetup.h:58
pat::ParticleType
ParticleType
Definition of particle types.
Definition: ParticleCode.h:17
MVAValueMapProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: MVAValueMapProducer.h:163
get
#define get
cc
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ValueMap.h
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
MVAValueMapProducer::mvaEstimators_
const std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
Definition: MVAValueMapProducer.h:70
Frameworkfwd.h
Exception
Definition: hltDiff.cc:245
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
MVAValueMapProducer::keysForValueMapsToken_
const edm::EDGetTokenT< edm::View< ParticleType > > keysForValueMapsToken_
Definition: MVAValueMapProducer.h:67
ConsumesCollector.h
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:78
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
MVAVariableHelper
Definition: MVAVariableHelper.h:12
edm::InputTag
Definition: InputTag.h:15
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
label
const char * label
Definition: PFTauDecayModeTools.cc:11
validateEgammaCandidate.h
MVAValueMapProducer::produce
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: MVAValueMapProducer.h:125