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