GlobalVectorAnalyzer

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
#include <vector>

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/global/EDAnalyzer.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"

namespace {

  template <typename T>
  T& operator<<(T& out, std::vector<double> const& values) {
    if (values.empty()) {
      out << "{}";
      return out;
    }

    auto it = values.begin();
    out << "{ " << *it;
    ++it;
    while (it != values.end()) {
      out << ", " << *it;
      ++it;
    }
    out << " }";
    return out;
  }

}  // namespace

namespace edmtest {

  class GlobalVectorAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    explicit GlobalVectorAnalyzer(edm::ParameterSet const& ps);

    void analyze(edm::StreamID sid, edm::Event const& event, edm::EventSetup const& es) const override;

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

  private:
    edm::EDGetTokenT<std::vector<double>> token_;
    std::vector<double> expected_;
  };

  GlobalVectorAnalyzer::GlobalVectorAnalyzer(edm::ParameterSet const& config)
      : token_(consumes(config.getParameter<edm::InputTag>("source"))),
        expected_(config.getParameter<std::vector<double>>("expected")) {}

  void GlobalVectorAnalyzer::analyze(edm::StreamID sid, edm::Event const& event, edm::EventSetup const& es) const {
    std::vector<double> const& values = event.get(token_);
    if (values != expected_) {
      throw cms::Exception("LogicError") << "expected values \"" << expected_ << "\"\nreceived values \"" << values
                                         << '"';
    }
  }

  void GlobalVectorAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    desc.add<edm::InputTag>("source");
    desc.add<std::vector<double>>("expected", {});
    descriptions.addDefault(desc);
  }

}  // namespace edmtest

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(edmtest::GlobalVectorAnalyzer);