CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/OnlineDB/EcalCondDB/src/RunDCSHVDat.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include <math.h>
00004 #include <list>
00005 #include <string>
00006 #include <map>
00007 
00008 #include "OnlineDB/EcalCondDB/interface/RunDCSHVDat.h"
00009 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00010 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00011 
00012 using namespace std;
00013 using namespace oracle::occi;
00014 
00015 RunDCSHVDat::RunDCSHVDat()
00016 {
00017   m_env = NULL;
00018   m_conn = NULL;
00019   m_writeStmt = NULL;
00020   m_readStmt = NULL;
00021 
00022   m_hv = 0;
00023   m_hvnom = 0;
00024   m_status= 0;
00025   m_tstatus= 0;
00026 }
00027 
00028 
00029 
00030 RunDCSHVDat::~RunDCSHVDat()
00031 {
00032 }
00033 
00034 
00035 
00036 void RunDCSHVDat::prepareWrite()
00037   throw(std::runtime_error)
00038 {
00039 
00040 
00041 }
00042 
00043 
00044 
00045 void RunDCSHVDat::writeDB(const EcalLogicID* ecid, const RunDCSHVDat* item, RunIOV* iov)
00046   throw(std::runtime_error)
00047 {
00048 }
00049 
00050 
00051 
00052 void RunDCSHVDat::fetchData(map< EcalLogicID, RunDCSHVDat >* fillMap, RunIOV* iov)
00053   throw(std::runtime_error)
00054 {
00055   fetchLastData(fillMap);
00056 
00057 }
00058 
00059 
00060 
00061 ResultSet *RunDCSHVDat::getBarrelRset(Tm timeStart) {
00062 
00063   DateHandler dh(m_env, m_conn);
00064 
00065   ResultSet* rset = NULL;
00066   string query="SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, " 
00067     " d.actual_vmon, h.nominal_value ,  d.change_date " 
00068     " FROM "+ getEBAccount()+".FWCAENCHANNEL d " 
00069     " JOIN "+ getEBAccount()+".HV_MAPPING h on h.DPID = d.DPID " 
00070     " join "+ getEBAccount()+".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
00071     " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date " ;
00072   try {
00073     m_readStmt->setSQL(query);
00074 
00075     m_readStmt->setDate(1, dh.tmToDate(timeStart));
00076 
00077     rset = m_readStmt->executeQuery();
00078   }
00079   catch (SQLException e) {
00080     throw(std::runtime_error("RunDCSHVDat::getBarrelRset():  " + e.getMessage() + " " + query));
00081   }
00082   return rset;
00083 }
00084 
00085 ResultSet *RunDCSHVDat::getEndcapAnodeRset(Tm timeStart) {
00086 
00087   DateHandler dh(m_env, m_conn);
00088 
00089   ResultSet* rset = NULL;
00090   string query="SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, " 
00091     " d.actual_vmon, '800' nominal_value ,  d.change_date " 
00092     " FROM "+ getEEAccount()+".FWCAENCHANNEL d " 
00093     " JOIN "+ getEEAccount()+".EE_HVA_MAPPING h on h.DPID = d.DPID " 
00094     " join "+ getEEAccount()+".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
00095     " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date " ;
00096   try {
00097     m_readStmt->setSQL(query);
00098 
00099     m_readStmt->setDate(1, dh.tmToDate(timeStart));
00100 
00101     rset = m_readStmt->executeQuery();
00102   }
00103   catch (SQLException e) {
00104     throw(std::runtime_error("RunDCSHVDat::getBarrelRset():  " + e.getMessage() + " " + query));
00105   }
00106   return rset;
00107 }
00108 
00109 ResultSet *RunDCSHVDat::getEndcapDynodeRset(Tm timeStart) {
00110 
00111   DateHandler dh(m_env, m_conn);
00112 
00113   ResultSet* rset = NULL;
00114   string query="SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, " 
00115     " d.actual_vmon, '600' nominal_value ,  d.change_date " 
00116     " FROM "+ getEEAccount()+".FWCAENCHANNEL d " 
00117     " JOIN "+ getEEAccount()+".EE_HVD_MAPPING h on h.DPID = d.DPID " 
00118     " join "+ getEEAccount()+".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
00119     " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date " ;
00120   try {
00121     m_readStmt->setSQL(query);
00122 
00123     m_readStmt->setDate(1, dh.tmToDate(timeStart));
00124 
00125     rset = m_readStmt->executeQuery();
00126   }
00127   catch (SQLException e) {
00128     throw(std::runtime_error("RunDCSHVDat::getBarrelRset():  " + e.getMessage() + " " + query));
00129   }
00130   return rset;
00131 }
00132 
00133 
00134 /*
00135 ResultSet *RunDCSHVDat::getBarrelRset() {
00136   ResultSet* rset = NULL;
00137   string query = "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00138     " d.value, h.nominal_value , d.since "
00139     "FROM "+ getEBAccount()+".DCSLASTVALUE_VOLTAGE_VMON d "
00140     " JOIN "+ getEBAccount()+".HV_MAPPING h on "
00141     " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name"; 
00142   try {
00143     m_readStmt->setSQL(query);
00144     rset = m_readStmt->executeQuery();
00145   }
00146   catch (SQLException e) {
00147     throw(std::runtime_error("RunDCSHVDat::getBarrelRset():  " + e.getMessage() + " " + query));
00148   }
00149   return rset;
00150 }
00151 */
00152 
00153 ResultSet *RunDCSHVDat::getBarrelRset() {
00154   ResultSet* rset = NULL;
00155   string query = "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00156     " d.VALUE_NUMBER, h.nominal_value , d.CHANGE_DATE "
00157     "FROM "+ getEBAccount()+".FWCAENCHANNEL_LV d "
00158     " JOIN "+ getEBAccount()+".HV_MAPPING h on "
00159     " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and DPE_NAME='ACTUAL_VMON' "; 
00160   try {
00161     m_readStmt->setSQL(query);
00162     rset = m_readStmt->executeQuery();
00163   }
00164   catch (SQLException e) {
00165     throw(std::runtime_error("RunDCSHVDat::getBarrelRset():  " + e.getMessage() + " " + query));
00166   }
00167   return rset;
00168 }
00169 
00170 ResultSet *RunDCSHVDat::getEndcapAnodeRset() {
00171   ResultSet* rset = NULL;
00172   string query = "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00173     " d.value_number, '800' NOMINAL_VALUE , d.CHANGE_DATE "
00174     "FROM "+ getEEAccount()+".FWCAENCHANNEL_LV d "
00175     " JOIN "+ getEEAccount()+".EE_HVA_MAPPING h on "
00176     " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and dpe_name='ACTUAL_VMON' "; 
00177   try {
00178     m_readStmt->setSQL(query);
00179     rset = m_readStmt->executeQuery();
00180   }
00181   catch (SQLException e) {
00182     throw(std::runtime_error("RunDCSHVDat::getEndcapAnodeRset():  " + e.getMessage() + " " + query));
00183   }
00184   return rset;
00185 }
00186 
00187 ResultSet *RunDCSHVDat::getEndcapDynodeRset() {
00188   ResultSet* rset = NULL;
00189   string query = "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00190     " d.value_number, '600' NOMINAL_VALUE , d.CHANGE_DATE "
00191     "FROM "+ getEEAccount()+".FWCAENCHANNEL_LV d "
00192     " JOIN "+ getEEAccount()+".EE_HVD_MAPPING h on "
00193     " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and dpe_name='ACTUAL_VMON' ";
00194   try {
00195     m_readStmt->setSQL(query);
00196     rset = m_readStmt->executeQuery();
00197   } 
00198   catch (SQLException e) {
00199     throw(std::runtime_error("RunDCSHVDat::getEndcapDynodeRset():  " + e.getMessage() + " " + query));
00200   }
00201   return rset;
00202 }
00203 
00204 void RunDCSHVDat::fillTheMap(ResultSet *rset, 
00205                                map< EcalLogicID, RunDCSHVDat >* fillMap) {
00206 
00207   // method for last value queries 
00208 
00209   std::pair< EcalLogicID, RunDCSHVDat > p;
00210   RunDCSHVDat dat;
00211   DateHandler dh(m_env, m_conn);
00212 
00213   try {
00214     while(rset->next()) {
00215       p.first = EcalLogicID( rset->getString(1),     // name
00216                              rset->getInt(2),        // logic_id
00217                              rset->getInt(3),        // id1
00218                              rset->getInt(4),        // id2
00219                              rset->getInt(5),        // id3
00220                              rset->getString(6));    // maps_to
00221       
00222       dat.setHV(        rset->getFloat(7) );
00223       dat.setHVNominal( rset->getFloat(8) );
00224       Date sinceDate = rset->getDate(9);
00225       Tm  sinceTm = dh.dateToTm( sinceDate );
00226       dat.setStatus(0);
00227       if (p.first.getName() == "EB_HV_channel") {
00228         setStatusForBarrel(dat, sinceTm);      
00229       } else {
00230         setStatusForEndcaps(dat, sinceTm);      
00231       }
00232       p.second = dat;
00233       fillMap->insert(p);
00234     } 
00235   }
00236   catch (SQLException &e) {
00237     throw(std::runtime_error("RunDCSHVDat::fetchData():  "+e.getMessage()));
00238   }
00239 }
00240 
00241 
00242 
00243 
00244 void RunDCSHVDat::fillTheMapByTime(ResultSet *rset,
00245                                    std::list<  DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat>  >* my_data_list ) {
00246  
00247   //                           std::list< std::pair< Tm, std::map< EcalLogicID, RunDCSHVDat > > >* fillMap) {
00248 
00249   // method for historic queries
00250 
00251   RunDCSHVDat dat;
00252   DateHandler dh(m_env, m_conn);
00253 
00254 
00255   try {
00256     int count=-1;
00257     while(rset->next()) {
00258       EcalLogicID ec = EcalLogicID( rset->getString(1),     // name
00259                              rset->getInt(2),        // logic_id
00260                              rset->getInt(3),        // id1
00261                              rset->getInt(4),        // id2
00262                              rset->getInt(5),        // id3
00263                              rset->getString(6));    // maps_to
00264       
00265 
00266       dat.setHV(        rset->getFloat(7) );
00267       dat.setHVNominal( rset->getFloat(8) );
00268       //      Date sinceDate = rset->getDate(9);
00269       //     Tm  sinceTm = dh.dateToTm( sinceDate );
00270       // Date sinceDate = rset->getDate(9);
00271       Timestamp  ora_timestamp = rset->getTimestamp(9);
00272       Tm sinceTm; // YYYY-MM-DD HH:MM:SS
00273       sinceTm.setToString(ora_timestamp.toText("yyyy-mm-dd hh24:mi:ss",0));
00274 
00275       dat.setStatus(0);
00276       if (ec.getName() == "EB_HV_channel") {
00277         setStatusForBarrel(dat, sinceTm);      
00278       } else {
00279         setStatusForEndcaps(dat, sinceTm);      
00280       }
00281 
00282       std::pair< EcalLogicID, RunDCSHVDat > d;
00283       d.first=ec;
00284       d.second=dat;
00285       std::pair< Tm, std::pair< EcalLogicID, RunDCSHVDat > > p;
00286       p.first=sinceTm; 
00287       p.second = d;
00288 
00289       DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat> pp;
00290       pp.m_iData=p;
00291 
00292       my_data_list->push_back(pp);
00293       count++;
00294       if(count<100) std::cout<<"DCS DB : size:"<< my_data_list->size()<<" Tm " <<sinceTm.str()<<" "<<ec.getID1()<<" "<<ec.getID2()<<" "<<dat.getHV()<<std::endl;
00295     }
00296       std::cout<<"DCS DB : size:"<< my_data_list->size()<<std::endl;
00297 
00298 
00299   }
00300   catch (SQLException &e) {
00301     throw(std::runtime_error("RunDCSHVDat::fetchData():  "+e.getMessage()));
00302   }
00303 }
00304 
00305 int  RunDCSHVDat::nowMicroseconds() {
00306   Tm t_now_gmt;
00307   
00308   t_now_gmt.setToCurrentGMTime();
00309   int t_now_gmt_micros = t_now_gmt.microsTime();
00310   return t_now_gmt_micros;
00311 }
00312 
00313 
00314 
00315 void RunDCSHVDat::setStatusForBarrel(RunDCSHVDat &dat, Tm sinceTm) {
00316   int t_now_gmt_micros = nowMicroseconds();
00317 
00318   float hv_diff=dat.getHV() - dat.getHVNominal();
00319   if(hv_diff<0) hv_diff=-hv_diff; 
00320   if (hv_diff*1000 > maxHVDifferenceEB) {
00321     dat.setStatus(HVNOTNOMINAL);
00322   }
00323   if (dat.getHV()*1000 < minHV) {
00324     dat.setStatus(HVOFF);
00325   }
00326 
00327   int result=0;
00328   long long d= (t_now_gmt_micros - sinceTm.microsTime()) ;
00329   if (d> maxDifference) {
00330     result= -d/1000000 ;
00331   } 
00332   dat.setTimeStatus(result);
00333 
00334 
00335 }
00336 
00337 void  RunDCSHVDat::setStatusForEndcaps(RunDCSHVDat &dat, Tm sinceTm) {
00338   int t_now_gmt_micros = nowMicroseconds();
00339 
00340   if (fabs(dat.getHV() - dat.getHVNominal())*1000 > maxHVDifferenceEE) {
00341     dat.setStatus(HVNOTNOMINAL);
00342   }
00343   if (dat.getHV()*1000 < minHV) {
00344     dat.setStatus(HVOFF);
00345   }
00346 
00347   int result=0;
00348   long long d= (t_now_gmt_micros - sinceTm.microsTime()) ;
00349   if (d> maxDifference) {
00350     result= -d/1000000 ;
00351   } 
00352   dat.setTimeStatus(result);
00353 }
00354 
00355 void RunDCSHVDat::fetchLastData(map< EcalLogicID, RunDCSHVDat >* fillMap )
00356   throw(std::runtime_error)
00357 {
00358   this->checkConnection();
00359 
00360   fillMap->clear();
00361 
00362   try {
00363     std::pair< EcalLogicID, RunDCSHVDat > p;
00364     RunDCSHVDat dat;
00365 
00366     ResultSet* rset = getBarrelRset();
00367     
00368     fillTheMap(rset, fillMap);
00369     rset = getEndcapAnodeRset();
00370     
00371     fillTheMap(rset, fillMap);
00372     rset = getEndcapDynodeRset();
00373     
00374     fillTheMap(rset, fillMap);
00375   } 
00376   catch (SQLException &e) {
00377     throw(std::runtime_error("RunDCSHVDat::fetchData():  "+e.getMessage()));
00378   }
00379 }
00380 
00381 void RunDCSHVDat::fetchHistoricalData(std::list< std::pair<Tm, std::map< EcalLogicID, RunDCSHVDat > > >* fillMap, Tm timeStart  )
00382   throw(std::runtime_error)
00383 {
00384   this->checkConnection();
00385 
00386   fillMap->clear();
00387 
00388   std::list<  DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat>  > my_data_list;
00389 
00390 
00391   try {
00392 
00393     std::pair< EcalLogicID, RunDCSHVDat > p;
00394     RunDCSHVDat dat;
00395 
00396     ResultSet* rset1 = getBarrelRset(timeStart);
00397     fillTheMapByTime(rset1, &my_data_list);
00398 
00399     ResultSet* rset2 = getEndcapAnodeRset(timeStart);
00400     fillTheMapByTime(rset2, &my_data_list);
00401 
00402     ResultSet* rset3 = getEndcapDynodeRset(timeStart);
00403     fillTheMapByTime(rset3, &my_data_list);  
00404 
00405 
00406     DataReducer<RunDCSHVDat> my_dr;
00407     my_dr.setDataList(my_data_list);
00408     my_dr.getReducedDataList(fillMap);
00409 
00410 
00411   } 
00412   catch (SQLException &e) {
00413     throw(std::runtime_error("RunDCSHVDat::fetchData():  "+e.getMessage()));
00414   }
00415 }