Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /**
0002  * \class L1GtPsbSetupConfigOnlineProd
0003  *
0004  *
0005  * Description: online producer for L1GtPsbSetup.
0006  *
0007  * Implementation:
0008  *    <TODO: enter implementation details>
0009  *
0010  * \author: Vasile Mihai Ghete - HEPHY Vienna
0011  * \author: Thomas Themel      - HEPHY Vienna
0012  *
0013  *
0014  */
0015 
0016 // this class header
0017 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtPsbSetupConfigOnlineProd.h"
0018 
0019 // system include files
0020 #include <algorithm>
0021 
0022 // user include files
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 
0025 // constructor
0026 L1GtPsbSetupConfigOnlineProd::L1GtPsbSetupConfigOnlineProd(const edm::ParameterSet& parSet)
0027     : L1ConfigOnlineProdBase<L1GtPsbSetupRcd, L1GtPsbSetup>(parSet) {
0028   // empty
0029 }
0030 
0031 // destructor
0032 L1GtPsbSetupConfigOnlineProd::~L1GtPsbSetupConfigOnlineProd() {
0033   // empty
0034 }
0035 
0036 // public methods
0037 
0038 std::unique_ptr<L1GtPsbSetup> L1GtPsbSetupConfigOnlineProd::newObject(const std::string& objectKey) {
0039   auto pL1GtPsbSetup = std::make_unique<L1GtPsbSetup>();
0040 
0041   const std::string gtSchema = "CMS_GT";
0042 
0043   // the setup's contents, to be filled from database
0044   std::vector<L1GtPsbConfig> psbConfigurations;
0045 
0046   // SELECT PSB_SLOT_*_SETUP_FK FROM CMS_GT.GT_SETUP WHERE GT_SETUP.ID = MyKey
0047   std::vector<std::string> psbColumns = m_omdsReader.columnNames(gtSchema, "GT_SETUP");
0048 
0049   std::vector<std::string>::iterator newEnd = std::remove_if(psbColumns.begin(), psbColumns.end(), &notPsbColumnName);
0050   psbColumns.erase(newEnd, psbColumns.end());
0051 
0052   // select * from CMS_GT.GT_SETUP where GT_SETUP.ID = objectKey
0053   l1t::OMDSReader::QueryResults psbKeys =
0054       m_omdsReader.basicQuery(psbColumns, gtSchema, "GT_SETUP", "GT_SETUP.ID", m_omdsReader.singleAttribute(objectKey));
0055 
0056   // check if query was successful and produced one line only
0057   if (!checkOneLineResult(psbKeys, "GT_SETUP query for PSB keys with ID = " + objectKey)) {
0058     edm::LogError("L1-O2O") << "Problem to get content of CMS_GT.GT_SETUP for GT_SETUP.ID key: " << objectKey;
0059     return pL1GtPsbSetup;
0060   }
0061 
0062   // fill the psbConfigurations vector
0063   for (std::vector<std::string>::const_iterator it = psbColumns.begin(); it != psbColumns.end(); ++it) {
0064     std::string psbKey;
0065     psbKeys.fillVariable(*it, psbKey);
0066 
0067     if (psbKey.empty()) {
0068       addDefaultPsb(*it, psbConfigurations);
0069     } else {
0070       addPsbFromDb(psbKey, psbConfigurations);
0071     }
0072   }
0073 
0074   // assign to the result object
0075   pL1GtPsbSetup->setGtPsbSetup(psbConfigurations);
0076 
0077   if (edm::isDebugEnabled()) {
0078     LogTrace("L1-O2O") << (*pL1GtPsbSetup) << std::endl;
0079   }
0080 
0081   return pL1GtPsbSetup;
0082 }
0083 
0084 // Return true if columnName does NOT match the pattern
0085 // PSB_SLOT_.*_SETUP_FK
0086 bool L1GtPsbSetupConfigOnlineProd::notPsbColumnName(const std::string& columnName) {
0087   static std::string startMatch("PSB_SLOT_");
0088   static std::string endMatch("_SETUP_FK");
0089 
0090   unsigned len = columnName.size();
0091 
0092   // it's not a PSB column name if it's too short
0093   return len <= (startMatch.size() + endMatch.size()) ||
0094          // or the start doesn't match
0095          columnName.substr(0, startMatch.size()) != startMatch ||
0096          // or the end doesn't match
0097          columnName.substr(len - endMatch.size(), endMatch.size()) != endMatch;
0098 }
0099 
0100 void L1GtPsbSetupConfigOnlineProd::addPsbFromDb(const std::string& psbKey, std::vector<L1GtPsbConfig>& psbSetup) {
0101   // SQL> describe gt_psb_setup;
0102   // (heavily pruned to just the stuff we need to set up a GtPsbConfig)
0103   //  Name                                      Null?    Type
0104   //  ----------------------------------------- -------- ----------------------------
0105   //  ID                                        NOT NULL VARCHAR2(256)
0106   //  BOARD_SLOT                                         NUMBER(3)
0107   //  CH0_SEND_LVDS_NOT_DS92LV16                         NUMBER(1)
0108   //  CH1_SEND_LVDS_NOT_DS92LV16                         NUMBER(1)
0109   //  ENABLE_TT_LVDS_0                                   NUMBER(1)
0110   //  ENABLE_TT_LVDS_1                                   NUMBER(1)
0111   //  ENABLE_TT_LVDS_2                                   NUMBER(1)
0112   //  ENABLE_TT_LVDS_3                                   NUMBER(1)
0113   //  ENABLE_TT_LVDS_4                                   NUMBER(1)
0114   //  ENABLE_TT_LVDS_5                                   NUMBER(1)
0115   //  ENABLE_TT_LVDS_6                                   NUMBER(1)
0116   //  ENABLE_TT_LVDS_7                                   NUMBER(1)
0117   //  ENABLE_TT_LVDS_8                                   NUMBER(1)
0118   //  ENABLE_TT_LVDS_9                                   NUMBER(1)
0119   //  ENABLE_TT_LVDS_10                                  NUMBER(1)
0120   //  ENABLE_TT_LVDS_11                                  NUMBER(1)
0121   //  ENABLE_TT_LVDS_12                                  NUMBER(1)
0122   //  ENABLE_TT_LVDS_13                                  NUMBER(1)
0123   //  ENABLE_TT_LVDS_14                                  NUMBER(1)
0124   //  ENABLE_TT_LVDS_15                                  NUMBER(1)
0125   //  SERLINK_CH0_REC_ENABLE                             NUMBER(1)
0126   //  SERLINK_CH1_REC_ENABLE                             NUMBER(1)
0127   //  SERLINK_CH2_REC_ENABLE                             NUMBER(1)
0128   //  SERLINK_CH3_REC_ENABLE                             NUMBER(1)
0129   //  SERLINK_CH4_REC_ENABLE                             NUMBER(1)
0130   //  SERLINK_CH5_REC_ENABLE                             NUMBER(1)
0131   //  SERLINK_CH6_REC_ENABLE                             NUMBER(1)
0132   //  SERLINK_CH7_REC_ENABLE                             NUMBER(1)
0133 
0134   const std::string gtSchema = "CMS_GT";
0135 
0136   // setup up columns to query
0137   std::vector<std::string> columnNames;
0138 
0139   static const std::string lvdPrefix = "ENABLE_TT_LVDS_";
0140   static const std::string lvdSuffix = "";
0141   static const std::string serPrefix = "SERLINK_CH";
0142   static const std::string serSuffix = "_REC_ENABLE";
0143   static const std::string boardSlotColumn = "BOARD_SLOT";
0144   static const std::string ch0FormatColumn = "CH0_SEND_LVDS_NOT_DS92LV16";
0145   static const std::string ch1FormatColumn = "CH1_SEND_LVDS_NOT_DS92LV16";
0146 
0147   columnNames.push_back(boardSlotColumn);
0148   columnNames.push_back(ch0FormatColumn);
0149   columnNames.push_back(ch1FormatColumn);
0150 
0151   for (unsigned i = 0; i < (unsigned)L1GtPsbConfig::PsbNumberLvdsGroups; ++i) {
0152     columnNames.push_back(numberedColumnName(lvdPrefix, i, lvdSuffix));
0153   }
0154 
0155   for (unsigned i = 0; i < (unsigned)L1GtPsbConfig::PsbSerLinkNumberChannels; ++i) {
0156     columnNames.push_back(numberedColumnName(serPrefix, i, serSuffix));
0157   }
0158 
0159   // execute database query
0160   l1t::OMDSReader::QueryResults psbQuery = m_omdsReader.basicQuery(
0161       columnNames, gtSchema, "GT_PSB_SETUP", "GT_PSB_SETUP.ID", m_omdsReader.singleAttribute(psbKey));
0162 
0163   // check if query was successful and we get only one line
0164   if (!checkOneLineResult(psbQuery, "GT_PSB_SETUP query for PSB keys with ID = " + psbKey)) {
0165     edm::LogError("L1-O2O") << "Problem to get setup for PSB keys with ID = " << psbKey;
0166     return;  // FIXME: change method to bool?
0167   }
0168 
0169   // extract values
0170   int16_t boardSlot;
0171 
0172   getRequiredValue(psbQuery, boardSlotColumn, boardSlot);
0173 
0174   bool sendLvds0, sendLvds1;
0175 
0176   getRequiredValue(psbQuery, ch0FormatColumn, sendLvds0);
0177   getRequiredValue(psbQuery, ch1FormatColumn, sendLvds1);
0178 
0179   const std::vector<bool>& enableRecLvds =
0180       extractBoolVector(psbQuery, lvdPrefix, lvdSuffix, L1GtPsbConfig::PsbNumberLvdsGroups);
0181   const std::vector<bool>& serLinkRecEnable =
0182       extractBoolVector(psbQuery, serPrefix, serSuffix, L1GtPsbConfig::PsbSerLinkNumberChannels);
0183 
0184   // create new PSB object with db values
0185   L1GtPsbConfig toAdd;
0186 
0187   toAdd.setGtBoardSlot(boardSlot);
0188   toAdd.setGtPsbCh0SendLvds(sendLvds0);
0189   toAdd.setGtPsbCh1SendLvds(sendLvds1);
0190   toAdd.setGtPsbEnableRecLvds(enableRecLvds);
0191   toAdd.setGtPsbEnableRecSerLink(serLinkRecEnable);
0192 
0193   // add to vector
0194   psbSetup.push_back(toAdd);
0195 }
0196 
0197 void L1GtPsbSetupConfigOnlineProd::addDefaultPsb(const std::string& psbColumn,
0198                                                  std::vector<L1GtPsbConfig>& psbSetup) const {
0199   // deduce the assigned board from the column name
0200   unsigned boardSlot = numberFromString(psbColumn);
0201 
0202   // create a default PsbConfig with the appropriate board slot and all links disabled
0203   L1GtPsbConfig toAdd;
0204   static std::vector<bool> allFalseLvds(L1GtPsbConfig::PsbNumberLvdsGroups, false);
0205   static std::vector<bool> allFalseSerLink(L1GtPsbConfig::PsbSerLinkNumberChannels, false);
0206 
0207   toAdd.setGtBoardSlot(boardSlot);
0208   toAdd.setGtPsbCh0SendLvds(false);
0209   toAdd.setGtPsbCh1SendLvds(false);
0210   toAdd.setGtPsbEnableRecLvds(allFalseLvds);
0211   toAdd.setGtPsbEnableRecSerLink(allFalseSerLink);
0212 
0213   psbSetup.push_back(toAdd);
0214 }
0215 
0216 std::vector<bool> L1GtPsbSetupConfigOnlineProd::extractBoolVector(const l1t::OMDSReader::QueryResults& query,
0217                                                                   const std::string& prefix,
0218                                                                   const std::string& suffix,
0219                                                                   unsigned nColumns) const {
0220   std::vector<bool> result(nColumns);
0221 
0222   for (unsigned i = 0; i < nColumns; ++i) {
0223     bool dbValue;
0224     getRequiredValue(query, numberedColumnName(prefix, i, suffix), dbValue);
0225     result[i] = dbValue;
0226   }
0227 
0228   return result;
0229 }
0230 
0231 bool L1GtPsbSetupConfigOnlineProd::checkOneLineResult(const l1t::OMDSReader::QueryResults& result,
0232                                                       const std::string& queryDescription) const {
0233   // check if query was successful
0234   if (result.queryFailed()) {
0235     edm::LogError("L1-O2O") << "\n " << queryDescription + " failed: no match found!";
0236     return false;
0237 
0238   } else if (result.numberRows() != 1) {
0239     edm::LogError("L1-O2O") << "\nProblem with " << queryDescription << ": " << (result.numberRows())
0240                             << " rows were returned, expected 1.";
0241     return false;
0242   }
0243 
0244   return true;
0245 }
0246 
0247 std::string L1GtPsbSetupConfigOnlineProd::numberedColumnName(const std::string& prefix, unsigned number) const {
0248   return numberedColumnName(prefix, number, "");
0249 }
0250 
0251 std::string L1GtPsbSetupConfigOnlineProd::numberedColumnName(const std::string& prefix,
0252                                                              unsigned number,
0253                                                              const std::string& suffix) const {
0254   std::ostringstream colName;
0255   colName << prefix << number << suffix;
0256 
0257   return colName.str();
0258 }
0259 
0260 unsigned L1GtPsbSetupConfigOnlineProd::numberFromString(const std::string& aString) const {
0261   std::istringstream stream(aString);
0262   unsigned result;
0263 
0264   for (unsigned i = 0; i < aString.size(); ++i) {
0265     if (stream >> result) {
0266       // we got a value
0267       return result;
0268     } else {
0269       // clear error flags from failed >>
0270       stream.clear();
0271       // skip another character
0272       stream.seekg(i);
0273     }
0274   }
0275 
0276   // throw here
0277   throw cms::Exception("NumberNotFound") << "Failed to extract numeric value from " << aString;
0278 }
0279 
0280 DEFINE_FWK_EVENTSETUP_MODULE(L1GtPsbSetupConfigOnlineProd);