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 #include "SealBase/TimeInfo.h"
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::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::RunSummary sum;
00098 RunSummary::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,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 boost::posix_time::time_duration td(hour,minute,second,nanosecond/1000);
00289 boost::posix_time::ptime pt( dt, td);
00290 std::cout<<"ptime == "<< pt <<std::endl;
00291 temp_sum.m_stop_time_str = boost::posix_time::to_iso_extended_string(pt);
00292 std::cout<<"stop time string extracted == "<<temp_sum.m_stop_time_str <<std::endl;
00293 boost::posix_time::ptime time_at_epoch( boost::gregorian::date( 1970, 1, 1 ) ) ;
00294
00295 boost::posix_time::time_duration time_diff = ( pt - time_at_epoch ) ;
00296 temp_sum.m_stop_time_ll = time_diff.total_microseconds();
00297 std::cout << "microsecond since Epoch (UTC) : " <<temp_sum.m_stop_time_ll <<std::endl;
00298 }
00299 else{
00300 temp_sum.m_stop_time_str="null";
00301 temp_sum.m_stop_time_ll=-1;
00302 }
00303 delete queryIVbis;
00304
00305
00306 const std::string m_tableToRead_ls="HLT_SUPERVISOR_LUMISECTIONS_V2";
00307 const std::string m_columnToRead_ls = "LSNUMBER";
00308
00309 coral::IQuery* queryV = schema.tableHandle( m_tableToRead_ls ).newQuery();
00310 queryV->addToOutputList( "MAX(" + m_tableToRead_ls + "." + m_columnToRead_ls +")", m_columnToRead_ls );
00311 std::string condition5 = m_tableToRead_ls + ".runnr=:n_run " ;
00312
00313
00314 queryV->setCondition( condition5, conditionData );
00315
00316
00317 coral::ICursor& cursorV = queryV->execute();
00318 if ( cursorV.next()!=0 ) {
00319 const coral::AttributeList& row = cursorV.currentRow();
00320 double lumisections = row[m_columnToRead_ls].data<double>();
00321 temp_sum.m_lumisections = static_cast<int>(lumisections);
00322 std::cout<<" lumisections number extracted == "<<temp_sum.m_lumisections << std::endl;
00323 }
00324 else{
00325 temp_sum.m_lumisections=-1;
00326 }
00327 std::cout<<" leaving the query "<< std::endl;
00328 delete queryV;
00329
00330
00331
00332
00333
00334
00335
00336
00337 coral::IQuery* queryVIPIXEL = schema.tableHandle( m_tableToRead).newQuery();
00338 queryVIPIXEL->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00339
00340 std::string condition6PIXEL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:PIXEL'";
00341 queryVIPIXEL->setCondition( condition6PIXEL, conditionData );
00342 coral::ICursor& cursorVIPIXEL = queryVIPIXEL->execute();
00343
00344 if ( cursorVIPIXEL.next()!=0 ) {
00345 const coral::AttributeList& row = cursorVIPIXEL.currentRow();
00346
00347 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.PIXEL) ;
00348 }
00349 else{
00350
00351
00352 }
00353 delete queryVIPIXEL;
00354
00355 coral::IQuery* queryVITRACKER = schema.tableHandle( m_tableToRead).newQuery();
00356 queryVITRACKER->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00357
00358 std::string condition6TRACKER = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:TRACKER'";
00359 queryVITRACKER->setCondition( condition6TRACKER, conditionData );
00360 coral::ICursor& cursorVITRACKER = queryVITRACKER->execute();
00361
00362 if ( cursorVITRACKER.next()!=0 ) {
00363 const coral::AttributeList& row = cursorVITRACKER.currentRow();
00364
00365
00366 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.TRACKER) ;
00367 }
00368 else{
00369
00370
00371 }
00372 delete queryVITRACKER;
00373
00374 coral::IQuery* queryVIECAL = schema.tableHandle( m_tableToRead).newQuery();
00375 queryVIECAL->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00376
00377 std::string condition6ECAL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:ECAL'";
00378 queryVIECAL->setCondition( condition6ECAL, conditionData );
00379 coral::ICursor& cursorVIECAL = queryVIECAL->execute();
00380
00381 if ( cursorVIECAL.next()!=0 ) {
00382 const coral::AttributeList& row = cursorVIECAL.currentRow();
00383
00384
00385 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.ECAL) ;
00386 }
00387 else{
00388
00389
00390 }
00391 delete queryVIECAL;
00392
00393 coral::IQuery* queryVIHCAL = schema.tableHandle( m_tableToRead).newQuery();
00394 queryVIHCAL->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00395
00396 std::string condition6HCAL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:HCAL'";
00397 queryVIHCAL->setCondition( condition6HCAL, conditionData );
00398 coral::ICursor& cursorVIHCAL = queryVIHCAL->execute();
00399
00400 if ( cursorVIHCAL.next()!=0 ) {
00401 const coral::AttributeList& row = cursorVIHCAL.currentRow();
00402
00403
00404 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.HCAL) ;
00405 }
00406 else{
00407
00408
00409 }
00410 delete queryVIHCAL;
00411
00412
00413 coral::IQuery* queryVIDT = schema.tableHandle( m_tableToRead).newQuery();
00414 queryVIDT->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00415
00416 std::string condition6DT = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:DT'";
00417 queryVIDT->setCondition( condition6DT, conditionData );
00418 coral::ICursor& cursorVIDT = queryVIDT->execute();
00419
00420 if ( cursorVIDT.next()!=0 ) {
00421 const coral::AttributeList& row = cursorVIDT.currentRow();
00422
00423
00424 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.DT) ;
00425 }
00426 else{
00427
00428
00429 }
00430 delete queryVIDT;
00431
00432 coral::IQuery* queryVICSC = schema.tableHandle( m_tableToRead).newQuery();
00433 queryVICSC->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00434
00435 std::string condition6CSC = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:CSC'";
00436 queryVICSC->setCondition( condition6CSC, conditionData );
00437 coral::ICursor& cursorVICSC = queryVICSC->execute();
00438
00439 if ( cursorVICSC.next()!=0 ) {
00440 const coral::AttributeList& row = cursorVICSC.currentRow();
00441
00442
00443 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.CSC) ;
00444 }
00445 else{
00446
00447
00448 }
00449 delete queryVICSC;
00450
00451 coral::IQuery* queryVIRPC = schema.tableHandle( m_tableToRead).newQuery();
00452 queryVIRPC->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00453
00454 std::string condition6RPC = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:RPC'";
00455 queryVIRPC->setCondition( condition6RPC, conditionData );
00456 coral::ICursor& cursorVIRPC = queryVIRPC->execute();
00457
00458 if ( cursorVIRPC.next()!=0 ) {
00459 const coral::AttributeList& row = cursorVIRPC.currentRow();
00460
00461
00462 if (row[m_columnToRead].data<std::string>()=="In") temp_sum.m_subdt_in.push_back(Sum.RPC) ;
00463 }
00464 else{
00465
00466
00467 }
00468 delete queryVIRPC;
00469
00470
00471
00472
00473
00474
00475
00476 coral::IQuery* queryVII = schema.tableHandle( m_tableToRead).newQuery();
00477
00478 queryVII->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00479
00480
00481 coral::AttributeList conditionData7;
00482 conditionData7.extend<int>( "n_run" );
00483 conditionData7[0].data<int>() = r_number;
00484
00485
00486
00487 std::string condition7 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:HLT_KEY_DESCRIPTION'";
00488 queryVII->setCondition( condition7, conditionData7 );
00489 coral::ICursor& cursorVII = queryVII->execute();
00490
00491 if ( cursorVII.next()!=0 ) {
00492 const coral::AttributeList& row = cursorVII.currentRow();
00493 temp_sum.m_hltkey = row[m_columnToRead].data<std::string>();
00494 std::cout<< " hlt key extracted == " << temp_sum.m_hltkey << std::endl;
00495 }
00496 else{
00497 temp_sum.m_hltkey = "null";
00498 std::cout<< " hlt key extracted == " << temp_sum.m_hltkey << std::endl;
00499 }
00500 delete queryVII;
00501
00502
00503 coral::IQuery* queryVIII = schema.tableHandle( m_tableToRead).newQuery();
00504
00505 queryVIII->addToOutputList( m_tableToRead + "." + m_columnToRead, m_columnToRead );
00506
00507
00508 coral::AttributeList conditionData8;
00509 conditionData8.extend<int>( "n_run" );
00510 conditionData8[0].data<int>() = r_number;
00511
00512
00513
00514 std::string condition8 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.TRG:EVNR' ORDER BY TIME DESC";
00515 queryVIII->setCondition( condition8, conditionData8 );
00516 coral::ICursor& cursorVIII = queryVIII->execute();
00517
00518 if ( cursorVIII.next()!=0 ) {
00519 const coral::AttributeList& row = cursorVIII.currentRow();
00520
00521 temp_sum.m_nevents=atoll(row[m_columnToRead].data<std::string>().c_str());
00522
00523
00524 std::cout<< " number of events extracted == " << temp_sum.m_nevents << std::endl;
00525 }
00526 else{
00527 temp_sum.m_nevents = -1;
00528 std::cout<< " number of events extracted == " << temp_sum.m_nevents << std::endl;
00529 }
00530 delete queryVIII;
00531
00532
00533 coral::IQuery* queryIX = schema.tableHandle( m_tableToRead).newQuery();
00534
00535 queryIX->addToOutputList( "AVG(" + m_tableToRead + "." + m_columnToRead + ")", m_columnToRead );
00536
00537
00538
00539 coral::AttributeList conditionData9;
00540 conditionData9.extend<int>( "n_run" );
00541 conditionData9[0].data<int>() = r_number;
00542
00543
00544
00545 std::string condition9 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.TRG:Rate' ORDER BY TIME";
00546
00547 queryIX->setCondition( condition9, conditionData9 );
00548 coral::ICursor& cursorIX = queryIX->execute();
00549
00550 if ( cursorIX.next()!=0 ) {
00551 const coral::AttributeList& row = cursorIX.currentRow();
00552
00553 temp_sum.m_rate=(float)row[m_columnToRead].data<double>();
00554
00555
00556 std::cout<< " rate extracted == " << temp_sum.m_rate << std::endl;
00557 }
00558 else{
00559 temp_sum.m_rate = -1;
00560 std::cout<< " rate extracted == " << temp_sum.m_rate << std::endl;
00561 }
00562 delete queryIX;
00563
00564
00565
00566
00567 session->transaction().commit();
00568 delete session;
00569
00570
00571 sum= temp_sum;
00572 return sum;
00573 }
00574
00575
00576