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 std::stringstream errMsg;
121 errMsg <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number <<
" start time not found.";
122 throw std::runtime_error(errMsg.str());
126 query.reset( schema.newQuery() );
127 query->addToTableList( sParameterTable );
128 query->addToTableList( sDateTable );
129 query->addToOutputList( sValueDataColumn );
130 query->defineOutput( runTimeDataOutput );
132 + sNameParameterColumn +
std::string(
"='CMS.LVL0:STOP_TIME_T' AND " )
133 + sIdParameterColumn +
std::string(
"=" ) + sRunSessionParameterIdDataColumn );
134 query->setCondition( runStopWhereClause, runNumberBindVariableList );
135 coral::ICursor& runStopCursor =
query->execute();
136 coral::TimeStamp stop;
137 if( runStopCursor.next() ) {
138 std::ostringstream osstopdebug;
139 runStopCursor.currentRow().toOutputStream( osstopdebug );
140 LogDebug(
"RunInfoReader" ) << osstopdebug.str() << std::endl;
141 const coral::AttributeList& row = runStopCursor.currentRow();
142 stop = row[ sValueDataColumn ].data<coral::TimeStamp>();
143 LogDebug(
"RunInfoReader" ) <<
"stop time extracted == " 144 <<
"-->year " << stop.year()
145 <<
"-- month " << stop.month()
146 <<
"-- day " << stop.day()
147 <<
"-- hour " << stop.hour()
148 <<
"-- minute " << stop.minute()
149 <<
"-- second " << stop.second()
150 <<
"-- nanosecond " << stop.nanosecond()
152 boost::posix_time::ptime stop_ptime = stop.time();
153 boost::posix_time::time_duration stopTimeFromEpoch = stop_ptime -
time0;
154 temp_sum.
m_stop_time_str = boost::posix_time::to_iso_extended_string(stop_ptime);
156 std::ostringstream osstop;
157 osstop <<
"[RunInfoRead::" << __func__ <<
"]: Timestamp for stop of run " << r_number << std::endl
158 <<
"Posix time: " << stop_ptime << std::endl
161 edm::LogInfo(
"RunInfoReader" ) << osstop.str() << std::endl;
164 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number
165 <<
" stop time not found." << std::endl;
171 query.reset( schema.newQuery() );
172 query->addToTableList( sParameterTable );
173 query->addToTableList( sStringTable );
174 query->addToOutputList( sValueDataColumn );
175 query->defineOutputType( sValueDataColumn,
"string" );
177 + sNameParameterColumn +
std::string(
"='CMS.LVL0:FED_ENABLE_MASK' AND " )
178 + sIdParameterColumn +
std::string(
"=" ) + sRunSessionParameterIdDataColumn );
179 query->setCondition( fedWhereClause, runNumberBindVariableList );
180 coral::ICursor& fedCursor =
query->execute();
182 if ( fedCursor.next() ) {
183 std::ostringstream osfeddebug;
184 fedCursor.currentRow().toOutputStream( osfeddebug );
185 LogDebug(
"RunInfoReader" ) << osfeddebug.str() << std::endl;
186 const coral::AttributeList& row = fedCursor.currentRow();
190 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: run " << r_number
191 <<
"has no FED included." << std::endl;
195 std::stringstream stream(fed);
198 if ( !(stream >> word) ){
break;}
200 std::stringstream ss(word);
203 ss >> fedNumber >>
val;
204 LogDebug(
"RunInfoReader" ) <<
"FED: " << fedNumber <<
" --> value: " << val << std::endl;
206 if( (val & 0001) == 1 && (val != 5) && (val != 7) )
207 temp_sum.
m_fed_in.push_back(fedNumber);
209 std::ostringstream osfed;
210 osfed <<
"[RunInfoRead::" << __func__ <<
"]: feds included in run " << r_number <<
": ";
212 edm::LogInfo(
"RunInfoReader" ) << osfed.str() << std::endl;
215 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing schema " << dcsenv_schema << std::endl;
216 coral::ISchema& schema2 = session->schema( dcsenv_schema );
217 query.reset( schema2.tableHandle( sDCSMagnetTable ).newQuery() );
218 query->addToOutputList( squoted( sDCSMagnetCurrentColumn ), sDCSMagnetCurrentColumn );
219 query->addToOutputList( sDCSMagnetChangeDateColumn );
220 coral::AttributeList magnetDataOutput;
221 magnetDataOutput.extend<
float>( sDCSMagnetCurrentColumn );
222 magnetDataOutput.extend<coral::TimeStamp>( sDCSMagnetChangeDateColumn );
223 query->defineOutput( magnetDataOutput );
225 coral::AttributeList magnetCurrentBindVariableList;
226 float last_current = -1;
227 magnetCurrentBindVariableList.extend<coral::TimeStamp>(
"runstart_time" );
228 magnetCurrentBindVariableList[
"runstart_time" ].data<coral::TimeStamp>() = start;
231 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing the magnet currents measured during run " << r_number
234 magnetCurrentBindVariableList.extend<coral::TimeStamp>(
"runstop_time" );
235 magnetCurrentBindVariableList[
"runstop_time" ].data<coral::TimeStamp>() = stop;
236 magnetCurrentWhereClause =
std::string(
"NOT " ) + squoted(sDCSMagnetCurrentColumn) +
std::string(
" IS NULL AND " )
237 + sDCSMagnetChangeDateColumn +
std::string(
">:runstart_time AND " )
238 + sDCSMagnetChangeDateColumn +
std::string(
"<:runstop_time" );
240 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Accessing the magnet currents measured before run " << r_number
242 magnetCurrentWhereClause =
std::string(
"NOT " ) + squoted(sDCSMagnetCurrentColumn) +
std::string(
" IS NULL AND " )
243 + sDCSMagnetChangeDateColumn +
std::string(
"<:runstart_time" );
245 query->setCondition( magnetCurrentWhereClause, magnetCurrentBindVariableList );
246 query->addToOrderList( sDCSMagnetChangeDateColumn +
std::string(
" DESC" ) );
247 query->limitReturnedRows( 10000 );
248 coral::ICursor& magnetCurrentCursor =
query->execute();
249 coral::TimeStamp lastCurrentDate;
251 std::vector<double> time_curr;
252 if ( !magnetCurrentCursor.next() ) {
254 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: The magnet current did not change during run " << r_number
255 <<
". Looking for the most recent change before " << temp_sum.
m_stop_time_str << std::endl;
257 std::unique_ptr<coral::IQuery> lastValueQuery( schema2.tableHandle(sDCSMagnetTable).newQuery() );
258 lastValueQuery->addToOutputList( squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn );
259 lastValueQuery->defineOutputType( sDCSMagnetCurrentColumn,
"float" );
260 coral::AttributeList lastValueBindVariableList;
261 lastValueBindVariableList.extend<coral::TimeStamp>(
"runstop_time" );
262 lastValueBindVariableList[
"runstop_time" ].data<coral::TimeStamp>() = stop;
264 + sDCSMagnetChangeDateColumn +
std::string(
" <:runstop_time" ) );
265 lastValueQuery->setCondition( lastValueWhereClause, lastValueBindVariableList );
266 lastValueQuery->addToOrderList( sDCSMagnetChangeDateColumn +
std::string(
" DESC" ) );
267 coral::ICursor& lastValueCursor = lastValueQuery->execute();
268 if( lastValueCursor.next() ) {
269 std::ostringstream oslastvaluedebug;
270 lastValueCursor.currentRow().toOutputStream( oslastvaluedebug );
271 LogDebug(
"RunInfoReader" ) << oslastvaluedebug.str() << std::endl;
272 const coral::AttributeList& row = lastValueCursor.currentRow();
273 last_current = row[sDCSMagnetCurrentColumn].data<
float>();
274 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Magnet current of previos run(s), not changed during run " << r_number
275 <<
": " << last_current << std::endl;
283 while( magnetCurrentCursor.next() ) {
284 std::ostringstream oscurrentdebug;
285 magnetCurrentCursor.currentRow().toOutputStream( oscurrentdebug );
286 LogDebug(
"RunInfoReader" ) << oscurrentdebug.str() << std::endl;
287 const coral::AttributeList& row = magnetCurrentCursor.currentRow();
288 lastCurrentDate = row[sDCSMagnetChangeDateColumn].data<coral::TimeStamp>();
289 temp_sum.
m_current.push_back( row[sDCSMagnetCurrentColumn].data<float>() );
291 LogDebug(
"RunInfoReader" ) <<
" last current time extracted == " 292 <<
"-->year " << lastCurrentDate.year()
293 <<
"-- month " << lastCurrentDate.month()
294 <<
"-- day " << lastCurrentDate.day()
295 <<
"-- hour " << lastCurrentDate.hour()
296 <<
"-- minute " << lastCurrentDate.minute()
297 <<
"-- second " << lastCurrentDate.second()
298 <<
"-- nanosecond " << lastCurrentDate.nanosecond()
300 boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
301 boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime -
time0;
302 last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
303 long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
304 time_curr.push_back(last_date_ll);
305 std::ostringstream ostrans;
306 ostrans <<
"[RunInfoRead::" << __func__ <<
"]: Transition of the magnet current " << std::endl
307 <<
"New value: " << row[sDCSMagnetCurrentColumn].data<
float>() << std::endl
308 <<
"Posix time for the transition timestamp: " << lastCurrentDate_ptime << std::endl
309 <<
"ISO string for the transition timestamp: " << last_date << std::endl
310 <<
"Microseconds since Epoch (UTC) for the transition timestamp: " << last_date_ll;
311 edm::LogInfo(
"RunInfoReader" ) << ostrans.str() << std::endl;
314 size_t csize = temp_sum.
m_current.size();
315 size_t tsize = time_curr.size();
316 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: size of time: " << tsize
317 <<
", size of currents: " << csize << std::endl;
319 edm::LogWarning(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: current and time not filled correctly." << std::endl;
326 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Duration in microseconds of the magnet ramp during run " << r_number
338 for(
size_t i = 0;
i < csize; ++
i) {
339 if( (tsize > 1) && (
i < csize - 1 ) ) {
340 wi = (time_curr[
i] - time_curr[
i+1]);
348 std::ostringstream oswi;
349 oswi <<
"[RunInfoRead::" << __func__ <<
"]: Duration of current values in run " << r_number <<
": ";
351 edm::LogInfo(
"RunInfoReader" ) << oswi.str() << std::endl;
368 edm::LogWarning(
"RunInfoReader" ) <<
"Inserting fake values for magnet current." << std::endl;
376 std::ostringstream oscurr;
377 oscurr <<
"[RunInfoRead::" << __func__ <<
"]: Values of currents for run " << r_number << std::endl;
378 oscurr <<
"Average current (A): " << temp_sum.
m_avg_current << std::endl;
379 oscurr <<
"Minimum current (A): " << temp_sum.
m_min_current << std::endl;
380 oscurr <<
"Maximum current (A): " << temp_sum.
m_max_current << std::endl;
381 oscurr <<
"Current at run stop (A): " << temp_sum.
m_stop_current << std::endl;
383 edm::LogInfo(
"RunInfoReader" ) << oscurr.str() << std::endl;
385 session->transaction().commit();
388 throw cms::Exception(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: " 389 <<
"Unable to create a RunInfo payload. Original Exception:\n" 390 << e.what() << std::endl;
const boost::posix_time::ptime time0
def replace(string, replacements)
def query(query_str, verbose=False)
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
std::string m_stop_time_str
std::string m_connectionString
std::vector< float > m_times_of_currents
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
long long m_start_time_ll