File indexing completed on 2023-10-25 09:36:13
0001 #ifndef CommonTools_UtilAlgos_AssociationVector2ValueMap_h
0002 #define CommonTools_UtilAlgos_AssociationVector2ValueMap_h
0003
0004
0005
0006
0007
0008
0009
0010 #include "DataFormats/Common/interface/AssociationVector.h"
0011 #include "DataFormats/Common/interface/ValueMap.h"
0012 #include "FWCore/Framework/interface/global/EDProducer.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014
0015 template <typename KeyRefProd, typename CVal>
0016 class AssociationVector2ValueMap : public edm::global::EDProducer<> {
0017 public:
0018 AssociationVector2ValueMap(const edm::ParameterSet&);
0019
0020 private:
0021 typedef edm::AssociationVector<KeyRefProd, CVal> av_t;
0022 typedef typename CVal::value_type value_t;
0023 typedef edm::ValueMap<value_t> vm_t;
0024 typedef typename av_t::CKey collection_t;
0025 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0026 edm::EDGetTokenT<av_t> av_;
0027 };
0028
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "DataFormats/Common/interface/Handle.h"
0032 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0033 #include "DataFormats/Common/interface/CloneTrait.h"
0034
0035 template <typename KeyRefProd, typename CVal>
0036 AssociationVector2ValueMap<KeyRefProd, CVal>::AssociationVector2ValueMap(const edm::ParameterSet& cfg)
0037 : av_(consumes<av_t>(cfg.template getParameter<edm::InputTag>("src"))) {
0038 produces<vm_t>();
0039 }
0040
0041 template <typename KeyRefProd, typename CVal>
0042 void AssociationVector2ValueMap<KeyRefProd, CVal>::produce(edm::StreamID,
0043 edm::Event& evt,
0044 const edm::EventSetup&) const {
0045 using namespace edm;
0046 using namespace std;
0047 Handle<av_t> av;
0048 evt.getByToken(av_, av);
0049
0050 unique_ptr<vm_t> vm(new vm_t);
0051 typename vm_t::Filler filler(*vm);
0052 filler.fill();
0053 size_t size = av->size();
0054 vector<value_t> values;
0055 values.reserve(size);
0056 for (typename av_t::const_iterator i = av->begin(); i != av->end(); ++i) {
0057 values.push_back(i->second);
0058 }
0059 filler.insert(av->keyProduct(), values.begin(), values.end());
0060 evt.put(std::move(vm));
0061 }
0062
0063 #endif