CMS 3D CMS Logo

HcalDbPoolOCCI.cc

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

Generated on Tue Jun 9 17:26:54 2009 for CMSSW by  doxygen 1.5.4