CMS 3D CMS Logo

EGFull5x5ShowerShapeModifier.cc
Go to the documentation of this file.
2 
6 
9 
10 namespace {
11  const edm::EDGetTokenT<edm::ValueMap<float> > empty_token;
12  const static edm::InputTag empty_tag("");
13 }
14 
15 #include <unordered_map>
16 
18 public:
19  struct electron_config {
44  };
45 
46  struct photon_config {
71  };
72 
74 
75  void setEvent(const edm::Event&) override final;
76  void setEventContent(const edm::EventSetup&) override final;
77  void setConsumes(edm::ConsumesCollector&) override final;
78 
79  void modifyObject(pat::Electron&) const override final;
80  void modifyObject(pat::Photon&) const override final;
81 
82 private:
85  std::unordered_map<unsigned,edm::Ptr<reco::GsfElectron> > eles_by_oop; // indexed by original object ptr
86  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<float> > > ele_vmaps;
87  std::unordered_map<unsigned,edm::Ptr<reco::Photon> > phos_by_oop;
88  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<float> > > pho_vmaps;
89  mutable unsigned ele_idx,pho_idx; // hack here until we figure out why some slimmedPhotons don't have original object ptrs
90 };
91 
94  "EGFull5x5ShowerShapeModifierFromValueMaps");
95 
96 EGFull5x5ShowerShapeModifierFromValueMaps::
97 EGFull5x5ShowerShapeModifierFromValueMaps(const edm::ParameterSet& conf) :
98  ModifyObjectValueBase(conf) {
99  if( conf.exists("electron_config") ) {
100  const edm::ParameterSet& electrons = conf.getParameter<edm::ParameterSet>("electron_config");
101  if( electrons.exists("electronSrc") ) e_conf.electron_src = electrons.getParameter<edm::InputTag>("electronSrc");
102  if( electrons.exists("sigmaEtaEta") ) e_conf.sigmaEtaEta = electrons.getParameter<edm::InputTag>("sigmaEtaEta");
103  if( electrons.exists("sigmaIetaIeta") ) e_conf.sigmaIetaIeta = electrons.getParameter<edm::InputTag>("sigmaIetaIeta");
104  if( electrons.exists("sigmaIphiIphi") ) e_conf.sigmaIphiIphi = electrons.getParameter<edm::InputTag>("sigmaIphiIphi");
105  if( electrons.exists("e1x5") ) e_conf.e1x5 = electrons.getParameter<edm::InputTag>("e1x5");
106  if( electrons.exists("e2x5Max") ) e_conf.e2x5Max = electrons.getParameter<edm::InputTag>("e2x5Max");
107  if( electrons.exists("e5x5") ) e_conf.e5x5 = electrons.getParameter<edm::InputTag>("e5x5");
108  if( electrons.exists("r9") ) e_conf.r9 = electrons.getParameter<edm::InputTag>("r9");
109  if( electrons.exists("hcalDepth1OverEcal") ) e_conf.hcalDepth1OverEcal = electrons.getParameter<edm::InputTag>("hcalDepth1OverEcal");
110  if( electrons.exists("hcalDepth2OverEcal") ) e_conf.hcalDepth2OverEcal = electrons.getParameter<edm::InputTag>("hcalDepth2OverEcal");
111  if( electrons.exists("hcalDepth1OverEcalBc") ) e_conf.hcalDepth1OverEcalBc = electrons.getParameter<edm::InputTag>("hcalDepth1OverEcalBc");
112  if( electrons.exists("hcalDepth2OverEcalBc") ) e_conf.hcalDepth2OverEcalBc = electrons.getParameter<edm::InputTag>("hcalDepth2OverEcalBc");
113  }
114  if( conf.exists("photon_config") ) {
115  const edm::ParameterSet& photons = conf.getParameter<edm::ParameterSet>("photon_config");
116  if( photons.exists("photonSrc") ) ph_conf.photon_src = photons.getParameter<edm::InputTag>("photonSrc");
117  if( photons.exists("sigmaEtaEta") ) ph_conf.sigmaEtaEta = photons.getParameter<edm::InputTag>("sigmaEtaEta");
118  if( photons.exists("sigmaIetaIeta") ) ph_conf.sigmaIetaIeta = photons.getParameter<edm::InputTag>("sigmaIetaIeta");
119  if( photons.exists("e1x5") ) ph_conf.e1x5 = photons.getParameter<edm::InputTag>("e1x5");
120  if( photons.exists("e2x5") ) ph_conf.e2x5 = photons.getParameter<edm::InputTag>("e2x5");
121  if( photons.exists("e3x3") ) ph_conf.e3x3 = photons.getParameter<edm::InputTag>("e3x3");
122  if( photons.exists("e5x5") ) ph_conf.e5x5 = photons.getParameter<edm::InputTag>("e5x5");
123  if( photons.exists("maxEnergyXtal") ) ph_conf.maxEnergyXtal = photons.getParameter<edm::InputTag>("maxEnergyXtal");
124  if( photons.exists("hcalDepth1OverEcal") ) ph_conf.hcalDepth1OverEcal = photons.getParameter<edm::InputTag>("hcalDepth1OverEcal");
125  if( photons.exists("hcalDepth2OverEcal") ) ph_conf.hcalDepth2OverEcal = photons.getParameter<edm::InputTag>("hcalDepth2OverEcal");
126  if( photons.exists("hcalDepth1OverEcalBc") ) ph_conf.hcalDepth1OverEcalBc = photons.getParameter<edm::InputTag>("hcalDepth1OverEcalBc");
127  if( photons.exists("hcalDepth2OverEcalBc") ) ph_conf.hcalDepth2OverEcalBc = photons.getParameter<edm::InputTag>("hcalDepth2OverEcalBc");
128  }
129 
130  ele_idx = pho_idx = 0;
131 }
132 
133 namespace {
134  inline void get_product(const edm::Event& evt,
136  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<float> > >& map) {
137  if( !tok.isUninitialized() ) evt.getByToken(tok,map[tok.index()]);
138  }
139 }
140 
142 setEvent(const edm::Event& evt) {
143  eles_by_oop.clear();
144  phos_by_oop.clear();
145  ele_vmaps.clear();
146  pho_vmaps.clear();
147 
148  ele_idx = pho_idx = 0;
149 
153 
154  for( unsigned i = 0; i < eles->size(); ++i ) {
155  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
156  eles_by_oop[i] = ptr;
157  }
158  }
159 
160  get_product(evt,e_conf.tok_sigmaEtaEta,ele_vmaps);
161  get_product(evt,e_conf.tok_sigmaIetaIeta,ele_vmaps);
162  get_product(evt,e_conf.tok_sigmaIphiIphi,ele_vmaps);
163  get_product(evt,e_conf.tok_e1x5,ele_vmaps);
164  get_product(evt,e_conf.tok_e2x5Max,ele_vmaps);
165  get_product(evt,e_conf.tok_e5x5,ele_vmaps);
166  get_product(evt,e_conf.tok_r9,ele_vmaps);
167  get_product(evt,e_conf.tok_hcalDepth1OverEcal,ele_vmaps);
168  get_product(evt,e_conf.tok_hcalDepth2OverEcal,ele_vmaps);
169  get_product(evt,e_conf.tok_hcalDepth1OverEcalBc,ele_vmaps);
170  get_product(evt,e_conf.tok_hcalDepth2OverEcalBc,ele_vmaps);
171 
175 
176  for( unsigned i = 0; i < phos->size(); ++i ) {
177  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
178  phos_by_oop[i] = ptr;
179  }
180  }
181 
182  get_product(evt,ph_conf.tok_sigmaEtaEta,pho_vmaps);
183  get_product(evt,ph_conf.tok_sigmaIetaIeta,pho_vmaps);
184  get_product(evt,ph_conf.tok_e1x5,pho_vmaps);
185  get_product(evt,ph_conf.tok_e2x5,pho_vmaps);
186  get_product(evt,ph_conf.tok_e3x3,pho_vmaps);
187  get_product(evt,ph_conf.tok_e5x5,pho_vmaps);
188  get_product(evt,ph_conf.tok_maxEnergyXtal,pho_vmaps);
189  get_product(evt,ph_conf.tok_hcalDepth1OverEcal,pho_vmaps);
190  get_product(evt,ph_conf.tok_hcalDepth2OverEcal,pho_vmaps);
191  get_product(evt,ph_conf.tok_hcalDepth1OverEcalBc,pho_vmaps);
192  get_product(evt,ph_conf.tok_hcalDepth2OverEcalBc,pho_vmaps);
193 
194 }
195 
198 }
199 
200 namespace {
201  template<typename T, typename U, typename V>
202  inline void make_consumes(T& tag,U& tok,V& sume) { if( !(empty_tag == tag) ) tok = sume.template consumes<edm::ValueMap<float> >(tag); }
203 }
204 
207  //setup electrons
209  make_consumes(e_conf.sigmaEtaEta,e_conf.tok_sigmaEtaEta,sumes);
210  make_consumes(e_conf.sigmaIetaIeta,e_conf.tok_sigmaIetaIeta,sumes);
211  make_consumes(e_conf.sigmaIphiIphi,e_conf.tok_sigmaIphiIphi,sumes);
212  make_consumes(e_conf.e1x5,e_conf.tok_e1x5,sumes);
213  make_consumes(e_conf.e2x5Max,e_conf.tok_e2x5Max,sumes);
214  make_consumes(e_conf.e5x5,e_conf.tok_e5x5,sumes);
215  make_consumes(e_conf.r9,e_conf.tok_r9,sumes);
220 
221  // setup photons
223  make_consumes(ph_conf.sigmaEtaEta,ph_conf.tok_sigmaEtaEta,sumes);
224  make_consumes(ph_conf.sigmaIetaIeta,ph_conf.tok_sigmaIetaIeta,sumes);
225  make_consumes(ph_conf.e1x5,ph_conf.tok_e1x5,sumes);
226  make_consumes(ph_conf.e2x5,ph_conf.tok_e2x5,sumes);
227  make_consumes(ph_conf.e3x3,ph_conf.tok_e3x3,sumes);
228  make_consumes(ph_conf.e5x5,ph_conf.tok_e5x5,sumes);
229  make_consumes(ph_conf.maxEnergyXtal,ph_conf.tok_maxEnergyXtal,sumes);
234 }
235 
236 namespace {
237  template<typename T, typename U, typename V>
238  inline void assignValue(const T& ptr, const U& tok, const V& map, float& value) {
239  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
240  }
241 }
242 
245  // we encounter two cases here, either we are running AOD -> MINIAOD
246  // and the value maps are to the reducedEG object, can use original object ptr
247  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
250  auto key = eles_by_oop.find(ele_idx);
251  if( key != eles_by_oop.end() ) {
252  ptr = key->second;
253  } else {
254  throw cms::Exception("BadElectronKey")
255  << "Original object pointer with key = " << ele.originalObjectRef().key() << " not found in cache!";
256  }
257  }
258  //now we go through and modify the objects using the valuemaps we read in
259  auto full5x5 = ele.full5x5_showerShape();
260  assignValue(ptr,e_conf.tok_sigmaEtaEta,ele_vmaps,full5x5.sigmaEtaEta);
261  assignValue(ptr,e_conf.tok_sigmaIetaIeta,ele_vmaps,full5x5.sigmaIetaIeta);
262  assignValue(ptr,e_conf.tok_sigmaIphiIphi,ele_vmaps,full5x5.sigmaIphiIphi);
263  assignValue(ptr,e_conf.tok_e1x5,ele_vmaps,full5x5.e1x5);
264  assignValue(ptr,e_conf.tok_e2x5Max,ele_vmaps,full5x5.e2x5Max);
265  assignValue(ptr,e_conf.tok_e5x5,ele_vmaps,full5x5.e5x5);
266  assignValue(ptr,e_conf.tok_r9,ele_vmaps,full5x5.r9);
267  assignValue(ptr,e_conf.tok_hcalDepth1OverEcal,ele_vmaps,full5x5.hcalDepth1OverEcal);
268  assignValue(ptr,e_conf.tok_hcalDepth2OverEcal,ele_vmaps,full5x5.hcalDepth2OverEcal);
269  assignValue(ptr,e_conf.tok_hcalDepth1OverEcalBc,ele_vmaps,full5x5.hcalDepth1OverEcalBc);
270  assignValue(ptr,e_conf.tok_hcalDepth2OverEcalBc,ele_vmaps,full5x5.hcalDepth2OverEcalBc);
271 
272  ele.full5x5_setShowerShape(full5x5);
273  ++ele_idx;
274 }
275 
276 
279  // we encounter two cases here, either we are running AOD -> MINIAOD
280  // and the value maps are to the reducedEG object, can use original object ptr
281  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
284  auto key = phos_by_oop.find(pho_idx);
285  if( key != phos_by_oop.end() ) {
286  ptr = key->second;
287  } else {
288  throw cms::Exception("BadPhotonKey")
289  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
290  }
291  }
292  //now we go through and modify the objects using the valuemaps we read in
293  auto full5x5 = pho.full5x5_showerShapeVariables();
294  assignValue(ptr,ph_conf.tok_sigmaEtaEta,pho_vmaps,full5x5.sigmaEtaEta);
295  assignValue(ptr,ph_conf.tok_sigmaIetaIeta,pho_vmaps,full5x5.sigmaIetaIeta);
296  assignValue(ptr,ph_conf.tok_e1x5,pho_vmaps,full5x5.e1x5);
297  assignValue(ptr,ph_conf.tok_e2x5,pho_vmaps,full5x5.e2x5);
298  assignValue(ptr,ph_conf.tok_e3x3,pho_vmaps,full5x5.e3x3);
299  assignValue(ptr,ph_conf.tok_e5x5,pho_vmaps,full5x5.e5x5);
300  assignValue(ptr,ph_conf.tok_maxEnergyXtal,pho_vmaps,full5x5.maxEnergyXtal);
301  assignValue(ptr,ph_conf.tok_hcalDepth1OverEcal,pho_vmaps,full5x5.hcalDepth1OverEcal);
302  assignValue(ptr,ph_conf.tok_hcalDepth2OverEcal,pho_vmaps,full5x5.hcalDepth2OverEcal);
303  assignValue(ptr,ph_conf.tok_hcalDepth1OverEcalBc,pho_vmaps,full5x5.hcalDepth1OverEcalBc);
304  assignValue(ptr,ph_conf.tok_hcalDepth2OverEcalBc,pho_vmaps,full5x5.hcalDepth2OverEcalBc);
305 
306  pho.full5x5_setShowerShapeVariables(full5x5);
307  ++pho_idx;
308 }
edm::EDGetTokenT< edm::View< pat::Photon > > tok_photon_src
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
Analysis-level Photon class.
Definition: Photon.h:47
Definition: Photon.py:1
edm::EDGetTokenT< edm::ValueMap< float > > tok_hcalDepth2OverEcalBc
key_type key() const
Definition: Ptr.h:186
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
std::unordered_map< unsigned, edm::Ptr< reco::Photon > > phos_by_oop
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::EDGetTokenT< edm::View< pat::Electron > > tok_electron_src
std::unordered_map< unsigned, edm::Ptr< reco::GsfElectron > > eles_by_oop
void setEventContent(const edm::EventSetup &) override final
edm::EDGetTokenT< edm::ValueMap< float > > tok_hcalDepth2OverEcalBc
Definition: HeavyIon.h:7
U second(std::pair< T, U > const &p)
void setEvent(const edm::Event &) override final
edm::EDGetTokenT< edm::ValueMap< float > > tok_hcalDepth2OverEcal
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > ele_vmaps
edm::EDGetTokenT< edm::ValueMap< float > > tok_hcalDepth1OverEcal
Definition: value.py:1
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Definition: PATObject.h:500
edm::EDGetTokenT< edm::ValueMap< float > > tok_hcalDepth1OverEcalBc
void modifyObject(pat::Electron &) const override final
Analysis-level electron class.
Definition: Electron.h:52
EGFull5x5ShowerShapeModifierFromValueMaps(const edm::ParameterSet &conf)
void full5x5_setShowerShapeVariables(const ShowerShape &a)
Definition: Photon.h:201
const ShowerShape & full5x5_showerShape() const
Definition: GsfElectron.h:475
fixed size matrix
HLT enums.
const ShowerShape & full5x5_showerShapeVariables() const
Definition: Photon.h:198
void setConsumes(edm::ConsumesCollector &) override final
bool isUninitialized() const
Definition: EDGetToken.h:73
#define DEFINE_EDM_PLUGIN(factory, type, name)
edm::EDGetTokenT< edm::ValueMap< float > > tok_hcalDepth1OverEcalBc
long double T
void full5x5_setShowerShape(const ShowerShape &s)
Definition: GsfElectron.h:479
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > pho_vmaps