Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:48

0001 #include "FWCore/Sources/interface/ProducerSourceBase.h"
0002 #include "CondCore/CondDB/interface/Time.h"
0003 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0005 #include <string>
0006 #include <fstream>
0007 #include <unistd.h>
0008 namespace cond {
0009   class FileBasedEmptySource : public edm::ProducerSourceBase {
0010   public:
0011     FileBasedEmptySource(edm::ParameterSet const&, edm::InputSourceDescription const&);
0012     ~FileBasedEmptySource() override;
0013     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0014 
0015   private:
0016     void produce(edm::Event& e) override;
0017     bool setRunAndEventInfo(edm::EventID& id,
0018                             edm::TimeValue_t& time,
0019                             edm::EventAuxiliary::ExperimentType& eType) override;
0020     void initialize(edm::EventID& id, edm::TimeValue_t& time, edm::TimeValue_t& interval) override;
0021 
0022   private:
0023     unsigned int m_interval;
0024     unsigned long long m_eventId;
0025     unsigned int m_eventsPerLumi;
0026     std::string m_pathForLastLumiFile;
0027     unsigned int m_currentRun;
0028     unsigned int m_currentLumi;
0029     boost::posix_time::ptime m_currentLumiTime;
0030   };
0031 }  // namespace cond
0032 
0033 #include "FWCore/Utilities/interface/EDMException.h"
0034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0035 #include "FWCore/Framework/interface/IOVSyncValue.h"
0036 //#include "DataFormats/Provenance/interface/EventID.h"
0037 
0038 namespace cond {
0039   //allowed parameters: firstRun, firstTime, lastRun, lastTime,
0040   //common paras: timetype,interval
0041   FileBasedEmptySource::FileBasedEmptySource(edm::ParameterSet const& pset, edm::InputSourceDescription const& desc)
0042       : edm::ProducerSourceBase(pset, desc, true),
0043         m_interval(pset.getParameter<unsigned int>("interval")),
0044         m_eventId(0),
0045         m_eventsPerLumi(pset.getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock")),
0046         m_pathForLastLumiFile(pset.getParameter<std::string>("pathForLastLumiFile")),
0047         m_currentRun(0),
0048         m_currentLumi(0),
0049         m_currentLumiTime() {}
0050 
0051   FileBasedEmptySource::~FileBasedEmptySource() {}
0052 
0053   void FileBasedEmptySource::produce(edm::Event&) {}
0054 
0055   bool FileBasedEmptySource::setRunAndEventInfo(edm::EventID& id,
0056                                                 edm::TimeValue_t& time,
0057                                                 edm::EventAuxiliary::ExperimentType&) {
0058     cond::Time_t lastLumi = cond::time::MIN_VAL;
0059     {
0060       std::ifstream lastLumiFile(m_pathForLastLumiFile);
0061       if (lastLumiFile) {
0062         lastLumiFile >> lastLumi;
0063       } else {
0064         std::cout << "Error: last lumi file can't be read." << std::endl;
0065         return false;
0066       }
0067     }
0068     auto t = cond::time::unpack(lastLumi);
0069     unsigned int runId = t.first;
0070     unsigned int lumiId = t.second;
0071     //std::cout <<"###### setRunAndEventInfo Run: "<<runId<<" lumi: "<<lumiId<<std::endl;
0072     boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
0073     if (runId == m_currentRun && lumiId == m_currentLumi) {
0074       m_eventId += 1;
0075       if (m_eventId >= m_eventsPerLumi) {
0076         return false;
0077       }
0078     } else {
0079       m_currentRun = runId;
0080       m_currentLumi = lumiId;
0081       m_currentLumiTime = now;
0082       m_eventId = 1;
0083     }
0084     std::cout << "###### setRunAndEventInfo Run: " << runId << " lumi: " << lumiId << " event id: " << m_eventId
0085               << " time:" << boost::posix_time::to_simple_string(now) << std::endl;
0086     time = cond::time::from_boost(now);
0087     id = edm::EventID(runId, lumiId, m_eventId);
0088     usleep(20000);
0089     return true;
0090   }
0091 
0092   void FileBasedEmptySource::initialize(edm::EventID& id, edm::TimeValue_t& time, edm::TimeValue_t& interval) {
0093     cond::Time_t lastLumi = cond::time::MIN_VAL;
0094     {
0095       std::ifstream lastLumiFile(m_pathForLastLumiFile);
0096       if (lastLumiFile) {
0097         lastLumiFile >> lastLumi;
0098       } else {
0099         std::cout << "Error: last lumi file can't be read." << std::endl;
0100         return;
0101       }
0102     }
0103     m_eventId = 0;
0104     auto t = cond::time::unpack(lastLumi);
0105     unsigned int runId = t.first;
0106     unsigned int lumiId = t.second;
0107     std::cout << "###### initialize Run: " << runId << " lumi: " << lumiId << std::endl;
0108     m_currentRun = runId;
0109     m_currentLumi = lumiId;
0110     boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
0111     m_currentLumiTime = now;
0112     time = cond::time::from_boost(now);
0113     id = edm::EventID(runId, lumiId, m_eventId);
0114     interval = m_interval;
0115   }
0116 
0117   void FileBasedEmptySource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0118     edm::ParameterSetDescription desc;
0119     desc.setComment("Creates runs, lumis and events containing no products.");
0120     ProducerSourceBase::fillDescription(desc);
0121 
0122     //desc.add<unsigned int>("firstRunnumber")->setComment("The first run number to use");
0123     //desc.add<unsigned int>("lastRunnumber")->setComment("The last run number to use");
0124     //desc.add<unsigned int>("firstLumi")->setComment("The first lumi id to use");
0125     //desc.add<unsigned int>("lastLumi")->setComment("The last lumi id to use");
0126     //desc.add<unsigned int>("maxLumiInRun");
0127     //desc.add<std::string>("startTime");
0128     //desc.add<std::string>("endTime");
0129     desc.add<unsigned int>("interval");
0130     desc.add<unsigned int>("maxEvents");
0131     desc.add<std::string>("pathForLastLumiFile");
0132     descriptions.add("source", desc);
0133   }
0134 
0135 }  // namespace cond
0136 
0137 #include "FWCore/Framework/interface/InputSourceMacros.h"
0138 using cond::FileBasedEmptySource;
0139 
0140 DEFINE_FWK_INPUT_SOURCE(FileBasedEmptySource);