CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondTools/RunInfo/src/RunInfoRead.cc

Go to the documentation of this file.
00001 //#include "CondFormats/Common/interface/TimeConversions.h"
00002 //#include "CondFormats/Common/interface/Time.h"
00003 #include "CondTools/RunInfo/interface/RunInfoRead.h"
00004 #include "RelationalAccess/ISession.h"
00005 #include "RelationalAccess/ITransaction.h"
00006 #include "RelationalAccess/ISchema.h"
00007 #include "RelationalAccess/ITable.h"
00008 #include "RelationalAccess/ITableDataEditor.h"
00009 #include "RelationalAccess/TableDescription.h"
00010 #include "RelationalAccess/IQuery.h"
00011 #include "RelationalAccess/ICursor.h"
00012 #include "CoralBase/AttributeList.h"
00013 #include "CoralBase/Attribute.h"
00014 #include "CoralBase/AttributeSpecification.h"
00015 #include "CoralBase/TimeStamp.h"
00016 #include <algorithm>
00017 #include <iostream>
00018 #include <iterator>
00019 #include <stdexcept>
00020 #include <vector>
00021 #include <math.h>
00022 
00023 RunInfoRead::RunInfoRead(const std::string& connectionString,
00024                          const std::string& user,
00025                          const std::string& pass):
00026   TestBase(),
00027   m_connectionString( connectionString ),
00028   m_user( user ),
00029   m_pass( pass ) {
00030   m_tableToRead="";
00031   m_columnToRead="";
00032 }
00033 
00034 RunInfoRead::~RunInfoRead() {}
00035 
00036 void RunInfoRead::run() {}
00037 
00038 RunInfo 
00039 RunInfoRead::readData(const std::string & table, 
00040                       const std::string &column, const int r_number) {
00041   m_tableToRead = table; // to be cms_runinfo.runsession_parameter
00042   m_columnToRead= column;  // to be string_value;
00043   RunInfo  sum;
00044   RunInfo temp_sum;
00045   //RunInfo Sum; 
00046   //for B currents...
00047   bool Bnotchanged = 0;
00048   //from TimeConversions.h
00049   const boost::posix_time::ptime time0 =
00050     boost::posix_time::from_time_t(0);
00051   //if cursor is null setting null values  
00052   temp_sum.m_run = r_number;
00053   std::cout << "entering readData" << std::endl;
00054   coral::ISession* session = this->connect( m_connectionString,
00055                                             m_user, m_pass );
00056   try{
00057     session->transaction().start();
00058     std::cout << "starting session " << std::endl;
00059     coral::ISchema& schema = session->schema("CMS_RUNINFO");
00060     std::cout << " accessing schema " << std::endl;
00061     std::cout << " trying to handle table ::  " << m_tableToRead << std::endl;
00062     std::string m_columnToRead_id = "ID";
00063     long long id_start = 0;
00064     //new query to obtain the start_time, fist obtaining the id
00065     coral::IQuery* queryI = schema.tableHandle(m_tableToRead).newQuery();  
00066     //implementing the query here....... 
00067     queryI->addToOutputList(m_tableToRead + "." + m_columnToRead_id, m_columnToRead_id);
00068     //condition 
00069     coral::AttributeList conditionData;
00070     conditionData.extend<int>( "n_run" );
00071     conditionData[0].data<int>() = r_number;
00072     std::string condition1 = m_tableToRead + ".runnumber=:n_run AND " +  m_tableToRead +  ".name='CMS.LVL0:START_TIME_T'";
00073     queryI->setCondition(condition1, conditionData);
00074     coral::ICursor& cursorI = queryI->execute();
00075     if( cursorI.next() ) {
00076       //cursorI.currentRow().toOutputStream(std::cout) << std::endl;
00077       const coral::AttributeList& row = cursorI.currentRow();
00078       id_start = row[m_columnToRead_id].data<long long>();
00079     }
00080     else {
00081       id_start = -1;
00082     }
00083     //std::cout << "id for start time time extracted == " << id_start << std::endl;
00084     delete queryI;
00085     
00086     //now extracting the start time
00087     std::string m_tableToRead_date = "RUNSESSION_DATE";
00088     std::string m_columnToRead_val = "VALUE";
00089     //new query to obtain the start_time, fist obtaining the id
00090     coral::IQuery* queryII = schema.tableHandle(m_tableToRead_date).newQuery();  
00091     //implementing the query here....... 
00092     queryII->addToOutputList(m_tableToRead_date + "." + m_columnToRead_val, m_columnToRead_val);
00093     //condition 
00094     coral::AttributeList conditionData2;
00095     conditionData2.extend<long long>( "n_id" );
00096     conditionData2[0].data<long long>() = id_start;
00097     std::string condition2 = m_tableToRead_date + ".runsession_parameter_id=:n_id";
00098     queryII->setCondition(condition2, conditionData2);
00099     coral::ICursor& cursorII = queryII->execute();
00100     coral::TimeStamp start; //now all times are UTC!
00101     if( cursorII.next() ) {
00102       //cursorII.currentRow().toOutputStream(std::cout) << std::endl;
00103       const coral::AttributeList& row = cursorII.currentRow();
00104       start = row[m_columnToRead_val].data<coral::TimeStamp>();
00105       /*
00106       std::cout << "start time extracted == " 
00107                 << "-->year " << start.year()
00108                 << "-- month " << start.month()
00109                 << "-- day " << start.day()
00110                 << "-- hour " << start.hour()
00111                 << "-- minute " << start.minute()
00112                 << "-- second " << start.second()
00113                 << "-- nanosecond " << start.nanosecond() 
00114                 << std::endl;
00115       */
00116       boost::posix_time::ptime start_ptime = start.time();
00117       std::cout << "Posix time for run start: "<< start_ptime << std::endl;
00118       boost::posix_time::time_duration startTimeFromEpoch = start_ptime - time0;
00119       temp_sum.m_start_time_str = boost::posix_time::to_iso_extended_string(start_ptime);
00120       temp_sum.m_start_time_ll = startTimeFromEpoch.total_microseconds();
00121       std::cout << "start time string extracted == " << temp_sum.m_start_time_str << std::endl; 
00122       std::cout << "microsecond since Epoch (UTC) : " << temp_sum.m_start_time_ll << std::endl;    
00123     }
00124     else {
00125         temp_sum.m_start_time_str = "null";
00126         temp_sum.m_start_time_ll = -1;
00127     }
00128     delete queryII;
00129     
00130     //new query to obtain the stop_time, fist obtaining the id
00131     coral::IQuery* queryIII = schema.tableHandle(m_tableToRead).newQuery();  
00132     //implementing the query here....... 
00133     queryIII->addToOutputList(m_tableToRead + "." + m_columnToRead_id, m_columnToRead_id);
00134     //condition 
00135     std::string condition3 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:STOP_TIME_T'";
00136     queryIII->setCondition(condition3, conditionData);
00137     coral::ICursor& cursorIII = queryIII->execute();
00138     long long id_stop = 0;
00139     if( cursorIII.next() ) {
00140       //cursorIII.currentRow().toOutputStream(std::cout) << std::endl;
00141       const coral::AttributeList& row = cursorIII.currentRow();
00142       id_stop = row[m_columnToRead_id].data<long long>();  
00143     }
00144     else {
00145       id_stop = -1;
00146     }
00147     //std::cout << "id for stop time time extracted == " << id_stop << std::endl;
00148     delete queryIII;
00149     
00150     //now exctracting the stop time
00151     coral::IQuery* queryIV = schema.tableHandle(m_tableToRead_date).newQuery(); 
00152     //implementing the query here....... 
00153     queryIV->addToOutputList(m_tableToRead_date + "." + m_columnToRead_val, m_columnToRead_val);
00154     //condition
00155     coral::AttributeList conditionData4;
00156     conditionData4.extend<long long>( "n_id" );
00157     conditionData4[0].data<long long>() = id_stop;
00158     std::string condition4 = m_tableToRead_date + ".runsession_parameter_id=:n_id";
00159     queryIV->setCondition(condition4, conditionData4);
00160     coral::ICursor& cursorIV = queryIV->execute();
00161     coral::TimeStamp stop;
00162     if( cursorIV.next() ) {
00163       //cursorIV.currentRow().toOutputStream(std::cout) << std::endl;
00164       const coral::AttributeList& row = cursorIV.currentRow();
00165       stop = row[m_columnToRead_val].data<coral::TimeStamp>(); 
00166       /*
00167       std::cout << "stop time extracted == " 
00168                 << "-->year " << stop.year()
00169                 << "-- month " << stop.month()
00170                 << "-- day " << stop.day()
00171                 << "-- hour " << stop.hour()
00172                 << "-- minute " << stop.minute()
00173                 << "-- second " << stop.second()
00174                 << "-- nanosecond " << stop.nanosecond() 
00175                 << std::endl;
00176       */
00177       boost::posix_time::ptime stop_ptime = stop.time();
00178       std::cout << "Posix time for run stop: "<< stop_ptime << std::endl;
00179       boost::posix_time::time_duration stopTimeFromEpoch = stop_ptime - time0;
00180       temp_sum.m_stop_time_str = boost::posix_time::to_iso_extended_string(stop_ptime);
00181       temp_sum.m_stop_time_ll = stopTimeFromEpoch.total_microseconds();
00182       std::cout << "stop time string extracted == " << temp_sum.m_stop_time_str << std::endl; 
00183       std::cout << "microsecond since Epoch (UTC) : " << temp_sum.m_stop_time_ll << std::endl;
00184     }
00185     else {
00186       temp_sum.m_stop_time_str = "null";
00187       temp_sum.m_stop_time_ll = -1;
00188     }
00189     delete queryIV;
00190     
00191     std::string m_tableToRead_fed = "RUNSESSION_STRING";
00192     coral::IQuery* queryV = schema.newQuery();  
00193     queryV->addToTableList(m_tableToRead);
00194     queryV->addToTableList(m_tableToRead_fed);
00195     queryV->addToOutputList(m_tableToRead_fed + "." + m_columnToRead_val, m_columnToRead_val);
00196     //queryV->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
00197     //condition
00198     std::string condition5 = m_tableToRead + ".RUNNUMBER=:n_run AND " + m_tableToRead + ".NAME='CMS.LVL0:FED_ENABLE_MASK' AND RUNSESSION_PARAMETER.ID = RUNSESSION_STRING.RUNSESSION_PARAMETER_ID";
00199     //std::string condition5 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:FED_ENABLE_MASK'";
00200     queryV->setCondition(condition5, conditionData);
00201     coral::ICursor& cursorV = queryV->execute();
00202     std::string fed;
00203     if ( cursorV.next() ) {
00204       //cursorV.currentRow().toOutputStream(std::cout) << std::endl;
00205       const coral::AttributeList& row = cursorV.currentRow();
00206       fed = row[m_columnToRead_val].data<std::string>();
00207     }
00208     else {
00209       fed="null";
00210     }
00211     //std::cout << "string fed emask == " << fed << std::endl;
00212     delete queryV;
00213     
00214     std::replace(fed.begin(), fed.end(), '%', ' ');
00215     std::stringstream stream(fed);
00216     for(;;) {
00217       std::string word; 
00218       if ( !(stream >> word) ){break;}
00219       std::replace(word.begin(), word.end(), '&', ' ');
00220       std::stringstream ss(word);
00221       int fedNumber; 
00222       int val;
00223       ss >> fedNumber >> val;
00224       //std::cout << "fed:: " << fed << "--> val:: " << val << std::endl; 
00225       //val bit 0 represents the status of the SLINK, but 5 and 7 means the SLINK/TTS is ON but NA or BROKEN (see mail of alex....)
00226       if( (val & 0001) == 1 && (val != 5) && (val != 7) ) 
00227         temp_sum.m_fed_in.push_back(fedNumber);
00228     } 
00229     std::cout << "feds in run:--> ";
00230     std::copy(temp_sum.m_fed_in.begin(), temp_sum.m_fed_in.end(), std::ostream_iterator<int>(std::cout, ", "));
00231     std::cout << std::endl;
00232     /*
00233     for (size_t i =0; i<temp_sum.m_fed_in.size() ; ++i){
00234       std::cout << "fed in run:--> " << temp_sum.m_fed_in[i] << std::endl; 
00235     } 
00236     */
00237     
00238     coral::ISchema& schema2 = session->schema("CMS_DCS_ENV_PVSS_COND");
00239     std::string m_tableToRead_cur= "CMSFWMAGNET";
00240     std::string m_columnToRead_cur= "CURRENT";
00241     std::string m_columnToRead_date= "CHANGE_DATE";
00242     coral::IQuery* queryVI = schema2.tableHandle(m_tableToRead_cur).newQuery();
00243     queryVI->addToOutputList(m_tableToRead_cur +  "." + m_columnToRead_cur, m_columnToRead_cur);
00244     queryVI->addToOutputList(m_tableToRead_cur +  "." + m_columnToRead_date, m_columnToRead_date);
00245     //condition 
00246     coral::AttributeList conditionData6;
00247     float last_current = -1;
00248     if(temp_sum.m_stop_time_str != "null") { 
00249       conditionData6.extend<coral::TimeStamp>( "runstart_time" );
00250       conditionData6.extend<coral::TimeStamp>( "runstop_time" );
00251       conditionData6["runstart_time"].data<coral::TimeStamp>() = start; //start_time ;
00252       conditionData6["runstop_time"].data<coral::TimeStamp>() = stop; //stop_time ;
00253       std::string conditionVI = " NOT " + m_tableToRead_cur + "." + m_columnToRead_cur + " IS NULL AND " 
00254         + m_tableToRead_cur +  "." + m_columnToRead_date + ">:runstart_time AND " 
00255         + m_tableToRead_cur +  "." + m_columnToRead_date + "<:runstop_time"  /*" ORDER BY " + m_columnToRead_date + " DESC"*/;
00256       queryVI->setCondition(conditionVI, conditionData6);
00257       queryVI->addToOrderList(m_tableToRead_cur +  "." + m_columnToRead_date + " DESC");
00258     } else {
00259       std::cout << "run stop null" << std::endl;
00260       conditionData6.extend<coral::TimeStamp>( "runstart_time" );
00261       conditionData6["runstart_time"].data<coral::TimeStamp>() = start; //start_time ;
00262       std::string conditionVI = " NOT " + m_tableToRead_cur + "." + m_columnToRead_cur + " IS NULL AND " 
00263         + m_tableToRead_cur +  "." + m_columnToRead_date + "<:runstart_time" /*" ORDER BY " + m_columnToRead_date + " DESC"*/;
00264       queryVI->setCondition(conditionVI, conditionData6);
00265       queryVI->addToOrderList(m_tableToRead_cur +  "." + m_columnToRead_date + " DESC");
00266     }
00267     queryVI->limitReturnedRows(10000);
00268     coral::ICursor& cursorVI = queryVI->execute();
00269     coral::TimeStamp lastCurrentDate;
00270     std::string last_date;
00271     std::vector<double> time_curr;
00272     if ( !cursorVI.next() ) {
00273       // we should deal with stable currents... so the query is returning no value and we should take the last modified current value...
00274       Bnotchanged = 1;
00275       coral::AttributeList conditionData6bis;
00276       conditionData6bis.extend<coral::TimeStamp>( "runstop_time" );
00277       conditionData6bis["runstop_time"].data<coral::TimeStamp>() = stop; //stop_time ;
00278       std::string conditionVIbis = " NOT " + m_tableToRead_cur + "." + m_columnToRead_cur + " IS NULL AND " 
00279         + m_tableToRead_cur +  "." + m_columnToRead_date + " <:runstop_time" /*" ORDER BY " + m_columnToRead_date + " DESC"*/;
00280       coral::IQuery* queryVIbis = schema2.tableHandle(m_tableToRead_cur).newQuery();
00281       queryVIbis->addToOutputList(m_tableToRead_cur + "." +  m_columnToRead_cur, m_columnToRead_cur);
00282       queryVIbis->setCondition(conditionVIbis, conditionData6bis);
00283       queryVIbis->addToOrderList(m_tableToRead_cur +  "." + m_columnToRead_date + " DESC");
00284       coral::ICursor& cursorVIbis= queryVIbis->execute();
00285       if( cursorVIbis.next() ) {
00286         //cursorVIbis.currentRow().toOutputStream(std::cout) << std::endl;
00287         const coral::AttributeList& row = cursorVIbis.currentRow();
00288         last_current = row[m_columnToRead_cur].data<float>();
00289         std::cout << "previos run(s) current, not changed in this run... " << last_current << std::endl;
00290       } 
00291       temp_sum.m_avg_current = last_current;
00292       temp_sum.m_min_current = last_current;
00293       temp_sum.m_max_current = last_current;
00294       temp_sum.m_stop_current = last_current;
00295       temp_sum.m_start_current = last_current; 
00296     }
00297     while( cursorVI.next() ) {
00298       //cursorVI.currentRow().toOutputStream(std::cout) << std::endl;
00299       const coral::AttributeList& row = cursorVI.currentRow();
00300       lastCurrentDate = row[m_columnToRead_date].data<coral::TimeStamp>();
00301       temp_sum.m_current.push_back( row[m_columnToRead_cur].data<float>() );
00302       if(temp_sum.m_stop_time_str == "null") break;
00303       /*
00304       std::cout << "  last current time extracted == " 
00305                 << "-->year " << lastCurrentDate.year()
00306                 << "-- month " << lastCurrentDate.month()
00307                 << "-- day " << lastCurrentDate.day()
00308                 << "-- hour " << lastCurrentDate.hour() 
00309                 << "-- minute " << lastCurrentDate.minute() 
00310                 << "-- second " << lastCurrentDate.second()
00311                 << "-- nanosecond " << lastCurrentDate.nanosecond()
00312                 << std::endl;
00313       */
00314       boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
00315       std::cout << "Posix time for last current time: " << lastCurrentDate_ptime << std::endl;
00316       boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime - time0;
00317       last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
00318       std::cout << "last current time extracted  == " << last_date << std::endl;
00319       long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
00320       time_curr.push_back(last_date_ll);
00321     }
00322     delete queryVI;
00323     
00324     size_t csize = temp_sum.m_current.size();
00325     std::cout << "size of currents  " << csize << std::endl;  
00326     size_t tsize = time_curr.size(); 
00327     std::cout << "size of time " << tsize << std::endl;
00328     if(csize != tsize) { 
00329       std::cout<< "current and time not filled correctly" << std::endl;
00330     }
00331     if(tsize > 1) { 
00332       temp_sum.m_run_intervall_micros = time_curr.front() - time_curr.back();
00333     } else { 
00334       temp_sum.m_run_intervall_micros = 0;
00335     }
00336     std::cout << "change current during run interval in microseconds " << temp_sum.m_run_intervall_micros << std::endl;
00337     
00338     double wi = 0;
00339     //std::vector<double> v_wi;
00340     double sumwixi = 0;
00341     double sumwi = 0;
00342     float min = -1;
00343     float max = -1;
00344     
00345     if(csize != 0) {
00346       min = temp_sum.m_current.front();
00347       max = temp_sum.m_current.front();
00348       for(size_t i = 0; i < csize; ++i) {
00349         std::cout << "--> " << temp_sum.m_current[i] << std::endl;
00350         if( (tsize > 1) && ( i < csize - 1 ) ) { 
00351           wi = (time_curr[i] - time_curr[i+1])  ;
00352           temp_sum.m_times_of_currents.push_back(wi);
00353           //v_wi.push_back(wi);
00354           sumwixi += wi * temp_sum.m_current[i] ;
00355           sumwi += wi;
00356         }  
00357         min = std::min(min, temp_sum.m_current[i]);
00358         max = std::max(max, temp_sum.m_current[i]);
00359       }
00360       //for (size_t i = 0; i < v_wi.size(); ++i) {
00361       for (size_t i = 0; i < temp_sum.m_times_of_currents.size(); ++i){
00362         std::cout << "wi " << temp_sum.m_times_of_currents[i] << std::endl;
00363       }
00364       temp_sum.m_start_current = temp_sum.m_current.back(); //temp_sum.m_current[csize - 1];
00365       std::cout << "--> " << "start cur " << temp_sum.m_start_current << std::endl;
00366       temp_sum.m_stop_current = temp_sum.m_current.front(); //temp_sum.m_current[0];
00367       std::cout<< "--> " << "stop cur " << temp_sum.m_stop_current << std::endl;
00368       if (tsize>1) {
00369         temp_sum.m_avg_current=sumwixi/sumwi;
00370       } else { 
00371         temp_sum.m_avg_current= temp_sum.m_start_current; 
00372       }
00373       std::cout<< "--> " << "avg cur  " << temp_sum.m_avg_current << std::endl;
00374       temp_sum.m_max_current= max;
00375       std::cout<< "--> " << "max cur  " << temp_sum.m_max_current << std::endl;
00376       temp_sum.m_min_current= min;
00377       std::cout<< "--> " << "min cur  " << temp_sum.m_min_current << std::endl;
00378     } else {
00379       if (!Bnotchanged) {
00380         temp_sum.m_avg_current = -1;
00381         temp_sum.m_min_current = -1;
00382         temp_sum.m_max_current = -1;
00383         temp_sum.m_stop_current = -1;
00384         temp_sum.m_start_current = -1;
00385       }
00386     }
00387     
00388     std::cout << "temp_sum.m_avg_current " << temp_sum.m_avg_current << std::endl;
00389     std::cout << "temp_sum.m_min_current " << temp_sum.m_min_current << std::endl;
00390     std::cout << "temp_sum.m_max_current " << temp_sum.m_max_current << std::endl;
00391     std::cout << "temp_sum.m_stop_current " << temp_sum.m_stop_current << std::endl;
00392     std::cout << "temp_sum.m_start_current " << temp_sum.m_start_current << std::endl;
00393     
00394     session->transaction().commit();
00395   }
00396   catch (const std::exception& e) { 
00397     std::cout << "Exception: " << e.what() << std::endl;
00398   }
00399   delete session;
00400   
00401   sum= temp_sum;
00402   return sum;
00403 }