00001 #include "CondTools/RunInfo/interface/RunSummaryRead.h"
00002
00003
00004
00005 #include "RelationalAccess/ISession.h"
00006 #include "RelationalAccess/ITransaction.h"
00007 #include "RelationalAccess/ISchema.h"
00008 #include "RelationalAccess/ITable.h"
00009 #include "RelationalAccess/ITableDataEditor.h"
00010 #include "RelationalAccess/TableDescription.h"
00011 #include "RelationalAccess/IQuery.h"
00012 #include "RelationalAccess/ICursor.h"
00013 #include "CoralBase/AttributeList.h"
00014 #include "CoralBase/Attribute.h"
00015 #include "CoralBase/AttributeSpecification.h"
00016
00017
00018 #include "CondCore/DBCommon/interface/Time.h"
00019
00020 #include "CoralBase/TimeStamp.h"
00021
00022 #include "boost/date_time/posix_time/posix_time.hpp"
00023 #include "boost/date_time.hpp"
00024
00025 #include <iostream>
00026 #include <stdexcept>
00027 #include <vector>
00028 #include <math.h>
00029
00030
00031
00032
00033
00034 RunSummaryRead::RunSummaryRead(
00035
00036 const std::string& connectionString,
00037 const std::string& user,
00038 const std::string& pass):
00039 TestBase(),
00040
00041 m_connectionString( connectionString ),
00042 m_user( user ),
00043 m_pass( pass )
00044 {
00045
00046 m_tableToRead="";
00047 m_columnToRead="";
00048
00049 }
00050
00051
00052 RunSummaryRead::~RunSummaryRead()
00053 {}
00054
00055
00056
00057 void
00058 RunSummaryRead::run()
00059 {
00060
00061 }
00062
00063
00064
00065
00066
00067 RunSummary
00068 RunSummaryRead::readData(const std::string & table, const std::string &column, const int r_number)
00069 {
00070 m_tableToRead = table;
00071 m_columnToRead= column;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 RunSummary sum;
00098 RunSummary temp_sum;
00099 RunSummary Sum;
00100
00101
00102
00103
00104 temp_sum.m_run = r_number;
00105
00106 std::cout<< "entering readData" << std::endl;
00107 coral::ISession* session = this->connect( m_connectionString,
00108 m_user, m_pass );
00109 session->transaction().start( );
00110 std::cout<< "starting session " << std::endl;
00111 coral::ISchema& schema = session->nominalSchema();
00112 std::cout<< " accessing schema " << std::endl;
00113 std::cout<< " trying to handle table :: " << m_tableToRead << std::endl;
00114
00115 coral::IQuery* queryI = schema.tableHandle( m_tableToRead).newQuery();
00116
00117 queryI->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00118
00119
00120 coral::AttributeList conditionData;
00121 conditionData.extend<int>( "n_run" );
00122 conditionData[0].data<int>() = r_number;
00123
00124
00125
00126 std::string condition1 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:SEQ_NAME'";
00127 queryI->setCondition( condition1, conditionData );
00128 coral::ICursor& cursorI = queryI->execute();
00129
00130 if ( cursorI.next()!=0 ) {
00131 const coral::AttributeList& row = cursorI.currentRow();
00132 temp_sum.m_name = row[m_columnToRead].data<std::string>();
00133 std::cout<< " name extracted == " << temp_sum.m_name << std::endl;
00134 }
00135 else{
00136 temp_sum.m_name = "null";
00137 std::cout<< " name extracted == " << temp_sum.m_name << std::endl;
00138 }
00139 delete queryI;
00140
00141
00142 std::string m_columnToRead_id = "ID";
00143 long long id_start=0;
00144
00145 coral::IQuery* queryII = schema.tableHandle( m_tableToRead).newQuery();
00146
00147
00148 queryII->addToOutputList( m_tableToRead + "." + m_columnToRead_id, m_columnToRead_id );
00149
00150 std::string condition2 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:START_TIME_T'";
00151 queryII->setCondition( condition2, conditionData );
00152 coral::ICursor& cursorII = queryII->execute();
00153
00154 if ( cursorII.next()!=0 ) {
00155 const coral::AttributeList& row = cursorII.currentRow();
00156 id_start= row[m_columnToRead_id].data<long long>();
00157 std::cout<< " id for start time time extracted == " <<id_start << std::endl;
00158 }
00159 else{
00160 id_start=-1;
00161 std::cout<< " id for start time time extracted == " <<id_start << std::endl;
00162 }
00163
00164 delete queryII;
00165
00166
00167 std::string m_tableToRead_date= "RUNSESSION_DATE";
00168 std::string m_columnToRead_val= "VALUE";
00169
00170 coral::IQuery* queryIII = schema.tableHandle( m_tableToRead_date).newQuery();
00171
00172
00173 queryIII->addToOutputList( m_tableToRead_date + "." + m_columnToRead_val, m_columnToRead_val );
00174
00175 coral::AttributeList conditionData3;
00176 conditionData3.extend<long long>( "n_id" );
00177 conditionData3[0].data<long long>() = id_start;
00178 std::string condition3 = m_tableToRead_date + ".runsession_parameter_id=:n_id";
00179 queryIII->setCondition( condition3, conditionData3 );
00180 coral::ICursor& cursorIII = queryIII->execute();
00181
00182 if ( cursorIII.next()!=0 ) {
00183 const coral::AttributeList& row = cursorIII.currentRow();
00184 coral::TimeStamp ts = row[m_columnToRead_val].data<coral::TimeStamp>();
00185 int year= ts.year();
00186 int month= ts.month();
00187 int day= ts.day();
00188 int hour= ts.hour();
00189 int minute= ts.minute();
00190 int second = ts.second();
00191 long nanosecond = ts.nanosecond();
00192
00194
00195
00196
00197 std::cout<< " start time time extracted == " << "-->year " << year
00198 << "-- month " << month
00199 << "-- day " << day
00200 << "-- hour " << hour
00201 << "-- minute " << minute
00202 << "-- second " << second
00203 << "-- nanosecond " << nanosecond<<std::endl;
00204 boost::gregorian::date dt(year,month,day);
00205
00206 boost::posix_time::time_duration td(hour-1,minute,second,nanosecond/1000);
00207
00208 boost::posix_time::ptime pt( dt, td);
00209
00210
00211
00212
00213
00214 std::cout<<"ptime == "<< pt <<std::endl;
00215
00216 temp_sum.m_start_time_str = boost::posix_time::to_iso_extended_string(pt);
00217 std::cout<<"start time string extracted == "<<temp_sum.m_start_time_str <<std::endl;
00218 boost::posix_time::ptime time_at_epoch( boost::gregorian::date( 1970, 1, 1 ) ) ;
00219
00220 boost::posix_time::time_duration time_diff = ( pt - time_at_epoch ) ;
00221 temp_sum.m_start_time_ll = time_diff.total_microseconds();
00222 std::cout << "microsecond since Epoch (UTC) : " <<temp_sum.m_start_time_ll <<std::endl;
00223 }
00224 else
00225 {
00226 temp_sum.m_start_time_str = "null";
00227 temp_sum.m_start_time_ll = -1;
00228 }
00229 delete queryIII;
00230
00231
00232
00233 coral::IQuery* queryIV = schema.tableHandle( m_tableToRead).newQuery();
00234
00235
00236 queryIV->addToOutputList( m_tableToRead + "." + m_columnToRead_id, m_columnToRead_id );
00237
00238 std::string condition4 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:STOP_TIME_T'";
00239
00240 queryIV->setCondition( condition4, conditionData );
00241
00242 coral::ICursor& cursorIV = queryIV->execute();
00243
00244
00245 long long id_stop=0;
00246 if ( cursorIV.next()!=0 ) {
00247 const coral::AttributeList& row = cursorIV.currentRow();
00248
00249 id_stop= row[m_columnToRead_id].data<long long>();
00250 std::cout<< " id for stop time time extracted == " <<id_stop << std::endl;
00251 }
00252 else{
00253 id_stop=-1;
00254 }
00255 delete queryIV;
00256
00257
00258
00259 coral::IQuery* queryIVbis = schema.tableHandle( m_tableToRead_date).newQuery();
00260
00261
00262 queryIVbis->addToOutputList( m_tableToRead_date + "." + m_columnToRead_val, m_columnToRead_val );
00263
00264 coral::AttributeList conditionData4bis;
00265 conditionData4bis.extend<long long>( "n_id" );
00266 conditionData4bis[0].data<long long>() = id_stop;
00267 std::string condition4bis = m_tableToRead_date + ".runsession_parameter_id=:n_id";
00268 queryIVbis->setCondition( condition4bis, conditionData4bis );
00269 coral::ICursor& cursorIVbis = queryIVbis->execute();
00270 if ( cursorIVbis.next()!=0 ) {
00271 const coral::AttributeList& row = cursorIVbis.currentRow();
00272 coral::TimeStamp ts = row[m_columnToRead_val].data<coral::TimeStamp>();
00273 int year= ts.year();
00274 int month= ts.month();
00275 int day= ts.day();
00276 int hour= ts.hour();
00277 int minute= ts.minute();
00278 int second = ts.second();
00279 long nanosecond = ts.nanosecond();
00280 std::cout<< " stop time time extracted == " << "-->year " << year
00281 << "-- month " << month
00282 << "-- day " << day
00283 << "-- hour " << hour
00284 << "-- minute " << minute
00285 << "-- second " << second
00286 << "-- nanosecond " << nanosecond<<std::endl;
00287 boost::gregorian::date dt(year,month,day);
00288
00289 boost::posix_time::time_duration td(hour-1,minute,second,nanosecond/1000);
00290 boost::posix_time::ptime pt( dt, td);
00291 std::cout<<"ptime == "<< pt <<std::endl;
00292 temp_sum.m_stop_time_str = boost::posix_time::to_iso_extended_string(pt);
00293 std::cout<<"stop time string extracted == "<<temp_sum.m_stop_time_str <<std::endl;
00294 boost::posix_time::ptime time_at_epoch( boost::gregorian::date( 1970, 1, 1 ) ) ;
00295
00296 boost::posix_time::time_duration time_diff = ( pt - time_at_epoch ) ;
00297 temp_sum.m_stop_time_ll = time_diff.total_microseconds();
00298 std::cout << "microsecond since Epoch (UTC) : " <<temp_sum.m_stop_time_ll <<std::endl;
00299 }
00300 else{
00301 temp_sum.m_stop_time_str="null";
00302 temp_sum.m_stop_time_ll=-1;
00303 }
00304 delete queryIVbis;
00305
00306
00307 const std::string m_tableToRead_ls="HLT_SUPERVISOR_LUMISECTIONS_V2";
00308 const std::string m_columnToRead_ls = "LSNUMBER";
00309
00310 coral::IQuery* queryV = schema.tableHandle( m_tableToRead_ls ).newQuery();
00311 queryV->addToOutputList( "MAX(" + m_tableToRead_ls + "." + m_columnToRead_ls +")", m_columnToRead_ls );
00312 std::string condition5 = m_tableToRead_ls + ".runnr=:n_run " ;
00313
00314
00315 queryV->setCondition( condition5, conditionData );
00316
00317
00318 coral::ICursor& cursorV = queryV->execute();
00319 if ( cursorV.next()!=0 ) {
00320 const coral::AttributeList& row = cursorV.currentRow();
00321 double lumisections = row[m_columnToRead_ls].data<double>();
00322 temp_sum.m_lumisections = static_cast<int>(lumisections);
00323 std::cout<<" lumisections number extracted == "<<temp_sum.m_lumisections << std::endl;
00324 }
00325 else{
00326 temp_sum.m_lumisections=-1;
00327 }
00328 std::cout<<" leaving the query "<< std::endl;
00329 delete queryV;
00330
00331
00332
00333
00334
00335
00336
00337
00338 coral::IQuery* queryVIPIXEL = schema.tableHandle( m_tableToRead).newQuery();
00339 queryVIPIXEL->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00340
00341 std::string condition6PIXEL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:PIXEL'";
00342 queryVIPIXEL->setCondition( condition6PIXEL, conditionData );
00343 coral::ICursor& cursorVIPIXEL = queryVIPIXEL->execute();
00344
00345 if ( cursorVIPIXEL.next()!=0 ) {
00346 const coral::AttributeList& row = cursorVIPIXEL.currentRow();
00347
00348 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.PIXEL) ;
00349 }
00350 else{
00351
00352
00353 }
00354 delete queryVIPIXEL;
00355
00356 coral::IQuery* queryVITRACKER = schema.tableHandle( m_tableToRead).newQuery();
00357 queryVITRACKER->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00358
00359 std::string condition6TRACKER = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:TRACKER'";
00360 queryVITRACKER->setCondition( condition6TRACKER, conditionData );
00361 coral::ICursor& cursorVITRACKER = queryVITRACKER->execute();
00362
00363 if ( cursorVITRACKER.next()!=0 ) {
00364 const coral::AttributeList& row = cursorVITRACKER.currentRow();
00365
00366
00367 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.TRACKER) ;
00368 }
00369 else{
00370
00371
00372 }
00373 delete queryVITRACKER;
00374
00375 coral::IQuery* queryVIECAL = schema.tableHandle( m_tableToRead).newQuery();
00376 queryVIECAL->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00377
00378 std::string condition6ECAL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:ECAL'";
00379 queryVIECAL->setCondition( condition6ECAL, conditionData );
00380 coral::ICursor& cursorVIECAL = queryVIECAL->execute();
00381
00382 if ( cursorVIECAL.next()!=0 ) {
00383 const coral::AttributeList& row = cursorVIECAL.currentRow();
00384
00385
00386 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.ECAL) ;
00387 }
00388 else{
00389
00390
00391 }
00392 delete queryVIECAL;
00393
00394 coral::IQuery* queryVIHCAL = schema.tableHandle( m_tableToRead).newQuery();
00395 queryVIHCAL->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00396
00397 std::string condition6HCAL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:HCAL'";
00398 queryVIHCAL->setCondition( condition6HCAL, conditionData );
00399 coral::ICursor& cursorVIHCAL = queryVIHCAL->execute();
00400
00401 if ( cursorVIHCAL.next()!=0 ) {
00402 const coral::AttributeList& row = cursorVIHCAL.currentRow();
00403
00404
00405 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.HCAL) ;
00406 }
00407 else{
00408
00409
00410 }
00411 delete queryVIHCAL;
00412
00413
00414 coral::IQuery* queryVIDT = schema.tableHandle( m_tableToRead).newQuery();
00415 queryVIDT->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00416
00417 std::string condition6DT = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:DT'";
00418 queryVIDT->setCondition( condition6DT, conditionData );
00419 coral::ICursor& cursorVIDT = queryVIDT->execute();
00420
00421 if ( cursorVIDT.next()!=0 ) {
00422 const coral::AttributeList& row = cursorVIDT.currentRow();
00423
00424
00425 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.DT) ;
00426 }
00427 else{
00428
00429
00430 }
00431 delete queryVIDT;
00432
00433 coral::IQuery* queryVICSC = schema.tableHandle( m_tableToRead).newQuery();
00434 queryVICSC->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00435
00436 std::string condition6CSC = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:CSC'";
00437 queryVICSC->setCondition( condition6CSC, conditionData );
00438 coral::ICursor& cursorVICSC = queryVICSC->execute();
00439
00440 if ( cursorVICSC.next()!=0 ) {
00441 const coral::AttributeList& row = cursorVICSC.currentRow();
00442
00443
00444 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.CSC) ;
00445 }
00446 else{
00447
00448
00449 }
00450 delete queryVICSC;
00451
00452 coral::IQuery* queryVIRPC = schema.tableHandle( m_tableToRead).newQuery();
00453 queryVIRPC->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00454
00455 std::string condition6RPC = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:RPC'";
00456 queryVIRPC->setCondition( condition6RPC, conditionData );
00457 coral::ICursor& cursorVIRPC = queryVIRPC->execute();
00458
00459 if ( cursorVIRPC.next()!=0 ) {
00460 const coral::AttributeList& row = cursorVIRPC.currentRow();
00461
00462
00463 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.RPC) ;
00464 }
00465 else{
00466
00467
00468 }
00469 delete queryVIRPC;
00470
00471
00472
00473
00474
00475
00476
00477 coral::IQuery* queryVII = schema.tableHandle( m_tableToRead).newQuery();
00478
00479 queryVII->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00480
00481
00482 coral::AttributeList conditionData7;
00483 conditionData7.extend<int>( "n_run" );
00484 conditionData7[0].data<int>() = r_number;
00485
00486
00487
00488 std::string condition7 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:HLT_KEY_DESCRIPTION'";
00489 queryVII->setCondition( condition7, conditionData7 );
00490 coral::ICursor& cursorVII = queryVII->execute();
00491
00492 if ( cursorVII.next()!=0 ) {
00493 const coral::AttributeList& row = cursorVII.currentRow();
00494 temp_sum.m_hltkey = row[m_columnToRead].data<std::string>();
00495 std::cout<< " hlt key extracted == " << temp_sum.m_hltkey << std::endl;
00496 }
00497 else{
00498 temp_sum.m_hltkey = "null";
00499 std::cout<< " hlt key extracted == " << temp_sum.m_hltkey << std::endl;
00500 }
00501 delete queryVII;
00502
00503
00504 coral::IQuery* queryVIII = schema.tableHandle( m_tableToRead).newQuery();
00505
00506 queryVIII->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00507
00508
00509 coral::AttributeList conditionData8;
00510 conditionData8.extend<int>( "n_run" );
00511 conditionData8[0].data<int>() = r_number;
00512
00513
00514
00515 std::string condition8 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.TRG:EVNR' ORDER BY TIME DESC";
00516 queryVIII->setCondition( condition8, conditionData8 );
00517 coral::ICursor& cursorVIII = queryVIII->execute();
00518
00519 if ( cursorVIII.next()!=0 ) {
00520 const coral::AttributeList& row = cursorVIII.currentRow();
00521
00522 temp_sum.m_nevents=atoll(row[m_columnToRead].data<std::string>().c_str());
00523
00524
00525 std::cout<< " number of events extracted == " << temp_sum.m_nevents << std::endl;
00526 }
00527 else{
00528 temp_sum.m_nevents = -1;
00529 std::cout<< " number of events extracted == " << temp_sum.m_nevents << std::endl;
00530 }
00531 delete queryVIII;
00532
00533
00534 coral::IQuery* queryIX = schema.tableHandle( m_tableToRead).newQuery();
00535
00536 queryIX->addToOutputList( "AVG(" + m_tableToRead + "." + m_columnToRead + ")", m_columnToRead );
00537
00538
00539
00540 coral::AttributeList conditionData9;
00541 conditionData9.extend<int>( "n_run" );
00542 conditionData9[0].data<int>() = r_number;
00543
00544
00545
00546 std::string condition9 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.TRG:Rate' ORDER BY TIME";
00547
00548 queryIX->setCondition( condition9, conditionData9 );
00549 coral::ICursor& cursorIX = queryIX->execute();
00550
00551 if ( cursorIX.next()!=0 ) {
00552 const coral::AttributeList& row = cursorIX.currentRow();
00553
00554 temp_sum.m_rate=(float)row[m_columnToRead].data<double>();
00555
00556
00557 std::cout<< " rate extracted == " << temp_sum.m_rate << std::endl;
00558 }
00559 else{
00560 temp_sum.m_rate = -1;
00561 std::cout<< " rate extracted == " << temp_sum.m_rate << std::endl;
00562 }
00563 delete queryIX;
00564
00565
00566
00567
00568 session->transaction().commit();
00569 delete session;
00570
00571
00572 sum= temp_sum;
00573 return sum;
00574 }
00575
00576
00577