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 
6 
9 
11 
14 
16 
19 
20 #include <memory>
21 #include <vector>
22 #include <cmath>
23 
24 template <class ParticleType>
26 
27  public:
28 
30  ~MVAValueMapProducer() override;
31 
32  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
33 
34  private:
35 
36  void produce(edm::Event&, const edm::EventSetup&) override;
37 
38  // for AOD and MiniAOD case
40 
41  // MVA estimator
42  std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators_;
43 
44  // Value map names
45  std::vector <std::string> mvaValueMapNames_;
46  std::vector <std::string> mvaRawValueMapNames_;
47  std::vector <std::string> mvaCategoriesMapNames_;
48 
49 };
50 
51 template <class ParticleType>
53  : src_(consumesCollector(), iConfig, "src", "srcMiniAOD")
54 
55 {
56  // Loop over the list of MVA configurations passed here from python and
57  // construct all requested MVA estimators.
58  const std::vector<edm::ParameterSet>& mvaEstimatorConfigs
59  = iConfig.getParameterSetVector("mvaConfigurations");
60 
61  for( auto &imva : mvaEstimatorConfigs ){
62 
63  // The factory below constructs the MVA of the appropriate type based
64  // on the "mvaName" which is the name of the derived MVA class (plugin)
65  if( !imva.empty() ) {
66 
68  imva.getParameter<std::string>("mvaName"), imva));
69 
70  } else
71  throw cms::Exception(" MVA configuration not found: ")
72  << " failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
73 
74  mvaEstimators_.back()->setConsumes( consumesCollector() );
75 
76  //
77  // Compose and save the names of the value maps to be produced
78  //
79 
80  const std::string fullName = ( mvaEstimators_.back()->getName() +
81  mvaEstimators_.back()->getTag() );
82 
83  const std::string thisValueMapName = fullName + "Values";
84  const std::string thisRawValueMapName = fullName + "RawValues";
85  const std::string thisCategoriesMapName = fullName + "Categories";
86 
87  mvaValueMapNames_ .push_back( thisValueMapName );
88  mvaRawValueMapNames_ .push_back( thisRawValueMapName );
89  mvaCategoriesMapNames_.push_back( thisCategoriesMapName );
90 
91  // Declare the maps to the framework
92  produces<edm::ValueMap<float>>(thisValueMapName );
93  produces<edm::ValueMap<float>>(thisRawValueMapName );
94  produces<edm::ValueMap<int>> (thisCategoriesMapName);
95 
96  }
97 
98 }
99 
100 template <class ParticleType>
102 }
103 
104 template <class ParticleType>
106 
108 
109  // Loop over MVA estimators
110  for( unsigned iEstimator = 0; iEstimator < mvaEstimators_.size(); iEstimator++ ){
111 
112  std::vector<float> mvaValues;
113  std::vector<float> mvaRawValues;
114  std::vector<int> mvaCategories;
115 
116  // Loop over particles
117  for (size_t i = 0; i < src->size(); ++i){
118  auto iCand = src->ptrAt(i);
119  int cat = -1; // Passed by reference to the mvaValue function to store the category
120  const float response = mvaEstimators_[iEstimator]->mvaValue( iCand, iEvent, cat );
121  mvaRawValues.push_back( response ); // The MVA score
122  mvaValues.push_back( 2.0/(1.0+exp(-2.0*response))-1 ); // MVA output between -1 and 1
123  mvaCategories.push_back( cat );
124  } // end loop over particles
125 
126  writeValueMap(iEvent, src, mvaValues , mvaValueMapNames_ [iEstimator] );
127  writeValueMap(iEvent, src, mvaRawValues , mvaRawValueMapNames_ [iEstimator] );
128  writeValueMap(iEvent, src, mvaCategories, mvaCategoriesMapNames_[iEstimator] );
129 
130  } // end loop over estimators
131 
132 }
133 
134 template <class ParticleType>
136  //The following says we do not know what parameters are allowed so do no validation
137  // Please change this to state exactly what you do use, even if it is no parameters
139  desc.setUnknown();
140  descriptions.addDefault(desc);
141 }
142 
143 #endif
VParameterSet const & getParameterSetVector(std::string const &name) const
def create(alignables, pedeDump, additionalData, outputFile, config)
std::vector< std::string > mvaValueMapNames_
std::vector< std::string > mvaRawValueMapNames_
void writeValueMap(edm::Event &iEvent, const edm::Handle< HandleType > &handle, const std::vector< ValueType > &values, const std::string &label)
Definition: Utils.h:13
std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:230
def cat(path)
Definition: eostools.py:401
void addDefault(ParameterSetDescription const &psetDescription)
void produce(edm::Event &, const edm::EventSetup &) override
edm::Handle< T > getValidHandle(const edm::Event &iEvent)
Definition: MultiToken.h:83
std::vector< std::string > mvaCategoriesMapNames_
MVAValueMapProducer(const edm::ParameterSet &)
MultiTokenT< edm::View< ParticleType > > src_
T get(const Candidate &c)
Definition: component.h:55