Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:22

0001 
0002 /*----------------------------------------------------------------------
0003 
0004 Toy EDProducers of Ref/Ptr for testing purposes only.
0005 
0006 ----------------------------------------------------------------------*/
0007 
0008 #include "DataFormats/Common/interface/Handle.h"
0009 #include "DataFormats/Common/interface/Ref.h"
0010 #include "DataFormats/Common/interface/View.h"
0011 #include "DataFormats/Common/interface/PtrVector.h"
0012 #include "DataFormats/Common/interface/RefVector.h"
0013 #include "DataFormats/Common/interface/RefToBaseVector.h"
0014 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0015 
0016 #include "FWCore/Framework/interface/global/EDProducer.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/MakerMacros.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 #include "FWCore/Utilities/interface/EDGetToken.h"
0021 
0022 #include <cassert>
0023 #include <string>
0024 #include <vector>
0025 
0026 namespace edmtest {
0027 
0028   //--------------------------------------------------------------------
0029   //
0030   // Toy Ref or Ptr producers
0031   //
0032   //--------------------------------------------------------------------
0033 
0034   //--------------------------------------------------------------------
0035   //
0036   // Produces an edm::RefVector<std::vector<int> > instance.
0037   // This requires that an instance of IntVectorProducer be run *before*
0038   // this producer.
0039   class IntVecRefVectorProducer : public edm::global::EDProducer<> {
0040     typedef edm::RefVector<std::vector<int>> product_type;
0041 
0042   public:
0043     explicit IntVecRefVectorProducer(edm::ParameterSet const& p)
0044         : target_{consumes(p.getParameter<edm::InputTag>("target"))}, select_(p.getParameter<int>("select")) {
0045       produces<product_type>();
0046     }
0047     virtual void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0048 
0049     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0050       edm::ParameterSetDescription desc;
0051       desc.add<int>("select", 0);
0052       desc.add<edm::InputTag>("target");
0053       descriptions.addDefault(desc);
0054     }
0055 
0056   private:
0057     const edm::EDGetTokenT<std::vector<int>> target_;
0058     int select_;
0059   };
0060 
0061   void IntVecRefVectorProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0062     // EventSetup is not used.
0063     // Get our input:
0064     edm::Handle<std::vector<int>> input = e.getHandle(target_);
0065     assert(input.isValid());
0066 
0067     auto prod = std::make_unique<product_type>();
0068 
0069     typedef product_type::value_type ref;
0070     for (size_t i = 0, sz = input->size(); i != sz; ++i) {
0071       if (select_ != 0 && (i % select_) == 0)
0072         continue;
0073       prod->push_back(ref(input, i));
0074     }
0075 
0076     e.put(std::move(prod));
0077   }
0078 
0079   //--------------------------------------------------------------------
0080   //
0081   // Produces an edm::RefToBaseVector<int> instance.
0082   // This requires that an instance of IntVectorProducer be run *before*
0083   // this producer. The input collection is read as an edm::View<int>
0084   class IntVecRefToBaseVectorProducer : public edm::global::EDProducer<> {
0085     typedef edm::RefToBaseVector<int> product_type;
0086 
0087   public:
0088     explicit IntVecRefToBaseVectorProducer(edm::ParameterSet const& p)
0089         : target_{consumes(p.getParameter<edm::InputTag>("target"))} {
0090       produces<product_type>();
0091     }
0092     virtual void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0093 
0094   private:
0095     const edm::EDGetTokenT<edm::View<int>> target_;
0096   };
0097 
0098   void IntVecRefToBaseVectorProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0099     // EventSetup is not used.
0100     // Get our input:
0101     edm::Handle<edm::View<int>> input = e.getHandle(target_);
0102     assert(input.isValid());
0103 
0104     edm::RefToBaseVector<int> refVector;
0105     for (size_t i = 0; i < input->size(); ++i) {
0106       refVector.push_back(input->refAt(i));
0107     }
0108 
0109     e.put(std::make_unique<product_type>(refVector));
0110   }
0111 
0112   //--------------------------------------------------------------------
0113   //
0114   // Produces an edm::PtrVector<int> instance.
0115   // This requires that an instance of IntVectorProducer be run *before*
0116   // this producer. The input collection is read as an edm::View<int>
0117   class IntVecPtrVectorProducer : public edm::global::EDProducer<> {
0118     typedef edm::PtrVector<int> product_type;
0119 
0120   public:
0121     explicit IntVecPtrVectorProducer(edm::ParameterSet const& p)
0122         : target_{consumes(p.getParameter<edm::InputTag>("target"))} {
0123       produces<product_type>();
0124     }
0125     virtual void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0126 
0127   private:
0128     const edm::EDGetTokenT<edm::View<int>> target_;
0129   };
0130 
0131   void IntVecPtrVectorProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0132     // EventSetup is not used.
0133     // Get our input:
0134     edm::Handle<edm::View<int>> input = e.getHandle(target_);
0135     assert(input.isValid());
0136 
0137     auto prod = std::make_unique<product_type>();
0138 
0139     typedef product_type::value_type ref;
0140     for (size_t i = 0, sz = input->size(); i != sz; ++i)
0141       prod->push_back(ref(input, i));
0142 
0143     e.put(std::move(prod));
0144   }
0145 
0146   //--------------------------------------------------------------------
0147   //
0148   // Produces an std::vector<edm::Ptr<int>> instance.
0149   // This requires that an instance of IntVectorProducer be run *before*
0150   // this producer. The input collection is read as an edm::View<int>
0151   class IntVecStdVectorPtrProducer : public edm::global::EDProducer<> {
0152     typedef std::vector<edm::Ptr<int>> product_type;
0153 
0154   public:
0155     explicit IntVecStdVectorPtrProducer(edm::ParameterSet const& p)
0156         : target_(consumes(p.getParameter<edm::InputTag>("target"))) {
0157       produces<product_type>();
0158     }
0159     virtual void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0160 
0161   private:
0162     const edm::EDGetTokenT<edm::View<int>> target_;
0163   };
0164 
0165   void IntVecStdVectorPtrProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0166     // EventSetup is not used.
0167     // Get our input:
0168     edm::Handle<edm::View<int>> input = e.getHandle(target_);
0169     assert(input.isValid());
0170 
0171     auto prod = std::make_unique<product_type>();
0172 
0173     typedef product_type::value_type ref;
0174     for (size_t i = 0, sz = input->size(); i != sz; ++i)
0175       prod->emplace_back(input, i);
0176 
0177     e.put(std::move(prod));
0178   }
0179 
0180 }  // namespace edmtest
0181 
0182 using edmtest::IntVecPtrVectorProducer;
0183 using edmtest::IntVecRefToBaseVectorProducer;
0184 using edmtest::IntVecRefVectorProducer;
0185 using edmtest::IntVecStdVectorPtrProducer;
0186 DEFINE_FWK_MODULE(IntVecRefVectorProducer);
0187 DEFINE_FWK_MODULE(IntVecRefToBaseVectorProducer);
0188 DEFINE_FWK_MODULE(IntVecPtrVectorProducer);
0189 DEFINE_FWK_MODULE(IntVecStdVectorPtrProducer);