Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef CommonTools_UtilAlgos_ProductFromFwdPtrProducer_h
0002 #define CommonTools_UtilAlgos_ProductFromFwdPtrProducer_h
0003 
0004 /**
0005   \class    edm::ProductFromFwdPtrProducer ProductFromFwdPtrProducer.h "CommonTools/UtilAlgos/interface/ProductFromFwdPtrProducer.h"
0006   \brief    Produces a list of objects "by value" that correspond to the FwdPtr's from an input collection.
0007 
0008 
0009   \author   Salvatore Rappoccio
0010 */
0011 
0012 #include "FWCore/Framework/interface/global/EDProducer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "DataFormats/Common/interface/View.h"
0017 #include "DataFormats/Common/interface/FwdPtr.h"
0018 #include "CommonTools/UtilAlgos/interface/FwdPtrConversionFactory.h"
0019 #include <vector>
0020 
0021 namespace edm {
0022 
0023   template <class T, class H = ProductFromFwdPtrFactory<T> >
0024   class ProductFromFwdPtrProducer : public edm::global::EDProducer<> {
0025   public:
0026     explicit ProductFromFwdPtrProducer(edm::ParameterSet const& params)
0027         : srcToken_(consumes<std::vector<edm::FwdPtr<T> > >(params.getParameter<edm::InputTag>("src"))) {
0028       produces<std::vector<T> >();
0029     }
0030 
0031     ~ProductFromFwdPtrProducer() override {}
0032 
0033     void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
0034       edm::Handle<std::vector<edm::FwdPtr<T> > > hSrc;
0035       iEvent.getByToken(srcToken_, hSrc);
0036 
0037       std::unique_ptr<std::vector<T> > pOutput(new std::vector<T>);
0038 
0039       for (typename std::vector<edm::FwdPtr<T> >::const_iterator ibegin = hSrc->begin(), iend = hSrc->end(), i = ibegin;
0040            i != iend;
0041            ++i) {
0042         H factory;
0043         T t = factory(*i);
0044         pOutput->push_back(t);
0045       }
0046 
0047       iEvent.put(std::move(pOutput));
0048     }
0049 
0050   protected:
0051     const edm::EDGetTokenT<std::vector<edm::FwdPtr<T> > > srcToken_;
0052   };
0053 }  // namespace edm
0054 
0055 #endif