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"
35 std::string sParameterTable(
"RUNSESSION_PARAMETER" );
39 std::string sRunNumberParameterColumn(
"RUNNUMBER" );
41 std::string sRunSessionParameterIdDataColumn(
"RUNSESSION_PARAMETER_ID" );
45 std::string sDCSMagnetChangeDateColumn(
"CHANGE_DATE" );
50 m_connectionString( connectionString )
51 ,m_connectionPset( connectionPset ) {}
58 ,
const int r_number ) {
63 const boost::posix_time::ptime
time0 = boost::posix_time::from_time_t(0);
65 temp_sum.
m_run = r_number;
66 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Initialising Connection Pool" << std::endl;
73 session->transaction().start(
true );
74 coral::ISchema&
schema = session->schema( runinfo_schema );
75 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing schema " << runinfo_schema << std::endl;
77 std::unique_ptr<coral::IQuery>
query( schema.newQuery() );
78 query->addToTableList( sParameterTable );
79 query->addToTableList( sDateTable );
80 query->addToOutputList( sValueDataColumn );
81 coral::AttributeList runTimeDataOutput;
82 runTimeDataOutput.extend<coral::TimeStamp>( sValueDataColumn );
83 query->defineOutput( runTimeDataOutput );
85 + sNameParameterColumn +
std::string(
"='CMS.LVL0:START_TIME_T' AND " )
86 + sIdParameterColumn +
std::string(
"=" ) + sRunSessionParameterIdDataColumn );
87 coral::AttributeList runNumberBindVariableList;
88 runNumberBindVariableList.extend<
int>(
"n_run" );
89 runNumberBindVariableList[
"n_run" ].data<
int>() = r_number;
90 query->setCondition( runStartWhereClause, runNumberBindVariableList );
91 coral::ICursor& runStartCursor =
query->execute();
92 coral::TimeStamp
start;
93 if( runStartCursor.next() ) {
94 std::ostringstream osstartdebug;
95 runStartCursor.currentRow().toOutputStream( osstartdebug );
96 LogDebug(
"RunInfoReader" ) << osstartdebug.str() << std::endl;
97 const coral::AttributeList& row = runStartCursor.currentRow();
98 start = row[ sValueDataColumn ].data<coral::TimeStamp>();
99 LogDebug(
"RunInfoReader" ) <<
"UTC start time extracted == "
100 <<
"-->year " << start.year()
101 <<
"-- month " << start.month()
102 <<
"-- day " << start.day()
103 <<
"-- hour " << start.hour()
104 <<
"-- minute " << start.minute()
105 <<
"-- second " << start.second()
106 <<
"-- nanosecond " << start.nanosecond()
108 boost::posix_time::ptime start_ptime = start.time();
109 boost::posix_time::time_duration startTimeFromEpoch = start_ptime -
time0;
110 temp_sum.
m_start_time_str = boost::posix_time::to_iso_extended_string(start_ptime);
112 std::ostringstream osstart;
113 osstart <<
"[RunInfoRead::" << __func__ <<
"]: Timestamp for start of run " << r_number << std::endl
114 <<
"Posix time: " << start_ptime << std::endl
117 edm::LogInfo(
"RunInfoReader" ) << osstart.str() << std::endl;
120 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number
121 <<
" start time not found." << std::endl;
127 query.reset( schema.newQuery() );
128 query->addToTableList( sParameterTable );
129 query->addToTableList( sDateTable );
130 query->addToOutputList( sValueDataColumn );
131 query->defineOutput( runTimeDataOutput );
133 + sNameParameterColumn +
std::string(
"='CMS.LVL0:STOP_TIME_T' AND " )
134 + sIdParameterColumn +
std::string(
"=" ) + sRunSessionParameterIdDataColumn );
135 query->setCondition( runStopWhereClause, runNumberBindVariableList );
136 coral::ICursor& runStopCursor =
query->execute();
137 coral::TimeStamp stop;
138 if( runStopCursor.next() ) {
139 std::ostringstream osstopdebug;
140 runStopCursor.currentRow().toOutputStream( osstopdebug );
141 LogDebug(
"RunInfoReader" ) << osstopdebug.str() << std::endl;
142 const coral::AttributeList& row = runStopCursor.currentRow();
143 stop = row[ sValueDataColumn ].data<coral::TimeStamp>();
144 LogDebug(
"RunInfoReader" ) <<
"stop time extracted == "
145 <<
"-->year " << stop.year()
146 <<
"-- month " << stop.month()
147 <<
"-- day " << stop.day()
148 <<
"-- hour " << stop.hour()
149 <<
"-- minute " << stop.minute()
150 <<
"-- second " << stop.second()
151 <<
"-- nanosecond " << stop.nanosecond()
153 boost::posix_time::ptime stop_ptime = stop.time();
154 boost::posix_time::time_duration stopTimeFromEpoch = stop_ptime -
time0;
155 temp_sum.
m_stop_time_str = boost::posix_time::to_iso_extended_string(stop_ptime);
157 std::ostringstream osstop;
158 osstop <<
"[RunInfoRead::" << __func__ <<
"]: Timestamp for stop of run " << r_number << std::endl
159 <<
"Posix time: " << stop_ptime << std::endl
162 edm::LogInfo(
"RunInfoReader" ) << osstop.str() << std::endl;
165 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number
166 <<
" stop time not found." << std::endl;
172 query.reset( schema.newQuery() );
173 query->addToTableList( sParameterTable );
174 query->addToTableList( sStringTable );
175 query->addToOutputList( sValueDataColumn );
176 query->defineOutputType( sValueDataColumn,
"string" );
178 + sNameParameterColumn +
std::string(
"='CMS.LVL0:FED_ENABLE_MASK' AND " )
179 + sIdParameterColumn +
std::string(
"=" ) + sRunSessionParameterIdDataColumn );
180 query->setCondition( fedWhereClause, runNumberBindVariableList );
181 coral::ICursor& fedCursor =
query->execute();
183 if ( fedCursor.next() ) {
184 std::ostringstream osfeddebug;
185 fedCursor.currentRow().toOutputStream( osfeddebug );
186 LogDebug(
"RunInfoReader" ) << osfeddebug.str() << std::endl;
187 const coral::AttributeList& row = fedCursor.currentRow();
191 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number
192 <<
"has no FED included." << std::endl;
196 std::stringstream stream(fed);
199 if ( !(stream >> word) ){
break;}
201 std::stringstream
ss(word);
204 ss >> fedNumber >> val;
205 LogDebug(
"RunInfoReader" ) <<
"FED: " << fedNumber <<
" --> value: " << val << std::endl;
207 if( (val & 0001) == 1 && (val != 5) && (val != 7) )
208 temp_sum.
m_fed_in.push_back(fedNumber);
210 std::ostringstream osfed;
211 osfed <<
"[RunInfoRead::" << __func__ <<
"]: feds included in run " << r_number <<
": ";
213 edm::LogInfo(
"RunInfoReader" ) << osfed.str() << std::endl;
216 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing schema " << dcsenv_schema << std::endl;
217 coral::ISchema& schema2 = session->schema( dcsenv_schema );
218 query.reset( schema2.tableHandle( sDCSMagnetTable ).newQuery() );
219 query->addToOutputList( squoted( sDCSMagnetCurrentColumn ), sDCSMagnetCurrentColumn );
220 query->addToOutputList( sDCSMagnetChangeDateColumn );
221 coral::AttributeList magnetDataOutput;
222 magnetDataOutput.extend<
float>( sDCSMagnetCurrentColumn );
223 magnetDataOutput.extend<coral::TimeStamp>( sDCSMagnetChangeDateColumn );
224 query->defineOutput( magnetDataOutput );
226 coral::AttributeList magnetCurrentBindVariableList;
227 float last_current = -1;
228 magnetCurrentBindVariableList.extend<coral::TimeStamp>(
"runstart_time" );
229 magnetCurrentBindVariableList[
"runstart_time" ].data<coral::TimeStamp>() = start;
232 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing the magnet currents measured during run " << r_number
235 magnetCurrentBindVariableList.extend<coral::TimeStamp>(
"runstop_time" );
236 magnetCurrentBindVariableList[
"runstop_time" ].data<coral::TimeStamp>() = stop;
237 magnetCurrentWhereClause =
std::string(
"NOT " ) + squoted(sDCSMagnetCurrentColumn) +
std::string(
" IS NULL AND " )
238 + sDCSMagnetChangeDateColumn +
std::string(
">:runstart_time AND " )
239 + sDCSMagnetChangeDateColumn +
std::string(
"<:runstop_time" );
241 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing the magnet currents measured before run " << r_number
243 magnetCurrentWhereClause =
std::string(
"NOT " ) + squoted(sDCSMagnetCurrentColumn) +
std::string(
" IS NULL AND " )
244 + sDCSMagnetChangeDateColumn +
std::string(
"<:runstart_time" );
246 query->setCondition( magnetCurrentWhereClause, magnetCurrentBindVariableList );
247 query->addToOrderList( sDCSMagnetChangeDateColumn +
std::string(
" DESC" ) );
248 query->limitReturnedRows( 10000 );
249 coral::ICursor& magnetCurrentCursor =
query->execute();
250 coral::TimeStamp lastCurrentDate;
252 std::vector<double> time_curr;
253 if ( !magnetCurrentCursor.next() ) {
255 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: The magnet current did not change during run " << r_number
256 <<
". Looking for the most recent change before " << temp_sum.
m_stop_time_str << std::endl;
258 std::unique_ptr<coral::IQuery> lastValueQuery( schema2.tableHandle(sDCSMagnetTable).newQuery() );
259 lastValueQuery->addToOutputList( squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn );
260 lastValueQuery->defineOutputType( sDCSMagnetCurrentColumn,
"float" );
261 coral::AttributeList lastValueBindVariableList;
262 lastValueBindVariableList.extend<coral::TimeStamp>(
"runstop_time" );
263 lastValueBindVariableList[
"runstop_time" ].data<coral::TimeStamp>() = stop;
265 + sDCSMagnetChangeDateColumn +
std::string(
" <:runstop_time" ) );
266 lastValueQuery->setCondition( lastValueWhereClause, lastValueBindVariableList );
267 lastValueQuery->addToOrderList( sDCSMagnetChangeDateColumn +
std::string(
" DESC" ) );
268 coral::ICursor& lastValueCursor = lastValueQuery->execute();
269 if( lastValueCursor.next() ) {
270 std::ostringstream oslastvaluedebug;
271 lastValueCursor.currentRow().toOutputStream( oslastvaluedebug );
272 LogDebug(
"RunInfoReader" ) << oslastvaluedebug.str() << std::endl;
273 const coral::AttributeList& row = lastValueCursor.currentRow();
274 last_current = row[sDCSMagnetCurrentColumn].data<
float>();
275 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Magnet current of previos run(s), not changed during run " << r_number
276 <<
": " << last_current << std::endl;
284 while( magnetCurrentCursor.next() ) {
285 std::ostringstream oscurrentdebug;
286 magnetCurrentCursor.currentRow().toOutputStream( oscurrentdebug );
287 LogDebug(
"RunInfoReader" ) << oscurrentdebug.str() << std::endl;
288 const coral::AttributeList& row = magnetCurrentCursor.currentRow();
289 lastCurrentDate = row[sDCSMagnetChangeDateColumn].data<coral::TimeStamp>();
290 temp_sum.
m_current.push_back( row[sDCSMagnetCurrentColumn].data<float>() );
292 LogDebug(
"RunInfoReader" ) <<
" last current time extracted == "
293 <<
"-->year " << lastCurrentDate.year()
294 <<
"-- month " << lastCurrentDate.month()
295 <<
"-- day " << lastCurrentDate.day()
296 <<
"-- hour " << lastCurrentDate.hour()
297 <<
"-- minute " << lastCurrentDate.minute()
298 <<
"-- second " << lastCurrentDate.second()
299 <<
"-- nanosecond " << lastCurrentDate.nanosecond()
301 boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
302 boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime -
time0;
303 last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
304 long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
305 time_curr.push_back(last_date_ll);
306 std::ostringstream ostrans;
307 ostrans <<
"[RunInfoRead::" << __func__ <<
"]: Transition of the magnet current " << std::endl
308 <<
"New value: " << row[sDCSMagnetCurrentColumn].data<
float>() << std::endl
309 <<
"Posix time for the transition timestamp: " << lastCurrentDate_ptime << std::endl
310 <<
"ISO string for the transition timestamp: " << last_date << std::endl
311 <<
"Microseconds since Epoch (UTC) for the transition timestamp: " << last_date_ll;
312 edm::LogInfo(
"RunInfoReader" ) << ostrans.str() << std::endl;
315 size_t csize = temp_sum.
m_current.size();
316 size_t tsize = time_curr.size();
317 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: size of time: " << tsize
318 <<
", size of currents: " << csize << std::endl;
320 edm::LogWarning(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: current and time not filled correctly." << std::endl;
327 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Duration in microseconds of the magnet ramp during run " << r_number
339 for(
size_t i = 0;
i < csize; ++
i) {
340 if( (tsize > 1) && (
i < csize - 1 ) ) {
341 wi = (time_curr[
i] - time_curr[
i+1]);
349 std::ostringstream oswi;
350 oswi <<
"[RunInfoRead::" << __func__ <<
"]: Duration of current values in run " << r_number <<
": ";
352 edm::LogInfo(
"RunInfoReader" ) << oswi.str() << std::endl;
369 edm::LogWarning(
"RunInfoReader" ) <<
"Inserting fake values for magnet current." << std::endl;
377 std::ostringstream oscurr;
378 oscurr <<
"[RunInfoRead::" << __func__ <<
"]: Values of currents for run " << r_number << std::endl;
379 oscurr <<
"Average current (A): " << temp_sum.
m_avg_current << std::endl;
380 oscurr <<
"Minimum current (A): " << temp_sum.
m_min_current << std::endl;
381 oscurr <<
"Maximum current (A): " << temp_sum.
m_max_current << std::endl;
382 oscurr <<
"Current at run stop (A): " << temp_sum.
m_stop_current << std::endl;
384 edm::LogInfo(
"RunInfoReader" ) << oscurr.str() << std::endl;
386 session->transaction().commit();
389 throw cms::Exception(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: "
390 <<
"Unable to create a RunInfo payload. Original Exception:\n"
391 << e.what() << std::endl;
tuple start
Check for commandline option errors.
const boost::posix_time::ptime time0
boost::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
RunInfoRead(const std::string &connectionString, const edm::ParameterSet &connectionPset)
RunInfo readData(const std::string &runinfo_schema, const std::string &dcsenv_schema, const int r_number)
void setParameters(const edm::ParameterSet &connectionPset)
std::vector< int > m_fed_in
std::string m_start_time_str
std::vector< float > m_current
edm::ParameterSet m_connectionPset
float m_run_intervall_micros
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
std::string m_stop_time_str
std::string m_connectionString
std::vector< float > m_times_of_currents
long long m_start_time_ll