Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:11

0001 #ifndef UtilAlgos_Merger_h
0002 #define UtilAlgos_Merger_h
0003 /** \class Merger
0004  *
0005  * Merges an arbitrary number of collections
0006  * into a single collection.
0007  *
0008  * Template parameters:
0009  * - C : collection type
0010  * - P : policy class that specifies how objects
0011  *       in the collection are are cloned
0012  *
0013  * \author Luca Lista, INFN
0014  *
0015  * \version $Revision: 1.2 $
0016  *
0017  * $Id: Merger.h,v 1.2 2010/02/20 20:55:21 wmtan Exp $
0018  *
0019  */
0020 #include "FWCore/Framework/interface/global/EDProducer.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/Utilities/interface/transform.h"
0024 #include "FWCore/Utilities/interface/InputTag.h"
0025 #include "DataFormats/Common/interface/CloneTrait.h"
0026 #include <vector>
0027 
0028 template <typename InputCollection,
0029           typename OutputCollection = InputCollection,
0030           typename P = typename edm::clonehelper::CloneTrait<InputCollection>::type>
0031 class Merger : public edm::global::EDProducer<> {
0032 public:
0033   /// constructor from parameter set
0034   explicit Merger(const edm::ParameterSet&);
0035   /// destructor
0036   ~Merger() override;
0037 
0038 private:
0039   /// process an event
0040   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0041   /// vector of strings
0042   typedef std::vector<edm::EDGetTokenT<InputCollection> > vtoken;
0043   /// labels of the collections to be merged
0044   vtoken srcToken_;
0045 };
0046 
0047 template <typename InputCollection, typename OutputCollection, typename P>
0048 Merger<InputCollection, OutputCollection, P>::Merger(const edm::ParameterSet& par)
0049     : srcToken_(edm::vector_transform(par.template getParameter<std::vector<edm::InputTag> >("src"),
0050                                       [this](edm::InputTag const& tag) { return consumes<InputCollection>(tag); })) {
0051   produces<OutputCollection>();
0052 }
0053 
0054 template <typename InputCollection, typename OutputCollection, typename P>
0055 Merger<InputCollection, OutputCollection, P>::~Merger() {}
0056 
0057 template <typename InputCollection, typename OutputCollection, typename P>
0058 void Merger<InputCollection, OutputCollection, P>::produce(edm::StreamID,
0059                                                            edm::Event& evt,
0060                                                            const edm::EventSetup&) const {
0061   std::unique_ptr<OutputCollection> coll(new OutputCollection);
0062   for (typename vtoken::const_iterator s = srcToken_.begin(); s != srcToken_.end(); ++s) {
0063     edm::Handle<InputCollection> h;
0064     evt.getByToken(*s, h);
0065     for (typename InputCollection::const_iterator c = h->begin(); c != h->end(); ++c) {
0066       coll->push_back(P::clone(*c));
0067     }
0068   }
0069   evt.put(std::move(coll));
0070 }
0071 
0072 #endif