Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:15:09

0001 //
0002 // Gena Kukartsev (Brown), Feb 23, 2008
0003 // $Id:
0004 
0005 #include <fstream>
0006 
0007 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalQIEManager.h"
0008 #include "CaloOnlineTools/HcalOnlineDb/interface/RooGKCounter.h"
0009 #include "OnlineDB/Oracle/interface/Oracle.h"
0010 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
0011 
0012 #ifdef HAVE_XDAQ
0013 #include "toolbox/string.h"
0014 #else
0015 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h"  // Replaces toolbox::toString
0016 #endif
0017 
0018 using namespace std;
0019 using namespace oracle::occi;
0020 using namespace hcal;
0021 
0022 HcalQIEManager::HcalQIEManager(void) {}
0023 
0024 HcalQIEManager::~HcalQIEManager(void) {}
0025 
0026 bool HcalChannelId::operator<(const HcalChannelId& other) const {
0027   long long int _res_this, _res_other;
0028   int _sub_this, _sub_other;
0029 
0030   if (this->subdetector == "HE")
0031     _sub_this = 1;
0032   else if (this->subdetector == "HF")
0033     _sub_this = 2;
0034   else if (this->subdetector == "HO")
0035     _sub_this = 3;
0036   else
0037     _sub_this = 4;
0038 
0039   if (other.subdetector == "HE")
0040     _sub_other = 1;
0041   else if (other.subdetector == "HF")
0042     _sub_other = 2;
0043   else if (other.subdetector == "HO")
0044     _sub_other = 3;
0045   else
0046     _sub_other = 4;
0047 
0048   _res_this = 100 + eta + (phi + 100) * 1000 + (depth + 10) * 1000000 + _sub_this * 1000000000;
0049   _res_other = 100 + other.eta + (other.phi + 100) * 1000 + (other.depth + 10) * 1000000 + _sub_other * 1000000000;
0050 
0051   return _res_this < _res_other;
0052 }
0053 
0054 std::map<HcalChannelId, HcalQIECaps>& HcalQIEManager::getQIETableFromFile(std::string _filename) {
0055   std::map<HcalChannelId, HcalQIECaps>* result_sup = new std::map<HcalChannelId, HcalQIECaps>;
0056   std::map<HcalChannelId, HcalQIECaps>& result = (*result_sup);
0057 
0058   ifstream infile(_filename.c_str());
0059   std::string buf;
0060 
0061   if (infile.is_open()) {
0062     std::cout << "File is open" << std::endl;
0063     while (getline(infile, buf)) {
0064       std::vector<std::string> _line = splitString(buf);
0065 
0066       HcalChannelId _id;
0067       sscanf(_line[0].c_str(), "%d", &_id.eta);
0068       sscanf(_line[1].c_str(), "%d", &_id.phi);
0069       sscanf(_line[2].c_str(), "%d", &_id.depth);
0070       _id.subdetector = _line[3];
0071 
0072       HcalQIECaps _adc;
0073       int _columns = _line.size();
0074       for (int i = 4; i != _columns; i++) {
0075         sscanf(_line[i].c_str(), "%lf", &_adc.caps[i - 4]);
0076       }
0077 
0078       /* DEBUG: double entries
0079       if(result.find(_id) != result.end()){
0080     cout << "TABLE DEBUG: " << _filename << "   " << _id.eta << "   " << _id.phi << "   " << _id.depth << " " << _id.subdetector << std::endl;
0081       }
0082       */
0083 
0084       //result[_id]=_adc;
0085       result.insert(std::pair<HcalChannelId, HcalQIECaps>(_id, _adc));
0086 
0087       //std::cout << result.size() << std::endl;
0088 
0089       //std::cout << _id.eta << "   " << _id . subdetector << " " << _adc.caps[7] << std::endl;
0090     }
0091   }
0092   return result;
0093 }
0094 
0095 // courtesy of Fedor Ratnikov
0096 std::vector<std::string> HcalQIEManager::splitString(const std::string& fLine) {
0097   std::vector<std::string> result;
0098   int start = 0;
0099   bool empty = true;
0100   for (unsigned i = 0; i <= fLine.size(); i++) {
0101     if (fLine[i] == ' ' || fLine[i] == '\n' || fLine[i] == '    ' || i == fLine.size()) {
0102       if (!empty) {
0103         std::string item(fLine, start, i - start);
0104         result.push_back(item);
0105         empty = true;
0106       }
0107       start = i + 1;
0108     } else {
0109       if (empty)
0110         empty = false;
0111     }
0112   }
0113   return result;
0114 }
0115 
0116 void HcalQIEManager::getTableFromDb(std::string query_file, std::string output_file) {
0117   std::cout << "Creating the output file: " << output_file << "... ";
0118   ofstream out_file;
0119   out_file.open(output_file.c_str());
0120   std::cout << " done" << std::endl;
0121 
0122   HCALConfigDB* db = new HCALConfigDB();
0123   const std::string _accessor =
0124       "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
0125   db->connect(_accessor);
0126 
0127   oracle::occi::Connection* _connection = db->getConnection();
0128 
0129   std::cout << "Preparing to request the QIE table from the database..." << std::endl;
0130 
0131   //loop over RM slots and QIEs (to save time, otherwise the query runs forever)
0132   for (int _rm = 1; _rm != 5; _rm++) {
0133     for (int _qie = 1; _qie != 4; _qie++) {
0134       try {
0135         cout << "Preparing the query..." << std::endl;
0136         Statement* stmt = _connection->createStatement();
0137         std::string query, buf;
0138         ifstream inFile(query_file.c_str(), std::ios::in);
0139         if (!inFile) {
0140           std::cout << " Unable to open file with query!" << std::endl;
0141         } else {
0142           std::cout << "Query file opened successfully: " << query_file << std::endl;
0143         }
0144         while (getline(inFile, buf)) {
0145           query.append(buf);
0146           query.append("\n");
0147         }
0148 
0149         char query_fixed[50000];
0150         sprintf(query_fixed, query.c_str(), _rm, _rm, _qie, _qie);
0151         inFile.close();
0152         cout << "Preparing the query... done" << std::endl;
0153 
0154         //SELECT
0155         cout << "Executing the query..." << std::endl;
0156         //std::cout << query_fixed << std::endl;
0157         ResultSet* rs = stmt->executeQuery(query_fixed);
0158         cout << "Executing the query... done" << std::endl;
0159 
0160         cout << "Processing the query results..." << std::endl;
0161         RooGKCounter _lines(1, 100);
0162         //int count;
0163         while (rs->next()) {
0164           _lines.count();
0165           HcalChannelId _id;
0166           HcalQIECaps _caps;
0167           //count = rs->getInt(1);
0168           _id.eta = rs->getInt(1);
0169           _id.phi = rs->getInt(2);
0170           _id.depth = rs->getInt(3);
0171           _id.subdetector = rs->getString(4);
0172           for (int j = 0; j != 32; j++) {
0173             _caps.caps[j] = rs->getDouble(j + 5);
0174           }
0175 
0176           //==> output QIE table line
0177           char buffer[1024];
0178           sprintf(buffer, "%15d %15d %15d %15s", _id.eta, _id.phi, _id.depth, _id.subdetector.c_str());
0179           //std::cout << buffer;
0180           out_file << buffer;
0181           for (int j = 0; j != 32; j++) {
0182             double _x = _caps.caps[j];
0183             sprintf(buffer, " %8.5f", _x);
0184             //std::cout << buffer;
0185             out_file << buffer;
0186           }
0187           //std::cout << std::endl;
0188           out_file << std::endl;
0189           //===
0190         }
0191         //Always terminate statement
0192         _connection->terminateStatement(stmt);
0193 
0194         //std::cout << "Query count: " << count << std::endl;
0195         cout << "Query line count: " << _lines.getCount() << std::endl;
0196       } catch (SQLException& e) {
0197         XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0198                     ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
0199       }
0200     }
0201   }
0202 
0203   db->disconnect();
0204   out_file.close();
0205 }
0206 
0207 // This function generates proper QIE table ASCII file based on the result
0208 // of the database query from db_file.
0209 // Missing channels are filled from the old table old_file.
0210 // The result is placed in output_file.
0211 int HcalQIEManager::generateQieTable(std::string db_file, std::string old_file, std::string output_file) {
0212   std::cout << "Creating the output file: " << output_file << "... ";
0213   ofstream out_file;
0214   out_file.open(output_file.c_str());
0215   std::cout << " done" << std::endl;
0216 
0217   std::string badchan_file = output_file + ".badchannels";
0218   std::cout << "Creating the output file for bad channels: " << badchan_file << "... ";
0219   ofstream bad_file;
0220   bad_file.open(badchan_file.c_str());
0221   std::cout << " done" << std::endl;
0222 
0223   std::map<HcalChannelId, HcalQIECaps>& _old = getQIETableFromFile(old_file);
0224   std::map<HcalChannelId, HcalQIECaps>& _new = getQIETableFromFile(db_file);
0225   //std::map<HcalChannelId,HcalQIECaps> & _old = _manager . getQIETableFromFile( "qie_normalmode_v3.txt" );
0226   //std::map<HcalChannelId,HcalQIECaps> & _new = _manager . getQIETableFromFile( "qie_adc_table_after.txt" );
0227 
0228   int goodChannels = 0;
0229   int badChannels = 0;
0230   std::cout << "old size: " << _old.size() << std::endl;
0231   std::cout << "new size: " << _new.size() << std::endl;
0232   for (std::map<HcalChannelId, HcalQIECaps>::const_iterator line = _old.begin(); line != _old.end(); line++) {
0233     HcalQIECaps* the_caps;
0234     HcalChannelId theId = line->first;
0235     bool badchannel = false;
0236     if (_new.find(theId) == _new.end()) {
0237       badchannel = true;
0238       badChannels++;
0239       the_caps = &_old[theId];
0240     } else {
0241       goodChannels++;
0242       the_caps = &_new[theId];
0243     }
0244     char buffer[1024];
0245     int eta = theId.eta;
0246     int phi = theId.phi;
0247     int depth = theId.depth;
0248     sprintf(buffer, "%15d %15d %15d %15s", eta, phi, depth, theId.subdetector.c_str());
0249     out_file << buffer;
0250     if (badchannel)
0251       bad_file << buffer;
0252 
0253     for (int j = 0; j != 32; j++) {
0254       double _x = the_caps->caps[j];
0255       sprintf(buffer, " %8.5f", _x);
0256       out_file << buffer;
0257       if (badchannel)
0258         bad_file << buffer;
0259     }
0260     out_file << std::endl;
0261     if (badchannel)
0262       bad_file << std::endl;
0263   }
0264 
0265   std::cout << goodChannels << "   " << badChannels << "   " << goodChannels + badChannels << std::endl;
0266 
0267   out_file.close();
0268   bad_file.close();
0269 
0270   return 0;
0271 }
0272 
0273 // get QIE barcodes per RBX from ascii file based on Tina Vernon's spreadsheet,
0274 // get the LMAP channels based on LMAP from the database (OMDS, validation)
0275 // associate QIEs with channels, then append respective ADC slopes and offsets
0276 // based on QIE normmode table info from the database
0277 // finally, generate the QIE table ascii file for HF
0278 int HcalQIEManager::getHfQieTable(std::string input_file, std::string output_file) {
0279   std::cout << "Creating the output file: " << output_file << "... ";
0280   std::ofstream out_file;
0281   out_file.open(output_file.c_str());
0282   std::cout << " done" << std::endl;
0283 
0284   // process the file with QIE-per-RBX info
0285   std::map<std::string, std::vector<int> > _qie;  // 12 QIE barcodes per RBX: _qie["HFP11"]=...
0286   std::ifstream infile(input_file.c_str());
0287   std::string buf;
0288   if (infile.is_open()) {
0289     std::cout << "File is open" << std::endl;
0290     getline(infile, buf);
0291     std::cout << "Table legend: " << std::endl << buf << std::endl;
0292     while (getline(infile, buf)) {
0293       std::vector<std::string> _line = splitString(buf);
0294       if (_line.size() != 17) {
0295         cout << "Table line is malformed, not clear what to do... exiting." << std::endl;
0296         return -1;
0297       }
0298       std::string _rbx = _line[0];
0299       std::vector<int> _barcodes;
0300       for (int i = 0; i != 12; i++) {
0301         int _code;
0302         sscanf(_line[i + 5].c_str(), "%d", &_code);
0303         _barcodes.push_back(_code);
0304       }
0305       _qie.insert(std::pair<std::string, std::vector<int> >(_rbx, _barcodes));
0306     }
0307   }
0308 
0309   // database stuff
0310   HCALConfigDB* db = new HCALConfigDB();
0311   const std::string _accessor =
0312       "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
0313   db->connect(_accessor);
0314   oracle::occi::Connection* _connection = db->getConnection();
0315   std::cout << "Preparing to request the HF channels from LMAP in the database..." << std::endl;
0316   try {
0317     std::cout << "Preparing the query..." << std::endl;
0318     Statement* stmt = _connection->createStatement();
0319 
0320     std::cout << "Preparing the query... done" << std::endl;
0321     std::string query;
0322     query.append("select\n");
0323     query.append("lmap.side*lmap.eta, lmap.phi, lmap.depth, lmap.subdetector,\n");
0324     query.append("lmap.rbx, lmap.rm_slot, lmap.qie_slot, lmap.adc\n");
0325     query.append("from cms_hcl_hcal_condition_owner.hcal_hardware_logical_maps_v3 lmap\n");
0326     query.append("inner join cms_hcl_core_condition_owner.cond_data_sets cds\n");
0327     query.append("on cds.condition_data_set_id=lmap.condition_data_set_id\n");
0328     query.append("where cds.version='30'\n");
0329     query.append("and lmap.subdetector='HF'\n");
0330     query.append("order by lmap.rbx, lmap.rm_slot, lmap.qie_slot, lmap.adc\n");
0331 
0332     //SELECT
0333     std::cout << "Executing the query..." << std::endl;
0334     //std::cout << query << std::endl;
0335     ResultSet* rs = stmt->executeQuery(query);
0336     std::cout << "Executing the query... done" << std::endl;
0337 
0338     std::cout << "Processing the query results..." << std::endl;
0339     RooGKCounter _lines(1, 100);
0340     while (rs->next()) {
0341       _lines.count();
0342       HcalChannelId _id;
0343       std::string rbx;
0344       int rm_slot, qie_slot, adc, qie_barcode;
0345       //count = rs->getInt(1);
0346       _id.eta = rs->getInt(1);
0347       _id.phi = rs->getInt(2);
0348       _id.depth = rs->getInt(3);
0349       _id.subdetector = rs->getString(4);
0350       rbx = rs->getString(5);
0351       rm_slot = rs->getInt(6);
0352       qie_slot = rs->getInt(7);
0353       adc = rs->getInt(8);
0354       qie_barcode = _qie[rbx][(rm_slot - 1) * 4 + qie_slot - 1];
0355 
0356       //==>another DB query to get the ADC caps' slopes and offsets
0357       // NOTE! In HF slope range and capID seem to be exchanged
0358       try {
0359         //std::cout << "Preparing the query..." << std::endl;
0360         Statement* stmt2 = _connection->createStatement();
0361         std::string query2;
0362         query2.append("select\n");
0363         query2.append(
0364             "vadcs.cap0_range0_offset,vadcs.cap0_range1_offset,vadcs.cap0_range2_offset,vadcs.cap0_range3_offset,\n");
0365         query2.append(
0366             "vadcs.cap1_range0_offset,vadcs.cap1_range1_offset,vadcs.cap1_range2_offset,vadcs.cap1_range3_offset,\n");
0367         query2.append(
0368             "vadcs.cap2_range0_offset,vadcs.cap2_range1_offset,vadcs.cap2_range2_offset,vadcs.cap2_range3_offset,\n");
0369         query2.append(
0370             "vadcs.cap3_range0_offset,vadcs.cap3_range1_offset,vadcs.cap3_range2_offset,vadcs.cap3_range3_offset,\n");
0371         query2.append(
0372             "vadcs.cap0_range0_slope,vadcs.cap0_range1_slope,vadcs.cap0_range2_slope,vadcs.cap0_range3_slope,\n");
0373         query2.append(
0374             "vadcs.cap1_range0_slope,vadcs.cap1_range1_slope,vadcs.cap1_range2_slope,vadcs.cap1_range3_slope,\n");
0375         query2.append(
0376             "vadcs.cap2_range0_slope,vadcs.cap2_range1_slope,vadcs.cap2_range2_slope,vadcs.cap2_range3_slope,\n");
0377         query2.append(
0378             "vadcs.cap3_range0_slope,vadcs.cap3_range1_slope,vadcs.cap3_range2_slope,vadcs.cap3_range3_slope\n");
0379         query2.append("from CMS_HCL_HCAL_CONDITION_OWNER.V_QIECARD_ADC_NORMMODE vadcs\n");
0380         query2.append("where substr(vadcs.name_label,14,6)='%d'\n");
0381         query2.append("and substr(vadcs.name_label,21,1)='%d'\n");
0382         query2.append("order by version desc,record_id desc, condition_data_set_id desc\n");
0383         char query2_fixed[5000];
0384         sprintf(query2_fixed, query2.c_str(), qie_barcode, adc);
0385         //std::cout << "Preparing the query... done" << std::endl;
0386         //std::cout << query2_fixed << std::endl;
0387 
0388         //SELECT
0389         //std::cout << "Executing the query..." << std::endl;
0390         //std::cout << query2 << std::endl;
0391         ResultSet* rs2 = stmt2->executeQuery(query2_fixed);
0392         //std::cout << "Executing the query... done" << std::endl;
0393 
0394         //std::cout << "Processing the query results..." << std::endl;
0395         // take only the first line - sorted by version descending, latest on top
0396         if (rs2->next()) {
0397           HcalQIECaps _caps;
0398           for (int j = 0; j != 32; j++) {
0399             _caps.caps[j] = rs2->getDouble(j + 1);
0400           }
0401           //==> output QIE table line
0402           char buffer[1024];
0403           sprintf(buffer, "%15d %15d %15d %15s", _id.eta, _id.phi, _id.depth, _id.subdetector.c_str());
0404           //std::cout << buffer;
0405           out_file << buffer;
0406           for (int j = 0; j != 32; j++) {
0407             double _x = _caps.caps[j];
0408             sprintf(buffer, " %8.5f", _x);
0409             //std::cout << buffer;
0410             out_file << buffer;
0411           }
0412           //std::cout << std::endl;
0413           out_file << std::endl;
0414           //===
0415         }
0416         //Always terminate statement
0417         _connection->terminateStatement(stmt2);
0418 
0419       } catch (SQLException& e) {
0420         XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0421                     ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
0422       }
0423     }
0424     //Always terminate statement
0425     _connection->terminateStatement(stmt);
0426 
0427     //std::cout << "Query count: " << count << std::endl;
0428     std::cout << "Query line count: " << _lines.getCount() << std::endl;
0429   } catch (SQLException& e) {
0430     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0431                 ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
0432   }
0433 
0434   db->disconnect();
0435   out_file.close();
0436 
0437   return 0;
0438 }