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 
17 #include <memory>
18 #include <vector>
19 #include <cmath>
20 
21 template <class ParticleType>
23 
24  public:
25 
27  ~MVAValueMapProducer() override;
28 
29  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
30 
31  private:
32 
33  void produce(edm::Event&, const edm::EventSetup&) override;
34 
35  template<typename T>
38  const std::vector<T> & values,
39  const std::string & label) const ;
40 
41  // for AOD case
43 
44  // for miniAOD case
46 
47  // MVA estimator
48  std::vector<std::unique_ptr<AnyMVAEstimatorRun2Base>> mvaEstimators_;
49 
50  // Value map names
51  std::vector <std::string> mvaValueMapNames_;
52  std::vector <std::string> mvaRawValueMapNames_;
53  std::vector <std::string> mvaCategoriesMapNames_;
54 
55 };
56 
57 template <class ParticleType>
59 {
60 
61  //
62  // Declare consummables, handle both AOD and miniAOD case
63  //
64  src_ = mayConsume<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("src"));
65  srcMiniAOD_ = mayConsume<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"));
66 
67  // Loop over the list of MVA configurations passed here from python and
68  // construct all requested MVA estimators.
69  const std::vector<edm::ParameterSet>& mvaEstimatorConfigs
70  = iConfig.getParameterSetVector("mvaConfigurations");
71 
72  for( auto &imva : mvaEstimatorConfigs ){
73 
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 
79  imva.getParameter<std::string>("mvaName"), imva));
80 
81  } else
82  throw cms::Exception(" MVA configuration not found: ")
83  << " failed to find proper configuration for one of the MVAs in the main python script " << std::endl;
84 
85  mvaEstimators_.back()->setConsumes( consumesCollector() );
86 
87  //
88  // Compose and save the names of the value maps to be produced
89  //
90 
91  const std::string fullName = ( mvaEstimators_.back()->getName() +
92  mvaEstimators_.back()->getTag() );
93 
94  const std::string thisValueMapName = fullName + "Values";
95  const std::string thisRawValueMapName = fullName + "RawValues";
96  const std::string thisCategoriesMapName = fullName + "Categories";
97 
98  mvaValueMapNames_ .push_back( thisValueMapName );
99  mvaRawValueMapNames_ .push_back( thisRawValueMapName );
100  mvaCategoriesMapNames_.push_back( thisCategoriesMapName );
101 
102  // Declare the maps to the framework
103  produces<edm::ValueMap<float>>(thisValueMapName );
104  produces<edm::ValueMap<float>>(thisRawValueMapName );
105  produces<edm::ValueMap<int>> (thisCategoriesMapName);
106 
107  }
108 
109 }
110 
111 template <class ParticleType>
113 }
114 
115 template <class ParticleType>
117 
119 
120  // Retrieve the collection of particles from the event.
121  // If we fail to retrieve the collection with the standard AOD
122  // name, we next look for the one with the stndard miniAOD name.
123  iEvent.getByToken(src_, src);
124  if( !src.isValid() ){
125  iEvent.getByToken(srcMiniAOD_,src);
126  if( !src.isValid() )
127  throw cms::Exception(" Collection not found: ")
128  << " failed to find a standard AOD or miniAOD particle collection " << std::endl;
129  }
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 (size_t i = 0; i < src->size(); ++i){
141  auto iCand = src->ptrAt(i);
142  const float response = mvaEstimators_[iEstimator]->mvaValue( iCand, iEvent );
143  mvaRawValues.push_back( response ); // The MVA score
144  mvaValues.push_back( 2.0/(1.0+exp(-2.0*response))-1 ); // MVA output between -1 and 1
145  mvaCategories.push_back( mvaEstimators_[iEstimator]->findCategory( iCand ) );
146  } // end loop over particles
147 
148  writeValueMap(iEvent, src, mvaValues , mvaValueMapNames_ [iEstimator] );
149  writeValueMap(iEvent, src, mvaRawValues , mvaRawValueMapNames_ [iEstimator] );
150  writeValueMap(iEvent, src, mvaCategories, mvaCategoriesMapNames_[iEstimator] );
151 
152  } // end loop over estimators
153 
154 }
155 
156 template<class ParticleType> template<typename T>
159  const std::vector<T> & values,
160  const std::string & label) const
161 {
162  auto valMap = std::make_unique<edm::ValueMap<T>>();
163  typename edm::ValueMap<T>::Filler filler(*valMap);
164  filler.insert(handle, values.begin(), values.end());
165  filler.fill();
166  iEvent.put(std::move(valMap), label);
167 }
168 
169 template <class ParticleType>
171  //The following says we do not know what parameters are allowed so do no validation
172  // Please change this to state exactly what you do use, even if it is no parameters
174  desc.setUnknown();
175  descriptions.addDefault(desc);
176 }
177 
178 #endif
T getParameter(std::string const &) const
VParameterSet const & getParameterSetVector(std::string const &name) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
def create(alignables, pedeDump, additionalData, outputFile, config)
std::vector< std::string > mvaValueMapNames_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
std::vector< std::string > mvaRawValueMapNames_
std::vector< std::unique_ptr< AnyMVAEstimatorRun2Base > > mvaEstimators_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
void produce(edm::Event &, const edm::EventSetup &) override
void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< ParticleType > > &handle, const std::vector< T > &values, const std::string &label) const
bool isValid() const
Definition: HandleBase.h:74
std::vector< std::string > mvaCategoriesMapNames_
MVAValueMapProducer(const edm::ParameterSet &)
edm::EDGetToken srcMiniAOD_
def move(src, dest)
Definition: eostools.py:510
T get(const Candidate &c)
Definition: component.h:55