File indexing completed on 2024-04-06 12:27:15
0001 #ifndef CollectionCombiner_H
0002 #define CollectionCombiner_H
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <memory>
0016
0017
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/global/EDProducer.h"
0020
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/MakerMacros.h"
0023
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025
0026 template <typename Collection>
0027 class CollectionCombiner : public edm::global::EDProducer<> {
0028 public:
0029 explicit CollectionCombiner(const edm::ParameterSet&);
0030 ~CollectionCombiner() override;
0031
0032 private:
0033 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0034
0035
0036 std::vector<edm::InputTag> labels;
0037 std::vector<edm::EDGetTokenT<Collection> > collectionTokens;
0038 };
0039
0040 template <typename Collection>
0041 CollectionCombiner<Collection>::CollectionCombiner(const edm::ParameterSet& iConfig) {
0042 labels = iConfig.getParameter<std::vector<edm::InputTag> >("labels");
0043 produces<Collection>();
0044 for (unsigned int i = 0; i < labels.size(); ++i)
0045 collectionTokens.push_back(consumes<Collection>(labels.at(i)));
0046 }
0047 template <typename Collection>
0048 CollectionCombiner<Collection>::~CollectionCombiner() {}
0049
0050 template <typename Collection>
0051 void CollectionCombiner<Collection>::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& es) const {
0052 unsigned int i = 0, i_max = labels.size();
0053 edm::Handle<Collection> handle;
0054 auto merged = std::make_unique<Collection>();
0055 for (; i != i_max; ++i) {
0056 iEvent.getByToken(collectionTokens[i], handle);
0057 merged->insert(merged->end(), handle->begin(), handle->end());
0058 }
0059 iEvent.put(std::move(merged));
0060 }
0061
0062 #endif