CMS 3D CMS Logo

RunInfoRead.cc
Go to the documentation of this file.
1 //#include "CondFormats/Common/interface/TimeConversions.h"
2 //#include "CondFormats/Common/interface/Time.h"
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"
17 #include <algorithm>
18 #include <iostream>
19 #include <iterator>
20 #include <memory>
21 #include <stdexcept>
22 #include <vector>
23 #include <cmath>
24 
25 namespace {
26  const std::string dot(".");
27  const std::string quote("\"");
28  const std::string bNOTb(" NOT ");
29  const std::string squoted(const std::string& s) { return quote + s + quote; }
30  //now strings for the tables and columns to be queried
31  const std::string sParameterTable("RUNSESSION_PARAMETER");
32  const std::string sDateTable("RUNSESSION_DATE");
33  const std::string sStringTable("RUNSESSION_STRING");
34  const std::string sIdParameterColumn("ID");
35  const std::string sRunNumberParameterColumn("RUNNUMBER");
36  const std::string sNameParameterColumn("NAME");
37  const std::string sRunSessionParameterIdDataColumn("RUNSESSION_PARAMETER_ID");
38  const std::string sValueDataColumn("VALUE");
39  const std::string sDCSMagnetTable("CMSFWMAGNET");
40  const std::string sDCSMagnetCurrentColumn("CURRENT");
41  const std::string sDCSMagnetChangeDateColumn("CHANGE_DATE");
42 } // namespace
43 
45  : m_connectionString(connectionString), m_connectionPset(connectionPset) {}
46 
48 
49 RunInfo RunInfoRead::readData(const std::string& runinfo_schema, const std::string& dcsenv_schema, const int r_number) {
50  RunInfo temp_sum;
51  //for B currents...
52  bool Bnotchanged = false;
53  //from TimeConversions.h
54  const boost::posix_time::ptime time0 = boost::posix_time::from_time_t(0);
55  //if cursor is null setting null values
56  temp_sum.m_run = r_number;
57  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Initialising Connection Pool" << std::endl;
59  connection.setParameters(m_connectionPset);
60  connection.configure();
61  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Initialising read-only session to "
62  << m_connectionString << std::endl;
63  std::shared_ptr<coral::ISessionProxy> session = connection.createCoralSession(m_connectionString, false);
64  try {
65  session->transaction().start(true);
66  coral::ISchema& schema = session->schema(runinfo_schema);
67  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Accessing schema " << runinfo_schema
68  << std::endl;
69  //new query to obtain the start_time
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; //now all times are UTC!
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()
96  << std::endl;
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);
100  temp_sum.m_start_time_ll = startTimeFromEpoch.total_microseconds();
101  std::ostringstream osstart;
102  osstart << "[RunInfoRead::" << __func__ << "]: Timestamp for start of run " << r_number << std::endl
103  << "Posix time: " << start_ptime << std::endl
104  << "ISO string: " << temp_sum.m_start_time_str << std::endl
105  << "Microsecond since Epoch (UTC): " << temp_sum.m_start_time_ll;
106  edm::LogInfo("RunInfoReader") << osstart.str() << std::endl;
107  } else {
108  std::stringstream errMsg;
109  errMsg << "[RunInfoRead::" << __func__ << "]: run " << r_number << " start time not found.";
110  throw std::runtime_error(errMsg.str());
111  }
112 
113  //new query to obtain the stop_time
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 +
120  std::string("='CMS.LVL0:STOP_TIME_T' AND ") + sIdParameterColumn + std::string("=") +
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);
138  temp_sum.m_stop_time_ll = stopTimeFromEpoch.total_microseconds();
139  std::ostringstream osstop;
140  osstop << "[RunInfoRead::" << __func__ << "]: Timestamp for stop of run " << r_number << std::endl
141  << "Posix time: " << stop_ptime << std::endl
142  << "ISO string: " << temp_sum.m_stop_time_str << std::endl
143  << "Microsecond since Epoch (UTC): " << temp_sum.m_stop_time_ll;
144  edm::LogInfo("RunInfoReader") << osstop.str() << std::endl;
145  } else {
146  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: run " << r_number << " stop time not found."
147  << std::endl;
148  temp_sum.m_stop_time_str = "null";
149  temp_sum.m_stop_time_ll = -1;
150  }
151 
152  //new query for obtaining the list of FEDs included in the run
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 +
159  std::string("='CMS.LVL0:FED_ENABLE_MASK' AND ") + sIdParameterColumn + std::string("=") +
160  sRunSessionParameterIdDataColumn);
161  query->setCondition(fedWhereClause, runNumberBindVariableList);
162  coral::ICursor& fedCursor = query->execute();
163  std::string fed;
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();
169  fed = row[sValueDataColumn].data<std::string>();
170  } else {
171  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: run " << r_number << "has no FED included."
172  << std::endl;
173  fed = "null";
174  }
175  std::replace(fed.begin(), fed.end(), '%', ' ');
176  std::stringstream stream(fed);
177  for (;;) {
179  if (!(stream >> word)) {
180  break;
181  }
182  std::replace(word.begin(), word.end(), '&', ' ');
183  std::stringstream ss(word);
184  int fedNumber;
185  int val;
186  ss >> fedNumber >> val;
187  LogDebug("RunInfoReader") << "FED: " << fedNumber << " --> value: " << val << std::endl;
188  //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....)
189  if ((val & 0001) == 1 && (val != 5) && (val != 7))
190  temp_sum.m_fed_in.push_back(fedNumber);
191  }
192  std::ostringstream osfed;
193  osfed << "[RunInfoRead::" << __func__ << "]: feds included in run " << r_number << ": ";
194  std::copy(temp_sum.m_fed_in.begin(), temp_sum.m_fed_in.end(), std::ostream_iterator<int>(osfed, ", "));
195  edm::LogInfo("RunInfoReader") << osfed.str() << std::endl;
196 
197  //we connect now to the DCS schema in order to retrieve the magnet current
198  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: Accessing schema " << dcsenv_schema
199  << std::endl;
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);
208  //condition
209  coral::AttributeList magnetCurrentBindVariableList;
210  float last_current = -1;
211  magnetCurrentBindVariableList.extend<coral::TimeStamp>("runstart_time");
212  magnetCurrentBindVariableList["runstart_time"].data<coral::TimeStamp>() = start;
213  std::string magnetCurrentWhereClause;
214  if (temp_sum.m_stop_time_str != "null") {
215  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
216  << "]: Accessing the magnet currents measured during run " << r_number
217  << " between " << temp_sum.m_start_time_str << " and " << temp_sum.m_stop_time_str
218  << std::endl;
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");
224  } else {
225  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
226  << "]: Accessing the magnet currents measured before run " << r_number
227  << "start time " << temp_sum.m_start_time_str << std::endl;
228  magnetCurrentWhereClause = std::string("NOT ") + squoted(sDCSMagnetCurrentColumn) + std::string(" IS NULL AND ") +
229  sDCSMagnetChangeDateColumn + std::string("<:runstart_time");
230  }
231  query->setCondition(magnetCurrentWhereClause, magnetCurrentBindVariableList);
232  query->addToOrderList(sDCSMagnetChangeDateColumn + std::string(" DESC"));
233  query->limitReturnedRows(10000);
234  coral::ICursor& magnetCurrentCursor = query->execute();
235  coral::TimeStamp lastCurrentDate;
236  std::string last_date;
237  std::vector<double> time_curr;
238 
239  bool changeFound = false;
240  // first process the changes found within the run boundaries
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>());
248  changeFound = true;
249  if (temp_sum.m_stop_time_str == "null")
250  break;
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;
268  }
269 
270  // if not change is found within run boundaries, search for the most recent change
271  if (!changeFound) {
272  // we should deal with stable currents... so the query is returning no value and we should take the last modified current value...
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
276  << std::endl;
277  Bnotchanged = true;
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;
284  std::string lastValueWhereClause(std::string(" NOT ") + squoted(sDCSMagnetCurrentColumn) +
285  std::string(" IS NULL AND ") + sDCSMagnetChangeDateColumn +
286  std::string(" <:runstop_time"));
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;
299  }
300  temp_sum.m_avg_current = last_current;
301  temp_sum.m_min_current = last_current;
302  temp_sum.m_max_current = last_current;
303  temp_sum.m_stop_current = last_current;
304  temp_sum.m_start_current = last_current;
305  }
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."
312  << std::endl;
313  }
314  if (tsize > 1) {
315  temp_sum.m_run_intervall_micros = time_curr.front() - time_curr.back();
316  } else {
317  temp_sum.m_run_intervall_micros = 0;
318  }
319  edm::LogInfo("RunInfoReader") << "[RunInfoRead::" << __func__
320  << "]: Duration in microseconds of the magnet ramp during run " << r_number << ": "
321  << temp_sum.m_run_intervall_micros << std::endl;
322 
323  double wi = 0;
324  double sumwixi = 0;
325  double sumwi = 0;
326  float min = -1;
327  float max = -1;
328 
329  if (csize != 0) {
330  min = temp_sum.m_current.front();
331  max = temp_sum.m_current.front();
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]);
335  temp_sum.m_times_of_currents.push_back(wi);
336  sumwixi += wi * temp_sum.m_current[i];
337  sumwi += wi;
338  }
339  min = std::min(min, temp_sum.m_current[i]);
340  max = std::max(max, temp_sum.m_current[i]);
341  }
342  std::ostringstream oswi;
343  oswi << "[RunInfoRead::" << __func__ << "]: Duration of current values in run " << r_number << ": ";
344  std::copy(temp_sum.m_times_of_currents.begin(),
345  temp_sum.m_times_of_currents.end(),
346  std::ostream_iterator<float>(oswi, ", "));
347  edm::LogInfo("RunInfoReader") << oswi.str() << std::endl;
348  temp_sum.m_start_current = temp_sum.m_current.back();
349  LogDebug("RunInfoReader") << "start current: " << temp_sum.m_start_current << std::endl;
350  temp_sum.m_stop_current = temp_sum.m_current.front();
351  LogDebug("RunInfoReader") << "stop current: " << temp_sum.m_stop_current << std::endl;
352  if (tsize > 1) {
353  temp_sum.m_avg_current = sumwixi / sumwi;
354  } else {
355  temp_sum.m_avg_current = temp_sum.m_start_current;
356  }
357  LogDebug("RunInfoReader") << "average current: " << temp_sum.m_avg_current << std::endl;
358  temp_sum.m_max_current = max;
359  LogDebug("RunInfoReader") << "maximum current: " << temp_sum.m_max_current << std::endl;
360  temp_sum.m_min_current = min;
361  LogDebug("RunInfoReader") << "minimum current: " << temp_sum.m_min_current << std::endl;
362  } else {
363  if (!Bnotchanged) {
364  edm::LogWarning("RunInfoReader") << "Inserting fake values for magnet current." << std::endl;
365  temp_sum.m_avg_current = -1;
366  temp_sum.m_min_current = -1;
367  temp_sum.m_max_current = -1;
368  temp_sum.m_stop_current = -1;
369  temp_sum.m_start_current = -1;
370  }
371  }
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;
378  oscurr << "Current at run start (A): " << temp_sum.m_start_current;
379  edm::LogInfo("RunInfoReader") << oscurr.str() << std::endl;
380 
381  session->transaction().commit();
382  } catch (const std::exception& e) {
383  throw cms::Exception("RunInfoReader") << "[RunInfoRead::" << __func__ << "]: "
384  << "Unable to create a RunInfo payload. Original Exception:\n"
385  << e.what() << std::endl;
386  }
387 
388  return temp_sum;
389 }
Definition: start.py:1
const boost::posix_time::ptime time0
RunInfoRead(const std::string &connectionString, const edm::ParameterSet &connectionPset)
Definition: RunInfoRead.cc:44
def replace(string, replacements)
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
RunInfo readData(const std::string &runinfo_schema, const std::string &dcsenv_schema, const int r_number)
Definition: RunInfoRead.cc:49
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
void setParameters(const edm::ParameterSet &connectionPset)
Definition: query.py:1
uint64_t word
std::vector< int > m_fed_in
Definition: RunInfo.h:25
std::string m_start_time_str
Definition: RunInfo.h:22
std::vector< float > m_current
Definition: RunInfo.h:32
float m_stop_current
Definition: RunInfo.h:27
Log< level::Info, false > LogInfo
long long m_stop_time_ll
Definition: RunInfo.h:23
edm::ParameterSet m_connectionPset
Definition: RunInfoRead.h:16
float m_min_current
Definition: RunInfo.h:30
float m_avg_current
Definition: RunInfo.h:28
float m_run_intervall_micros
Definition: RunInfo.h:31
float m_max_current
Definition: RunInfo.h:29
float m_start_current
Definition: RunInfo.h:26
std::string m_stop_time_str
Definition: RunInfo.h:24
Log< level::Warning, false > LogWarning
int m_run
Definition: RunInfo.h:20
std::string m_connectionString
Definition: RunInfoRead.h:15
std::vector< float > m_times_of_currents
Definition: RunInfo.h:33
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
long long m_start_time_ll
Definition: RunInfo.h:21
#define LogDebug(id)