CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1GtPsbSetupConfigOnlineProd.cc
Go to the documentation of this file.
1 
18 // this class header
20 
21 // system include files
22 #include "boost/lexical_cast.hpp"
23 #include <algorithm>
24 
25 // user include files
27 
28 // constructor
31 
32  // empty
33 
34 }
35 
36 // destructor
38 
39  // empty
40 
41 }
42 
43 // public methods
44 
45 boost::shared_ptr<L1GtPsbSetup> L1GtPsbSetupConfigOnlineProd::newObject(
46  const std::string& objectKey) {
47 
48  // shared pointer for L1GtPsbSetup
49  boost::shared_ptr<L1GtPsbSetup> pL1GtPsbSetup = boost::shared_ptr<L1GtPsbSetup>(
50  new L1GtPsbSetup());
51 
52  const std::string gtSchema = "CMS_GT";
53 
54  // the setup's contents, to be filled from database
55  std::vector<L1GtPsbConfig> psbConfigurations;
56 
57  // SELECT PSB_SLOT_*_SETUP_FK FROM CMS_GT.GT_SETUP WHERE GT_SETUP.ID = MyKey
58  std::vector<std::string> psbColumns = m_omdsReader.columnNames(gtSchema, "GT_SETUP");
59 
60  std::vector<std::string>::iterator newEnd = std::remove_if(
61  psbColumns.begin(), psbColumns.end(), &notPsbColumnName);
62  psbColumns.erase(newEnd, psbColumns.end());
63 
64  // select * from CMS_GT.GT_SETUP where GT_SETUP.ID = objectKey
66  psbColumns, gtSchema, "GT_SETUP", "GT_SETUP.ID",
67  m_omdsReader.singleAttribute(objectKey));
68 
69  // check if query was successful and produced one line only
70  if (!checkOneLineResult(psbKeys, "GT_SETUP query for PSB keys with ID = " + objectKey)) {
71  edm::LogError("L1-O2O")
72  << "Problem to get content of CMS_GT.GT_SETUP for GT_SETUP.ID key: "
73  << objectKey;
74  return pL1GtPsbSetup;
75  }
76 
77  // fill the psbConfigurations vector
78  for (std::vector<std::string>::const_iterator it = psbColumns.begin(); it != psbColumns.end(); ++it) {
79 
80  std::string psbKey;
81  psbKeys.fillVariable(*it, psbKey);
82 
83  if (psbKey.empty()) {
84  addDefaultPsb(*it, psbConfigurations);
85  } else {
86  addPsbFromDb(psbKey, psbConfigurations);
87  }
88  }
89 
90  // assign to the result object
91  pL1GtPsbSetup->setGtPsbSetup(psbConfigurations);
92 
93  if (edm::isDebugEnabled()) {
94  LogTrace("L1-O2O") << (*pL1GtPsbSetup) << std::endl;
95  }
96 
97  return pL1GtPsbSetup;
98 }
99 
100 // Return true if columnName does NOT match the pattern
101 // PSB_SLOT_.*_SETUP_FK
102 bool L1GtPsbSetupConfigOnlineProd::notPsbColumnName(const std::string& columnName) {
103 
104  static std::string startMatch("PSB_SLOT_");
105  static std::string endMatch("_SETUP_FK");
106 
107  unsigned len = columnName.size();
108 
109  // it's not a PSB column name if it's too short
110  return len <= ( startMatch.size() + endMatch.size() ) ||
111  // or the start doesn't match
112  columnName.substr(0, startMatch.size()) != startMatch ||
113  // or the end doesn't match
114  columnName.substr(len - endMatch.size(), endMatch.size()) != endMatch;
115 }
116 
117 void L1GtPsbSetupConfigOnlineProd::addPsbFromDb(const std::string& psbKey, std::vector<
118  L1GtPsbConfig>& psbSetup) const {
119 
120  // SQL> describe gt_psb_setup;
121  // (heavily pruned to just the stuff we need to set up a GtPsbConfig)
122  // Name Null? Type
123  // ----------------------------------------- -------- ----------------------------
124  // ID NOT NULL VARCHAR2(256)
125  // BOARD_SLOT NUMBER(3)
126  // CH0_SEND_LVDS_NOT_DS92LV16 NUMBER(1)
127  // CH1_SEND_LVDS_NOT_DS92LV16 NUMBER(1)
128  // ENABLE_TT_LVDS_0 NUMBER(1)
129  // ENABLE_TT_LVDS_1 NUMBER(1)
130  // ENABLE_TT_LVDS_2 NUMBER(1)
131  // ENABLE_TT_LVDS_3 NUMBER(1)
132  // ENABLE_TT_LVDS_4 NUMBER(1)
133  // ENABLE_TT_LVDS_5 NUMBER(1)
134  // ENABLE_TT_LVDS_6 NUMBER(1)
135  // ENABLE_TT_LVDS_7 NUMBER(1)
136  // ENABLE_TT_LVDS_8 NUMBER(1)
137  // ENABLE_TT_LVDS_9 NUMBER(1)
138  // ENABLE_TT_LVDS_10 NUMBER(1)
139  // ENABLE_TT_LVDS_11 NUMBER(1)
140  // ENABLE_TT_LVDS_12 NUMBER(1)
141  // ENABLE_TT_LVDS_13 NUMBER(1)
142  // ENABLE_TT_LVDS_14 NUMBER(1)
143  // ENABLE_TT_LVDS_15 NUMBER(1)
144  // SERLINK_CH0_REC_ENABLE NUMBER(1)
145  // SERLINK_CH1_REC_ENABLE NUMBER(1)
146  // SERLINK_CH2_REC_ENABLE NUMBER(1)
147  // SERLINK_CH3_REC_ENABLE NUMBER(1)
148  // SERLINK_CH4_REC_ENABLE NUMBER(1)
149  // SERLINK_CH5_REC_ENABLE NUMBER(1)
150  // SERLINK_CH6_REC_ENABLE NUMBER(1)
151  // SERLINK_CH7_REC_ENABLE NUMBER(1)
152 
153  const std::string gtSchema = "CMS_GT";
154 
155  // setup up columns to query
156  std::vector<std::string> columnNames;
157 
158  static const std::string lvdPrefix = "ENABLE_TT_LVDS_";
159  static const std::string lvdSuffix = "";
160  static const std::string serPrefix = "SERLINK_CH";
161  static const std::string serSuffix = "_REC_ENABLE";
162  static const std::string boardSlotColumn = "BOARD_SLOT";
163  static const std::string ch0FormatColumn = "CH0_SEND_LVDS_NOT_DS92LV16";
164  static const std::string ch1FormatColumn = "CH1_SEND_LVDS_NOT_DS92LV16";
165 
166  columnNames.push_back(boardSlotColumn);
167  columnNames.push_back(ch0FormatColumn);
168  columnNames.push_back(ch1FormatColumn);
169 
170  for (unsigned i = 0; i < (unsigned) L1GtPsbConfig::PsbNumberLvdsGroups; ++i) {
171  columnNames.push_back(numberedColumnName(lvdPrefix, i, lvdSuffix));
172  }
173 
174  for (unsigned i = 0; i < (unsigned) L1GtPsbConfig::PsbSerLinkNumberChannels; ++i) {
175  columnNames.push_back(numberedColumnName(serPrefix, i, serSuffix));
176  }
177 
178  // execute database query
180  columnNames, gtSchema, "GT_PSB_SETUP", "GT_PSB_SETUP.ID", m_omdsReader.singleAttribute(
181  psbKey));
182 
183  // check if query was successful and we get only one line
184  if (!checkOneLineResult(psbQuery, "GT_PSB_SETUP query for PSB keys with ID = " + psbKey)) {
185  edm::LogError("L1-O2O")
186  << "Problem to get setup for PSB keys with ID = " << psbKey;
187  return; // FIXME: change method to bool?
188  }
189 
190  // extract values
191  int16_t boardSlot;
192 
193  getRequiredValue(psbQuery, boardSlotColumn, boardSlot);
194 
195  bool sendLvds0, sendLvds1;
196 
197  getRequiredValue(psbQuery, ch0FormatColumn, sendLvds0);
198  getRequiredValue(psbQuery, ch1FormatColumn, sendLvds1);
199 
200  const std::vector<bool>& enableRecLvds = extractBoolVector(
201  psbQuery, lvdPrefix, lvdSuffix, L1GtPsbConfig::PsbNumberLvdsGroups);
202  const std::vector<bool>& serLinkRecEnable = extractBoolVector(
203  psbQuery, serPrefix, serSuffix, L1GtPsbConfig::PsbSerLinkNumberChannels);
204 
205  // create new PSB object with db values
206  L1GtPsbConfig toAdd;
207 
208  toAdd.setGtBoardSlot(boardSlot);
209  toAdd.setGtPsbCh0SendLvds(sendLvds0);
210  toAdd.setGtPsbCh1SendLvds(sendLvds1);
211  toAdd.setGtPsbEnableRecLvds(enableRecLvds);
212  toAdd.setGtPsbEnableRecSerLink(serLinkRecEnable);
213 
214  // add to vector
215  psbSetup.push_back(toAdd);
216 }
217 
218 void L1GtPsbSetupConfigOnlineProd::addDefaultPsb(const std::string& psbColumn, std::vector<
219  L1GtPsbConfig>& psbSetup) const {
220 
221  // deduce the assigned board from the column name
222  unsigned boardSlot = numberFromString(psbColumn);
223 
224  // create a default PsbConfig with the appropriate board slot and all links disabled
225  L1GtPsbConfig toAdd;
226  static std::vector<bool> allFalseLvds(L1GtPsbConfig::PsbNumberLvdsGroups, false);
227  static std::vector<bool> allFalseSerLink(L1GtPsbConfig::PsbSerLinkNumberChannels, false);
228 
229  toAdd.setGtBoardSlot(boardSlot);
230  toAdd.setGtPsbCh0SendLvds(false);
231  toAdd.setGtPsbCh1SendLvds(false);
232  toAdd.setGtPsbEnableRecLvds(allFalseLvds);
233  toAdd.setGtPsbEnableRecSerLink(allFalseSerLink);
234 
235  psbSetup.push_back(toAdd);
236 }
237 
239  const l1t::OMDSReader::QueryResults& query, const std::string& prefix,
240  const std::string& suffix, unsigned nColumns) const {
241 
242  std::vector<bool> result(nColumns);
243 
244  for (unsigned i = 0; i < nColumns; ++i) {
245  bool dbValue;
246  getRequiredValue(query, numberedColumnName(prefix, i, suffix), dbValue);
247  result[i] = dbValue;
248  }
249 
250  return result;
251 }
252 
254  const l1t::OMDSReader::QueryResults& result, const std::string& queryDescription) const {
255 
256  // check if query was successful
257  if (result.queryFailed()) {
258  edm::LogError("L1-O2O") << "\n " << queryDescription + " failed: no match found!";
259  return false;
260 
261  } else if (result.numberRows() != 1) {
262  edm::LogError("L1-O2O") << "\nProblem with " << queryDescription << ": "
263  << ( result.numberRows() ) << " rows were returned, expected 1.";
264  return false;
265  }
266 
267  return true;
268 }
269 
270 
272  const std::string& prefix, unsigned number) const {
273 
274  return numberedColumnName(prefix, number, "");
275 
276 }
277 
279  const std::string& prefix, unsigned number, const std::string& suffix) const {
280 
281  std::ostringstream colName;
282  colName << prefix << number << suffix;
283 
284  return colName.str();
285 }
286 
287 unsigned L1GtPsbSetupConfigOnlineProd::numberFromString(const std::string& aString) const {
288 
289  std::istringstream stream(aString);
290  unsigned result;
291 
292  for (unsigned i = 0; i < aString.size(); ++i) {
293  if (stream >> result) {
294  // we got a value
295  return result;
296  } else {
297  // clear error flags from failed >>
298  stream.clear();
299  // skip another character
300  stream.seekg(i);
301  }
302  }
303 
304  // throw here
305  throw cms::Exception("NumberNotFound") << "Failed to extract numeric value from " << aString;
306 }
307 
bool checkOneLineResult(const l1t::OMDSReader::QueryResults &result, const std::string &queryDescription) const
Ensures that result contains exactly one line, returning false otherwise.
bool isDebugEnabled()
static bool notPsbColumnName(const std::string &columnName)
int i
Definition: DBlmapReader.cc:9
std::string numberedColumnName(const std::string &prefix, unsigned number, const std::string &suffix) const
Concatenates prefix, number and suffix into a string.
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:311
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:295
void getRequiredValue(const l1t::OMDSReader::QueryResults &result, const std::string &colName, T &value) const
void setGtPsbEnableRecLvds(const std::vector< bool > &)
void setGtPsbCh0SendLvds(const bool &)
void addDefaultPsb(const std::string &psbColumn, std::vector< L1GtPsbConfig > &psbSetup) const
Creates a default valued PSB from an empty foreign key in the GT_SETUP table.
static const int PsbSerLinkNumberChannels
number of channels per board
Definition: L1GtPsbConfig.h:65
virtual boost::shared_ptr< L1GtPsbSetup > newObject(const std::string &objectKey)
public methods
static const int PsbNumberLvdsGroups
number of LVDS groups per board
Definition: L1GtPsbConfig.h:62
void setGtPsbEnableRecSerLink(const std::vector< bool > &)
tuple result
Definition: query.py:137
const QueryResults basicQuery(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &tableName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="") const
Definition: OMDSReader.cc:87
#define LogTrace(id)
std::vector< bool > extractBoolVector(const l1t::OMDSReader::QueryResults &query, const std::string &prefix, const std::string &suffix, unsigned nColumns) const
void setGtBoardSlot(const int &)
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:48
L1GtPsbSetupConfigOnlineProd(const edm::ParameterSet &)
constructor
void setGtPsbCh1SendLvds(const bool &)
tuple query
Definition: o2o.py:269
void addPsbFromDb(const std::string &psbKey, std::vector< L1GtPsbConfig > &psbSetup) const
Creates a new PSB object from a GT_PSB_SETUP entry and adds.
std::vector< std::string > columnNames(const std::string &schemaName, const std::string &tableName) const
Definition: OMDSReader.cc:165
unsigned numberFromString(const std::string &aString) const