CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/L1TriggerConfig/L1GtConfigProducers/src/L1GtPsbSetupConfigOnlineProd.cc

Go to the documentation of this file.
00001 
00018 // this class header
00019 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtPsbSetupConfigOnlineProd.h"
00020 
00021 // system include files
00022 #include "boost/lexical_cast.hpp"
00023 #include <algorithm>
00024 
00025 // user include files
00026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00027 
00028 // constructor
00029 L1GtPsbSetupConfigOnlineProd::L1GtPsbSetupConfigOnlineProd(const edm::ParameterSet& parSet) :
00030     L1ConfigOnlineProdBase<L1GtPsbSetupRcd, L1GtPsbSetup> (parSet) {
00031 
00032     // empty
00033 
00034 }
00035 
00036 // destructor
00037 L1GtPsbSetupConfigOnlineProd::~L1GtPsbSetupConfigOnlineProd() {
00038 
00039     // empty
00040 
00041 }
00042 
00043 // public methods
00044 
00045 boost::shared_ptr<L1GtPsbSetup> L1GtPsbSetupConfigOnlineProd::newObject(
00046         const std::string& objectKey) {
00047 
00048     // shared pointer for L1GtPsbSetup
00049     boost::shared_ptr<L1GtPsbSetup> pL1GtPsbSetup = boost::shared_ptr<L1GtPsbSetup>(
00050             new L1GtPsbSetup());
00051 
00052     const std::string gtSchema = "CMS_GT";
00053 
00054     // the setup's contents, to be filled from database
00055     std::vector<L1GtPsbConfig> psbConfigurations;
00056 
00057     // SELECT PSB_SLOT_*_SETUP_FK FROM CMS_GT.GT_SETUP WHERE GT_SETUP.ID = MyKey
00058     std::vector<std::string> psbColumns = m_omdsReader.columnNames(gtSchema, "GT_SETUP");
00059 
00060     std::vector<std::string>::iterator newEnd = std::remove_if(
00061             psbColumns.begin(), psbColumns.end(), &notPsbColumnName);
00062     psbColumns.erase(newEnd, psbColumns.end());
00063 
00064     // select * from CMS_GT.GT_SETUP where GT_SETUP.ID = objectKey
00065     l1t::OMDSReader::QueryResults psbKeys = m_omdsReader.basicQuery(
00066             psbColumns, gtSchema, "GT_SETUP", "GT_SETUP.ID",
00067             m_omdsReader.singleAttribute(objectKey));
00068 
00069     // check if query was successful and produced one line only
00070     if (!checkOneLineResult(psbKeys, "GT_SETUP query for PSB keys with ID = " + objectKey)) {
00071         edm::LogError("L1-O2O")
00072             << "Problem to get content of CMS_GT.GT_SETUP for GT_SETUP.ID key: "
00073             << objectKey;
00074         return pL1GtPsbSetup;
00075     }
00076 
00077     // fill the psbConfigurations vector
00078     for (std::vector<std::string>::const_iterator it = psbColumns.begin(); it != psbColumns.end(); ++it) {
00079 
00080         std::string psbKey;
00081         psbKeys.fillVariable(*it, psbKey);
00082 
00083         if (psbKey.empty()) {
00084             addDefaultPsb(*it, psbConfigurations);
00085         } else {
00086             addPsbFromDb(psbKey, psbConfigurations);
00087         }
00088     }
00089 
00090     // assign to the result object
00091     pL1GtPsbSetup->setGtPsbSetup(psbConfigurations);
00092 
00093     if (edm::isDebugEnabled()) {
00094         LogTrace("L1-O2O") << (*pL1GtPsbSetup) << std::endl;
00095     }
00096 
00097     return pL1GtPsbSetup;
00098 }
00099 
00100 // Return true if columnName does NOT match the pattern
00101 // PSB_SLOT_.*_SETUP_FK
00102 bool L1GtPsbSetupConfigOnlineProd::notPsbColumnName(const std::string& columnName) {
00103 
00104     static std::string startMatch("PSB_SLOT_");
00105     static std::string endMatch("_SETUP_FK");
00106 
00107     unsigned len = columnName.size();
00108 
00109     // it's not a PSB column name if it's too short
00110     return len <= ( startMatch.size() + endMatch.size() ) ||
00111     // or the start doesn't match
00112             columnName.substr(0, startMatch.size()) != startMatch ||
00113     // or the end doesn't match
00114             columnName.substr(len - endMatch.size(), endMatch.size()) != endMatch;
00115 }
00116 
00117 void L1GtPsbSetupConfigOnlineProd::addPsbFromDb(const std::string& psbKey, std::vector<
00118         L1GtPsbConfig>& psbSetup) const {
00119 
00120     // SQL> describe gt_psb_setup;
00121     // (heavily pruned to just the stuff we need to set up a GtPsbConfig)
00122     //  Name                                      Null?    Type
00123     //  ----------------------------------------- -------- ----------------------------
00124     //  ID                                        NOT NULL VARCHAR2(256)
00125     //  BOARD_SLOT                                         NUMBER(3)
00126     //  CH0_SEND_LVDS_NOT_DS92LV16                         NUMBER(1)
00127     //  CH1_SEND_LVDS_NOT_DS92LV16                         NUMBER(1)
00128     //  ENABLE_TT_LVDS_0                                   NUMBER(1)
00129     //  ENABLE_TT_LVDS_1                                   NUMBER(1)
00130     //  ENABLE_TT_LVDS_2                                   NUMBER(1)
00131     //  ENABLE_TT_LVDS_3                                   NUMBER(1)
00132     //  ENABLE_TT_LVDS_4                                   NUMBER(1)
00133     //  ENABLE_TT_LVDS_5                                   NUMBER(1)
00134     //  ENABLE_TT_LVDS_6                                   NUMBER(1)
00135     //  ENABLE_TT_LVDS_7                                   NUMBER(1)
00136     //  ENABLE_TT_LVDS_8                                   NUMBER(1)
00137     //  ENABLE_TT_LVDS_9                                   NUMBER(1)
00138     //  ENABLE_TT_LVDS_10                                  NUMBER(1)
00139     //  ENABLE_TT_LVDS_11                                  NUMBER(1)
00140     //  ENABLE_TT_LVDS_12                                  NUMBER(1)
00141     //  ENABLE_TT_LVDS_13                                  NUMBER(1)
00142     //  ENABLE_TT_LVDS_14                                  NUMBER(1)
00143     //  ENABLE_TT_LVDS_15                                  NUMBER(1)
00144     //  SERLINK_CH0_REC_ENABLE                             NUMBER(1)
00145     //  SERLINK_CH1_REC_ENABLE                             NUMBER(1)
00146     //  SERLINK_CH2_REC_ENABLE                             NUMBER(1)
00147     //  SERLINK_CH3_REC_ENABLE                             NUMBER(1)
00148     //  SERLINK_CH4_REC_ENABLE                             NUMBER(1)
00149     //  SERLINK_CH5_REC_ENABLE                             NUMBER(1)
00150     //  SERLINK_CH6_REC_ENABLE                             NUMBER(1)
00151     //  SERLINK_CH7_REC_ENABLE                             NUMBER(1)
00152 
00153     const std::string gtSchema = "CMS_GT";
00154 
00155     // setup up columns to query
00156     std::vector<std::string> columnNames;
00157 
00158     static const std::string lvdPrefix = "ENABLE_TT_LVDS_";
00159     static const std::string lvdSuffix = "";
00160     static const std::string serPrefix = "SERLINK_CH";
00161     static const std::string serSuffix = "_REC_ENABLE";
00162     static const std::string boardSlotColumn = "BOARD_SLOT";
00163     static const std::string ch0FormatColumn = "CH0_SEND_LVDS_NOT_DS92LV16";
00164     static const std::string ch1FormatColumn = "CH1_SEND_LVDS_NOT_DS92LV16";
00165 
00166     columnNames.push_back(boardSlotColumn);
00167     columnNames.push_back(ch0FormatColumn);
00168     columnNames.push_back(ch1FormatColumn);
00169 
00170     for (unsigned i = 0; i < (unsigned) L1GtPsbConfig::PsbNumberLvdsGroups; ++i) {
00171         columnNames.push_back(numberedColumnName(lvdPrefix, i, lvdSuffix));
00172     }
00173 
00174     for (unsigned i = 0; i < (unsigned) L1GtPsbConfig::PsbSerLinkNumberChannels; ++i) {
00175         columnNames.push_back(numberedColumnName(serPrefix, i, serSuffix));
00176     }
00177 
00178     // execute database query
00179     l1t::OMDSReader::QueryResults psbQuery = m_omdsReader.basicQuery(
00180             columnNames, gtSchema, "GT_PSB_SETUP", "GT_PSB_SETUP.ID", m_omdsReader.singleAttribute(
00181                     psbKey));
00182 
00183     // check if query was successful and we get only one line
00184     if (!checkOneLineResult(psbQuery, "GT_PSB_SETUP query for PSB keys with ID = " + psbKey)) {
00185         edm::LogError("L1-O2O")
00186             << "Problem to get setup for PSB keys with ID = " << psbKey;
00187         return; // FIXME: change method to bool?
00188     }
00189 
00190     // extract values
00191     int16_t boardSlot;
00192 
00193     getRequiredValue(psbQuery, boardSlotColumn, boardSlot);
00194 
00195     bool sendLvds0, sendLvds1;
00196 
00197     getRequiredValue(psbQuery, ch0FormatColumn, sendLvds0);
00198     getRequiredValue(psbQuery, ch1FormatColumn, sendLvds1);
00199 
00200     const std::vector<bool>& enableRecLvds = extractBoolVector(
00201             psbQuery, lvdPrefix, lvdSuffix, L1GtPsbConfig::PsbNumberLvdsGroups);
00202     const std::vector<bool>& serLinkRecEnable = extractBoolVector(
00203             psbQuery, serPrefix, serSuffix, L1GtPsbConfig::PsbSerLinkNumberChannels);
00204 
00205     // create new PSB object with db values
00206     L1GtPsbConfig toAdd;
00207 
00208     toAdd.setGtBoardSlot(boardSlot);
00209     toAdd.setGtPsbCh0SendLvds(sendLvds0);
00210     toAdd.setGtPsbCh1SendLvds(sendLvds1);
00211     toAdd.setGtPsbEnableRecLvds(enableRecLvds);
00212     toAdd.setGtPsbEnableRecSerLink(serLinkRecEnable);
00213 
00214     // add to vector
00215     psbSetup.push_back(toAdd);
00216 }
00217 
00218 void L1GtPsbSetupConfigOnlineProd::addDefaultPsb(const std::string& psbColumn, std::vector<
00219         L1GtPsbConfig>& psbSetup) const {
00220 
00221     // deduce the assigned board from the column name
00222     unsigned boardSlot = numberFromString(psbColumn);
00223 
00224     // create a default PsbConfig with the appropriate board slot and all links disabled
00225     L1GtPsbConfig toAdd;
00226     static std::vector<bool> allFalseLvds(L1GtPsbConfig::PsbNumberLvdsGroups, false);
00227     static std::vector<bool> allFalseSerLink(L1GtPsbConfig::PsbSerLinkNumberChannels, false);
00228 
00229     toAdd.setGtBoardSlot(boardSlot);
00230     toAdd.setGtPsbCh0SendLvds(false);
00231     toAdd.setGtPsbCh1SendLvds(false);
00232     toAdd.setGtPsbEnableRecLvds(allFalseLvds);
00233     toAdd.setGtPsbEnableRecSerLink(allFalseSerLink);
00234 
00235     psbSetup.push_back(toAdd);
00236 }
00237 
00238 std::vector<bool> L1GtPsbSetupConfigOnlineProd::extractBoolVector(
00239         const l1t::OMDSReader::QueryResults& query, const std::string& prefix,
00240         const std::string& suffix, unsigned nColumns) const {
00241 
00242     std::vector<bool> result(nColumns);
00243 
00244     for (unsigned i = 0; i < nColumns; ++i) {
00245         bool dbValue;
00246         getRequiredValue(query, numberedColumnName(prefix, i, suffix), dbValue);
00247         result[i] = dbValue;
00248     }
00249 
00250     return result;
00251 }
00252 
00253 bool L1GtPsbSetupConfigOnlineProd::checkOneLineResult(
00254         const l1t::OMDSReader::QueryResults& result, const std::string& queryDescription) const {
00255 
00256     // check if query was successful
00257     if (result.queryFailed()) {
00258         edm::LogError("L1-O2O") << "\n " << queryDescription + " failed: no match found!";
00259         return false;
00260 
00261     } else if (result.numberRows() != 1) {
00262         edm::LogError("L1-O2O") << "\nProblem with " << queryDescription << ": "
00263                 << ( result.numberRows() ) << " rows were returned, expected 1.";
00264         return false;
00265     }
00266 
00267     return true;
00268 }
00269 
00270 
00271 std::string L1GtPsbSetupConfigOnlineProd::numberedColumnName(
00272         const std::string& prefix, unsigned number) const {
00273 
00274     return numberedColumnName(prefix, number, "");
00275 
00276 }
00277 
00278 std::string L1GtPsbSetupConfigOnlineProd::numberedColumnName(
00279         const std::string& prefix, unsigned number, const std::string& suffix) const {
00280 
00281     std::ostringstream colName;
00282     colName << prefix << number << suffix;
00283 
00284     return colName.str();
00285 }
00286 
00287 unsigned L1GtPsbSetupConfigOnlineProd::numberFromString(const std::string& aString) const {
00288 
00289     std::istringstream stream(aString);
00290     unsigned result;
00291 
00292     for (unsigned i = 0; i < aString.size(); ++i) {
00293         if (stream >> result) {
00294             // we got a value
00295             return result;
00296         } else {
00297             // clear error flags from failed >>
00298             stream.clear();
00299             // skip another character
00300             stream.seekg(i);
00301         }
00302     }
00303 
00304     // throw here
00305     throw cms::Exception("NumberNotFound") << "Failed to extract numeric value from " << aString;
00306 }
00307 
00308 DEFINE_FWK_EVENTSETUP_MODULE( L1GtPsbSetupConfigOnlineProd);