Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:14

0001 #include <iostream>
0002 #include <fstream>
0003 #include <stdexcept>
0004 #include <sstream>
0005 
0006 #include "CondTools/L1TriggerExt/interface/L1ConfigOnlineProdBaseExt.h"
0007 #include "CondFormats/L1TObjects/interface/CaloParams.h"
0008 #include "CondFormats/DataRecord/interface/L1TCaloParamsRcd.h"
0009 #include "CondFormats/DataRecord/interface/L1TCaloParamsO2ORcd.h"
0010 #include "L1Trigger/L1TCommon/interface/TriggerSystem.h"
0011 #include "L1Trigger/L1TCommon/interface/XmlConfigParser.h"
0012 #include "L1Trigger/L1TCommon/interface/ConvertToLUT.h"
0013 //#include "L1Trigger/L1TCalorimeter/interface/CaloParamsHelper.h"
0014 #include "CaloParamsHelperO2O.h"
0015 #include "OnlineDBqueryHelper.h"
0016 
0017 #include "xercesc/util/PlatformUtils.hpp"
0018 using namespace XERCES_CPP_NAMESPACE;
0019 
0020 class L1TCaloParamsOnlineProd : public L1ConfigOnlineProdBaseExt<L1TCaloParamsO2ORcd, l1t::CaloParams> {
0021 private:
0022   const edm::ESGetToken<l1t::CaloParams, L1TCaloParamsRcd> baseSettings_token;
0023   const unsigned int exclusiveLayer;  // 0 - process calol1 and calol2, 1 - only calol1, 2 - only calol2
0024   const bool transactionSafe;
0025 
0026   bool readCaloLayer1OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper,
0027                                     std::map<std::string, l1t::Parameter>& conf,
0028                                     std::map<std::string, l1t::Mask>&);
0029   bool readCaloLayer2OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper,
0030                                     std::map<std::string, l1t::Parameter>& conf,
0031                                     std::map<std::string, l1t::Mask>&);
0032 
0033 public:
0034   std::unique_ptr<const l1t::CaloParams> newObject(const std::string& objectKey,
0035                                                    const L1TCaloParamsO2ORcd& record) override;
0036 
0037   L1TCaloParamsOnlineProd(const edm::ParameterSet&);
0038   ~L1TCaloParamsOnlineProd(void) override {}
0039 };
0040 
0041 bool L1TCaloParamsOnlineProd::readCaloLayer1OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper,
0042                                                            std::map<std::string, l1t::Parameter>& conf,
0043                                                            std::map<std::string, l1t::Mask>&) {
0044   const char* expectedParams[] = {
0045       "layer1ECalScaleFactors",
0046       "layer1HCalScaleFactors",
0047       "layer1HFScaleFactors",
0048       "layer1ECalScaleETBins",
0049       "layer1HCalScaleETBins",
0050       "layer1HFScaleETBins"
0051       // Optional params
0052       //"layer1ECalScalePhiBins",
0053       //"layer1HCalScalePhiBins",
0054       //"layer1HFScalePhiBins",
0055       //"layer1SecondStageLUT"
0056   };
0057   for (const auto param : expectedParams) {
0058     if (conf.find(param) == conf.end()) {
0059       edm::LogError("L1-O2O: L1TCaloParamsOnlineProd")
0060           << "Unable to locate expected CaloLayer1 parameter: " << param << " in L1 settings payload!";
0061       return false;
0062     }
0063   }
0064   // Layer 1 LUT specification
0065   paramsHelper.setLayer1ECalScaleFactors((conf["layer1ECalScaleFactors"].getVector<double>()));
0066   paramsHelper.setLayer1HCalScaleFactors((conf["layer1HCalScaleFactors"].getVector<double>()));
0067   paramsHelper.setLayer1HFScaleFactors((conf["layer1HFScaleFactors"].getVector<double>()));
0068   paramsHelper.setLayer1ECalScaleETBins(conf["layer1ECalScaleETBins"].getVector<int>());
0069   paramsHelper.setLayer1HCalScaleETBins(conf["layer1HCalScaleETBins"].getVector<int>());
0070   paramsHelper.setLayer1HFScaleETBins(conf["layer1HFScaleETBins"].getVector<int>());
0071 
0072   if (conf.find("layer1ECalScalePhiBins") != conf.end())
0073     paramsHelper.setLayer1ECalScalePhiBins(
0074         conf["layer1ECalScalePhiBins"].getVector<unsigned int>());  // std::vector<unsigned>(36,0)
0075   if (conf.find("layer1HCalScalePhiBins") != conf.end())
0076     paramsHelper.setLayer1HCalScalePhiBins(conf["layer1HCalScalePhiBins"].getVector<unsigned int>());
0077   if (conf.find("layer1HFScalePhiBins") != conf.end())
0078     paramsHelper.setLayer1HFScalePhiBins(conf["layer1HFScalePhiBins"].getVector<unsigned int>());
0079   if (conf.find("layer1SecondStageLUT") != conf.end())
0080     paramsHelper.setLayer1SecondStageLUT(conf["layer1SecondStageLUT"].getVector<unsigned int>());
0081 
0082   return true;
0083 }
0084 
0085 bool L1TCaloParamsOnlineProd::readCaloLayer2OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper,
0086                                                            std::map<std::string, l1t::Parameter>& conf,
0087                                                            std::map<std::string, l1t::Mask>&) {
0088   const char* expectedParams[] = {
0089       "leptonSeedThreshold",
0090       "leptonTowerThreshold",
0091       "pileUpTowerThreshold",
0092       "egammaRelaxationThreshold",
0093       "egammaMaxEta",
0094       "egammaBypassCuts",
0095       "egammaBypassShape",
0096       "egammaBypassEcalFG",
0097       "egammaBypassExtendedHOverE",
0098       "egammaHOverECut_iEtaLT15",
0099       "egammaHOverECut_iEtaGTEq15",
0100       "egammaEnergyCalibLUT",
0101       "egammaIsoLUT1",
0102       "egammaIsoLUT2",
0103       "tauMaxEta",
0104       "tauEnergyCalibLUT",
0105       "tauIsoLUT",
0106       "tauTrimmingLUT",
0107       "jetSeedThreshold",
0108       "HTMHT_maxJetEta",
0109       "HT_jetThreshold",
0110       "MHT_jetThreshold",
0111       "jetBypassPileUpSub",
0112       "jetEnergyCalibLUT",
0113       "jetPUSUsePhiRing",
0114       "towerCountThreshold",
0115       "towerCountMaxEta",
0116       "ETMET_maxTowerEta",
0117       "ecalET_towerThresholdLUT",
0118       "ET_towerThresholdLUT",
0119       "MET_towerThresholdLUT",
0120       "ET_centralityLowerThresholds",
0121       "ET_centralityUpperThresholds",
0122       "ET_energyCalibLUT",
0123       "ecalET_energyCalibLUT",
0124       "MET_energyCalibLUT",
0125       "METHF_energyCalibLUT",
0126       "MET_phiCalibLUT",
0127       "METHF_phiCalibLUT",
0128   };
0129 
0130   for (const auto param : expectedParams) {
0131     if (conf.find(param) == conf.end()) {
0132       edm::LogError("L1-O2O: L1TCaloParamsOnlineProd")
0133           << "Unable to locate expected CaloLayer2 parameter: " << param << " in L1 settings payload!";
0134       return false;
0135     }
0136   }
0137   // Layer 2 params specification
0138   paramsHelper.setEgSeedThreshold((conf["leptonSeedThreshold"].getValue<int>()) / 2);
0139   paramsHelper.setTauSeedThreshold((conf["leptonSeedThreshold"].getValue<int>()) / 2);
0140   paramsHelper.setEgNeighbourThreshold((conf["leptonTowerThreshold"].getValue<int>()) / 2);
0141   paramsHelper.setTauNeighbourThreshold((conf["leptonTowerThreshold"].getValue<int>()) / 2);
0142   paramsHelper.setPileUpTowerThreshold((conf["pileUpTowerThreshold"].getValue<int>()) / 2);
0143 
0144   paramsHelper.setEgMaxPtHOverE((conf["egammaRelaxationThreshold"].getValue<int>()) / 2.);
0145   paramsHelper.setEgEtaCut((conf["egammaMaxEta"].getValue<int>()));
0146   paramsHelper.setEgBypassEGVetos(conf["egammaBypassCuts"].getValue<bool>());
0147   paramsHelper.setEgBypassShape(conf["egammaBypassShape"].getValue<bool>());
0148   paramsHelper.setEgBypassECALFG(conf["egammaBypassEcalFG"].getValue<bool>());
0149   paramsHelper.setEgBypassExtHOverE(conf["egammaBypassExtendedHOverE"].getValue<bool>());
0150   paramsHelper.setEgHOverEcutBarrel(conf["egammaHOverECut_iEtaLT15"].getValue<int>());
0151   paramsHelper.setEgHOverEcutEndcap(conf["egammaHOverECut_iEtaGTEq15"].getValue<int>());
0152   paramsHelper.setEgCalibrationLUT(l1t::convertToLUT(conf["egammaEnergyCalibLUT"].getVector<int>()));
0153   paramsHelper.setEgIsolationLUT(l1t::convertToLUT(conf["egammaIsoLUT1"].getVector<int>()));
0154   paramsHelper.setEgIsolationLUT2(l1t::convertToLUT(conf["egammaIsoLUT2"].getVector<int>()));
0155 
0156   paramsHelper.setIsoTauEtaMax((conf["tauMaxEta"].getValue<int>()));
0157   paramsHelper.setTauCalibrationLUT(l1t::convertToLUT(conf["tauEnergyCalibLUT"].getVector<int>()));
0158   paramsHelper.setTauIsolationLUT(l1t::convertToLUT(conf["tauIsoLUT"].getVector<int>()));
0159   paramsHelper.setTauTrimmingShapeVetoLUT(l1t::convertToLUT(conf["tauTrimmingLUT"].getVector<int>()));
0160 
0161   paramsHelper.setJetSeedThreshold((conf["jetSeedThreshold"].getValue<int>()) / 2);
0162   paramsHelper.setJetBypassPUS(conf["jetBypassPileUpSub"].getValue<bool>());
0163   paramsHelper.setJetPUSUsePhiRing(conf["jetPUSUsePhiRing"].getValue<bool>());
0164   paramsHelper.setJetCalibrationLUT(l1t::convertToLUT(conf["jetEnergyCalibLUT"].getVector<uint32_t>()));
0165 
0166   std::vector<int> etSumEtaMax;
0167   std::vector<int> etSumEtThresh;
0168 
0169   etSumEtaMax.push_back(conf["ETMET_maxTowerEta"].getValue<int>());
0170   etSumEtaMax.push_back(conf["HTMHT_maxJetEta"].getValue<int>());
0171   etSumEtaMax.push_back(conf["ETMET_maxTowerEta"].getValue<int>());
0172   etSumEtaMax.push_back(conf["HTMHT_maxJetEta"].getValue<int>());
0173   etSumEtaMax.push_back(conf["towerCountMaxEta"].getValue<int>());
0174 
0175   etSumEtThresh.push_back(0);  //deprecated by EttPUSLUT
0176   etSumEtThresh.push_back(conf["HT_jetThreshold"].getValue<int>() / 2);
0177   etSumEtThresh.push_back(0);  //deprecated by MetPUSLUT
0178   etSumEtThresh.push_back(conf["MHT_jetThreshold"].getValue<int>() / 2);
0179   etSumEtThresh.push_back(conf["towerCountThreshold"].getValue<int>() / 2);
0180 
0181   for (uint i = 0; i < 5; ++i) {
0182     paramsHelper.setEtSumEtaMax(i, etSumEtaMax.at(i));
0183     paramsHelper.setEtSumEtThreshold(i, etSumEtThresh.at(i));
0184   }
0185 
0186   paramsHelper.setEtSumMetPUSLUT(l1t::convertToLUT(conf["MET_towerThresholdLUT"].getVector<int>()));
0187   paramsHelper.setEtSumEttPUSLUT(l1t::convertToLUT(conf["ET_towerThresholdLUT"].getVector<int>()));
0188   paramsHelper.setEtSumEcalSumPUSLUT(l1t::convertToLUT(conf["ecalET_towerThresholdLUT"].getVector<int>()));
0189 
0190   std::vector<double> etSumCentLowerValues;
0191   std::vector<double> etSumCentUpperValues;
0192 
0193   etSumCentLowerValues = conf["ET_centralityLowerThresholds"].getVector<double>();
0194   etSumCentUpperValues = conf["ET_centralityUpperThresholds"].getVector<double>();
0195 
0196   for (uint i = 0; i < 8; ++i) {
0197     paramsHelper.setEtSumCentLower(i, etSumCentLowerValues[i] / 2);
0198     paramsHelper.setEtSumCentUpper(i, etSumCentUpperValues[i] / 2);
0199   }
0200 
0201   // demux tower sum calib LUTs
0202   paramsHelper.setEtSumEttCalibrationLUT(l1t::convertToLUT(conf["ET_energyCalibLUT"].getVector<int>()));
0203   paramsHelper.setEtSumEcalSumCalibrationLUT(l1t::convertToLUT(conf["ecalET_energyCalibLUT"].getVector<int>()));
0204   paramsHelper.setMetCalibrationLUT(l1t::convertToLUT(conf["MET_energyCalibLUT"].getVector<int>()));
0205   paramsHelper.setMetHFCalibrationLUT(l1t::convertToLUT(conf["METHF_energyCalibLUT"].getVector<int>()));
0206   paramsHelper.setMetPhiCalibrationLUT(l1t::convertToLUT(conf["MET_phiCalibLUT"].getVector<int>()));
0207   paramsHelper.setMetHFPhiCalibrationLUT(l1t::convertToLUT(conf["METHF_phiCalibLUT"].getVector<int>()));
0208 
0209   return true;
0210 }
0211 
0212 L1TCaloParamsOnlineProd::L1TCaloParamsOnlineProd(const edm::ParameterSet& iConfig)
0213     : L1ConfigOnlineProdBaseExt<L1TCaloParamsO2ORcd, l1t::CaloParams>(iConfig),
0214       baseSettings_token(wrappedSetWhatProduced(iConfig).consumes()),
0215       exclusiveLayer(iConfig.getParameter<uint32_t>("exclusiveLayer")),
0216       transactionSafe(iConfig.getParameter<bool>("transactionSafe")) {}
0217 
0218 std::unique_ptr<const l1t::CaloParams> L1TCaloParamsOnlineProd::newObject(const std::string& objectKey,
0219                                                                           const L1TCaloParamsO2ORcd& record) {
0220   const L1TCaloParamsRcd& baseRcd = record.template getRecord<L1TCaloParamsRcd>();
0221   auto const& baseSettings = baseRcd.get(baseSettings_token);
0222 
0223   if (objectKey.empty()) {
0224     edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "Key is empty";
0225     if (transactionSafe)
0226       throw std::runtime_error("SummaryForFunctionManager: Calo  | Faulty  | Empty objectKey");
0227     else {
0228       edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "returning unmodified prototype of l1t::CaloParams";
0229       return std::make_unique<const l1t::CaloParams>(baseSettings);
0230     }
0231   }
0232 
0233   std::string tscKey = objectKey.substr(0, objectKey.find(':'));
0234   std::string rsKey = objectKey.substr(objectKey.find(':') + 1, std::string::npos);
0235 
0236   edm::LogInfo("L1-O2O: L1TCaloParamsOnlineProd")
0237       << "Producing L1TCaloParamsOnlineProd with TSC key = " << tscKey << " and RS key = " << rsKey;
0238 
0239   std::string calol1_top_key, calol1_algo_key;
0240   std::string calol1_algo_payload;
0241   std::string calol2_top_key, calol2_algo_key, calol2_hw_key;
0242   std::string calol2_hw_payload;
0243   std::map<std::string, std::string> calol2_algo_payloads;  // key -> XML payload
0244   try {
0245     std::map<std::string, std::string> topKeys =
0246         l1t::OnlineDBqueryHelper::fetch({"CALOL1_KEY", "CALOL2_KEY"}, "L1_TRG_CONF_KEYS", tscKey, m_omdsReader);
0247 
0248     if (exclusiveLayer == 0 || exclusiveLayer == 1) {
0249       calol1_top_key = topKeys["CALOL1_KEY"];
0250 
0251       calol1_algo_key = l1t::OnlineDBqueryHelper::fetch({"ALGO"}, "CALOL1_KEYS", calol1_top_key, m_omdsReader)["ALGO"];
0252 
0253       calol1_algo_payload =
0254           l1t::OnlineDBqueryHelper::fetch({"CONF"}, "CALOL1_CLOBS", calol1_algo_key, m_omdsReader)["CONF"];
0255     }
0256 
0257     if (exclusiveLayer == 0 || exclusiveLayer == 2) {
0258       calol2_top_key = topKeys["CALOL2_KEY"];
0259 
0260       std::map<std::string, std::string> calol2_keys =
0261           l1t::OnlineDBqueryHelper::fetch({"ALGO", "HW"}, "CALOL2_KEYS", calol2_top_key, m_omdsReader);
0262 
0263       calol2_hw_key = calol2_keys["HW"];
0264       calol2_hw_payload =
0265           l1t::OnlineDBqueryHelper::fetch({"CONF"}, "CALOL2_CLOBS", calol2_hw_key, m_omdsReader)["CONF"];
0266 
0267       calol2_algo_key = calol2_keys["ALGO"];
0268 
0269       std::map<std::string, std::string> calol2_algo_keys =
0270           l1t::OnlineDBqueryHelper::fetch({"DEMUX", "MPS_COMMON", "MPS_JET", "MP_EGAMMA", "MP_SUM", "MP_TAU"},
0271                                           "CALOL2_ALGO_KEYS",
0272                                           calol2_algo_key,
0273                                           m_omdsReader);
0274 
0275       for (auto& key : calol2_algo_keys)
0276         calol2_algo_payloads[key.second] =
0277             l1t::OnlineDBqueryHelper::fetch({"CONF"}, "CALOL2_CLOBS", key.second, m_omdsReader)["CONF"];
0278     }
0279 
0280   } catch (std::runtime_error& e) {
0281     edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << e.what();
0282     if (transactionSafe)
0283       throw std::runtime_error(std::string("SummaryForFunctionManager: Calo  | Faulty  | ") + e.what());
0284     else {
0285       edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "returning unmodified prototype of l1t::CaloParams";
0286       return std::make_unique<const l1t::CaloParams>(baseSettings);
0287     }
0288   }
0289 
0290   if (exclusiveLayer == 0 || exclusiveLayer == 2) {
0291     // for debugging purposes dump the configs to local files
0292     for (auto& conf : calol2_algo_payloads) {
0293       std::ofstream output(std::string("/tmp/").append(conf.first.substr(0, conf.first.find("/"))).append(".xml"));
0294       output << conf.second;
0295       output.close();
0296     }
0297     std::ofstream output(std::string("/tmp/").append(calol2_hw_key.substr(0, calol2_hw_key.find('/'))).append(".xml"));
0298     output << calol2_hw_payload;
0299     output.close();
0300   }
0301   if (exclusiveLayer == 0 || exclusiveLayer == 1) {
0302     std::ofstream output(
0303         std::string("/tmp/").append(calol1_algo_key.substr(0, calol1_algo_key.find('/'))).append(".xml"));
0304     output << calol1_algo_payload;
0305     output.close();
0306   }
0307 
0308   l1t::CaloParamsHelperO2O m_params_helper(baseSettings);
0309 
0310   if (exclusiveLayer == 0 || exclusiveLayer == 1) {
0311     try {
0312       l1t::XmlConfigParser xmlReader1;
0313       xmlReader1.readDOMFromString(calol1_algo_payload);
0314 
0315       l1t::TriggerSystem calol1;
0316       calol1.addProcessor("processors", "processors", "-1", "-1");
0317       xmlReader1.readRootElement(calol1, "calol1");
0318       calol1.setConfigured();
0319 
0320       std::map<std::string, l1t::Parameter> calol1_conf = calol1.getParameters("processors");
0321       std::map<std::string, l1t::Mask> calol1_rs;  //= calol1.getMasks   ("processors");
0322 
0323       if (!readCaloLayer1OnlineSettings(m_params_helper, calol1_conf, calol1_rs))
0324         throw std::runtime_error("Parsing error for CaloLayer1");
0325 
0326     } catch (std::runtime_error& e) {
0327       edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << e.what();
0328       if (transactionSafe)
0329         throw std::runtime_error(std::string("SummaryForFunctionManager: Calo  | Faulty  | ") + e.what());
0330       else {
0331         edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "returning unmodified prototype of l1t::CaloParams";
0332         return std::make_unique<const l1t::CaloParams>(baseSettings);
0333       }
0334     }
0335   }
0336 
0337   if (exclusiveLayer == 0 || exclusiveLayer == 2) {
0338     try {
0339       l1t::TriggerSystem calol2;
0340       l1t::XmlConfigParser xmlReader2;
0341       xmlReader2.readDOMFromString(calol2_hw_payload);
0342       xmlReader2.readRootElement(calol2, "calol2");
0343 
0344       for (auto& conf : calol2_algo_payloads) {
0345         xmlReader2.readDOMFromString(conf.second);
0346         xmlReader2.readRootElement(calol2, "calol2");
0347       }
0348 
0349       //            calol2.setSystemId("calol2");
0350       calol2.setConfigured();
0351 
0352       std::map<std::string, l1t::Parameter> calol2_conf = calol2.getParameters("MP1");
0353       std::map<std::string, l1t::Parameter> calol2_conf_demux = calol2.getParameters("DEMUX");
0354       calol2_conf.insert(calol2_conf_demux.begin(), calol2_conf_demux.end());
0355       std::map<std::string, l1t::Mask> calol2_rs;  //= calol2.getMasks   ("processors");
0356 
0357       if (!readCaloLayer2OnlineSettings(m_params_helper, calol2_conf, calol2_rs))
0358         throw std::runtime_error("Parsing error for CaloLayer2");
0359 
0360     } catch (std::runtime_error& e) {
0361       edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << e.what();
0362       if (transactionSafe)
0363         throw std::runtime_error(std::string("SummaryForFunctionManager: Calo  | Faulty  | ") + e.what());
0364       else {
0365         edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "returning unmodified prototype of l1t::CaloParams";
0366         return std::make_unique<const l1t::CaloParams>(baseSettings);
0367       }
0368     }
0369   }
0370 
0371   auto retval = std::make_unique<const l1t::CaloParams>(m_params_helper);
0372 
0373   edm::LogInfo("L1-O2O: L1TCaloParamsOnlineProd") << "SummaryForFunctionManager: Calo  | OK      | All looks good";
0374   return retval;
0375 }
0376 
0377 //define this as a plug-in
0378 DEFINE_FWK_EVENTSETUP_MODULE(L1TCaloParamsOnlineProd);