Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:00:07

0001 
0002 //
0003 // Gena Kukartsev (Brown), Feb 1, 2008
0004 // $Id:
0005 //
0006 
0007 #include <iostream>
0008 #include <cstring>
0009 
0010 #ifdef HAVE_XDAQ
0011 #include <toolbox/string.h>
0012 #else
0013 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h"  // Replaces toolbox::toString
0014 #endif
0015 
0016 #include "OnlineDB/Oracle/interface/Oracle.h"
0017 
0018 #include "CaloOnlineTools/HcalOnlineDb/interface/HCALConfigDB.h"
0019 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
0020 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabase.hh"
0021 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplOracle.hh"
0022 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplXMLFile.hh"
0023 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
0024 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0025 
0026 using namespace std;
0027 using namespace oracle::occi;
0028 using namespace hcal;
0029 
0030 HCALConfigDB::HCALConfigDB(void) {
0031   database = nullptr;
0032   database2 = nullptr;
0033 }
0034 
0035 HCALConfigDB::HCALConfigDB(std::string _accessor) {
0036   database = nullptr;
0037   database2 = nullptr;
0038   accessor = _accessor;
0039 }
0040 
0041 HCALConfigDB::~HCALConfigDB(void) {
0042   delete database;
0043   delete database2;
0044 }
0045 
0046 void HCALConfigDB::setAccessor(std::string _accessor) { accessor = _accessor; }
0047 
0048 void HCALConfigDB::connect(std::string _accessor) {
0049   accessor = _accessor;
0050 
0051   std::string::size_type i = accessor.find("occi://");
0052   if (i != std::string::npos) {
0053     database = new ConfigurationDatabaseImplOracle();
0054     database->connect(accessor);
0055   } else {
0056     database = new ConfigurationDatabaseImplXMLFile();
0057     database->connect(accessor);
0058   }
0059 }
0060 
0061 void HCALConfigDB::connect(std::string _accessor1, std::string _accessor2) {
0062   connect(_accessor1);
0063 
0064   accessor2 = _accessor2;
0065 
0066   std::string::size_type i = accessor2.find("occi://");
0067   if (i != std::string::npos) {
0068     database2 = new ConfigurationDatabaseImplOracle();
0069     database2->connect(accessor2);
0070   } else {
0071     database2 = new ConfigurationDatabaseImplXMLFile();
0072     database2->connect(accessor2);
0073   }
0074 }
0075 
0076 void HCALConfigDB::disconnect(void) {
0077   if (database != nullptr)
0078     database->disconnect();
0079   if (database2 != nullptr)
0080     database2->disconnect();
0081 }
0082 
0083 std::vector<unsigned int> HCALConfigDB::getOnlineLUT(
0084     std::string tag, int crate, int slot, int topbottom, int fiber, int channel, int luttype) {
0085   //connect( accessor );
0086 
0087   std::vector<unsigned int> result;
0088 
0089   hcal::ConfigurationDatabase::FPGASelection _fpga;
0090   if (topbottom == 0)
0091     _fpga = hcal::ConfigurationDatabase::Bottom;
0092   else if (topbottom == 1)
0093     _fpga = hcal::ConfigurationDatabase::Top;
0094   else {
0095     std::cout << "topbottom out of range" << std::endl;
0096     exit(-1);
0097   }
0098 
0099   hcal::ConfigurationDatabase::LUTType _lt;
0100   if (luttype == 1)
0101     _lt = hcal::ConfigurationDatabase::LinearizerLUT;
0102   else if (luttype == 2)
0103     _lt = hcal::ConfigurationDatabase::CompressionLUT;
0104   else {
0105     std::cout << "LUT type out of range" << std::endl;
0106     exit(-1);
0107   }
0108 
0109   hcal::ConfigurationDatabase::LUTId _lutid(crate, slot, _fpga, fiber, channel, _lt);
0110   std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::LUT> testLUTs;
0111 
0112   XMLProcessor::getInstance();
0113 
0114   try {
0115     database->getLUTs(tag, crate, slot, testLUTs);
0116   } catch (hcal::exception::ConfigurationItemNotFoundException& e) {
0117     std::cout << "Found nothing!" << std::endl;
0118   } catch (hcal::exception::Exception& e2) {
0119     std::cout << "Exception: " << e2.what() << std::endl;
0120   }
0121 
0122   result = testLUTs[_lutid];
0123 
0124   //database -> disconnect();
0125 
0126   return result;
0127 }
0128 
0129 std::vector<unsigned int> HCALConfigDB::getOnlineLUT(std::string tag,
0130                                                      uint32_t _rawid,
0131                                                      hcal::ConfigurationDatabase::LUTType _lt) {
0132   std::vector<unsigned int> result;
0133   HcalDetId _id(_rawid);
0134 
0135   double _condition_data_set_id;
0136   unsigned int _crate, _slot, _fiber, _channel;
0137   hcal::ConfigurationDatabase::FPGASelection _fpga;
0138 
0139   int side = _id.zside();
0140   int etaAbs = _id.ietaAbs();
0141   int phi = _id.iphi();
0142   int depth = _id.depth();
0143   std::string subdetector;
0144   if (_id.subdet() == HcalBarrel)
0145     subdetector = "HB";
0146   else if (_id.subdet() == HcalEndcap)
0147     subdetector = "HE";
0148   else if (_id.subdet() == HcalOuter)
0149     subdetector = "HO";
0150   else if (_id.subdet() == HcalForward)
0151     subdetector = "HF";
0152 
0153   oracle::occi::Connection* _connection = database->getConnection();
0154 
0155   try {
0156     Statement* stmt = _connection->createStatement();
0157     std::string query = ("SELECT RECORD_ID, CRATE, HTR_SLOT, HTR_FPGA, HTR_FIBER, FIBER_CHANNEL ");
0158     query += " FROM CMS_HCL_HCAL_CONDITION_OWNER.HCAL_HARDWARE_LOGICAL_MAPS_V3 ";
0159     query += toolbox::toString(" WHERE SIDE=%d AND ETA=%d AND PHI=%d AND DEPTH=%d AND SUBDETECTOR='%s'",
0160                                side,
0161                                etaAbs,
0162                                phi,
0163                                depth,
0164                                subdetector.c_str());
0165 
0166     //SELECT
0167     ResultSet* rs = stmt->executeQuery(query);
0168 
0169     _condition_data_set_id = 0.0;
0170 
0171     while (rs->next()) {
0172       double _cdsi = rs->getDouble(1);
0173       if (_condition_data_set_id < _cdsi) {
0174         _condition_data_set_id = _cdsi;
0175         _crate = rs->getInt(2);
0176         _slot = rs->getInt(3);
0177         std::string fpga_ = rs->getString(4);
0178         if (fpga_ == "top")
0179           _fpga = hcal::ConfigurationDatabase::Top;
0180         else
0181           _fpga = hcal::ConfigurationDatabase::Bottom;
0182         _fiber = rs->getInt(5);
0183         _channel = rs->getInt(6);
0184 
0185         int topbottom, luttype;
0186         if (_fpga == hcal::ConfigurationDatabase::Top)
0187           topbottom = 1;
0188         else
0189           topbottom = 0;
0190         if (_lt == hcal::ConfigurationDatabase::LinearizerLUT)
0191           luttype = 1;
0192         else
0193           luttype = 2;
0194 
0195         result = getOnlineLUT(tag, _crate, _slot, topbottom, _fiber, _channel, luttype);
0196       }
0197     }
0198     //Always terminate statement
0199     _connection->terminateStatement(stmt);
0200   } catch (SQLException& e) {
0201     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0202                 ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
0203   }
0204   return result;
0205 }
0206 
0207 std::vector<unsigned int> HCALConfigDB::getOnlineLUTFromXML(std::string tag,
0208                                                             uint32_t _rawid,
0209                                                             hcal::ConfigurationDatabase::LUTType _lt) {
0210   std::vector<unsigned int> result;
0211 
0212   if (database && database2) {
0213     HcalDetId _id(_rawid);
0214 
0215     double _condition_data_set_id;
0216     unsigned int _crate, _slot, _fiber, _channel;
0217     hcal::ConfigurationDatabase::FPGASelection _fpga;
0218 
0219     int side = _id.zside();
0220     int etaAbs = _id.ietaAbs();
0221     int phi = _id.iphi();
0222     int depth = _id.depth();
0223     std::string subdetector;
0224     if (_id.subdet() == HcalBarrel)
0225       subdetector = "HB";
0226     else if (_id.subdet() == HcalEndcap)
0227       subdetector = "HE";
0228     else if (_id.subdet() == HcalOuter)
0229       subdetector = "HO";
0230     else if (_id.subdet() == HcalForward)
0231       subdetector = "HF";
0232 
0233     oracle::occi::Connection* _connection = database2->getConnection();
0234 
0235     try {
0236       Statement* stmt = _connection->createStatement();
0237       std::string query = ("SELECT RECORD_ID, CRATE, HTR_SLOT, HTR_FPGA, HTR_FIBER, FIBER_CHANNEL ");
0238       query += " FROM CMS_HCL_HCAL_CONDITION_OWNER.HCAL_HARDWARE_LOGICAL_MAPS_V3 ";
0239       query += toolbox::toString(" WHERE SIDE=%d AND ETA=%d AND PHI=%d AND DEPTH=%d AND SUBDETECTOR='%s'",
0240                                  side,
0241                                  etaAbs,
0242                                  phi,
0243                                  depth,
0244                                  subdetector.c_str());
0245 
0246       //SELECT
0247       ResultSet* rs = stmt->executeQuery(query);
0248 
0249       _condition_data_set_id = 0.0;
0250 
0251       while (rs->next()) {
0252         double _cdsi = rs->getDouble(1);
0253         if (_condition_data_set_id < _cdsi) {
0254           _condition_data_set_id = _cdsi;
0255           _crate = rs->getInt(2);
0256           _slot = rs->getInt(3);
0257           std::string fpga_ = rs->getString(4);
0258           if (fpga_ == "top")
0259             _fpga = hcal::ConfigurationDatabase::Top;
0260           else
0261             _fpga = hcal::ConfigurationDatabase::Bottom;
0262           _fiber = rs->getInt(5);
0263           _channel = rs->getInt(6);
0264 
0265           //
0266           int topbottom, luttype;
0267           if (_fpga == hcal::ConfigurationDatabase::Top)
0268             topbottom = 1;
0269           else
0270             topbottom = 0;
0271           if (_lt == hcal::ConfigurationDatabase::LinearizerLUT)
0272             luttype = 1;
0273           else
0274             luttype = 2;
0275           result = getOnlineLUT(tag, _crate, _slot, topbottom, _fiber, _channel, luttype);
0276         }
0277       }
0278       //Always terminate statement
0279       _connection->terminateStatement(stmt);
0280 
0281     } catch (SQLException& e) {
0282       XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0283                   ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
0284     }
0285   } else {
0286     std::cout << "Either the XML file with LUTs or the database with LMap are not defined" << std::endl;
0287   }
0288 
0289   return result;
0290 }
0291 
0292 oracle::occi::Connection* HCALConfigDB::getConnection(void) { return database->getConnection(); }
0293 
0294 oracle::occi::Environment* HCALConfigDB::getEnvironment(void) { return database->getEnvironment(); }
0295 
0296 //Utility function that cnverts oracle::occi::Clob to std::string
0297 string HCALConfigDB::clobToString(const oracle::occi::Clob& _clob) {
0298   oracle::occi::Clob clob = _clob;
0299   Stream* instream = clob.getStream(1, 0);
0300   unsigned int size = clob.length();
0301   char* cbuffer = new char[size];
0302   memset(cbuffer, 0, size);
0303   instream->readBuffer(cbuffer, size);
0304   std::string str(cbuffer, size);
0305   return str;
0306 }