Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-08 08:15:55

0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "CondFormats/Common/interface/TimeConversions.h"
0004 #include "CondTools/BeamSpot/interface/BeamSpotOnlinePopConSourceHandler.h"
0005 
0006 #include <chrono>
0007 #include <memory>
0008 
0009 BeamSpotOnlinePopConSourceHandler::BeamSpotOnlinePopConSourceHandler(edm::ParameterSet const& pset)
0010     : m_debug(pset.getUntrackedParameter<bool>("debug", false)),
0011       m_name(pset.getUntrackedParameter<std::string>("name", "BeamSpotOnlineSourceHandler")),
0012       m_maxAge(pset.getUntrackedParameter<unsigned int>("maxAge", 86400)),
0013       m_runNumber(pset.getUntrackedParameter<unsigned int>("runNumber", 1)),
0014       m_sourcePayloadTag(pset.getUntrackedParameter<std::string>("sourcePayloadTag", "")) {}
0015 
0016 BeamSpotOnlinePopConSourceHandler::~BeamSpotOnlinePopConSourceHandler() {}
0017 
0018 bool checkPayloadAge(const BeamSpotOnlineObjects& payload, unsigned int maxAge) {
0019   long creationTimeInSeconds = payload.creationTime() >> 32;
0020   auto timeNow = std::chrono::system_clock::now();
0021   auto nowSinceEpoch = std::chrono::duration_cast<std::chrono::seconds>(timeNow.time_since_epoch()).count();
0022   long age = nowSinceEpoch - creationTimeInSeconds;
0023   return age < maxAge;
0024 }
0025 
0026 std::unique_ptr<BeamSpotOnlineObjects> makeDummyPayload() {
0027   // implement here
0028   std::unique_ptr<BeamSpotOnlineObjects> ret;
0029   ret = std::make_unique<BeamSpotOnlineObjects>();
0030   auto timeNow = std::chrono::system_clock::now();
0031   auto nowSinceEpoch = std::chrono::duration_cast<std::chrono::seconds>(timeNow.time_since_epoch()).count();
0032   ret->setCreationTime(nowSinceEpoch << 32);
0033   return ret;
0034 }
0035 
0036 void BeamSpotOnlinePopConSourceHandler::getNewObjects() {
0037   bool addNewPayload = false;
0038   if (!tagInfo().size) {
0039     edm::LogInfo(m_name) << "New tag " << tagInfo().name << "; from " << m_name << "::getNewObjects";
0040     addNewPayload = true;
0041   } else {
0042     edm::LogInfo(m_name) << "got info for tag " << tagInfo().name << ", last object valid since "
0043                          << tagInfo().lastInterval.since << "; from " << m_name << "::getNewObjects";
0044     if (!checkPayloadAge(*lastPayload(), m_maxAge)) {
0045       addNewPayload = true;
0046     }
0047   }
0048 
0049   if (addNewPayload) {
0050     if (!m_sourcePayloadTag.empty()) {
0051       edm::LogInfo(m_name) << "Reading target payload from tag " << m_sourcePayloadTag;
0052       auto session = dbSession();
0053       session.transaction().start(true);
0054       auto lastIov = session.readIov(m_sourcePayloadTag).getLast();
0055       m_payload = session.fetchPayload<BeamSpotOnlineObjects>(lastIov.payloadId);
0056       session.transaction().commit();
0057     } else {
0058       m_payload = makeDummyPayload();
0059     }
0060 
0061     cond::Time_t targetTime = cond::time::lumiTime(m_runNumber, 1);
0062     m_to_transfer.push_back(std::make_pair(m_payload.get(), targetTime));
0063 
0064     edm::LogInfo(m_name) << "Payload added with IOV since " << targetTime;
0065   } else {
0066     edm::LogInfo(m_name) << "Nothing to do, last payload satisfies maximum age requirement.";
0067   }
0068 }
0069 
0070 std::string BeamSpotOnlinePopConSourceHandler::id() const { return m_name; }