CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CaloOnlineTools/HcalOnlineDb/src/HCALConfigDB.cc

Go to the documentation of this file.
00001 
00002 //
00003 // Gena Kukartsev (Brown), Feb 1, 2008
00004 // $Id:
00005 //
00006 
00007 #include <iostream>
00008 #include <string.h>
00009 
00010 #include "xgi/Utils.h"
00011 #include "toolbox/string.h"
00012 
00013 #include "OnlineDB/Oracle/interface/Oracle.h"
00014 
00015 #include "CaloOnlineTools/HcalOnlineDb/interface/HCALConfigDB.h"
00016 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
00017 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabase.hh"
00018 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplOracle.hh"
00019 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplXMLFile.hh"
00020 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
00021 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00022 
00023 using namespace std;
00024 using namespace oracle::occi;
00025 using namespace hcal;
00026 
00027 HCALConfigDB::HCALConfigDB( void )
00028 {    
00029   database = 0;
00030   database2 = 0;
00031 }
00032 
00033 HCALConfigDB::HCALConfigDB( std::string _accessor )
00034 {    
00035   database = 0;
00036   database2 = 0;
00037   accessor = _accessor;
00038 }
00039 
00040 
00041 HCALConfigDB::~HCALConfigDB( void )
00042 {    
00043   delete database;
00044   delete database2;
00045 }
00046 
00047 
00048 void HCALConfigDB::setAccessor( std::string _accessor )
00049 {      
00050   accessor = _accessor;
00051 }
00052 
00053 void HCALConfigDB::connect( std::string _accessor )
00054 {
00055 
00056   accessor = _accessor;
00057 
00058   std::string::size_type i = accessor . find( "occi://" );
00059   if ( i!=std::string::npos )
00060     {
00061       database = new ConfigurationDatabaseImplOracle();
00062       database -> connect( accessor );
00063     }
00064   else
00065     {
00066       database = new ConfigurationDatabaseImplXMLFile();
00067       database -> connect( accessor );
00068     }
00069 }
00070 
00071 
00072 
00073 void HCALConfigDB::connect( std::string _accessor1, std::string _accessor2 )
00074 {
00075 
00076   connect (_accessor1 );
00077 
00078   accessor2 = _accessor2;
00079 
00080   std::string::size_type i = accessor2 . find( "occi://" );
00081   if ( i!=std::string::npos )
00082     {
00083       database2 = new ConfigurationDatabaseImplOracle();
00084       database2 -> connect( accessor2 );
00085     }
00086   else
00087     {
00088       database2 = new ConfigurationDatabaseImplXMLFile();
00089       database2 -> connect( accessor2 );
00090     }
00091 }
00092 
00093 
00094 
00095 
00096 void HCALConfigDB::disconnect( void )
00097 {
00098   if ( database != NULL ) database -> disconnect();
00099   if ( database2 != NULL ) database2 -> disconnect();
00100 }
00101 
00102 
00103 
00104 std::vector<unsigned int> HCALConfigDB::getOnlineLUT( std::string tag, int crate, int slot, int topbottom, int fiber, int channel, int luttype )
00105 {
00106 
00107   //connect( accessor );
00108 
00109   std::vector<unsigned int> result;
00110 
00111   hcal::ConfigurationDatabase::FPGASelection _fpga;
00112   if ( topbottom == 0 ) _fpga = hcal::ConfigurationDatabase::Bottom;
00113   else if ( topbottom == 1 ) _fpga = hcal::ConfigurationDatabase::Top;
00114   else
00115     {
00116       std::cout << "topbottom out of range" << std::endl;
00117       exit(-1);
00118     }
00119 
00120   hcal::ConfigurationDatabase::LUTType _lt;
00121   if ( luttype == 1 ) _lt = hcal::ConfigurationDatabase::LinearizerLUT;
00122   else if ( luttype == 2 ) _lt = hcal::ConfigurationDatabase::CompressionLUT;
00123   else
00124     {
00125       std::cout << "LUT type out of range" << std::endl;
00126       exit(-1);
00127     }
00128 
00129   hcal::ConfigurationDatabase::LUTId _lutid( crate, slot, _fpga, fiber, channel, _lt );
00130   std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::LUT> testLUTs;
00131 
00132   XMLProcessor::getInstance();
00133 
00134   try {
00135     database -> getLUTs(tag, crate, slot, testLUTs);
00136   } catch (hcal::exception::ConfigurationItemNotFoundException& e) {
00137     std::cout << "Found nothing!" << std::endl;
00138   } catch (hcal::exception::Exception& e2) {
00139     std::cout << "Exception: " << e2.what() << std::endl;
00140   }
00141 
00142   result = testLUTs[_lutid];
00143 
00144   //database -> disconnect();
00145 
00146   return result;
00147 }
00148 
00149 std::vector<unsigned int> HCALConfigDB::getOnlineLUT( std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt )
00150 {
00151   std::vector<unsigned int> result;
00152   HcalDetId _id( _rawid );
00153 
00154   double _condition_data_set_id;
00155   unsigned int _crate, _slot, _fiber, _channel;
00156   hcal::ConfigurationDatabase::FPGASelection _fpga;
00157 
00158   int side   = _id . zside();
00159   int etaAbs = _id . ietaAbs();
00160   int phi    = _id . iphi();
00161   int depth  = _id . depth();
00162   std::string subdetector;
00163   if ( _id . subdet() == HcalBarrel) subdetector = "HB";
00164   else if ( _id . subdet() == HcalEndcap) subdetector = "HE";
00165   else if ( _id . subdet() == HcalOuter) subdetector = "HO";
00166   else if ( _id . subdet() == HcalForward) subdetector = "HF";
00167 
00168   oracle::occi::Connection * _connection = database -> getConnection();
00169 
00170   try {
00171     Statement* stmt = _connection -> createStatement();
00172     std::string query = ("SELECT RECORD_ID, CRATE, HTR_SLOT, HTR_FPGA, HTR_FIBER, FIBER_CHANNEL ");
00173     query += " FROM CMS_HCL_HCAL_CONDITION_OWNER.HCAL_HARDWARE_LOGICAL_MAPS_V3 ";
00174     query += toolbox::toString(" WHERE SIDE=%d AND ETA=%d AND PHI=%d AND DEPTH=%d AND SUBDETECTOR='%s'", side, etaAbs, phi, depth, subdetector . c_str() );
00175     
00176     //SELECT
00177     ResultSet *rs = stmt->executeQuery(query.c_str());
00178 
00179     _condition_data_set_id = 0.0;
00180 
00181     while (rs->next()) {
00182       double _cdsi = rs -> getDouble(1);
00183       if ( _condition_data_set_id < _cdsi )
00184         {
00185           _condition_data_set_id = _cdsi;
00186           _crate    = rs -> getInt(2);
00187           _slot     = rs -> getInt(3);
00188           std::string fpga_ = rs -> getString(4);
00189           if ( fpga_ == "top" ) _fpga = hcal::ConfigurationDatabase::Top;
00190           else _fpga  = hcal::ConfigurationDatabase::Bottom;
00191           _fiber    = rs -> getInt(5);
00192           _channel  = rs -> getInt(6);
00193           
00194           int topbottom, luttype;
00195           if ( _fpga == hcal::ConfigurationDatabase::Top ) topbottom = 1;
00196           else topbottom = 0;
00197           if ( _lt == hcal::ConfigurationDatabase::LinearizerLUT ) luttype = 1;
00198           else luttype = 2;
00199           
00200           result = getOnlineLUT( tag, _crate, _slot, topbottom, _fiber, _channel, luttype );
00201         }
00202     }
00203     //Always terminate statement
00204     _connection -> terminateStatement(stmt);
00205   } catch (SQLException& e) {
00206     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
00207   }
00208   return result;
00209 }
00210 
00211 
00212 std::vector<unsigned int> HCALConfigDB::getOnlineLUTFromXML( std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt ){
00213 
00214   std::vector<unsigned int> result;
00215 
00216   if ( database && database2 ){
00217     
00218     HcalDetId _id( _rawid );
00219     
00220     double _condition_data_set_id;
00221     unsigned int _crate, _slot, _fiber, _channel;
00222     hcal::ConfigurationDatabase::FPGASelection _fpga;
00223     
00224     int side   = _id . zside();
00225     int etaAbs = _id . ietaAbs();
00226     int phi    = _id . iphi();
00227     int depth  = _id . depth();
00228     std::string subdetector;
00229     if ( _id . subdet() == HcalBarrel) subdetector = "HB";
00230     else if ( _id . subdet() == HcalEndcap) subdetector = "HE";
00231     else if ( _id . subdet() == HcalOuter) subdetector = "HO";
00232     else if ( _id . subdet() == HcalForward) subdetector = "HF";
00233     
00234     oracle::occi::Connection * _connection = database2 -> getConnection();
00235     
00236     try {
00237       Statement* stmt = _connection -> createStatement();
00238       std::string query = ("SELECT RECORD_ID, CRATE, HTR_SLOT, HTR_FPGA, HTR_FIBER, FIBER_CHANNEL ");
00239       query += " FROM CMS_HCL_HCAL_CONDITION_OWNER.HCAL_HARDWARE_LOGICAL_MAPS_V3 ";
00240       query += toolbox::toString(" WHERE SIDE=%d AND ETA=%d AND PHI=%d AND DEPTH=%d AND SUBDETECTOR='%s'", side, etaAbs, phi, depth, subdetector . c_str() );
00241       
00242       //SELECT
00243       ResultSet *rs = stmt->executeQuery(query.c_str());
00244       
00245       _condition_data_set_id = 0.0;
00246       
00247       while (rs->next()) {
00248         double _cdsi = rs -> getDouble(1);
00249         if ( _condition_data_set_id < _cdsi )
00250           {
00251             _condition_data_set_id = _cdsi;
00252             _crate    = rs -> getInt(2);
00253             _slot     = rs -> getInt(3);
00254             std::string fpga_ = rs -> getString(4);
00255             if ( fpga_ == "top" ) _fpga = hcal::ConfigurationDatabase::Top;
00256             else _fpga  = hcal::ConfigurationDatabase::Bottom;
00257             _fiber    = rs -> getInt(5);
00258             _channel  = rs -> getInt(6);
00259             
00260             //
00261             int topbottom, luttype;
00262             if ( _fpga == hcal::ConfigurationDatabase::Top ) topbottom = 1;
00263             else topbottom = 0;
00264             if ( _lt == hcal::ConfigurationDatabase::LinearizerLUT ) luttype = 1;
00265             else luttype = 2;
00266             result = getOnlineLUT( tag, _crate, _slot, topbottom, _fiber, _channel, luttype );
00267           }
00268       }
00269       //Always terminate statement
00270       _connection -> terminateStatement(stmt);
00271 
00272     } catch (SQLException& e) {
00273       XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
00274     }
00275   }
00276   else{
00277     std::cout << "Either the XML file with LUTs or the database with LMap are not defined" << std::endl;
00278   }
00279 
00280   return result;
00281 }
00282 
00283 
00284 oracle::occi::Connection * HCALConfigDB::getConnection( void ){
00285   return database -> getConnection();
00286 }
00287 
00288 oracle::occi::Environment * HCALConfigDB::getEnvironment( void ){
00289   return database -> getEnvironment();
00290 }
00291 
00292 
00293 
00294 //Utility function that cnverts oracle::occi::Clob to std::string
00295 string HCALConfigDB::clobToString(const oracle::occi::Clob& _clob){
00296                 oracle::occi::Clob clob = _clob;
00297                 Stream *instream = clob.getStream (1,0);
00298                 unsigned int size = clob.length();
00299                 char *cbuffer = new char[size];
00300                 memset (cbuffer, 0, size);
00301                 instream->readBuffer (cbuffer, size);
00302                 std::string str(cbuffer,size);
00303                 return str;
00304 }