File indexing completed on 2024-04-06 12:23:05
0001 #include "OnlineDB/EcalCondDB/interface/LMFCorrCoefDat.h"
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003
0004 LMFCorrCoefDat::LMFCorrCoefDat() { init(); }
0005
0006 LMFCorrCoefDat::LMFCorrCoefDat(EcalDBConnection *c) {
0007 init();
0008 m_env = c->getEnv();
0009 m_conn = c->getConn();
0010 }
0011
0012 LMFCorrCoefDat::LMFCorrCoefDat(oracle::occi::Environment *env, oracle::occi::Connection *conn) {
0013 init();
0014 m_env = env;
0015 m_conn = conn;
0016 }
0017
0018 LMFCorrCoefDat::~LMFCorrCoefDat() {
0019 std::map<int, LMFCorrCoefDatComponent *>::iterator i = m_data.begin();
0020 std::map<int, LMFCorrCoefDatComponent *>::iterator e = m_data.end();
0021 while (i != e) {
0022 delete i->second;
0023 i++;
0024 }
0025 m_data.clear();
0026 std::map<int, LMFLmrSubIOV *>::iterator si = m_subiov.begin();
0027 std::map<int, LMFLmrSubIOV *>::iterator se = m_subiov.end();
0028 while (si != se) {
0029 delete si->second;
0030 si++;
0031 }
0032 m_subiov.clear();
0033 }
0034
0035 void LMFCorrCoefDat::init() {
0036 m_data.clear();
0037 m_subiov.clear();
0038 m_env = nullptr;
0039 m_conn = nullptr;
0040 nodebug();
0041 }
0042
0043 LMFCorrCoefDat &LMFCorrCoefDat::setConnection(oracle::occi::Environment *env, oracle::occi::Connection *conn) {
0044 m_env = env;
0045 m_conn = conn;
0046 std::map<int, LMFCorrCoefDatComponent *>::iterator i = m_data.begin();
0047 std::map<int, LMFCorrCoefDatComponent *>::iterator e = m_data.end();
0048 while (i != e) {
0049 i->second->setConnection(m_env, m_conn);
0050 i++;
0051 }
0052 return *this;
0053 }
0054
0055 LMFCorrCoefDat &LMFCorrCoefDat::setP123(const LMFLmrSubIOV &iov, const EcalLogicID &id, float p1, float p2, float p3) {
0056 find(iov)->setP123(id, p1, p2, p3);
0057 return *this;
0058 }
0059
0060 LMFCorrCoefDat &LMFCorrCoefDat::setP123(
0061 const LMFLmrSubIOV &iov, const EcalLogicID &id, float p1, float p2, float p3, float p1e, float p2e, float p3e) {
0062 find(iov)->setP123(id, p1, p2, p3, p1e, p2e, p3e);
0063 return *this;
0064 }
0065
0066 LMFCorrCoefDat &LMFCorrCoefDat::setP123Errors(
0067 const LMFLmrSubIOV &iov, const EcalLogicID &id, float p1e, float p2e, float p3e) {
0068 find(iov)->setP123Errors(id, p1e, p2e, p3e);
0069 return *this;
0070 }
0071
0072 LMFCorrCoefDat &LMFCorrCoefDat::setFlag(const LMFLmrSubIOV &iov, const EcalLogicID &id, int flag) {
0073 find(iov)->setFlag(id, flag);
0074 return *this;
0075 }
0076
0077 LMFCorrCoefDat &LMFCorrCoefDat::setSequence(const LMFLmrSubIOV &iov, const EcalLogicID &id, int seq_id) {
0078 find(iov)->setSequence(id, seq_id);
0079 return *this;
0080 }
0081
0082 LMFCorrCoefDat &LMFCorrCoefDat::setSequence(const LMFLmrSubIOV &iov, const EcalLogicID &id, const LMFSeqDat &seq) {
0083 find(iov)->setSequence(id, seq);
0084 return *this;
0085 }
0086
0087 LMFCorrCoefDatComponent *LMFCorrCoefDat::find(const LMFLmrSubIOV &iov) {
0088 if (m_data.find(iov.getID()) != m_data.end()) {
0089 return m_data[iov.getID()];
0090 } else {
0091 LMFCorrCoefDatComponent *c = new LMFCorrCoefDatComponent();
0092 LMFLmrSubIOV *subiov = new LMFLmrSubIOV();
0093 if (m_conn != nullptr) {
0094 c->setConnection(m_env, m_conn);
0095 subiov->setConnection(m_env, m_conn);
0096 }
0097 *subiov = iov;
0098 c->setLMFLmrSubIOV(*subiov);
0099 m_data[subiov->getID()] = c;
0100 m_subiov[subiov->getID()] = subiov;
0101 return c;
0102 }
0103 }
0104
0105 void LMFCorrCoefDat::dump() {
0106 std::cout << std::endl;
0107 std::cout << "##################### LMF_CORR_COEF_DAT ########################" << std::endl;
0108 std::cout << "This structure contains " << m_data.size() << " LMR_SUB_IOV_ID" << std::endl;
0109 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
0110 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
0111 int count = 0;
0112 while (i != e) {
0113 std::cout << "### SUB IOV ID: " << i->second->getLMFLmrSubIOVID() << std::endl;
0114 std::list<int> logic_ids = i->second->getLogicIds();
0115 std::cout << " Contains data for " << logic_ids.size() << " xtals" << std::endl;
0116 count += logic_ids.size();
0117 i++;
0118 }
0119 std::cout << "Total no. of xtals for which data are stored: " << count << std::endl;
0120 std::cout << "##################### LMF_CORR_COEF_DAT ########################" << std::endl;
0121 }
0122
0123 void LMFCorrCoefDat::writeDB() {
0124 std::map<int, LMFCorrCoefDatComponent *>::iterator i = m_data.begin();
0125 std::map<int, LMFCorrCoefDatComponent *>::iterator e = m_data.end();
0126 while (i != e) {
0127 if (m_debug) {
0128 std::cout << "Writing data for LMR_SUB_IOV_ID " << i->first << std::endl;
0129 }
0130 i->second->writeDB();
0131 i++;
0132 }
0133 }
0134
0135 void LMFCorrCoefDat::debug() {
0136 std::cout << "Set debug" << std::endl << std::flush;
0137 m_debug = true;
0138 }
0139
0140 void LMFCorrCoefDat::nodebug() { m_debug = false; }
0141
0142 RunIOV LMFCorrCoefDat::fetchLastInsertedRun() {
0143 RunIOV iov;
0144 if (m_conn == nullptr) {
0145 throw std::runtime_error(
0146 "[LMFCorrCoefDat::fetchLastInsertedRun] ERROR: "
0147 "Connection not set");
0148 }
0149 iov.setConnection(m_env, m_conn);
0150 std::string sql =
0151 "SELECT IOV_ID FROM CMS_ECAL_COND.RUN_IOV WHERE "
0152 "IOV_ID = (SELECT RUN_IOV_ID FROM LMF_SEQ_DAT WHERE SEQ_ID = "
0153 "(SELECT MAX(SEQ_ID) FROM LMF_CORR_COEF_DAT))";
0154 oracle::occi::Statement *stmt;
0155 try {
0156 stmt = m_conn->createStatement();
0157 stmt->setSQL(sql);
0158 } catch (oracle::occi::SQLException &e) {
0159 throw(std::runtime_error("[LMFCorrCoefDat::fetchLastInsertedRun]: " + e.getMessage()));
0160 }
0161 if (m_debug) {
0162 std::cout << "[LMFCorrCoefDat::fetchLastInsertedRun] executing query" << std::endl
0163 << sql << std::endl
0164 << std::flush;
0165 }
0166 oracle::occi::ResultSet *rset = stmt->executeQuery();
0167 if (m_debug) {
0168 std::cout << " done" << std::endl << std::flush;
0169 }
0170 int iov_id = -1;
0171 try {
0172 while (rset->next()) {
0173
0174 iov_id = rset->getInt(1);
0175 }
0176 } catch (oracle::occi::SQLException &e) {
0177 throw(std::runtime_error("[LMFCorrCoefDat::fetchLastInsertedRun]: " + e.getMessage()));
0178 }
0179 if (iov_id > 0) {
0180 iov.setByID(iov_id);
0181 }
0182 return iov;
0183 }
0184
0185 void LMFCorrCoefDat::fetchAfter(const Tm &t) {
0186 Tm tmax;
0187 tmax.setToString("9999-12-31 23:59:59");
0188 fetchBetween(t, tmax, 0);
0189 }
0190
0191 void LMFCorrCoefDat::fetchAfter(const Tm &t, int howmany) {
0192 Tm tmax;
0193 tmax.setToString("9999-12-31 23:59:59");
0194 fetchBetween(t, tmax, howmany);
0195 }
0196
0197 void LMFCorrCoefDat::fetchBetween(const Tm &tmin, const Tm &tmax) { fetchBetween(tmin, tmax, 0); }
0198
0199 void LMFCorrCoefDat::fetchBetween(const Tm &tmin, const Tm &tmax, int maxNumberOfIOVs) {
0200 LMFLmrSubIOV iov(m_env, m_conn);
0201 Tm tinf;
0202 tinf.setToString("9999-12-31 23:59:59");
0203 if (m_debug) {
0204 std::cout << "Searching for data collected after " << tmin.str();
0205 if (tmax != tinf) {
0206 std::cout << " and before " << tmax.str();
0207 }
0208 std::cout << ". Retrieving the first " << maxNumberOfIOVs << " records" << std::endl;
0209 iov.debug();
0210 }
0211 std::list<int> l = iov.getIOVIDsLaterThan(tmin, tmax, maxNumberOfIOVs);
0212 if (m_debug) {
0213 std::cout << "Now we are going to fetch details about "
0214 << "data collected within the above mentioned "
0215 << "LMR_SUB_IOV's" << std::endl;
0216 }
0217 fetch(l);
0218 if (m_debug) {
0219 std::cout << "Fetched a list of " << m_data.size() << " IOVs" << std::endl << std::flush;
0220 }
0221 }
0222
0223 void LMFCorrCoefDat::fetch(std::list<int> subiov_ids) {
0224 std::list<int>::const_iterator i = subiov_ids.begin();
0225 std::list<int>::const_iterator e = subiov_ids.end();
0226 int c = 0;
0227 while (i != e) {
0228 if (m_debug) {
0229 std::cout << "[LMFCorrCoefDat] Fetching data taken "
0230 << "during LMR_SUB_IOV no. " << ++c << std::endl;
0231 }
0232 fetch(*i);
0233 i++;
0234 }
0235 if (m_debug) {
0236 std::cout << "[LMFCorrCoefDat] fetch done for all sub iovs" << std::endl << std::flush;
0237 }
0238 }
0239
0240 void LMFCorrCoefDat::fetch(int subiov_id) {
0241 LMFLmrSubIOV iov(m_env, m_conn);
0242 iov.setByID(subiov_id);
0243 if (m_debug) {
0244 std::cout << "[LMFCorrCoefDat] Looking for LMR_SUB_IOV with ID " << iov.getID() << std::endl << std::flush;
0245 }
0246
0247 LMFLmrSubIOV *subiov = new LMFLmrSubIOV(m_env, m_conn);
0248 *subiov = iov;
0249 m_subiov[subiov_id] = subiov;
0250 if (m_debug) {
0251 std::cout << "[LMFCorrCoefDat] Latest LMR_SUB_IOV data follows" << std::endl;
0252 subiov->dump();
0253 std::cout << "[LMFCorrCoefDat] Fetching data taken "
0254 << "during LMR_SUB_IOV ID " << subiov_id << std::endl
0255 << std::flush;
0256 }
0257 fetch(iov);
0258 }
0259
0260 void LMFCorrCoefDat::fetch(const LMFLmrSubIOV &iov) {
0261
0262 if (m_data.find(iov.getID()) == m_data.end()) {
0263 if (m_debug) {
0264 std::cout << " Data collected in LMR_SUB_IOV " << iov.getID() << " not found in private data. "
0265 << "Getting it from DB " << std::endl
0266 << std::flush;
0267 }
0268 LMFCorrCoefDatComponent *comp = new LMFCorrCoefDatComponent(m_env, m_conn);
0269 if (m_debug) {
0270 comp->debug();
0271 }
0272
0273 comp->setLMFLmrSubIOV(iov);
0274 comp->fetch();
0275 if (m_debug) {
0276 std::cout << "====== DEBUGGING: Data collected during this LMR_SUB_IOV" << std::endl;
0277 comp->dump();
0278 std::cout << "====== DEBUGGING: ======================================" << std::endl << std::endl;
0279 }
0280 m_data[iov.getID()] = comp;
0281 } else if (m_debug) {
0282
0283 std::cout << " Data collected in LMR_SUB_IOV " << iov.getID() << " found in private data. "
0284 << std::endl
0285 << std::flush;
0286 }
0287 if (m_debug) {
0288 std::cout << "[LMFCorrCoefDat] Fetch done" << std::endl << std::endl << std::flush;
0289 }
0290 }
0291
0292 std::vector<Tm> LMFCorrCoefDat::getTimes(const LMFLmrSubIOV &iov) { return iov.getTimes(); }
0293
0294 std::map<int, std::map<int, LMFSextuple> > LMFCorrCoefDat::getCorrections(const Tm &t) {
0295 return getCorrections(t, MAX_NUMBER_OF_SEQUENCES_TO_FETCH);
0296 }
0297
0298 void LMFCorrCoefDat::checkTriplets(int logic_id, const LMFSextuple &s, const std::map<int, LMFSextuple> &lastMap) {
0299
0300
0301 if (lastMap.find(logic_id) != lastMap.end()) {
0302 const LMFSextuple sold = lastMap.find(logic_id)->second;
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 } else {
0313
0314
0315
0316
0317 }
0318 }
0319
0320 std::map<int, std::map<int, LMFSextuple> > LMFCorrCoefDat::getCorrections(const Tm &t, int max) {
0321 return getCorrections(t, Tm().plusInfinity(), max);
0322 }
0323
0324 std::map<int, std::map<int, LMFSextuple> > LMFCorrCoefDat::getCorrections(const Tm &t, const Tm &t2, int max) {
0325
0326
0327
0328
0329
0330
0331
0332
0333 if (m_conn == nullptr) {
0334 throw std::runtime_error(
0335 "[LMFCorrCoefDat::getCorrections] ERROR: "
0336 "Connection not set");
0337 }
0338
0339 if (max > MAX_NUMBER_OF_SEQUENCES_TO_FETCH) {
0340 if (m_debug) {
0341 std::cout << " Required to fetch " << max << " sequences from OMDS. " << MAX_NUMBER_OF_SEQUENCES_TO_FETCH
0342 << " allowed" << std::endl;
0343 }
0344 max = MAX_NUMBER_OF_SEQUENCES_TO_FETCH;
0345 }
0346
0347 std::map<int, std::map<int, LMFSextuple> > ret;
0348 std::string sql =
0349 "SELECT * FROM (SELECT LOGIC_ID, T1, T2, T3, P1, P2, P3, "
0350 "SEQ_ID, D.LMR_SUB_IOV_ID FROM LMF_LMR_SUB_IOV JOIN LMF_CORR_COEF_DAT D ON "
0351 "D.LMR_SUB_IOV_ID = LMF_LMR_SUB_IOV.LMR_SUB_IOV_ID "
0352 "WHERE T1 > TO_DATE(:1, 'YYYY-MM-DD HH24:MI:SS') AND "
0353 "T1 <= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS') ORDER BY T1) WHERE ROWNUM <= :3";
0354 try {
0355 DateHandler dh(m_env, m_conn);
0356 const int PREFETCH = 10000;
0357 oracle::occi::Statement *stmt = m_conn->createStatement();
0358 stmt->setSQL(sql);
0359 int toFetch = (max * (61200 + 14648));
0360 stmt->setString(1, t.str());
0361 stmt->setString(2, t2.str());
0362 stmt->setInt(3, toFetch);
0363 stmt->setPrefetchRowCount(PREFETCH);
0364 if (m_debug) {
0365 std::cout << "[LMFCorrCoefDat::getCorrections] executing query" << std::endl
0366 << sql << std::endl
0367 << "Parameters 1 = " << t.str() << " 2 = " << t2.str() << " 3 = " << toFetch << std::endl
0368 << "Prefetching " << PREFETCH << " rows " << std::endl
0369 << std::flush;
0370 }
0371 oracle::occi::ResultSet *rset = stmt->executeQuery();
0372 if (m_debug) {
0373 std::cout << " done" << std::endl << std::flush;
0374 }
0375 int c = 0;
0376 std::map<int, LMFSextuple> theMap;
0377 int lastSeqId = 0;
0378 int previousSeqId = 0;
0379 int startingSeqId = -1;
0380 LMFSextuple s;
0381 bool proceed = true;
0382 while (rset->next()) {
0383 int logic_id = rset->getInt(1);
0384 int seq_id = rset->getInt(8);
0385 if (startingSeqId < 0) {
0386 startingSeqId = seq_id;
0387 }
0388 if ((seq_id == 0) && (startingSeqId == 0)) {
0389
0390
0391 if (c == 0) {
0392 std::cout << "[LMFCorrCoefDat::getCorrections] Using fixed-time IOV" << std::endl;
0393 }
0394 seq_id = rset->getInt(9);
0395 } else if ((startingSeqId == 0) && (proceed = true)) {
0396 std::cout << "[LMFCorrCoefDat::getCorrections] Switch to normal (sequence based) mode. "
0397 << "Exiting..." << std::endl;
0398 proceed = false;
0399 } else if ((seq_id == 0) && (proceed = true)) {
0400 std::cout << "[LMFCorrCoefDat::getCorrections] Switch to fixed-time IOV mode. "
0401 << "Exiting..." << std::endl;
0402 proceed = false;
0403 }
0404 if (proceed) {
0405 if (seq_id != lastSeqId) {
0406 if (m_debug) {
0407 if (lastSeqId != 0) {
0408 std::cout << " Triplets in sequences: " << c << std::endl;
0409 std::cout << " T1: " << s.t[0].str() << " T2: " << s.t[1].str() << " T3: " << s.t[2].str()
0410 << std::endl;
0411 }
0412 c = 0;
0413 std::cout << " Found new sequence: " << seq_id << std::endl;
0414 }
0415
0416
0417 for (int i = 0; i < 3; i++) {
0418 oracle::occi::Date d = rset->getDate(i + 2);
0419 s.t[i] = dh.dateToTm(d);
0420 }
0421 if (lastSeqId > 0) {
0422 ret[lastSeqId] = theMap;
0423 }
0424 theMap.clear();
0425 previousSeqId = lastSeqId;
0426 lastSeqId = seq_id;
0427 }
0428 for (int i = 0; i < 3; i++) {
0429 s.p[i] = rset->getDouble(i + 5);
0430 }
0431 theMap[logic_id] = s;
0432
0433 if (!ret.empty()) {
0434 checkTriplets(logic_id, s, ret[previousSeqId]);
0435 }
0436 c++;
0437 }
0438 }
0439
0440 ret[lastSeqId] = theMap;
0441 if (m_debug) {
0442 std::cout << " Triplets in sequences: " << c << std::endl;
0443 std::cout << " T1: " << s.t[0].str() << " T2: " << s.t[1].str() << " T3: " << s.t[2].str() << std::endl;
0444 std::cout << std::endl;
0445 }
0446 } catch (oracle::occi::SQLException &e) {
0447 throw(std::runtime_error("LMFCorrCoefDat::getCorrections: " + e.getMessage()));
0448 }
0449 if (m_debug) {
0450 std::cout << "[LMFCorrCoefDat::getCorrections] Map built" << std::endl
0451 << " Contains " << ret.size()
0452 << " sequences. These are the size of all sequences" << std::endl;
0453 std::map<int, std::map<int, LMFSextuple> >::const_iterator i = ret.begin();
0454 std::map<int, std::map<int, LMFSextuple> >::const_iterator e = ret.end();
0455 while (i != e) {
0456 std::cout << " SEQ " << i->first << " Size " << i->second.size() << std::endl;
0457 i++;
0458 }
0459 }
0460 return ret;
0461 }
0462
0463 std::list<std::vector<float> > LMFCorrCoefDat::getParameters(const EcalLogicID &id) {
0464 return getParameters(id.getLogicID());
0465 }
0466
0467 std::list<std::vector<float> > LMFCorrCoefDat::getParameters(int id) {
0468 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
0469 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
0470 std::list<std::vector<float> > ret;
0471 while (i != e) {
0472 std::list<int> logic_ids = i->second->getLogicIds();
0473 std::list<int>::iterator p = std::find(logic_ids.begin(), logic_ids.end(), id);
0474 if (p != logic_ids.end()) {
0475
0476 std::vector<float> ppar;
0477 std::vector<Tm> tpar;
0478
0479 i->second->getData(id, ppar);
0480 tpar = m_subiov[i->first]->getTimes();
0481
0482 std::vector<float> par(6);
0483 for (int k = 0; k < 3; k++) {
0484 par[k + 3] = ppar[k];
0485 par[k] = tpar[k].microsTime();
0486 }
0487 ret.push_back(par);
0488 }
0489 i++;
0490 }
0491 return ret;
0492 }
0493
0494 std::vector<float> LMFCorrCoefDat::getParameters(const LMFLmrSubIOV &iov, const EcalLogicID &id) {
0495 std::vector<float> x(3);
0496 int key = iov.getID();
0497 fetch(iov);
0498 if (m_data.find(key) != m_data.end()) {
0499 x = (m_data.find(key)->second)->getParameters(id);
0500 }
0501 return x;
0502 }
0503
0504 std::vector<float> LMFCorrCoefDat::getParameterErrors(const LMFLmrSubIOV &iov, const EcalLogicID &id) {
0505 std::vector<float> x;
0506 int key = iov.getID();
0507 fetch(iov);
0508 if (m_data.find(key) != m_data.end()) {
0509 x = (m_data.find(key)->second)->getParameterErrors(id);
0510 }
0511 return x;
0512 }
0513
0514 int LMFCorrCoefDat::getFlag(const LMFLmrSubIOV &iov, const EcalLogicID &id) {
0515 int x = -1;
0516 fetch(iov);
0517 if (m_data.find(iov.getID()) != m_data.end()) {
0518 x = (m_data.find(iov.getID())->second)->getFlag(id);
0519 }
0520 return x;
0521 }
0522
0523 int LMFCorrCoefDat::getSeqID(const LMFLmrSubIOV &iov, const EcalLogicID &id) {
0524 int x = -1;
0525 fetch(iov);
0526 if (m_data.find(iov.getID()) != m_data.end()) {
0527 x = (m_data.find(iov.getID())->second)->getSeqID(id);
0528 }
0529 return x;
0530 }
0531
0532 LMFSeqDat LMFCorrCoefDat::getSequence(const LMFLmrSubIOV &iov, const EcalLogicID &id) {
0533 LMFSeqDat seq(m_env, m_conn);
0534 fetch(iov);
0535 if (m_data.find(iov.getID()) != m_data.end()) {
0536 seq = (m_data.find(iov.getID())->second)->getSequence(id);
0537 }
0538 return seq;
0539 }
0540
0541 int LMFCorrCoefDat::size() const {
0542 int c = 0;
0543 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
0544 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
0545 while (i != e) {
0546 c += i->second->size();
0547 i++;
0548 }
0549 return c;
0550 }
0551
0552 std::list<int> LMFCorrCoefDat::getSubIOVIDs() {
0553 std::list<int> ret;
0554 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
0555 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
0556 while (i != e) {
0557 ret.push_back(i->first);
0558 i++;
0559 }
0560 return ret;
0561 }