File indexing completed on 2024-04-06 12:03:12
0001 #ifndef CondTools_L1Trigger_L1ConfigOnlineProdBase_h
0002 #define CondTools_L1Trigger_L1ConfigOnlineProdBase_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <memory>
0026
0027
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029
0030 #include "FWCore/Framework/interface/ModuleFactory.h"
0031 #include "FWCore/Framework/interface/ESProducer.h"
0032 #include "FWCore/Framework/interface/ESHandle.h"
0033
0034 #include "CondFormats/L1TObjects/interface/L1TriggerKeyList.h"
0035 #include "CondFormats/DataRecord/interface/L1TriggerKeyListRcd.h"
0036 #include "CondFormats/L1TObjects/interface/L1TriggerKey.h"
0037 #include "CondFormats/DataRecord/interface/L1TriggerKeyRcd.h"
0038
0039 #include "CondTools/L1Trigger/interface/OMDSReader.h"
0040 #include "CondTools/L1Trigger/interface/DataWriter.h"
0041 #include "CondTools/L1Trigger/interface/Exception.h"
0042
0043 #include "FWCore/Utilities/interface/typelookup.h"
0044 #include "FWCore/Framework/interface/EventSetup.h"
0045
0046 #include "CondCore/CondDB/interface/Session.h"
0047 #include "CondCore/CondDB/interface/ConnectionPool.h"
0048
0049 #include <optional>
0050
0051
0052 template <class TRcd, class TData>
0053 class L1ConfigOnlineProdBase : public edm::ESProducer {
0054 public:
0055 L1ConfigOnlineProdBase(const edm::ParameterSet&);
0056 ~L1ConfigOnlineProdBase() override;
0057
0058 virtual std::unique_ptr<TData> produce(const TRcd& iRecord);
0059
0060 virtual std::unique_ptr<TData> newObject(const std::string& objectKey) = 0;
0061
0062 private:
0063
0064 edm::ESGetToken<L1TriggerKeyList, TRcd> l1TriggerKeyListToken_;
0065 edm::ESGetToken<L1TriggerKey, TRcd> l1TriggerKeyToken_;
0066
0067 protected:
0068 l1t::OMDSReader m_omdsReader;
0069 std::optional<edm::ESConsumesCollectorT<TRcd>> m_consumesCollector;
0070 bool m_forceGeneration;
0071
0072
0073
0074
0075
0076 bool getObjectKey(const TRcd& record, std::string& objectKey);
0077
0078
0079 cond::persistency::Session m_dbSession;
0080 bool m_copyFromCondDB;
0081 };
0082
0083 template <class TRcd, class TData>
0084 L1ConfigOnlineProdBase<TRcd, TData>::L1ConfigOnlineProdBase(const edm::ParameterSet& iConfig)
0085 : m_omdsReader(),
0086 m_forceGeneration(iConfig.getParameter<bool>("forceGeneration")),
0087 m_dbSession(),
0088 m_copyFromCondDB(false) {
0089
0090
0091 auto cc = setWhatProduced(this);
0092
0093
0094 l1TriggerKeyListToken_ = cc.consumes();
0095 l1TriggerKeyToken_ = cc.consumes();
0096
0097 m_consumesCollector = std::move(cc);
0098
0099 if (iConfig.exists("copyFromCondDB")) {
0100 m_copyFromCondDB = iConfig.getParameter<bool>("copyFromCondDB");
0101
0102 if (m_copyFromCondDB) {
0103 cond::persistency::ConnectionPool connectionPool;
0104
0105 connectionPool.setAuthenticationPath(iConfig.getParameter<std::string>("onlineAuthentication"));
0106 connectionPool.configure();
0107 m_dbSession = connectionPool.createSession(iConfig.getParameter<std::string>("onlineDB"));
0108 }
0109 } else {
0110 m_omdsReader.connect(iConfig.getParameter<std::string>("onlineDB"),
0111 iConfig.getParameter<std::string>("onlineAuthentication"));
0112 }
0113 }
0114
0115 template <class TRcd, class TData>
0116 L1ConfigOnlineProdBase<TRcd, TData>::~L1ConfigOnlineProdBase() {
0117
0118
0119 }
0120
0121 template <class TRcd, class TData>
0122 std::unique_ptr<TData> L1ConfigOnlineProdBase<TRcd, TData>::produce(const TRcd& iRecord) {
0123 std::unique_ptr<TData> pData;
0124
0125
0126 std::string key;
0127 if (getObjectKey(iRecord, key) || m_forceGeneration) {
0128 if (m_copyFromCondDB) {
0129 auto keyList = iRecord.getHandle(l1TriggerKeyListToken_);
0130
0131
0132 std::string recordName = edm::typelookup::className<TRcd>();
0133 std::string dataType = edm::typelookup::className<TData>();
0134 std::string payloadToken = keyList->token(recordName, dataType, key);
0135
0136 edm::LogVerbatim("L1-O2O") << "Copying payload for " << recordName << "@" << dataType << " obj key " << key
0137 << " from CondDB.";
0138 edm::LogVerbatim("L1-O2O") << "TOKEN " << payloadToken;
0139
0140
0141
0142 if (!payloadToken.empty()) {
0143 m_dbSession.transaction().start();
0144 pData = m_dbSession.fetchPayload<TData>(payloadToken);
0145 m_dbSession.transaction().commit();
0146 }
0147 } else {
0148 pData = newObject(key);
0149 }
0150
0151
0152 if (pData == std::unique_ptr<TData>()) {
0153 std::string dataType = edm::typelookup::className<TData>();
0154
0155 throw l1t::DataInvalidException("Unable to generate " + dataType + " for key " + key + ".");
0156 }
0157 } else {
0158 std::string dataType = edm::typelookup::className<TData>();
0159
0160 throw l1t::DataAlreadyPresentException(dataType + " for key " + key + " already in CondDB.");
0161 }
0162
0163 return pData;
0164 }
0165
0166 template <class TRcd, class TData>
0167 bool L1ConfigOnlineProdBase<TRcd, TData>::getObjectKey(const TRcd& iRecord, std::string& objectKey) {
0168
0169
0170
0171
0172
0173
0174 edm::ESHandle<L1TriggerKey> key;
0175 try {
0176 key = iRecord.getHandle(l1TriggerKeyToken_);
0177 } catch (l1t::DataAlreadyPresentException& ex) {
0178 objectKey = std::string();
0179 return false;
0180 }
0181
0182
0183 std::string recordName = edm::typelookup::className<TRcd>();
0184 std::string dataType = edm::typelookup::className<TData>();
0185
0186 objectKey = key->get(recordName, dataType);
0187
0188
0189 L1TriggerKeyList keyList;
0190 l1t::DataWriter dataWriter;
0191 if (!dataWriter.fillLastTriggerKeyList(keyList)) {
0192 edm::LogError("L1-O2O") << "Problem getting last L1TriggerKeyList";
0193 }
0194
0195
0196 return keyList.token(recordName, dataType, objectKey).empty();
0197 }
0198
0199 #endif