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 
18 
19 #include <atomic>
20 #include <cmath>
21 #include <memory>
22 #include <vector>
23 #include <cmath>
24 
25 template <class ParticleType>
27 
28  public:
29 
31  ~MVAValueMapProducer() override {}
32 
33  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
34 
35  private:
36 
37  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
38 
39  // for AOD and MiniAOD case
42 
43  // MVA estimators
44  const std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators_;
45 
46  // Value map names
47  const std::vector<std::string> mvaValueMapNames_;
48  const std::vector<std::string> mvaRawValueMapNames_;
49  const std::vector<std::string> mvaCategoriesMapNames_;
50 
51  // To get the auxiliary MVA variables
53 
54  CMS_THREAD_SAFE mutable std::atomic<bool> validated_ = false;
55 };
56 
57 namespace {
58 
59  auto getMVAEstimators(const edm::VParameterSet& vConfig)
60  {
61  std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators;
62 
63  // Loop over the list of MVA configurations passed here from python and
64  // construct all requested MVA estimators.
65  for( auto &imva : vConfig )
66  {
67 
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 
72  mvaEstimators.emplace_back(AnyMVAEstimatorRun2Factory::get()->create(
73  imva.getParameter<std::string>("mvaName"), imva));
74 
75  } else
76  throw cms::Exception(" MVA configuration not found: ")
77  << " failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
78  }
79 
80  return mvaEstimators;
81  }
82 
83  std::vector<std::string> getValueMapNames(const edm::VParameterSet& vConfig, std::string && suffix)
84  {
85  std::vector<std::string> names;
86  for( auto &imva : vConfig )
87  {
88  names.push_back(imva.getParameter<std::string>("mvaName") + imva.getParameter<std::string>("mvaTag") + suffix);
89  }
90 
91  return names;
92  }
93 
94  template <class ParticleType>
95  auto getKeysForValueMapsToken(edm::InputTag const& keysForValueMapsTag, edm::ConsumesCollector&& cc) {
96  const bool tagGiven = !keysForValueMapsTag.label().empty();
97  return tagGiven ? cc.consumes<edm::View<ParticleType>>(keysForValueMapsTag)
99  }
100 }
101 
102 template <class ParticleType>
104  : src_(consumesCollector(), iConfig, "src", "srcMiniAOD")
105  , keysForValueMapsToken_(getKeysForValueMapsToken<ParticleType>(
106  iConfig.getParameter<edm::InputTag>("keysForValueMaps"), consumesCollector()))
107  , mvaEstimators_(getMVAEstimators(iConfig.getParameterSetVector("mvaConfigurations")))
108  , mvaValueMapNames_ (getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "Values" ))
109  , mvaRawValueMapNames_ (getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "RawValues" ))
110  , mvaCategoriesMapNames_(getValueMapNames(iConfig.getParameterSetVector("mvaConfigurations"), "Categories"))
112 {
113  for( auto const& name : mvaValueMapNames_ ) produces<edm::ValueMap<float>>(name);
114  for( auto const& name : mvaRawValueMapNames_ ) produces<edm::ValueMap<float>>(name);
115  for( auto const& name : mvaCategoriesMapNames_ ) produces<edm::ValueMap<int >>(name);
116 }
117 
118 template <class ParticleType>
120 {
121 
123  auto keysForValueMapsHandle =
125 
126  // check if nothing is wrong with the data format of the candidates
127  if (!validated_ && !srcHandle->empty()) {
128  egammaTools::validateEgammaCandidate((*srcHandle)[0]);
129  validated_ = true;
130  }
131 
132  // Loop over MVA estimators
133  for( unsigned iEstimator = 0; iEstimator < mvaEstimators_.size(); iEstimator++ ){
134 
135  std::vector<float> mvaValues;
136  std::vector<float> mvaRawValues;
137  std::vector<int> mvaCategories;
138 
139  // Loop over particles
140  for (auto const& cand : srcHandle->ptrs())
141  {
142  std::vector<float> auxVariables = variableHelper_.getAuxVariables(cand, iEvent);
143 
144  int cat = -1; // Passed by reference to the mvaValue function to store the category
145  const float response = mvaEstimators_[iEstimator]->mvaValue( cand.get(), auxVariables, cat );
146  mvaRawValues.push_back( response ); // The MVA score
147  mvaValues.push_back( 2.0/(1.0+exp(-2.0*response))-1 ); // MVA output between -1 and 1
148  mvaCategories.push_back( cat );
149  } // end loop over particles
150 
151  writeValueMap(iEvent, keysForValueMapsHandle, mvaValues, mvaValueMapNames_[iEstimator]);
152  writeValueMap(iEvent, keysForValueMapsHandle, mvaRawValues, mvaRawValueMapNames_[iEstimator]);
153  writeValueMap(iEvent, keysForValueMapsHandle, mvaCategories, mvaCategoriesMapNames_[iEstimator]);
154 
155  } // end loop over estimators
156 
157 }
158 
159 template <class ParticleType>
162  desc.add<edm::InputTag>("src", {});
163  desc.add<edm::InputTag>("srcMiniAOD", {});
164  desc.add<edm::InputTag>("keysForValueMaps", {});
165  {
166  //The following says we do not know what parameters are allowed so do no validation
167  // Please change this to state exactly what you do use, even if it is no parameters
169  mvaConfigurations.setUnknown();
170  desc.addVPSet("mvaConfigurations", mvaConfigurations);
171  }
172  descriptions.addDefault(desc);
173 }
174 
175 #endif
const edm::EDGetTokenT< edm::View< ParticleType > > keysForValueMapsToken_
const std::vector< std::string > mvaRawValueMapNames_
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
def create(alignables, pedeDump, additionalData, outputFile, config)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
void validateEgammaCandidate(Candidate const &candidate)
const std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
const std::string names[nVars_]
void writeValueMap(edm::Event &iEvent, const edm::Handle< HandleType > &handle, const std::vector< ValueType > &values, const std::string &label)
Definition: Utils.h:13
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:539
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:224
def cat(path)
Definition: eostools.py:401
void addDefault(ParameterSetDescription const &psetDescription)
const MultiTokenT< edm::View< ParticleType > > src_
const std::vector< std::string > mvaCategoriesMapNames_
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
const std::vector< std::string > mvaValueMapNames_
#define CMS_THREAD_SAFE
edm::Handle< T > getValidHandle(const edm::Event &iEvent) const
Definition: MultiToken.h:95
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::atomic< bool > validated_
MVAValueMapProducer(const edm::ParameterSet &)
const MVAVariableHelper< ParticleType > variableHelper_
std::string const & label() const
Definition: InputTag.h:36
HLT enums.
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
bool isUninitialized() const
Definition: EDGetToken.h:70
T get(const Candidate &c)
Definition: component.h:55
ParticleType
Definition of particle types.
Definition: ParticleCode.h:18