CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/OnlineDB/EcalCondDB/interface/ITimingDat.h

Go to the documentation of this file.
00001 #ifndef ITIMINGDAT_H
00002 #define ITIMINGDAT_H
00003 
00004 #include <map>
00005 #include <stdexcept>
00006 #include <string>
00007 
00008 #include "OnlineDB/EcalCondDB/interface/IDataItem.h"
00009 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
00010 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
00011 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00012 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
00013 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00014 
00015 #include "OnlineDB/Oracle/interface/Oracle.h"
00016 
00017 class ITimingDat : public IDataItem {
00018  public:
00019   typedef oracle::occi::SQLException SQLException;
00020   typedef oracle::occi::ResultSet ResultSet;
00021   friend class EcalCondDBInterface;
00022  
00023 
00024 ITimingDat()
00025 {
00026   m_env = NULL;
00027   m_conn = NULL;
00028   m_writeStmt = NULL;
00029   m_readStmt = NULL;
00030 
00031   m_timingMean = 0;
00032   m_timingRMS = 0;
00033   m_taskStatus=0;
00034 };
00035 
00036 
00037 
00038  ~ITimingDat(){};
00039 
00040 
00041   // User data methods
00042   inline std::string getTable() { return m_table_name;}
00043   inline void setTable(std::string x) { m_table_name=x; }
00044 
00045   inline void setTimingMean(float mean) { m_timingMean = mean; }
00046   inline float getTimingMean() const { return m_timingMean; }
00047   
00048   inline void setTimingRMS(float rms) { m_timingRMS = rms; }
00049   inline float getTimingRMS() const { return m_timingRMS; }
00050 
00051   inline void setTaskStatus(bool status) { m_taskStatus = status; }
00052   inline bool getTaskStatus() const { return m_taskStatus; }
00053   
00054 
00055  private:
00056 void prepareWrite()
00057   throw(std::runtime_error)
00058 {
00059   this->checkConnection();
00060 
00061   try {
00062     m_writeStmt = m_conn->createStatement();
00063     m_writeStmt->setSQL("INSERT INTO " + getTable() + " (iov_id, logic_id, "
00064                         "timing_mean, timing_rms , task_status ) "
00065                         "VALUES (:iov_id, :logic_id, "
00066                         ":timing_mean, :timing_rms, :task_status )");
00067   } catch (SQLException &e) {
00068     throw(std::runtime_error("ITimingDat::prepareWrite():  "+e.getMessage()));
00069   }
00070 }
00071 
00072 
00073   template<class DATT, class IOVT>
00074   void writeDB(const EcalLogicID* ecid, const DATT* item, IOVT* iov)
00075     throw(std::runtime_error)
00076 {
00077   this->checkConnection();
00078   this->checkPrepare();
00079 
00080   int iovID = iov->fetchID();
00081   if (!iovID) { throw(std::runtime_error("ITimingDat::writeDB:  IOV not in DB")); }
00082 
00083   int logicID = ecid->getLogicID();
00084   if (!logicID) { throw(std::runtime_error("ITimingDat::writeDB:  Bad EcalLogicID")); }
00085   
00086   try {
00087     m_writeStmt->setInt(1, iovID);
00088     m_writeStmt->setInt(2, logicID);
00089 
00090     m_writeStmt->setFloat(3, item->getTimingMean() );
00091     m_writeStmt->setFloat(4, item->getTimingRMS() );
00092     m_writeStmt->setInt(5, item->getTaskStatus() );
00093 
00094 
00095     m_writeStmt->executeUpdate();
00096   } catch (SQLException &e) {
00097     throw(std::runtime_error("ITimingDat::writeDB():  "+e.getMessage()));
00098   }
00099 }
00100 
00101   template<class DATT, class IOVT>
00102     void writeArrayDB(const std::map< EcalLogicID, DATT >* data, IOVT* iov)
00103     throw(std::runtime_error)
00104 
00105 {
00106   using oracle::occi::OCCIINT;
00107   using oracle::occi::OCCIFLOAT;
00108 
00109   this->checkConnection();
00110   this->checkPrepare();
00111 
00112   int iovID = iov->fetchID();
00113   if (!iovID) { throw(std::runtime_error("ITimingDat::writeArrayDB:  IOV not in DB")); }
00114 
00115 
00116   int nrows=data->size(); 
00117   int* ids= new int[nrows];
00118   int* iovid_vec= new int[nrows];
00119   float* xx= new float[nrows];
00120   float* yy= new float[nrows];
00121   int* st= new int[nrows];
00122 
00123 
00124   ub2* ids_len= new ub2[nrows];
00125   ub2* iov_len= new ub2[nrows];
00126   ub2* x_len= new ub2[nrows];
00127   ub2* y_len= new ub2[nrows];
00128   ub2* st_len= new ub2[nrows];
00129 
00130 
00131   const EcalLogicID* channel;
00132   const DATT* dataitem;
00133   int count=0;
00134   //  typedef std::map< EcalLogicID, DATT >::const_iterator CI;
00135   typedef typename std::map< EcalLogicID, DATT >::const_iterator CI;
00136   for (CI p = data->begin(); p != data->end(); ++p) {
00137         channel = &(p->first);
00138         int logicID = channel->getLogicID();
00139         if (!logicID) { throw(std::runtime_error("ITimingDat::writeArrayDB:  Bad EcalLogicID")); }
00140         ids[count]=logicID;
00141         iovid_vec[count]=iovID;
00142 
00143         dataitem = &(p->second);
00144         
00145         float x=dataitem->getTimingMean();
00146         float y=dataitem->getTimingRMS();
00147         int statu=dataitem->getTaskStatus();
00148 
00149         xx[count]=x;
00150         yy[count]=y;
00151         st[count]=statu;
00152 
00153 
00154 
00155         ids_len[count]=sizeof(ids[count]);
00156         iov_len[count]=sizeof(iovid_vec[count]);
00157         
00158         x_len[count]=sizeof(xx[count]);
00159         y_len[count]=sizeof(yy[count]);
00160         st_len[count]=sizeof(st[count]);
00161 
00162         count++;
00163      }
00164 
00165 
00166   try {
00167     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00168     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00169     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT , sizeof(xx[0]), x_len );
00170     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT , sizeof(yy[0]), y_len );
00171     m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT , sizeof(st[0]), st_len );
00172  
00173     m_writeStmt->executeArrayUpdate(nrows);
00174 
00175     delete [] ids;
00176     delete [] iovid_vec;
00177     delete [] xx;
00178     delete [] yy;
00179     delete [] st;
00180 
00181     delete [] ids_len;
00182     delete [] iov_len;
00183     delete [] x_len;
00184     delete [] y_len;
00185     delete [] st_len;
00186 
00187   } catch (SQLException &e) {
00188     throw(std::runtime_error("ITimingDat::writeArrayDB():  "+e.getMessage()));
00189   }
00190 }
00191 
00192 
00193 
00194 
00195 
00196   template<class DATT, class IOVT>
00197   void fetchData(std::map< EcalLogicID, DATT >* fillMap, IOVT* iov)
00198   throw(std::runtime_error)
00199 {
00200   this->checkConnection();
00201   fillMap->clear();
00202 
00203   iov->setConnection(m_env, m_conn);
00204   int iovID = iov->fetchID();
00205   if (!iovID) { 
00206     //  throw(std::runtime_error("ITimingDat::writeDB:  IOV not in DB")); 
00207     return;
00208   }
00209 
00210   try {
00211 
00212     m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00213                  "d.timing_mean, d.timing_rms, d.task_status "
00214                  "FROM channelview cv JOIN "+ getTable() +"  d "
00215                  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00216                  "WHERE d.iov_id = :iov_id");
00217     m_readStmt->setInt(1, iovID);
00218     ResultSet* rset = m_readStmt->executeQuery();
00219     
00220     std::pair< EcalLogicID, DATT > p;
00221     DATT dat;
00222     while(rset->next()) {
00223       p.first = EcalLogicID( rset->getString(1),     // name
00224                              rset->getInt(2),        // logic_id
00225                              rset->getInt(3),        // id1
00226                              rset->getInt(4),        // id2
00227                              rset->getInt(5),        // id3
00228                              rset->getString(6));    // maps_to
00229 
00230       dat.setTimingMean( rset->getFloat(7) );
00231       dat.setTimingRMS( rset->getFloat(8) );
00232       dat.setTaskStatus( rset->getInt(9) );
00233 
00234 
00235       p.second = dat;
00236       fillMap->insert(p);
00237     }
00238 
00239 
00240   } catch (SQLException &e) {
00241     throw(std::runtime_error("ITimingDat::fetchData():  "+e.getMessage()));
00242   }
00243 }
00244 
00245 
00246 
00247 
00248 
00249 
00250   // User data
00251   float m_timingMean;
00252   float m_timingRMS;
00253   bool m_taskStatus;
00254   std::string m_table_name ; 
00255    
00256 };
00257 
00258 #endif
00259 
00260 
00261 
00262