DummyRunCache

TestESDummyDataAnalyzer

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
// -*- C++ -*-
//
// Package:    Framework
// Class:      TestESDummyDataAnalyzer
//
/**\class TestESDummyDataAnalyzer TestESDummyDataAnalyzer.cc FWCore/Framework/test/stubs/TestESDummyDataAnalyzer.cc

 Description: <one line class summary>

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Chris Jones
//         Created:  Thu Dec 22 11:02:00 EST 2005
//
//

// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/test/DummyData.h"

#include "FWCore/Utilities/interface/Exception.h"

namespace testesdummydata {
  struct DummyRunCache {};
}  // namespace testesdummydata

using testesdummydata::DummyRunCache;

class TestESDummyDataAnalyzer : public edm::one::EDAnalyzer<edm::RunCache<DummyRunCache>> {
public:
  explicit TestESDummyDataAnalyzer(const edm::ParameterSet&);

private:
  void endJob() override;
  std::shared_ptr<DummyRunCache> globalBeginRun(const edm::Run&, const edm::EventSetup&) const override { return {}; }
  void analyze(const edm::Event&, const edm::EventSetup&) override;
  void globalEndRun(const edm::Run&, const edm::EventSetup&) override {}

  int m_expectedValue;
  int const m_nEventsValue;
  int m_counter{};
  int m_totalCounter{};
  int const m_totalNEvents;
  // At the moment the begin run token is not used to get anything,
  // but just its existence tests the indexing used in the esConsumes
  // function call
  edm::ESGetToken<edm::eventsetup::test::DummyData, edm::DefaultRecord> const m_esTokenBeginRun;
  edm::ESGetToken<edm::eventsetup::test::DummyData, edm::DefaultRecord> const m_esToken;
};

TestESDummyDataAnalyzer::TestESDummyDataAnalyzer(const edm::ParameterSet& iConfig)
    : m_expectedValue{iConfig.getParameter<int>("expected")},
      m_nEventsValue{iConfig.getUntrackedParameter<int>("nEvents", 0)},
      m_totalNEvents{iConfig.getUntrackedParameter<int>("totalNEvents", -1)},
      m_esTokenBeginRun{esConsumes<edm::Transition::BeginRun>()},
      m_esToken{esConsumes()} {}

void TestESDummyDataAnalyzer::analyze(const edm::Event&, const edm::EventSetup& iSetup) {
  using namespace edm;

  ++m_totalCounter;
  if (m_nEventsValue) {
    ++m_counter;
    if (m_nEventsValue < m_counter) {
      ++m_expectedValue;
      m_counter = 0;
    }
  }

  ESHandle<edm::eventsetup::test::DummyData> pData = iSetup.getHandle(m_esToken);

  if (m_expectedValue != pData->value_) {
    throw cms::Exception("WrongValue") << "got value " << pData->value_ << " but expected " << m_expectedValue;
  }
}

void TestESDummyDataAnalyzer::endJob() {
  if (-1 != m_totalNEvents && m_totalNEvents != m_totalCounter) {
    throw cms::Exception("WrongNumberOfEvents")
        << "expected " << m_totalNEvents << " but instead saw " << m_totalCounter << "\n";
  }
}

DEFINE_FWK_MODULE(TestESDummyDataAnalyzer);