File indexing completed on 2024-04-06 11:59:49
0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006
0007 #include <iostream>
0008 #include <sstream>
0009
0010 #include "CondCore/CondDB/interface/ConnectionPool.h"
0011
0012 #include "CalibTracker/SiStripDCS/interface/SiStripDetVOffBuilder.h"
0013
0014 class SiStripDetVOffHandler : public edm::one::EDAnalyzer<> {
0015 public:
0016 explicit SiStripDetVOffHandler(const edm::ParameterSet& iConfig);
0017 ~SiStripDetVOffHandler() override;
0018 void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
0019 void endJob() override;
0020
0021 private:
0022 cond::persistency::ConnectionPool m_connectionPool;
0023 std::string m_condDb;
0024 std::string m_localCondDbFile;
0025 std::string m_targetTag;
0026
0027 int maxTimeBeforeNewIOV_;
0028
0029 std::vector<std::pair<SiStripDetVOff*, cond::Time_t> > newPayloads;
0030 edm::Service<SiStripDetVOffBuilder> modHVBuilder;
0031 };
0032
0033 SiStripDetVOffHandler::SiStripDetVOffHandler(const edm::ParameterSet& iConfig)
0034 : m_connectionPool(),
0035 m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
0036 m_localCondDbFile(iConfig.getParameter<std::string>("condDbFile")),
0037 m_targetTag(iConfig.getParameter<std::string>("targetTag")),
0038 maxTimeBeforeNewIOV_(iConfig.getUntrackedParameter<int>("maxTimeBeforeNewIOV", 24)) {
0039 m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0040 m_connectionPool.configure();
0041
0042 if (m_condDb.empty())
0043 m_condDb = m_localCondDbFile;
0044 }
0045
0046 SiStripDetVOffHandler::~SiStripDetVOffHandler() = default;
0047
0048 void SiStripDetVOffHandler::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0049
0050 cond::Time_t lastIov = 0;
0051 std::shared_ptr<SiStripDetVOff> lastPayload;
0052
0053 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
0054 << "Retrieve last IOV from " << m_condDb;
0055 cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDb);
0056 condDbSession.transaction().start(true);
0057 if (m_condDb.find("sqlite") == 0 && (!condDbSession.existsDatabase())) {
0058
0059 edm::LogInfo("SiStripDetVOffHandler")
0060 << "[SiStripDetVOffHandler::" << __func__ << "] "
0061 << "No information can be retrieved from " << m_condDb << " because the file is empty.\n"
0062 << "Will assume all HV/LV's are off.";
0063 } else {
0064 cond::persistency::IOVProxy iovProxy = condDbSession.readIov(m_targetTag);
0065 cond::Hash lastPayloadHash = iovProxy.getLast().payloadId;
0066 if (!lastPayloadHash.empty()) {
0067 lastPayload = condDbSession.fetchPayload<SiStripDetVOff>(lastPayloadHash);
0068 lastIov = iovProxy.getLast().since;
0069 }
0070 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
0071 << " ... last IOV: " << lastIov << " , "
0072 << "last Payload: " << lastPayloadHash;
0073 }
0074 condDbSession.transaction().commit();
0075
0076
0077 newPayloads.clear();
0078 modHVBuilder->setLastSiStripDetVOff(lastPayload.get(), lastIov);
0079 modHVBuilder->BuildDetVOffObj();
0080 newPayloads = modHVBuilder->getModulesVOff();
0081 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
0082 << "Finished building " << newPayloads.size() << " new payloads.";
0083
0084
0085 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
0086 << "Write new payloads to sqlite file " << m_localCondDbFile;
0087 cond::persistency::Session localFileSession = m_connectionPool.createSession(m_localCondDbFile, true);
0088 localFileSession.transaction().start(false);
0089 if (lastPayload && newPayloads.size() == 1 && *newPayloads[0].first == *lastPayload) {
0090
0091 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
0092 << "No HV/LV transition was found from PVSS query.";
0093 bool forceNewIOV = true;
0094 if (maxTimeBeforeNewIOV_ < 0)
0095 forceNewIOV = false;
0096 else {
0097 auto deltaT = cond::time::to_boost(newPayloads[0].second) - cond::time::to_boost(lastIov);
0098 forceNewIOV = deltaT > boost::posix_time::hours(maxTimeBeforeNewIOV_);
0099 }
0100 if (!forceNewIOV) {
0101 newPayloads.erase(newPayloads.begin());
0102 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
0103 << " ... No payload transfered.";
0104 } else {
0105 edm::LogInfo("SiStripDetVOffHandler")
0106 << "[SiStripDetVOffHandler::" << __func__ << "] "
0107 << " ... The last IOV is too old. Will start a new IOV from " << newPayloads[0].second << "("
0108 << boost::posix_time::to_simple_string(cond::time::to_boost(newPayloads[0].second))
0109 << ") with the same payload.";
0110 }
0111 }
0112
0113 cond::persistency::IOVEditor iovEditor;
0114 if (localFileSession.existsDatabase() && localFileSession.existsIov(m_targetTag)) {
0115 edm::LogWarning("SiStripDetVOffHandler")
0116 << "[SiStripDetVOffHandler::" << __func__ << "] "
0117 << "IOV of tag " << m_targetTag << " already exists in sqlite file " << m_localCondDbFile;
0118 iovEditor = localFileSession.editIov(m_targetTag);
0119 } else {
0120 iovEditor = localFileSession.createIov<SiStripDetVOff>(m_targetTag, cond::timestamp);
0121 iovEditor.setDescription("New IOV");
0122 }
0123 for (const auto& payload : newPayloads) {
0124 cond::Hash thePayloadHash = localFileSession.storePayload<SiStripDetVOff>(*payload.first);
0125 iovEditor.insert(payload.second, thePayloadHash);
0126 }
0127 iovEditor.flush();
0128 localFileSession.transaction().commit();
0129
0130 edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] " << newPayloads.size()
0131 << " payloads written to sqlite file.";
0132 }
0133
0134 void SiStripDetVOffHandler::endJob() {}
0135
0136
0137 typedef SiStripDetVOffHandler SiStripO2ODetVOff;
0138 DEFINE_FWK_MODULE(SiStripO2ODetVOff);