00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalO2OManager.h"
00016
00017 #include "FWCore/PluginManager/interface/PluginManager.h"
00018 #include "FWCore/PluginManager/interface/standard.h"
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020
00021 #include "CondCore/DBCommon/interface/DbConnection.h"
00022 #include "CondCore/DBCommon/interface/DbSession.h"
00023 #include "CondCore/DBCommon/interface/DbScopedTransaction.h"
00024
00025 #include "CondCore/DBCommon/interface/Exception.h"
00026 #include "CondCore/MetaDataService/interface/MetaData.h"
00027 #include "CondCore/IOVService/interface/IOVService.h"
00028
00029 #include "CondCore/IOVService/interface/IOVProxy.h"
00030
00031 #include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
00032 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
00033 #include "xgi/Utils.h"
00034 #include "toolbox/string.h"
00035 #include "OnlineDB/Oracle/interface/Oracle.h"
00036
00037
00038 using namespace oracle::occi;
00039
00040
00041
00042 HcalO2OManager::HcalO2OManager()
00043 {
00044 edmplugin::PluginManager::configure(edmplugin::standard::config());
00045 }
00046
00047
00048 HcalO2OManager::~HcalO2OManager()
00049 {
00050 }
00051
00052
00053
00054
00055 std::vector<std::string> HcalO2OManager::getListOfPoolTags(std::string connect, std::string auth_path){
00056
00057
00058
00059 std::cout << "===> WARNING! auth_path is specified as " << auth_path;
00060 std::cout << " but is not used explicitely. Is it being used at all?" << std::endl;
00061 cond::DbConnection conn;
00062
00063 conn.configuration().setAuthenticationPath(auth_path);
00064
00065 conn.configure();
00066 cond::DbSession session = conn.createSession();
00067 session.open(connect);
00068 std::vector<std::string> alltags;
00069 try{
00070 cond::MetaData metadata_svc(session);
00071 cond::DbScopedTransaction tr(session);
00072 tr.start(true);
00073 metadata_svc.listAllTags(alltags);
00074 tr.commit();
00075 }catch(cond::Exception& er){
00076 std::cout<<er.what()<<std::endl;
00077 }catch(std::exception& er){
00078 std::cout<<er.what()<<std::endl;
00079 }
00080 return alltags;
00081 }
00082
00083
00084
00085
00086
00087 int HcalO2OManager::getListOfPoolIovs(std::vector<uint32_t> & out,
00088 std::string tag,
00089 std::string connect,
00090 std::string auth_path){
00091
00092
00093 std::cout << "===> WARNING! auth_path is specified as " << auth_path;
00094 std::cout << " but is not used explicitely. Is it being used at all?" << std::endl;
00095 bool details=false;
00096 cond::DbConnection conn;
00097
00098 conn.configuration().setAuthenticationPath(auth_path);
00099
00100 conn.configure();
00101 cond::DbSession session = conn.createSession();
00102 session.open(connect);
00103 out.clear();
00104 try{
00105 cond::MetaData metadata_svc(session);
00106 cond::DbScopedTransaction tr(session);
00107 tr.start(true);
00108 std::string token;
00109 if(!metadata_svc.hasTag(tag)){
00110
00111 return -1;
00112 }
00113 token=metadata_svc.getToken(tag);
00114 cond::IOVProxy iov(session, token, !details, details);
00115 unsigned int counter=0;
00116 std::string payloadContainer=iov.payloadContainerName();
00117
00118 for (cond::IOVProxy::const_iterator ioviterator=iov.begin(); ioviterator!=iov.end(); ioviterator++) {
00119 out.push_back(ioviterator->since());
00120 ++counter;
00121 }
00122 tr.commit();
00123 }
00124 catch(cond::Exception& er){
00125 std::cout<<er.what()<<std::endl;
00126 }catch(std::exception& er){
00127 std::cout<<er.what()<<std::endl;
00128 }
00129 return out.size();
00130 }
00131
00132
00133
00134 std::vector<std::string> HcalO2OManager::getListOfOmdsTags(){
00135 std::vector<std::string> alltags;
00136 static ConnectionManager conn;
00137 conn.connect();
00138 std::string query = "select ";
00139 query += " channel_map_id,subdet,ieta,iphi,depth ";
00140 query += "from ";
00141 query += " cms_hcl_hcal_cond.hcal_channels ";
00142 query += "where ";
00143 query += " subdet='HB' or subdet='HE' or subdet='HF' or subdet='HO' ";
00144 int _n_tags = 0;
00145 try {
00146 oracle::occi::Statement* stmt = conn.getStatement(query);
00147 oracle::occi::ResultSet *rs = stmt->executeQuery();
00148 while (rs->next()) {
00149 _n_tags++;
00150
00151 }
00152 }
00153 catch (SQLException& e) {
00154 std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
00155 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
00156 }
00157 conn.disconnect();
00158 return alltags;
00159 }
00160
00161
00162
00163 int HcalO2OManager::getListOfOmdsIovs(std::vector<uint32_t> & out, std::string tagname){
00164 out.clear();
00165 static ConnectionManager conn;
00166 conn.connect();
00167 std::string query = " ";
00168
00169
00170
00171
00172 query += " select iovs.interval_of_validity_begin as iov, ";
00173 query += " min(iovs.record_insertion_time) time ";
00174 query += " from cms_hcl_core_iov_mgmnt.cond_tags tags ";
00175 query += " inner join cms_hcl_core_iov_mgmnt.cond_iov2tag_maps i2t ";
00176 query += " on tags.cond_tag_id=i2t.cond_tag_id ";
00177 query += " inner join cms_hcl_core_iov_mgmnt.cond_iovs iovs ";
00178 query += " on i2t.cond_iov_record_id=iovs.cond_iov_record_id ";
00179 query += "where ";
00180 query += " tags.tag_name=:1 ";
00181 query += "group by iovs.interval_of_validity_begin ";
00182
00183
00184
00185 query += "order by time asc ";
00186 int _n_iovs = 0;
00187 try {
00188 oracle::occi::Statement* stmt = conn.getStatement(query);
00189
00190 stmt->setString(1,tagname);
00191 oracle::occi::ResultSet *rs = stmt->executeQuery();
00192 while (rs->next()) {
00193 _n_iovs++;
00194 out.push_back(rs->getInt(1));
00195 }
00196 }
00197 catch (SQLException& e) {
00198 std::cerr << ::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()) << std::endl;
00199 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
00200 }
00201 conn.disconnect();
00202 return out.size();
00203 }
00204
00205
00206 void HcalO2OManager::getListOfNewIovs_test( void ){
00207 std::vector<uint32_t> omds, orcon, out;
00208 orcon.push_back(1);
00209 orcon.push_back(100);
00210
00211
00212
00213
00214
00215
00216
00217 omds.push_back(1);
00218 omds.push_back(100);
00219
00220
00221
00222
00223
00224
00225
00226 if (getListOfNewIovs(out, omds, orcon) == -1){
00227 std::cout << "HcalO2OManager::getListOfNewIovs_test(): O2O is not possible" << std::endl;
00228 }
00229 else if (getListOfNewIovs(out, omds, orcon) == 0){
00230 std::cout << "HcalO2OManager::getListOfNewIovs_test(): O2O is not needed, the tag is up to date" << std::endl;
00231 }
00232 else{
00233 std::cout << "HcalO2OManager::getListOfNewIovs_test(): O2O is possible" << std::endl;
00234 std::cout << "HcalO2OManager::getListOfNewIovs_test(): " << out.size() << " IOVs to be copied to ORCON" << std::endl;
00235 std::copy (out.begin(),
00236 out.end(),
00237 std::ostream_iterator<uint32_t>(std::cout,"\n")
00238 );
00239 }
00240 }
00241
00242
00243 int HcalO2OManager::getListOfNewIovs(std::vector<uint32_t> & iovs,
00244 const std::vector<uint32_t> & omds_iovs,
00245 const std::vector<uint32_t> & orcon_iovs){
00246 int result = -1;
00247 iovs.clear();
00248
00249
00250 int _orcon_index_offset = 0;
00251 if (omds_iovs.size() > 0 &&
00252 orcon_iovs.size() > 0 &&
00253 orcon_iovs[0] == 1 &&
00254 omds_iovs[0] != 1){
00255 std::cout << std::endl << "HcalO2OManager: First IOV in the OMDS tag is not 1," << std::endl;
00256 std::cout << "HcalO2OManager: while it must be 1 in the offline tag." << std::endl;
00257 std::cout << "HcalO2OManager: O2O will assume that IOV=1 in the offline tag" << std::endl;
00258 std::cout << "HcalO2OManager: is filled with some safe default." << std::endl;
00259 std::cout << "HcalO2OManager: IOV=1 will be ignored, and O2O will proceeed" << std::endl;
00260 std::cout << "HcalO2OManager: as long as other inconsistencies are not detected." << std::endl << std::endl;
00261 _orcon_index_offset = 1;
00262
00263 }
00264 if (omds_iovs.size()+_orcon_index_offset < orcon_iovs.size()){
00265 std::cout << "HcalO2OManager: too many IOVs in the Pool tag, cannot sync, exiting..." << std::endl;
00266 return result;
00267 }
00268
00269
00270 unsigned int _index = 0;
00271
00272 bool enforce_strict_matching = false;
00273
00274
00275 unsigned int _sync_status = 0;
00276 for (std::vector<uint32_t>::const_iterator _iov = orcon_iovs.begin();
00277 _iov != orcon_iovs.end();
00278 ++_iov){
00279 _index = (int)(_iov - orcon_iovs.begin());
00280
00281
00282
00283
00284
00285
00286
00287
00288 if (_orcon_index_offset == 1 && _index == 0) continue;
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 if (omds_iovs[_index-_orcon_index_offset] != orcon_iovs[_index]){
00302
00303 std::cout << std::endl;
00304 std::cout << "HcalO2OManager: existing IOVs do not match, cannot sync in the strict sense." << std::endl;
00305 std::cout << "HcalO2OManager: mismatched pair is (OMDS/offline): " << omds_iovs[_index-_orcon_index_offset] << "/" << orcon_iovs[_index] << std::endl;
00306 std::cout << "HcalO2OManager: In the strict sense, the SYNCHRONIZATION OF THIS TAG HAS FAILED!" << std::endl;
00307 std::cout << "HcalO2OManager: As an interim solution, I will still copy to the offline tag" << std::endl;
00308 std::cout << "HcalO2OManager: those IOV from the OMDS tag that are newer than the last IOV" << std::endl;
00309 std::cout << "HcalO2OManager: currently in the offline tag. " << std::endl;
00310
00311 _sync_status = 1;
00312
00313
00314
00315 if (enforce_strict_matching){
00316 return result;
00317 }
00318 else{
00319 break;
00320 }
00321
00322 }
00323 ++_index;
00324 }
00325
00326
00327
00328
00329
00330
00331 int _counter = 0;
00332 uint32_t _lastOrconIov = orcon_iovs[orcon_iovs.size()-1];
00333
00334 for (;_index < omds_iovs.size();++_index){
00335
00336 uint32_t _aIov = omds_iovs[_index];
00337
00338 if (_index == 0 && _aIov > _lastOrconIov){
00339 iovs.push_back(_aIov);
00340 ++_counter;
00341 }
00342 else if (omds_iovs[_index]>omds_iovs[_index-1] &&
00343 _aIov > _lastOrconIov){
00344 iovs.push_back(omds_iovs[_index]);
00345 ++_counter;
00346 }
00347 else{
00348 if (enforce_strict_matching){
00349 return result;
00350 }
00351 else{
00352 continue;
00353 }
00354 }
00355 }
00356
00357 result = _counter;
00358 return result;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368 int HcalO2OManager::getListOfUpdateIovs(std::vector<uint32_t> & _iovs,
00369 std::string _tag,
00370 std::string pool_connect_string,
00371 std::string pool_auth_path
00372 ){
00373
00374 std::vector<uint32_t> omds_iovs;
00375 std::vector<uint32_t> pool_iovs;
00376 getListOfOmdsIovs(omds_iovs, _tag);
00377 getListOfPoolIovs(pool_iovs, _tag, pool_connect_string, pool_auth_path);
00378 int n_iovs = getListOfNewIovs(_iovs,
00379 omds_iovs,
00380 pool_iovs);
00381 if (n_iovs == -1){
00382 std::cout << "HcalO2OManager: O2O is not possible" << std::endl;
00383 }
00384 else if (n_iovs == 0){
00385 std::cout << "HcalO2OManager: O2O is not needed, the tag is up to date" << std::endl;
00386 }
00387 else{
00388 edm::LogInfo("HcalO2OManager") << "These IOVs are to be updated:" << std::endl;
00389 for (std::vector<uint32_t>::const_iterator iov = _iovs.begin();
00390 iov != _iovs.end();
00391 ++iov){
00392 std::cout << "O2O_IOV_LIST: " << *iov << std::endl;
00393 }
00394 }
00395 return n_iovs;
00396 }