File indexing completed on 2023-03-17 11:16:43
0001 #include "PhysicsTools/TagAndProbe/plugins/AnythingToValueMap.h"
0002 #include "DataFormats/Common/interface/AssociationVector.h"
0003 #include "DataFormats/Common/interface/AssociationMap.h"
0004 #include "DataFormats/Common/interface/OneToValue.h"
0005 #include "DataFormats/Common/interface/ValueMap.h"
0006 #include "DataFormats/Candidate/interface/Candidate.h"
0007 #include "FWCore/Framework/interface/ConsumesCollector.h"
0008
0009 namespace pat {
0010 namespace helper {
0011 class AnyNumberAssociationAdaptor {
0012 public:
0013 typedef float value_type;
0014 typedef edm::View<reco::Candidate> Collection;
0015 template <typename T>
0016 struct AssoVec {
0017 typedef typename edm::AssociationVector<reco::CandidateBaseRefProd, typename std::vector<T> > type;
0018 };
0019
0020 AnyNumberAssociationAdaptor(const edm::InputTag &in,
0021 const edm::ParameterSet &iConfig,
0022 edm::ConsumesCollector &&iC)
0023 : type_(Uninitialized),
0024 in_(in),
0025 label_(in.label() + in.instance()),
0026 tokenVMd_(iC.consumes<edm::ValueMap<double> >(in)),
0027 tokenVMf_(iC.consumes<edm::ValueMap<float> >(in)),
0028 tokenVMi_(iC.consumes<edm::ValueMap<int> >(in)),
0029 tokenVMb_(iC.consumes<edm::ValueMap<bool> >(in)),
0030 tokenAVd_(iC.consumes<AssoVec<double>::type>(in)),
0031 tokenAVf_(iC.consumes<AssoVec<float>::type>(in)),
0032 tokenAVi_(iC.consumes<AssoVec<int>::type>(in)) {}
0033
0034 const std::string &label() { return label_; }
0035
0036 bool run(const edm::Event &iEvent, const Collection &coll, std::vector<value_type> &ret) {
0037 switch (type_) {
0038 case Uninitialized:
0039 if (run_<edm::ValueMap<double> >(tokenVMd_, iEvent, coll, ret)) {
0040 type_ = ValueMapDouble;
0041 return true;
0042 }
0043 if (run_<edm::ValueMap<float> >(tokenVMf_, iEvent, coll, ret)) {
0044 type_ = ValueMapFloat;
0045 return true;
0046 }
0047 if (run_<edm::ValueMap<int> >(tokenVMi_, iEvent, coll, ret)) {
0048 type_ = ValueMapInt;
0049 return true;
0050 }
0051 if (run_<edm::ValueMap<bool> >(tokenVMb_, iEvent, coll, ret)) {
0052 type_ = ValueMapBool;
0053 return true;
0054 }
0055 if (run_<AssoVec<double>::type>(tokenAVd_, iEvent, coll, ret)) {
0056 type_ = AssoVecDouble;
0057 return true;
0058 }
0059 if (run_<AssoVec<float>::type>(tokenAVf_, iEvent, coll, ret)) {
0060 type_ = AssoVecFloat;
0061 return true;
0062 }
0063 if (run_<AssoVec<int>::type>(tokenAVi_, iEvent, coll, ret)) {
0064 type_ = AssoVecInt;
0065 return true;
0066 }
0067 type_ = Nothing;
0068 return false;
0069 break;
0070 case ValueMapDouble:
0071 return run_<edm::ValueMap<double> >(tokenVMd_, iEvent, coll, ret);
0072 case ValueMapFloat:
0073 return run_<edm::ValueMap<float> >(tokenVMf_, iEvent, coll, ret);
0074 case ValueMapInt:
0075 return run_<edm::ValueMap<int> >(tokenVMi_, iEvent, coll, ret);
0076 case ValueMapBool:
0077 return run_<edm::ValueMap<bool> >(tokenVMb_, iEvent, coll, ret);
0078 case AssoVecDouble:
0079 return run_<AssoVec<double>::type>(tokenAVd_, iEvent, coll, ret);
0080 case AssoVecFloat:
0081 return run_<AssoVec<float>::type>(tokenAVf_, iEvent, coll, ret);
0082 case AssoVecInt:
0083 return run_<AssoVec<int>::type>(tokenAVi_, iEvent, coll, ret);
0084 case Nothing:
0085 return false;
0086 }
0087 return false;
0088 }
0089
0090 private:
0091 enum Type {
0092 Uninitialized = 0,
0093 ValueMapDouble,
0094 ValueMapFloat,
0095 ValueMapInt,
0096 ValueMapBool,
0097 AssoVecDouble,
0098 AssoVecFloat,
0099 AssoVecInt,
0100 Nothing
0101 };
0102 template <typename T>
0103 bool run_(const edm::EDGetTokenT<T> &token,
0104 const edm::Event &iEvent,
0105 const Collection &coll,
0106 std::vector<value_type> &ret);
0107 Type type_;
0108 edm::InputTag in_;
0109 std::string label_;
0110 edm::EDGetTokenT<edm::ValueMap<double> > tokenVMd_;
0111 edm::EDGetTokenT<edm::ValueMap<float> > tokenVMf_;
0112 edm::EDGetTokenT<edm::ValueMap<int> > tokenVMi_;
0113 edm::EDGetTokenT<edm::ValueMap<bool> > tokenVMb_;
0114 edm::EDGetTokenT<AssoVec<double>::type> tokenAVd_;
0115 edm::EDGetTokenT<AssoVec<float>::type> tokenAVf_;
0116 edm::EDGetTokenT<AssoVec<int>::type> tokenAVi_;
0117 };
0118
0119 template <typename T>
0120 bool AnyNumberAssociationAdaptor::run_(const edm::EDGetTokenT<T> &token,
0121 const edm::Event &iEvent,
0122 const Collection &coll,
0123 std::vector<value_type> &ret) {
0124 edm::Handle<T> handle;
0125 iEvent.getByToken(token, handle);
0126 if (handle.failedToGet())
0127 return false;
0128
0129 for (size_t i = 0, n = coll.size(); i < n; ++i) {
0130 reco::CandidateBaseRef ref = coll.refAt(i);
0131 ret.push_back((*handle)[ref]);
0132 }
0133 return true;
0134 }
0135
0136 typedef ManyThingsToValueMaps<AnyNumberAssociationAdaptor> AnyNumbersToValueMaps;
0137
0138 }
0139 }
0140
0141 #include "FWCore/Framework/interface/MakerMacros.h"
0142 using namespace pat::helper;
0143 DEFINE_FWK_MODULE(AnyNumbersToValueMaps);