CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/OnlineDB/EcalCondDB/src/CaliCrystalIntercalDat.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004 
00005 #include "OnlineDB/EcalCondDB/interface/CaliCrystalIntercalDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/CaliTag.h"
00007 #include "OnlineDB/EcalCondDB/interface/CaliIOV.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 CaliCrystalIntercalDat::CaliCrystalIntercalDat()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017 
00018   m_cali = 0;
00019   m_caliRMS = 0;
00020   m_numEvents = 0;
00021   m_taskStatus = false;
00022 }
00023 
00024 
00025 
00026 CaliCrystalIntercalDat::~CaliCrystalIntercalDat()
00027 {
00028 }
00029 
00030 
00031 
00032 void CaliCrystalIntercalDat::prepareWrite()
00033   throw(std::runtime_error)
00034 {
00035   this->checkConnection();
00036   
00037   try {
00038     m_writeStmt = m_conn->createStatement();
00039     m_writeStmt->setSQL("INSERT INTO cali_crystal_intercal_dat (iov_id, logic_id, "
00040                         "cali, cali_rms, num_events, task_status) "
00041                         "VALUES (:iov_id, :logic_id, "
00042                         ":3, :4, :5, :6)");
00043   } catch (SQLException &e) {
00044     throw(std::runtime_error("CaliCrystalIntercalDat::prepareWrite():  "+e.getMessage()));
00045   }
00046 }
00047 
00048 
00049 
00050 void CaliCrystalIntercalDat::writeDB(const EcalLogicID* ecid, const CaliCrystalIntercalDat* item, CaliIOV* iov)
00051   throw(std::runtime_error)
00052 {
00053   this->checkConnection();
00054   this->checkPrepare();
00055   
00056   int iovID = iov->fetchID();
00057   if (!iovID) { throw(std::runtime_error("CaliCrystalIntercalDat::writeDB:  IOV not in DB")); }
00058   
00059   int logicID = ecid->getLogicID();
00060   if (!logicID) { throw(std::runtime_error("CaliCrystalIntercalDat::writeDB:  Bad EcalLogicID")); }
00061   
00062   try {
00063     m_writeStmt->setInt(1, iovID);
00064     m_writeStmt->setInt(2, logicID);
00065     
00066     m_writeStmt->setFloat(3, item->getCali() );
00067     m_writeStmt->setFloat(4, item->getCaliRMS() );
00068     m_writeStmt->setInt(5, item->getNumEvents() );
00069     m_writeStmt->setInt(6, item->getTaskStatus() );
00070     
00071     m_writeStmt->executeUpdate();
00072   } catch (SQLException &e) {
00073     throw(std::runtime_error("CaliCrystalIntercalDat::writeDB():  "+e.getMessage()));
00074   }
00075 }
00076 
00077 
00078 
00079 void CaliCrystalIntercalDat::fetchData(std::map< EcalLogicID, CaliCrystalIntercalDat >* fillMap, CaliIOV* iov)
00080   throw(std::runtime_error)
00081 {
00082   this->checkConnection();
00083   fillMap->clear();
00084   
00085   iov->setConnection(m_env, m_conn);
00086   int iovID = iov->fetchID();
00087   if (!iovID) { 
00088     //  throw(std::runtime_error("CaliCrystalIntercalDat::writeDB:  IOV not in DB")); 
00089     return;
00090   }
00091   
00092   try {
00093     
00094     m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00095                  "d.cali, d.cali_rms, d.num_events, d.task_status "
00096                  "FROM channelview cv JOIN cali_crystal_intercal_dat d "
00097                  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00098                  "WHERE d.iov_id = :iov_id");
00099     m_readStmt->setInt(1, iovID);
00100     ResultSet* rset = m_readStmt->executeQuery();
00101     
00102     std::pair< EcalLogicID, CaliCrystalIntercalDat > p;
00103     CaliCrystalIntercalDat dat;
00104     while(rset->next()) {
00105       p.first = EcalLogicID( rset->getString(1),     // name
00106                              rset->getInt(2),        // logic_id
00107                              rset->getInt(3),        // id1
00108                              rset->getInt(4),        // id2
00109                              rset->getInt(5),        // id3
00110                              rset->getString(6));    // maps_to
00111       
00112       dat.setCali( rset->getFloat(7) );
00113       dat.setCaliRMS( rset->getFloat(8) );
00114       dat.setNumEvents( rset->getInt(9) );
00115       dat.setTaskStatus( rset->getInt(10) );
00116       
00117       p.second = dat;
00118       fillMap->insert(p);
00119     }
00120   } catch (SQLException &e) {
00121     throw(std::runtime_error("CaliCrystalIntercalDat::fetchData():  "+e.getMessage()));
00122   }
00123 }
00124 
00125 void CaliCrystalIntercalDat::writeArrayDB(const std::map< EcalLogicID, CaliCrystalIntercalDat >* data, CaliIOV* iov)
00126   throw(std::runtime_error)
00127 {
00128   this->checkConnection();
00129   this->checkPrepare();
00130 
00131   int iovID = iov->fetchID();
00132   if (!iovID) { throw(std::runtime_error("CaliCrystalIntercalDat::writeArrayDB:  IOV not in DB")); }
00133 
00134 
00135   int nrows=data->size();
00136   int* ids= new int[nrows];
00137   int* iovid_vec= new int[nrows];
00138   float* xx= new float[nrows];
00139   float* yy= new float[nrows];
00140   int* tt= new int[nrows];
00141   int* st= new int[nrows];
00142 
00143   ub2* ids_len= new ub2[nrows];
00144   ub2* iov_len= new ub2[nrows];
00145   ub2* x_len= new ub2[nrows];
00146   ub2* y_len= new ub2[nrows];
00147   ub2* tt_len= new ub2[nrows];
00148   ub2* st_len= new ub2[nrows];
00149 
00150   const EcalLogicID* channel;
00151   const CaliCrystalIntercalDat* dataitem;
00152   int count=0;
00153   typedef map< EcalLogicID, CaliCrystalIntercalDat >::const_iterator CI;
00154   for (CI p = data->begin(); p != data->end(); ++p) {
00155     channel = &(p->first);
00156     int logicID = channel->getLogicID();
00157     if (!logicID) { throw(std::runtime_error("CaliCrystalIntercalDat::writeArrayDB:  Bad EcalLogicID")); }
00158     ids[count]=logicID;
00159     iovid_vec[count]=iovID;
00160 
00161     dataitem = &(p->second);
00162     // dataIface.writeDB( channel, dataitem, iov);
00163     float x=dataitem->getCali();
00164     float y=dataitem->getCaliRMS();
00165     int xtt=dataitem->getNumEvents();
00166     int statu=dataitem->getTaskStatus();
00167 
00168 
00169 
00170     xx[count]=x;
00171     yy[count]=y;
00172     tt[count]=xtt;
00173     st[count]=statu;
00174 
00175 
00176     ids_len[count]=sizeof(ids[count]);
00177     iov_len[count]=sizeof(iovid_vec[count]);
00178 
00179     x_len[count]=sizeof(xx[count]);
00180     y_len[count]=sizeof(yy[count]);
00181     tt_len[count]=sizeof(tt[count]);
00182     st_len[count]=sizeof(st[count]);
00183 
00184     count++;
00185   }
00186 
00187 
00188   try {
00189     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00190     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00191     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT , sizeof(xx[0]), x_len );
00192     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT , sizeof(yy[0]), y_len );
00193     m_writeStmt->setDataBuffer(5, (dvoid*)tt, OCCIINT , sizeof(tt[0]), tt_len );
00194     m_writeStmt->setDataBuffer(6, (dvoid*)st, OCCIINT , sizeof(st[0]), st_len );
00195 
00196 
00197     m_writeStmt->executeArrayUpdate(nrows);
00198 
00199     delete [] ids;
00200     delete [] iovid_vec;
00201     delete [] xx;
00202     delete [] yy;
00203     delete [] st;
00204     delete [] tt;
00205 
00206     delete [] ids_len;
00207     delete [] iov_len;
00208     delete [] x_len;
00209     delete [] y_len;
00210     delete [] tt_len;
00211     delete [] st_len;
00212 
00213 
00214 
00215   } catch (SQLException &e) {
00216     throw(std::runtime_error("MonPedestalsDat::writeArrayDB():  "+e.getMessage()));
00217   }
00218 }