Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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   using GenericUInt64Analyzer = GenericAnalyzerT<UInt64Product>;
0240 
0241   //--------------------------------------------------------------------
0242   //
0243   class IntConsumingAnalyzer : public edm::global::EDAnalyzer<> {
0244   public:
0245     IntConsumingAnalyzer(edm::ParameterSet const& iPSet) {
0246       consumes<IntProduct>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
0247     }
0248 
0249     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}
0250 
0251     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0252       edm::ParameterSetDescription desc;
0253       desc.addUntracked<edm::InputTag>("getFromModule");
0254       descriptions.addDefault(desc);
0255     }
0256   };
0257   //--------------------------------------------------------------------
0258   //
0259   class IntFromRunConsumingAnalyzer : public edm::global::EDAnalyzer<> {
0260   public:
0261     IntFromRunConsumingAnalyzer(edm::ParameterSet const& iPSet) {
0262       consumes<IntProduct, edm::InRun>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
0263     }
0264 
0265     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}
0266 
0267     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0268       edm::ParameterSetDescription desc;
0269       desc.addUntracked<edm::InputTag>("getFromModule");
0270       descriptions.addDefault(desc);
0271     }
0272   };
0273   //--------------------------------------------------------------------
0274   //
0275   class ConsumingStreamAnalyzer : public edm::stream::EDAnalyzer<> {
0276   public:
0277     ConsumingStreamAnalyzer(edm::ParameterSet const& iPSet)
0278         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0279           moduleLabel_(iPSet.getUntrackedParameter<std::string>("moduleLabel"), "") {
0280       mayConsume<IntProduct>(moduleLabel_);
0281     }
0282 
0283     void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0284       edm::Handle<IntProduct> handle;
0285       iEvent.getByLabel(moduleLabel_, handle);
0286       if (handle->value != value_) {
0287         throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
0288                                                << " but it was supposed to be " << value_;
0289       }
0290     }
0291 
0292   private:
0293     int value_;
0294     edm::InputTag moduleLabel_;
0295   };
0296 
0297   //--------------------------------------------------------------------
0298   //
0299   class ConsumingOneSharedResourceAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0300   public:
0301     ConsumingOneSharedResourceAnalyzer(edm::ParameterSet const& iPSet)
0302         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0303           moduleLabel_(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel")) {
0304       mayConsume<IntProduct>(moduleLabel_);
0305       usesResource(iPSet.getUntrackedParameter<std::string>("resourceName"));
0306     }
0307 
0308     void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0309       edm::Handle<IntProduct> handle;
0310       iEvent.getByLabel(moduleLabel_, handle);
0311       if (handle->value != value_) {
0312         throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
0313                                                << " but it was supposed to be " << value_;
0314       }
0315     }
0316 
0317   private:
0318     int value_;
0319     edm::InputTag moduleLabel_;
0320   };
0321 
0322   //--------------------------------------------------------------------
0323   //
0324   class SCSimpleAnalyzer : public edm::global::EDAnalyzer<> {
0325   public:
0326     SCSimpleAnalyzer(edm::ParameterSet const&) {}
0327 
0328     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
0329   };
0330 
0331   void SCSimpleAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0332     // Get the product back out; it should be sorted.
0333     edm::Handle<SCSimpleProduct> h;
0334     e.getByLabel("scs", h);
0335     assert(h.isValid());
0336 
0337     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0338     // copying all the values out of the SortedCollection so we can
0339     // manipulate them via an interface different from
0340     // SortedCollection, just so that we can make sure the collection
0341     // is sorted.
0342     std::vector<Simple> after(h->begin(), h->end());
0343     typedef std::vector<Simple>::size_type size_type;
0344 
0345     // Verify that the vector *is* sorted.
0346 
0347     for (size_type i = 1, end = after.size(); i < end; ++i) {
0348       assert(after[i - 1].id() < after[i].id());
0349     }
0350   }
0351 
0352   //--------------------------------------------------------------------
0353   //
0354   // Consumes View<Simple>
0355   //
0356   class SimpleViewAnalyzer : public edm::global::EDAnalyzer<> {
0357   public:
0358     explicit SimpleViewAnalyzer(edm::ParameterSet const& p)
0359         : token_(consumes(p.getUntrackedParameter<edm::InputTag>("label"))),
0360           size_(p.getUntrackedParameter<unsigned int>("sizeMustMatch")),
0361           checkSize_(p.getUntrackedParameter<bool>("checkSize")) {}
0362 
0363     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0364       edm::ParameterSetDescription desc;
0365       desc.addUntracked<bool>("checkSize", true);
0366       desc.addUntracked<unsigned int>("sizeMustMatch");
0367       desc.addUntracked<edm::InputTag>("label");
0368       descriptions.addDefault(desc);
0369     }
0370 
0371     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0372       if (checkSize_) {
0373         auto const& prod = e.get(token_);
0374         if (prod.size() != size_) {
0375           throw cms::Exception("SizeMismatch")
0376               << "Product size " << prod.size() << " differs from expectation " << size_;
0377         }
0378       }
0379     }
0380 
0381   private:
0382     edm::EDGetTokenT<edm::View<Simple>> const token_;
0383     unsigned int const size_;
0384     bool const checkSize_;
0385   };
0386 
0387   //--------------------------------------------------------------------
0388   //
0389   class DSVAnalyzer : public edm::global::EDAnalyzer<> {
0390   public:
0391     DSVAnalyzer(edm::ParameterSet const&) {}
0392 
0393     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
0394 
0395   private:
0396     void do_sorted_stuff(edm::Event const& e) const;
0397     void do_unsorted_stuff(edm::Event const& e) const;
0398   };
0399 
0400   void DSVAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0401     do_sorted_stuff(e);
0402     do_unsorted_stuff(e);
0403   }
0404 
0405   void DSVAnalyzer::do_sorted_stuff(edm::Event const& e) const {
0406     typedef DSVSimpleProduct product_type;
0407     typedef product_type::value_type detset;
0408     typedef detset::value_type value_type;
0409     // Get the product back out; it should be sorted.
0410     edm::Handle<product_type> h;
0411     e.getByLabel("dsv1", h);
0412     assert(h.isValid());
0413 
0414     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0415     // copying all the values out of the DetSetVector's first DetSet so we can
0416     // manipulate them via an interface different from
0417     // DetSet, just so that we can make sure the collection
0418     // is sorted.
0419     std::vector<value_type> const& after = (h->end() - 1)->data;
0420     typedef std::vector<value_type>::size_type size_type;
0421 
0422     // Verify that the vector *is* sorted.
0423 
0424     for (size_type i = 1, end = after.size(); i < end; ++i) {
0425       assert(after[i - 1].data < after[i].data);
0426     }
0427   }
0428 
0429   void DSVAnalyzer::do_unsorted_stuff(edm::Event const& e) const {
0430     typedef DSVWeirdProduct product_type;
0431     typedef product_type::value_type detset;
0432     typedef detset::value_type value_type;
0433     // Get the product back out; it should be unsorted.
0434     edm::Handle<product_type> h;
0435     e.getByLabel("dsv1", h);
0436     assert(h.isValid());
0437 
0438     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0439     // copying all the values out of the DetSetVector's first DetSet so we can
0440     // manipulate them via an interface different from
0441     // DetSet, just so that we can make sure the collection
0442     // is not sorted.
0443     std::vector<value_type> const& after = (h->end() - 1)->data;
0444     typedef std::vector<value_type>::size_type size_type;
0445 
0446     // Verify that the vector is reverse-sorted.
0447 
0448     for (size_type i = 1, end = after.size(); i < end; ++i) {
0449       assert(after[i - 1].data > after[i].data);
0450     }
0451   }
0452 }  // namespace edmtest
0453 
0454 using edmtest::BuiltinIntTestAnalyzer;
0455 using edmtest::ConsumingOneSharedResourceAnalyzer;
0456 using edmtest::ConsumingStreamAnalyzer;
0457 using edmtest::DSVAnalyzer;
0458 using edmtest::IntConsumingAnalyzer;
0459 using edmtest::IntTestAnalyzer;
0460 using edmtest::MultipleIntsAnalyzer;
0461 using edmtest::NonAnalyzer;
0462 using edmtest::SCSimpleAnalyzer;
0463 using edmtest::SimpleViewAnalyzer;
0464 DEFINE_FWK_MODULE(NonAnalyzer);
0465 DEFINE_FWK_MODULE(IntTestAnalyzer);
0466 DEFINE_FWK_MODULE(MultipleIntsAnalyzer);
0467 DEFINE_FWK_MODULE(edmtest::GenericIntsAnalyzer);
0468 DEFINE_FWK_MODULE(edmtest::GenericUInt64Analyzer);
0469 DEFINE_FWK_MODULE(IntConsumingAnalyzer);
0470 DEFINE_FWK_MODULE(edmtest::IntFromRunConsumingAnalyzer);
0471 DEFINE_FWK_MODULE(ConsumingStreamAnalyzer);
0472 DEFINE_FWK_MODULE(ConsumingOneSharedResourceAnalyzer);
0473 DEFINE_FWK_MODULE(SCSimpleAnalyzer);
0474 DEFINE_FWK_MODULE(SimpleViewAnalyzer);
0475 DEFINE_FWK_MODULE(DSVAnalyzer);
0476 DEFINE_FWK_MODULE(BuiltinIntTestAnalyzer);