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