Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 
0005 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0006 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0007 
0008 namespace edmtest {
0009   template <typename T>
0010   class TestGetByLabelAnalyzerT : public edm::global::EDAnalyzer<> {
0011   public:
0012     TestGetByLabelAnalyzerT(edm::ParameterSet const& iPSet)
0013         : src_(iPSet.getUntrackedParameter<edm::InputTag>("src")),
0014           getCategory_(iPSet.getUntrackedParameter<std::string>("getExceptionCategory")),
0015           accessCategory_(iPSet.getUntrackedParameter<std::string>("accessExceptionCategory")) {
0016       if (iPSet.getUntrackedParameter<bool>("consumes")) {
0017         consumes<T>(src_);
0018       }
0019     }
0020 
0021     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0022       edm::ParameterSetDescription desc;
0023       desc.addUntracked<edm::InputTag>("src");
0024       desc.addUntracked<std::string>("getExceptionCategory", "");
0025       desc.addUntracked<std::string>("accessExceptionCategory", "");
0026       desc.addUntracked<bool>("consumes", false);
0027       descriptions.addDefault(desc);
0028     }
0029 
0030     void analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const&) const override {
0031       edm::Handle<T> handle;
0032 
0033       auto test = [](std::string const& category, std::string const& msg, auto func) {
0034         bool caught = false;
0035         try {
0036           func();
0037         } catch (cms::Exception& e) {
0038           caught = true;
0039           if (category.empty())
0040             throw;
0041           if (e.category() != category) {
0042             throw cms::Exception("Assert")
0043                 << "Expected cms::Exception from " << msg << " with category " << category << ", got " << e.category();
0044           }
0045           return false;
0046         }
0047         if (not category.empty() and not caught) {
0048           throw cms::Exception("Assert") << "Expected cms::Exception to be thrown from " << msg << ", but got nothing";
0049         }
0050         return true;
0051       };
0052 
0053       bool noException = test(getCategory_, "getByLabel(InputTag)", [&]() { event.getByLabel(src_, handle); });
0054       if (noException) {
0055         test(accessCategory_, "*handle from InputTag", [&]() { *handle; });
0056       }
0057 
0058       noException =
0059           test(getCategory_, "getByLabel(strings)", [&]() { event.getByLabel(src_.label(), src_.instance(), handle); });
0060       if (noException) {
0061         test(accessCategory_, "*handle from strings", [&]() { *handle; });
0062       }
0063     }
0064 
0065   private:
0066     edm::InputTag const src_;
0067     std::string const getCategory_;
0068     std::string const accessCategory_;
0069   };
0070 
0071   using TestGetByLabelIntAnalyzer = TestGetByLabelAnalyzerT<edmtest::IntProduct>;
0072   using TestGetByLabelThingAnalyzer = TestGetByLabelAnalyzerT<edmtest::ThingCollection>;
0073 }  // namespace edmtest
0074 
0075 DEFINE_FWK_MODULE(edmtest::TestGetByLabelIntAnalyzer);
0076 DEFINE_FWK_MODULE(edmtest::TestGetByLabelThingAnalyzer);