test
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) :
61  ModifyObjectValueBase(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 namespace {
91  inline void get_product(const edm::Event& evt,
93  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<int> > >& map) {
94  evt.getByToken(tok,map[tok.index()]);
95  }
96 }
97 
99 setEvent(const edm::Event& evt) {
100  eles_by_oop.clear();
101  phos_by_oop.clear();
102  ele_vmaps.clear();
103  pho_vmaps.clear();
104 
105  ele_idx = pho_idx = 0;
106 
110 
111  for( unsigned i = 0; i < eles->size(); ++i ) {
112  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
113  eles_by_oop[i] = ptr;
114  }
115  }
116 
117  for( auto itr = e_conf.tok_valuemaps.begin(); itr != e_conf.tok_valuemaps.end(); ++itr ) {
118  get_product(evt,itr->second,ele_vmaps);
119  }
120 
124 
125  for( unsigned i = 0; i < phos->size(); ++i ) {
126  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
127  phos_by_oop[i] = ptr;
128  }
129  }
130 
131  for( auto itr = ph_conf.tok_valuemaps.begin(); itr != ph_conf.tok_valuemaps.end(); ++itr ) {
132  get_product(evt,itr->second,pho_vmaps);
133  }
134 }
135 
138 }
139 
140 namespace {
141  template<typename T, typename U, typename V>
142  inline void make_consumes(T& tag,U& tok,V& sume) { if( !(empty_tag == tag) ) tok = sume.template consumes<edm::ValueMap<int> >(tag); }
143 }
144 
147  //setup electrons
149 
150  for( auto itr = e_conf.valuemaps.begin(); itr != e_conf.valuemaps.end(); ++itr ) {
151  make_consumes(itr->second,e_conf.tok_valuemaps[itr->first],sumes);
152  }
153 
154  // setup photons
156 
157  for( auto itr = ph_conf.valuemaps.begin(); itr != ph_conf.valuemaps.end(); ++itr ) {
158  make_consumes(itr->second,ph_conf.tok_valuemaps[itr->first],sumes);
159  }
160 }
161 
162 namespace {
163  template<typename T, typename U, typename V>
164  inline void assignValue(const T& ptr, const U& tok, const V& map, int& value) {
165  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
166  }
167 }
168 
171  // we encounter two cases here, either we are running AOD -> MINIAOD
172  // and the value maps are to the reducedEG object, can use original object ptr
173  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
176  auto key = eles_by_oop.find(ele_idx);
177  if( key != eles_by_oop.end() ) {
178  ptr = key->second;
179  } else {
180  throw cms::Exception("BadElectronKey")
181  << "Original object pointer with key = " << ele.originalObjectRef().key()
182  << " not found in cache!";
183  }
184  }
185  //now we go through and modify the objects using the valuemaps we read in
186  for( auto itr = e_conf.tok_valuemaps.begin(); itr != e_conf.tok_valuemaps.end(); ++itr ) {
187  int value(0);
188  assignValue(ptr,itr->second,ele_vmaps,value);
189  if( !ele.hasUserInt(itr->first) ) {
190  ele.addUserInt(itr->first,value);
191  } else {
192  throw cms::Exception("ValueNameAlreadyExists")
193  << "Trying to add new UserInt = " << itr->first
194  << " failed because it already exists!";
195  }
196  }
197  ++ele_idx;
198 }
199 
202  // we encounter two cases here, either we are running AOD -> MINIAOD
203  // and the value maps are to the reducedEG object, can use original object ptr
204  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
207  auto key = phos_by_oop.find(pho_idx);
208  if( key != phos_by_oop.end() ) {
209  ptr = key->second;
210  } else {
211  throw cms::Exception("BadPhotonKey")
212  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
213  }
214  }
215  //now we go through and modify the objects using the valuemaps we read in
216  for( auto itr = ph_conf.tok_valuemaps.begin(); itr != ph_conf.tok_valuemaps.end(); ++itr ) {
217  int value(0);
218  assignValue(ptr,itr->second,pho_vmaps,value);
219  if( !pho.hasUserInt(itr->first) ) {
220  pho.addUserInt(itr->first,value);
221  } else {
222  throw cms::Exception("ValueNameAlreadyExists")
223  << "Trying to add new UserInt = " << itr->first
224  << " failed because it already exists!";
225  }
226  }
227  ++pho_idx;
228 }
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
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:186
void modifyObject(pat::Electron &) const overridefinal
key_type key() const
Definition: Ptr.h:186
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
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:351
const std::string & name() const
std::unordered_map< unsigned, edm::Ptr< reco::GsfElectron > > eles_by_oop
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
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:500
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
void setConsumes(edm::ConsumesCollector &) overridefinal
void addUserInt(const std::string &label, int32_t data, const bool overwrite=false)
Set user-defined int.
Definition: PATObject.h:855
Analysis-level electron class.
Definition: Electron.h:52
string const
Definition: compareJSON.py:14
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< int > > > pho_vmaps
bool isUninitialized() const
Definition: EDGetToken.h:73
#define DEFINE_EDM_PLUGIN(factory, type, name)
long double T
EGExtraInfoModifierFromIntValueMaps(const edm::ParameterSet &conf)