CMS 3D CMS Logo

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_NFloatVars = 0 };
26 
27  enum reg_int_vars { k_NIntVars = 0 };
28 
29  const std::vector<std::string> float_var_names( { } );
30 
31  const std::vector<std::string> integer_var_names( { } );
32 
33  inline void set_map_val( const reg_float_vars index, const float value,
34  std::unordered_map<std::string,float>& map) {
35  map[float_var_names[index]] = value;
36  }
37  inline void set_map_val( const reg_int_vars index, const int value,
38  std::unordered_map<std::string,int>& map) {
39  map[integer_var_names[index]] = value;
40  }
41 
42  template<typename T>
43  inline void check_map(const std::unordered_map<std::string,T>& map, unsigned exp_size) {
44  if( map.size() != exp_size ) {
45  throw cms::Exception("PhotonRegressionWeirdConfig")
46  << "variable map size: " << map.size()
47  << " not equal to expected size: " << exp_size << " !"
48  << " The regression variable calculation code definitely has a bug, fix it!";
49  }
50  }
51 
52  template<typename LazyTools,typename SeedType>
53  inline void calculateValues(EcalClusterLazyToolsBase* tools_tocast,
54  const SeedType& the_seed,
55  std::unordered_map<std::string,float>& float_vars,
56  std::unordered_map<std::string,int>& int_vars ) {
57  }
58 }
59 
61 
62  public:
63 
66 
67  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
68 
69  private:
70 
71  void produce(edm::Event&, const edm::EventSetup&) override;
72 
73  template<typename T>
76  const std::vector<T> & values,
77  const std::string & label) const ;
78 
79  // The object that will compute 5x5 quantities
80  std::unique_ptr<EcalClusterLazyToolsBase> lazyTools;
81 
82  // for AOD case
87 
88  // for miniAOD case
93 
94  const bool use_full5x5_;
95 };
96 
98  use_full5x5_(iConfig.getParameter<bool>("useFull5x5")) {
99 
100  //
101  // Declare consummables, handle both AOD and miniAOD case
102  //
103  ebReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
104  ("ebReducedRecHitCollection"));
105  ebReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
106  ("ebReducedRecHitCollectionMiniAOD"));
107 
108  eeReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
109  ("eeReducedRecHitCollection"));
110  eeReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
111  ("eeReducedRecHitCollectionMiniAOD"));
112 
113  esReducedRecHitCollection_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
114  ("esReducedRecHitCollection"));
115  esReducedRecHitCollectionMiniAOD_ = mayConsume<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>
116  ("esReducedRecHitCollectionMiniAOD"));
117 
118  // reco photons are castable into pat photons, so no need to handle reco/pat seprately
119  src_ = mayConsume<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("src"));
120  srcMiniAOD_ = mayConsume<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"));
121 
122  //
123  // Declare producibles
124  //
125  // Cluster shapes
126  for( const std::string& name : float_var_names ) {
127  produces<edm::ValueMap<float> >(name);
128  }
129 
130  for( const std::string& name : integer_var_names ) {
131  produces<edm::ValueMap<int> >(name);
132  }
133 }
134 
136 {}
137 
139 
140  using namespace edm;
141 
143 
144  bool isAOD = true;
145  iEvent.getByToken(src_, src);
146  if(!src.isValid() ){
147  isAOD = false;
148  iEvent.getByToken(srcMiniAOD_, src);
149  }
150 
151  if( !src.isValid() ) {
152  throw cms::Exception("IllDefinedDataTier")
153  << "DataFormat does not contain a photon source!";
154  }
155 
156  // configure lazy tools
158 
159  if( isAOD ) {
163  } else {
167  }
168 
169  if( use_full5x5_ ) {
170  lazyTools = std::make_unique<noZS::EcalClusterLazyTools>( iEvent, iSetup,
171  ebrh, eerh, esrh );
172  } else {
173  lazyTools = std::make_unique<EcalClusterLazyTools>( iEvent, iSetup,
174  ebrh, eerh, esrh );
175  }
176 
177  if( !isAOD && !src->empty() ) {
178  edm::Ptr<pat::Photon> test(src->ptrAt(0));
179  if( test.isNull() || !test.isAvailable() ) {
180  throw cms::Exception("InvalidConfiguration")
181  <<"DataFormat is detected as miniAOD but cannot cast to pat::Photon!";
182  }
183  }
184 
185  std::vector<std::vector<float> > float_vars(k_NFloatVars);
186  std::vector<std::vector<int> > int_vars(k_NIntVars);
187 
188  std::unordered_map<std::string,float> float_vars_map;
189  std::unordered_map<std::string,int> int_vars_map;
190 
191  // reco::Photon::superCluster() is virtual so we can exploit polymorphism
192  for (unsigned idxpho = 0; idxpho < src->size(); ++idxpho) {
193  const auto& iPho = src->ptrAt(idxpho);
194 
195  //
196  // Compute full 5x5 quantities
197  //
198  const auto& theseed = *(iPho->superCluster()->seed());
199 
200  if( use_full5x5_ ) {
201  calculateValues<noZS::EcalClusterLazyTools>(lazyTools.get(),
202  theseed,
203  float_vars_map,
204  int_vars_map);
205  } else {
206  calculateValues<EcalClusterLazyTools>(lazyTools.get(),
207  theseed,
208  float_vars_map,
209  int_vars_map);
210  }
211 
212  check_map(float_vars_map, k_NFloatVars);
213  check_map(int_vars_map, k_NIntVars);
214 
215  for( unsigned i = 0; i < float_vars.size(); ++i ) {
216  float_vars[i].emplace_back(float_vars_map.at(float_var_names[i]));
217  }
218 
219 
220  for( unsigned i = 0; i < int_vars.size(); ++i ) {
221  int_vars[i].emplace_back(int_vars_map.at(integer_var_names[i]));
222  }
223 
224  }
225 
226  for( unsigned i = 0; i < float_vars.size(); ++i ) {
227  writeValueMap(iEvent, src, float_vars[i], float_var_names[i]);
228  }
229 
230  for( unsigned i = 0; i < int_vars.size(); ++i ) {
231  writeValueMap(iEvent, src, int_vars[i], integer_var_names[i]);
232  }
233 
234  lazyTools.reset(nullptr);
235 }
236 
237 template<typename T>
240  const std::vector<T> & values,
241  const std::string & label) const
242 {
243  using namespace edm;
244  using namespace std;
245  typedef ValueMap<T> TValueMap;
246 
247  auto valMap = std::make_unique<TValueMap>();
248  typename TValueMap::Filler filler(*valMap);
249  filler.insert(handle, values.begin(), values.end());
250  filler.fill();
251  iEvent.put(std::move(valMap), label);
252 }
253 
255  //The following says we do not know what parameters are allowed so do no validation
256  // Please change this to state exactly what you do use, even if it is no parameters
258  desc.setUnknown();
259  descriptions.addDefault(desc);
260 }
261 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
std::unique_ptr< EcalClusterLazyToolsBase > lazyTools
edm::EDGetTokenT< EcalRecHitCollection > ebReducedRecHitCollectionMiniAOD_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#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
Definition: value.py:1
bool isValid() const
Definition: HandleBase.h:74
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HLT enums.
edm::EDGetTokenT< EcalRecHitCollection > esReducedRecHitCollection_
edm::EDGetTokenT< EcalRecHitCollection > eeReducedRecHitCollectionMiniAOD_
void produce(edm::Event &, const edm::EventSetup &) override
def move(src, dest)
Definition: eostools.py:510