Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef CalibTracker_SiStripESProducer_DummyCondDBWriter_h
0002 #define CalibTracker_SiStripESProducer_DummyCondDBWriter_h
0003 
0004 // user include files
0005 #include <string>
0006 
0007 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0008 #include "FWCore/Framework/interface/ESWatcher.h"
0009 #include "FWCore/Framework/interface/EventSetup.h"
0010 #include "FWCore/Framework/interface/Run.h"
0011 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/ServiceRegistry/interface/Service.h"
0015 #include "FWCore/Utilities/interface/Exception.h"
0016 
0017 template <typename TObject, typename TObjectO, typename TRecord>
0018 class DummyCondDBWriter : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0019 public:
0020   explicit DummyCondDBWriter(const edm::ParameterSet& iConfig);
0021   ~DummyCondDBWriter() override;
0022   void analyze(const edm::Event& e, const edm::EventSetup& es) override {}
0023   void beginRun(const edm::Run& run, const edm::EventSetup& es) override {}
0024   void endRun(const edm::Run& run, const edm::EventSetup& es) override;
0025 
0026 private:
0027   edm::ParameterSet iConfig_;
0028   edm::ESWatcher<TRecord> watcher_;
0029   edm::ESGetToken<TObject, TRecord> token_;
0030 };
0031 
0032 template <typename TObject, typename TObjectO, typename TRecord>
0033 DummyCondDBWriter<TObject, TObjectO, TRecord>::DummyCondDBWriter(const edm::ParameterSet& iConfig)
0034     : iConfig_(iConfig),
0035       token_(esConsumes<edm::Transition::EndRun>(
0036           edm::ESInputTag{"", iConfig.getUntrackedParameter<std::string>("label", "")})) {
0037   usesResource(cond::service::PoolDBOutputService::kSharedResource);
0038   edm::LogInfo("DummyCondDBWriter") << "DummyCondDBWriter constructor for typename " << typeid(TObject).name()
0039                                     << " and record " << typeid(TRecord).name();
0040 }
0041 
0042 template <typename TObject, typename TObjectO, typename TRecord>
0043 DummyCondDBWriter<TObject, TObjectO, TRecord>::~DummyCondDBWriter() {
0044   edm::LogInfo("DummyCondDBWriter") << "DummyCondDBWriter::~DummyCondDBWriter()";
0045 }
0046 
0047 template <typename TObject, typename TObjectO, typename TRecord>
0048 void DummyCondDBWriter<TObject, TObjectO, TRecord>::endRun(const edm::Run& run, const edm::EventSetup& es) {
0049   std::string rcdName = iConfig_.getParameter<std::string>("record");
0050 
0051   if (!watcher_.check(es)) {
0052     edm::LogInfo("DummyCondDBWriter") << "Not needed to store objects with Record " << rcdName << " at run "
0053                                       << run.run();
0054     return;
0055   }
0056 
0057   auto obj = std::make_unique<TObjectO>(es.getData(token_));
0058   cond::Time_t Time_;
0059 
0060   //And now write  data in DB
0061   edm::Service<cond::service::PoolDBOutputService> dbservice;
0062   if (dbservice.isAvailable()) {
0063     std::string openIovAt = iConfig_.getUntrackedParameter<std::string>("OpenIovAt", "beginOfTime");
0064     if (openIovAt == "beginOfTime")
0065       Time_ = dbservice->beginOfTime();
0066     else if (openIovAt == "currentTime")
0067       Time_ = dbservice->currentTime();
0068     else
0069       Time_ = iConfig_.getUntrackedParameter<uint32_t>("OpenIovAtTime", 1);
0070 
0071     dbservice->writeOneIOV(*obj, Time_, rcdName);
0072   } else {
0073     edm::LogError("DummyCondDBWriter") << "Service is unavailable";
0074   }
0075 }
0076 
0077 #endif