Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-08 02:27:18

0001 
0002 /*----------------------------------------------------------------------
0003 
0004 Toy EDProducers of STL containers for testing purposes only.
0005 
0006 ----------------------------------------------------------------------*/
0007 
0008 #include "DataFormats/Common/interface/Handle.h"
0009 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0010 
0011 #include "FWCore/Framework/interface/global/EDProducer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/MakerMacros.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 
0016 #include <cassert>
0017 #include <string>
0018 #include <vector>
0019 
0020 namespace edmtest {
0021 
0022   //--------------------------------------------------------------------
0023   //
0024   // STL container producers
0025   //
0026   //--------------------------------------------------------------------
0027 
0028   //--------------------------------------------------------------------
0029   //
0030   // Produces an std::vector<int> instance.
0031   //
0032   class IntVectorProducer : public edm::global::EDProducer<> {
0033   public:
0034     explicit IntVectorProducer(edm::ParameterSet const& p)
0035         : value_(p.getParameter<int>("ivalue")),
0036           count_(p.getParameter<int>("count")),
0037           delta_(p.getParameter<int>("delta")) {
0038       produces<std::vector<int>>();
0039     }
0040     void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0041 
0042     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0043       edm::ParameterSetDescription desc;
0044       desc.add<int>("ivalue", 0);
0045       desc.add<int>("count", 0);
0046       desc.add<int>("delta", 0);
0047       descriptions.addDefault(desc);
0048     }
0049 
0050   private:
0051     int value_;
0052     size_t count_;
0053     int delta_;
0054   };
0055 
0056   void IntVectorProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0057     // EventSetup is not used.
0058     auto p = std::make_unique<std::vector<int>>(count_, value_);
0059     if (delta_ != 0) {
0060       for (unsigned int i = 0; i < p->size(); ++i) {
0061         p->at(i) = value_ + i * delta_;
0062       }
0063     }
0064     e.put(std::move(p));
0065   }
0066 
0067   //--------------------------------------------------------------------
0068   //
0069   // Produces an std::vector<int> and set<int> instance.
0070   // Used to test ambiguous getByToken calls with View
0071   // arguments.
0072   class IntVectorSetProducer : public edm::global::EDProducer<> {
0073   public:
0074     explicit IntVectorSetProducer(edm::ParameterSet const& p) {
0075       produces<std::vector<int>>();
0076       produces<std::set<int>>();
0077     }
0078     void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0079   };
0080 
0081   void IntVectorSetProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0082     // EventSetup is not used.
0083     e.put(std::make_unique<std::vector<int>>(1, 11));
0084 
0085     e.put(std::make_unique<std::set<int>>());
0086   }
0087 
0088   //--------------------------------------------------------------------
0089   //
0090   // Produces an std::list<int> instance.
0091   //
0092   class IntListProducer : public edm::global::EDProducer<> {
0093   public:
0094     explicit IntListProducer(edm::ParameterSet const& p)
0095         : value_(p.getParameter<int>("ivalue")), count_(p.getParameter<int>("count")) {
0096       produces<std::list<int>>();
0097     }
0098     void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0099 
0100   private:
0101     int value_;
0102     size_t count_;
0103   };
0104 
0105   void IntListProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0106     // EventSetup is not used.
0107     e.put(std::make_unique<std::list<int>>(count_, value_));
0108   }
0109 
0110   //--------------------------------------------------------------------
0111   //
0112   // Produces an std::deque<int> instance.
0113   //
0114   class IntDequeProducer : public edm::global::EDProducer<> {
0115   public:
0116     explicit IntDequeProducer(edm::ParameterSet const& p)
0117         : value_(p.getParameter<int>("ivalue")), count_(p.getParameter<int>("count")) {
0118       produces<std::deque<int>>();
0119     }
0120     void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0121 
0122   private:
0123     int value_;
0124     size_t count_;
0125   };
0126 
0127   void IntDequeProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0128     // EventSetup is not used.
0129     e.put(std::make_unique<std::deque<int>>(count_, value_));
0130   }
0131 
0132   //--------------------------------------------------------------------
0133   //
0134   // Produces an std::set<int> instance.
0135   //
0136   class IntSetProducer : public edm::global::EDProducer<> {
0137   public:
0138     explicit IntSetProducer(edm::ParameterSet const& p)
0139         : start_(p.getParameter<int>("start")), stop_(p.getParameter<int>("stop")) {
0140       produces<std::set<int>>();
0141     }
0142     void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override;
0143 
0144   private:
0145     int start_;
0146     int stop_;
0147   };
0148 
0149   void IntSetProducer::produce(edm::StreamID, edm::Event& e, edm::EventSetup const&) const {
0150     // EventSetup is not used.
0151     auto p = std::make_unique<std::set<int>>();
0152     for (int i = start_; i < stop_; ++i)
0153       p->insert(i);
0154     e.put(std::move(p));
0155   }
0156 
0157   //--------------------------------------------------------------------
0158   //
0159   // Produces std::vector<std::unique_ptr<int>> and std::vector<std::unique_ptr<IntProduct>> instances.
0160   //
0161   class UniqPtrIntVectorProducer : public edm::global::EDProducer<> {
0162   public:
0163     explicit UniqPtrIntVectorProducer(edm::ParameterSet const& p)
0164         : value_(p.getParameter<int>("ivalue")),
0165           count_(p.getParameter<int>("count")),
0166           delta_(p.getParameter<int>("delta")),
0167           intToken_(produces<std::vector<std::unique_ptr<int>>>()),
0168           intProductToken_(produces<std::vector<std::unique_ptr<IntProduct>>>()) {}
0169 
0170     void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override {
0171       std::vector<std::unique_ptr<int>> p1(count_);
0172       std::vector<std::unique_ptr<IntProduct>> p2(count_);
0173       for (unsigned int i = 0; i < count_; ++i) {
0174         const int v = value_ + i * delta_;
0175         p1[i] = std::make_unique<int>(v);
0176         p2[i] = std::make_unique<IntProduct>(v);
0177       }
0178       e.emplace(intToken_, std::move(p1));
0179       e.emplace(intProductToken_, std::move(p2));
0180     }
0181 
0182     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0183       edm::ParameterSetDescription desc;
0184       desc.add<int>("ivalue", 0);
0185       desc.add<int>("count", 0);
0186       desc.add<int>("delta", 0);
0187       descriptions.addDefault(desc);
0188     }
0189 
0190   private:
0191     int value_;
0192     size_t count_;
0193     int delta_;
0194     edm::EDPutTokenT<std::vector<std::unique_ptr<int>>> intToken_;
0195     edm::EDPutTokenT<std::vector<std::unique_ptr<IntProduct>>> intProductToken_;
0196   };
0197 }  // namespace edmtest
0198 
0199 using edmtest::IntDequeProducer;
0200 using edmtest::IntListProducer;
0201 using edmtest::IntSetProducer;
0202 using edmtest::IntVectorProducer;
0203 using edmtest::IntVectorSetProducer;
0204 using edmtest::UniqPtrIntVectorProducer;
0205 DEFINE_FWK_MODULE(IntVectorProducer);
0206 DEFINE_FWK_MODULE(IntVectorSetProducer);
0207 DEFINE_FWK_MODULE(IntListProducer);
0208 DEFINE_FWK_MODULE(IntDequeProducer);
0209 DEFINE_FWK_MODULE(IntSetProducer);
0210 DEFINE_FWK_MODULE(UniqPtrIntVectorProducer);