Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:24

0001 #include <iostream>
0002 #include <fstream>
0003 
0004 #include "CondTools/L1TriggerExt/interface/L1ConfigOnlineProdBaseExt.h"
0005 #include "CondFormats/L1TObjects/interface/L1TMuonGlobalParams.h"
0006 #include "CondFormats/DataRecord/interface/L1TMuonGlobalParamsRcd.h"
0007 #include "CondFormats/DataRecord/interface/L1TMuonGlobalParamsO2ORcd.h"
0008 #include "L1Trigger/L1TMuon/interface/L1TMuonGlobalParamsHelper.h"
0009 #include "L1Trigger/L1TMuon/interface/L1TMuonGlobalParams_PUBLIC.h"
0010 #include "L1Trigger/L1TCommon/interface/TriggerSystem.h"
0011 #include "L1Trigger/L1TCommon/interface/XmlConfigParser.h"
0012 #include "OnlineDBqueryHelper.h"
0013 
0014 class L1TMuonGlobalParamsOnlineProd : public L1ConfigOnlineProdBaseExt<L1TMuonGlobalParamsO2ORcd, L1TMuonGlobalParams> {
0015 private:
0016   const bool transactionSafe;
0017   const edm::ESGetToken<L1TMuonGlobalParams, L1TMuonGlobalParamsRcd> baseSettings_token;
0018 
0019 public:
0020   std::unique_ptr<const L1TMuonGlobalParams> newObject(const std::string &objectKey,
0021                                                        const L1TMuonGlobalParamsO2ORcd &record) override;
0022 
0023   L1TMuonGlobalParamsOnlineProd(const edm::ParameterSet &);
0024   ~L1TMuonGlobalParamsOnlineProd(void) override {}
0025 };
0026 
0027 L1TMuonGlobalParamsOnlineProd::L1TMuonGlobalParamsOnlineProd(const edm::ParameterSet &iConfig)
0028     : L1ConfigOnlineProdBaseExt<L1TMuonGlobalParamsO2ORcd, L1TMuonGlobalParams>(iConfig),
0029       transactionSafe(iConfig.getParameter<bool>("transactionSafe")),
0030       baseSettings_token(wrappedSetWhatProduced(iConfig).consumes()) {}
0031 
0032 std::unique_ptr<const L1TMuonGlobalParams> L1TMuonGlobalParamsOnlineProd::newObject(
0033     const std::string &objectKey, const L1TMuonGlobalParamsO2ORcd &record) {
0034   const L1TMuonGlobalParamsRcd &baseRcd = record.template getRecord<L1TMuonGlobalParamsRcd>();
0035   auto const &baseSettings = baseRcd.get(baseSettings_token);
0036 
0037   if (objectKey.empty()) {
0038     edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << "Key is empty";
0039     if (transactionSafe)
0040       throw std::runtime_error("SummaryForFunctionManager: uGMT  | Faulty  | Empty objectKey");
0041     else {
0042       edm::LogError("L1-O2O: L1TMuonGlobalParams") << "returning unmodified prototype of L1TMuonGlobalParams";
0043       return std::make_unique<const L1TMuonGlobalParams>(baseSettings);
0044     }
0045   }
0046 
0047   std::string tscKey = objectKey.substr(0, objectKey.find(':'));
0048   std::string rsKey = objectKey.substr(objectKey.find(':') + 1, std::string::npos);
0049 
0050   edm::LogInfo("L1-O2O: L1TMuonGlobalParamsOnlineProd")
0051       << "Producing L1TMuonGlobalParams with TSC key =" << tscKey << " and RS key = " << rsKey;
0052 
0053   std::string algo_key, hw_key;
0054   std::string hw_payload;
0055   std::map<std::string, std::string> rs_payloads, algo_payloads;
0056   try {
0057     std::map<std::string, std::string> keys =
0058         l1t::OnlineDBqueryHelper::fetch({"ALGO", "HW"}, "UGMT_KEYS", tscKey, m_omdsReader);
0059     algo_key = keys["ALGO"];
0060     hw_key = keys["HW"];
0061 
0062     hw_payload = l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGMT_CLOBS", hw_key, m_omdsReader)["CONF"];
0063 
0064     std::map<std::string, std::string> rsKeys =
0065         l1t::OnlineDBqueryHelper::fetch({"MP7", "MP7_MONI", "AMC13_MONI"}, "UGMT_RS_KEYS", rsKey, m_omdsReader);
0066 
0067     std::map<std::string, std::string> algoKeys =
0068         l1t::OnlineDBqueryHelper::fetch({"MP7", "LUTS"}, "UGMT_ALGO_KEYS", algo_key, m_omdsReader);
0069 
0070     for (auto &key : rsKeys)
0071       rs_payloads[key.second] =
0072           l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGMT_CLOBS", key.second, m_omdsReader)["CONF"];
0073 
0074     for (auto &key : algoKeys)
0075       algo_payloads[key.second] =
0076           l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGMT_CLOBS", key.second, m_omdsReader)["CONF"];
0077   } catch (std::runtime_error &e) {
0078     edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << e.what();
0079     if (transactionSafe)
0080       throw std::runtime_error("SummaryForFunctionManager: uGMT  | Faulty  | Broken key");
0081     else {
0082       edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << "returning unmodified prototype of L1TMuonGlobalParams";
0083       return std::make_unique<const L1TMuonGlobalParams>(baseSettings);
0084     }
0085   }
0086 
0087   // for debugging dump the configs to local files
0088   {
0089     std::ofstream output(std::string("/tmp/").append(hw_key.substr(0, hw_key.find('/'))).append(".xml"));
0090     output << hw_payload;
0091     output.close();
0092   }
0093   for (auto &conf : rs_payloads) {
0094     std::ofstream output(std::string("/tmp/").append(conf.first.substr(0, conf.first.find("/"))).append(".xml"));
0095     output << conf.second;
0096     output.close();
0097   }
0098   for (auto &conf : algo_payloads) {
0099     std::ofstream output(std::string("/tmp/").append(conf.first.substr(0, conf.first.find("/"))).append(".xml"));
0100     output << conf.second;
0101     output.close();
0102   }
0103 
0104   // finally, push all payloads to the XML parser and construct the TrigSystem objects with each of those
0105   l1t::XmlConfigParser xmlRdr;
0106   l1t::TriggerSystem trgSys;
0107 
0108   try {
0109     // HW settings should always go first
0110     xmlRdr.readDOMFromString(hw_payload);
0111     xmlRdr.readRootElement(trgSys);
0112 
0113     // now let's parse ALGO and then RS settings
0114     for (auto &conf : algo_payloads) {
0115       xmlRdr.readDOMFromString(conf.second);
0116       xmlRdr.readRootElement(trgSys);
0117     }
0118     for (auto &conf : rs_payloads) {
0119       xmlRdr.readDOMFromString(conf.second);
0120       xmlRdr.readRootElement(trgSys);
0121     }
0122     trgSys.setConfigured();
0123   } catch (std::runtime_error &e) {
0124     edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << e.what();
0125     if (transactionSafe)
0126       throw std::runtime_error("SummaryForFunctionManager: uGMT  | Faulty  | Cannot parse XMLs");
0127     else {
0128       edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << "returning unmodified prototype of L1TMuonGlobalParams";
0129       return std::make_unique<const L1TMuonGlobalParams>(baseSettings);
0130     }
0131   }
0132 
0133   L1TMuonGlobalParamsHelper m_params_helper(baseSettings);
0134   try {
0135     m_params_helper.loadFromOnline(trgSys);
0136   } catch (std::runtime_error &e) {
0137     edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << e.what();
0138     if (transactionSafe)
0139       throw std::runtime_error("SummaryForFunctionManager: uGMT  | Faulty  | Cannot run helper");
0140     else {
0141       edm::LogError("L1-O2O: L1TMuonGlobalParamsOnlineProd") << "returning unmodified prototype of L1TMuonGlobalParams";
0142       return std::make_unique<const L1TMuonGlobalParams>(baseSettings);
0143     }
0144   }
0145 
0146   auto retval = std::make_unique<const L1TMuonGlobalParams>(cast_to_L1TMuonGlobalParams(m_params_helper));
0147 
0148   edm::LogInfo("L1-O2O: L1TMuonGlobalParamsOnlineProd")
0149       << "SummaryForFunctionManager: uGMT  | OK      | All looks good";
0150   return retval;
0151 }
0152 
0153 //define this as a plug-in
0154 DEFINE_FWK_EVENTSETUP_MODULE(L1TMuonGlobalParamsOnlineProd);