Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-14 11:44:44

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "CondCore/DBOutputService/interface/OnlineDBOutputService.h"
0007 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0008 
0009 #include <iostream>
0010 #include <string>
0011 #include <chrono>
0012 
0013 namespace edm {
0014   class ParameterSet;
0015   class Event;
0016   class EventSetup;
0017 }  // namespace edm
0018 
0019 // class declaration
0020 class LumiBasedUpdateAnalyzer : public edm::one::EDAnalyzer<> {
0021 public:
0022   explicit LumiBasedUpdateAnalyzer(const edm::ParameterSet& iConfig);
0023   virtual ~LumiBasedUpdateAnalyzer();
0024   virtual void beginJob();
0025   virtual void endJob();
0026   virtual void beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& context);
0027   virtual void endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& iSetup);
0028   virtual void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup);
0029 
0030 private:
0031   std::string m_record;
0032   unsigned int m_iovSize;
0033   std::string m_lumiFile;
0034   cond::Time_t m_lastLumi;
0035   unsigned int m_nLumi;
0036   int m_ret;
0037 };
0038 
0039 LumiBasedUpdateAnalyzer::LumiBasedUpdateAnalyzer(const edm::ParameterSet& iConfig)
0040     : m_record(iConfig.getUntrackedParameter<std::string>("record")),
0041       m_iovSize(iConfig.getUntrackedParameter<unsigned int>("iovSize")),
0042       m_lumiFile(iConfig.getUntrackedParameter<std::string>("lastLumiFile")),
0043       m_nLumi(0),
0044       m_ret(-2) {}
0045 
0046 LumiBasedUpdateAnalyzer::~LumiBasedUpdateAnalyzer() {}
0047 
0048 void LumiBasedUpdateAnalyzer::beginJob() {
0049   edm::Service<cond::service::OnlineDBOutputService> mydbservice;
0050   if (!mydbservice.isAvailable()) {
0051     return;
0052   }
0053   mydbservice->lockRecords();
0054 }
0055 
0056 void LumiBasedUpdateAnalyzer::endJob() {
0057   edm::Service<cond::service::OnlineDBOutputService> mydbservice;
0058   if (mydbservice.isAvailable()) {
0059     mydbservice->releaseLocks();
0060   }
0061 }
0062 
0063 void LumiBasedUpdateAnalyzer::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
0064                                                    const edm::EventSetup& context) {}
0065 
0066 void LumiBasedUpdateAnalyzer::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& iSetup) {}
0067 
0068 void LumiBasedUpdateAnalyzer::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0069   edm::Service<cond::service::OnlineDBOutputService> mydbservice;
0070   if (!mydbservice.isAvailable()) {
0071     return;
0072   }
0073   auto& lumiBlock = evt.getLuminosityBlock();
0074   unsigned int irun = lumiBlock.getRun().run();
0075   unsigned int lumiId = lumiBlock.luminosityBlock();
0076   cond::Time_t currentLumi = cond::time::lumiTime(irun, lumiId);
0077   auto& rec = mydbservice->lookUpRecord(m_record);
0078   m_ret = -1;
0079   mydbservice->logger().start();
0080   mydbservice->logger().logDebug() << "Transaction id for time " << currentLumi << " : "
0081                                    << cond::time::transactionIdForLumiTime(currentLumi, rec.m_refreshTime, "");
0082   if (currentLumi != m_lastLumi) {
0083     m_nLumi++;
0084     std::ofstream lastLumiFile(m_lumiFile, std::ofstream::trunc);
0085     lastLumiFile << currentLumi;
0086     lastLumiFile.close();
0087     m_lastLumi = currentLumi;
0088     if (m_nLumi == 3) {
0089       std::string tag = mydbservice->tag(m_record);
0090       mydbservice->logger().logDebug() << "Tag: " << tag;
0091       BeamSpotObjects mybeamspot;
0092       mybeamspot.setPosition(0.053, 0.1, 0.13);
0093       mybeamspot.setSigmaZ(3.8);
0094       mybeamspot.setType(int(lumiId));
0095       mydbservice->logger().logDebug() << "BeamType: " << mybeamspot.beamType();
0096       try {
0097         auto iov = mydbservice->writeIOVForNextLumisection(mybeamspot, m_record);
0098         if (iov) {
0099           auto utime = cond::time::unpack(iov);
0100           mydbservice->logger().logDebug() << " Run: " << irun << " Lumi: " << lumiId << " IOV lumi: " << utime.second;
0101           m_ret = 0;
0102         }
0103       } catch (const std::exception& e) {
0104         mydbservice->logger().logError() << e.what();
0105         m_ret = 1;
0106       }
0107       m_nLumi = 0;
0108     } else {
0109       mydbservice->logger().logDebug() << "Skipping lumisection " << lumiId;
0110     }
0111   }
0112   mydbservice->logger().end(m_ret);
0113 }
0114 
0115 DEFINE_FWK_MODULE(LumiBasedUpdateAnalyzer);