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 
17 
18 #include <memory>
19 #include <vector>
20 
21 template <class ParticleType>
22 class MVAValueMapProducer : public edm::stream::EDProducer< edm::GlobalCache<egamma::MVAObjectCache> > {
23 
24  public:
25 
27  ~MVAValueMapProducer() override;
28 
29  static std::unique_ptr<egamma::MVAObjectCache>
31  return std::make_unique<egamma::MVAObjectCache>(conf);
32  }
33 
34  static void globalEndJob(const egamma::MVAObjectCache * ) {
35  }
36 
37  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
38 
39  private:
40 
41  void produce(edm::Event&, const edm::EventSetup&) override;
42 
43  template<typename T>
46  const std::vector<T> & values,
47  const std::string & label) const ;
48 
49  // for AOD case
51 
52  // for miniAOD case
54 
55  // MVA estimators are now stored in MVAObjectCache!
56 
57  // Value map names
58  std::vector <std::string> mvaValueMapNames_;
59  std::vector <std::string> mvaRawValueMapNames_;
60  std::vector <std::string> mvaCategoriesMapNames_;
61 
62 };
63 
64 template <class ParticleType>
66  const egamma::MVAObjectCache* mva_cache)
67 {
68 
69  //
70  // Declare consummables, handle both AOD and miniAOD case
71  //
72  src_ = mayConsume<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("src"));
73  srcMiniAOD_ = mayConsume<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"));
74 
75  // Loop over the list of MVA configurations passed here from python and
76  // construct all requested MVA esimtators.
77  const auto& all_mvas = mva_cache->allMVAs();
78  for( auto mvaItr = all_mvas.begin(); mvaItr != all_mvas.end(); ++mvaItr ) {
79  // set the consumes
80  mvaItr->second->setConsumes(consumesCollector());
81  //
82  // Compose and save the names of the value maps to be produced
83  //
84  const auto& currentEstimator = mvaItr->second;
85  const std::string full_name = ( currentEstimator->getName() +
86  currentEstimator->getTag() );
87  std::string thisValueMapName = full_name + "Values";
88  std::string thisRawValueMapName = full_name + "RawValues";
89  std::string thisCategoriesMapName = full_name + "Categories";
90  mvaValueMapNames_.push_back( thisValueMapName );
91  mvaRawValueMapNames_.push_back( thisRawValueMapName );
92  mvaCategoriesMapNames_.push_back( thisCategoriesMapName );
93 
94  // Declare the maps to the framework
95  produces<edm::ValueMap<float> >(thisValueMapName);
96  produces<edm::ValueMap<float> >(thisRawValueMapName);
97  produces<edm::ValueMap<int> >(thisCategoriesMapName);
98  }
99 
100 
101 }
102 
103 template <class ParticleType>
105 }
106 
107 template <class ParticleType>
109 
110  using namespace edm;
111 
113 
114  // Retrieve the collection of particles from the event.
115  // If we fail to retrieve the collection with the standard AOD
116  // name, we next look for the one with the stndard miniAOD name.
117  iEvent.getByToken(src_, src);
118  if( !src.isValid() ){
119  iEvent.getByToken(srcMiniAOD_,src);
120  if( !src.isValid() )
121  throw cms::Exception(" Collection not found: ")
122  << " failed to find a standard AOD or miniAOD particle collection " << std::endl;
123  }
124 
125 
126  // Loop over MVA estimators
127  const auto& all_mvas = globalCache()->allMVAs();
128  for( auto mva_itr = all_mvas.begin(); mva_itr != all_mvas.end(); ++mva_itr ){
129  const int iEstimator = std::distance(all_mvas.begin(),mva_itr);
130 
131  // Set up all event content, such as ValueMaps produced upstream or other,
132  // original event data pieces, that is needed (if any is implemented in the specific
133  // MVA classes)
134  const auto& thisEstimator = mva_itr->second;
135 
136  std::vector<float> mvaValues;
137  std::vector<float> mvaRawValues;
138  std::vector<int> mvaCategories;
139 
140  // Loop over particles
141  for (size_t i = 0; i < src->size(); ++i){
142  auto iCand = src->ptrAt(i);
143  const float response = thisEstimator->mvaValue( iCand, iEvent );
144  mvaRawValues.push_back( response ); // The MVA score
145  mvaValues.push_back( 2.0/(1.0+exp(-2.0*response))-1 ); // MVA output between -1 and 1
146  mvaCategories.push_back( thisEstimator->findCategory( iCand ) );
147  } // end loop over particles
148 
149  writeValueMap(iEvent, src, mvaValues, mvaValueMapNames_[iEstimator] );
150  writeValueMap(iEvent, src, mvaRawValues, mvaRawValueMapNames_[iEstimator] );
151  writeValueMap(iEvent, src, mvaCategories, mvaCategoriesMapNames_[iEstimator] );
152  } // end loop over estimators
153 
154 
155 }
156 
157 template<class ParticleType> template<typename T>
160  const std::vector<T> & values,
161  const std::string & label) const
162 {
163  using namespace edm;
164  using namespace std;
165  auto valMap = std::make_unique<ValueMap<T>>();
166  typename edm::ValueMap<T>::Filler filler(*valMap);
167  filler.insert(handle, values.begin(), values.end());
168  filler.fill();
169  iEvent.put(std::move(valMap), label);
170 }
171 
172 template <class ParticleType>
174  //The following says we do not know what parameters are allowed so do no validation
175  // Please change this to state exactly what you do use, even if it is no parameters
177  desc.setUnknown();
178  descriptions.addDefault(desc);
179 }
180 
181 #endif
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
const std::unordered_map< std::string, MVAPtr > & allMVAs() const
std::vector< std::string > mvaValueMapNames_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
std::vector< std::string > mvaRawValueMapNames_
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
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
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
static std::unique_ptr< egamma::MVAObjectCache > initializeGlobalCache(const edm::ParameterSet &conf)
std::vector< std::string > mvaCategoriesMapNames_
MVAValueMapProducer(const edm::ParameterSet &, const egamma::MVAObjectCache *)
static void globalEndJob(const egamma::MVAObjectCache *)
HLT enums.
edm::EDGetToken srcMiniAOD_
def move(src, dest)
Definition: eostools.py:510