CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalChannelQualityXml.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CaloOnlineTools/HcalOnlineDb
4 // Class : HcalChannelQualityXml
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Gena Kukartsev, kukarzev@fnal.gov
10 // Created: Wed Jul 01 06:30:00 CDT 2009
11 //
12 
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 
20 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
22 
23 #ifdef HAVE_XDAQ
24 #include <toolbox/string.h>
25 #else
26 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h" // Replaces toolbox::toString
27 #endif
28 
29 using namespace std;
30 using namespace oracle::occi;
31 
32 
34  status = -1;
35  onoff = -1;
36  comment = "Comment has not been entered";
37 }
38 
39 
41 /*
42  :extension_table_name(""),
43  type_name(""),
44  run_number(-1),
45  channel_map("HCAL_CHANNELS"),
46  data_set_id(-1),
47  iov_id(1),
48  iov_begin(1),
49  iov_end(-1),
50  tag_id(2),
51  tag_mode("auto"),
52  tag_name("test_tag_name"),
53  detector_name("HCAL"),
54  comment(""),
55  tag_idref(2),
56  iov_idref(1),
57  data_set_idref(-1)
58 */
59 {
60  extension_table_name="HCAL_CHANNEL_QUALITY_V1";
61  type_name="HCAL Channel Quality [V1]";
62  run_number = -1;
63  channel_map = "HCAL_CHANNELS";
64  data_set_id = -1;
65  iov_id = 1;
66  iov_begin = 1;
67  iov_end = -1;
68  tag_id = 2;
69  tag_mode = "auto";
70  tag_name = "test_channel_quality_tag_name";
71  detector_name = "HCAL";
72  comment = hcal_ass.getRandomQuote();
73  tag_idref = 2;
74  iov_idref = 1;
75  data_set_idref = -1;
76  //
77  geomid_cq.clear();
78  detid_cq.clear();
79  //
80  //hAss.getListOfChannelsFromDb();
81  //
82  init_data();
83 }
84 
85 
87 {
88 }
89 
90 
91 DOMElement * HcalChannelQualityXml::add_data( DOMNode * _dataset, int _channel_status, int _on_off, std::string _comment ){
92  DOMElement * _data_elem = get_data_element(_dataset);
93  add_element(_data_elem, XMLProcessor::_toXMLCh("CHANNEL_STATUS_WORD"), XMLProcessor::_toXMLCh(_channel_status));
94  add_element(_data_elem, XMLProcessor::_toXMLCh("CHANNEL_ON_OFF_STATE"), XMLProcessor::_toXMLCh(_on_off));
95  add_element(_data_elem, XMLProcessor::_toXMLCh("COMMENT_DESCRIPTION"), XMLProcessor::_toXMLCh(_comment));
96  //
97  return _data_elem;
98 }
99 
100 
101 DOMNode * HcalChannelQualityXml::add_hcal_channel_dataset( int ieta, int iphi, int depth, std::string subdetector,
102  int _channel_status, int _on_off, std::string _comment ){
103  DOMNode * _dataset = add_dataset();
104  add_hcal_channel(_dataset, ieta, iphi, depth, subdetector);
105  add_data(_dataset, _channel_status, _on_off, _comment);
106  return _dataset;
107 }
108 
109 
110 int HcalChannelQualityXml::set_all_channels_on_off( int _hb, int _he, int _hf, int _ho){
113  std::string _subdetector = "";
114  int _onoff = -1;
115  std::string _comment = get_random_comment();
116  for (iter.begin(); !iter.end(); iter.next()){
117  HcalSubdetector _det = iter.getHcalSubdetector();
118  if (_det == HcalBarrel){
119  _subdetector = "HB";
120  _onoff = _hb;
121  }
122  else if (_det == HcalEndcap){
123  _subdetector = "HE";
124  _onoff = _he;
125  }
126  if (_det == HcalForward){
127  _subdetector = "HF";
128  _onoff = _hf;
129  }
130  if (_det == HcalOuter){
131  _subdetector = "HO";
132  _onoff = _ho;
133  }
134  add_hcal_channel_dataset( iter.getIeta(), iter.getIphi(), iter.getDepth(), _subdetector,
135  0, _onoff, _comment );
136 
137  }
138 
139  return 0;
140 }
141 
142 
144  uint32_t _he,
145  uint32_t _hf,
146  uint32_t _ho){
149  std::string _subdetector = "";
150  uint32_t _status = 0;
151  int _onoff = 0;
152  std::string _comment = get_random_comment();
153  for (iter.begin(); !iter.end(); iter.next()){
154  HcalSubdetector _det = iter.getHcalSubdetector();
155  if (_det == HcalBarrel){
156  _subdetector = "HB";
157  _status = _hb;
158  }
159  else if (_det == HcalEndcap){
160  _subdetector = "HE";
161  _status = _he;
162  }
163  else if (_det == HcalForward){
164  _subdetector = "HF";
165  _status = _hf;
166  }
167  else if (_det == HcalOuter){
168  _subdetector = "HO";
169  _status = _ho;
170  }
171  else continue;
172  add_hcal_channel_dataset( iter.getIeta(), iter.getIphi(), iter.getDepth(), _subdetector,
173  _status, _onoff, _comment );
174  }
175  return 0;
176 }
177 
178 
179 
181  return hcal_ass.getRandomQuote();
182 }
183 
184 
185 
186 //
187 //_____ reads on/off channel states from OMDS for a given tag and IOV
188 // returns number of channels retrieved
190  static ConnectionManager conn;
191  conn.connect();
192  std::string query = "select ";
193  //query += " sp.record_id as record_id ";
194  query += " sp.subdet as subdetector ";
195  query += " ,sp.ieta as IETA ";
196  query += " ,sp.iphi as IPHI ";
197  query += " ,sp.depth as DEPTH ";
198  query += " ,sp.channel_status_word as STATUS_WORD ";
199  query += " ,sp.channel_on_off_state as ON_OFF ";
200  query += " ,sp.commentdescription ";
201  //query += " ,sp.channel_map_id detid ";
202  //query += " ,sp.interval_of_validity_begin as IOV_BEGIN ";
203  //query += " ,sp.interval_of_validity_end as IOV_END ";
204  query += "from ";
205  query += " ( ";
206  query += " select MAX(cq.record_id) as record_id ";
207  query += " ,MAX(cq.interval_of_validity_begin) as iov_begin ";
208  query += " ,cq.channel_map_id ";
209  query += " from ";
210  query += " cms_hcl_hcal_cond.v_hcal_channel_quality cq ";
211  query += " where ";
212  query += " tag_name=:1 ";
213  query += " and";
214  query += " cq.interval_of_validity_begin<=:2";
215  query += " group by ";
216  query += " cq.channel_map_id ";
217  query += " order by ";
218  query += " cq.channel_map_id ";
219  query += " ) fp ";
220  query += "inner join ";
221  query += " cms_hcl_hcal_cond.v_hcal_channel_quality sp ";
222  query += "on ";
223  query += " fp.record_id=sp.record_id ";
224  int _n_channels = 0;
225  try {
226  oracle::occi::Statement* stmt = conn.getStatement(query);
227  stmt->setString(1,_tag);
228  stmt->setInt(2,_iov_begin);
229  oracle::occi::ResultSet *rs = stmt->executeQuery();
230  geomid_cq.clear();
231  //detid_cq.clear();
232  while (rs->next()) {
233  _n_channels++;
234  int _geomId = hAss.getGeomId(hAss.getSubdetector(rs->getString(1)),
235  rs->getInt(2),
236  rs->getInt(3),
237  rs->getInt(4)
238  );
240  _cq.status = rs->getInt(5);
241  _cq.onoff = rs->getInt(6);
242  _cq.comment = rs->getString(7);
243  geomid_cq.insert(std::pair<int, HcalChannelQualityXml::ChannelQuality>(_geomId, _cq));
244  }
245  }
246  catch (SQLException& e) {
247  std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
248  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
249  }
250  conn.disconnect();
251  return _n_channels;
252 }
253 
254 
255 // map key is the proprietary channel hash, see HcalAssistant
256 int HcalChannelQualityXml::addChannelQualityGeom(std::map<int,ChannelQuality> & _cq){
257  int _n_channels = 0;
258  for (std::map<int,ChannelQuality>::const_iterator _chan=_cq.begin();
259  _chan!=_cq.end();
260  _chan++
261  ){
262  add_hcal_channel_dataset( hAss.getHcalIeta(_chan->first),
263  hAss.getHcalIphi(_chan->first),
264  hAss.getHcalDepth(_chan->first),
265  hAss.getSubdetectorString(hAss.getHcalSubdetector(_chan->first)),
266  _chan->second.status,
267  _chan->second.onoff,
268  _chan->second.comment
269  );
270  _n_channels++;
271  }
272  return _n_channels;
273 }
274 
275 
276 int HcalChannelQualityXml::addChannelToGeomIdMap( int ieta, int iphi, int depth, std::string subdetector,
277  int _channel_status, int _on_off, std::string _comment ){
278  int _geomId = hAss.getGeomId(hAss.getSubdetector(subdetector),
279  ieta,
280  iphi,
281  depth);
283  _cq.status = _channel_status;
284  _cq.onoff = _on_off;
285  _cq.comment = _comment;
286  if (geomid_cq.find(_geomId)==geomid_cq.end()){
287  geomid_cq.insert(std::pair<int, HcalChannelQualityXml::ChannelQuality>(_geomId,_cq));
288  }
289  else{
290  geomid_cq[_geomId]=_cq;
291  }
292  return 0;
293 }
294 
295 
297  std::string _row;
298  int _lines = 0;
299  ChannelQuality _cq;
300  _cq.onoff = 0;
301  _cq.status = 0;
302  _cq.comment = "filled from an ASCII stream";
303  geomid_cq.clear();
304  while ( getline( std::cin, _row ) > 0 ){
305  //#(empty) eta phi dep det value DetId(optional)
306  int _eta, _phi, _dep, _value;
307  char _det[32];
308  int _read;
309  if ( base.find("hex")!=std::string::npos ){
310  const char * _format = "%d %d %d %s %X";
311  _read = sscanf( _row . c_str(), _format,
312  &_eta, &_phi, &_dep, _det, &_value
313  );
314  }
315  else if ( base.find("dec")!=std::string::npos ){
316  const char * _format = "%d %d %d %s %d";
317  _read = sscanf( _row . c_str(), _format,
318  &_eta, &_phi, &_dep, _det, &_value
319  );
320  }
321  else{
322  std::cerr << "Undefined or invalid base. Specify hex or dec. Exiting..." << std::endl;
323  exit(-1);
324  }
325  if ( _read == 5 ){
326  _lines++;
327 
328  int _geomId = hAss.getGeomId(hAss.getSubdetector(std::string(_det))
329  , _eta, _phi, _dep);
330  _cq.status = _value;
331  _cq.onoff = (_value & 65536)>>15;
332  geomid_cq.insert(std::pair<int, HcalChannelQualityXml::ChannelQuality>(_geomId, _cq));
333  //std::cerr << "Line: " << _geomId << " " << _cq.status << std::endl;
334  }
335  }
336  return _lines;
337 }
338 
339 
341  int _lines = 0;
342  char _buf[128];
343  int _detId = 0; // dummy as it is optional in the ASCII file
344  sprintf(_buf, "# eta phi dep det value DetId");
345  std::cout << _buf << std::endl;
346  for (std::map<int,ChannelQuality>::const_iterator _cq = geomid_cq.begin();
347  _cq != geomid_cq.end();
348  _cq++){
349  _lines++;
350  _detId = hAss.getRawIdFromCmssw(_cq->first);
351  if ( base.find("hex")!=std::string::npos ){
352  sprintf(_buf," %16d%16d%16d%16s%16.8X%11.8X",
353  hAss.getHcalIeta(_cq->first),
354  hAss.getHcalIphi(_cq->first),
355  hAss.getHcalDepth(_cq->first),
356  hAss.getSubdetectorString(hAss.getHcalSubdetector(_cq->first)).c_str(),
357  _cq->second.status,
358  _detId
359  );
360  }
361  else if ( base.find("dec")!=std::string::npos ){
362  sprintf(_buf," %16d%16d%16d%16s%16d%11.8d",
363  hAss.getHcalIeta(_cq->first),
364  hAss.getHcalIphi(_cq->first),
365  hAss.getHcalDepth(_cq->first),
366  hAss.getSubdetectorString(hAss.getHcalSubdetector(_cq->first)).c_str(),
367  _cq->second.status,
368  _detId
369  );
370  }
371  else{
372  std::cerr << "Undefined or invalid base. Specify hex or dec. Exiting..." << std::endl;
373  exit(-1);
374  }
375  std::cout << _buf << std::endl;
376  }
377  return _lines;
378 }
379 
380 
382  int _iov_begin,
383  int _iov_end,
384  std::string _tag,
385  std::string _elements_comment,
386  std::string _base
387  )
388 {
389  readStatusWordFromStdin(_base);
390  set_header_run_number(_runnumber);
391  set_elements_iov_begin(_iov_begin);
392  set_elements_iov_end(_iov_end);
393  set_elements_tag_name(_tag);
394  set_elements_comment(_elements_comment);
395  addChannelQualityGeom(geomid_cq);
396  write();
397  return 0;
398 }
399 
400 
402  getBaseLineFromOmds(_tag, _iov_begin);
403  writeStatusWordToStdout(base);
404  return 0;
405 }
406 
407 
408 
409 //
410 //_____ dumps a sorted list of tags to stdout, newest first
411 //
413  std::vector<std::string> _tags = getTagsFromOmds();
414  for (std::vector<std::string>::const_iterator tag=_tags.begin(); tag!=_tags.end(); tag++){
415  std::cout << *tag << std::endl;
416  }
417  return _tags.size();
418 }
419 
420 //
421 //_____ reads a sorted list of tags, newest first
422 //
423 std::vector<std::string> HcalChannelQualityXml::getTagsFromOmds(){
424  std::vector<std::string> _tags;
425  static ConnectionManager conn;
426  conn.connect();
427  std::string query = "select distinct tag_name, min(record_id) as mrid ";
428  query += "from ";
429  query += "cms_hcl_hcal_cond.v_hcal_channel_quality cq ";
430  query += "group by tag_name ";
431  query += "order by mrid desc ";
432  int _n_tags = 0;
433  try {
434  oracle::occi::Statement* stmt = conn.getStatement(query);
435  oracle::occi::ResultSet *rs = stmt->executeQuery();
436  while (rs->next()) {
437  _n_tags++;
438  _tags.push_back( rs->getString(1) );
439  }
440  }
441  catch (SQLException& e) {
442  std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
443  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
444  }
445  conn.disconnect();
446  return _tags;
447 }
448 
449 
450 
451 //
452 //_____ reads a sorted list of Iovs for a given tag, newest on top
453 //
455  std::vector<int> _iovs = getIovsFromOmds(tag);
456  for (std::vector<int>::const_iterator tag=_iovs.begin(); tag!=_iovs.end(); tag++){
457  std::cout << *tag << std::endl;
458  }
459  return _iovs.size();
460 }
461 
462 //
463 //_____ reads a sorted list of iovs, newest first
464 //
466  std::vector<int> _iovs;
467  static ConnectionManager conn;
468  conn.connect();
469  std::string query = "select distinct cq.interval_of_validity_begin, min(record_id) as mrid ";
470  query += "from ";
471  query += "cms_hcl_hcal_cond.v_hcal_channel_quality cq ";
472  query += "where ";
473  query += "tag_name=:1 ";
474  query += "group by cq.interval_of_validity_begin ";
475  query += "order by mrid desc ";
476  int _n_iovs = 0;
477  try {
478  oracle::occi::Statement* stmt = conn.getStatement(query);
479  stmt->setString(1,tag);
480  oracle::occi::ResultSet *rs = stmt->executeQuery();
481  while (rs->next()) {
482  _n_iovs++;
483  _iovs.push_back( rs->getInt(1) );
484  }
485  }
486  catch (SQLException& e) {
487  std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
488  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
489  }
490  conn.disconnect();
491  return _iovs;
492 }
tuple base
Main Program
Definition: newFWLiteAna.py:92
int makeXmlFromAsciiStream(int _runnumber, int _iov_begin, int _iov_end, std::string _tag, std::string _elements_comment, std::string _base="hex")
std::string get_random_comment(void)
int set_all_channels_status(uint32_t _hb, uint32_t _he, uint32_t _hf, uint32_t _ho)
oracle::occi::Statement * getStatement(const std::string &query)
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
std::vector< std::string > getTagsFromOmds(void)
int writeBaseLineFromOmdsToStdout(std::string _tag, int _iov_begin, std::string base="hex")
DOMNode * add_hcal_channel_dataset(int ieta, int iphi, int depth, std::string subdetector, int _channel_status, int _on_off, std::string _comment)
int dumpIovsFromOmdsToStdout(std::string tag)
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
HcalSubdetector
Definition: HcalAssistant.h:31
std::vector< int > getIovsFromOmds(std::string tag)
int addChannelToGeomIdMap(int ieta, int iphi, int depth, std::string subdetector, int _channel_status, int _on_off, std::string _comment)
DOMElement * add_data(DOMNode *_dataset, int _channel_status, int _on_off, std::string _comment)
int readStatusWordFromStdin(std::string base="hex")
int set_all_channels_on_off(int _hb, int _he, int _hf, int _ho)
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
static XMLCh * _toXMLCh(std::string temp)
Definition: XMLProcessor.h:184
int getBaseLineFromOmds(std::string _tag, int _iov_begin)
tuple query
Definition: o2o.py:269
tuple cout
Definition: gather_cfg.py:121
tuple status
Definition: ntuplemaker.py:245
int writeStatusWordToStdout(std::string base="hex")
tuple conn
Definition: results_mgr.py:53
HcalSubdetector getHcalSubdetector(void)
int addChannelQualityGeom(std::map< int, ChannelQuality > &_cq)
#define comment(par)
Definition: vmac.h:161