File indexing completed on 2023-03-17 11:14:23
0001 #include <iostream>
0002 #include <fstream>
0003
0004 #include "CondTools/L1TriggerExt/interface/L1ConfigOnlineProdBaseExt.h"
0005 #include "CondFormats/L1TObjects/interface/L1TMuonOverlapFwVersion.h"
0006 #include "CondFormats/DataRecord/interface/L1TMuonOverlapFwVersionRcd.h"
0007 #include "CondFormats/DataRecord/interface/L1TMuonOverlapFwVersionO2ORcd.h"
0008 #include "L1Trigger/L1TCommon/interface/TriggerSystem.h"
0009 #include "L1Trigger/L1TCommon/interface/XmlConfigParser.h"
0010 #include "OnlineDBqueryHelper.h"
0011
0012 #include "xercesc/util/PlatformUtils.hpp"
0013 using namespace XERCES_CPP_NAMESPACE;
0014
0015 class L1TMuonOverlapFwVersionOnlineProd
0016 : public L1ConfigOnlineProdBaseExt<L1TMuonOverlapFwVersionO2ORcd, L1TMuonOverlapFwVersion> {
0017 private:
0018 const bool transactionSafe;
0019 const edm::ESGetToken<L1TMuonOverlapFwVersion, L1TMuonOverlapFwVersionRcd> baseSettings_token;
0020
0021 public:
0022 std::unique_ptr<const L1TMuonOverlapFwVersion> newObject(const std::string& objectKey,
0023 const L1TMuonOverlapFwVersionO2ORcd& record) override;
0024
0025 L1TMuonOverlapFwVersionOnlineProd(const edm::ParameterSet&);
0026 ~L1TMuonOverlapFwVersionOnlineProd(void) override = default;
0027 };
0028
0029 void replaceAll(std::string& str, const std::string& from, const std::string& to) {
0030 if (from.empty())
0031 return;
0032 size_t start_pos = 0;
0033 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
0034 str.replace(start_pos, from.length(), to);
0035 start_pos += to.length();
0036 }
0037 }
0038
0039 void removeAll(std::string& str, const std::string& from, const std::string& to) {
0040 if (from.empty())
0041 return;
0042 size_t start_pos = 0;
0043 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
0044 size_t end_pos = str.find(to) + to.length();
0045 int length = end_pos - start_pos;
0046 str.replace(start_pos, length, "");
0047 }
0048 }
0049
0050 L1TMuonOverlapFwVersionOnlineProd::L1TMuonOverlapFwVersionOnlineProd(const edm::ParameterSet& iConfig)
0051 : L1ConfigOnlineProdBaseExt<L1TMuonOverlapFwVersionO2ORcd, L1TMuonOverlapFwVersion>(iConfig),
0052 transactionSafe(iConfig.getParameter<bool>("transactionSafe")),
0053 baseSettings_token(wrappedSetWhatProduced(iConfig).consumes()) {}
0054
0055 std::unique_ptr<const L1TMuonOverlapFwVersion> L1TMuonOverlapFwVersionOnlineProd::newObject(
0056 const std::string& objectKey, const L1TMuonOverlapFwVersionO2ORcd& record) {
0057 const L1TMuonOverlapFwVersionRcd& baseRcd = record.template getRecord<L1TMuonOverlapFwVersionRcd>();
0058 auto const& baseSettings = baseRcd.get(baseSettings_token);
0059
0060 if (objectKey.empty()) {
0061 edm::LogError("L1-O2O: L1TMuonOverlapFwVersionOnlineProd")
0062 << "Key is empty, returning empty L1TMuonOverlapFwVersion";
0063 if (transactionSafe)
0064 throw std::runtime_error("SummaryForFunctionManager: OMTF | Faulty | Empty objectKey");
0065 else {
0066 edm::LogError("L1-O2O: L1TMuonOverlapFwVersionOnlineProd")
0067 << "returning unmodified prototype of L1TMuonOverlapFwVersion";
0068 return std::make_unique<const L1TMuonOverlapFwVersion>(baseSettings);
0069 }
0070 }
0071
0072 edm::LogInfo("L1-O2O: L1TMuonOverlapFwVersionOnlineProd")
0073 << "Producing L1TMuonOverlapFwVersion for key = " << objectKey;
0074
0075 std::string payload, hw_fake;
0076 std::string algoV_string, layersV_string, patternsV_string, synthDate;
0077
0078 try {
0079 payload = l1t::OnlineDBqueryHelper::fetch({"CONF"}, "OMTF_CLOBS", objectKey, m_omdsReader)["CONF"];
0080
0081 } catch (std::runtime_error& e) {
0082 edm::LogError("L1-O2O: L1TMuonOverlapFwVersionOnlineProd") << e.what();
0083 if (transactionSafe)
0084 throw std::runtime_error(std::string("SummaryForFunctionManager: OMTF | Faulty | ") + e.what());
0085 else {
0086 edm::LogError("L1-O2O: L1TMuonOverlapFwVersionOnlineProd")
0087 << "returning unmodified prototype of L1TMuonOverlapFwVersion";
0088 return std::make_unique<const L1TMuonOverlapFwVersion>(baseSettings);
0089 }
0090 }
0091
0092 {
0093 std::ofstream output(std::string("/tmp/").append(objectKey.substr(0, objectKey.find('/'))).append(".xml"));
0094 output << objectKey;
0095 output.close();
0096 }
0097
0098
0099 l1t::XmlConfigParser xmlRdr;
0100 l1t::TriggerSystem parsedXMLs;
0101
0102 try {
0103
0104 hw_fake = "<system id=\"OMTF\"> </system>";
0105 xmlRdr.readDOMFromString(hw_fake);
0106 parsedXMLs.addProcessor("processors", "processors", "all_crates", "all_slots");
0107 xmlRdr.readRootElement(parsedXMLs);
0108
0109
0110 replaceAll(payload, "infra", "algo");
0111 removeAll(payload, "<context id=\"daq", "</context>");
0112 removeAll(payload, "<context id=\"OMTF", "</context>");
0113 xmlRdr.readDOMFromString(payload);
0114 xmlRdr.readRootElement(parsedXMLs);
0115
0116 parsedXMLs.setConfigured();
0117
0118 } catch (std::runtime_error& e) {
0119 edm::LogError("L1-O2O: L1TMuonOverlapFwVersionOnlineProd") << e.what();
0120 if (transactionSafe)
0121 throw std::runtime_error(std::string("SummaryForFunctionManager: OMTF | Faulty at parsing XML | ") + e.what());
0122 else {
0123 edm::LogError("L1-O2O: L1TMuonOverlapFwVersionOnlineProd")
0124 << "returning unmodified prototype of L1TMuonOverlapFwVersion";
0125 return std::make_unique<const L1TMuonOverlapFwVersion>(baseSettings);
0126 }
0127 }
0128
0129 std::map<std::string, l1t::Parameter> conf = parsedXMLs.getParameters("processors");
0130 algoV_string = conf["algorithmVer"].getValueAsStr();
0131 layersV_string = conf["layersVer"].getValueAsStr();
0132 patternsV_string = conf["patternsVer"].getValueAsStr();
0133 synthDate = conf["synthDate"].getValueAsStr();
0134
0135 unsigned algoV, layersV, patternsV;
0136 std::stringstream ssalgoV, sslayersV, sspatternsV;
0137 ssalgoV << std::hex << algoV_string.c_str();
0138 ssalgoV >> algoV;
0139 sslayersV << std::hex << layersV_string.c_str();
0140 sslayersV >> layersV;
0141 sspatternsV << std::hex << patternsV_string.c_str();
0142 sspatternsV >> patternsV;
0143 auto retval = std::make_unique<const L1TMuonOverlapFwVersion>(algoV, layersV, patternsV, synthDate);
0144
0145 edm::LogInfo("L1-O2O: L1TMuonOverlapFwVersionOnlineProd")
0146 << "SummaryForFunctionManager: OMTF | OK | All looks good";
0147 return retval;
0148 }
0149
0150
0151 DEFINE_FWK_EVENTSETUP_MODULE(L1TMuonOverlapFwVersionOnlineProd);