Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:46

0001 #ifndef BTRANSITIONANALYZER_H
0002 #define BTRANSITIONANALYZER_H
0003 
0004 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/Framework/interface/EventSetup.h"
0007 #include "FWCore/Framework/interface/Run.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ServiceRegistry/interface/Service.h"
0010 #include "CondCore/CondDB/interface/Utils.h"
0011 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0012 #include "CondFormats/RunInfo/interface/RunInfo.h"
0013 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
0014 
0015 namespace cond {
0016   template <class T, class R>
0017   class BTransitionAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0018   public:
0019     BTransitionAnalyzer(const edm::ParameterSet& pset)
0020         : m_currentThreshold(pset.getUntrackedParameter<double>("currentThreshold", 18000.)),
0021           m_RunInfoToken(esConsumes<edm::Transition::EndRun>()),
0022           m_ESToken(esConsumes<edm::Transition::EndRun>()),
0023           m_ESTokenB0T(esConsumes<edm::Transition::EndRun>(edm::ESInputTag("", "0T"))),
0024           m_ESTokenB38T(esConsumes<edm::Transition::EndRun>(edm::ESInputTag("", "38T"))) {}
0025 #ifdef __INTEL_COMPILER
0026     virtual ~BTransitionAnalyzer() = default;
0027 #endif
0028     // implicit copy constructor
0029     // implicit assignment operator
0030     // implicit destructor
0031     void beginJob() final {}
0032     void beginRun(edm::Run const&, edm::EventSetup const&) final {}
0033     void analyze(edm::Event const&, edm::EventSetup const&) final {}
0034     void endRun(edm::Run const& run, edm::EventSetup const& eventSetup) final {
0035       edm::ESHandle<RunInfo> runInfoHandle = eventSetup.getHandle(m_RunInfoToken);
0036       edm::ESHandle<T> payloadHandle, payloadRefHandle;
0037       double avg_current = (double)runInfoHandle->m_avg_current;
0038       double current_default = -1;
0039       std::string bOnLabel = std::string("38T");
0040       std::string bOffLabel = std::string("0T");
0041       std::string bFieldLabel = bOnLabel;
0042       LogDebug("BTransitionAnalyzer") << "Comparing value of magnet current: " << avg_current
0043                                       << " A for run: " << run.run()
0044                                       << " with the corresponding threshold: " << m_currentThreshold << " A."
0045                                       << std::endl;
0046       if (avg_current != current_default && avg_current <= m_currentThreshold) {
0047         bFieldLabel = bOffLabel;
0048         payloadHandle = eventSetup.getHandle(m_ESTokenB0T);
0049       } else {
0050         payloadHandle = eventSetup.getHandle(m_ESTokenB38T);
0051       }
0052       edm::LogInfo("BTransitionAnalyzer")
0053           << "The magnet was " << (bFieldLabel == bOnLabel ? "ON" : "OFF") << " during run " << run.run()
0054           << ".\nLoading the product for the corrisponding label " << bFieldLabel << std::endl;
0055       payloadRefHandle = eventSetup.getHandle(m_ESToken);
0056       edm::Service<cond::service::PoolDBOutputService> mydbservice;
0057       if (mydbservice.isAvailable()) {
0058         if (!equalPayloads(payloadHandle, payloadRefHandle)) {
0059           edm::LogInfo("BTransitionAnalyzer")
0060               << "Exporting payload corresponding to the calibrations for magnetic field "
0061               << (bFieldLabel == bOnLabel ? "ON" : "OFF") << " starting from run number: " << run.run() << std::endl;
0062           mydbservice->writeOneIOV(*payloadHandle.product(), run.run(), demangledName(typeid(R)));
0063         } else {
0064           edm::LogInfo("BTransitionAnalyzer") << "The payload corresponding to the calibrations for magnetic field "
0065                                               << (bFieldLabel == bOnLabel ? "ON" : "OFF") << " is still valid for run "
0066                                               << run.run() << ".\nNo transfer needed." << std::endl;
0067         }
0068       } else {
0069         edm::LogError("BTransitionAnalyzer") << "PoolDBOutputService unavailable";
0070       }
0071     }
0072     void endJob() final {}
0073     virtual bool equalPayloads(edm::ESHandle<T> const& payloadHandle, edm::ESHandle<T> const& payloadRefHandle) = 0;
0074 
0075   private:
0076     double m_currentThreshold;
0077     const edm::ESGetToken<RunInfo, RunInfoRcd> m_RunInfoToken;
0078     const edm::ESGetToken<T, R> m_ESToken;
0079     const edm::ESGetToken<T, R> m_ESTokenB0T;
0080     const edm::ESGetToken<T, R> m_ESTokenB38T;
0081   };
0082 }  //namespace cond
0083 #endif  //BTRANSITIONANALYZER_H