7 #include "RelationalAccess/ISessionProxy.h"
8 #include "RelationalAccess/ITransaction.h"
9 #include "RelationalAccess/ISchema.h"
10 #include "RelationalAccess/ITable.h"
11 #include "RelationalAccess/IQuery.h"
12 #include "RelationalAccess/ICursor.h"
13 #include "CoralBase/AttributeList.h"
14 #include "CoralBase/Attribute.h"
15 #include "CoralBase/AttributeSpecification.h"
16 #include "CoralBase/TimeStamp.h"
31 std::string sParameterTable(
"RUNSESSION_PARAMETER");
37 std::string sRunSessionParameterIdDataColumn(
"RUNSESSION_PARAMETER_ID");
41 std::string sDCSMagnetChangeDateColumn(
"CHANGE_DATE");
52 bool Bnotchanged =
false;
54 const boost::posix_time::ptime
time0 = boost::posix_time::from_time_t(0);
56 temp_sum.
m_run = r_number;
57 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: Initialising Connection Pool" << std::endl;
61 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: Initialising read-only session to "
65 session->transaction().start(
true);
66 coral::ISchema& schema = session->schema(runinfo_schema);
67 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: Accessing schema " << runinfo_schema
70 std::unique_ptr<coral::IQuery>
query(schema.newQuery());
71 query->addToTableList(sParameterTable);
72 query->addToTableList(sDateTable);
73 query->addToOutputList(sValueDataColumn);
74 coral::AttributeList runTimeDataOutput;
75 runTimeDataOutput.extend<coral::TimeStamp>(sValueDataColumn);
76 query->defineOutput(runTimeDataOutput);
77 std::string runStartWhereClause(sRunNumberParameterColumn +
std::string(
"=:n_run AND ") + sNameParameterColumn +
78 std::string(
"='CMS.LVL0:START_TIME_T' AND ") + sIdParameterColumn +
79 std::string(
"=") + sRunSessionParameterIdDataColumn);
80 coral::AttributeList runNumberBindVariableList;
81 runNumberBindVariableList.extend<
int>(
"n_run");
82 runNumberBindVariableList[
"n_run"].data<
int>() = r_number;
83 query->setCondition(runStartWhereClause, runNumberBindVariableList);
84 coral::ICursor& runStartCursor =
query->execute();
85 coral::TimeStamp
start;
86 if (runStartCursor.next()) {
87 std::ostringstream osstartdebug;
88 runStartCursor.currentRow().toOutputStream(osstartdebug);
89 LogDebug(
"RunInfoReader") << osstartdebug.str() << std::endl;
90 const coral::AttributeList& row = runStartCursor.currentRow();
91 start = row[sValueDataColumn].data<coral::TimeStamp>();
92 LogDebug(
"RunInfoReader") <<
"UTC start time extracted == "
93 <<
"-->year " <<
start.year() <<
"-- month " <<
start.month() <<
"-- day "
94 <<
start.day() <<
"-- hour " <<
start.hour() <<
"-- minute " <<
start.minute()
95 <<
"-- second " <<
start.second() <<
"-- nanosecond " <<
start.nanosecond()
97 boost::posix_time::ptime start_ptime =
start.time();
98 boost::posix_time::time_duration startTimeFromEpoch = start_ptime -
time0;
99 temp_sum.
m_start_time_str = boost::posix_time::to_iso_extended_string(start_ptime);
101 std::ostringstream osstart;
102 osstart <<
"[RunInfoRead::" << __func__ <<
"]: Timestamp for start of run " << r_number << std::endl
103 <<
"Posix time: " << start_ptime << std::endl
106 edm::LogInfo(
"RunInfoReader") << osstart.str() << std::endl;
108 std::stringstream errMsg;
109 errMsg <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number <<
" start time not found.";
110 throw std::runtime_error(errMsg.str());
114 query.reset(schema.newQuery());
115 query->addToTableList(sParameterTable);
116 query->addToTableList(sDateTable);
117 query->addToOutputList(sValueDataColumn);
118 query->defineOutput(runTimeDataOutput);
119 std::string runStopWhereClause(sRunNumberParameterColumn +
std::string(
"=:n_run AND ") + sNameParameterColumn +
121 sRunSessionParameterIdDataColumn);
122 query->setCondition(runStopWhereClause, runNumberBindVariableList);
123 coral::ICursor& runStopCursor =
query->execute();
124 coral::TimeStamp stop;
125 if (runStopCursor.next()) {
126 std::ostringstream osstopdebug;
127 runStopCursor.currentRow().toOutputStream(osstopdebug);
128 LogDebug(
"RunInfoReader") << osstopdebug.str() << std::endl;
129 const coral::AttributeList& row = runStopCursor.currentRow();
130 stop = row[sValueDataColumn].data<coral::TimeStamp>();
131 LogDebug(
"RunInfoReader") <<
"stop time extracted == "
132 <<
"-->year " << stop.year() <<
"-- month " << stop.month() <<
"-- day " << stop.day()
133 <<
"-- hour " << stop.hour() <<
"-- minute " << stop.minute() <<
"-- second "
134 << stop.second() <<
"-- nanosecond " << stop.nanosecond() << std::endl;
135 boost::posix_time::ptime stop_ptime = stop.time();
136 boost::posix_time::time_duration stopTimeFromEpoch = stop_ptime -
time0;
137 temp_sum.
m_stop_time_str = boost::posix_time::to_iso_extended_string(stop_ptime);
139 std::ostringstream osstop;
140 osstop <<
"[RunInfoRead::" << __func__ <<
"]: Timestamp for stop of run " << r_number << std::endl
141 <<
"Posix time: " << stop_ptime << std::endl
144 edm::LogInfo(
"RunInfoReader") << osstop.str() << std::endl;
146 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number <<
" stop time not found."
153 query.reset(schema.newQuery());
154 query->addToTableList(sParameterTable);
155 query->addToTableList(sStringTable);
156 query->addToOutputList(sValueDataColumn);
157 query->defineOutputType(sValueDataColumn,
"string");
158 std::string fedWhereClause(sRunNumberParameterColumn +
std::string(
"=:n_run AND ") + sNameParameterColumn +
160 sRunSessionParameterIdDataColumn);
161 query->setCondition(fedWhereClause, runNumberBindVariableList);
162 coral::ICursor& fedCursor =
query->execute();
164 if (fedCursor.next()) {
165 std::ostringstream osfeddebug;
166 fedCursor.currentRow().toOutputStream(osfeddebug);
167 LogDebug(
"RunInfoReader") << osfeddebug.str() << std::endl;
168 const coral::AttributeList& row = fedCursor.currentRow();
171 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number <<
"has no FED included."
176 std::stringstream
stream(fed);
183 std::stringstream
ss(
word);
186 ss >> fedNumber >>
val;
187 LogDebug(
"RunInfoReader") <<
"FED: " << fedNumber <<
" --> value: " <<
val << std::endl;
189 if ((
val & 0001) == 1 && (
val != 5) && (
val != 7))
190 temp_sum.
m_fed_in.push_back(fedNumber);
192 std::ostringstream osfed;
193 osfed <<
"[RunInfoRead::" << __func__ <<
"]: feds included in run " << r_number <<
": ";
195 edm::LogInfo(
"RunInfoReader") << osfed.str() << std::endl;
198 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: Accessing schema " << dcsenv_schema
200 coral::ISchema& schema2 = session->schema(dcsenv_schema);
201 query.reset(schema2.tableHandle(sDCSMagnetTable).newQuery());
202 query->addToOutputList(squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn);
203 query->addToOutputList(sDCSMagnetChangeDateColumn);
204 coral::AttributeList magnetDataOutput;
205 magnetDataOutput.extend<
float>(sDCSMagnetCurrentColumn);
206 magnetDataOutput.extend<coral::TimeStamp>(sDCSMagnetChangeDateColumn);
207 query->defineOutput(magnetDataOutput);
209 coral::AttributeList magnetCurrentBindVariableList;
210 float last_current = -1;
211 magnetCurrentBindVariableList.extend<coral::TimeStamp>(
"runstart_time");
212 magnetCurrentBindVariableList[
"runstart_time"].data<coral::TimeStamp>() =
start;
215 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__
216 <<
"]: Accessing the magnet currents measured during run " << r_number
219 magnetCurrentBindVariableList.extend<coral::TimeStamp>(
"runstop_time");
220 magnetCurrentBindVariableList[
"runstop_time"].data<coral::TimeStamp>() = stop;
221 magnetCurrentWhereClause =
std::string(
"NOT ") + squoted(sDCSMagnetCurrentColumn) +
std::string(
" IS NULL AND ") +
222 sDCSMagnetChangeDateColumn +
std::string(
">:runstart_time AND ") +
223 sDCSMagnetChangeDateColumn +
std::string(
"<:runstop_time");
225 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__
226 <<
"]: Accessing the magnet currents measured before run " << r_number
228 magnetCurrentWhereClause =
std::string(
"NOT ") + squoted(sDCSMagnetCurrentColumn) +
std::string(
" IS NULL AND ") +
229 sDCSMagnetChangeDateColumn +
std::string(
"<:runstart_time");
231 query->setCondition(magnetCurrentWhereClause, magnetCurrentBindVariableList);
233 query->limitReturnedRows(10000);
234 coral::ICursor& magnetCurrentCursor =
query->execute();
235 coral::TimeStamp lastCurrentDate;
237 std::vector<double> time_curr;
239 bool changeFound =
false;
241 while (magnetCurrentCursor.next()) {
242 std::ostringstream oscurrentdebug;
243 magnetCurrentCursor.currentRow().toOutputStream(oscurrentdebug);
244 LogDebug(
"RunInfoReader") << oscurrentdebug.str() << std::endl;
245 const coral::AttributeList& row = magnetCurrentCursor.currentRow();
246 lastCurrentDate = row[sDCSMagnetChangeDateColumn].data<coral::TimeStamp>();
247 temp_sum.
m_current.push_back(row[sDCSMagnetCurrentColumn].data<float>());
251 LogDebug(
"RunInfoReader") <<
" last current time extracted == "
252 <<
"-->year " << lastCurrentDate.year() <<
"-- month " << lastCurrentDate.month()
253 <<
"-- day " << lastCurrentDate.day() <<
"-- hour " << lastCurrentDate.hour()
254 <<
"-- minute " << lastCurrentDate.minute() <<
"-- second " << lastCurrentDate.second()
255 <<
"-- nanosecond " << lastCurrentDate.nanosecond() << std::endl;
256 boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
257 boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime -
time0;
258 last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
259 long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
260 time_curr.push_back(last_date_ll);
261 std::ostringstream ostrans;
262 ostrans <<
"[RunInfoRead::" << __func__ <<
"]: Transition of the magnet current " << std::endl
263 <<
"New value: " << row[sDCSMagnetCurrentColumn].data<
float>() << std::endl
264 <<
"Posix time for the transition timestamp: " << lastCurrentDate_ptime << std::endl
265 <<
"ISO string for the transition timestamp: " << last_date << std::endl
266 <<
"Microseconds since Epoch (UTC) for the transition timestamp: " << last_date_ll;
267 edm::LogInfo(
"RunInfoReader") << ostrans.str() << std::endl;
273 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__
274 <<
"]: The magnet current did not change during run " << r_number
275 <<
". Looking for the most recent change before " << temp_sum.
m_stop_time_str
278 std::unique_ptr<coral::IQuery> lastValueQuery(schema2.tableHandle(sDCSMagnetTable).newQuery());
279 lastValueQuery->addToOutputList(squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn);
280 lastValueQuery->defineOutputType(sDCSMagnetCurrentColumn,
"float");
281 coral::AttributeList lastValueBindVariableList;
282 lastValueBindVariableList.extend<coral::TimeStamp>(
"runstop_time");
283 lastValueBindVariableList[
"runstop_time"].data<coral::TimeStamp>() = stop;
285 std::string(
" IS NULL AND ") + sDCSMagnetChangeDateColumn +
287 lastValueQuery->setCondition(lastValueWhereClause, lastValueBindVariableList);
288 lastValueQuery->addToOrderList(sDCSMagnetChangeDateColumn +
std::string(
" DESC"));
289 coral::ICursor& lastValueCursor = lastValueQuery->execute();
290 if (lastValueCursor.next()) {
291 std::ostringstream oslastvaluedebug;
292 lastValueCursor.currentRow().toOutputStream(oslastvaluedebug);
293 LogDebug(
"RunInfoReader") << oslastvaluedebug.str() << std::endl;
294 const coral::AttributeList& row = lastValueCursor.currentRow();
295 last_current = row[sDCSMagnetCurrentColumn].data<
float>();
296 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__
297 <<
"]: Magnet current of previos run(s), not changed during run " << r_number
298 <<
": " << last_current << std::endl;
306 size_t csize = temp_sum.
m_current.size();
307 size_t tsize = time_curr.size();
308 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: size of time: " << tsize
309 <<
", size of currents: " << csize << std::endl;
310 if (csize != tsize) {
311 edm::LogWarning(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: current and time not filled correctly."
319 edm::LogInfo(
"RunInfoReader") <<
"[RunInfoRead::" << __func__
320 <<
"]: Duration in microseconds of the magnet ramp during run " << r_number <<
": "
332 for (
size_t i = 0;
i < csize; ++
i) {
333 if ((tsize > 1) && (
i < csize - 1)) {
334 wi = (time_curr[
i] - time_curr[
i + 1]);
342 std::ostringstream oswi;
343 oswi <<
"[RunInfoRead::" << __func__ <<
"]: Duration of current values in run " << r_number <<
": ";
346 std::ostream_iterator<float>(oswi,
", "));
347 edm::LogInfo(
"RunInfoReader") << oswi.str() << std::endl;
364 edm::LogWarning(
"RunInfoReader") <<
"Inserting fake values for magnet current." << std::endl;
372 std::ostringstream oscurr;
373 oscurr <<
"[RunInfoRead::" << __func__ <<
"]: Values of currents for run " << r_number << std::endl;
374 oscurr <<
"Average current (A): " << temp_sum.
m_avg_current << std::endl;
375 oscurr <<
"Minimum current (A): " << temp_sum.
m_min_current << std::endl;
376 oscurr <<
"Maximum current (A): " << temp_sum.
m_max_current << std::endl;
377 oscurr <<
"Current at run stop (A): " << temp_sum.
m_stop_current << std::endl;
379 edm::LogInfo(
"RunInfoReader") << oscurr.str() << std::endl;
381 session->transaction().commit();
383 throw cms::Exception(
"RunInfoReader") <<
"[RunInfoRead::" << __func__ <<
"]: "
384 <<
"Unable to create a RunInfo payload. Original Exception:\n"
385 <<
e.what() << std::endl;