CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalO2OManager.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HcalOnlineDb
4 // Class : HcalO2OManager
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Gena Kukartsev
10 // Created: Sun Aug 16 20:44:05 CEST 2009
11 //
12 
13 #include <iostream>
14 
16 
20 
24 
27 
29 
31 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
33 
34 #ifdef HAVE_XDAQ
35 #include <toolbox/string.h>
36 #else
37 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h" // Replaces toolbox::toString
38 #endif
39 
40 using namespace oracle::occi;
41 
42 
43 
45 {
47 }
48 
49 
51 {
52 }
53 
54 
55 // inspired by cmscond_list_iov
56 //
58  //edmplugin::PluginManager::configure(edmplugin::standard::config()); // in the constructor for now
59  //
60  // FIXME: how to add auth_path authentication to this? See v1.25 for the functionality using old API
61  std::cout << "===> WARNING! auth_path is specified as " << auth_path;
62  std::cout << " but is not used explicitely. Is it being used at all?" << std::endl;
64  //conn.configure( cond::CmsDefaults );
65  conn.configuration().setAuthenticationPath(auth_path);
66  //conn.configuration().setMessageLevel( coral::Debug );
67  conn.configure();
69  session.open(connect);
70  std::vector<std::string> alltags;
71  try{
72  cond::MetaData metadata_svc(session);
73  cond::DbScopedTransaction tr(session);
74  tr.start(true);
75  metadata_svc.listAllTags(alltags);
76  tr.commit();
77  }catch(cond::Exception& er){
78  std::cout<<er.what()<<std::endl;
79  }catch(std::exception& er){
80  std::cout<<er.what()<<std::endl;
81  }
82  return alltags;
83 }
84 
85 
86 
87 // inspired by cmscond_list_iov
88 //
89 int HcalO2OManager::getListOfPoolIovs(std::vector<uint32_t> & out,
92  std::string auth_path){
93  //edmplugin::PluginManager::configure(edmplugin::standard::config()); // in the constructor for now
94  // FIXME: how to add auth_path authentication to this? See v1.25 for the functionality using old API
95  std::cout << "===> WARNING! auth_path is specified as " << auth_path;
96  std::cout << " but is not used explicitely. Is it being used at all?" << std::endl;
98  //conn.configure( cond::CmsDefaults );
99  conn.configuration().setAuthenticationPath(auth_path);
100  //conn.configuration().setMessageLevel( coral::Debug );
101  conn.configure();
103  session.open(connect);
104  out.clear();
105  try{
106  cond::MetaData metadata_svc(session);
107  cond::DbScopedTransaction tr(session);
108  tr.start(true);
109  std::string token;
110  if(!metadata_svc.hasTag(tag)){
111  //std::cout << "no such tag in the Pool database!" << std::endl;
112  return -1;
113  }
114  token=metadata_svc.getToken(tag);
115  cond::IOVProxy iov(session, token);
116  unsigned int counter=0;
117 
118  for (cond::IOVProxy::const_iterator ioviterator=iov.begin(); ioviterator!=iov.end(); ioviterator++) {
119  out.push_back(ioviterator->since());
120  ++counter;
121  }
122  tr.commit();
123  }
124  catch(cond::Exception& er){
125  std::cout<<er.what()<<std::endl;
126  }catch(std::exception& er){
127  std::cout<<er.what()<<std::endl;
128  }
129  return out.size();
130 }
131 
132 
133 
134 std::vector<std::string> HcalO2OManager::getListOfOmdsTags(){
135  std::vector<std::string> alltags;
136  static ConnectionManager conn;
137  conn.connect();
138  std::string query = "select ";
139  query += " channel_map_id,subdet,ieta,iphi,depth ";
140  query += "from ";
141  query += " cms_hcl_hcal_cond.hcal_channels ";
142  query += "where ";
143  query += " subdet='HB' or subdet='HE' or subdet='HF' or subdet='HO' ";
144  int _n_tags = 0;
145  try {
146  oracle::occi::Statement* stmt = conn.getStatement(query);
147  oracle::occi::ResultSet *rs = stmt->executeQuery();
148  while (rs->next()) {
149  _n_tags++;
150  //alltags.push_back(rs->getString(1));
151  }
152  }
153  catch (SQLException& e) {
154  std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
155  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
156  }
157  conn.disconnect();
158  return alltags;
159 }
160 
161 
162 
164  out.clear();
165  static ConnectionManager conn;
166  conn.connect();
167  std::string query = " ";
168  //query += "select iov, ";
169  //query += " i.cond_iov_record_id, ";
170  //query += " time ";
171  //query += "from ( ";
172  query += " select iovs.interval_of_validity_begin as iov, ";
173  query += " min(iovs.record_insertion_time) time ";
174  query += " from cms_hcl_core_iov_mgmnt.cond_tags tags ";
175  query += " inner join cms_hcl_core_iov_mgmnt.cond_iov2tag_maps i2t ";
176  query += " on tags.cond_tag_id=i2t.cond_tag_id ";
177  query += " inner join cms_hcl_core_iov_mgmnt.cond_iovs iovs ";
178  query += " on i2t.cond_iov_record_id=iovs.cond_iov_record_id ";
179  query += "where ";
180  query += " tags.tag_name=:1 ";
181  query += "group by iovs.interval_of_validity_begin ";
182  //query += " ) ";
183  //query += "inner join cms_hcl_core_iov_mgmnt.cond_iovs i ";
184  //query += "on time=i.record_insertion_time ";
185  query += "order by time asc ";
186  int _n_iovs = 0;
187  try {
188  oracle::occi::Statement* stmt = conn.getStatement(query);
189  //_____ set bind variables
190  stmt->setString(1,tagname);
191  oracle::occi::ResultSet *rs = stmt->executeQuery();
192  while (rs->next()) {
193  _n_iovs++;
194  out.push_back(rs->getInt(1));
195  }
196  }
197  catch (SQLException& e) {
198  std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
199  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
200  }
201  conn.disconnect();
202  return out.size();
203 }
204 
205 
207  std::vector<uint32_t> omds, orcon, out;
208  orcon.push_back(1);
209  orcon.push_back(100);
210  //orcon.push_back(80000);
211  //orcon.push_back(90000);
212  //orcon.push_back(100000);
213  //orcon.push_back(199000);
214  //orcon.push_back(199001);
215  //orcon.push_back(199002);
216  //orcon.push_back(199003);
217  omds.push_back(1);
218  omds.push_back(100);
219  //omds.push_back(80000);
220  //omds.push_back(90000);
221  //omds.push_back(100000);
222  //omds.push_back(199000);
223  //omds.push_back(199001);
224  //omds.push_back(199002);
225  //omds.push_back(199004);
226  if (getListOfNewIovs(out, omds, orcon) == -1){
227  std::cout << "HcalO2OManager::getListOfNewIovs_test(): O2O is not possible" << std::endl;
228  }
229  else if (getListOfNewIovs(out, omds, orcon) == 0){
230  std::cout << "HcalO2OManager::getListOfNewIovs_test(): O2O is not needed, the tag is up to date" << std::endl;
231  }
232  else{
233  std::cout << "HcalO2OManager::getListOfNewIovs_test(): O2O is possible" << std::endl;
234  std::cout << "HcalO2OManager::getListOfNewIovs_test(): " << out.size() << " IOVs to be copied to ORCON" << std::endl;
235  std::copy (out.begin(),
236  out.end(),
237  std::ostream_iterator<uint32_t>(std::cout,"\n")
238  );
239  }
240 }
241 
242 
243 int HcalO2OManager::getListOfNewIovs(std::vector<uint32_t> & iovs,
244  const std::vector<uint32_t> & omds_iovs,
245  const std::vector<uint32_t> & orcon_iovs){
246  int result = -1; // default fail
247  iovs.clear();
248 
249  // OMDS tag may not have the first IOV=1
250  int _orcon_index_offset = 0;
251  if (omds_iovs.size() > 0 &&
252  orcon_iovs.size() > 0 &&
253  orcon_iovs[0] == 1 &&
254  omds_iovs[0] != 1){
255  std::cout << std::endl << "HcalO2OManager: First IOV in the OMDS tag is not 1," << std::endl;
256  std::cout << "HcalO2OManager: while it must be 1 in the offline tag." << std::endl;
257  std::cout << "HcalO2OManager: O2O will assume that IOV=1 in the offline tag" << std::endl;
258  std::cout << "HcalO2OManager: is filled with some safe default." << std::endl;
259  std::cout << "HcalO2OManager: IOV=1 will be ignored, and O2O will proceeed" << std::endl;
260  std::cout << "HcalO2OManager: as long as other inconsistencies are not detected." << std::endl << std::endl;
261  _orcon_index_offset = 1; // skip the first IOV in ORCON because it
262  // // is 1 while OMDS doesn't have IOV=1
263  }
264  if (omds_iovs.size()+_orcon_index_offset < orcon_iovs.size()){
265  std::cout << "HcalO2OManager: too many IOVs in the Pool tag, cannot sync, exiting..." << std::endl;
266  return result;
267  }
268 
269  // loop over all OMDS IOVs
270  unsigned int _index = 0;
271 
272  bool enforce_strict_matching = false; // set to true if the strict IOV matching is desired, see description in the comments below
273 
274  for (std::vector<uint32_t>::const_iterator _iov = orcon_iovs.begin();
275  _iov != orcon_iovs.end();
276  ++_iov){
277  _index = (int)(_iov - orcon_iovs.begin());
278 
279  // special case when the first Pool IOV = 1 (must always be true)
280  // but OMDS IOVs do not start with IOV = 1
281  // This can be a ligitimate mismatch when the OMDS tag is created for
282  // current conditions without regard to the history
283  // In such cases, when creating a copy of this tag in offline,
284  // O2O copies the first IOV from OMDS and assigns it as IOV=1.
285  // With later sync passes, O2O must skip the first offline IOV = 1
286  if (_orcon_index_offset == 1 && _index == 0) continue;
287 
288  // current pair of OMDS-offline IOVs does not match
289  // There are several options in such case:
290  //
291  // - with strict matching, the program should quit, as it is not possible
292  // to keep the tag in sync between OMDS and offline because
293  // offline tags do not allow fixes, only updates
294  //
295  // - intermediate solution is to find the latest IOV in the offline tag
296  // and append any IOVs from the OMDS tag that come after it
297  //
298 
299  if (omds_iovs[_index-_orcon_index_offset] != orcon_iovs[_index]){
300 
301  std::cout << std::endl;
302  std::cout << "HcalO2OManager: existing IOVs do not match, cannot sync in the strict sense." << std::endl;
303  std::cout << "HcalO2OManager: mismatched pair is (OMDS/offline): " << omds_iovs[_index-_orcon_index_offset] << "/" << orcon_iovs[_index] << std::endl;
304  std::cout << "HcalO2OManager: In the strict sense, the SYNCHRONIZATION OF THIS TAG HAS FAILED!" << std::endl;
305  std::cout << "HcalO2OManager: As an interim solution, I will still copy to the offline tag" << std::endl;
306  std::cout << "HcalO2OManager: those IOV from the OMDS tag that are newer than the last IOV" << std::endl;
307  std::cout << "HcalO2OManager: currently in the offline tag. " << std::endl;
308 
309  // existing IOVs do not match
310 
311  if (enforce_strict_matching){
312  return result;
313  }
314  else{
315  break; // _index now contains the last "valid" OMDS IOV
316  }
317 
318  }
319  ++_index;
320  }
321 
322 
323  //
324  //_____ loop over remaining OMDS IOVs
325  //
326  //std::cout << "HcalO2OManager: DEBUG: " << std::endl;
327  int _counter = 0; // count output IOVs
328  uint32_t _lastOrconIov = orcon_iovs[orcon_iovs.size()-1];
329 
330  for (;_index < omds_iovs.size();++_index){
331 
332  uint32_t _aIov = omds_iovs[_index];
333 
334  if (_index == 0 && _aIov > _lastOrconIov){ // can only copy later IOVs
335  iovs.push_back(_aIov);
336  ++_counter;
337  }
338  else if (omds_iovs[_index]>omds_iovs[_index-1] &&
339  _aIov > _lastOrconIov){ // can only copy later IOVs
340  iovs.push_back(omds_iovs[_index]);
341  ++_counter;
342  }
343  else{
344  if (enforce_strict_matching){
345  return result;
346  }
347  else{
348  continue;
349  }
350  }
351  }
352  //if (_counter != 0) result = _counter;
353  result = _counter;
354  return result;
355 }
356 
357 
358 // get list of IOVs to update for a given tag, or report impossible
359 // return:
360 // list of IOVs as first argument,
361 // number of IOVs to update as return value
362 // -1 if tag is inconsistent with the update
363 // 0 if everything's up to date already
364 int HcalO2OManager::getListOfUpdateIovs(std::vector<uint32_t> & _iovs,
365  std::string _tag,
366  std::string pool_connect_string,
368  ){
369  //std::cout << "DEBUG: " << pool_connect_string << std::endl;
370  std::vector<uint32_t> omds_iovs;
371  std::vector<uint32_t> pool_iovs;
372  getListOfOmdsIovs(omds_iovs, _tag);
373  getListOfPoolIovs(pool_iovs, _tag, pool_connect_string, pool_auth_path);
374  int n_iovs = getListOfNewIovs(_iovs,
375  omds_iovs,
376  pool_iovs);
377  if (n_iovs == -1){
378  std::cout << "HcalO2OManager: O2O is not possible" << std::endl;
379  }
380  else if (n_iovs == 0){
381  std::cout << "HcalO2OManager: O2O is not needed, the tag is up to date" << std::endl;
382  }
383  else{
384  edm::LogInfo("HcalO2OManager") << "These IOVs are to be updated:" << std::endl;
385  for (std::vector<uint32_t>::const_iterator iov = _iovs.begin();
386  iov != _iovs.end();
387  ++iov){
388  std::cout << "O2O_IOV_LIST: " << *iov << std::endl;
389  }
390  }
391  return n_iovs;
392 }
virtual char const * what() const
Definition: Exception.cc:141
static PluginManager & configure(const Config &)
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:159
DbConnectionConfiguration & configuration()
Definition: DbConnection.cc:83
virtual ~HcalO2OManager()
const_iterator begin() const
Definition: IOVProxy.h:188
const std::string getToken(const std::string &tagname) const
Definition: MetaData.cc:52
oracle::occi::Statement * getStatement(const std::string &query)
const_iterator end() const
Definition: IOVProxy.h:193
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
int getListOfNewIovs(std::vector< uint32_t > &iovs, const std::vector< uint32_t > &omds_iovs, const std::vector< uint32_t > &orcon_iovs)
PluginManager::Config config()
Definition: standard.cc:21
tuple iov
Definition: o2o.py:307
bool hasTag(const std::string &name) const
Definition: MetaData.cc:68
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
tuple result
Definition: query.py:137
int getListOfUpdateIovs(std::vector< uint32_t > &_iovs, std::string _tag, std::string pool_connect_string, std::string pool_auth_path)
string pool_auth_path
Definition: o2o.py:29
int start(bool readOnly=false)
start transaction
int _index
the final reconstruction sequence
tuple out
Definition: dbtoconf.py:99
DbSession createSession() const
Definition: DbConnection.cc:66
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
void setAuthenticationPath(const std::string &p)
std::vector< std::string > getListOfOmdsTags()
static std::atomic< unsigned int > counter
tuple alltags
Definition: lumiTag.py:77
int getListOfPoolIovs(std::vector< uint32_t > &out, std::string tagname, std::string connect, std::string auth_path)
int getListOfOmdsIovs(std::vector< uint32_t > &out, std::string tagname)
tuple query
Definition: o2o.py:269
tuple cout
Definition: gather_cfg.py:121
std::vector< std::string > getListOfPoolTags(std::string connect, std::string auth_path)
void listAllTags(std::vector< std::string > &result) const
Definition: MetaData.cc:80
int commit()
commit transaction. Will disconnect from database if connection timeout==0 or connectted time close t...
tuple conn
Definition: results_mgr.py:53
iov_range_iterator const_iterator
Definition: IOVProxy.h:160
void getListOfNewIovs_test(void)