File indexing completed on 2024-04-06 12:23:47
0001
0002
0003
0004 #ifndef PhysicsTools_PatAlgos_PATUserDataMerger_h
0005 #define PhysicsTools_PatAlgos_PATUserDataMerger_h
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include "FWCore/Framework/interface/Event.h"
0024 #include "FWCore/Framework/interface/ConsumesCollector.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/Utilities/interface/InputTag.h"
0027 #include "FWCore/Utilities/interface/transform.h"
0028 #include "DataFormats/Common/interface/View.h"
0029 #include "DataFormats/Common/interface/Ptr.h"
0030 #include "DataFormats/Common/interface/Association.h"
0031 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0032 #include "DataFormats/Candidate/interface/Candidate.h"
0033 #include "DataFormats/PatCandidates/interface/PATObject.h"
0034
0035 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0036 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0037
0038 #include <iostream>
0039
0040 namespace pat {
0041 namespace helper {
0042 struct AddUserInt {
0043 typedef int value_type;
0044 typedef edm::ValueMap<value_type> product_type;
0045 template <typename ObjectType>
0046 void addData(ObjectType &obj, const std::string &key, const value_type &val) {
0047 obj.addUserInt(key, val);
0048 }
0049 };
0050 struct AddUserFloat {
0051 typedef float value_type;
0052 typedef edm::ValueMap<value_type> product_type;
0053 template <typename ObjectType>
0054 void addData(ObjectType &obj, const std::string &key, const value_type &val) {
0055 obj.addUserFloat(key, val);
0056 }
0057 };
0058 struct AddUserPtr {
0059 typedef edm::Ptr<UserData> value_type;
0060 typedef edm::ValueMap<value_type> product_type;
0061 template <typename ObjectType>
0062 void addData(ObjectType &obj, const std::string &key, const value_type &val) {
0063 obj.addUserDataFromPtr(key, val);
0064 }
0065 };
0066 struct AddUserCand {
0067 typedef reco::CandidatePtr value_type;
0068 typedef edm::ValueMap<value_type> product_type;
0069 template <typename ObjectType>
0070 void addData(ObjectType &obj, const std::string &key, const value_type &val) {
0071 obj.addUserCand(key, val);
0072 }
0073 };
0074
0075 }
0076
0077 template <typename ObjectType, typename Operation>
0078 class PATUserDataMerger {
0079 public:
0080 PATUserDataMerger() {}
0081 PATUserDataMerger(const edm::ParameterSet &iConfig, edm::ConsumesCollector &iC);
0082 ~PATUserDataMerger() {}
0083
0084 static void fillDescription(edm::ParameterSetDescription &iDesc);
0085
0086
0087 void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup);
0088
0089 private:
0090
0091 std::vector<edm::InputTag> userDataSrc_;
0092 std::vector<edm::EDGetTokenT<typename Operation::product_type>> userDataSrcTokens_;
0093 std::vector<std::string> labelPostfixesToStrip_, labels_;
0094 Operation loader_;
0095 };
0096
0097 }
0098
0099
0100 template <typename ObjectType, typename Operation>
0101 pat::PATUserDataMerger<ObjectType, Operation>::PATUserDataMerger(const edm::ParameterSet &iConfig,
0102 edm::ConsumesCollector &iC)
0103 : userDataSrc_(iConfig.getParameter<std::vector<edm::InputTag>>("src")),
0104 labelPostfixesToStrip_(iConfig.existsAs<std::vector<std::string>>("labelPostfixesToStrip")
0105 ? iConfig.getParameter<std::vector<std::string>>("labelPostfixesToStrip")
0106 : std::vector<std::string>()) {
0107 for (std::vector<edm::InputTag>::const_iterator input_it = userDataSrc_.begin(); input_it != userDataSrc_.end();
0108 ++input_it) {
0109 userDataSrcTokens_.push_back(iC.consumes<typename Operation::product_type>(*input_it));
0110 }
0111 for (edm::InputTag tag : userDataSrc_) {
0112 for (const std::string &stripme : labelPostfixesToStrip_) {
0113 auto match = tag.label().rfind(stripme);
0114 if (match == (tag.label().length() - stripme.length())) {
0115 tag = edm::InputTag(tag.label().substr(0, match), tag.instance(), tag.process());
0116 }
0117 }
0118 labels_.push_back(tag.encode());
0119 }
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 template <class ObjectType, typename Operation>
0139 void pat::PATUserDataMerger<ObjectType, Operation>::add(ObjectType &patObject,
0140 edm::Event const &iEvent,
0141 const edm::EventSetup &iSetup) {
0142 typename std::vector<edm::EDGetTokenT<typename Operation::product_type>>::const_iterator
0143 token_begin = userDataSrcTokens_.begin(),
0144 token_it = userDataSrcTokens_.begin(), token_end = userDataSrcTokens_.end();
0145
0146 for (; token_it != token_end; ++token_it) {
0147 const std::string &encoded = (labels_.at(token_it - token_begin));
0148
0149
0150
0151
0152 edm::Handle<typename Operation::product_type> userData;
0153
0154
0155 if (encoded.empty())
0156 continue;
0157 iEvent.getByToken(*token_it, userData);
0158
0159 edm::Ptr<reco::Candidate> recoObject = patObject.originalObjectRef();
0160 loader_.addData(patObject, encoded, (*userData)[recoObject]);
0161 }
0162 }
0163
0164 template <class ObjectType, typename Operation>
0165 void pat::PATUserDataMerger<ObjectType, Operation>::fillDescription(edm::ParameterSetDescription &iDesc) {
0166 iDesc.add<std::vector<edm::InputTag>>("src");
0167 iDesc.addOptional<std::vector<std::string>>("labelPostfixesToStrip", std::vector<std::string>());
0168 }
0169
0170 #endif