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 ) {
61 bool Bnotchanged =
false;
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;
253 bool changeFound =
false;
255 while( magnetCurrentCursor.next() ) {
256 std::ostringstream oscurrentdebug;
257 magnetCurrentCursor.currentRow().toOutputStream( oscurrentdebug );
258 LogDebug(
"RunInfoReader" ) << oscurrentdebug.str() << std::endl;
259 const coral::AttributeList& row = magnetCurrentCursor.currentRow();
260 lastCurrentDate = row[sDCSMagnetChangeDateColumn].data<coral::TimeStamp>();
261 temp_sum.
m_current.push_back( row[sDCSMagnetCurrentColumn].data<float>() );
264 LogDebug(
"RunInfoReader" ) <<
" last current time extracted == " 265 <<
"-->year " << lastCurrentDate.year()
266 <<
"-- month " << lastCurrentDate.month()
267 <<
"-- day " << lastCurrentDate.day()
268 <<
"-- hour " << lastCurrentDate.hour()
269 <<
"-- minute " << lastCurrentDate.minute()
270 <<
"-- second " << lastCurrentDate.second()
271 <<
"-- nanosecond " << lastCurrentDate.nanosecond()
273 boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
274 boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime -
time0;
275 last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
276 long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
277 time_curr.push_back(last_date_ll);
278 std::ostringstream ostrans;
279 ostrans <<
"[RunInfoRead::" << __func__ <<
"]: Transition of the magnet current " << std::endl
280 <<
"New value: " << row[sDCSMagnetCurrentColumn].data<
float>() << std::endl
281 <<
"Posix time for the transition timestamp: " << lastCurrentDate_ptime << std::endl
282 <<
"ISO string for the transition timestamp: " << last_date << std::endl
283 <<
"Microseconds since Epoch (UTC) for the transition timestamp: " << last_date_ll;
284 edm::LogInfo(
"RunInfoReader" ) << ostrans.str() << std::endl;
290 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: The magnet current did not change during run " << r_number
291 <<
". Looking for the most recent change before " << temp_sum.
m_stop_time_str << std::endl;
293 std::unique_ptr<coral::IQuery> lastValueQuery( schema2.tableHandle(sDCSMagnetTable).newQuery() );
294 lastValueQuery->addToOutputList( squoted(sDCSMagnetCurrentColumn), sDCSMagnetCurrentColumn );
295 lastValueQuery->defineOutputType( sDCSMagnetCurrentColumn,
"float" );
296 coral::AttributeList lastValueBindVariableList;
297 lastValueBindVariableList.extend<coral::TimeStamp>(
"runstop_time" );
298 lastValueBindVariableList[
"runstop_time" ].data<coral::TimeStamp>() = stop;
300 + sDCSMagnetChangeDateColumn +
std::string(
" <:runstop_time" ) );
301 lastValueQuery->setCondition( lastValueWhereClause, lastValueBindVariableList );
302 lastValueQuery->addToOrderList( sDCSMagnetChangeDateColumn +
std::string(
" DESC" ) );
303 coral::ICursor& lastValueCursor = lastValueQuery->execute();
304 if( lastValueCursor.next() ) {
305 std::ostringstream oslastvaluedebug;
306 lastValueCursor.currentRow().toOutputStream( oslastvaluedebug );
307 LogDebug(
"RunInfoReader" ) << oslastvaluedebug.str() << std::endl;
308 const coral::AttributeList& row = lastValueCursor.currentRow();
309 last_current = row[sDCSMagnetCurrentColumn].data<
float>();
310 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Magnet current of previos run(s), not changed during run " << r_number
311 <<
": " << last_current << std::endl;
319 size_t csize = temp_sum.
m_current.size();
320 size_t tsize = time_curr.size();
321 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: size of time: " << tsize
322 <<
", size of currents: " << csize << std::endl;
324 edm::LogWarning(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: current and time not filled correctly." << std::endl;
331 edm::LogInfo(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: Duration in microseconds of the magnet ramp during run " << r_number
343 for(
size_t i = 0;
i < csize; ++
i) {
344 if( (tsize > 1) && (
i < csize - 1 ) ) {
345 wi = (time_curr[
i] - time_curr[
i+1]);
353 std::ostringstream oswi;
354 oswi <<
"[RunInfoRead::" << __func__ <<
"]: Duration of current values in run " << r_number <<
": ";
356 edm::LogInfo(
"RunInfoReader" ) << oswi.str() << std::endl;
373 edm::LogWarning(
"RunInfoReader" ) <<
"Inserting fake values for magnet current." << std::endl;
381 std::ostringstream oscurr;
382 oscurr <<
"[RunInfoRead::" << __func__ <<
"]: Values of currents for run " << r_number << std::endl;
383 oscurr <<
"Average current (A): " << temp_sum.
m_avg_current << std::endl;
384 oscurr <<
"Minimum current (A): " << temp_sum.
m_min_current << std::endl;
385 oscurr <<
"Maximum current (A): " << temp_sum.
m_max_current << std::endl;
386 oscurr <<
"Current at run stop (A): " << temp_sum.
m_stop_current << std::endl;
388 edm::LogInfo(
"RunInfoReader" ) << oscurr.str() << std::endl;
390 session->transaction().commit();
393 throw cms::Exception(
"RunInfoReader" ) <<
"[RunInfoRead::" << __func__ <<
"]: " 394 <<
"Unable to create a RunInfo payload. Original Exception:\n" 395 << e.what() << std::endl;
const boost::posix_time::ptime time0
RunInfoRead(const std::string &connectionString, const edm::ParameterSet &connectionPset)
def replace(string, replacements)
def query(query_str, verbose=False)
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
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
long long m_start_time_ll