CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CondTools/Hcal/src/HcalDbPoolOCCI.cc

Go to the documentation of this file.
00001 
00002 //
00003 // F.Ratnikov (UMd), Dec 14, 2005
00004 // $Id: HcalDbPoolOCCI.cc,v 1.7 2010/11/29 20:41:57 wmtan Exp $
00005 //
00006 #include <string>
00007 #include <iostream>
00008 #include <cstdio>
00009 
00010 #include "OnlineDB/Oracle/interface/Oracle.h" 
00011 
00012 #include "CondTools/Hcal/interface/HcalDbPoolOCCI.h"
00013 
00014 const bool debug = false;
00015 
00016 namespace {
00017   long getObjectId (const std::string& fToken) {
00018     size_t ipos = fToken.find ("OID=");
00019     if (ipos != std::string::npos) {
00020       ipos = fToken.find ('-', ipos);
00021       if (ipos != std::string::npos) {
00022         size_t ipos2 = fToken.find (']', ipos);
00023         if (ipos2 != std::string::npos) {
00024           while (fToken [++ipos] != '0');
00025           std::string id (fToken, ipos, ipos2-ipos);
00026           char* endptr = 0;
00027           unsigned long result = strtoul (id.c_str (), &endptr, 16);
00028           if (endptr && !*endptr) return long (result);
00029         }
00030       }
00031     }
00032     return -1;
00033   }
00034   
00035   const char* getTable (const HcalPedestals* fObject) {return "HCALPEDESTAL";}
00036   const char* getTable (const HcalGains* fObject) {return "HCALGAIN";}
00037 }
00038 
00039 HcalDbPoolOCCI::HcalDbPoolOCCI (const std::string& fDb) 
00040   : mConnect (0)
00041 {
00042   mEnvironment = oracle::occi::Environment::createEnvironment (oracle::occi::Environment::OBJECT);
00043   // decode connect string
00044   size_t ipass = fDb.find ('/');
00045   size_t ihost = fDb.find ('@');
00046   
00047   if (ipass == std::string::npos || ihost == std::string::npos) {
00048     std::cerr << "HcalDbPoolOCCI::HcalDbPoolOCCI-> Error in connection string format: " << fDb
00049               << " Expect user/password@db" << std::endl;
00050   }
00051   else {
00052     std::string user (fDb, 0, ipass);
00053     std::string pass (fDb, ipass+1, ihost-ipass-1);
00054     std::string host (fDb, ihost+1);
00055     //     if (debug) std::cout << "HcalDbPoolOCCI::HcalDbPoolOCCI-> Connecting " << user << '/' << pass << '@' << host << std::endl;
00056     try {
00057       mConnect = mEnvironment->createConnection(user, pass, host);
00058       mStatement = mConnect->createStatement ();
00059     }
00060     catch (oracle::occi::SQLException& sqlExcp) {
00061       std::cerr << "HcalDbPoolOCCI::HcalDbPoolOCCI exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
00062     }
00063   }
00064 }
00065 
00066 HcalDbPoolOCCI::~HcalDbPoolOCCI () {
00067   delete mStatement;
00068   mEnvironment->terminateConnection (mConnect);
00069   oracle::occi::Environment::terminateEnvironment (mEnvironment);
00070 }
00071 
00072 bool HcalDbPoolOCCI::getObject (HcalPedestals* fObject, const std::string& fTag, unsigned long fRun) {
00073   HcalPedestal* myped(0);
00074   return getObjectGeneric (fObject, myped, fTag, fRun);
00075 }
00076 
00077 bool HcalDbPoolOCCI::getObject (HcalGains* fObject, const std::string& fTag, unsigned long fRun) {
00078   HcalGain* mygain(0);
00079   return getObjectGeneric (fObject, mygain, fTag, fRun);
00080 }
00081 
00082 bool HcalDbPoolOCCI::getObject (HcalElectronicsMap* fObject, const std::string& fTag, unsigned long fRun) {
00083   return false;
00084 }
00085 
00086 
00087 
00088 std::string HcalDbPoolOCCI::getMetadataToken (const std::string& fTag) {
00089   std::string result = "";
00090   std::string sql_query = "select * from metadata";
00091   try {
00092      if (debug) std::cout << "executing query: \n" << sql_query << std::endl;
00093     mStatement->setPrefetchRowCount (100);
00094     mStatement->setSQL (sql_query);
00095     oracle::occi::ResultSet* rset = mStatement->executeQuery ();
00096     while (rset->next ()) {
00097       std::string name = rset->getString (1);
00098       std::string token = rset->getString (2);
00099       if (name == fTag) {
00100         result = token;
00101         break;
00102       }
00103     }
00104     delete rset;
00105   }
00106   catch (oracle::occi::SQLException& sqlExcp) {
00107     std::cerr << "HcalDbPoolOCCI::getMetadataToken exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
00108   }
00109   return result;
00110 }
00111 
00112 std::string HcalDbPoolOCCI::getDataToken (const std::string& fIov, unsigned long fRun) {
00113   std::string result = "";
00114   long iovId = getObjectId (fIov);
00115   if (iovId >= 0) {
00116     char sql_query [1024];
00117     sprintf (sql_query, "select IOV_IOV_UNSIGNED_LONG, IOV_IOV_STRING from COND__IOV_IOV where ID_ID = %ld ORDER BY IOV_IOV_UNSIGNED_LONG DESC", iovId);
00118     try {
00119        if (debug) std::cout << "executing query: \n" << sql_query << std::endl;
00120       mStatement->setPrefetchRowCount (100);
00121       mStatement->setSQL (std::string (sql_query));
00122       oracle::occi::ResultSet* rset = mStatement->executeQuery ();
00123       while (rset->next ()) {
00124         unsigned long runMax = rset->getUInt (1);
00125         std::string token = rset->getString (2);
00126         if (fRun <= runMax) {
00127           result = token;
00128           break;
00129         }
00130       }
00131       delete rset;
00132     }
00133     catch (oracle::occi::SQLException& sqlExcp) {
00134       std::cerr << "HcalDbPoolOCCI::getDataToken exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
00135     }
00136   }
00137   return result;
00138 }
00139 
00140 template <class T, class S>
00141 bool HcalDbPoolOCCI::getObjectGeneric (T* fObject, S* fCondObject, const std::string& fTag, unsigned long fRun) {
00142   std::string mdToken = getMetadataToken (fTag);
00143    if (debug) std::cout << "HcalDbPoolOCCI::getObjectGeneric-> tag/token: " << fTag << '/' << mdToken << std::endl;
00144   if (mdToken.empty ()) return false;
00145   std::string objToken = getDataToken (mdToken, fRun);
00146    if (debug) std::cout << "HcalDbPoolOCCI::getObjectGeneric-> Run/token: " << fRun << '/' << objToken << std::endl;
00147   if (objToken.empty ()) return false;
00148   long id = getObjectId (objToken);
00149   if (id >= 0) {
00150     char sql_query [1024];
00151     const char* name = getTable (fObject);
00152     sprintf (sql_query, "select MITEMS_%s_MID, MITEMS_%s_MVALUE1, MITEMS_%s_MVALUE2, MITEMS_%s_MVALUE3, MITEMS_%s_MVALUE4 from %sS_MITEMS where ID_ID = %ld ORDER BY MITEMS_%s_MID",
00153              name, name, name, name, name, name, id, name);
00154     try {
00155        if (debug) std::cout << "executing query: \n" << sql_query << std::endl;
00156       mStatement->setPrefetchRowCount (100);
00157       mStatement->setSQL (sql_query);
00158       oracle::occi::ResultSet* rset = mStatement->executeQuery ();
00159       while (rset->next ()) {
00160         unsigned long hcalId = rset->getUInt (1);
00161         float values [4];
00162         for (int i = 0; i < 4; i++) values [i] = rset->getFloat (i+2);
00163 
00164         fCondObject = new S(DetId (hcalId), values[0], values[1], values[2], values[3]);
00165         fObject->addValues (*fCondObject);
00166         delete fCondObject;
00167         //       if (debug) std::cout << "new entry: " << hcalId << '/' << values [0] << '/' << values [1] << '/' 
00168         //        << values [2] << '/' << values [3] << std::endl;
00169       }
00170       delete rset;
00171       return true;
00172     }
00173     catch (oracle::occi::SQLException& sqlExcp) {
00174       std::cerr << "HcalDbPoolOCCI::getObjectn exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
00175     }
00176   }
00177   return false;
00178 }