File indexing completed on 2023-03-17 10:48:38
0001 #include "CondTools/RunInfo/interface/RunSummaryRead.h"
0002
0003 #include "RelationalAccess/ISession.h"
0004 #include "RelationalAccess/ITransaction.h"
0005 #include "RelationalAccess/ISchema.h"
0006 #include "RelationalAccess/ITable.h"
0007 #include "RelationalAccess/ITableDataEditor.h"
0008 #include "RelationalAccess/TableDescription.h"
0009 #include "RelationalAccess/IQuery.h"
0010 #include "RelationalAccess/ICursor.h"
0011 #include "CoralBase/AttributeList.h"
0012 #include "CoralBase/Attribute.h"
0013 #include "CoralBase/AttributeSpecification.h"
0014
0015
0016 #include "CondCore/CondDB/interface/Time.h"
0017
0018 #include "CoralBase/TimeStamp.h"
0019
0020 #include "boost/date_time/posix_time/posix_time.hpp"
0021 #include "boost/date_time.hpp"
0022
0023 #include <iostream>
0024 #include <stdexcept>
0025 #include <vector>
0026 #include <cmath>
0027
0028 RunSummaryRead::RunSummaryRead(
0029
0030 const std::string& connectionString, const std::string& user, const std::string& pass)
0031 : TestBase(),
0032
0033 m_connectionString(connectionString),
0034 m_user(user),
0035 m_pass(pass) {
0036 m_tableToRead = "";
0037 m_columnToRead = "";
0038 }
0039
0040 RunSummaryRead::~RunSummaryRead() {}
0041
0042 void RunSummaryRead::run() {}
0043
0044 RunSummary RunSummaryRead::readData(const std::string& table, const std::string& column, const int r_number) {
0045 m_tableToRead = table;
0046 m_columnToRead = column;
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 RunSummary sum;
0073 RunSummary temp_sum;
0074 RunSummary Sum;
0075
0076
0077 temp_sum.m_run = r_number;
0078
0079 std::cout << "entering readData" << std::endl;
0080 coral::ISession* session = this->connect(m_connectionString, m_user, m_pass);
0081 session->transaction().start();
0082 std::cout << "starting session " << std::endl;
0083 coral::ISchema& schema = session->nominalSchema();
0084 std::cout << " accessing schema " << std::endl;
0085 std::cout << " trying to handle table :: " << m_tableToRead << std::endl;
0086
0087 coral::IQuery* queryI = schema.tableHandle(m_tableToRead).newQuery();
0088
0089 queryI->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0090
0091
0092 coral::AttributeList conditionData;
0093 conditionData.extend<int>("n_run");
0094 conditionData[0].data<int>() = r_number;
0095
0096
0097 std::string condition1 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:SEQ_NAME'";
0098 queryI->setCondition(condition1, conditionData);
0099 coral::ICursor& cursorI = queryI->execute();
0100
0101 if (cursorI.next() != 0) {
0102 const coral::AttributeList& row = cursorI.currentRow();
0103 temp_sum.m_name = row[m_columnToRead].data<std::string>();
0104 std::cout << " name extracted == " << temp_sum.m_name << std::endl;
0105 } else {
0106 temp_sum.m_name = "null";
0107 std::cout << " name extracted == " << temp_sum.m_name << std::endl;
0108 }
0109 delete queryI;
0110
0111 std::string m_columnToRead_id = "ID";
0112 long long id_start = 0;
0113
0114 coral::IQuery* queryII = schema.tableHandle(m_tableToRead).newQuery();
0115
0116
0117 queryII->addToOutputList(m_tableToRead + "." + m_columnToRead_id, m_columnToRead_id);
0118
0119 std::string condition2 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:START_TIME_T'";
0120 queryII->setCondition(condition2, conditionData);
0121 coral::ICursor& cursorII = queryII->execute();
0122
0123 if (cursorII.next() != 0) {
0124 const coral::AttributeList& row = cursorII.currentRow();
0125 id_start = row[m_columnToRead_id].data<long long>();
0126 std::cout << " id for start time time extracted == " << id_start << std::endl;
0127 } else {
0128 id_start = -1;
0129 std::cout << " id for start time time extracted == " << id_start << std::endl;
0130 }
0131
0132 delete queryII;
0133
0134
0135 std::string m_tableToRead_date = "RUNSESSION_DATE";
0136 std::string m_columnToRead_val = "VALUE";
0137
0138 coral::IQuery* queryIII = schema.tableHandle(m_tableToRead_date).newQuery();
0139
0140
0141 queryIII->addToOutputList(m_tableToRead_date + "." + m_columnToRead_val, m_columnToRead_val);
0142
0143 coral::AttributeList conditionData3;
0144 conditionData3.extend<long long>("n_id");
0145 conditionData3[0].data<long long>() = id_start;
0146 std::string condition3 = m_tableToRead_date + ".runsession_parameter_id=:n_id";
0147 queryIII->setCondition(condition3, conditionData3);
0148 coral::ICursor& cursorIII = queryIII->execute();
0149
0150 if (cursorIII.next() != 0) {
0151 const coral::AttributeList& row = cursorIII.currentRow();
0152 coral::TimeStamp ts = row[m_columnToRead_val].data<coral::TimeStamp>();
0153 int year = ts.year();
0154 int month = ts.month();
0155 int day = ts.day();
0156 int hour = ts.hour();
0157 int minute = ts.minute();
0158 int second = ts.second();
0159 long nanosecond = ts.nanosecond();
0160
0161
0162
0163
0164 std::cout << " start time time extracted == "
0165 << "-->year " << year << "-- month " << month << "-- day " << day << "-- hour " << hour << "-- minute "
0166 << minute << "-- second " << second << "-- nanosecond " << nanosecond << std::endl;
0167 boost::gregorian::date dt(year, month, day);
0168
0169 boost::posix_time::time_duration td(hour - 1, minute, second, nanosecond / 1000);
0170
0171 boost::posix_time::ptime pt(dt, td);
0172
0173
0174
0175
0176
0177 std::cout << "ptime == " << pt << std::endl;
0178
0179 temp_sum.m_start_time_str = boost::posix_time::to_iso_extended_string(pt);
0180 std::cout << "start time string extracted == " << temp_sum.m_start_time_str << std::endl;
0181 boost::posix_time::ptime time_at_epoch(boost::gregorian::date(1970, 1, 1));
0182
0183 boost::posix_time::time_duration time_diff = (pt - time_at_epoch);
0184 temp_sum.m_start_time_ll = time_diff.total_microseconds();
0185 std::cout << "microsecond since Epoch (UTC) : " << temp_sum.m_start_time_ll << std::endl;
0186 } else {
0187 temp_sum.m_start_time_str = "null";
0188 temp_sum.m_start_time_ll = -1;
0189 }
0190 delete queryIII;
0191
0192
0193 coral::IQuery* queryIV = schema.tableHandle(m_tableToRead).newQuery();
0194
0195
0196 queryIV->addToOutputList(m_tableToRead + "." + m_columnToRead_id, m_columnToRead_id);
0197
0198 std::string condition4 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:STOP_TIME_T'";
0199
0200 queryIV->setCondition(condition4, conditionData);
0201
0202 coral::ICursor& cursorIV = queryIV->execute();
0203
0204 long long id_stop = 0;
0205 if (cursorIV.next() != 0) {
0206 const coral::AttributeList& row = cursorIV.currentRow();
0207
0208 id_stop = row[m_columnToRead_id].data<long long>();
0209 std::cout << " id for stop time time extracted == " << id_stop << std::endl;
0210 } else {
0211 id_stop = -1;
0212 }
0213 delete queryIV;
0214
0215
0216
0217 coral::IQuery* queryIVbis = schema.tableHandle(m_tableToRead_date).newQuery();
0218
0219
0220 queryIVbis->addToOutputList(m_tableToRead_date + "." + m_columnToRead_val, m_columnToRead_val);
0221
0222 coral::AttributeList conditionData4bis;
0223 conditionData4bis.extend<long long>("n_id");
0224 conditionData4bis[0].data<long long>() = id_stop;
0225 std::string condition4bis = m_tableToRead_date + ".runsession_parameter_id=:n_id";
0226 queryIVbis->setCondition(condition4bis, conditionData4bis);
0227 coral::ICursor& cursorIVbis = queryIVbis->execute();
0228 if (cursorIVbis.next() != 0) {
0229 const coral::AttributeList& row = cursorIVbis.currentRow();
0230 coral::TimeStamp ts = row[m_columnToRead_val].data<coral::TimeStamp>();
0231 int year = ts.year();
0232 int month = ts.month();
0233 int day = ts.day();
0234 int hour = ts.hour();
0235 int minute = ts.minute();
0236 int second = ts.second();
0237 long nanosecond = ts.nanosecond();
0238 std::cout << " stop time time extracted == "
0239 << "-->year " << year << "-- month " << month << "-- day " << day << "-- hour " << hour << "-- minute "
0240 << minute << "-- second " << second << "-- nanosecond " << nanosecond << std::endl;
0241 boost::gregorian::date dt(year, month, day);
0242
0243 boost::posix_time::time_duration td(hour - 1, minute, second, nanosecond / 1000);
0244 boost::posix_time::ptime pt(dt, td);
0245 std::cout << "ptime == " << pt << std::endl;
0246 temp_sum.m_stop_time_str = boost::posix_time::to_iso_extended_string(pt);
0247 std::cout << "stop time string extracted == " << temp_sum.m_stop_time_str << std::endl;
0248 boost::posix_time::ptime time_at_epoch(boost::gregorian::date(1970, 1, 1));
0249
0250 boost::posix_time::time_duration time_diff = (pt - time_at_epoch);
0251 temp_sum.m_stop_time_ll = time_diff.total_microseconds();
0252 std::cout << "microsecond since Epoch (UTC) : " << temp_sum.m_stop_time_ll << std::endl;
0253 } else {
0254 temp_sum.m_stop_time_str = "null";
0255 temp_sum.m_stop_time_ll = -1;
0256 }
0257 delete queryIVbis;
0258
0259
0260 const std::string m_tableToRead_ls = "HLT_SUPERVISOR_LUMISECTIONS_V2";
0261 const std::string m_columnToRead_ls = "LSNUMBER";
0262
0263 coral::IQuery* queryV = schema.tableHandle(m_tableToRead_ls).newQuery();
0264 queryV->addToOutputList("MAX(" + m_tableToRead_ls + "." + m_columnToRead_ls + ")", m_columnToRead_ls);
0265 std::string condition5 = m_tableToRead_ls + ".runnr=:n_run ";
0266
0267
0268 queryV->setCondition(condition5, conditionData);
0269
0270
0271 coral::ICursor& cursorV = queryV->execute();
0272 if (cursorV.next() != 0) {
0273 const coral::AttributeList& row = cursorV.currentRow();
0274 double lumisections = row[m_columnToRead_ls].data<double>();
0275 temp_sum.m_lumisections = static_cast<int>(lumisections);
0276 std::cout << " lumisections number extracted == " << temp_sum.m_lumisections << std::endl;
0277 } else {
0278 temp_sum.m_lumisections = -1;
0279 }
0280 std::cout << " leaving the query " << std::endl;
0281 delete queryV;
0282
0283
0284
0285 coral::IQuery* queryVIPIXEL = schema.tableHandle(m_tableToRead).newQuery();
0286 queryVIPIXEL->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0287
0288 std::string condition6PIXEL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:PIXEL'";
0289 queryVIPIXEL->setCondition(condition6PIXEL, conditionData);
0290 coral::ICursor& cursorVIPIXEL = queryVIPIXEL->execute();
0291
0292 if (cursorVIPIXEL.next() != 0) {
0293 const coral::AttributeList& row = cursorVIPIXEL.currentRow();
0294
0295 if (row[m_columnToRead].data<std::string>() == "In")
0296 temp_sum.m_subdt_in.push_back(Sum.PIXEL);
0297 } else {
0298
0299 }
0300 delete queryVIPIXEL;
0301
0302 coral::IQuery* queryVITRACKER = schema.tableHandle(m_tableToRead).newQuery();
0303 queryVITRACKER->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0304
0305 std::string condition6TRACKER = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:TRACKER'";
0306 queryVITRACKER->setCondition(condition6TRACKER, conditionData);
0307 coral::ICursor& cursorVITRACKER = queryVITRACKER->execute();
0308
0309 if (cursorVITRACKER.next() != 0) {
0310 const coral::AttributeList& row = cursorVITRACKER.currentRow();
0311
0312
0313 if (row[m_columnToRead].data<std::string>() == "In")
0314 temp_sum.m_subdt_in.push_back(Sum.TRACKER);
0315 } else {
0316
0317 }
0318 delete queryVITRACKER;
0319
0320 coral::IQuery* queryVIECAL = schema.tableHandle(m_tableToRead).newQuery();
0321 queryVIECAL->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0322
0323 std::string condition6ECAL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:ECAL'";
0324 queryVIECAL->setCondition(condition6ECAL, conditionData);
0325 coral::ICursor& cursorVIECAL = queryVIECAL->execute();
0326
0327 if (cursorVIECAL.next() != 0) {
0328 const coral::AttributeList& row = cursorVIECAL.currentRow();
0329
0330
0331 if (row[m_columnToRead].data<std::string>() == "In")
0332 temp_sum.m_subdt_in.push_back(Sum.ECAL);
0333 } else {
0334
0335 }
0336 delete queryVIECAL;
0337
0338 coral::IQuery* queryVIHCAL = schema.tableHandle(m_tableToRead).newQuery();
0339 queryVIHCAL->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0340
0341 std::string condition6HCAL = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:HCAL'";
0342 queryVIHCAL->setCondition(condition6HCAL, conditionData);
0343 coral::ICursor& cursorVIHCAL = queryVIHCAL->execute();
0344
0345 if (cursorVIHCAL.next() != 0) {
0346 const coral::AttributeList& row = cursorVIHCAL.currentRow();
0347
0348
0349 if (row[m_columnToRead].data<std::string>() == "In")
0350 temp_sum.m_subdt_in.push_back(Sum.HCAL);
0351 } else {
0352
0353 }
0354 delete queryVIHCAL;
0355
0356 coral::IQuery* queryVIDT = schema.tableHandle(m_tableToRead).newQuery();
0357 queryVIDT->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0358
0359 std::string condition6DT = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:DT'";
0360 queryVIDT->setCondition(condition6DT, conditionData);
0361 coral::ICursor& cursorVIDT = queryVIDT->execute();
0362
0363 if (cursorVIDT.next() != 0) {
0364 const coral::AttributeList& row = cursorVIDT.currentRow();
0365
0366
0367 if (row[m_columnToRead].data<std::string>() == "In")
0368 temp_sum.m_subdt_in.push_back(Sum.DT);
0369 } else {
0370
0371 }
0372 delete queryVIDT;
0373
0374 coral::IQuery* queryVICSC = schema.tableHandle(m_tableToRead).newQuery();
0375 queryVICSC->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0376
0377 std::string condition6CSC = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:CSC'";
0378 queryVICSC->setCondition(condition6CSC, conditionData);
0379 coral::ICursor& cursorVICSC = queryVICSC->execute();
0380
0381 if (cursorVICSC.next() != 0) {
0382 const coral::AttributeList& row = cursorVICSC.currentRow();
0383
0384
0385 if (row[m_columnToRead].data<std::string>() == "In")
0386 temp_sum.m_subdt_in.push_back(Sum.CSC);
0387 } else {
0388
0389 }
0390 delete queryVICSC;
0391
0392 coral::IQuery* queryVIRPC = schema.tableHandle(m_tableToRead).newQuery();
0393 queryVIRPC->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0394
0395 std::string condition6RPC = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:RPC'";
0396 queryVIRPC->setCondition(condition6RPC, conditionData);
0397 coral::ICursor& cursorVIRPC = queryVIRPC->execute();
0398
0399 if (cursorVIRPC.next() != 0) {
0400 const coral::AttributeList& row = cursorVIRPC.currentRow();
0401
0402
0403 if (row[m_columnToRead].data<std::string>() == "In")
0404 temp_sum.m_subdt_in.push_back(Sum.RPC);
0405 } else {
0406
0407 }
0408 delete queryVIRPC;
0409
0410
0411
0412
0413
0414
0415 coral::IQuery* queryVII = schema.tableHandle(m_tableToRead).newQuery();
0416
0417 queryVII->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0418
0419
0420 coral::AttributeList conditionData7;
0421 conditionData7.extend<int>("n_run");
0422 conditionData7[0].data<int>() = r_number;
0423
0424
0425 std::string condition7 =
0426 m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:HLT_KEY_DESCRIPTION'";
0427 queryVII->setCondition(condition7, conditionData7);
0428 coral::ICursor& cursorVII = queryVII->execute();
0429
0430 if (cursorVII.next() != 0) {
0431 const coral::AttributeList& row = cursorVII.currentRow();
0432 temp_sum.m_hltkey = row[m_columnToRead].data<std::string>();
0433 std::cout << " hlt key extracted == " << temp_sum.m_hltkey << std::endl;
0434 } else {
0435 temp_sum.m_hltkey = "null";
0436 std::cout << " hlt key extracted == " << temp_sum.m_hltkey << std::endl;
0437 }
0438 delete queryVII;
0439
0440
0441 coral::IQuery* queryVIII = schema.tableHandle(m_tableToRead).newQuery();
0442
0443 queryVIII->addToOutputList(m_tableToRead + "." + m_columnToRead, m_columnToRead);
0444
0445
0446 coral::AttributeList conditionData8;
0447 conditionData8.extend<int>("n_run");
0448 conditionData8[0].data<int>() = r_number;
0449
0450
0451 std::string condition8 =
0452 m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.TRG:EVNR' ORDER BY TIME DESC";
0453 queryVIII->setCondition(condition8, conditionData8);
0454 coral::ICursor& cursorVIII = queryVIII->execute();
0455
0456 if (cursorVIII.next() != 0) {
0457 const coral::AttributeList& row = cursorVIII.currentRow();
0458
0459 temp_sum.m_nevents = atoll(row[m_columnToRead].data<std::string>().c_str());
0460
0461 std::cout << " number of events extracted == " << temp_sum.m_nevents << std::endl;
0462 } else {
0463 temp_sum.m_nevents = -1;
0464 std::cout << " number of events extracted == " << temp_sum.m_nevents << std::endl;
0465 }
0466 delete queryVIII;
0467
0468
0469 coral::IQuery* queryIX = schema.tableHandle(m_tableToRead).newQuery();
0470
0471 queryIX->addToOutputList("AVG(" + m_tableToRead + "." + m_columnToRead + ")", m_columnToRead);
0472
0473
0474 coral::AttributeList conditionData9;
0475 conditionData9.extend<int>("n_run");
0476 conditionData9[0].data<int>() = r_number;
0477
0478
0479 std::string condition9 =
0480 m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.TRG:Rate' ORDER BY TIME";
0481
0482 queryIX->setCondition(condition9, conditionData9);
0483 coral::ICursor& cursorIX = queryIX->execute();
0484
0485 if (cursorIX.next() != 0) {
0486 const coral::AttributeList& row = cursorIX.currentRow();
0487
0488 temp_sum.m_rate = (float)row[m_columnToRead].data<double>();
0489
0490 std::cout << " rate extracted == " << temp_sum.m_rate << std::endl;
0491 } else {
0492 temp_sum.m_rate = -1;
0493 std::cout << " rate extracted == " << temp_sum.m_rate << std::endl;
0494 }
0495 delete queryIX;
0496
0497 session->transaction().commit();
0498 delete session;
0499
0500 sum = temp_sum;
0501 return sum;
0502 }