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