CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CaloOnlineTools/HcalOnlineDb/src/HcalChannelQualityXml.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     CaloOnlineTools/HcalOnlineDb
00004 // Class  :     HcalChannelQualityXml
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Gena Kukartsev, kukarzev@fnal.gov
00010 //         Created:  Wed Jul 01 06:30:00 CDT 2009
00011 // $Id: HcalChannelQualityXml.cc,v 1.12 2010/08/06 20:24:12 wmtan Exp $
00012 //
00013 
00014 #include <iostream>
00015 #include <string>
00016 #include <vector>
00017 
00018 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalChannelQualityXml.h"
00019 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalChannelIterator.h"
00020 #include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
00021 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
00022 #include "xgi/Utils.h"
00023 #include "toolbox/string.h"
00024 #include "OnlineDB/Oracle/interface/Oracle.h"
00025 
00026 using namespace std;
00027 using namespace oracle::occi;
00028 
00029 
00030 HcalChannelQualityXml::ChannelQuality::_ChannelQuality(){
00031   status = -1;
00032   onoff = -1;
00033   comment = "Comment has not been entered";
00034 }
00035 
00036 
00037 HcalChannelQualityXml::HcalChannelQualityXml()
00038 /*
00039   :extension_table_name(""),
00040   type_name(""),
00041   run_number(-1),
00042   channel_map("HCAL_CHANNELS"),
00043   data_set_id(-1),
00044   iov_id(1),
00045   iov_begin(1),
00046   iov_end(-1),
00047   tag_id(2),
00048   tag_mode("auto"),
00049   tag_name("test_tag_name"),
00050   detector_name("HCAL"),
00051   comment(""),
00052   tag_idref(2),
00053   iov_idref(1),
00054   data_set_idref(-1)
00055 */
00056 {
00057   extension_table_name="HCAL_CHANNEL_QUALITY_V1";
00058   type_name="HCAL Channel Quality [V1]";
00059   run_number = -1;
00060   channel_map = "HCAL_CHANNELS";
00061   data_set_id = -1;
00062   iov_id = 1;
00063   iov_begin = 1;
00064   iov_end = -1;
00065   tag_id = 2;
00066   tag_mode = "auto";
00067   tag_name = "test_channel_quality_tag_name";
00068   detector_name = "HCAL";
00069   comment = hcal_ass.getRandomQuote();
00070   tag_idref = 2;
00071   iov_idref = 1;
00072   data_set_idref = -1;
00073   //
00074   geomid_cq.clear();
00075   detid_cq.clear();
00076   //
00077   //hAss.getListOfChannelsFromDb();
00078   //
00079   init_data();
00080 }
00081 
00082 
00083 HcalChannelQualityXml::~HcalChannelQualityXml()
00084 {
00085 }
00086 
00087 
00088 DOMElement * HcalChannelQualityXml::add_data( DOMNode * _dataset, int _channel_status, int _on_off, std::string _comment ){
00089   DOMElement * _data_elem = get_data_element(_dataset);
00090   add_element(_data_elem, XMLProcessor::_toXMLCh("CHANNEL_STATUS_WORD"), XMLProcessor::_toXMLCh(_channel_status));
00091   add_element(_data_elem, XMLProcessor::_toXMLCh("CHANNEL_ON_OFF_STATE"), XMLProcessor::_toXMLCh(_on_off));
00092   add_element(_data_elem, XMLProcessor::_toXMLCh("COMMENT_DESCRIPTION"), XMLProcessor::_toXMLCh(_comment));
00093   //
00094   return _data_elem;
00095 }
00096 
00097 
00098 DOMNode * HcalChannelQualityXml::add_hcal_channel_dataset( int ieta, int iphi, int depth, std::string subdetector,
00099                                                            int _channel_status, int _on_off, std::string _comment ){
00100   DOMNode * _dataset = add_dataset();
00101   add_hcal_channel(_dataset, ieta, iphi, depth, subdetector);
00102   add_data(_dataset, _channel_status, _on_off, _comment);
00103   return _dataset;
00104 }
00105 
00106 
00107 int HcalChannelQualityXml::set_all_channels_on_off( int _hb, int _he, int _hf, int _ho){
00108   HcalChannelIterator iter;
00109   iter.initHBEFListFromLmapAscii();
00110   std::string _subdetector = "";
00111   int _onoff = -1;
00112   std::string _comment = get_random_comment();
00113   for (iter.begin(); !iter.end(); iter.next()){
00114     HcalSubdetector _det = iter.getHcalSubdetector();
00115     if (_det == HcalBarrel){
00116       _subdetector = "HB";
00117       _onoff = _hb;
00118     }
00119     else if (_det == HcalEndcap){
00120       _subdetector = "HE";
00121       _onoff = _he;
00122     }
00123     if (_det == HcalForward){
00124       _subdetector = "HF";
00125       _onoff = _hf;
00126     }
00127     if (_det == HcalOuter){
00128       _subdetector = "HO";
00129       _onoff = _ho;
00130     }
00131     add_hcal_channel_dataset( iter.getIeta(), iter.getIphi(), iter.getDepth(), _subdetector,
00132                               0, _onoff, _comment );
00133     
00134   }
00135 
00136   return 0;
00137 }
00138 
00139 
00140 int HcalChannelQualityXml::set_all_channels_status( uint32_t _hb,
00141                                                     uint32_t _he,
00142                                                     uint32_t _hf,
00143                                                     uint32_t _ho){
00144   HcalChannelIterator iter;
00145   iter.initHBEFListFromLmapAscii();
00146   std::string _subdetector = "";
00147   uint32_t _status = 0;
00148   int _onoff = 0;
00149   std::string _comment = get_random_comment();
00150   for (iter.begin(); !iter.end(); iter.next()){
00151     HcalSubdetector _det = iter.getHcalSubdetector();
00152     if (_det == HcalBarrel){
00153       _subdetector = "HB";
00154       _status = _hb;
00155     }
00156     else if (_det == HcalEndcap){
00157       _subdetector = "HE";
00158       _status = _he;
00159     }
00160     else if (_det == HcalForward){
00161       _subdetector = "HF";
00162       _status = _hf;
00163     }
00164     else if (_det == HcalOuter){
00165       _subdetector = "HO";
00166       _status = _ho;
00167     }
00168     else continue;
00169     add_hcal_channel_dataset( iter.getIeta(), iter.getIphi(), iter.getDepth(), _subdetector,
00170                               _status, _onoff, _comment );
00171   }
00172   return 0;
00173 }
00174 
00175 
00176 
00177 std::string HcalChannelQualityXml::get_random_comment( void ){
00178   return hcal_ass.getRandomQuote();
00179 }
00180 
00181 
00182 
00183 //
00184 //_____ reads on/off channel states from OMDS for a given tag and IOV
00185 //      returns number of channels retrieved
00186 int HcalChannelQualityXml::getBaseLineFromOmds(std::string _tag, int _iov_begin){
00187   static ConnectionManager conn;
00188   conn.connect();
00189   std::string query = "select ";
00190   //query            += "       sp.record_id as record_id ";
00191   query            += "       sp.subdet as subdetector ";
00192   query            += "       ,sp.ieta as IETA ";
00193   query            += "       ,sp.iphi as IPHI ";
00194   query            += "       ,sp.depth as DEPTH ";
00195   query            += "       ,sp.channel_status_word as STATUS_WORD ";
00196   query            += "       ,sp.channel_on_off_state as ON_OFF ";
00197   query            += "       ,sp.commentdescription ";
00198   //query            += "       ,sp.channel_map_id detid ";
00199   //query            += "       ,sp.interval_of_validity_begin as IOV_BEGIN ";
00200   //query            += "       ,sp.interval_of_validity_end as IOV_END ";
00201   query            += "from ";
00202   query            += "       ( ";
00203   query            += "       select MAX(cq.record_id) as record_id ";
00204   query            += "             ,MAX(cq.interval_of_validity_begin) as iov_begin ";
00205   query            += "             ,cq.channel_map_id ";
00206   query            += "       from ";
00207   query            += "             cms_hcl_hcal_cond.v_hcal_channel_quality cq ";
00208   query            += "       where ";
00209   query            += "             tag_name=:1 ";
00210   query            += "       and";
00211   query            += "       cq.interval_of_validity_begin<=:2";
00212   query            += "       group by ";
00213   query            += "             cq.channel_map_id ";
00214   query            += "       order by ";
00215   query            += "             cq.channel_map_id ";
00216   query            += "       ) fp ";
00217   query            += "inner join ";
00218   query            += "       cms_hcl_hcal_cond.v_hcal_channel_quality sp ";
00219   query            += "on ";
00220   query            += "       fp.record_id=sp.record_id ";
00221   int _n_channels = 0;
00222   try {
00223     oracle::occi::Statement* stmt = conn.getStatement(query);
00224     stmt->setString(1,_tag);
00225     stmt->setInt(2,_iov_begin);
00226     oracle::occi::ResultSet *rs = stmt->executeQuery();
00227     geomid_cq.clear();
00228     //detid_cq.clear();
00229     while (rs->next()) {
00230       _n_channels++;
00231       int _geomId   = hAss.getGeomId(hAss.getSubdetector(rs->getString(1)),
00232                                 rs->getInt(2),
00233                                 rs->getInt(3),
00234                                 rs->getInt(4)
00235                                 );
00236       HcalChannelQualityXml::ChannelQuality _cq;
00237       _cq.status  = rs->getInt(5);
00238       _cq.onoff   = rs->getInt(6);
00239       _cq.comment = rs->getString(7);
00240       geomid_cq.insert(std::pair<int, HcalChannelQualityXml::ChannelQuality>(_geomId, _cq));
00241     }
00242   }
00243   catch (SQLException& e) {
00244     std::cerr << ::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()) << std::endl;
00245     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
00246   }
00247   conn.disconnect();
00248   return _n_channels;
00249 }
00250 
00251 
00252 // map key is the proprietary channel hash, see HcalAssistant
00253 int HcalChannelQualityXml::addChannelQualityGeom(std::map<int,ChannelQuality> & _cq){
00254   int _n_channels = 0;
00255   for (std::map<int,ChannelQuality>::const_iterator _chan=_cq.begin();
00256        _chan!=_cq.end();
00257        _chan++
00258        ){
00259     add_hcal_channel_dataset( hAss.getHcalIeta(_chan->first),
00260                               hAss.getHcalIphi(_chan->first),
00261                               hAss.getHcalDepth(_chan->first),
00262                               hAss.getSubdetectorString(hAss.getHcalSubdetector(_chan->first)),
00263                               _chan->second.status,
00264                               _chan->second.onoff,
00265                               _chan->second.comment
00266                               );
00267     _n_channels++;
00268   }
00269   return _n_channels;
00270 }
00271 
00272 
00273 int HcalChannelQualityXml::addChannelToGeomIdMap( int ieta, int iphi, int depth, std::string subdetector,
00274                            int _channel_status, int _on_off, std::string _comment ){
00275   int _geomId = hAss.getGeomId(hAss.getSubdetector(subdetector),
00276                                ieta,
00277                                iphi,
00278                                depth);
00279   HcalChannelQualityXml::ChannelQuality _cq;
00280   _cq.status  = _channel_status;
00281   _cq.onoff   = _on_off;
00282   _cq.comment = _comment;
00283   if (geomid_cq.find(_geomId)==geomid_cq.end()){
00284     geomid_cq.insert(std::pair<int, HcalChannelQualityXml::ChannelQuality>(_geomId,_cq));
00285   }
00286   else{
00287     geomid_cq[_geomId]=_cq;
00288   }
00289   return 0;
00290 }
00291 
00292 
00293 int HcalChannelQualityXml::readStatusWordFromStdin(std::string base){
00294   std::string _row;
00295   int _lines = 0;
00296   ChannelQuality _cq;
00297   _cq.onoff   = 0;
00298   _cq.status  = 0;
00299   _cq.comment = "filled from an ASCII stream";
00300   geomid_cq.clear();
00301   while ( getline( std::cin, _row ) > 0 ){
00302     //#(empty) eta phi dep det value DetId(optional)
00303     int _eta, _phi, _dep, _value;
00304     char _det[32];
00305     int _read;
00306     if ( base.find("hex")!=std::string::npos ){
00307       const char * _format = "%d %d %d %s %X";
00308       _read = sscanf( _row . c_str(), _format,
00309                       &_eta, &_phi, &_dep, _det, &_value
00310                       );
00311     }
00312     else if ( base.find("dec")!=std::string::npos ){
00313       const char * _format = "%d %d %d %s %d";
00314       _read = sscanf( _row . c_str(), _format,
00315                       &_eta, &_phi, &_dep, _det, &_value
00316                       );
00317     }
00318     else{
00319       std::cerr << "Undefined or invalid base. Specify hex or dec. Exiting..." << std::endl;
00320       exit(-1);
00321     }
00322     if ( _read == 5 ){
00323       _lines++;
00324       
00325       int _geomId = hAss.getGeomId(hAss.getSubdetector(std::string(_det))
00326                               , _eta, _phi, _dep);
00327       _cq.status = _value;
00328       _cq.onoff  = (_value & 65536)>>15;
00329       geomid_cq.insert(std::pair<int, HcalChannelQualityXml::ChannelQuality>(_geomId, _cq));      
00330       //std::cerr << "Line: " << _geomId << "   " << _cq.status << std::endl;
00331     } 
00332   }
00333   return _lines;
00334 }
00335 
00336 
00337 int HcalChannelQualityXml::writeStatusWordToStdout(std::string base){
00338   int _lines = 0;
00339   char _buf[128];
00340   int _detId = 0; // dummy as it is optional in the ASCII file
00341   sprintf(_buf, "#             eta             phi             dep             det    value      DetId");
00342   std::cout << _buf << std::endl;
00343   for (std::map<int,ChannelQuality>::const_iterator _cq = geomid_cq.begin();
00344        _cq != geomid_cq.end();
00345        _cq++){
00346     _lines++;
00347     _detId = hAss.getRawIdFromCmssw(_cq->first);
00348     if ( base.find("hex")!=std::string::npos ){
00349       sprintf(_buf," %16d%16d%16d%16s%16.8X%11.8X",
00350               hAss.getHcalIeta(_cq->first),
00351               hAss.getHcalIphi(_cq->first),
00352               hAss.getHcalDepth(_cq->first),
00353               hAss.getSubdetectorString(hAss.getHcalSubdetector(_cq->first)).c_str(),
00354               _cq->second.status,
00355               _detId
00356               );
00357     }
00358     else if ( base.find("dec")!=std::string::npos ){
00359       sprintf(_buf," %16d%16d%16d%16s%16d%11.8d",
00360               hAss.getHcalIeta(_cq->first),
00361               hAss.getHcalIphi(_cq->first),
00362               hAss.getHcalDepth(_cq->first),
00363               hAss.getSubdetectorString(hAss.getHcalSubdetector(_cq->first)).c_str(),
00364               _cq->second.status,
00365               _detId
00366               );
00367     }
00368     else{
00369       std::cerr << "Undefined or invalid base. Specify hex or dec. Exiting..." << std::endl;
00370       exit(-1);
00371     }
00372     std::cout << _buf << std::endl;
00373   }
00374   return _lines;
00375 }
00376 
00377 
00378 int HcalChannelQualityXml::makeXmlFromAsciiStream(int _runnumber,
00379                                                   int _iov_begin,
00380                                                   int _iov_end,
00381                                                   std::string _tag,
00382                                                   std::string _elements_comment,
00383                                                   std::string _base
00384                                                   )
00385 {
00386   readStatusWordFromStdin(_base);
00387   set_header_run_number(_runnumber);
00388   set_elements_iov_begin(_iov_begin);
00389   set_elements_iov_end(_iov_end);
00390   set_elements_tag_name(_tag);
00391   set_elements_comment(_elements_comment);
00392   addChannelQualityGeom(geomid_cq);
00393   write();
00394   return 0;
00395 }
00396 
00397 
00398 int HcalChannelQualityXml::writeBaseLineFromOmdsToStdout(std::string _tag, int _iov_begin, std::string base){
00399   getBaseLineFromOmds(_tag, _iov_begin);
00400   writeStatusWordToStdout(base);
00401   return 0;
00402 }
00403 
00404 
00405 
00406 //
00407 //_____ dumps a sorted list of tags to stdout, newest first
00408 //
00409 int HcalChannelQualityXml::dumpTagsFromOmdsToStdout(){
00410   std::vector<std::string> _tags = getTagsFromOmds();
00411   for (std::vector<std::string>::const_iterator tag=_tags.begin(); tag!=_tags.end(); tag++){
00412     std::cout << *tag << std::endl;
00413   }
00414   return _tags.size();
00415 }
00416 
00417 //
00418 //_____ reads a sorted list of tags, newest first
00419 //
00420 std::vector<std::string> HcalChannelQualityXml::getTagsFromOmds(){
00421   std::vector<std::string> _tags;
00422   static ConnectionManager conn;
00423   conn.connect();
00424   std::string query = "select distinct tag_name, min(record_id) as mrid ";
00425   query            += "from ";
00426   query            += "cms_hcl_hcal_cond.v_hcal_channel_quality cq ";
00427   query            += "group by tag_name ";
00428   query            += "order by mrid desc ";
00429   int _n_tags = 0;
00430   try {
00431     oracle::occi::Statement* stmt = conn.getStatement(query);
00432     oracle::occi::ResultSet *rs = stmt->executeQuery();
00433     while (rs->next()) {
00434       _n_tags++;
00435       _tags.push_back( rs->getString(1) );
00436     }
00437   }
00438   catch (SQLException& e) {
00439     std::cerr << ::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()) << std::endl;
00440     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
00441   }
00442   conn.disconnect();
00443   return _tags;
00444 }
00445 
00446 
00447 
00448 //
00449 //_____ reads a sorted list of Iovs for a given tag, newest on top
00450 //
00451 int HcalChannelQualityXml::dumpIovsFromOmdsToStdout(std::string tag){
00452   std::vector<int> _iovs = getIovsFromOmds(tag);
00453   for (std::vector<int>::const_iterator tag=_iovs.begin(); tag!=_iovs.end(); tag++){
00454     std::cout << *tag << std::endl;
00455   }
00456   return _iovs.size();
00457 }
00458 
00459 //
00460 //_____ reads a sorted list of iovs, newest first
00461 //
00462 std::vector<int> HcalChannelQualityXml::getIovsFromOmds(std::string tag){
00463   std::vector<int> _iovs;
00464   static ConnectionManager conn;
00465   conn.connect();
00466   std::string query = "select distinct cq.interval_of_validity_begin, min(record_id) as mrid ";
00467   query            += "from ";
00468   query            += "cms_hcl_hcal_cond.v_hcal_channel_quality cq ";
00469   query            += "where ";
00470   query            += "tag_name=:1 ";
00471   query            += "group by cq.interval_of_validity_begin ";
00472   query            += "order by mrid desc ";
00473   int _n_iovs = 0;
00474   try {
00475     oracle::occi::Statement* stmt = conn.getStatement(query);
00476     stmt->setString(1,tag);
00477     oracle::occi::ResultSet *rs = stmt->executeQuery();
00478     while (rs->next()) {
00479       _n_iovs++;
00480       _iovs.push_back( rs->getInt(1) );
00481     }
00482   }
00483   catch (SQLException& e) {
00484     std::cerr << ::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()) << std::endl;
00485     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
00486   }
00487   conn.disconnect();
00488   return _iovs;
00489 }