CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RunInfoRead.cc
Go to the documentation of this file.
1 //#include "CondFormats/Common/interface/TimeConversions.h"
2 //#include "CondFormats/Common/interface/Time.h"
4 #include "RelationalAccess/ISession.h"
5 #include "RelationalAccess/ITransaction.h"
6 #include "RelationalAccess/ISchema.h"
7 #include "RelationalAccess/ITable.h"
8 #include "RelationalAccess/ITableDataEditor.h"
9 #include "RelationalAccess/TableDescription.h"
10 #include "RelationalAccess/IQuery.h"
11 #include "RelationalAccess/ICursor.h"
12 #include "CoralBase/AttributeList.h"
13 #include "CoralBase/Attribute.h"
14 #include "CoralBase/AttributeSpecification.h"
15 #include "CoralBase/TimeStamp.h"
16 #include <algorithm>
17 #include <iostream>
18 #include <iterator>
19 #include <stdexcept>
20 #include <vector>
21 #include <math.h>
22 
23 
24 
25 namespace {
26  std::string dot(".");
27  std::string quote("\"");
28  std::string bNOTb(" NOT ");
29  std::string squoted( const std::string& s ){
30  return quote+s+quote;
31  }
32 
33 
34 }
36  const std::string& user,
37  const std::string& pass):
38  TestBase(),
39  m_connectionString( connectionString ),
40  m_user( user ),
41  m_pass( pass ) {
42  m_tableToRead="";
43  m_columnToRead="";
44 }
45 
47 
49 
50 RunInfo
52  const std::string &column, const int r_number) {
53  m_tableToRead = table; // to be cms_runinfo.runsession_parameter
54  m_columnToRead= column; // to be string_value;
55  RunInfo sum;
56  RunInfo temp_sum;
57  //RunInfo Sum;
58  //for B currents...
59  bool Bnotchanged = 0;
60  //from TimeConversions.h
61  const boost::posix_time::ptime time0 =
62  boost::posix_time::from_time_t(0);
63  //if cursor is null setting null values
64  temp_sum.m_run = r_number;
65  std::cout << "entering readData" << std::endl;
66  coral::ISession* session = this->connect( m_connectionString,
67  m_user, m_pass );
68  try{
69  session->transaction().start();
70  std::cout << "starting session " << std::endl;
71  coral::ISchema& schema = session->schema("CMS_RUNINFO");
72  std::cout << " accessing schema " << std::endl;
73  std::cout << " trying to handle table :: " << m_tableToRead << std::endl;
74  std::string m_columnToRead_id = "ID";
75  long long id_start = 0;
76  //new query to obtain the start_time, fist obtaining the id
77  coral::IQuery* queryI = schema.tableHandle(m_tableToRead).newQuery();
78  //implementing the query here.......
79  queryI->addToOutputList(m_tableToRead + dot + m_columnToRead_id, m_columnToRead_id);
80  //condition
81  coral::AttributeList conditionData;
82  conditionData.extend<int>( "n_run" );
83  conditionData[0].data<int>() = r_number;
84  std::string condition1 = m_tableToRead + ".RUNNUMBER=:n_run AND " + m_tableToRead + ".NAME='CMS.LVL0:START_TIME_T'";
85  queryI->setCondition(condition1, conditionData);
86  coral::ICursor& cursorI = queryI->execute();
87  if( cursorI.next() ) {
88  //cursorI.currentRow().toOutputStream(std::cout) << std::endl;
89  const coral::AttributeList& row = cursorI.currentRow();
90  id_start = row[m_columnToRead_id].data<long long>();
91  }
92  else {
93  id_start = -1;
94  }
95  //std::cout << "id for start time time extracted == " << id_start << std::endl;
96  delete queryI;
97 
98  //now extracting the start time
99  std::string m_tableToRead_date = "RUNSESSION_DATE";
100  std::string m_columnToRead_val = "VALUE";
101  //new query to obtain the start_time, fist obtaining the id
102  coral::IQuery* queryII = schema.tableHandle(m_tableToRead_date).newQuery();
103  //implementing the query here.......
104  queryII->addToOutputList(m_tableToRead_date + dot + m_columnToRead_val, m_columnToRead_val);
105  //condition
106  coral::AttributeList conditionData2;
107  conditionData2.extend<long long>( "n_id" );
108  conditionData2[0].data<long long>() = id_start;
109  std::string condition2 = m_tableToRead_date + ".RUNSESSION_PARAMETER_ID=:n_id";
110  queryII->setCondition(condition2, conditionData2);
111  coral::ICursor& cursorII = queryII->execute();
112  coral::TimeStamp start; //now all times are UTC!
113  if( cursorII.next() ) {
114  //cursorII.currentRow().toOutputStream(std::cout) << std::endl;
115  const coral::AttributeList& row = cursorII.currentRow();
116  start = row[m_columnToRead_val].data<coral::TimeStamp>();
117  /*
118  std::cout << "start time extracted == "
119  << "-->year " << start.year()
120  << "-- month " << start.month()
121  << "-- day " << start.day()
122  << "-- hour " << start.hour()
123  << "-- minute " << start.minute()
124  << "-- second " << start.second()
125  << "-- nanosecond " << start.nanosecond()
126  << std::endl;
127  */
128  boost::posix_time::ptime start_ptime = start.time();
129  std::cout << "Posix time for run start: "<< start_ptime << std::endl;
130  boost::posix_time::time_duration startTimeFromEpoch = start_ptime - time0;
131  temp_sum.m_start_time_str = boost::posix_time::to_iso_extended_string(start_ptime);
132  temp_sum.m_start_time_ll = startTimeFromEpoch.total_microseconds();
133  std::cout << "start time string extracted == " << temp_sum.m_start_time_str << std::endl;
134  std::cout << "microsecond since Epoch (UTC) : " << temp_sum.m_start_time_ll << std::endl;
135  }
136  else {
137  temp_sum.m_start_time_str = "null";
138  temp_sum.m_start_time_ll = -1;
139  }
140  delete queryII;
141 
142  //new query to obtain the stop_time, fist obtaining the id
143  coral::IQuery* queryIII = schema.tableHandle(m_tableToRead).newQuery();
144  //implementing the query here.......
145  queryIII->addToOutputList(m_tableToRead + dot + m_columnToRead_id, m_columnToRead_id);
146  //condition
147  std::string condition3 = m_tableToRead + ".RUNNUMBER=:n_run AND " + m_tableToRead + ".NAME='CMS.LVL0:STOP_TIME_T'";
148  queryIII->setCondition(condition3, conditionData);
149  coral::ICursor& cursorIII = queryIII->execute();
150  long long id_stop = 0;
151  if( cursorIII.next() ) {
152  //cursorIII.currentRow().toOutputStream(std::cout) << std::endl;
153  const coral::AttributeList& row = cursorIII.currentRow();
154  id_stop = row[m_columnToRead_id].data<long long>();
155  }
156  else {
157  id_stop = -1;
158  }
159  //std::cout << "id for stop time time extracted == " << id_stop << std::endl;
160  delete queryIII;
161 
162  //now exctracting the stop time
163  coral::IQuery* queryIV = schema.tableHandle(m_tableToRead_date).newQuery();
164  //implementing the query here.......
165  queryIV->addToOutputList(m_tableToRead_date + dot + m_columnToRead_val, m_columnToRead_val);
166  //condition
167  coral::AttributeList conditionData4;
168  conditionData4.extend<long long>( "n_id" );
169  conditionData4[0].data<long long>() = id_stop;
170  std::string condition4 = m_tableToRead_date + ".RUNSESSION_PARAMETER_ID=:n_id";
171  queryIV->setCondition(condition4, conditionData4);
172  coral::ICursor& cursorIV = queryIV->execute();
173  coral::TimeStamp stop;
174  if( cursorIV.next() ) {
175  //cursorIV.currentRow().toOutputStream(std::cout) << std::endl;
176  const coral::AttributeList& row = cursorIV.currentRow();
177  stop = row[m_columnToRead_val].data<coral::TimeStamp>();
178  /*
179  std::cout << "stop time extracted == "
180  << "-->year " << stop.year()
181  << "-- month " << stop.month()
182  << "-- day " << stop.day()
183  << "-- hour " << stop.hour()
184  << "-- minute " << stop.minute()
185  << "-- second " << stop.second()
186  << "-- nanosecond " << stop.nanosecond()
187  << std::endl;
188  */
189  boost::posix_time::ptime stop_ptime = stop.time();
190  std::cout << "Posix time for run stop: "<< stop_ptime << std::endl;
191  boost::posix_time::time_duration stopTimeFromEpoch = stop_ptime - time0;
192  temp_sum.m_stop_time_str = boost::posix_time::to_iso_extended_string(stop_ptime);
193  temp_sum.m_stop_time_ll = stopTimeFromEpoch.total_microseconds();
194  std::cout << "stop time string extracted == " << temp_sum.m_stop_time_str << std::endl;
195  std::cout << "microsecond since Epoch (UTC) : " << temp_sum.m_stop_time_ll << std::endl;
196  }
197  else {
198  temp_sum.m_stop_time_str = "null";
199  temp_sum.m_stop_time_ll = -1;
200  }
201  delete queryIV;
202 
203  std::string m_tableToRead_fed = "RUNSESSION_STRING";
204  coral::IQuery* queryV = schema.newQuery();
205  queryV->addToTableList(m_tableToRead);
206  queryV->addToTableList(m_tableToRead_fed);
207  queryV->addToOutputList(m_tableToRead_fed + dot + m_columnToRead_val, m_columnToRead_val);
208  //queryV->addToOutputList(m_tableToRead + dot + m_columnToRead, m_columnToRead);
209  //condition
210  std::string condition5 = m_tableToRead + ".RUNNUMBER=:n_run AND " + m_tableToRead + ".NAME='CMS.LVL0:FED_ENABLE_MASK' AND RUNSESSION_PARAMETER.ID = RUNSESSION_STRING.RUNSESSION_PARAMETER_ID";
211  //std::string condition5 = m_tableToRead + ".runnumber=:n_run AND " + m_tableToRead + ".name='CMS.LVL0:FED_ENABLE_MASK'";
212  queryV->setCondition(condition5, conditionData);
213  coral::ICursor& cursorV = queryV->execute();
214  std::string fed;
215  if ( cursorV.next() ) {
216  //cursorV.currentRow().toOutputStream(std::cout) << std::endl;
217  const coral::AttributeList& row = cursorV.currentRow();
218  fed = row[m_columnToRead_val].data<std::string>();
219  }
220  else {
221  fed="null";
222  }
223  //std::cout << "string fed emask == " << fed << std::endl;
224  delete queryV;
225 
226  std::replace(fed.begin(), fed.end(), '%', ' ');
227  std::stringstream stream(fed);
228  for(;;) {
229  std::string word;
230  if ( !(stream >> word) ){break;}
231  std::replace(word.begin(), word.end(), '&', ' ');
232  std::stringstream ss(word);
233  int fedNumber;
234  int val;
235  ss >> fedNumber >> val;
236  //std::cout << "fed:: " << fed << "--> val:: " << val << std::endl;
237  //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....)
238  if( (val & 0001) == 1 && (val != 5) && (val != 7) )
239  temp_sum.m_fed_in.push_back(fedNumber);
240  }
241  std::cout << "feds in run:--> ";
242  std::copy(temp_sum.m_fed_in.begin(), temp_sum.m_fed_in.end(), std::ostream_iterator<int>(std::cout, ", "));
243  std::cout << std::endl;
244  /*
245  for (size_t i =0; i<temp_sum.m_fed_in.size() ; ++i){
246  std::cout << "fed in run:--> " << temp_sum.m_fed_in[i] << std::endl;
247  }
248  */
249 
250  coral::ISchema& schema2 = session->schema("CMS_DCS_ENV_PVSS_COND");
251  std::string m_tableToRead_cur= "CMSFWMAGNET";
252  std::string m_columnToRead_cur= "CURRENT";
253  std::string m_columnToRead_date= "CHANGE_DATE";
254  coral::IQuery* queryVI = schema2.tableHandle(m_tableToRead_cur).newQuery();
255  queryVI->addToOutputList(m_tableToRead_cur + dot + squoted(m_columnToRead_cur), m_columnToRead_cur);
256  queryVI->addToOutputList(m_tableToRead_cur + dot + m_columnToRead_date, m_columnToRead_date);
257  //condition
258  coral::AttributeList conditionData6;
259  float last_current = -1;
260  if(temp_sum.m_stop_time_str != "null") {
261  conditionData6.extend<coral::TimeStamp>( "runstart_time" );
262  conditionData6.extend<coral::TimeStamp>( "runstop_time" );
263  conditionData6["runstart_time"].data<coral::TimeStamp>() = start; //start_time ;
264  conditionData6["runstop_time"].data<coral::TimeStamp>() = stop; //stop_time ;
265  std::string conditionVI = " NOT " + m_tableToRead_cur + dot + squoted(m_columnToRead_cur) + " IS NULL AND "
266  + m_tableToRead_cur + dot + m_columnToRead_date + ">:runstart_time AND "
267  + m_tableToRead_cur + dot + m_columnToRead_date + "<:runstop_time" /*" ORDER BY " + m_columnToRead_date + " DESC"*/;
268  queryVI->setCondition(conditionVI, conditionData6);
269  queryVI->addToOrderList(m_tableToRead_cur + dot + m_columnToRead_date + " DESC");
270  } else {
271  std::cout << "run stop null" << std::endl;
272  conditionData6.extend<coral::TimeStamp>( "runstart_time" );
273  conditionData6["runstart_time"].data<coral::TimeStamp>() = start; //start_time ;
274  std::string conditionVI = " NOT " + m_tableToRead_cur + dot + squoted(m_columnToRead_cur) + " IS NULL AND "
275  + m_tableToRead_cur + dot + m_columnToRead_date + "<:runstart_time" /*" ORDER BY " + m_columnToRead_date + " DESC"*/;
276  queryVI->setCondition(conditionVI, conditionData6);
277  queryVI->addToOrderList(m_tableToRead_cur + dot + m_columnToRead_date + " DESC");
278  }
279  queryVI->limitReturnedRows(10000);
280  coral::ICursor& cursorVI = queryVI->execute();
281  coral::TimeStamp lastCurrentDate;
282  std::string last_date;
283  std::vector<double> time_curr;
284  if ( !cursorVI.next() ) {
285  // we should deal with stable currents... so the query is returning no value and we should take the last modified current value...
286  Bnotchanged = 1;
287  coral::AttributeList conditionData6bis;
288  conditionData6bis.extend<coral::TimeStamp>( "runstop_time" );
289  conditionData6bis["runstop_time"].data<coral::TimeStamp>() = stop; //stop_time ;
290  std::string conditionVIbis = " NOT " + m_tableToRead_cur + dot + squoted(m_columnToRead_cur) + " IS NULL AND "
291  + m_tableToRead_cur + dot + m_columnToRead_date + " <:runstop_time" /*" ORDER BY " + m_columnToRead_date + " DESC"*/;
292  coral::IQuery* queryVIbis = schema2.tableHandle(m_tableToRead_cur).newQuery();
293  queryVIbis->addToOutputList(m_tableToRead_cur + dot + squoted(m_columnToRead_cur), m_columnToRead_cur);
294  queryVIbis->setCondition(conditionVIbis, conditionData6bis);
295  queryVIbis->addToOrderList(m_tableToRead_cur + dot + m_columnToRead_date + " DESC");
296  coral::ICursor& cursorVIbis= queryVIbis->execute();
297  if( cursorVIbis.next() ) {
298  //cursorVIbis.currentRow().toOutputStream(std::cout) << std::endl;
299  const coral::AttributeList& row = cursorVIbis.currentRow();
300  last_current = row[m_columnToRead_cur].data<float>();
301  std::cout << "previos run(s) current, not changed in this run... " << last_current << std::endl;
302  }
303  temp_sum.m_avg_current = last_current;
304  temp_sum.m_min_current = last_current;
305  temp_sum.m_max_current = last_current;
306  temp_sum.m_stop_current = last_current;
307  temp_sum.m_start_current = last_current;
308  }
309  while( cursorVI.next() ) {
310  //cursorVI.currentRow().toOutputStream(std::cout) << std::endl;
311  const coral::AttributeList& row = cursorVI.currentRow();
312  lastCurrentDate = row[m_columnToRead_date].data<coral::TimeStamp>();
313  temp_sum.m_current.push_back( row[m_columnToRead_cur].data<float>() );
314  if(temp_sum.m_stop_time_str == "null") break;
315  /*
316  std::cout << " last current time extracted == "
317  << "-->year " << lastCurrentDate.year()
318  << "-- month " << lastCurrentDate.month()
319  << "-- day " << lastCurrentDate.day()
320  << "-- hour " << lastCurrentDate.hour()
321  << "-- minute " << lastCurrentDate.minute()
322  << "-- second " << lastCurrentDate.second()
323  << "-- nanosecond " << lastCurrentDate.nanosecond()
324  << std::endl;
325  */
326  boost::posix_time::ptime lastCurrentDate_ptime = lastCurrentDate.time();
327  std::cout << "Posix time for last current time: " << lastCurrentDate_ptime << std::endl;
328  boost::posix_time::time_duration lastCurrentDateTimeFromEpoch = lastCurrentDate_ptime - time0;
329  last_date = boost::posix_time::to_iso_extended_string(lastCurrentDate_ptime);
330  std::cout << "last current time extracted == " << last_date << std::endl;
331  long long last_date_ll = lastCurrentDateTimeFromEpoch.total_microseconds();
332  time_curr.push_back(last_date_ll);
333  }
334  delete queryVI;
335 
336  size_t csize = temp_sum.m_current.size();
337  std::cout << "size of currents " << csize << std::endl;
338  size_t tsize = time_curr.size();
339  std::cout << "size of time " << tsize << std::endl;
340  if(csize != tsize) {
341  std::cout<< "current and time not filled correctly" << std::endl;
342  }
343  if(tsize > 1) {
344  temp_sum.m_run_intervall_micros = time_curr.front() - time_curr.back();
345  } else {
346  temp_sum.m_run_intervall_micros = 0;
347  }
348  std::cout << "change current during run interval in microseconds " << temp_sum.m_run_intervall_micros << std::endl;
349 
350  double wi = 0;
351  //std::vector<double> v_wi;
352  double sumwixi = 0;
353  double sumwi = 0;
354  float min = -1;
355  float max = -1;
356 
357  if(csize != 0) {
358  min = temp_sum.m_current.front();
359  max = temp_sum.m_current.front();
360  for(size_t i = 0; i < csize; ++i) {
361  std::cout << "--> " << temp_sum.m_current[i] << std::endl;
362  if( (tsize > 1) && ( i < csize - 1 ) ) {
363  wi = (time_curr[i] - time_curr[i+1]) ;
364  temp_sum.m_times_of_currents.push_back(wi);
365  //v_wi.push_back(wi);
366  sumwixi += wi * temp_sum.m_current[i] ;
367  sumwi += wi;
368  }
369  min = std::min(min, temp_sum.m_current[i]);
370  max = std::max(max, temp_sum.m_current[i]);
371  }
372  //for (size_t i = 0; i < v_wi.size(); ++i) {
373  for (size_t i = 0; i < temp_sum.m_times_of_currents.size(); ++i){
374  std::cout << "wi " << temp_sum.m_times_of_currents[i] << std::endl;
375  }
376  temp_sum.m_start_current = temp_sum.m_current.back(); //temp_sum.m_current[csize - 1];
377  std::cout << "--> " << "start cur " << temp_sum.m_start_current << std::endl;
378  temp_sum.m_stop_current = temp_sum.m_current.front(); //temp_sum.m_current[0];
379  std::cout<< "--> " << "stop cur " << temp_sum.m_stop_current << std::endl;
380  if (tsize>1) {
381  temp_sum.m_avg_current=sumwixi/sumwi;
382  } else {
383  temp_sum.m_avg_current= temp_sum.m_start_current;
384  }
385  std::cout<< "--> " << "avg cur " << temp_sum.m_avg_current << std::endl;
386  temp_sum.m_max_current= max;
387  std::cout<< "--> " << "max cur " << temp_sum.m_max_current << std::endl;
388  temp_sum.m_min_current= min;
389  std::cout<< "--> " << "min cur " << temp_sum.m_min_current << std::endl;
390  } else {
391  if (!Bnotchanged) {
392  temp_sum.m_avg_current = -1;
393  temp_sum.m_min_current = -1;
394  temp_sum.m_max_current = -1;
395  temp_sum.m_stop_current = -1;
396  temp_sum.m_start_current = -1;
397  }
398  }
399 
400  std::cout << "temp_sum.m_avg_current " << temp_sum.m_avg_current << std::endl;
401  std::cout << "temp_sum.m_min_current " << temp_sum.m_min_current << std::endl;
402  std::cout << "temp_sum.m_max_current " << temp_sum.m_max_current << std::endl;
403  std::cout << "temp_sum.m_stop_current " << temp_sum.m_stop_current << std::endl;
404  std::cout << "temp_sum.m_start_current " << temp_sum.m_start_current << std::endl;
405 
406  session->transaction().commit();
407  }
408  catch (const std::exception& e) {
409  std::cout << "Exception: " << e.what() << std::endl;
410  }
411  delete session;
412 
413  sum= temp_sum;
414  return sum;
415 }
int i
Definition: DBlmapReader.cc:9
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
const boost::posix_time::ptime time0
tuple schema
Definition: dataDML.py:2334
std::string m_pass
Definition: RunInfoRead.h:21
RunInfoRead(const std::string &connectionString, const std::string &user, const std::string &pass)
Definition: RunInfoRead.cc:35
void run()
Definition: RunInfoRead.cc:48
std::string m_tableToRead
Definition: RunInfoRead.h:17
std::vector< int > m_fed_in
Definition: RunInfo.h:26
std::string m_start_time_str
Definition: RunInfo.h:23
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< float > m_current
Definition: RunInfo.h:33
RunInfo readData(const std::string &table, const std::string &column, const int r_number)
Definition: RunInfoRead.cc:51
float m_stop_current
Definition: RunInfo.h:28
string connectionString
Definition: autoCondHLT.py:4
std::string m_user
Definition: RunInfoRead.h:20
long long m_stop_time_ll
Definition: RunInfo.h:24
float m_min_current
Definition: RunInfo.h:31
float m_avg_current
Definition: RunInfo.h:29
float m_run_intervall_micros
Definition: RunInfo.h:32
float m_max_current
Definition: RunInfo.h:30
T dot(const Basic3DVector &v) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
std::string m_columnToRead
Definition: RunInfoRead.h:18
float m_start_current
Definition: RunInfo.h:27
std::string m_stop_time_str
Definition: RunInfo.h:25
tuple cout
Definition: gather_cfg.py:145
session
Definition: models.py:201
int m_run
Definition: RunInfo.h:21
std::string m_connectionString
Definition: RunInfoRead.h:19
std::vector< float > m_times_of_currents
Definition: RunInfo.h:34
long long m_start_time_ll
Definition: RunInfo.h:22
virtual ~RunInfoRead()
Definition: RunInfoRead.cc:46
coral::ISession * connect(const std::string &connectionString, const std::string &user, const std::string &password)
Definition: TestBase.cc:24