Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:16

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/ConfigurationDescriptions.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/Utilities/interface/transform.h"
0026 #include "FWCore/Utilities/interface/InputTag.h"
0027 #include "DataFormats/Common/interface/CloneTrait.h"
0028 #include <vector>
0029 
0030 template <typename InputCollection,
0031           typename OutputCollection = InputCollection,
0032           typename P = typename edm::clonehelper::CloneTrait<InputCollection>::type>
0033 class Merger : public edm::global::EDProducer<> {
0034 public:
0035   /// constructor from parameter set
0036   explicit Merger(const edm::ParameterSet&);
0037   /// destructor
0038   ~Merger() override;
0039   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0040 
0041 private:
0042   /// process an event
0043   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0044   /// vector of strings
0045   typedef std::vector<edm::EDGetTokenT<InputCollection>> vtoken;
0046   /// labels of the collections to be merged
0047   vtoken srcToken_;
0048 };
0049 
0050 template <typename InputCollection, typename OutputCollection, typename P>
0051 Merger<InputCollection, OutputCollection, P>::Merger(const edm::ParameterSet& par)
0052     : srcToken_(edm::vector_transform(par.template getParameter<std::vector<edm::InputTag>>("src"),
0053                                       [this](edm::InputTag const& tag) { return consumes<InputCollection>(tag); })) {
0054   produces<OutputCollection>();
0055 }
0056 
0057 template <typename InputCollection, typename OutputCollection, typename P>
0058 Merger<InputCollection, OutputCollection, P>::~Merger() {}
0059 
0060 template <typename InputCollection, typename OutputCollection, typename P>
0061 void Merger<InputCollection, OutputCollection, P>::produce(edm::StreamID,
0062                                                            edm::Event& evt,
0063                                                            const edm::EventSetup&) const {
0064   std::unique_ptr<OutputCollection> coll(new OutputCollection);
0065   for (typename vtoken::const_iterator s = srcToken_.begin(); s != srcToken_.end(); ++s) {
0066     edm::Handle<InputCollection> h;
0067     evt.getByToken(*s, h);
0068     for (typename InputCollection::const_iterator c = h->begin(); c != h->end(); ++c) {
0069       coll->push_back(P::clone(*c));
0070     }
0071   }
0072   evt.put(std::move(coll));
0073 }
0074 
0075 template <typename InputCollection, typename OutputCollection, typename P>
0076 void Merger<InputCollection, OutputCollection, P>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0077   edm::ParameterSetDescription desc;
0078   desc.add<std::vector<edm::InputTag>>("src",
0079                                        {
0080                                            edm::InputTag("collection1"),
0081                                            edm::InputTag("collection2"),
0082                                        });
0083   descriptions.addWithDefaultLabel(desc);
0084 }
0085 
0086 #endif