test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
28 
29  static std::unique_ptr<egamma::MVAObjectCache>
31  return std::unique_ptr<egamma::MVAObjectCache>(new 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  virtual 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> mvaCategoriesMapNames_;
60 
61 };
62 
63 template <class ParticleType>
65  const egamma::MVAObjectCache* mva_cache)
66 {
67 
68  //
69  // Declare consummables, handle both AOD and miniAOD case
70  //
71  src_ = mayConsume<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("src"));
72  srcMiniAOD_ = mayConsume<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"));
73 
74  // Loop over the list of MVA configurations passed here from python and
75  // construct all requested MVA esimtators.
76  const auto& all_mvas = mva_cache->allMVAs();
77  for( auto mvaItr = all_mvas.begin(); mvaItr != all_mvas.end(); ++mvaItr ) {
78  // set the consumes
79  mvaItr->second->setConsumes(consumesCollector());
80  //
81  // Compose and save the names of the value maps to be produced
82  //
83  const auto& currentEstimator = mvaItr->second;
84  const std::string full_name = ( currentEstimator->getName() +
85  currentEstimator->getTag() );
86  std::string thisValueMapName = full_name + "Values";
87  std::string thisCategoriesMapName = full_name + "Categories";
88  mvaValueMapNames_.push_back( thisValueMapName );
89  mvaCategoriesMapNames_.push_back( thisCategoriesMapName );
90 
91  // Declare the maps to the framework
92  produces<edm::ValueMap<float> >(thisValueMapName);
93  produces<edm::ValueMap<int> >(thisCategoriesMapName);
94  }
95 
96 
97 }
98 
99 template <class ParticleType>
101 }
102 
103 template <class ParticleType>
105 
106  using namespace edm;
107 
109 
110  // Retrieve the collection of particles from the event.
111  // If we fail to retrieve the collection with the standard AOD
112  // name, we next look for the one with the stndard miniAOD name.
113  iEvent.getByToken(src_, src);
114  if( !src.isValid() ){
115  iEvent.getByToken(srcMiniAOD_,src);
116  if( !src.isValid() )
117  throw cms::Exception(" Collection not found: ")
118  << " failed to find a standard AOD or miniAOD particle collection " << std::endl;
119  }
120 
121 
122  // Loop over MVA estimators
123  const auto& all_mvas = globalCache()->allMVAs();
124  for( auto mva_itr = all_mvas.begin(); mva_itr != all_mvas.end(); ++mva_itr ){
125  const int iEstimator = std::distance(all_mvas.begin(),mva_itr);
126 
127  // Set up all event content, such as ValueMaps produced upstream or other,
128  // original event data pieces, that is needed (if any is implemented in the specific
129  // MVA classes)
130  const auto& thisEstimator = mva_itr->second;
131 
132  std::vector<float> mvaValues;
133  std::vector<int> mvaCategories;
134 
135  // Loop over particles
136  for (size_t i = 0; i < src->size(); ++i){
137  auto iCand = src->ptrAt(i);
138  mvaValues.push_back( thisEstimator->mvaValue( iCand, iEvent ) );
139  mvaCategories.push_back( thisEstimator->findCategory( iCand ) );
140  } // end loop over particles
141 
142  writeValueMap(iEvent, src, mvaValues, mvaValueMapNames_[iEstimator] );
143  writeValueMap(iEvent, src, mvaCategories, mvaCategoriesMapNames_[iEstimator] );
144  } // end loop over estimators
145 
146 
147 }
148 
149 template<class ParticleType> template<typename T>
152  const std::vector<T> & values,
153  const std::string & label) const
154 {
155  using namespace edm;
156  using namespace std;
157  auto_ptr<ValueMap<T> > valMap(new ValueMap<T>());
158  typename edm::ValueMap<T>::Filler filler(*valMap);
159  filler.insert(handle, values.begin(), values.end());
160  filler.fill();
161  iEvent.put(valMap, label);
162 }
163 
164 template <class ParticleType>
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  desc.setUnknown();
170  descriptions.addDefault(desc);
171 }
172 
173 #endif
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
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:462
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
virtual void produce(edm::Event &, const edm::EventSetup &) override
tuple handle
Definition: patZpeak.py:22
void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< ParticleType > > &handle, const std::vector< T > &values, const std::string &label) const
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 *)
edm::EDGetToken srcMiniAOD_