Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/Framework/interface/global/EDProducer.h"
0002 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0003 #include "FWCore/Framework/interface/global/OutputModule.h"
0004 #include "DataFormats/TestObjects/interface/TableTest.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/EventForOutput.h"
0007 #include "FWCore/Framework/interface/LuminosityBlock.h"
0008 #include "FWCore/Framework/interface/Run.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 
0011 #include <cstring>
0012 
0013 namespace {
0014   std::vector<float> doublesToFloats(std::vector<double> const& iDoubles) {
0015     std::vector<float> t;
0016     t.reserve(iDoubles.size());
0017     for (double d : iDoubles) {
0018       t.push_back(static_cast<float>(d));
0019     }
0020     return t;
0021   }
0022 }  // namespace
0023 
0024 namespace edmtest {
0025 
0026   class TableTestProducer : public edm::global::EDProducer<> {
0027   public:
0028     TableTestProducer(edm::ParameterSet const& iConfig)
0029         : anInts_(iConfig.getParameter<std::vector<int>>("anInts")),
0030           aFloats_(doublesToFloats(iConfig.getParameter<std::vector<double>>("aFloats"))),
0031           aStrings_(iConfig.getParameter<std::vector<std::string>>("aStrings")) {
0032       produces<edmtest::TableTest>();
0033     }
0034 
0035     void produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const final {
0036       iEvent.put(std::make_unique<TableTest>(anInts_, aFloats_, aStrings_));
0037     }
0038 
0039   private:
0040     const std::vector<int> anInts_;
0041     const std::vector<float> aFloats_;
0042     const std::vector<std::string> aStrings_;
0043   };
0044 
0045   class TableTestAnalyzer : public edm::global::EDAnalyzer<> {
0046   public:
0047     TableTestAnalyzer(edm::ParameterSet const& iConfig)
0048         : anInts_(iConfig.getUntrackedParameter<std::vector<int>>("anInts")),
0049           aFloats_(doublesToFloats(iConfig.getUntrackedParameter<std::vector<double>>("aFloats"))),
0050           aStrings_(iConfig.getUntrackedParameter<std::vector<std::string>>("aStrings")) {
0051       tableToken_ = consumes<edmtest::TableTest>(iConfig.getUntrackedParameter<edm::InputTag>("table"));
0052       if (anInts_.size() != aFloats_.size() or anInts_.size() != aStrings_.size()) {
0053         throw cms::Exception("Configuration") << "anInts_, aFloats_, and aStrings_ must have the same length";
0054       }
0055     }
0056 
0057     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const final {
0058       auto const& t = iEvent.get(tableToken_);
0059 
0060       auto size = t.size();
0061       if (size != anInts_.size()) {
0062         throw cms::Exception("RuntimeError")
0063             << "Table size (" << size << ") does not equal expected size (" << anInts_.size() << ")";
0064       }
0065 
0066       unsigned int index = 0;
0067       for (auto const& row : t) {
0068         if (anInts_[index] != row.get<edmtest::AnInt>()) {
0069           throw cms::Exception("RuntimeError")
0070               << "index " << index << " anInt =" << row.get<edmtest::AnInt>() << " expected " << anInts_[index];
0071         }
0072         if (aFloats_[index] != row.get<edmtest::AFloat>()) {
0073           throw cms::Exception("RuntimeError")
0074               << "index " << index << " aFloat =" << row.get<edmtest::AFloat>() << " expected " << aFloats_[index];
0075         }
0076         if (aStrings_[index] != row.get<edmtest::AString>()) {
0077           throw cms::Exception("RuntimeError")
0078               << "index " << index << " aString =" << row.get<edmtest::AString>() << " expected " << aStrings_[index];
0079         }
0080         ++index;
0081       }
0082     }
0083 
0084   private:
0085     const std::vector<int> anInts_;
0086     const std::vector<float> aFloats_;
0087     const std::vector<std::string> aStrings_;
0088     edm::EDGetTokenT<edmtest::TableTest> tableToken_;
0089   };
0090 
0091   class TableTestOutputModule : public edm::global::OutputModule<> {
0092   public:
0093     TableTestOutputModule(edm::ParameterSet const& pset)
0094         : edm::global::OutputModuleBase(pset), edm::global::OutputModule<>(pset) {}
0095 
0096     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0097       edm::ParameterSetDescription desc;
0098       desc.setComment("Tests edm::soa::Table.");
0099       OutputModule::fillDescription(desc);
0100       descriptions.add("tabletestOutput", desc);
0101     }
0102 
0103   private:
0104     void write(edm::EventForOutput const& e) override {
0105       using namespace edm;
0106       assert(!keptProducts()[InEvent].empty());
0107       for (auto product : keptProducts()[InEvent]) {
0108         BranchDescription const* branchDescription = product.first;
0109         TypeID const& tid = branchDescription->unwrappedTypeID();
0110         EDGetToken const& token = product.second;
0111         BasicHandle bh = e.getByToken(token, tid);
0112         assert(bh.isValid());
0113         auto examiner = bh.wrapper()->tableExaminer();
0114         assert(examiner);
0115         if (3 != examiner->columnDescriptions().size()) {
0116           throw cms::Exception("RuntimeError")
0117               << "wrong number of columns, expected 3 got " << examiner->columnDescriptions().size();
0118         }
0119         for (auto const& c : examiner->columnDescriptions()) {
0120           if (0 == std::strcmp(c.first, edmtest::AnInt::label())) {
0121             continue;
0122           }
0123           if (0 == std::strcmp(c.first, edmtest::AFloat::label())) {
0124             continue;
0125           }
0126           if (0 == std::strcmp(c.first, edmtest::AString::label())) {
0127             continue;
0128           }
0129           throw cms::Exception("RuntimeError") << "unknown column " << c.first;
0130         }
0131       }
0132     }
0133     void writeLuminosityBlock(edm::LuminosityBlockForOutput const&) override {}
0134     void writeRun(edm::RunForOutput const&) override {}
0135   };
0136 }  // namespace edmtest
0137 DEFINE_FWK_MODULE(edmtest::TableTestProducer);
0138 DEFINE_FWK_MODULE(edmtest::TableTestAnalyzer);
0139 DEFINE_FWK_MODULE(edmtest::TableTestOutputModule);