Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:52

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 PopConESTransitionSourceHandler : public PopConSourceHandler<T> {
0015   public:
0016     PopConESTransitionSourceHandler(edm::ParameterSet const& pset)
0017         : m_run(pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<unsigned long long>("runNumber")),
0018           //      m_currentThreshold( pset.getParameter<edm::ParameterSet>( "ESTransition" ).getUntrackedParameter<double>( "currentThreshold", 18000. ) ),
0019           m_tagForRunInfo(
0020               pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<std::string>("tagForRunInfo")),
0021           m_ESGain(pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<std::string>("ESGain")),
0022           m_tagForLowGain(
0023               pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<std::string>("ESLowGainTag")),
0024           m_tagForHighGain(
0025               pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<std::string>("ESHighGainTag")),
0026           m_connectionString(pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<std::string>("connect")),
0027           m_connectionPset(
0028               pset.getParameter<edm::ParameterSet>("ESTransition").getParameter<edm::ParameterSet>("DBParameters")) {
0029       edm::LogInfo("PopConESTransitionSourceHandler") << "["
0030                                                       << "PopConESTransitionSourceHandler:" << __func__ << "]: "
0031                                                       << "Initialising Connection Pool" << std::endl;
0032       m_connection.setParameters(m_connectionPset);
0033       m_connection.configure();
0034     }
0035 
0036     ~PopConESTransitionSourceHandler() override {}
0037 
0038     std::string id() const final { return std::string("PopConESTransitionSourceHandler"); }
0039 
0040     bool checkLowGain() {
0041       //the output boolean is set to true as default
0042       bool isLowGain = true;
0043       cond::persistency::Session& session = PopConSourceHandler<T>::dbSession();
0044       //reading RunInfo from Conditions
0045       cond::persistency::TransactionScope trans(session.transaction());
0046       edm::LogInfo("PopConESTransitionSourceHandler")
0047           << "["
0048           << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0049           << "Loading tag for RunInfo " << m_tagForRunInfo << " and IOV valid for run number: " << m_run << std::endl;
0050       cond::persistency::IOVProxy iov = session.readIov(m_tagForRunInfo);
0051       cond::Iov_t currentIov = iov.getInterval(m_run);
0052       LogDebug("PopConESTransitionSourceHandler")
0053           << "Loaded IOV sequence from tag " << m_tagForRunInfo << " with size: " << iov.loadedSize()
0054           << ", IOV valid for run number " << m_run << " starting from: " << currentIov.since
0055           << ", with corresponding payload hash: " << currentIov.payloadId << std::endl;
0056       /*    accessing the average magnet current for the run
0057             double current_default = -1;
0058            double avg_current = current_default;
0059            avg_current = session.fetchPayload<RunInfo>( currentIov.payloadId )->m_avg_current;
0060             LogDebug( "PopConESTransitionSourceHandler" ) << "Comparing value of magnet current: " << avg_current << " A for run: " << m_run
0061                                                    << " with the corresponding threshold: "<< m_currentThreshold << " A." << std::endl;
0062       comparing the magnet current with the user defined threshold
0063       if( avg_current != current_default && avg_current <= m_currentThreshold ) isLowGain = false;
0064       edm::LogInfo( "PopConESTransitionSourceHandler" ) << "[" << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0065                                                        << "The magnet was " << ( isBOn ? "ON" : "OFF" )
0066                                << " during run " << m_run << std::endl;   */
0067       if (m_ESGain == "HIGH") {
0068         isLowGain = false;
0069       } else {
0070         isLowGain = true;
0071       }
0072       trans.close();
0073       return isLowGain;
0074     }
0075 
0076     virtual void getObjectsForESTransition(bool isLowGain) {
0077       //reading payloads for 0T and 3.8T from Conditions
0078       cond::persistency::Session& session = PopConSourceHandler<T>::dbSession();
0079       cond::persistency::TransactionScope trans(session.transaction());
0080       edm::LogInfo("PopConESTransitionSourceHandler") << "["
0081                                                       << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0082                                                       << "Loading tag for ES " << (isLowGain ? "LowGain" : "HighGain")
0083                                                       << ": " << (isLowGain ? m_tagForLowGain : m_tagForHighGain)
0084                                                       << " and IOV valid for run number: " << m_run << std::endl;
0085       cond::persistency::IOVProxy iov = session.readIov(isLowGain ? m_tagForLowGain : m_tagForHighGain);
0086       cond::Iov_t currentIov = iov.getInterval(m_run);
0087       LogDebug("PopConESTransitionSourceHandler")
0088           << "Loaded IOV sequence from tag " << (isLowGain ? m_tagForLowGain : m_tagForHighGain)
0089           << " with size: " << iov.loadedSize() << ", IOV valid for run number " << m_run
0090           << " starting from: " << currentIov.since << ", with corresponding payload hash: " << currentIov.payloadId
0091           << std::endl;
0092       std::string destTag = this->tagInfo().name;
0093       if (currentIov.payloadId != this->tagInfo().lastInterval.payloadId) {
0094         std::ostringstream ss;
0095         ss << "Adding iov with since " << m_run << " pointing to hash " << currentIov.payloadId
0096            << " corresponding to the ES Gain " << (isLowGain ? "LOW" : "HIGH");
0097         edm::LogInfo("PopConESTransitionSourceHandler")
0098             << "["
0099             << "PopConESTransitionSourceHandler::" << __func__ << "]: " << ss.str() << std::endl;
0100         cond::persistency::IOVEditor editor;
0101         if (session.existsIov(destTag)) {
0102           editor = session.editIov(destTag);
0103         } else {
0104           editor = session.createIov<T>(destTag, iov.tagInfo().timeType);
0105           editor.setDescription("Tag created by PopConESTransitionSourceHandler");
0106         }
0107         editor.insert(m_run, currentIov.payloadId);
0108         editor.flush();
0109         this->m_userTextLog = ss.str();
0110       } else {
0111         edm::LogInfo("PopConESTransitionSourceHandler")
0112             << "["
0113             << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0114             << "The payload with hash " << currentIov.payloadId << " corresponding to ES Gain"
0115             << (isLowGain ? "LOW" : "HIGH") << " is still valid for run " << m_run << " in the destination tag "
0116             << destTag << ".\nNo transfer needed." << std::endl;
0117       }
0118       trans.close();
0119     }
0120 
0121     void getNewObjects() final {
0122       //check whats already inside of database
0123       edm::LogInfo("PopConESTransitionSourceHandler")
0124           << "["
0125           << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0126           << "Destination Tag Info: name " << this->tagInfo().name << ", size " << this->tagInfo().size
0127           << ", last object valid since " << this->tagInfo().lastInterval.since << ", hash "
0128           << this->tagInfo().lastInterval.payloadId << std::endl;
0129       //check if a transfer is needed:
0130       //if the new run number is smaller than or equal to the latest IOV, exit.
0131       //This is needed as now the IOV Editor does not always protect for insertions:
0132       //ANY and VALIDATION sychronizations are allowed to write in the past.
0133       if (this->tagInfo().size > 0 && this->tagInfo().lastInterval.since >= m_run) {
0134         edm::LogInfo("PopConESTransitionSourceHandler")
0135             << "["
0136             << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0137             << "last IOV " << this->tagInfo().lastInterval.since
0138             << (this->tagInfo().lastInterval.since == m_run ? " is equal to" : " is larger than")
0139             << " the run proposed for insertion " << m_run << ". No transfer needed." << std::endl;
0140         return;
0141       }
0142       bool isLowGain = checkLowGain();
0143       getObjectsForESTransition(isLowGain);
0144       edm::LogInfo("PopConESTransitionSourceHandler") << "["
0145                                                       << "PopConESTransitionSourceHandler::" << __func__ << "]: "
0146                                                       << "END." << std::endl;
0147     }
0148 
0149   private:
0150     unsigned long long m_run;
0151     double m_currentThreshold;
0152     // for reading from CondDB the current from RunInfo
0153     std::string m_tagForRunInfo;
0154     // for reading from CondDB the Low Gain and High Gain payloads
0155     std::string m_ESGain;
0156     std::string m_tagForLowGain;
0157     std::string m_tagForHighGain;
0158     std::string m_connectionString;
0159     edm::ParameterSet m_connectionPset;
0160     cond::persistency::ConnectionPool m_connection;
0161   };
0162 }  //namespace popcon
0163 
0164 #endif  //PopConESTransitionSourceHandler_H