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 and EDProducts for testing purposes only.
0005 
0006 ----------------------------------------------------------------------*/
0007 
0008 #include "DataFormats/Common/interface/DetSetVector.h"
0009 #include "DataFormats/Common/interface/Handle.h"
0010 #include "DataFormats/Common/interface/RefProd.h"
0011 #include "DataFormats/Common/interface/View.h"
0012 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0013 
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/Framework/interface/global/EDFilter.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 
0020 #include <cassert>
0021 #include <stdexcept>
0022 #include <string>
0023 #include <vector>
0024 
0025 namespace edmtest {
0026 
0027   //--------------------------------------------------------------------
0028   //
0029   // Toy producers
0030   //
0031   //--------------------------------------------------------------------
0032 
0033   //--------------------------------------------------------------------
0034   //
0035   // Produces and SCSimpleProduct product instance.
0036   //
0037   class SCSimpleProducer : public edm::stream::EDProducer<> {
0038   public:
0039     explicit SCSimpleProducer(edm::ParameterSet const& p) : size_(p.getParameter<int>("size")) {
0040       produces<SCSimpleProduct>();
0041       assert(size_ > 1);
0042     }
0043 
0044     explicit SCSimpleProducer(int i) : size_(i) {
0045       produces<SCSimpleProduct>();
0046       assert(size_ > 1);
0047     }
0048 
0049     virtual ~SCSimpleProducer() {}
0050     virtual void produce(edm::Event& e, edm::EventSetup const& c) override;
0051 
0052   private:
0053     int size_;  // number of Simples to put in the collection
0054   };
0055 
0056   void SCSimpleProducer::produce(edm::Event& e, edm::EventSetup const& /* unused */) {
0057     // Fill up a collection so that it is sorted *backwards*.
0058     std::vector<Simple> guts(size_);
0059     for (int i = 0; i < size_; ++i) {
0060       guts[i].key = size_ - i;
0061       guts[i].value = 1.5 * i;
0062     }
0063 
0064     // Verify that the vector is not sorted -- in fact, it is sorted
0065     // backwards!
0066     for (int i = 1; i < size_; ++i) {
0067       assert(guts[i - 1].id() > guts[i].id());
0068     }
0069 
0070     // Put the product into the Event, thus sorting it.
0071     e.put(std::make_unique<SCSimpleProduct>(guts));
0072   }
0073 
0074   //--------------------------------------------------------------------
0075   //
0076   // Produces and OVSimpleProduct product instance.
0077   //
0078   class OVSimpleProducer : public edm::stream::EDProducer<> {
0079   public:
0080     explicit OVSimpleProducer(edm::ParameterSet const& p) : size_(p.getParameter<int>("size")) {
0081       produces<OVSimpleProduct>();
0082       produces<OVSimpleDerivedProduct>("derived");
0083       assert(size_ > 1);
0084     }
0085 
0086     explicit OVSimpleProducer(int i) : size_(i) {
0087       produces<OVSimpleProduct>();
0088       produces<OVSimpleDerivedProduct>("derived");
0089       assert(size_ > 1);
0090     }
0091 
0092     virtual ~OVSimpleProducer() {}
0093     virtual void produce(edm::Event& e, edm::EventSetup const& c) override;
0094 
0095   private:
0096     int size_;  // number of Simples to put in the collection
0097   };
0098 
0099   void OVSimpleProducer::produce(edm::Event& e, edm::EventSetup const& /* unused */) {
0100     // Fill up a collection
0101     auto p = std::make_unique<OVSimpleProduct>();
0102 
0103     for (int i = 0; i < size_; ++i) {
0104       auto simple = std::make_unique<Simple>();
0105       simple->key = size_ - i;
0106       simple->value = 1.5 * i;
0107       p->push_back(std::move(simple));
0108     }
0109 
0110     // Put the product into the Event
0111     e.put(std::move(p));
0112 
0113     // Fill up a collection of SimpleDerived objects
0114     auto pd = std::make_unique<OVSimpleDerivedProduct>();
0115 
0116     for (int i = 0; i < size_; ++i) {
0117       auto simpleDerived = std::make_unique<SimpleDerived>();
0118       simpleDerived->key = size_ - i;
0119       simpleDerived->value = 1.5 * i + 100.0;
0120       simpleDerived->dummy = 0.0;
0121       pd->push_back(std::move(simpleDerived));
0122     }
0123 
0124     // Put the product into the Event
0125     e.put(std::move(pd), "derived");
0126   }
0127 
0128   //--------------------------------------------------------------------
0129   //
0130   // Produces and OVSimpleProduct product instance.
0131   //
0132   class VSimpleProducer : public edm::stream::EDProducer<> {
0133   public:
0134     explicit VSimpleProducer(edm::ParameterSet const& p) : size_(p.getParameter<int>("size")) {
0135       produces<VSimpleProduct>();
0136       assert(size_ > 1);
0137     }
0138 
0139     explicit VSimpleProducer(int i) : size_(i) {
0140       produces<VSimpleProduct>();
0141       assert(size_ > 1);
0142     }
0143 
0144     virtual ~VSimpleProducer() {}
0145     virtual void produce(edm::Event& e, edm::EventSetup const& c) override;
0146 
0147   private:
0148     int size_;  // number of Simples to put in the collection
0149   };
0150 
0151   void VSimpleProducer::produce(edm::Event& e, edm::EventSetup const& /* unused */) {
0152     // Fill up a collection
0153     auto p = std::make_unique<VSimpleProduct>();
0154 
0155     for (int i = 0; i < size_; ++i) {
0156       Simple simple;
0157       simple.key = size_ - i;
0158       simple.value = 1.5 * i + e.id().event();
0159       p->push_back(simple);
0160     }
0161 
0162     // Put the product into the Event
0163     e.put(std::move(p));
0164   }
0165 
0166   //--------------------------------------------------------------------
0167   //
0168   // Produces AssociationVector<vector<Simple>, vector<Simple> > object
0169   // This is used to test a View of an AssociationVector
0170   //
0171   class AVSimpleProducer : public edm::stream::EDProducer<> {
0172   public:
0173     explicit AVSimpleProducer(edm::ParameterSet const& p) : src_(p.getParameter<edm::InputTag>("src")) {
0174       produces<AVSimpleProduct>();
0175       consumes<std::vector<edmtest::Simple>>(src_);
0176     }
0177 
0178     virtual void produce(edm::Event& e, edm::EventSetup const& c) override;
0179 
0180   private:
0181     edm::InputTag src_;
0182   };
0183 
0184   void AVSimpleProducer::produce(edm::Event& e, edm::EventSetup const& /* unused */) {
0185     edm::Handle<std::vector<edmtest::Simple>> vs;
0186     e.getByLabel(src_, vs);
0187     // Fill up a collection
0188     auto p = std::make_unique<AVSimpleProduct>(edm::RefProd<std::vector<edmtest::Simple>>(vs));
0189 
0190     for (unsigned int i = 0; i < vs->size(); ++i) {
0191       edmtest::Simple simple;
0192       simple.key = 100 + i;  // just some arbitrary number for testing
0193       simple.value = .1 * e.id().event();
0194       p->setValue(i, simple);
0195     }
0196 
0197     // Put the product into the Event
0198     e.put(std::move(p));
0199   }
0200 
0201   //--------------------------------------------------------------------
0202   //
0203   // Produces two products:
0204   //    DSVSimpleProduct
0205   //    DSVWeirdProduct
0206   //
0207   class DSVProducer : public edm::stream::EDProducer<> {
0208   public:
0209     explicit DSVProducer(edm::ParameterSet const& p) : size_(p.getParameter<int>("size")) {
0210       produces<DSVSimpleProduct>();
0211       produces<DSVWeirdProduct>();
0212       assert(size_ > 1);
0213     }
0214 
0215     explicit DSVProducer(int i) : size_(i) {
0216       produces<DSVSimpleProduct>();
0217       produces<DSVWeirdProduct>();
0218       assert(size_ > 1);
0219     }
0220 
0221     virtual ~DSVProducer() {}
0222 
0223     virtual void produce(edm::Event& e, edm::EventSetup const&) override;
0224 
0225   private:
0226     template <typename PROD>
0227     void make_a_product(edm::Event& e);
0228     int size_;
0229   };
0230 
0231   void DSVProducer::produce(edm::Event& e, edm::EventSetup const& /* unused */) {
0232     this->make_a_product<DSVSimpleProduct>(e);
0233     this->make_a_product<DSVWeirdProduct>(e);
0234   }
0235 
0236   template <typename PROD>
0237   void DSVProducer::make_a_product(edm::Event& e) {
0238     typedef PROD product_type;
0239     typedef typename product_type::value_type detset;
0240     typedef typename detset::value_type value_type;
0241 
0242     // Fill up a collection so that it is sorted *backwards*.
0243     std::vector<value_type> guts(size_);
0244     for (int i = 0; i < size_; ++i) {
0245       guts[i].data = size_ - i;
0246     }
0247 
0248     // Verify that the vector is not sorted -- in fact, it is sorted
0249     // backwards!
0250     for (int i = 1; i < size_; ++i) {
0251       assert(guts[i - 1].data > guts[i].data);
0252     }
0253 
0254     auto p = std::make_unique<product_type>();
0255     int n = 0;
0256     for (int id = 1; id < size_; ++id) {
0257       ++n;
0258       detset item(id);  // this will get DetID id
0259       item.data.insert(item.data.end(), guts.begin(), guts.begin() + n);
0260       p->insert(item);
0261     }
0262 
0263     // Put the product into the Event, thus sorting it ... or not,
0264     // depending upon the product type.
0265     e.put(std::move(p));
0266   }
0267 
0268   //--------------------------------------------------------------------
0269   //
0270   // Produces two products: (new DataSetVector)
0271   //    DSTVSimpleProduct
0272   //    DSTVSimpleDerivedProduct
0273   //
0274   class DSTVProducer : public edm::stream::EDProducer<> {
0275   public:
0276     explicit DSTVProducer(edm::ParameterSet const& p) : size_(p.getParameter<int>("size")) {
0277       produces<DSTVSimpleProduct>();
0278       produces<DSTVSimpleDerivedProduct>();
0279       assert(size_ > 1);
0280     }
0281 
0282     explicit DSTVProducer(int i) : size_(i) {
0283       produces<DSTVSimpleProduct>();
0284       produces<DSTVSimpleDerivedProduct>();
0285       assert(size_ > 1);
0286     }
0287 
0288     virtual ~DSTVProducer() {}
0289 
0290     virtual void produce(edm::Event& e, edm::EventSetup const&) override;
0291 
0292   private:
0293     template <typename PROD>
0294     void make_a_product(edm::Event& e);
0295     void fill_a_data(DSTVSimpleProduct::data_type& d, unsigned int i);
0296     void fill_a_data(DSTVSimpleDerivedProduct::data_type& d, unsigned int i);
0297 
0298     int size_;
0299   };
0300 
0301   void DSTVProducer::produce(edm::Event& e, edm::EventSetup const& /* unused */) {
0302     this->make_a_product<DSTVSimpleProduct>(e);
0303     this->make_a_product<DSTVSimpleDerivedProduct>(e);
0304   }
0305 
0306   void DSTVProducer::fill_a_data(DSTVSimpleDerivedProduct::data_type& d, unsigned int i) {
0307     d.key = size_ - i;
0308     d.value = 1.5 * i;
0309   }
0310 
0311   void DSTVProducer::fill_a_data(DSTVSimpleProduct::data_type& d, unsigned int i) { d.data = size_ - i; }
0312 
0313   template <typename PROD>
0314   void DSTVProducer::make_a_product(edm::Event& e) {
0315     typedef PROD product_type;
0316     //FIXME
0317     typedef typename product_type::FastFiller detset;
0318     typedef typename detset::id_type id_type;
0319 
0320     auto p = std::make_unique<product_type>();
0321     product_type& v = *p;
0322 
0323     unsigned int n = 0;
0324     for (id_type id = 1; id < static_cast<id_type>(size_); ++id) {
0325       ++n;
0326       detset item(v, id);  // this will get DetID id
0327       item.resize(n);
0328       for (unsigned int i = 0; i < n; ++i)
0329         fill_a_data(item[i], i);
0330     }
0331 
0332     // Put the product into the Event, thus sorting is not done by magic,
0333     // up to one user-line
0334     e.put(std::move(p));
0335   }
0336 
0337   //--------------------------------------------------------------------
0338   //
0339   // Produces an Prodigal instance.
0340   //
0341   class ProdigalProducer : public edm::stream::EDProducer<> {
0342   public:
0343     explicit ProdigalProducer(edm::ParameterSet const& p) : label_(p.getParameter<std::string>("label")) {
0344       produces<Prodigal>();
0345       consumes<IntProduct>(edm::InputTag{label_});
0346     }
0347     virtual ~ProdigalProducer() {}
0348     virtual void produce(edm::Event& e, edm::EventSetup const& c) override;
0349 
0350   private:
0351     std::string label_;
0352   };
0353 
0354   void ProdigalProducer::produce(edm::Event& e, edm::EventSetup const&) {
0355     // EventSetup is not used.
0356 
0357     // The purpose of Prodigal is testing of *not* keeping
0358     // parentage. So we need to get a product...
0359     edm::Handle<IntProduct> parent;
0360     e.getByLabel(label_, parent);
0361 
0362     e.put(std::make_unique<Prodigal>(parent->value));
0363   }
0364 
0365   class IntProductFilter : public edm::global::EDFilter<> {
0366   public:
0367     explicit IntProductFilter(edm::ParameterSet const& p)
0368         : token_(consumes(p.getParameter<edm::InputTag>("label"))),
0369           threshold_(p.getParameter<int>("threshold")),
0370           shouldProduce_(p.getParameter<bool>("shouldProduce")) {
0371       if (shouldProduce_) {
0372         putToken_ = produces<edmtest::IntProduct>();
0373       }
0374     }
0375 
0376     bool filter(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const {
0377       auto const& product = iEvent.get(token_);
0378       if (product.value < threshold_) {
0379         return false;
0380       }
0381       if (shouldProduce_) {
0382         iEvent.emplace(putToken_, product);
0383       }
0384       return true;
0385     }
0386 
0387     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0388       edm::ParameterSetDescription desc;
0389       desc.add<edm::InputTag>("label");
0390       desc.add<int>("threshold", 0);
0391       desc.add<bool>("shouldProduce", false);
0392       descriptions.addDefault(desc);
0393     }
0394 
0395   private:
0396     const edm::EDGetTokenT<edmtest::IntProduct> token_;
0397     edm::EDPutTokenT<edmtest::IntProduct> putToken_;
0398     const int threshold_;
0399     const bool shouldProduce_;
0400   };
0401 
0402 }  // namespace edmtest
0403 
0404 using edmtest::AVSimpleProducer;
0405 using edmtest::DSTVProducer;
0406 using edmtest::DSVProducer;
0407 using edmtest::IntProductFilter;
0408 using edmtest::OVSimpleProducer;
0409 using edmtest::ProdigalProducer;
0410 using edmtest::SCSimpleProducer;
0411 using edmtest::VSimpleProducer;
0412 DEFINE_FWK_MODULE(SCSimpleProducer);
0413 DEFINE_FWK_MODULE(OVSimpleProducer);
0414 DEFINE_FWK_MODULE(VSimpleProducer);
0415 DEFINE_FWK_MODULE(AVSimpleProducer);
0416 DEFINE_FWK_MODULE(DSVProducer);
0417 DEFINE_FWK_MODULE(DSTVProducer);
0418 DEFINE_FWK_MODULE(ProdigalProducer);
0419 DEFINE_FWK_MODULE(IntProductFilter);