1 #ifndef RecoEgamma_EgammaTools_EGExtraInfoModifierFromValueMaps_h 2 #define RecoEgamma_EgammaTools_EGExtraInfoModifierFromValueMaps_h 13 #include <unordered_map> 54 template<
typename OutputType>
61 template<
typename ObjType,
typename MapType>
63 addValueToObject(ObjType&
obj,
67 bool overrideExistingValues);
69 template<
typename ObjType,
typename MapType>
75 bool overrideExistingValues){
76 for(
auto itr = vmaps_token.begin(); itr != vmaps_token.end(); ++itr ) {
77 addValueToObject(obj,ptr,vmaps,*itr,overrideExistingValues);
83 template<
typename MapType,
typename OutputType=MapType>
87 using ValueMaps = std::unordered_map<std::string,ValMapToken>;
107 void setConsumes(
edm::ConsumesCollector&) final;
110 void modifyObject(
pat::
Photon&) const final;
116 std::unordered_map<
unsigned,
edm::Ptr<
reco::GsfElectron> > eles_by_oop;
120 mutable
unsigned ele_idx,pho_idx;
121 bool overrideExistingValues_;
125 template<typename MapType,typename OutputType>
131 overrideExistingValues_ = conf.exists(
"overrideExistingValues") ? conf.getParameter<
bool>(
"overrideExistingValues") :
false;
132 if( conf.exists(
"electron_config") ) {
144 if( conf.exists(
"photon_config") ) {
155 ele_idx = pho_idx = 0;
160 inline void get_product(
const edm::Event& evt,
167 template<
typename MapType,
typename OutputType>
175 ele_idx = pho_idx = 0;
177 if( !e_conf.tok_electron_src.isUninitialized() ) {
181 for(
unsigned i = 0;
i < eles->size(); ++
i ) {
183 eles_by_oop[
i] = ptr;
187 for(
auto itr = e_conf.tok_valuemaps.begin(); itr != e_conf.tok_valuemaps.end(); ++itr ) {
188 get_product(evt,itr->second,ele_vmaps);
191 if( !ph_conf.tok_photon_src.isUninitialized() ) {
195 for(
unsigned i = 0;
i < phos->size(); ++
i ) {
197 phos_by_oop[
i] = ptr;
201 for(
auto itr = ph_conf.tok_valuemaps.begin(); itr != ph_conf.tok_valuemaps.end(); ++itr ) {
202 get_product(evt,itr->second,pho_vmaps);
207 template<
typename MapType,
typename OutputType>
216 {
if( !(empty_tag == tag) ) token = cc.
consumes<
T>(
tag); }
219 template<
typename MapType,
typename OutputType>
225 for(
auto itr = e_conf.valuemaps.begin(); itr != e_conf.valuemaps.end(); ++itr ) {
226 make_consumes(itr->second,e_conf.tok_valuemaps[itr->first],sumes);
232 for(
auto itr = ph_conf.valuemaps.begin(); itr != ph_conf.valuemaps.end(); ++itr ) {
233 make_consumes(itr->second,ph_conf.tok_valuemaps[itr->first],sumes);
238 template<
typename T,
typename U,
typename V,
typename MapType >
239 inline void assignValue(
const T& ptr,
const U& tok,
const V&
map, MapType&
value) {
240 if( !tok.isUninitialized() ) value = map.find(tok.index())->
second->get(ptr.id(),ptr.key());
244 template<
typename MapType,
typename OutputType>
251 if( !e_conf.tok_electron_src.isUninitialized() ) {
252 auto key = eles_by_oop.find(ele_idx);
253 if(
key != eles_by_oop.end() ) {
258 <<
" not found in cache!";
263 ele_vmaps,overrideExistingValues_);
268 template<
typename MapType,
typename OutputType>
275 if( !ph_conf.tok_photon_src.isUninitialized() ) {
276 auto key = phos_by_oop.find(pho_idx);
277 if(
key != phos_by_oop.end() ) {
281 <<
"Original object pointer with key = " << pho.
originalObjectRef().
key() <<
" not found in cache!";
286 pho_vmaps,overrideExistingValues_);
292 template<
typename OutputType>
293 template<
typename ObjType,
typename MapType>
299 bool overrideExistingValues)
302 assignValue(ptr,val_map.second,vmaps,
value);
303 if( overrideExistingValues || !obj.hasUserData(val_map.first) ) {
304 obj.addUserData(val_map.first,
value,
true);
307 <<
"Trying to add new UserData = " << val_map.first
308 <<
" failed because it already exists and you didnt specify to override it (set in the config overrideExistingValues=cms.bool(True) )";
313 template<
typename ObjType,
typename MapType>
319 bool overrideExistingValues)
322 assignValue(ptr,val_map.second,vmaps,value);
323 if( overrideExistingValues || !obj.hasUserFloat(val_map.first) ) {
324 obj.addUserFloat(val_map.first,value,
true);
327 <<
"Trying to add new UserFloat = " << val_map.first
328 <<
" failed because it already exists and you didnt specify to override it (set in the config overrideExistingValues=cms.bool(True) )";
333 template<
typename ObjType,
typename MapType>
339 bool overrideExistingValues)
342 assignValue(ptr,val_map.second,vmaps,value);
343 if( overrideExistingValues || !obj.hasUserInt(val_map.first) ) {
344 obj.addUserInt(val_map.first,value,
true);
347 <<
"Trying to add new UserInt = " << val_map.first
348 <<
" failed because it already exists and you didnt specify to override it (set in the config overrideExistingValues=cms.bool(True) )";
359 bool overrideExistingValues)
361 std::vector<std::pair<std::string,float >>
ids;
362 for(
auto itr = vmaps_token.begin(); itr != vmaps_token.end(); ++itr ) {
364 assignValue(ptr,itr->second,vmaps,idVal);
365 ids.push_back({itr->first,idVal});
367 std::sort(ids.begin(),ids.end(),[](
auto& lhs,
auto& rhs){
return lhs.first<rhs.first;});
378 bool overrideExistingValues)
381 std::vector<std::pair<std::string,bool> >
ids;
382 for(
auto itr = vmaps_token.begin(); itr != vmaps_token.end(); ++itr ) {
384 assignValue(ptr,itr->second,vmaps,idVal);
385 ids.push_back({itr->first,idVal});
387 std::sort(ids.begin(),ids.end(),[](
auto& lhs,
auto& rhs){
return lhs.first<rhs.first;});
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
Analysis-level Photon class.
bool existsAs(std::string const ¶meterName, bool trackiness=true) const
checks if a parameter exists as a given type
void setElectronIDs(const std::vector< IdPair > &ids)
Store multiple electron ID values, discarding existing ones. The first one in the list becomes the 'd...
bool getByToken(EDGetToken token, Handle< PROD > &result) const
bool exists(std::string const ¶meterName) const
checks if a parameter exists
U second(std::pair< T, U > const &p)
static void addValueToObject(ObjType &obj, const edm::Ptr< reco::Candidate > &ptr, const std::unordered_map< unsigned, edm::Handle< edm::ValueMap< MapType > > > &vmaps, const std::pair< const std::string, edm::EDGetTokenT< edm::ValueMap< MapType > > > &val_map, bool overrideExistingValues)
void setPhotonIDs(const std::vector< IdPair > &ids)
static void addValuesToObject(ObjType &obj, const edm::Ptr< reco::Candidate > &ptr, const std::unordered_map< std::string, edm::EDGetTokenT< edm::ValueMap< MapType > > > &vmaps_token, const std::unordered_map< unsigned, edm::Handle< edm::ValueMap< MapType > > > &vmaps, bool overrideExistingValues)
def template(fileName, svg, replaceme="REPLACEME")
std::vector< std::string > getParameterNames() const
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Analysis-level electron class.