CMS 3D CMS Logo

EGPfIsolationModifier.cc
Go to the documentation of this file.
2 
6 
9 
10 #include <tuple>
11 #include <array>
12 
13 namespace {
14  const edm::EDGetTokenT<edm::ValueMap<float> > empty_token;
15  const static edm::InputTag empty_tag("");
16 
17  const static std::array<std::string,7> electron_vars = { { "sumChargedHadronPt",
18  "sumNeutralHadronEt",
19  "sumPhotonEt",
20  "sumChargedParticlePt",
21  "sumNeutralHadronEtHighThreshold",
22  "sumPhotonEtHighThreshold",
23  "sumPUPt" } };
24 
25  const static std::array<std::string,9> photon_vars = { { "chargedHadronIso",
26  "chargedHadronIsoWrongVtx",
27  "neutralHadronIso",
28  "photonIso",
29  "modFrixione",
30  "sumChargedParticlePt",
31  "sumNeutralHadronEtHighThreshold",
32  "sumPhotonEtHighThreshold",
33  "sumPUPt" } };
34 }
35 
36 #include <unordered_map>
37 
39 public:
40  typedef std::tuple<edm::InputTag,edm::EDGetTokenT<edm::ValueMap<float> > > tag_and_token;
41  typedef std::unordered_map<std::string,tag_and_token> input_map;
42 
43  struct electron_config {
46  input_map electron_inputs;
47  };
48 
49  struct photon_config {
52  input_map photon_inputs;
53  };
54 
56 
57  void setEvent(const edm::Event&) override final;
58  void setEventContent(const edm::EventSetup&) override final;
59  void setConsumes(edm::ConsumesCollector&) override final;
60 
61  void modifyObject(pat::Electron&) const override final;
62  void modifyObject(pat::Photon&) const override final;
63 
64 private:
67  std::unordered_map<unsigned,edm::Ptr<reco::GsfElectron> > eles_by_oop; // indexed by original object ptr
68  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<float> > > ele_vmaps;
69  std::unordered_map<unsigned,edm::Ptr<reco::Photon> > phos_by_oop;
70  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<float> > > pho_vmaps;
71  mutable unsigned ele_idx,pho_idx; // hack here until we figure out why some slimmedPhotons don't have original object ptrs
72 };
73 
76  "EGPfIsolationModifierFromValueMaps");
77 
78 EGPfIsolationModifierFromValueMaps::
79 EGPfIsolationModifierFromValueMaps(const edm::ParameterSet& conf) :
80  ModifyObjectValueBase(conf) {
81  if( conf.exists("electron_config") ) {
82  const edm::ParameterSet& electrons = conf.getParameter<edm::ParameterSet>("electron_config");
83  if( electrons.exists("electronSrc") ) e_conf.electron_src = electrons.getParameter<edm::InputTag>("electronSrc");
84  for( const std::string& varname : electron_vars ) {
85  if( electrons.exists(varname) ) {
86  std::get<0>(e_conf.electron_inputs[varname]) = electrons.getParameter<edm::InputTag>(varname);
87  }
88  }
89  }
90  if( conf.exists("photon_config") ) {
91  const edm::ParameterSet& photons = conf.getParameter<edm::ParameterSet>("photon_config");
92  if( photons.exists("photonSrc") ) ph_conf.photon_src = photons.getParameter<edm::InputTag>("photonSrc");
93  for( const std::string& varname : photon_vars ) {
94  if( photons.exists(varname) ) {
95  std::get<0>(ph_conf.photon_inputs[varname]) = photons.getParameter<edm::InputTag>(varname);
96  }
97  }
98  }
99 
100  ele_idx = pho_idx = 0;
101 }
102 
103 namespace {
104  inline void get_product(const edm::Event& evt,
106  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<float> > >& map) {
107  if( !tok.isUninitialized() ) evt.getByToken(tok,map[tok.index()]);
108  }
109 }
110 
112 setEvent(const edm::Event& evt) {
113  eles_by_oop.clear();
114  phos_by_oop.clear();
115  ele_vmaps.clear();
116  pho_vmaps.clear();
117 
118  ele_idx = pho_idx = 0;
119 
123 
124  for( unsigned i = 0; i < eles->size(); ++i ) {
125  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
126  eles_by_oop[i] = ptr;
127  }
128  }
129 
130  for( const std::string& varname : electron_vars ) {
131  auto& inputs = e_conf.electron_inputs;
132  if( inputs.find(varname) == inputs.end() ) continue;
133  get_product(evt,std::get<1>(inputs[varname]),ele_vmaps);
134  }
135 
139 
140  for( unsigned i = 0; i < phos->size(); ++i ) {
141  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
142  phos_by_oop[i] = ptr;
143  }
144  }
145 
146  for( const std::string& varname : photon_vars ) {
147  auto& inputs = ph_conf.photon_inputs;
148  if( inputs.find(varname) == inputs.end() ) continue;
149  get_product(evt,std::get<1>(inputs[varname]),pho_vmaps);
150  }
151 }
152 
155 }
156 
157 namespace {
158  template<typename T, typename U, typename V>
159  inline void make_consumes(T& tag,U& tok,V& sume) { if( !(empty_tag == tag) ) tok = sume.template consumes<edm::ValueMap<float> >(tag); }
160 }
161 
164  //setup electrons
166 
167  for( const std::string& varname : electron_vars ) {
168  auto& inputs = e_conf.electron_inputs;
169  if( inputs.find(varname) == inputs.end() ) continue;
170  auto& the_tuple = inputs[varname];
171  make_consumes(std::get<0>(the_tuple),std::get<1>(the_tuple),sumes);
172  }
173 
174  // setup photons
176 
177  for( const std::string& varname : photon_vars ) {
178  auto& inputs = ph_conf.photon_inputs;
179  if( inputs.find(varname) == inputs.end() ) continue;
180  auto& the_tuple = inputs[varname];
181  make_consumes(std::get<0>(the_tuple),std::get<1>(the_tuple),sumes);
182  }
183 }
184 
185 namespace {
186  template<typename T, typename U, typename V>
187  inline void assignValue(const T& ptr, const U& input_map, const std::string& name, const V& map, float& value) {
188  auto itr = input_map.find(name);
189  if( itr == input_map.end() ) return;
190  const auto& tok = std::get<1>(itr->second);
191  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
192  }
193 }
194 
197  // we encounter two cases here, either we are running AOD -> MINIAOD
198  // and the value maps are to the reducedEG object, can use original object ptr
199  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
202  auto key = eles_by_oop.find(ele_idx);
203  if( key != eles_by_oop.end() ) {
204  ptr = key->second;
205  } else {
206  throw cms::Exception("BadElectronKey")
207  << "Original object pointer with key = " << ele.originalObjectRef().key() << " not found in cache!";
208  }
209  }
210  //now we go through and modify the objects using the valuemaps we read in
211  auto pfIso = ele.pfIsolationVariables();
212 
213  const auto& e_inputs = e_conf.electron_inputs;
214 
215  assignValue(ptr,e_inputs,electron_vars[0],ele_vmaps,pfIso.sumChargedHadronPt);
216  assignValue(ptr,e_inputs,electron_vars[1],ele_vmaps,pfIso.sumNeutralHadronEt);
217  assignValue(ptr,e_inputs,electron_vars[2],ele_vmaps,pfIso.sumPhotonEt);
218  assignValue(ptr,e_inputs,electron_vars[3],ele_vmaps,pfIso.sumChargedParticlePt);
219  assignValue(ptr,e_inputs,electron_vars[4],ele_vmaps,pfIso.sumNeutralHadronEtHighThreshold);
220  assignValue(ptr,e_inputs,electron_vars[5],ele_vmaps,pfIso.sumPhotonEtHighThreshold);
221  assignValue(ptr,e_inputs,electron_vars[6],ele_vmaps,pfIso.sumPUPt);
222 
223  ele.setPfIsolationVariables(pfIso);
224  ++ele_idx;
225 }
226 
227 
230  // we encounter two cases here, either we are running AOD -> MINIAOD
231  // and the value maps are to the reducedEG object, can use original object ptr
232  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
235  auto key = phos_by_oop.find(pho_idx);
236  if( key != phos_by_oop.end() ) {
237  ptr = key->second;
238  } else {
239  throw cms::Exception("BadPhotonKey")
240  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
241  }
242  }
243 
244  //now we go through and modify the objects using the valuemaps we read in
245  auto pfIso = pho.getPflowIsolationVariables();
246 
247  const auto& ph_inputs = ph_conf.photon_inputs;
248 
249  assignValue(ptr,ph_inputs,photon_vars[0],pho_vmaps,pfIso.chargedHadronIso);
250  assignValue(ptr,ph_inputs,photon_vars[1],pho_vmaps,pfIso.chargedHadronIsoWrongVtx);
251  assignValue(ptr,ph_inputs,photon_vars[2],pho_vmaps,pfIso.neutralHadronIso);
252  assignValue(ptr,ph_inputs,photon_vars[3],pho_vmaps,pfIso.photonIso);
253  assignValue(ptr,ph_inputs,photon_vars[4],pho_vmaps,pfIso.modFrixione);
254  assignValue(ptr,ph_inputs,photon_vars[5],pho_vmaps,pfIso.sumChargedParticlePt);
255  assignValue(ptr,ph_inputs,photon_vars[6],pho_vmaps,pfIso.sumNeutralHadronEtHighThreshold);
256  assignValue(ptr,ph_inputs,photon_vars[7],pho_vmaps,pfIso.sumPhotonEtHighThreshold);
257  assignValue(ptr,ph_inputs,photon_vars[8],pho_vmaps,pfIso.sumPUPt);
258 
259  pho.setPflowIsolationVariables(pfIso);
260  ++pho_idx;
261 }
const PflowIsolationVariables & pfIsolationVariables() const
Definition: GsfElectron.h:670
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
void setPflowIsolationVariables(const PflowIsolationVariables &pfisol)
Set Particle Flow Isolation variables.
Definition: Photon.h:497
T getParameter(std::string const &) const
void setEvent(const edm::Event &) override final
Analysis-level Photon class.
Definition: Photon.h:47
std::unordered_map< unsigned, edm::Ptr< reco::GsfElectron > > eles_by_oop
Definition: Photon.py:1
std::unordered_map< unsigned, edm::Ptr< reco::Photon > > phos_by_oop
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > pho_vmaps
std::tuple< edm::InputTag, edm::EDGetTokenT< edm::ValueMap< float > > > tag_and_token
key_type key() const
Definition: Ptr.h:186
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
void setConsumes(edm::ConsumesCollector &) override final
void setEventContent(const edm::EventSetup &) override final
bool exists(std::string const &parameterName) const
checks if a parameter exists
void setPfIsolationVariables(const PflowIsolationVariables &iso)
Definition: GsfElectron.h:675
void modifyObject(pat::Electron &) const override final
Definition: HeavyIon.h:7
U second(std::pair< T, U > const &p)
const std::string & name() const
edm::EDGetTokenT< edm::View< pat::Photon > > tok_photon_src
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
std::unordered_map< std::string, tag_and_token > input_map
Analysis-level electron class.
Definition: Electron.h:52
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > ele_vmaps
edm::EDGetTokenT< edm::View< pat::Electron > > tok_electron_src
fixed size matrix
HLT enums.
bool isUninitialized() const
Definition: EDGetToken.h:73
#define DEFINE_EDM_PLUGIN(factory, type, name)
EGPfIsolationModifierFromValueMaps(const edm::ParameterSet &conf)
const PflowIsolationVariables & getPflowIsolationVariables() const
Get Particle Flow Isolation variables block.
Definition: Photon.h:494
long double T