Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-14 23:36:17

0001 
0002 /*----------------------------------------------------------------------
0003 
0004 Toy EDAnalyzers for testing purposes only.
0005 
0006 ----------------------------------------------------------------------*/
0007 
0008 #include "DataFormats/Common/interface/DetSetVector.h"
0009 #include "DataFormats/Common/interface/Handle.h"
0010 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0011 //
0012 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0013 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0014 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/Framework/interface/MakerMacros.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/Utilities/interface/Exception.h"
0019 #include "FWCore/Utilities/interface/InputTag.h"
0020 #include "FWCore/Utilities/interface/transform.h"
0021 //
0022 #include <cassert>
0023 #include <string>
0024 #include <vector>
0025 
0026 namespace edmtest {
0027 
0028   //--------------------------------------------------------------------
0029   //
0030   // Toy analyzers
0031   //
0032   //--------------------------------------------------------------------
0033 
0034   //--------------------------------------------------------------------
0035   //
0036   // every call to NonAnalyzer::analyze does nothing.
0037   //
0038   class NonAnalyzer : public edm::global::EDAnalyzer<> {
0039   public:
0040     explicit NonAnalyzer(edm::ParameterSet const& /*p*/) {}
0041     ~NonAnalyzer() override {}
0042     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& c) const final;
0043   };
0044 
0045   void NonAnalyzer::analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const {}
0046 
0047   //--------------------------------------------------------------------
0048   //
0049   class IntTestAnalyzer : public edm::global::EDAnalyzer<> {
0050   public:
0051     IntTestAnalyzer(edm::ParameterSet const& iPSet)
0052         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0053           token_(consumes(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel"))),
0054           missing_(iPSet.getUntrackedParameter<bool>("valueMustBeMissing")) {}
0055 
0056     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0057       edm::ParameterSetDescription desc;
0058       desc.addUntracked<int>("valueMustMatch");
0059       desc.addUntracked<edm::InputTag>("moduleLabel");
0060       desc.addUntracked<bool>("valueMustBeMissing", false);
0061       descriptions.addDefault(desc);
0062     }
0063 
0064     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0065       auto const& prod = iEvent.getHandle(token_);
0066       if (missing_) {
0067         if (prod.isValid()) {
0068           edm::ProductLabels labels;
0069           labelsForToken(token_, labels);
0070           throw cms::Exception("ValueNotMissing")
0071               << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process
0072               << "\" is being produced, which is not expected.";
0073         }
0074         return;
0075       }
0076       if (prod->value != value_) {
0077         edm::ProductLabels labels;
0078         labelsForToken(token_, labels);
0079         throw cms::Exception("ValueMismatch")
0080             << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process << "\" is "
0081             << prod->value << " but it was supposed to be " << value_;
0082       }
0083     }
0084 
0085   private:
0086     int const value_;
0087     edm::EDGetTokenT<IntProduct> const token_;
0088     bool const missing_;
0089   };
0090 
0091   //--------------------------------------------------------------------
0092   //
0093   class MultipleIntsAnalyzer : public edm::global::EDAnalyzer<> {
0094   public:
0095     MultipleIntsAnalyzer(edm::ParameterSet const& iPSet) {
0096       auto const& tags = iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("getFromModules");
0097       for (auto const& tag : tags) {
0098         m_tokens.emplace_back(consumes(tag));
0099       }
0100     }
0101 
0102     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const override {
0103       for (auto const& token : m_tokens) {
0104         (void)iEvent.getHandle(token);
0105       }
0106     }
0107 
0108     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0109       edm::ParameterSetDescription desc;
0110       desc.addUntracked<std::vector<edm::InputTag>>("getFromModules");
0111       descriptions.addDefault(desc);
0112     }
0113 
0114   private:
0115     std::vector<edm::EDGetTokenT<IntProduct>> m_tokens;
0116   };
0117 
0118   //--------------------------------------------------------------------
0119   //
0120   class BuiltinIntTestAnalyzer : public edm::global::EDAnalyzer<> {
0121   public:
0122     BuiltinIntTestAnalyzer(edm::ParameterSet const& iPSet)
0123         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0124           token_(consumes(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel"))),
0125           missing_(iPSet.getUntrackedParameter<bool>("valueMustBeMissing")) {}
0126 
0127     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0128       edm::ParameterSetDescription desc;
0129       desc.addUntracked<int>("valueMustMatch");
0130       desc.addUntracked<edm::InputTag>("moduleLabel");
0131       desc.addUntracked<bool>("valueMustBeMissing", false);
0132       descriptions.addDefault(desc);
0133     }
0134 
0135     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0136       auto const& prod = iEvent.getHandle(token_);
0137       if (missing_) {
0138         if (prod.isValid()) {
0139           edm::ProductLabels labels;
0140           labelsForToken(token_, labels);
0141           throw cms::Exception("ValueNotMissing")
0142               << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process
0143               << "\" is being produced, which is not expected.";
0144         }
0145         return;
0146       }
0147       if (*prod != value_) {
0148         edm::ProductLabels labels;
0149         labelsForToken(token_, labels);
0150         throw cms::Exception("ValueMismatch")
0151             << "The value for \"" << labels.module << ":" << labels.productInstance << ":" << labels.process << "\" is "
0152             << *prod << " but it was supposed to be " << value_;
0153       }
0154     }
0155 
0156   private:
0157     int const value_;
0158     edm::EDGetTokenT<int> const token_;
0159     bool const missing_;
0160   };
0161 
0162   //--------------------------------------------------------------------
0163   //
0164   template <typename T>
0165   class GenericAnalyzerT : public edm::global::EDAnalyzer<> {
0166   public:
0167     GenericAnalyzerT(edm::ParameterSet const& iPSet)
0168         : tokensBeginProcess_(edm::vector_transform(
0169               iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginProcess"),
0170               [this](edm::InputTag const& tag) { return this->consumes<T, edm::InProcess>(tag); })),
0171           tokensBeginRun_(
0172               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun"),
0173                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InRun>(tag); })),
0174           tokensBeginLumi_(
0175               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi"),
0176                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InLumi>(tag); })),
0177           tokensEvent_(edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent"),
0178                                              [this](edm::InputTag const& tag) { return this->consumes<T>(tag); })),
0179           tokensEndLumi_(
0180               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndLumi"),
0181                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InLumi>(tag); })),
0182           tokensEndRun_(
0183               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndRun"),
0184                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InRun>(tag); })),
0185           tokensEndProcess_(edm::vector_transform(
0186               iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndProcess"),
0187               [this](edm::InputTag const& tag) { return this->consumes<T, edm::InProcess>(tag); })),
0188           shouldExist_(iPSet.getUntrackedParameter<bool>("inputShouldExist")),
0189           shouldBeMissing_(iPSet.getUntrackedParameter<bool>("inputShouldBeMissing")) {
0190       if (shouldExist_ and shouldBeMissing_) {
0191         throw cms::Exception("Configuration")
0192             << "inputShouldExist and inputShouldBeMissing both can not be set to True";
0193       }
0194     }
0195 
0196     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const override {
0197       for (auto const& token : tokensEvent_) {
0198         if (shouldExist_) {
0199           (void)iEvent.get(token);
0200         } else if (shouldBeMissing_) {
0201           auto h = iEvent.getHandle(token);
0202           if (h.isValid()) {
0203             edm::EDConsumerBase::Labels labels;
0204             labelsForToken(token, labels);
0205             throw cms::Exception("ProductExists")
0206                 << "Product " << labels.module << ":" << labels.productInstance << ":" << labels.process
0207                 << " exists, but expectation was it should be missing";
0208           }
0209         }
0210       }
0211     }
0212 
0213     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0214       edm::ParameterSetDescription desc;
0215       desc.addUntracked<std::vector<edm::InputTag>>("srcBeginProcess", std::vector<edm::InputTag>{});
0216       desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
0217       desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
0218       desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
0219       desc.addUntracked<std::vector<edm::InputTag>>("srcEndLumi", std::vector<edm::InputTag>{});
0220       desc.addUntracked<std::vector<edm::InputTag>>("srcEndRun", std::vector<edm::InputTag>{});
0221       desc.addUntracked<std::vector<edm::InputTag>>("srcEndProcess", std::vector<edm::InputTag>{});
0222       desc.addUntracked<bool>("inputShouldExist", false);
0223       desc.addUntracked<bool>("inputShouldBeMissing", false);
0224       descriptions.addDefault(desc);
0225     }
0226 
0227   private:
0228     const std::vector<edm::EDGetTokenT<T>> tokensBeginProcess_;
0229     const std::vector<edm::EDGetTokenT<T>> tokensBeginRun_;
0230     const std::vector<edm::EDGetTokenT<T>> tokensBeginLumi_;
0231     const std::vector<edm::EDGetTokenT<T>> tokensEvent_;
0232     const std::vector<edm::EDGetTokenT<T>> tokensEndLumi_;
0233     const std::vector<edm::EDGetTokenT<T>> tokensEndRun_;
0234     const std::vector<edm::EDGetTokenT<T>> tokensEndProcess_;
0235     const bool shouldExist_;
0236     const bool shouldBeMissing_;
0237   };
0238   using GenericIntsAnalyzer = GenericAnalyzerT<IntProduct>;
0239 
0240   //--------------------------------------------------------------------
0241   //
0242   class IntConsumingAnalyzer : public edm::global::EDAnalyzer<> {
0243   public:
0244     IntConsumingAnalyzer(edm::ParameterSet const& iPSet) {
0245       consumes<IntProduct>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
0246     }
0247 
0248     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}
0249 
0250     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0251       edm::ParameterSetDescription desc;
0252       desc.addUntracked<edm::InputTag>("getFromModule");
0253       descriptions.addDefault(desc);
0254     }
0255   };
0256   //--------------------------------------------------------------------
0257   //
0258   class IntFromRunConsumingAnalyzer : public edm::global::EDAnalyzer<> {
0259   public:
0260     IntFromRunConsumingAnalyzer(edm::ParameterSet const& iPSet) {
0261       consumes<IntProduct, edm::InRun>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
0262     }
0263 
0264     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}
0265 
0266     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0267       edm::ParameterSetDescription desc;
0268       desc.addUntracked<edm::InputTag>("getFromModule");
0269       descriptions.addDefault(desc);
0270     }
0271   };
0272   //--------------------------------------------------------------------
0273   //
0274   class ConsumingStreamAnalyzer : public edm::stream::EDAnalyzer<> {
0275   public:
0276     ConsumingStreamAnalyzer(edm::ParameterSet const& iPSet)
0277         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0278           moduleLabel_(iPSet.getUntrackedParameter<std::string>("moduleLabel"), "") {
0279       mayConsume<IntProduct>(moduleLabel_);
0280     }
0281 
0282     void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0283       edm::Handle<IntProduct> handle;
0284       iEvent.getByLabel(moduleLabel_, handle);
0285       if (handle->value != value_) {
0286         throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
0287                                                << " but it was supposed to be " << value_;
0288       }
0289     }
0290 
0291   private:
0292     int value_;
0293     edm::InputTag moduleLabel_;
0294   };
0295 
0296   //--------------------------------------------------------------------
0297   //
0298   class ConsumingOneSharedResourceAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0299   public:
0300     ConsumingOneSharedResourceAnalyzer(edm::ParameterSet const& iPSet)
0301         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0302           moduleLabel_(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel")) {
0303       mayConsume<IntProduct>(moduleLabel_);
0304       usesResource(iPSet.getUntrackedParameter<std::string>("resourceName"));
0305     }
0306 
0307     void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0308       edm::Handle<IntProduct> handle;
0309       iEvent.getByLabel(moduleLabel_, handle);
0310       if (handle->value != value_) {
0311         throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
0312                                                << " but it was supposed to be " << value_;
0313       }
0314     }
0315 
0316   private:
0317     int value_;
0318     edm::InputTag moduleLabel_;
0319   };
0320 
0321   //--------------------------------------------------------------------
0322   //
0323   class SCSimpleAnalyzer : public edm::global::EDAnalyzer<> {
0324   public:
0325     SCSimpleAnalyzer(edm::ParameterSet const&) {}
0326 
0327     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
0328   };
0329 
0330   void SCSimpleAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0331     // Get the product back out; it should be sorted.
0332     edm::Handle<SCSimpleProduct> h;
0333     e.getByLabel("scs", h);
0334     assert(h.isValid());
0335 
0336     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0337     // copying all the values out of the SortedCollection so we can
0338     // manipulate them via an interface different from
0339     // SortedCollection, just so that we can make sure the collection
0340     // is sorted.
0341     std::vector<Simple> after(h->begin(), h->end());
0342     typedef std::vector<Simple>::size_type size_type;
0343 
0344     // Verify that the vector *is* sorted.
0345 
0346     for (size_type i = 1, end = after.size(); i < end; ++i) {
0347       assert(after[i - 1].id() < after[i].id());
0348     }
0349   }
0350 
0351   //--------------------------------------------------------------------
0352   //
0353   // Consumes View<Simple>
0354   //
0355   class SimpleViewAnalyzer : public edm::global::EDAnalyzer<> {
0356   public:
0357     explicit SimpleViewAnalyzer(edm::ParameterSet const& p)
0358         : token_(consumes(p.getUntrackedParameter<edm::InputTag>("label"))),
0359           size_(p.getUntrackedParameter<unsigned int>("sizeMustMatch")),
0360           checkSize_(p.getUntrackedParameter<bool>("checkSize")) {}
0361 
0362     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0363       edm::ParameterSetDescription desc;
0364       desc.addUntracked<bool>("checkSize", true);
0365       desc.addUntracked<unsigned int>("sizeMustMatch");
0366       desc.addUntracked<edm::InputTag>("label");
0367       descriptions.addDefault(desc);
0368     }
0369 
0370     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0371       if (checkSize_) {
0372         auto const& prod = e.get(token_);
0373         if (prod.size() != size_) {
0374           throw cms::Exception("SizeMismatch")
0375               << "Product size " << prod.size() << " differs from expectation " << size_;
0376         }
0377       }
0378     }
0379 
0380   private:
0381     edm::EDGetTokenT<edm::View<Simple>> const token_;
0382     unsigned int const size_;
0383     bool const checkSize_;
0384   };
0385 
0386   //--------------------------------------------------------------------
0387   //
0388   class DSVAnalyzer : public edm::global::EDAnalyzer<> {
0389   public:
0390     DSVAnalyzer(edm::ParameterSet const&) {}
0391 
0392     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
0393 
0394   private:
0395     void do_sorted_stuff(edm::Event const& e) const;
0396     void do_unsorted_stuff(edm::Event const& e) const;
0397   };
0398 
0399   void DSVAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0400     do_sorted_stuff(e);
0401     do_unsorted_stuff(e);
0402   }
0403 
0404   void DSVAnalyzer::do_sorted_stuff(edm::Event const& e) const {
0405     typedef DSVSimpleProduct product_type;
0406     typedef product_type::value_type detset;
0407     typedef detset::value_type value_type;
0408     // Get the product back out; it should be sorted.
0409     edm::Handle<product_type> h;
0410     e.getByLabel("dsv1", h);
0411     assert(h.isValid());
0412 
0413     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0414     // copying all the values out of the DetSetVector's first DetSet so we can
0415     // manipulate them via an interface different from
0416     // DetSet, just so that we can make sure the collection
0417     // is sorted.
0418     std::vector<value_type> const& after = (h->end() - 1)->data;
0419     typedef std::vector<value_type>::size_type size_type;
0420 
0421     // Verify that the vector *is* sorted.
0422 
0423     for (size_type i = 1, end = after.size(); i < end; ++i) {
0424       assert(after[i - 1].data < after[i].data);
0425     }
0426   }
0427 
0428   void DSVAnalyzer::do_unsorted_stuff(edm::Event const& e) const {
0429     typedef DSVWeirdProduct product_type;
0430     typedef product_type::value_type detset;
0431     typedef detset::value_type value_type;
0432     // Get the product back out; it should be unsorted.
0433     edm::Handle<product_type> h;
0434     e.getByLabel("dsv1", h);
0435     assert(h.isValid());
0436 
0437     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0438     // copying all the values out of the DetSetVector's first DetSet so we can
0439     // manipulate them via an interface different from
0440     // DetSet, just so that we can make sure the collection
0441     // is not sorted.
0442     std::vector<value_type> const& after = (h->end() - 1)->data;
0443     typedef std::vector<value_type>::size_type size_type;
0444 
0445     // Verify that the vector is reverse-sorted.
0446 
0447     for (size_type i = 1, end = after.size(); i < end; ++i) {
0448       assert(after[i - 1].data > after[i].data);
0449     }
0450   }
0451 }  // namespace edmtest
0452 
0453 using edmtest::BuiltinIntTestAnalyzer;
0454 using edmtest::ConsumingOneSharedResourceAnalyzer;
0455 using edmtest::ConsumingStreamAnalyzer;
0456 using edmtest::DSVAnalyzer;
0457 using edmtest::IntConsumingAnalyzer;
0458 using edmtest::IntTestAnalyzer;
0459 using edmtest::MultipleIntsAnalyzer;
0460 using edmtest::NonAnalyzer;
0461 using edmtest::SCSimpleAnalyzer;
0462 using edmtest::SimpleViewAnalyzer;
0463 DEFINE_FWK_MODULE(NonAnalyzer);
0464 DEFINE_FWK_MODULE(IntTestAnalyzer);
0465 DEFINE_FWK_MODULE(MultipleIntsAnalyzer);
0466 DEFINE_FWK_MODULE(edmtest::GenericIntsAnalyzer);
0467 DEFINE_FWK_MODULE(IntConsumingAnalyzer);
0468 DEFINE_FWK_MODULE(edmtest::IntFromRunConsumingAnalyzer);
0469 DEFINE_FWK_MODULE(ConsumingStreamAnalyzer);
0470 DEFINE_FWK_MODULE(ConsumingOneSharedResourceAnalyzer);
0471 DEFINE_FWK_MODULE(SCSimpleAnalyzer);
0472 DEFINE_FWK_MODULE(SimpleViewAnalyzer);
0473 DEFINE_FWK_MODULE(DSVAnalyzer);
0474 DEFINE_FWK_MODULE(BuiltinIntTestAnalyzer);