FileBasedEmptySource

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 140
#include "FWCore/Sources/interface/ProducerSourceBase.h"
#include "CondCore/CondDB/interface/Time.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include <string>
#include <fstream>
#include <unistd.h>
namespace cond {
  class FileBasedEmptySource : public edm::ProducerSourceBase {
  public:
    FileBasedEmptySource(edm::ParameterSet const&, edm::InputSourceDescription const&);
    ~FileBasedEmptySource() override;
    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

  private:
    void produce(edm::Event& e) override;
    bool setRunAndEventInfo(edm::EventID& id,
                            edm::TimeValue_t& time,
                            edm::EventAuxiliary::ExperimentType& eType) override;
    void initialize(edm::EventID& id, edm::TimeValue_t& time, edm::TimeValue_t& interval) override;

  private:
    unsigned int m_interval;
    unsigned long long m_eventId;
    unsigned int m_eventsPerLumi;
    std::string m_pathForLastLumiFile;
    unsigned int m_currentRun;
    unsigned int m_currentLumi;
    boost::posix_time::ptime m_currentLumiTime;
  };
}  // namespace cond

#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/IOVSyncValue.h"
//#include "DataFormats/Provenance/interface/EventID.h"

namespace cond {
  //allowed parameters: firstRun, firstTime, lastRun, lastTime,
  //common paras: timetype,interval
  FileBasedEmptySource::FileBasedEmptySource(edm::ParameterSet const& pset, edm::InputSourceDescription const& desc)
      : edm::ProducerSourceBase(pset, desc, true),
        m_interval(pset.getParameter<unsigned int>("interval")),
        m_eventId(0),
        m_eventsPerLumi(pset.getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock")),
        m_pathForLastLumiFile(pset.getParameter<std::string>("pathForLastLumiFile")),
        m_currentRun(0),
        m_currentLumi(0),
        m_currentLumiTime() {}

  FileBasedEmptySource::~FileBasedEmptySource() {}

  void FileBasedEmptySource::produce(edm::Event&) {}

  bool FileBasedEmptySource::setRunAndEventInfo(edm::EventID& id,
                                                edm::TimeValue_t& time,
                                                edm::EventAuxiliary::ExperimentType&) {
    cond::Time_t lastLumi = cond::time::MIN_VAL;
    {
      std::ifstream lastLumiFile(m_pathForLastLumiFile);
      if (lastLumiFile) {
        lastLumiFile >> lastLumi;
      } else {
        std::cout << "Error: last lumi file can't be read." << std::endl;
        return false;
      }
    }
    auto t = cond::time::unpack(lastLumi);
    unsigned int runId = t.first;
    unsigned int lumiId = t.second;
    //std::cout <<"###### setRunAndEventInfo Run: "<<runId<<" lumi: "<<lumiId<<std::endl;
    boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
    if (runId == m_currentRun && lumiId == m_currentLumi) {
      m_eventId += 1;
      if (m_eventId >= m_eventsPerLumi) {
        return false;
      }
    } else {
      m_currentRun = runId;
      m_currentLumi = lumiId;
      m_currentLumiTime = now;
      m_eventId = 1;
    }
    std::cout << "###### setRunAndEventInfo Run: " << runId << " lumi: " << lumiId << " event id: " << m_eventId
              << " time:" << boost::posix_time::to_simple_string(now) << std::endl;
    time = cond::time::from_boost(now);
    id = edm::EventID(runId, lumiId, m_eventId);
    usleep(20000);
    return true;
  }

  void FileBasedEmptySource::initialize(edm::EventID& id, edm::TimeValue_t& time, edm::TimeValue_t& interval) {
    cond::Time_t lastLumi = cond::time::MIN_VAL;
    {
      std::ifstream lastLumiFile(m_pathForLastLumiFile);
      if (lastLumiFile) {
        lastLumiFile >> lastLumi;
      } else {
        std::cout << "Error: last lumi file can't be read." << std::endl;
        return;
      }
    }
    m_eventId = 0;
    auto t = cond::time::unpack(lastLumi);
    unsigned int runId = t.first;
    unsigned int lumiId = t.second;
    std::cout << "###### initialize Run: " << runId << " lumi: " << lumiId << std::endl;
    m_currentRun = runId;
    m_currentLumi = lumiId;
    boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
    m_currentLumiTime = now;
    time = cond::time::from_boost(now);
    id = edm::EventID(runId, lumiId, m_eventId);
    interval = m_interval;
  }

  void FileBasedEmptySource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    desc.setComment("Creates runs, lumis and events containing no products.");
    ProducerSourceBase::fillDescription(desc);

    //desc.add<unsigned int>("firstRunnumber")->setComment("The first run number to use");
    //desc.add<unsigned int>("lastRunnumber")->setComment("The last run number to use");
    //desc.add<unsigned int>("firstLumi")->setComment("The first lumi id to use");
    //desc.add<unsigned int>("lastLumi")->setComment("The last lumi id to use");
    //desc.add<unsigned int>("maxLumiInRun");
    //desc.add<std::string>("startTime");
    //desc.add<std::string>("endTime");
    desc.add<unsigned int>("interval");
    desc.add<unsigned int>("maxEvents");
    desc.add<std::string>("pathForLastLumiFile");
    descriptions.add("source", desc);
  }

}  // namespace cond

#include "FWCore/Framework/interface/InputSourceMacros.h"
using cond::FileBasedEmptySource;

DEFINE_FWK_INPUT_SOURCE(FileBasedEmptySource);