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  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
36 
39 
40  // MVA estimators
41  const std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators_;
42 
43  // Value map names
44  const std::vector<std::string> mvaValueMapNames_;
45  const std::vector<std::string> mvaRawValueMapNames_;
46  const std::vector<std::string> mvaCategoriesMapNames_;
47 
48  // To get the auxiliary MVA variables
50 
51  CMS_THREAD_SAFE mutable std::atomic<bool> validated_ = false;
52 };
53 
54 namespace {
55 
56  template <typename ValueType, class HandleType>
57  void writeValueMap(edm::Event& iEvent,
59  const std::vector<ValueType>& values,
60  const std::string& label) {
61  auto valMap = std::make_unique<edm::ValueMap<ValueType>>();
62  typename edm::ValueMap<ValueType>::Filler filler(*valMap);
63  filler.insert(handle, values.begin(), values.end());
64  filler.fill();
65  iEvent.put(std::move(valMap), label);
66  }
67 
68  auto getMVAEstimators(const edm::VParameterSet& vConfig) {
69  std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators;
70 
71  // Loop over the list of MVA configurations passed here from python and
72  // construct all requested MVA estimators.
73  for (auto& imva : vConfig) {
74  // The factory below constructs the MVA of the appropriate type based
75  // on the "mvaName" which is the name of the derived MVA class (plugin)
76  if (!imva.empty()) {
77  mvaEstimators.emplace_back(
78  AnyMVAEstimatorRun2Factory::get()->create(imva.getParameter<std::string>("mvaName"), imva));
79 
80  } else
81  throw cms::Exception(" MVA configuration not found: ")
82  << " failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
83  }
84 
85  return mvaEstimators;
86  }
87 
88  std::vector<std::string> getValueMapNames(const edm::VParameterSet& vConfig, std::string&& suffix) {
89  std::vector<std::string> names;
90  for (auto& imva : vConfig) {
91  names.push_back(imva.getParameter<std::string>("mvaName") + imva.getParameter<std::string>("mvaTag") + suffix);
92  }
93 
94  return names;
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::srcToken_
const edm::EDGetTokenT< edm::View< ParticleType > > srcToken_
Definition: MVAValueMapProducer.h:37
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:89287
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:51
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:32
MVAValueMapProducer::mvaValueMapNames_
const std::vector< std::string > mvaValueMapNames_
Definition: MVAValueMapProducer.h:44
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:46
edm::global::EDProducer
Definition: EDProducer.h:32
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::View
Definition: CaloClusterFwd.h:14
MVAValueMapProducer::mvaRawValueMapNames_
const std::vector< std::string > mvaRawValueMapNames_
Definition: MVAValueMapProducer.h:45
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:57
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
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:41
Frameworkfwd.h
Exception
Definition: hltDiff.cc:246
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
MVAValueMapProducer::keysForValueMapsToken_
const edm::EDGetTokenT< edm::View< ParticleType > > keysForValueMapsToken_
Definition: MVAValueMapProducer.h:38
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:49
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