File indexing completed on 2023-10-25 09:38:09
0001 #ifndef PopConBTransitionSourceHandler_H
0002 #define PopConBTransitionSourceHandler_H
0003
0004 #include "CondCore/CondDB/interface/ConnectionPool.h"
0005 #include "CondCore/CondDB/interface/IOVProxy.h"
0006 #include "CondCore/PopCon/interface/PopConSourceHandler.h"
0007 #include "CondFormats/RunInfo/interface/RunInfo.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include <string>
0011
0012 namespace popcon {
0013 template <class T>
0014 class PopConBTransitionSourceHandler : public PopConSourceHandler<T> {
0015 public:
0016 PopConBTransitionSourceHandler(edm::ParameterSet const& pset)
0017 : m_run(pset.getParameter<edm::ParameterSet>("BTransition").getParameter<unsigned long long>("runNumber")),
0018 m_currentThreshold(pset.getParameter<edm::ParameterSet>("BTransition")
0019 .getUntrackedParameter<double>("currentThreshold", 18000.)),
0020 m_tagForRunInfo(
0021 pset.getParameter<edm::ParameterSet>("BTransition").getParameter<std::string>("tagForRunInfo")),
0022 m_tagForBOff(pset.getParameter<edm::ParameterSet>("BTransition").getParameter<std::string>("tagForBOff")),
0023 m_tagForBOn(pset.getParameter<edm::ParameterSet>("BTransition").getParameter<std::string>("tagForBOn")),
0024 m_connectionString(pset.getParameter<edm::ParameterSet>("BTransition").getParameter<std::string>("connect")),
0025 m_connectionPset(
0026 pset.getParameter<edm::ParameterSet>("BTransition").getParameter<edm::ParameterSet>("DBParameters")) {
0027 edm::LogInfo("PopConBTransitionSourceHandler") << "["
0028 << "PopConBTransitionSourceHandler:" << __func__ << "]: "
0029 << "Initialising Connection Pool" << std::endl;
0030 m_connection.setParameters(m_connectionPset);
0031 m_connection.configure();
0032 }
0033
0034 ~PopConBTransitionSourceHandler() override {}
0035
0036 std::string id() const final { return std::string("PopConBTransitionSourceHandler"); }
0037
0038 bool checkBOn() {
0039
0040 bool isBOn = true;
0041 cond::persistency::Session& session = PopConSourceHandler<T>::dbSession();
0042
0043 cond::persistency::TransactionScope trans(session.transaction());
0044 edm::LogInfo("PopConBTransitionSourceHandler")
0045 << "["
0046 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0047 << "Loading tag for RunInfo " << m_tagForRunInfo << " and IOV valid for run number: " << m_run << std::endl;
0048 cond::persistency::IOVProxy iov = session.readIov(m_tagForRunInfo);
0049 cond::Iov_t currentIov = iov.getInterval(m_run);
0050 LogDebug("PopConBTransitionSourceHandler")
0051 << "Loaded IOV sequence from tag " << m_tagForRunInfo << " with size: " << iov.loadedSize()
0052 << ", IOV valid for run number " << m_run << " starting from: " << currentIov.since
0053 << ", with corresponding payload hash: " << currentIov.payloadId << std::endl;
0054
0055 double current_default = -1;
0056 double avg_current = current_default;
0057 avg_current = session.fetchPayload<RunInfo>(currentIov.payloadId)->m_avg_current;
0058 LogDebug("PopConBTransitionSourceHandler")
0059 << "Comparing value of magnet current: " << avg_current << " A for run: " << m_run
0060 << " with the corresponding threshold: " << m_currentThreshold << " A." << std::endl;
0061
0062 if (avg_current != current_default && avg_current <= m_currentThreshold)
0063 isBOn = false;
0064 edm::LogInfo("PopConBTransitionSourceHandler")
0065 << "["
0066 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0067 << "The magnet was " << (isBOn ? "ON" : "OFF") << " during run " << m_run << std::endl;
0068 trans.close();
0069 return isBOn;
0070 }
0071
0072 virtual void getObjectsForBTransition(bool isBOn) {
0073
0074 cond::persistency::Session& session = PopConSourceHandler<T>::dbSession();
0075 cond::persistency::TransactionScope trans(session.transaction());
0076 edm::LogInfo("PopConBTransitionSourceHandler")
0077 << "["
0078 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0079 << "Loading tag for B " << (isBOn ? "ON" : "OFF") << ": " << (isBOn ? m_tagForBOn : m_tagForBOff)
0080 << " and IOV valid for run number: " << m_run << std::endl;
0081 cond::persistency::IOVProxy iov = session.readIov(isBOn ? m_tagForBOn : m_tagForBOff);
0082 cond::Iov_t currentIov = iov.getInterval(m_run);
0083 LogDebug("PopConBTransitionSourceHandler")
0084 << "Loaded IOV sequence from tag " << (isBOn ? m_tagForBOn : m_tagForBOff)
0085 << " with size: " << iov.loadedSize() << ", IOV valid for run number " << m_run
0086 << " starting from: " << currentIov.since << ", with corresponding payload hash: " << currentIov.payloadId
0087 << std::endl;
0088 std::string destTag = this->tagInfo().name;
0089 if (currentIov.payloadId != this->tagInfo().lastInterval.payloadId) {
0090 std::ostringstream ss;
0091 ss << "Adding iov with since " << m_run << " pointing to hash " << currentIov.payloadId
0092 << " corresponding to the calibrations for magnetic field " << (isBOn ? "ON" : "OFF");
0093 edm::LogInfo("PopConBTransitionSourceHandler")
0094 << "["
0095 << "PopConBTransitionSourceHandler::" << __func__ << "]: " << ss.str() << std::endl;
0096 cond::persistency::IOVEditor editor;
0097 if (session.existsIov(destTag)) {
0098 editor = session.editIov(destTag);
0099 } else {
0100 editor = session.createIov<T>(destTag, iov.tagInfo().timeType);
0101 editor.setDescription("Tag created by PopConBTransitionSourceHandler");
0102 }
0103 editor.insert(m_run, currentIov.payloadId);
0104 editor.flush();
0105 this->m_userTextLog = ss.str();
0106 } else {
0107 edm::LogInfo("PopConBTransitionSourceHandler")
0108 << "["
0109 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0110 << "The payload with hash " << currentIov.payloadId
0111 << " corresponding to the calibrations for magnetic field " << (isBOn ? "ON" : "OFF")
0112 << " is still valid for run " << m_run << " in the destination tag " << destTag << ".\nNo transfer needed."
0113 << std::endl;
0114 }
0115 trans.close();
0116 }
0117
0118 void getNewObjects() final {
0119
0120 edm::LogInfo("PopConBTransitionSourceHandler")
0121 << "["
0122 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0123 << "Destination Tag Info: name " << this->tagInfo().name << ", size " << this->tagInfo().size
0124 << ", last object valid since " << this->tagInfo().lastInterval.since << ", hash "
0125 << this->tagInfo().lastInterval.payloadId << std::endl;
0126
0127
0128
0129
0130 if (this->tagInfo().size > 0 && this->tagInfo().lastInterval.since >= m_run) {
0131 edm::LogInfo("PopConBTransitionSourceHandler")
0132 << "["
0133 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0134 << "last IOV " << this->tagInfo().lastInterval.since
0135 << (this->tagInfo().lastInterval.since == m_run ? " is equal to" : " is larger than")
0136 << " the run proposed for insertion " << m_run << ". No transfer needed." << std::endl;
0137 return;
0138 }
0139 bool isBOn = checkBOn();
0140 getObjectsForBTransition(isBOn);
0141 edm::LogInfo("PopConBTransitionSourceHandler") << "["
0142 << "PopConBTransitionSourceHandler::" << __func__ << "]: "
0143 << "END." << std::endl;
0144 }
0145
0146 private:
0147 unsigned long long m_run;
0148 double m_currentThreshold;
0149
0150 std::string m_tagForRunInfo;
0151
0152 std::string m_tagForBOff;
0153 std::string m_tagForBOn;
0154 std::string m_connectionString;
0155 edm::ParameterSet m_connectionPset;
0156 cond::persistency::ConnectionPool m_connection;
0157 };
0158 }
0159
0160 #endif