Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-07 04:53:56

0001 //#include "CondFormats/Common/interface/TimeConversions.h"
0002 //#include "CondFormats/Common/interface/Time.h"
0003 #include "CondTools/RunInfo/interface/RunInfoRead.h"
0004 #include "CondCore/CondDB/interface/ConnectionPool.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "FWCore/Utilities/interface/Exception.h"
0007 #include "RelationalAccess/ISessionProxy.h"
0008 #include "RelationalAccess/ITransaction.h"
0009 #include "RelationalAccess/ISchema.h"
0010 #include "RelationalAccess/ITable.h"
0011 #include "RelationalAccess/IQuery.h"
0012 #include "RelationalAccess/ICursor.h"
0013 #include "CoralBase/AttributeList.h"
0014 #include "CoralBase/Attribute.h"
0015 #include "CoralBase/AttributeSpecification.h"
0016 #include "CoralBase/TimeStamp.h"
0017 #include <algorithm>
0018 #include <iostream>
0019 #include <iterator>
0020 #include <memory>
0021 #include <stdexcept>
0022 #include <vector>
0023 #include <cmath>
0024 
0025 namespace {
0026   const std::string dot(".");
0027   const std::string quote("\"");
0028   const std::string bNOTb(" NOT ");
0029   const std::string squoted(const std::string& s) { return quote + s + quote; }
0030   //now strings for the tables and columns to be queried
0031   const std::string sParameterTable("RUNSESSION_PARAMETER");
0032   const std::string sDateTable("RUNSESSION_DATE");
0033   const std::string sStringTable("RUNSESSION_STRING");
0034   const std::string sIdParameterColumn("ID");
0035   const std::string sRunNumberParameterColumn("RUNNUMBER");
0036   const std::string sNameParameterColumn("NAME");
0037   const std::string sRunSessionParameterIdDataColumn("RUNSESSION_PARAMETER_ID");
0038   const std::string sValueDataColumn("VALUE");
0039   const std::string sDCSMagnetTable("CMSFWMAGNET");
0040   const std::string sDCSMagnetCurrentColumn("CURRENT");
0041   const std::string sDCSMagnetChangeDateColumn("CHANGE_DATE");
0042 }  // namespace
0043 
0044 RunInfoRead::RunInfoRead(const std::string& connectionString, const edm::ParameterSet& connectionPset)
0045     : m_connectionString(connectionString), m_connectionPset(connectionPset) {}
0046 
0047 RunInfoRead::~RunInfoRead() {}
0048 
0049 RunInfo RunInfoRead::readData(const std::string& runinfo_schema, const std::string& dcsenv_schema, const int r_number) {
0050   RunInfo temp_sum;
0051   //for B currents...
0052   bool Bnotchanged = false;
0053   //from TimeConversions.h
0054   const boost::posix_time::ptime time0 = boost::posix_time::from_time_t(0);
0055   //if cursor is null setting null values
0056   temp_sum.m_run = r_number;
0057   edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Initialising Connection Pool" << std::endl;
0058   cond::persistency::ConnectionPool connection;
0059   connection.setParameters(m_connectionPset);
0060   connection.configure();
0061   edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Initialising read-only session to "
0062                                 << m_connectionString << std::endl;
0063   std::shared_ptr<coral::ISessionProxy> session = connection.createCoralSession(m_connectionString, false);
0064   try {
0065     session->transaction().start(true);
0066     coral::ISchema& schema = session->schema(runinfo_schema);
0067     edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Accessing schema " << runinfo_schema
0068                                   << std::endl;
0069     //new query to obtain the start_time
0070     std::unique_ptr<coral::IQuery> query(schema.newQuery());
0071     query->addToTableList(sParameterTable);
0072     query->addToTableList(sDateTable);
0073     query->addToOutputList(sValueDataColumn);
0074     coral::AttributeList runTimeDataOutput;
0075     runTimeDataOutput.extend<coral::TimeStamp>(sValueDataColumn);
0076     query->defineOutput(runTimeDataOutput);
0077     std::string runStartWhereClause(sRunNumberParameterColumn + std::string("=:n_run AND ") + sNameParameterColumn +
0078                                     std::string("='CMS.LVL0:START_TIME_T' AND ") + sIdParameterColumn +
0079                                     std::string("=") + sRunSessionParameterIdDataColumn);
0080     coral::AttributeList runNumberBindVariableList;
0081     runNumberBindVariableList.extend<int>("n_run");
0082     runNumberBindVariableList["n_run"].data<int>() = r_number;
0083     query->setCondition(runStartWhereClause, runNumberBindVariableList);
0084     coral::ICursor& runStartCursor = query->execute();
0085     coral::TimeStamp start;  //now all times are UTC!
0086     if (runStartCursor.next()) {
0087       std::ostringstream osstartdebug;
0088       runStartCursor.currentRow().toOutputStream(osstartdebug);
0089       LogDebug("RunInfoReader") << osstartdebug.str() << std::endl;
0090       const coral::AttributeList& row = runStartCursor.currentRow();
0091       start = row[sValueDataColumn].data<coral::TimeStamp>();
0092       LogDebug("RunInfoReader") << "UTC start time extracted == "
0093                                 << "-->year " << start.year() << "-- month " << start.month() << "-- day "
0094                                 << start.day() << "-- hour " << start.hour() << "-- minute " << start.minute()
0095                                 << "-- second " << start.second() << "-- nanosecond " << start.nanosecond()
0096                                 << std::endl;
0097       boost::posix_time::ptime start_ptime = start.time();
0098       boost::posix_time::time_duration startTimeFromEpoch = start_ptime - time0;
0099       temp_sum.m_start_time_str = boost::posix_time::to_iso_extended_string(start_ptime);
0100       temp_sum.m_start_time_ll = startTimeFromEpoch.total_microseconds();
0101       std::ostringstream osstart;
0102       osstart << "[RunInfoRead::" << __func__ << "]: Timestamp for start of run " << r_number << std::endl
0103               << "Posix time: " << start_ptime << std::endl
0104               << "ISO string: " << temp_sum.m_start_time_str << std::endl
0105               << "Microsecond since Epoch (UTC): " << temp_sum.m_start_time_ll;
0106       edm::LogInfo("RunInfoReader") << osstart.str() << std::endl;
0107     } else {
0108       std::stringstream errMsg;
0109       errMsg << "[RunInfoRead::" << __func__ << "]: run " << r_number << " start time not found.";
0110       throw std::runtime_error(errMsg.str());
0111     }
0112 
0113     //new query to obtain the stop_time
0114     query.reset(schema.newQuery());
0115     query->addToTableList(sParameterTable);
0116     query->addToTableList(sDateTable);
0117     query->addToOutputList(sValueDataColumn);
0118     query->defineOutput(runTimeDataOutput);
0119     std::string runStopWhereClause(sRunNumberParameterColumn + std::string("=:n_run AND ") + sNameParameterColumn +
0120                                    std::string("='CMS.LVL0:STOP_TIME_T' AND ") + sIdParameterColumn + std::string("=") +
0121                                    sRunSessionParameterIdDataColumn);
0122     query->setCondition(runStopWhereClause, runNumberBindVariableList);
0123     coral::ICursor& runStopCursor = query->execute();
0124     coral::TimeStamp stop;
0125     if (runStopCursor.next()) {
0126       std::ostringstream osstopdebug;
0127       runStopCursor.currentRow().toOutputStream(osstopdebug);
0128       LogDebug("RunInfoReader") << osstopdebug.str() << std::endl;
0129       const coral::AttributeList& row = runStopCursor.currentRow();
0130       stop = row[sValueDataColumn].data<coral::TimeStamp>();
0131       LogDebug("RunInfoReader") << "stop time extracted == "
0132                                 << "-->year " << stop.year() << "-- month " << stop.month() << "-- day " << stop.day()
0133                                 << "-- hour " << stop.hour() << "-- minute " << stop.minute() << "-- second "
0134                                 << stop.second() << "-- nanosecond " << stop.nanosecond() << std::endl;
0135       boost::posix_time::ptime stop_ptime = stop.time();
0136       boost::posix_time::time_duration stopTimeFromEpoch = stop_ptime - time0;
0137       temp_sum.m_stop_time_str = boost::posix_time::to_iso_extended_string(stop_ptime);
0138       temp_sum.m_stop_time_ll = stopTimeFromEpoch.total_microseconds();
0139       std::ostringstream osstop;
0140       osstop << "[RunInfoRead::" << __func__ << "]: Timestamp for stop of run " << r_number << std::endl
0141              << "Posix time: " << stop_ptime << std::endl
0142              << "ISO string: " << temp_sum.m_stop_time_str << std::endl
0143              << "Microsecond since Epoch (UTC): " << temp_sum.m_stop_time_ll;
0144       edm::LogInfo("RunInfoReader") << osstop.str() << std::endl;
0145     } else {
0146       edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: run " << r_number << " stop time not found."
0147                                     << std::endl;
0148       temp_sum.m_stop_time_str = "null";
0149       temp_sum.m_stop_time_ll = -1;
0150     }
0151 
0152     //new query for obtaining the list of FEDs included in the run
0153     query.reset(schema.newQuery());
0154     query->addToTableList(sParameterTable);
0155     query->addToTableList(sStringTable);
0156     query->addToOutputList(sValueDataColumn);
0157     query->defineOutputType(sValueDataColumn, "string");
0158     std::string fedWhereClause(sRunNumberParameterColumn + std::string("=:n_run AND ") + sNameParameterColumn +
0159                                std::string("='CMS.LVL0:FED_ENABLE_MASK' AND ") + sIdParameterColumn + std::string("=") +
0160                                sRunSessionParameterIdDataColumn);
0161     query->setCondition(fedWhereClause, runNumberBindVariableList);
0162     coral::ICursor& fedCursor = query->execute();
0163     std::string fed;
0164     if (fedCursor.next()) {
0165       std::ostringstream osfeddebug;
0166       fedCursor.currentRow().toOutputStream(osfeddebug);
0167       LogDebug("RunInfoReader") << osfeddebug.str() << std::endl;
0168       const coral::AttributeList& row = fedCursor.currentRow();
0169       fed = row[sValueDataColumn].data<std::string>();
0170     } else {
0171       edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: run " << r_number << "has no FED included."
0172                                     << std::endl;
0173       fed = "null";
0174     }
0175     std::replace(fed.begin(), fed.end(), '%', ' ');
0176     std::stringstream stream(fed);
0177     for (;;) {
0178       std::string word;
0179       if (!(stream >> word)) {
0180         break;
0181       }
0182       std::replace(word.begin(), word.end(), '&', ' ');
0183       std::stringstream ss(word);
0184       int fedNumber;
0185       int val;
0186       ss >> fedNumber >> val;
0187       LogDebug("RunInfoReader") << "FED: " << fedNumber << " --> value: " << val << std::endl;
0188       //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....)
0189       if ((val & 0001) == 1 && (val != 5) && (val != 7))
0190         temp_sum.m_fed_in.push_back(fedNumber);
0191     }
0192     std::ostringstream osfed;
0193     osfed << "[RunInfoRead::" << __func__ << "]: feds included in run " << r_number << ": ";
0194     std::copy(temp_sum.m_fed_in.begin(), temp_sum.m_fed_in.end(), std::ostream_iterator<int>(osfed, ", "));
0195     edm::LogInfo("RunInfoReader") << osfed.str() << std::endl;
0196 
0197     //we connect now to the DCS schema in order to retrieve the magnet current
0198     edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Accessing schema " << dcsenv_schema
0199                                   << std::endl;
0200     coral::ISchema& schema2 = session->schema(dcsenv_schema);
0201     query.reset(schema2.tableHandle(sDCSMagnetTable).newQuery());
0202     query->addToOutputList(squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn);
0203     query->addToOutputList(sDCSMagnetChangeDateColumn);
0204     coral::AttributeList magnetDataOutput;
0205     magnetDataOutput.extend<float>(sDCSMagnetCurrentColumn);
0206     magnetDataOutput.extend<coral::TimeStamp>(sDCSMagnetChangeDateColumn);
0207     query->defineOutput(magnetDataOutput);
0208     //condition
0209     coral::AttributeList magnetCurrentBindVariableList;
0210     float last_current = -1;
0211     magnetCurrentBindVariableList.extend<coral::TimeStamp>("runstart_time");
0212     magnetCurrentBindVariableList["runstart_time"].data<coral::TimeStamp>() = start;
0213     std::string magnetCurrentWhereClause;
0214     if (temp_sum.m_stop_time_str != "null") {
0215       edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
0216                                     << "]: Accessing the magnet currents measured during run " << r_number
0217                                     << " between " << temp_sum.m_start_time_str << " and " << temp_sum.m_stop_time_str
0218                                     << std::endl;
0219       magnetCurrentBindVariableList.extend<coral::TimeStamp>("runstop_time");
0220       magnetCurrentBindVariableList["runstop_time"].data<coral::TimeStamp>() = stop;
0221       magnetCurrentWhereClause = std::string("NOT ") + squoted(sDCSMagnetCurrentColumn) + std::string(" IS NULL AND ") +
0222                                  sDCSMagnetChangeDateColumn + std::string(">:runstart_time AND ") +
0223                                  sDCSMagnetChangeDateColumn + std::string("<:runstop_time");
0224     } else {
0225       edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
0226                                     << "]: Accessing the magnet currents measured before run " << r_number
0227                                     << "start time " << temp_sum.m_start_time_str << std::endl;
0228       magnetCurrentWhereClause = std::string("NOT ") + squoted(sDCSMagnetCurrentColumn) + std::string(" IS NULL AND ") +
0229                                  sDCSMagnetChangeDateColumn + std::string("<:runstart_time");
0230     }
0231     query->setCondition(magnetCurrentWhereClause, magnetCurrentBindVariableList);
0232     query->addToOrderList(sDCSMagnetChangeDateColumn + std::string(" DESC"));
0233     query->limitReturnedRows(10000);
0234     coral::ICursor& magnetCurrentCursor = query->execute();
0235     coral::TimeStamp lastCurrentDate;
0236     std::string last_date;
0237     std::vector<double> time_curr;
0238 
0239     bool changeFound = false;
0240     // first process the changes found within the run boundaries
0241     while (magnetCurrentCursor.next()) {
0242       std::ostringstream oscurrentdebug;
0243       magnetCurrentCursor.currentRow().toOutputStream(oscurrentdebug);
0244       LogDebug("RunInfoReader") << oscurrentdebug.str() << std::endl;
0245       const coral::AttributeList& row = magnetCurrentCursor.currentRow();
0246       lastCurrentDate = row[sDCSMagnetChangeDateColumn].data<coral::TimeStamp>();
0247       temp_sum.m_current.push_back(row[sDCSMagnetCurrentColumn].data<float>());
0248       changeFound = true;
0249       if (temp_sum.m_stop_time_str == "null")
0250         break;
0251       LogDebug("RunInfoReader") << "  last current time extracted == "
0252                                 << "-->year " << lastCurrentDate.year() << "-- month " << lastCurrentDate.month()
0253                                 << "-- day " << lastCurrentDate.day() << "-- hour " << lastCurrentDate.hour()
0254                                 << "-- minute " << lastCurrentDate.minute() << "-- second " << lastCurrentDate.second()
0255                                 << "-- nanosecond " << lastCurrentDate.nanosecond() << std::endl;
0256       boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
0257       boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime - time0;
0258       last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
0259       long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
0260       time_curr.push_back(last_date_ll);
0261       std::ostringstream ostrans;
0262       ostrans << "[RunInfoRead::" << __func__ << "]: Transition of the magnet current " << std::endl
0263               << "New value: " << row[sDCSMagnetCurrentColumn].data<float>() << std::endl
0264               << "Posix time for the transition timestamp: " << lastCurrentDate_ptime << std::endl
0265               << "ISO string for the transition timestamp: " << last_date << std::endl
0266               << "Microseconds since Epoch (UTC) for the transition timestamp: " << last_date_ll;
0267       edm::LogInfo("RunInfoReader") << ostrans.str() << std::endl;
0268     }
0269 
0270     // if not change is found within run boundaries, search for the most recent change
0271     if (!changeFound) {
0272       // we should deal with stable currents... so the query is returning no value and we should take the last modified current value...
0273       edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
0274                                     << "]: The magnet current did not change during run " << r_number
0275                                     << ". Looking for the most recent change before " << temp_sum.m_stop_time_str
0276                                     << std::endl;
0277       Bnotchanged = true;
0278       std::unique_ptr<coral::IQuery> lastValueQuery(schema2.tableHandle(sDCSMagnetTable).newQuery());
0279       lastValueQuery->addToOutputList(squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn);
0280       lastValueQuery->defineOutputType(sDCSMagnetCurrentColumn, "float");
0281       coral::AttributeList lastValueBindVariableList;
0282       lastValueBindVariableList.extend<coral::TimeStamp>("runstop_time");
0283       lastValueBindVariableList["runstop_time"].data<coral::TimeStamp>() = stop;
0284       std::string lastValueWhereClause(std::string(" NOT ") + squoted(sDCSMagnetCurrentColumn) +
0285                                        std::string(" IS NULL AND ") + sDCSMagnetChangeDateColumn +
0286                                        std::string(" <:runstop_time"));
0287       lastValueQuery->setCondition(lastValueWhereClause, lastValueBindVariableList);
0288       lastValueQuery->addToOrderList(sDCSMagnetChangeDateColumn + std::string(" DESC"));
0289       coral::ICursor& lastValueCursor = lastValueQuery->execute();
0290       if (lastValueCursor.next()) {
0291         std::ostringstream oslastvaluedebug;
0292         lastValueCursor.currentRow().toOutputStream(oslastvaluedebug);
0293         LogDebug("RunInfoReader") << oslastvaluedebug.str() << std::endl;
0294         const coral::AttributeList& row = lastValueCursor.currentRow();
0295         last_current = row[sDCSMagnetCurrentColumn].data<float>();
0296         edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
0297                                       << "]: Magnet current of previos run(s), not changed during run " << r_number
0298                                       << ": " << last_current << std::endl;
0299       }
0300       temp_sum.m_avg_current = last_current;
0301       temp_sum.m_min_current = last_current;
0302       temp_sum.m_max_current = last_current;
0303       temp_sum.m_stop_current = last_current;
0304       temp_sum.m_start_current = last_current;
0305     }
0306     size_t csize = temp_sum.m_current.size();
0307     size_t tsize = time_curr.size();
0308     edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: size of time: " << tsize
0309                                   << ", size of currents: " << csize << std::endl;
0310     if (csize != tsize) {
0311       edm::LogWarning("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: current and time not filled correctly."
0312                                        << std::endl;
0313     }
0314     if (tsize > 1) {
0315       temp_sum.m_run_intervall_micros = time_curr.front() - time_curr.back();
0316     } else {
0317       temp_sum.m_run_intervall_micros = 0;
0318     }
0319     edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
0320                                   << "]: Duration in microseconds of the magnet ramp during run " << r_number << ": "
0321                                   << temp_sum.m_run_intervall_micros << std::endl;
0322 
0323     double wi = 0;
0324     double sumwixi = 0;
0325     double sumwi = 0;
0326     float min = -1;
0327     float max = -1;
0328 
0329     if (csize != 0) {
0330       min = temp_sum.m_current.front();
0331       max = temp_sum.m_current.front();
0332       for (size_t i = 0; i < csize; ++i) {
0333         if ((tsize > 1) && (i < csize - 1)) {
0334           wi = (time_curr[i] - time_curr[i + 1]);
0335           temp_sum.m_times_of_currents.push_back(wi);
0336           sumwixi += wi * temp_sum.m_current[i];
0337           sumwi += wi;
0338         }
0339         min = std::min(min, temp_sum.m_current[i]);
0340         max = std::max(max, temp_sum.m_current[i]);
0341       }
0342       std::ostringstream oswi;
0343       oswi << "[RunInfoRead::" << __func__ << "]: Duration of current values in run " << r_number << ": ";
0344       std::copy(temp_sum.m_times_of_currents.begin(),
0345                 temp_sum.m_times_of_currents.end(),
0346                 std::ostream_iterator<float>(oswi, ", "));
0347       edm::LogInfo("RunInfoReader") << oswi.str() << std::endl;
0348       temp_sum.m_start_current = temp_sum.m_current.back();
0349       LogDebug("RunInfoReader") << "start current: " << temp_sum.m_start_current << std::endl;
0350       temp_sum.m_stop_current = temp_sum.m_current.front();
0351       LogDebug("RunInfoReader") << "stop current: " << temp_sum.m_stop_current << std::endl;
0352       if (tsize > 1) {
0353         temp_sum.m_avg_current = sumwixi / sumwi;
0354       } else {
0355         temp_sum.m_avg_current = temp_sum.m_start_current;
0356       }
0357       LogDebug("RunInfoReader") << "average current: " << temp_sum.m_avg_current << std::endl;
0358       temp_sum.m_max_current = max;
0359       LogDebug("RunInfoReader") << "maximum current: " << temp_sum.m_max_current << std::endl;
0360       temp_sum.m_min_current = min;
0361       LogDebug("RunInfoReader") << "minimum current: " << temp_sum.m_min_current << std::endl;
0362     } else {
0363       if (!Bnotchanged) {
0364         edm::LogWarning("RunInfoReader") << "Inserting fake values for magnet current." << std::endl;
0365         temp_sum.m_avg_current = -1;
0366         temp_sum.m_min_current = -1;
0367         temp_sum.m_max_current = -1;
0368         temp_sum.m_stop_current = -1;
0369         temp_sum.m_start_current = -1;
0370       }
0371     }
0372     std::ostringstream oscurr;
0373     oscurr << "[RunInfoRead::" << __func__ << "]: Values of currents for run " << r_number << std::endl;
0374     oscurr << "Average current (A): " << temp_sum.m_avg_current << std::endl;
0375     oscurr << "Minimum current (A): " << temp_sum.m_min_current << std::endl;
0376     oscurr << "Maximum current (A): " << temp_sum.m_max_current << std::endl;
0377     oscurr << "Current at run stop (A): " << temp_sum.m_stop_current << std::endl;
0378     oscurr << "Current at run start (A): " << temp_sum.m_start_current;
0379     edm::LogInfo("RunInfoReader") << oscurr.str() << std::endl;
0380 
0381     session->transaction().commit();
0382   } catch (const std::exception& e) {
0383     throw cms::Exception("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: "
0384                                           << "Unable to create a RunInfo payload. Original Exception:\n"
0385                                           << e.what() << std::endl;
0386   }
0387 
0388   return temp_sum;
0389 }