CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 {
47  };
48 
49  struct photon_config {
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) :
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 inline void get_product(const edm::Event& evt,
105  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<float> > >& map) {
106  if( !tok.isUninitialized() ) evt.getByToken(tok,map[tok.index()]);
107 }
108 
110 setEvent(const edm::Event& evt) {
111  eles_by_oop.clear();
112  phos_by_oop.clear();
113  ele_vmaps.clear();
114  pho_vmaps.clear();
115 
116  ele_idx = pho_idx = 0;
117 
121 
122  for( unsigned i = 0; i < eles->size(); ++i ) {
123  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
124  eles_by_oop[i] = ptr;
125  }
126  }
127 
128  for( const std::string& varname : electron_vars ) {
129  auto& inputs = e_conf.electron_inputs;
130  if( inputs.find(varname) == inputs.end() ) continue;
131  get_product(evt,std::get<1>(inputs[varname]),ele_vmaps);
132  }
133 
137 
138  for( unsigned i = 0; i < phos->size(); ++i ) {
139  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
140  phos_by_oop[i] = ptr;
141  }
142  }
143 
144  for( const std::string& varname : photon_vars ) {
145  auto& inputs = ph_conf.photon_inputs;
146  if( inputs.find(varname) == inputs.end() ) continue;
147  get_product(evt,std::get<1>(inputs[varname]),pho_vmaps);
148  }
149 }
150 
153 }
154 
155 template<typename T, typename U, typename V>
156 inline void make_consumes(T& tag,U& tok,V& sume) { if( !(empty_tag == tag) ) tok = sume.template consumes<edm::ValueMap<float> >(tag); }
157 
160  //setup electrons
162 
163  for( const std::string& varname : electron_vars ) {
164  auto& inputs = e_conf.electron_inputs;
165  if( inputs.find(varname) == inputs.end() ) continue;
166  auto& the_tuple = inputs[varname];
167  make_consumes(std::get<0>(the_tuple),std::get<1>(the_tuple),sumes);
168  }
169 
170  // setup photons
172 
173  for( const std::string& varname : photon_vars ) {
174  auto& inputs = ph_conf.photon_inputs;
175  if( inputs.find(varname) == inputs.end() ) continue;
176  auto& the_tuple = inputs[varname];
177  make_consumes(std::get<0>(the_tuple),std::get<1>(the_tuple),sumes);
178  }
179 }
180 
181 template<typename T, typename U, typename V>
182 inline void assignValue(const T& ptr, const U& input_map, const std::string& name, const V& map, float& value) {
183  auto itr = input_map.find(name);
184  if( itr == input_map.end() ) return;
185  const auto& tok = std::get<1>(itr->second);
186  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
187 }
188 
191  // we encounter two cases here, either we are running AOD -> MINIAOD
192  // and the value maps are to the reducedEG object, can use original object ptr
193  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
196  auto key = eles_by_oop.find(ele_idx);
197  if( key != eles_by_oop.end() ) {
198  ptr = key->second;
199  } else {
200  throw cms::Exception("BadElectronKey")
201  << "Original object pointer with key = " << ele.originalObjectRef().key() << " not found in cache!";
202  }
203  }
204  //now we go through and modify the objects using the valuemaps we read in
205  auto pfIso = ele.pfIsolationVariables();
206 
207  const auto& e_inputs = e_conf.electron_inputs;
208 
209  assignValue(ptr,e_inputs,electron_vars[0],ele_vmaps,pfIso.sumChargedHadronPt);
210  assignValue(ptr,e_inputs,electron_vars[1],ele_vmaps,pfIso.sumNeutralHadronEt);
211  assignValue(ptr,e_inputs,electron_vars[2],ele_vmaps,pfIso.sumPhotonEt);
212  assignValue(ptr,e_inputs,electron_vars[3],ele_vmaps,pfIso.sumChargedParticlePt);
213  assignValue(ptr,e_inputs,electron_vars[4],ele_vmaps,pfIso.sumNeutralHadronEtHighThreshold);
214  assignValue(ptr,e_inputs,electron_vars[5],ele_vmaps,pfIso.sumPhotonEtHighThreshold);
215  assignValue(ptr,e_inputs,electron_vars[6],ele_vmaps,pfIso.sumPUPt);
216 
217  ele.setPfIsolationVariables(pfIso);
218  ++ele_idx;
219 }
220 
221 
224  // we encounter two cases here, either we are running AOD -> MINIAOD
225  // and the value maps are to the reducedEG object, can use original object ptr
226  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
229  auto key = phos_by_oop.find(pho_idx);
230  if( key != phos_by_oop.end() ) {
231  ptr = key->second;
232  } else {
233  throw cms::Exception("BadPhotonKey")
234  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
235  }
236  }
237 
238  //now we go through and modify the objects using the valuemaps we read in
239  auto pfIso = pho.getPflowIsolationVariables();
240 
241  const auto& ph_inputs = ph_conf.photon_inputs;
242 
243  assignValue(ptr,ph_inputs,photon_vars[0],pho_vmaps,pfIso.chargedHadronIso);
244  assignValue(ptr,ph_inputs,photon_vars[1],pho_vmaps,pfIso.chargedHadronIsoWrongVtx);
245  assignValue(ptr,ph_inputs,photon_vars[2],pho_vmaps,pfIso.neutralHadronIso);
246  assignValue(ptr,ph_inputs,photon_vars[3],pho_vmaps,pfIso.photonIso);
247  assignValue(ptr,ph_inputs,photon_vars[4],pho_vmaps,pfIso.modFrixione);
248  assignValue(ptr,ph_inputs,photon_vars[5],pho_vmaps,pfIso.sumChargedParticlePt);
249  assignValue(ptr,ph_inputs,photon_vars[6],pho_vmaps,pfIso.sumNeutralHadronEtHighThreshold);
250  assignValue(ptr,ph_inputs,photon_vars[7],pho_vmaps,pfIso.sumPhotonEtHighThreshold);
251  assignValue(ptr,ph_inputs,photon_vars[8],pho_vmaps,pfIso.sumPUPt);
252 
253  pho.setPflowIsolationVariables(pfIso);
254  ++pho_idx;
255 }
const PflowIsolationVariables & pfIsolationVariables() const
Definition: GsfElectron.h:604
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
void setPflowIsolationVariables(const PflowIsolationVariables &pfisol)
Set Particle Flow Isolation variables.
Definition: Photon.h:450
T getParameter(std::string const &) const
void modifyObject(pat::Electron &) const overridefinal
int i
Definition: DBlmapReader.cc:9
Analysis-level Photon class.
Definition: Photon.h:47
std::unordered_map< unsigned, edm::Ptr< reco::GsfElectron > > eles_by_oop
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:169
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
void make_consumes(T &tag, U &tok, V &sume)
bool exists(std::string const &parameterName) const
checks if a parameter exists
void setConsumes(edm::ConsumesCollector &) overridefinal
void setPfIsolationVariables(const PflowIsolationVariables &iso)
Definition: GsfElectron.h:609
U second(std::pair< T, U > const &p)
void get_product(const edm::Event &evt, const edm::EDGetTokenT< edm::ValueMap< T > > &tok, std::unordered_map< unsigned, edm::Handle< edm::ValueMap< T > > > &map)
void setEvent(const edm::Event &) overridefinal
const std::string & name() const
edm::EDGetTokenT< edm::View< pat::Photon > > tok_photon_src
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Definition: PATObject.h:484
std::unordered_map< std::string, tag_and_token > input_map
tuple conf
Definition: dbtoconf.py:185
Analysis-level electron class.
Definition: Electron.h:52
string const
Definition: compareJSON.py:14
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > ele_vmaps
#define private
Definition: FWFileEntry.h:17
edm::EDGetTokenT< edm::View< pat::Electron > > tok_electron_src
void assignValue(const T &ptr, const U &tok, const V &map, Z &value)
void setEventContent(const edm::EventSetup &) overridefinal
bool isUninitialized() const
Definition: EDGetToken.h:71
#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:447
long double T