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