CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PhotonRegressionValueMapProducer.cc
Go to the documentation of this file.
3 
6 
8 
11 
14 
16 
18 
19 #include <memory>
20 #include <vector>
21 #include <unordered_map>
22 
23 namespace {
24  // Cluster shapes
25  enum reg_float_vars { k_sigmaIPhiIPhi = 0,
26  k_sigmaIEtaIPhi,
27  k_e2x5Max,
28  k_e2x5Left,
29  k_e2x5Right,
30  k_e2x5Top,
31  k_e2x5Bottom,
32  k_NFloatVars };
33 
34  enum reg_int_vars { k_NIntVars = 0 };
35 
36  static const std::vector<std::string> float_var_names( { "sigmaIPhiIPhi",
37  "sigmaIEtaIPhi",
38  "e2x5Max",
39  "e2x5Left",
40  "e2x5Right",
41  "e2x5Top",
42  "e2x5Bottom" } );
43 
44  static const std::vector<std::string> integer_var_names( { } );
45 
46  inline void set_map_val( const reg_float_vars index, const float value,
47  std::unordered_map<std::string,float>& map) {
48  map[float_var_names[index]] = value;
49  }
50  inline void set_map_val( const reg_int_vars index, const int value,
51  std::unordered_map<std::string,int>& map) {
52  map[integer_var_names[index]] = value;
53  }
54 
55  template<typename T>
56  inline void check_map(const std::unordered_map<std::string,T>& map, unsigned exp_size) {
57  if( map.size() != exp_size ) {
58  throw cms::Exception("PhotonRegressionWeirdConfig")
59  << "variable map size: " << map.size()
60  << " not equal to expected size: " << exp_size << " !"
61  << " The regression variable calculation code definitely has a bug, fix it!";
62  }
63  }
64 
65  template<typename LazyTools,typename SeedType>
66  inline void calculateValues(EcalClusterLazyToolsBase* tools_tocast,
67  const SeedType& the_seed,
68  std::unordered_map<std::string,float>& float_vars,
69  std::unordered_map<std::string,int>& /*int_vars*/ ) {
70  LazyTools* tools = static_cast<LazyTools*>(tools_tocast);
71 
72  float spp = -999;
73  std::vector<float> vCov = tools->localCovariances( the_seed );
74  spp = (isnan(vCov[2]) ? 0. : sqrt(vCov[2]));
75  float sep = vCov[1];
76 
77  set_map_val(k_sigmaIPhiIPhi, spp, float_vars);
78  set_map_val(k_sigmaIEtaIPhi, sep, float_vars);
79 
80  set_map_val(k_e2x5Max, tools->e2x5Max(the_seed), float_vars);
81  set_map_val(k_e2x5Left, tools->e2x5Left(the_seed), float_vars);
82  set_map_val(k_e2x5Right, tools->e2x5Right(the_seed), float_vars);
83  set_map_val(k_e2x5Top, tools->e2x5Top(the_seed), float_vars);
84  set_map_val(k_e2x5Bottom, tools->e2x5Bottom(the_seed), float_vars);
85  }
86 }
87 
89 
90  public:
91 
94 
95  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
96 
97  private:
98 
99  virtual void produce(edm::Event&, const edm::EventSetup&) override;
100 
101  template<typename T>
104  const std::vector<T> & values,
105  const std::string & label) const ;
106 
107  // The object that will compute 5x5 quantities
108  std::unique_ptr<EcalClusterLazyToolsBase> lazyTools;
109 
110  // for AOD case
115 
116  // for miniAOD case
121 
122  const bool use_full5x5_;
123 };
124 
126  use_full5x5_(iConfig.getParameter<bool>("useFull5x5")) {
127 
128  //
129  // Declare consummables, handle both AOD and miniAOD case
130  //
131  ebReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
132  ("ebReducedRecHitCollection"));
133  ebReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
134  ("ebReducedRecHitCollectionMiniAOD"));
135 
136  eeReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
137  ("eeReducedRecHitCollection"));
138  eeReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
139  ("eeReducedRecHitCollectionMiniAOD"));
140 
141  esReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
142  ("esReducedRecHitCollection"));
143  esReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
144  ("esReducedRecHitCollectionMiniAOD"));
145 
146  // reco photons are castable into pat photons, so no need to handle reco/pat seprately
147  src_ = mayConsume<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("src"));
148  srcMiniAOD_ = mayConsume<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"));
149 
150  //
151  // Declare producibles
152  //
153  // Cluster shapes
154  for( const std::string& name : float_var_names ) {
155  produces<edm::ValueMap<float> >(name);
156  }
157 
158  for( const std::string& name : integer_var_names ) {
159  produces<edm::ValueMap<int> >(name);
160  }
161 }
162 
164 {}
165 
167 
168  using namespace edm;
169 
171 
172  bool isAOD = true;
173  iEvent.getByToken(src_, src);
174  if(!src.isValid() ){
175  isAOD = false;
176  iEvent.getByToken(srcMiniAOD_, src);
177  }
178 
179  if( !src.isValid() ) {
180  throw cms::Exception("IllDefinedDataTier")
181  << "DataFormat does not contain a photon source!";
182  }
183 
184  // configure lazy tools
186 
187  if( isAOD ) {
191  } else {
195  }
196 
197  if( use_full5x5_ ) {
198  lazyTools = std::make_unique<noZS::EcalClusterLazyTools>( iEvent, iSetup,
199  ebrh, eerh, esrh );
200  } else {
201  lazyTools = std::make_unique<EcalClusterLazyTools>( iEvent, iSetup,
202  ebrh, eerh, esrh );
203  }
204 
205  if( !isAOD && src->size() ) {
206  edm::Ptr<pat::Photon> test(src->ptrAt(0));
207  if( test.isNull() || !test.isAvailable() ) {
208  throw cms::Exception("InvalidConfiguration")
209  <<"DataFormat is detected as miniAOD but cannot cast to pat::Photon!";
210  }
211  }
212 
213  std::vector<std::vector<float> > float_vars(k_NFloatVars);
214  std::vector<std::vector<int> > int_vars(k_NIntVars);
215 
216  std::unordered_map<std::string,float> float_vars_map;
217  std::unordered_map<std::string,int> int_vars_map;
218 
219  // reco::Photon::superCluster() is virtual so we can exploit polymorphism
220  for (unsigned idxpho = 0; idxpho < src->size(); ++idxpho) {
221  const auto& iPho = src->ptrAt(idxpho);
222 
223  //
224  // Compute full 5x5 quantities
225  //
226  const auto& theseed = *(iPho->superCluster()->seed());
227 
228  if( use_full5x5_ ) {
229  calculateValues<noZS::EcalClusterLazyTools>(lazyTools.get(),
230  theseed,
231  float_vars_map,
232  int_vars_map);
233  } else {
234  calculateValues<EcalClusterLazyTools>(lazyTools.get(),
235  theseed,
236  float_vars_map,
237  int_vars_map);
238  }
239 
240  check_map(float_vars_map, k_NFloatVars);
241  check_map(int_vars_map, k_NIntVars);
242 
243  for( unsigned i = 0; i < float_vars.size(); ++i ) {
244  float_vars[i].emplace_back(float_vars_map.at(float_var_names[i]));
245  }
246 
247 
248  for( unsigned i = 0; i < int_vars.size(); ++i ) {
249  int_vars[i].emplace_back(int_vars_map.at(integer_var_names[i]));
250  }
251 
252  }
253 
254  for( unsigned i = 0; i < float_vars.size(); ++i ) {
255  writeValueMap(iEvent, src, float_vars[i], float_var_names[i]);
256  }
257 
258  for( unsigned i = 0; i < int_vars.size(); ++i ) {
259  writeValueMap(iEvent, src, int_vars[i], integer_var_names[i]);
260  }
261 
262  lazyTools.reset(nullptr);
263 }
264 
265 template<typename T>
268  const std::vector<T> & values,
269  const std::string & label) const
270 {
271  using namespace edm;
272  using namespace std;
273  typedef ValueMap<T> TValueMap;
274 
275  auto_ptr<TValueMap> valMap(new TValueMap());
276  typename TValueMap::Filler filler(*valMap);
277  filler.insert(handle, values.begin(), values.end());
278  filler.fill();
279  iEvent.put(valMap, label);
280 }
281 
283  //The following says we do not know what parameters are allowed so do no validation
284  // Please change this to state exactly what you do use, even if it is no parameters
286  desc.setUnknown();
287  descriptions.addDefault(desc);
288 }
289 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::unique_ptr< EcalClusterLazyToolsBase > lazyTools
edm::EDGetTokenT< EcalRecHitCollection > ebReducedRecHitCollectionMiniAOD_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< EcalRecHitCollection > esReducedRecHitCollectionMiniAOD_
edm::EDGetTokenT< EcalRecHitCollection > eeReducedRecHitCollection_
edm::EDGetTokenT< EcalRecHitCollection > ebReducedRecHitCollection_
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
PhotonRegressionValueMapProducer(const edm::ParameterSet &)
void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< reco::Photon > > &handle, const std::vector< T > &values, const std::string &label) const
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
bool isnan(float x)
Definition: math.h:13
T sqrt(T t)
Definition: SSEVec.h:48
tuple handle
Definition: patZpeak.py:22
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< EcalRecHitCollection > esReducedRecHitCollection_
edm::EDGetTokenT< EcalRecHitCollection > eeReducedRecHitCollectionMiniAOD_
virtual void produce(edm::Event &, const edm::EventSetup &) override