CMS 3D CMS Logo

ElectronRegressionValueMapProducer.cc
Go to the documentation of this file.
3 
6 
8 
11 
14 
18 
19 #include "TVector2.h"
20 
21 #include <memory>
22 #include <vector>
23 #include <unordered_map>
24 
25 namespace {
26  enum reg_float_vars { k_NFloatVars = 0 };
27 
28  enum reg_int_vars { k_NIntVars = 0 };
29 
30  const std::vector<std::string> float_var_names( { } );
31 
32  const std::vector<std::string> integer_var_names( { } );
33 
34  inline void set_map_val( const reg_float_vars index, const float value,
35  std::unordered_map<std::string,float>& map) {
36  map[float_var_names[index]] = value;
37  }
38  inline void set_map_val( const reg_int_vars index, const int value,
39  std::unordered_map<std::string,int>& map) {
40  map[integer_var_names[index]] = value;
41  }
42 
43  template<typename T>
44  inline void check_map(const std::unordered_map<std::string,T>& map, unsigned exp_size) {
45  if( map.size() != exp_size ) {
46  throw cms::Exception("ElectronRegressionWeirdConfig")
47  << "variable map size: " << map.size()
48  << " not equal to expected size: " << exp_size << " !"
49  << " The regression variable calculation code definitely has a bug, fix it!";
50  }
51  }
52 
53  template<typename LazyTools>
54  void calculateValues(EcalClusterLazyToolsBase* tools_tocast,
55  const edm::Ptr<reco::GsfElectron>& iEle,
56  const edm::EventSetup& iSetup,
57  std::unordered_map<std::string,float>& float_vars,
58  std::unordered_map<std::string,int>& int_vars ) {
59  }
60 }
61 
63 
64  public:
65 
68 
69  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
70 
71  private:
72 
73  void produce(edm::Event&, const edm::EventSetup&) override;
74 
75  template<typename T>
78  const std::vector<T> & values,
79  const std::string & label) const ;
80 
81  std::unique_ptr<EcalClusterLazyToolsBase> lazyTools;
82 
83  // for AOD case
88 
89  // for miniAOD case
94 
95  const bool use_full5x5_;
96 };
97 
99  use_full5x5_(iConfig.getParameter<bool>("useFull5x5")) {
100 
101  //
102  // Declare consummables, handle both AOD and miniAOD case
103  //
104  ebReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
105  ("ebReducedRecHitCollection"));
106  ebReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
107  ("ebReducedRecHitCollectionMiniAOD"));
108 
109  eeReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
110  ("eeReducedRecHitCollection"));
111  eeReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
112  ("eeReducedRecHitCollectionMiniAOD"));
113 
114  esReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
115  ("esReducedRecHitCollection"));
116  esReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
117  ("esReducedRecHitCollectionMiniAOD"));
118 
119  src_ = mayConsume<edm::View<reco::GsfElectron> >(iConfig.getParameter<edm::InputTag>("src"));
120  srcMiniAOD_ = mayConsume<edm::View<reco::GsfElectron> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"));
121 
122  for( const std::string& name : float_var_names ) {
123  produces<edm::ValueMap<float> >(name);
124  }
125 
126  for( const std::string& name : integer_var_names ) {
127  produces<edm::ValueMap<int> >(name);
128  }
129 }
130 
132 }
133 
135 
136  using namespace edm;
137 
139 
140  // Retrieve the collection of electrons from the event.
141  // If we fail to retrieve the collection with the standard AOD
142  // name, we next look for the one with the stndard miniAOD name.
143  bool isAOD = true;
144  iEvent.getByToken(src_, src);
145 
146  if( !src.isValid() ) {
147  isAOD = false;
148  iEvent.getByToken(srcMiniAOD_,src);
149  }
150 
151  // configure lazy tools
153 
154  if( isAOD ) {
158  } else {
162  }
163 
164  if( use_full5x5_ ) {
165  lazyTools = std::make_unique<noZS::EcalClusterLazyTools>(iEvent, iSetup,
166  ebrh, eerh, esrh );
167  } else {
168  lazyTools = std::make_unique<EcalClusterLazyTools>(iEvent, iSetup,
169  ebrh, eerh, esrh );
170  }
171 
172  std::vector<std::vector<float> > float_vars(k_NFloatVars);
173  std::vector<std::vector<int> > int_vars(k_NIntVars);
174 
175  std::unordered_map<std::string,float> float_vars_map;
176  std::unordered_map<std::string,int> int_vars_map;
177 
178  // reco::GsfElectron::superCluster() is virtual so we can exploit polymorphism
179  for (size_t i = 0; i < src->size(); ++i){
180  auto iEle = src->ptrAt(i);
181 
182  if( use_full5x5_ ) {
183  calculateValues<noZS::EcalClusterLazyTools>(lazyTools.get(),
184  iEle,
185  iSetup,
186  float_vars_map,
187  int_vars_map);
188  } else {
189  calculateValues<EcalClusterLazyTools>(lazyTools.get(),
190  iEle,
191  iSetup,
192  float_vars_map,
193  int_vars_map);
194  }
195 
196  check_map(float_vars_map, k_NFloatVars);
197  check_map(int_vars_map, k_NIntVars);
198 
199  for( unsigned i = 0; i < float_vars.size(); ++i ) {
200  float_vars[i].emplace_back(float_vars_map.at(float_var_names[i]));
201  }
202 
203  for( unsigned i = 0; i < int_vars.size(); ++i ) {
204  int_vars[i].emplace_back(int_vars_map.at(integer_var_names[i]));
205  }
206  }
207 
208  for( unsigned i = 0; i < float_vars.size(); ++i ) {
209  writeValueMap(iEvent, src, float_vars[i], float_var_names[i]);
210  }
211 
212  for( unsigned i = 0; i < int_vars.size(); ++i ) {
213  writeValueMap(iEvent, src, int_vars[i], integer_var_names[i]);
214  }
215 
216  lazyTools.reset();
217 }
218 
219 template<typename T>
222  const std::vector<T> & values,
223  const std::string & label) const
224 {
225  using namespace edm;
226  using namespace std;
227  typedef ValueMap<T> TValueMap;
228 
229  auto valMap = std::make_unique<TValueMap>();
230  typename TValueMap::Filler filler(*valMap);
231  filler.insert(handle, values.begin(), values.end());
232  filler.fill();
233  iEvent.put(std::move(valMap), label);
234 }
235 
237  //The following says we do not know what parameters are allowed so do no validation
238  // Please change this to state exactly what you do use, even if it is no parameters
240  desc.setUnknown();
241  descriptions.addDefault(desc);
242 }
243 
T getParameter(std::string const &) const
edm::EDGetTokenT< EcalRecHitCollection > eeReducedRecHitCollection_
void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< reco::GsfElectron > > &handle, const std::vector< T > &values, const std::string &label) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< EcalRecHitCollection > esReducedRecHitCollection_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< EcalRecHitCollection > esReducedRecHitCollectionMiniAOD_
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< EcalRecHitCollection > ebReducedRecHitCollection_
edm::EDGetTokenT< EcalRecHitCollection > eeReducedRecHitCollectionMiniAOD_
Definition: value.py:1
std::unique_ptr< EcalClusterLazyToolsBase > lazyTools
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< EcalRecHitCollection > ebReducedRecHitCollectionMiniAOD_
void produce(edm::Event &, const edm::EventSetup &) override
HLT enums.
def move(src, dest)
Definition: eostools.py:510