TableTestAnalyzer

TableTestOutputModule

TableTestProducer

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/Framework/interface/global/EDAnalyzer.h"
#include "FWCore/Framework/interface/global/OutputModule.h"
#include "DataFormats/TestObjects/interface/TableTest.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventForOutput.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include <cstring>

namespace {
  std::vector<float> doublesToFloats(std::vector<double> const& iDoubles) {
    std::vector<float> t;
    t.reserve(iDoubles.size());
    for (double d : iDoubles) {
      t.push_back(static_cast<float>(d));
    }
    return t;
  }
}  // namespace

namespace edmtest {

  class TableTestProducer : public edm::global::EDProducer<> {
  public:
    TableTestProducer(edm::ParameterSet const& iConfig)
        : anInts_(iConfig.getParameter<std::vector<int>>("anInts")),
          aFloats_(doublesToFloats(iConfig.getParameter<std::vector<double>>("aFloats"))),
          aStrings_(iConfig.getParameter<std::vector<std::string>>("aStrings")) {
      produces<edmtest::TableTest>();
    }

    void produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const final {
      iEvent.put(std::make_unique<TableTest>(anInts_, aFloats_, aStrings_));
    }

  private:
    const std::vector<int> anInts_;
    const std::vector<float> aFloats_;
    const std::vector<std::string> aStrings_;
  };

  class TableTestAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    TableTestAnalyzer(edm::ParameterSet const& iConfig)
        : anInts_(iConfig.getUntrackedParameter<std::vector<int>>("anInts")),
          aFloats_(doublesToFloats(iConfig.getUntrackedParameter<std::vector<double>>("aFloats"))),
          aStrings_(iConfig.getUntrackedParameter<std::vector<std::string>>("aStrings")) {
      tableToken_ = consumes<edmtest::TableTest>(iConfig.getUntrackedParameter<edm::InputTag>("table"));
      if (anInts_.size() != aFloats_.size() or anInts_.size() != aStrings_.size()) {
        throw cms::Exception("Configuration") << "anInts_, aFloats_, and aStrings_ must have the same length";
      }
    }

    void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const final {
      auto const& t = iEvent.get(tableToken_);

      auto size = t.size();
      if (size != anInts_.size()) {
        throw cms::Exception("RuntimeError")
            << "Table size (" << size << ") does not equal expected size (" << anInts_.size() << ")";
      }

      unsigned int index = 0;
      for (auto const& row : t) {
        if (anInts_[index] != row.get<edmtest::AnInt>()) {
          throw cms::Exception("RuntimeError")
              << "index " << index << " anInt =" << row.get<edmtest::AnInt>() << " expected " << anInts_[index];
        }
        if (aFloats_[index] != row.get<edmtest::AFloat>()) {
          throw cms::Exception("RuntimeError")
              << "index " << index << " aFloat =" << row.get<edmtest::AFloat>() << " expected " << aFloats_[index];
        }
        if (aStrings_[index] != row.get<edmtest::AString>()) {
          throw cms::Exception("RuntimeError")
              << "index " << index << " aString =" << row.get<edmtest::AString>() << " expected " << aStrings_[index];
        }
        ++index;
      }
    }

  private:
    const std::vector<int> anInts_;
    const std::vector<float> aFloats_;
    const std::vector<std::string> aStrings_;
    edm::EDGetTokenT<edmtest::TableTest> tableToken_;
  };

  class TableTestOutputModule : public edm::global::OutputModule<> {
  public:
    TableTestOutputModule(edm::ParameterSet const& pset)
        : edm::global::OutputModuleBase(pset), edm::global::OutputModule<>(pset) {}

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
      edm::ParameterSetDescription desc;
      desc.setComment("Tests edm::soa::Table.");
      OutputModule::fillDescription(desc);
      descriptions.add("tabletestOutput", desc);
    }

  private:
    void write(edm::EventForOutput const& e) override {
      using namespace edm;
      assert(!keptProducts()[InEvent].empty());
      for (auto product : keptProducts()[InEvent]) {
        ProductDescription const* productDescription = product.first;
        TypeID const& tid = productDescription->unwrappedTypeID();
        EDGetToken const& token = product.second;
        BasicHandle bh = e.getByToken(token, tid);
        assert(bh.isValid());
        auto examiner = bh.wrapper()->tableExaminer();
        assert(examiner);
        if (3 != examiner->columnDescriptions().size()) {
          throw cms::Exception("RuntimeError")
              << "wrong number of columns, expected 3 got " << examiner->columnDescriptions().size();
        }
        for (auto const& c : examiner->columnDescriptions()) {
          if (0 == std::strcmp(c.first, edmtest::AnInt::label())) {
            continue;
          }
          if (0 == std::strcmp(c.first, edmtest::AFloat::label())) {
            continue;
          }
          if (0 == std::strcmp(c.first, edmtest::AString::label())) {
            continue;
          }
          throw cms::Exception("RuntimeError") << "unknown column " << c.first;
        }
      }
    }
    void writeLuminosityBlock(edm::LuminosityBlockForOutput const&) override {}
    void writeRun(edm::RunForOutput const&) override {}
  };
}  // namespace edmtest
DEFINE_FWK_MODULE(edmtest::TableTestProducer);
DEFINE_FWK_MODULE(edmtest::TableTestAnalyzer);
DEFINE_FWK_MODULE(edmtest::TableTestOutputModule);