File indexing completed on 2024-04-06 12:03:14
0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002
0003 #include "CondFormats/L1TObjects/interface/L1TriggerKeyExt.h"
0004 #include "CondFormats/DataRecord/interface/L1TriggerKeyExtRcd.h"
0005 #include "CondFormats/L1TObjects/interface/L1TriggerKeyListExt.h"
0006 #include "CondFormats/DataRecord/interface/L1TriggerKeyListExtRcd.h"
0007
0008 #include <memory>
0009
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/MakerMacros.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/Utilities/interface/ESGetToken.h"
0016 #include "CondTools/L1TriggerExt/interface/DataWriterExt.h"
0017
0018 using DataWriterExtPtr = std::unique_ptr<l1t::DataWriterExt>;
0019 using RecordToWriterMap = std::map<std::string, DataWriterExtPtr>;
0020
0021 class L1CondDBPayloadWriterExt : public edm::one::EDAnalyzer<> {
0022 public:
0023 explicit L1CondDBPayloadWriterExt(const edm::ParameterSet&);
0024 ~L1CondDBPayloadWriterExt() override;
0025
0026 private:
0027 void beginJob() override;
0028 void analyze(const edm::Event&, const edm::EventSetup&) override;
0029 void endJob() override;
0030
0031
0032 RecordToWriterMap m_rcdToWriterMap;
0033
0034
0035
0036 bool m_writeL1TriggerKeyExt;
0037
0038
0039 bool m_writeConfigData;
0040
0041
0042 bool m_overwriteKeys;
0043
0044 bool m_logTransactions;
0045
0046
0047 bool m_newL1TriggerKeyListExt;
0048
0049
0050 edm::ESGetToken<L1TriggerKeyExt, L1TriggerKeyExtRcd> theL1TriggerKeyExtToken_;
0051 };
0052
0053 L1CondDBPayloadWriterExt::L1CondDBPayloadWriterExt(const edm::ParameterSet& iConfig)
0054 : m_writeL1TriggerKeyExt(iConfig.getParameter<bool>("writeL1TriggerKeyExt")),
0055 m_writeConfigData(iConfig.getParameter<bool>("writeConfigData")),
0056 m_overwriteKeys(iConfig.getParameter<bool>("overwriteKeys")),
0057 m_logTransactions(iConfig.getParameter<bool>("logTransactions")),
0058 m_newL1TriggerKeyListExt(iConfig.getParameter<bool>("newL1TriggerKeyListExt")),
0059 theL1TriggerKeyExtToken_(esConsumes()) {
0060 auto cc = consumesCollector();
0061 for (const auto& sysWriter : iConfig.getParameter<std::vector<std::string>>("sysWriters")) {
0062
0063 DataWriterExtPtr writer = std::make_unique<l1t::DataWriterExt>(sysWriter);
0064 writer->getWriter()->setToken(cc);
0065 m_rcdToWriterMap[sysWriter] = std::move(writer);
0066 }
0067 }
0068
0069 L1CondDBPayloadWriterExt::~L1CondDBPayloadWriterExt() {
0070
0071
0072 }
0073
0074
0075 void L1CondDBPayloadWriterExt::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0076 using namespace edm;
0077
0078
0079 L1TriggerKeyListExt oldKeyList;
0080 l1t::DataWriterExt& m_writer = *m_rcdToWriterMap.at("L1TriggerKeyExtRcd@L1TriggerKeyExt");
0081
0082 if (not(m_newL1TriggerKeyListExt or m_writer.fillLastTriggerKeyList(oldKeyList)))
0083 edm::LogError("L1-O2O") << "Problem getting last L1TriggerKeyListExt";
0084
0085 L1TriggerKeyListExt* keyList = nullptr;
0086
0087
0088 std::string token;
0089 L1TriggerKeyExt key;
0090
0091
0092
0093 bool triggerKeyOK = true;
0094 try {
0095
0096 key = iSetup.getData(theL1TriggerKeyExtToken_);
0097 if (!m_overwriteKeys) {
0098 triggerKeyOK = oldKeyList.token(key.tscKey()).empty();
0099 }
0100 } catch (l1t::DataAlreadyPresentException& ex) {
0101 triggerKeyOK = false;
0102 edm::LogVerbatim("L1-O2O") << ex.what();
0103 }
0104
0105 if (triggerKeyOK and m_writeL1TriggerKeyExt) {
0106 edm::LogVerbatim("L1-O2O") << "Object key for L1TriggerKeyExtRcd@L1TriggerKeyExt: " << key.tscKey()
0107 << " (about to run writePayload)";
0108 token = m_writer.writePayload(iSetup);
0109 }
0110
0111
0112
0113
0114
0115 if (token.empty() and m_writeL1TriggerKeyExt) {
0116 edm::LogInfo("L1CondDBPayloadWriterExt::analyze") << " token = " << token;
0117 return;
0118 }
0119
0120
0121 if (m_writeL1TriggerKeyExt) {
0122 keyList = new L1TriggerKeyListExt(oldKeyList);
0123 if (not keyList->addKey(key.tscKey(), token, m_overwriteKeys))
0124 throw cond::Exception("L1CondDBPayloadWriter: TSC key " + key.tscKey() + " already in L1TriggerKeyListExt");
0125 }
0126
0127 if (not m_writeConfigData) {
0128
0129 if (keyList)
0130 m_writer.writeKeyList(keyList, 0, m_logTransactions);
0131 return;
0132 }
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 bool throwException = false;
0143 for (const auto& it : key.recordToKeyMap()) {
0144
0145
0146 if (m_rcdToWriterMap.find(it.first) == m_rcdToWriterMap.end())
0147 continue;
0148
0149
0150
0151 if (it.second == L1TriggerKeyExt::kNullKey) {
0152 edm::LogVerbatim("L1-O2O") << "L1CondDBPayloadWriter: null object key for " << it.first
0153 << "; skipping this record.";
0154 continue;
0155 }
0156
0157
0158 if (oldKeyList.token(it.first, it.second).empty() || m_overwriteKeys) {
0159
0160 if (!oldKeyList.token(it.first, it.second).empty()) {
0161 edm::LogVerbatim("L1-O2O") << "*** Overwriting payload: object key for " << it.first << ": " << it.second;
0162 } else {
0163 edm::LogVerbatim("L1-O2O") << "object key for " << it.first << ": " << it.second;
0164 }
0165
0166 try {
0167 edm::LogVerbatim("L1-O2O") << "about to run writePayload for " << it.first;
0168 token = m_rcdToWriterMap.at(it.first)->writePayload(iSetup);
0169 } catch (l1t::DataInvalidException& ex) {
0170 edm::LogVerbatim("L1-O2O") << ex.what() << " Skipping to next record.";
0171 throwException = true;
0172 continue;
0173 }
0174
0175 if (!token.empty()) {
0176
0177 if (!keyList)
0178 keyList = new L1TriggerKeyListExt(oldKeyList);
0179
0180
0181 if (!(keyList->addKey(it.first, it.second, token, m_overwriteKeys)))
0182 throw cond::Exception("L1CondDBPayloadWriter")
0183 << "subsystem key " << it.second << " for " << it.first << " already in L1TriggerKeyListExt";
0184 }
0185
0186 } else
0187 edm::LogVerbatim("L1-O2O") << "L1CondDBPayloadWriter: object key " << it.second << " for " << it.first
0188 << " already in L1TriggerKeyListExt";
0189
0190 }
0191
0192 if (keyList)
0193 m_writer.writeKeyList(keyList, 0, m_logTransactions);
0194
0195 if (throwException)
0196 throw l1t::DataInvalidException("Payload problem found.");
0197 }
0198
0199
0200 void L1CondDBPayloadWriterExt::beginJob() {}
0201
0202
0203 void L1CondDBPayloadWriterExt::endJob() {}
0204
0205
0206 DEFINE_FWK_MODULE(L1CondDBPayloadWriterExt);