TestSource

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
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/InputSourceMacros.h"
#include "FWCore/Framework/interface/EventPrincipal.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Sources/interface/ProducerSourceBase.h"

#include <vector>
#include <utility>

namespace edm {
  class TestSource : public InputSource {
  public:
    explicit TestSource(ParameterSet const&, InputSourceDescription const&);
    static void fillDescriptions(ConfigurationDescriptions& descriptions);

  private:
    ItemTypeInfo getNextItemType() final;
    std::shared_ptr<RunAuxiliary> readRunAuxiliary_() final;
    std::shared_ptr<LuminosityBlockAuxiliary> readLuminosityBlockAuxiliary_() final;
    void readEvent_(EventPrincipal& eventPrincipal) final;

    std::vector<std::pair<ItemType, EventID>> m_transitions;
    std::vector<std::pair<ItemType, EventID>>::const_iterator m_nextTransition;

    static ItemType stringToType(const std::string&);
  };

  TestSource::TestSource(ParameterSet const& pset, InputSourceDescription const& desc) : InputSource(pset, desc) {
    for (auto const& p : pset.getUntrackedParameter<std::vector<edm::ParameterSet>>("transitions")) {
      m_transitions.emplace_back(stringToType(p.getUntrackedParameter<std::string>("type")),
                                 p.getUntrackedParameter<EventID>("id"));
    }
    m_nextTransition = m_transitions.begin();
  }

  TestSource::ItemType TestSource::stringToType(const std::string& iTrans) {
    if (iTrans == "IsStop") {
      return ItemType::IsStop;
    }
    if (iTrans == "IsFile") {
      return ItemType::IsFile;
    }
    if (iTrans == "IsRun") {
      return ItemType::IsRun;
    }
    if (iTrans == "IsLumi") {
      return ItemType::IsLumi;
    }
    if (iTrans == "IsEvent") {
      return ItemType::IsEvent;
    }
    if (iTrans == "IsSynchronize") {
      return ItemType::IsSynchronize;
    }

    throw edm::Exception(errors::Configuration) << "Unknown transition type \'" << iTrans << "\'";

    return ItemType::IsInvalid;
  }

  TestSource::ItemTypeInfo TestSource::getNextItemType() {
    if (m_nextTransition == m_transitions.end()) {
      return ItemType::IsStop;
    }
    auto trans = m_nextTransition->first;
    ++m_nextTransition;
    return trans;
  }

  std::shared_ptr<RunAuxiliary> TestSource::readRunAuxiliary_() {
    auto it = m_nextTransition;
    --it;
    return std::make_shared<RunAuxiliary>(it->second.run(), Timestamp(0), Timestamp(10));
  }

  std::shared_ptr<LuminosityBlockAuxiliary> TestSource::readLuminosityBlockAuxiliary_() {
    auto it = m_nextTransition;
    --it;
    return std::make_shared<LuminosityBlockAuxiliary>(
        it->second.run(), it->second.luminosityBlock(), Timestamp(0), Timestamp(10));
  }

  void TestSource::readEvent_(EventPrincipal& eventPrincipal) {
    auto it = m_nextTransition;
    --it;
    EventAuxiliary aux(it->second, processGUID(), Timestamp(0), false);
    auto history = processHistoryRegistry().getMapped(aux.processHistoryID());

    eventPrincipal.fillEventPrincipal(aux, history);
  }

  void TestSource::fillDescriptions(ConfigurationDescriptions& descriptions) {
    ParameterSetDescription desc;
    desc.setComment("Generates the specified sequence of transitions.");
    ProducerSourceBase::fillDescription(desc);

    ParameterSetDescription trans;
    trans.addUntracked<std::string>("type");
    trans.addUntracked<edm::EventID>("id");
    desc.addVPSetUntracked("transitions", trans, {{}});
    descriptions.add("source", desc);
  }
}  // namespace edm

using edm::TestSource;
DEFINE_FWK_INPUT_SOURCE(TestSource);