Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-08 00:35:15

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   template <typename T>
0121   class GenericAnalyzerT : public edm::global::EDAnalyzer<> {
0122   public:
0123     GenericAnalyzerT(edm::ParameterSet const& iPSet)
0124         : tokensBeginProcess_(edm::vector_transform(
0125               iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginProcess"),
0126               [this](edm::InputTag const& tag) { return this->consumes<T, edm::InProcess>(tag); })),
0127           tokensBeginRun_(
0128               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun"),
0129                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InRun>(tag); })),
0130           tokensBeginLumi_(
0131               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi"),
0132                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InLumi>(tag); })),
0133           tokensEvent_(edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent"),
0134                                              [this](edm::InputTag const& tag) { return this->consumes<T>(tag); })),
0135           tokensEndLumi_(
0136               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndLumi"),
0137                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InLumi>(tag); })),
0138           tokensEndRun_(
0139               edm::vector_transform(iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndRun"),
0140                                     [this](edm::InputTag const& tag) { return this->consumes<T, edm::InRun>(tag); })),
0141           tokensEndProcess_(edm::vector_transform(
0142               iPSet.getUntrackedParameter<std::vector<edm::InputTag>>("srcEndProcess"),
0143               [this](edm::InputTag const& tag) { return this->consumes<T, edm::InProcess>(tag); })),
0144           shouldExist_(iPSet.getUntrackedParameter<bool>("inputShouldExist")),
0145           shouldBeMissing_(iPSet.getUntrackedParameter<bool>("inputShouldBeMissing")) {
0146       if (shouldExist_ and shouldBeMissing_) {
0147         throw cms::Exception("Configuration")
0148             << "inputShouldExist and inputShouldBeMissing both can not be set to True";
0149       }
0150     }
0151 
0152     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const override {
0153       for (auto const& token : tokensEvent_) {
0154         if (shouldExist_) {
0155           (void)iEvent.get(token);
0156         } else if (shouldBeMissing_) {
0157           auto h = iEvent.getHandle(token);
0158           if (h.isValid()) {
0159             edm::EDConsumerBase::Labels labels;
0160             labelsForToken(token, labels);
0161             throw cms::Exception("ProductExists")
0162                 << "Product " << labels.module << ":" << labels.productInstance << ":" << labels.process
0163                 << " exists, but expectation was it should be missing";
0164           }
0165         }
0166       }
0167     }
0168 
0169     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0170       edm::ParameterSetDescription desc;
0171       desc.addUntracked<std::vector<edm::InputTag>>("srcBeginProcess", std::vector<edm::InputTag>{});
0172       desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
0173       desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
0174       desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
0175       desc.addUntracked<std::vector<edm::InputTag>>("srcEndLumi", std::vector<edm::InputTag>{});
0176       desc.addUntracked<std::vector<edm::InputTag>>("srcEndRun", std::vector<edm::InputTag>{});
0177       desc.addUntracked<std::vector<edm::InputTag>>("srcEndProcess", std::vector<edm::InputTag>{});
0178       desc.addUntracked<bool>("inputShouldExist", false);
0179       desc.addUntracked<bool>("inputShouldBeMissing", false);
0180       descriptions.addDefault(desc);
0181     }
0182 
0183   private:
0184     const std::vector<edm::EDGetTokenT<T>> tokensBeginProcess_;
0185     const std::vector<edm::EDGetTokenT<T>> tokensBeginRun_;
0186     const std::vector<edm::EDGetTokenT<T>> tokensBeginLumi_;
0187     const std::vector<edm::EDGetTokenT<T>> tokensEvent_;
0188     const std::vector<edm::EDGetTokenT<T>> tokensEndLumi_;
0189     const std::vector<edm::EDGetTokenT<T>> tokensEndRun_;
0190     const std::vector<edm::EDGetTokenT<T>> tokensEndProcess_;
0191     const bool shouldExist_;
0192     const bool shouldBeMissing_;
0193   };
0194   using GenericIntsAnalyzer = GenericAnalyzerT<IntProduct>;
0195   using GenericUInt64Analyzer = GenericAnalyzerT<UInt64Product>;
0196 
0197   //--------------------------------------------------------------------
0198   //
0199   class IntConsumingAnalyzer : public edm::global::EDAnalyzer<> {
0200   public:
0201     IntConsumingAnalyzer(edm::ParameterSet const& iPSet) {
0202       consumes<IntProduct>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
0203     }
0204 
0205     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}
0206 
0207     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0208       edm::ParameterSetDescription desc;
0209       desc.addUntracked<edm::InputTag>("getFromModule");
0210       descriptions.addDefault(desc);
0211     }
0212   };
0213   //--------------------------------------------------------------------
0214   //
0215   class IntFromRunConsumingAnalyzer : public edm::global::EDAnalyzer<> {
0216   public:
0217     IntFromRunConsumingAnalyzer(edm::ParameterSet const& iPSet) {
0218       consumes<IntProduct, edm::InRun>(iPSet.getUntrackedParameter<edm::InputTag>("getFromModule"));
0219     }
0220 
0221     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override {}
0222 
0223     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0224       edm::ParameterSetDescription desc;
0225       desc.addUntracked<edm::InputTag>("getFromModule");
0226       descriptions.addDefault(desc);
0227     }
0228   };
0229   //--------------------------------------------------------------------
0230   //
0231   class ConsumingStreamAnalyzer : public edm::stream::EDAnalyzer<> {
0232   public:
0233     ConsumingStreamAnalyzer(edm::ParameterSet const& iPSet)
0234         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0235           moduleLabel_(iPSet.getUntrackedParameter<std::string>("moduleLabel"), "") {
0236       mayConsume<IntProduct>(moduleLabel_);
0237     }
0238 
0239     void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0240       edm::Handle<IntProduct> handle;
0241       iEvent.getByLabel(moduleLabel_, handle);
0242       if (handle->value != value_) {
0243         throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
0244                                                << " but it was supposed to be " << value_;
0245       }
0246     }
0247 
0248   private:
0249     int value_;
0250     edm::InputTag moduleLabel_;
0251   };
0252 
0253   //--------------------------------------------------------------------
0254   //
0255   class ConsumingOneSharedResourceAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0256   public:
0257     ConsumingOneSharedResourceAnalyzer(edm::ParameterSet const& iPSet)
0258         : value_(iPSet.getUntrackedParameter<int>("valueMustMatch")),
0259           moduleLabel_(iPSet.getUntrackedParameter<edm::InputTag>("moduleLabel")) {
0260       mayConsume<IntProduct>(moduleLabel_);
0261       usesResource(iPSet.getUntrackedParameter<std::string>("resourceName"));
0262     }
0263 
0264     void analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0265       edm::Handle<IntProduct> handle;
0266       iEvent.getByLabel(moduleLabel_, handle);
0267       if (handle->value != value_) {
0268         throw cms::Exception("ValueMissMatch") << "The value for \"" << moduleLabel_ << "\" is " << handle->value
0269                                                << " but it was supposed to be " << value_;
0270       }
0271     }
0272 
0273   private:
0274     int value_;
0275     edm::InputTag moduleLabel_;
0276   };
0277 
0278   //--------------------------------------------------------------------
0279   //
0280   class SCSimpleAnalyzer : public edm::global::EDAnalyzer<> {
0281   public:
0282     SCSimpleAnalyzer(edm::ParameterSet const&) {}
0283 
0284     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
0285   };
0286 
0287   void SCSimpleAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0288     // Get the product back out; it should be sorted.
0289     edm::Handle<SCSimpleProduct> h;
0290     e.getByLabel("scs", h);
0291     assert(h.isValid());
0292 
0293     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0294     // copying all the values out of the SortedCollection so we can
0295     // manipulate them via an interface different from
0296     // SortedCollection, just so that we can make sure the collection
0297     // is sorted.
0298     std::vector<Simple> after(h->begin(), h->end());
0299     typedef std::vector<Simple>::size_type size_type;
0300 
0301     // Verify that the vector *is* sorted.
0302 
0303     for (size_type i = 1, end = after.size(); i < end; ++i) {
0304       assert(after[i - 1].id() < after[i].id());
0305     }
0306   }
0307 
0308   //--------------------------------------------------------------------
0309   //
0310   // Consumes View<Simple>
0311   //
0312   class SimpleViewAnalyzer : public edm::global::EDAnalyzer<> {
0313   public:
0314     explicit SimpleViewAnalyzer(edm::ParameterSet const& p)
0315         : token_(consumes(p.getUntrackedParameter<edm::InputTag>("label"))),
0316           size_(p.getUntrackedParameter<unsigned int>("sizeMustMatch")),
0317           checkSize_(p.getUntrackedParameter<bool>("checkSize")) {}
0318 
0319     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0320       edm::ParameterSetDescription desc;
0321       desc.addUntracked<bool>("checkSize", true);
0322       desc.addUntracked<unsigned int>("sizeMustMatch");
0323       desc.addUntracked<edm::InputTag>("label");
0324       descriptions.addDefault(desc);
0325     }
0326 
0327     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0328       if (checkSize_) {
0329         auto const& prod = e.get(token_);
0330         if (prod.size() != size_) {
0331           throw cms::Exception("SizeMismatch")
0332               << "Product size " << prod.size() << " differs from expectation " << size_;
0333         }
0334       }
0335     }
0336 
0337   private:
0338     edm::EDGetTokenT<edm::View<Simple>> const token_;
0339     unsigned int const size_;
0340     bool const checkSize_;
0341   };
0342 
0343   //--------------------------------------------------------------------
0344   //
0345   class DSVAnalyzer : public edm::global::EDAnalyzer<> {
0346   public:
0347     DSVAnalyzer(edm::ParameterSet const&) {}
0348 
0349     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const final;
0350 
0351   private:
0352     void do_sorted_stuff(edm::Event const& e) const;
0353     void do_unsorted_stuff(edm::Event const& e) const;
0354   };
0355 
0356   void DSVAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0357     do_sorted_stuff(e);
0358     do_unsorted_stuff(e);
0359   }
0360 
0361   void DSVAnalyzer::do_sorted_stuff(edm::Event const& e) const {
0362     typedef DSVSimpleProduct product_type;
0363     typedef product_type::value_type detset;
0364     typedef detset::value_type value_type;
0365     // Get the product back out; it should be sorted.
0366     edm::Handle<product_type> h;
0367     e.getByLabel("dsv1", h);
0368     assert(h.isValid());
0369 
0370     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0371     // copying all the values out of the DetSetVector's first DetSet so we can
0372     // manipulate them via an interface different from
0373     // DetSet, just so that we can make sure the collection
0374     // is sorted.
0375     std::vector<value_type> const& after = (h->end() - 1)->data;
0376     typedef std::vector<value_type>::size_type size_type;
0377 
0378     // Verify that the vector *is* sorted.
0379 
0380     for (size_type i = 1, end = after.size(); i < end; ++i) {
0381       assert(after[i - 1].data < after[i].data);
0382     }
0383   }
0384 
0385   void DSVAnalyzer::do_unsorted_stuff(edm::Event const& e) const {
0386     typedef DSVWeirdProduct product_type;
0387     typedef product_type::value_type detset;
0388     typedef detset::value_type value_type;
0389     // Get the product back out; it should be unsorted.
0390     edm::Handle<product_type> h;
0391     e.getByLabel("dsv1", h);
0392     assert(h.isValid());
0393 
0394     // Check the sorting. DO NOT DO THIS IN NORMAL CODE; we are
0395     // copying all the values out of the DetSetVector's first DetSet so we can
0396     // manipulate them via an interface different from
0397     // DetSet, just so that we can make sure the collection
0398     // is not sorted.
0399     std::vector<value_type> const& after = (h->end() - 1)->data;
0400     typedef std::vector<value_type>::size_type size_type;
0401 
0402     // Verify that the vector is reverse-sorted.
0403 
0404     for (size_type i = 1, end = after.size(); i < end; ++i) {
0405       assert(after[i - 1].data > after[i].data);
0406     }
0407   }
0408 }  // namespace edmtest
0409 
0410 using edmtest::ConsumingOneSharedResourceAnalyzer;
0411 using edmtest::ConsumingStreamAnalyzer;
0412 using edmtest::DSVAnalyzer;
0413 using edmtest::IntConsumingAnalyzer;
0414 using edmtest::IntTestAnalyzer;
0415 using edmtest::MultipleIntsAnalyzer;
0416 using edmtest::NonAnalyzer;
0417 using edmtest::SCSimpleAnalyzer;
0418 using edmtest::SimpleViewAnalyzer;
0419 DEFINE_FWK_MODULE(NonAnalyzer);
0420 DEFINE_FWK_MODULE(IntTestAnalyzer);
0421 DEFINE_FWK_MODULE(MultipleIntsAnalyzer);
0422 DEFINE_FWK_MODULE(edmtest::GenericIntsAnalyzer);
0423 DEFINE_FWK_MODULE(edmtest::GenericUInt64Analyzer);
0424 DEFINE_FWK_MODULE(IntConsumingAnalyzer);
0425 DEFINE_FWK_MODULE(edmtest::IntFromRunConsumingAnalyzer);
0426 DEFINE_FWK_MODULE(ConsumingStreamAnalyzer);
0427 DEFINE_FWK_MODULE(ConsumingOneSharedResourceAnalyzer);
0428 DEFINE_FWK_MODULE(SCSimpleAnalyzer);
0429 DEFINE_FWK_MODULE(SimpleViewAnalyzer);
0430 DEFINE_FWK_MODULE(DSVAnalyzer);