CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EGExtraInfoModifierFromIntValueMaps.cc
Go to the documentation of this file.
2 
6 
9 
10 namespace {
11  const edm::EDGetTokenT<edm::ValueMap<int> > empty_token;
12  const edm::InputTag empty_tag;
13 }
14 
15 #include <unordered_map>
16 
18 public:
20  typedef std::unordered_map<std::string,ValMapIntToken> ValueMaps;
21  typedef std::unordered_map<std::string,edm::InputTag> ValueMapsTags;
22  struct electron_config {
27  };
28 
29  struct photon_config {
34  };
35 
37 
38  void setEvent(const edm::Event&) override final;
39  void setEventContent(const edm::EventSetup&) override final;
40  void setConsumes(edm::ConsumesCollector&) override final;
41 
42  void modifyObject(pat::Electron&) const override final;
43  void modifyObject(pat::Photon&) const override final;
44 
45 private:
48  std::unordered_map<unsigned,edm::Ptr<reco::GsfElectron> > eles_by_oop; // indexed by original object ptr
49  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<int> > > ele_vmaps;
50  std::unordered_map<unsigned,edm::Ptr<reco::Photon> > phos_by_oop;
51  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<int> > > pho_vmaps;
52  mutable unsigned ele_idx,pho_idx; // hack here until we figure out why some slimmedPhotons don't have original object ptrs
53 };
54 
57  "EGExtraInfoModifierFromIntValueMaps");
58 
59 EGExtraInfoModifierFromIntValueMaps::
60 EGExtraInfoModifierFromIntValueMaps(const edm::ParameterSet& conf) :
62  constexpr char electronSrc[] = "electronSrc";
63  constexpr char photonSrc[] = "photonSrc";
64 
65  if( conf.exists("electron_config") ) {
66  const edm::ParameterSet& electrons = conf.getParameter<edm::ParameterSet>("electron_config");
67  if( electrons.exists(electronSrc) ) e_conf.electron_src = electrons.getParameter<edm::InputTag>(electronSrc);
68  const std::vector<std::string> parameters = electrons.getParameterNames();
69  for( const std::string& name : parameters ) {
70  if( std::string(electronSrc) == name ) continue;
71  if( electrons.existsAs<edm::InputTag>(name) ) {
72  e_conf.valuemaps[name] = electrons.getParameter<edm::InputTag>(name);
73  }
74  }
75  }
76  if( conf.exists("photon_config") ) {
77  const edm::ParameterSet& photons = conf.getParameter<edm::ParameterSet>("photon_config");
78  if( photons.exists(photonSrc) ) ph_conf.photon_src = photons.getParameter<edm::InputTag>(photonSrc);
79  const std::vector<std::string> parameters = photons.getParameterNames();
80  for( const std::string& name : parameters ) {
81  if( std::string(photonSrc) == name ) continue;
82  if( photons.existsAs<edm::InputTag>(name) ) {
83  ph_conf.valuemaps[name] = photons.getParameter<edm::InputTag>(name);
84  }
85  }
86  }
87  ele_idx = pho_idx = 0;
88 }
89 
90 inline void get_product(const edm::Event& evt,
92  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<int> > >& map) {
93  evt.getByToken(tok,map[tok.index()]);
94 }
95 
97 setEvent(const edm::Event& evt) {
98  eles_by_oop.clear();
99  phos_by_oop.clear();
100  ele_vmaps.clear();
101  pho_vmaps.clear();
102 
103  ele_idx = pho_idx = 0;
104 
108 
109  for( unsigned i = 0; i < eles->size(); ++i ) {
110  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
111  eles_by_oop[i] = ptr;
112  }
113  }
114 
115  for( auto itr = e_conf.tok_valuemaps.begin(); itr != e_conf.tok_valuemaps.end(); ++itr ) {
116  get_product(evt,itr->second,ele_vmaps);
117  }
118 
122 
123  for( unsigned i = 0; i < phos->size(); ++i ) {
124  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
125  phos_by_oop[i] = ptr;
126  }
127  }
128 
129  for( auto itr = ph_conf.tok_valuemaps.begin(); itr != ph_conf.tok_valuemaps.end(); ++itr ) {
130  get_product(evt,itr->second,pho_vmaps);
131  }
132 }
133 
136 }
137 
138 template<typename T, typename U, typename V>
139 inline void make_consumes(T& tag,U& tok,V& sume) { if( !(empty_tag == tag) ) tok = sume.template consumes<edm::ValueMap<int> >(tag); }
140 
143  //setup electrons
145 
146  for( auto itr = e_conf.valuemaps.begin(); itr != e_conf.valuemaps.end(); ++itr ) {
147  make_consumes(itr->second,e_conf.tok_valuemaps[itr->first],sumes);
148  }
149 
150  // setup photons
152 
153  for( auto itr = ph_conf.valuemaps.begin(); itr != ph_conf.valuemaps.end(); ++itr ) {
154  make_consumes(itr->second,ph_conf.tok_valuemaps[itr->first],sumes);
155  }
156 }
157 
158 template<typename T, typename U, typename V>
159 inline void assignValue(const T& ptr, const U& tok, const V& map, int& value) {
160  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
161 }
162 
165  // we encounter two cases here, either we are running AOD -> MINIAOD
166  // and the value maps are to the reducedEG object, can use original object ptr
167  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
170  auto key = eles_by_oop.find(ele_idx);
171  if( key != eles_by_oop.end() ) {
172  ptr = key->second;
173  } else {
174  throw cms::Exception("BadElectronKey")
175  << "Original object pointer with key = " << ele.originalObjectRef().key()
176  << " not found in cache!";
177  }
178  }
179  //now we go through and modify the objects using the valuemaps we read in
180  for( auto itr = e_conf.tok_valuemaps.begin(); itr != e_conf.tok_valuemaps.end(); ++itr ) {
181  int value(0);
182  assignValue(ptr,itr->second,ele_vmaps,value);
183  if( !ele.hasUserInt(itr->first) ) {
184  ele.addUserInt(itr->first,value);
185  } else {
186  throw cms::Exception("ValueNameAlreadyExists")
187  << "Trying to add new UserInt = " << itr->first
188  << " failed because it already exists!";
189  }
190  }
191  ++ele_idx;
192 }
193 
196  // we encounter two cases here, either we are running AOD -> MINIAOD
197  // and the value maps are to the reducedEG object, can use original object ptr
198  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
201  auto key = phos_by_oop.find(pho_idx);
202  if( key != phos_by_oop.end() ) {
203  ptr = key->second;
204  } else {
205  throw cms::Exception("BadPhotonKey")
206  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
207  }
208  }
209  //now we go through and modify the objects using the valuemaps we read in
210  for( auto itr = ph_conf.tok_valuemaps.begin(); itr != ph_conf.tok_valuemaps.end(); ++itr ) {
211  int value(0);
212  assignValue(ptr,itr->second,pho_vmaps,value);
213  if( !pho.hasUserInt(itr->first) ) {
214  pho.addUserInt(itr->first,value);
215  } else {
216  throw cms::Exception("ValueNameAlreadyExists")
217  << "Trying to add new UserInt = " << itr->first
218  << " failed because it already exists!";
219  }
220  }
221  ++pho_idx;
222 }
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
Analysis-level Photon class.
Definition: Photon.h:47
dictionary parameters
Definition: Parameters.py:2
void setEvent(const edm::Event &) overridefinal
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:185
void modifyObject(pat::Electron &) const overridefinal
key_type key() const
Definition: Ptr.h:169
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
void setEventContent(const edm::EventSetup &) overridefinal
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::unordered_map< std::string, ValMapIntToken > ValueMaps
edm::EDGetTokenT< edm::ValueMap< int > > ValMapIntToken
#define constexpr
std::unordered_map< std::string, edm::InputTag > ValueMapsTags
U second(std::pair< T, U > const &p)
bool hasUserInt(const std::string &key) const
Return true if there is a user-defined int with a given name.
Definition: PATObject.h:339
const std::string & name() const
std::unordered_map< unsigned, edm::Ptr< reco::GsfElectron > > eles_by_oop
void assignValue(const T &ptr, const U &tok, const V &map, float &value)
edm::EDGetTokenT< edm::View< pat::Electron > > tok_electron_src
std::unordered_map< unsigned, edm::Ptr< reco::Photon > > phos_by_oop
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< int > > > ele_vmaps
void addUserInt(const std::string &label, int32_t data)
Set user-defined int.
Definition: PATObject.h:784
std::vector< std::string > getParameterNames() const
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Definition: PATObject.h:484
void setConsumes(edm::ConsumesCollector &) overridefinal
tuple conf
Definition: dbtoconf.py:185
void get_product(const edm::Event &evt, const edm::EDGetTokenT< edm::ValueMap< float > > &tok, std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > &map)
Analysis-level electron class.
Definition: Electron.h:52
string const
Definition: compareJSON.py:14
#define private
Definition: FWFileEntry.h:17
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< int > > > pho_vmaps
bool isUninitialized() const
Definition: EDGetToken.h:71
#define DEFINE_EDM_PLUGIN(factory, type, name)
void make_consumes(T &tag, U &tok, V &sume)
long double T
EGExtraInfoModifierFromIntValueMaps(const edm::ParameterSet &conf)