File indexing completed on 2024-04-06 12:03:12
0001 #ifndef CondTools_L1Trigger_WriterProxy_h
0002 #define CondTools_L1Trigger_WriterProxy_h
0003
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/Framework/interface/ConsumesCollector.h"
0007
0008 #include "FWCore/PluginManager/interface/PluginFactory.h"
0009
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0012
0013 #include "CondTools/L1Trigger/interface/Exception.h"
0014
0015 #include <string>
0016
0017 namespace l1t {
0018
0019
0020
0021
0022
0023 class WriterProxy {
0024 public:
0025 virtual ~WriterProxy() {}
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 virtual void setToken(edm::ConsumesCollector cc) = 0;
0037
0038 virtual std::string save(const edm::EventSetup& setup) const = 0;
0039
0040 protected:
0041 };
0042
0043
0044
0045
0046 template <class Record, class Type>
0047 class WriterProxyT : public WriterProxy {
0048 private:
0049 edm::ESGetToken<Type, Record> rcdToken;
0050
0051 public:
0052 ~WriterProxyT() override {}
0053
0054 void setToken(edm::ConsumesCollector cc) override { rcdToken = cc.esConsumes(); }
0055
0056
0057 std::string save(const edm::EventSetup& setup) const override {
0058
0059 edm::ESHandle<Type> handle;
0060
0061 try {
0062 handle = setup.getHandle(rcdToken);
0063 } catch (l1t::DataAlreadyPresentException& ex) {
0064 return std::string();
0065 }
0066
0067
0068 edm::Service<cond::service::PoolDBOutputService> poolDb;
0069 if (!poolDb.isAvailable()) {
0070 throw cond::Exception("DataWriter: PoolDBOutputService not available.");
0071 }
0072 poolDb->forceInit();
0073 cond::persistency::Session session = poolDb->session();
0074 if (not session.transaction().isActive())
0075 session.transaction().start(false);
0076
0077 std::shared_ptr<Type> pointer = std::make_shared<Type>(*(handle.product()));
0078 std::string payloadToken = session.storePayload(*pointer);
0079
0080 session.transaction().commit();
0081 return payloadToken;
0082 }
0083 };
0084
0085 typedef edmplugin::PluginFactory<l1t::WriterProxy*()> WriterFactory;
0086
0087
0088 #define REGISTER_L1_WRITER(record, type) \
0089 template class l1t::WriterProxyT<record, type>; \
0090 typedef l1t::WriterProxyT<record, type> record##_##type##_Writer; \
0091 DEFINE_EDM_PLUGIN(l1t::WriterFactory, record##_##type##_Writer, #record "@" #type "@Writer")
0092
0093 }
0094
0095 #endif