00001 #include "OnlineDB/EcalCondDB/interface/LMFCorrCoefDat.h"
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003
00004 LMFCorrCoefDat::LMFCorrCoefDat() {
00005 init();
00006 }
00007
00008 LMFCorrCoefDat::LMFCorrCoefDat(EcalDBConnection *c) {
00009 init();
00010 m_env = c->getEnv();
00011 m_conn = c->getConn();
00012 }
00013
00014 LMFCorrCoefDat::LMFCorrCoefDat(oracle::occi::Environment* env,
00015 oracle::occi::Connection* conn) {
00016 init();
00017 m_env = env;
00018 m_conn = conn;
00019 }
00020
00021 LMFCorrCoefDat::~LMFCorrCoefDat() {
00022 std::map<int, LMFCorrCoefDatComponent*>::iterator i = m_data.begin();
00023 std::map<int, LMFCorrCoefDatComponent*>::iterator e = m_data.end();
00024 while (i != e) {
00025 delete i->second;
00026 i++;
00027 }
00028 m_data.clear();
00029 std::map<int, LMFLmrSubIOV*>::iterator si = m_subiov.begin();
00030 std::map<int, LMFLmrSubIOV*>::iterator se = m_subiov.end();
00031 while (si != se) {
00032 delete si->second;
00033 si++;
00034 }
00035 m_subiov.clear();
00036 }
00037
00038 void LMFCorrCoefDat::init() {
00039 m_data.clear();
00040 m_subiov.clear();
00041 m_env = NULL;
00042 m_conn = NULL;
00043 nodebug();
00044 }
00045
00046 LMFCorrCoefDat& LMFCorrCoefDat::setConnection(oracle::occi::Environment* env,
00047 oracle::occi::Connection* conn) {
00048 m_env = env;
00049 m_conn = conn;
00050 std::map<int, LMFCorrCoefDatComponent*>::iterator i = m_data.begin();
00051 std::map<int, LMFCorrCoefDatComponent*>::iterator e = m_data.end();
00052 while (i != e) {
00053 i->second->setConnection(m_env, m_conn);
00054 i++;
00055 }
00056 return *this;
00057 }
00058
00059 LMFCorrCoefDat& LMFCorrCoefDat::setP123(const LMFLmrSubIOV &iov,
00060 const EcalLogicID &id, float p1, float p2, float p3) {
00061 find(iov)->setP123(id, p1, p2, p3);
00062 return *this;
00063 }
00064
00065 LMFCorrCoefDat& LMFCorrCoefDat::setP123(const LMFLmrSubIOV &iov, const EcalLogicID &id,
00066 float p1, float p2, float p3,
00067 float p1e, float p2e, float p3e) {
00068 find(iov)->setP123(id, p1, p2, p3, p1e, p2e, p3e);
00069 return *this;
00070 }
00071
00072 LMFCorrCoefDat& LMFCorrCoefDat::setP123Errors(const LMFLmrSubIOV &iov,
00073 const EcalLogicID &id, float p1e, float p2e,
00074 float p3e) {
00075 find(iov)->setP123Errors(id, p1e, p2e, p3e);
00076 return *this;
00077 }
00078
00079 LMFCorrCoefDat& LMFCorrCoefDat::setFlag(const LMFLmrSubIOV & iov,
00080 const EcalLogicID &id, int flag) {
00081 find(iov)->setFlag(id, flag);
00082 return *this;
00083 }
00084
00085 LMFCorrCoefDat& LMFCorrCoefDat::setSequence(const LMFLmrSubIOV & iov,
00086 const EcalLogicID &id, int seq_id) {
00087 find(iov)->setSequence(id, seq_id);
00088 return *this;
00089 }
00090
00091 LMFCorrCoefDat& LMFCorrCoefDat::setSequence(const LMFLmrSubIOV & iov,
00092 const EcalLogicID &id,
00093 const LMFSeqDat &seq) {
00094 find(iov)->setSequence(id, seq);
00095 return *this;
00096 }
00097
00098 LMFCorrCoefDatComponent* LMFCorrCoefDat::find(const LMFLmrSubIOV &iov) {
00099 if (m_data.find(iov.getID()) != m_data.end()) {
00100 return m_data[iov.getID()];
00101 } else {
00102 LMFCorrCoefDatComponent *c = new LMFCorrCoefDatComponent();
00103 LMFLmrSubIOV *subiov = new LMFLmrSubIOV();
00104 if (m_conn != NULL) {
00105 c->setConnection(m_env, m_conn);
00106 subiov->setConnection(m_env, m_conn);
00107 }
00108 *subiov = iov;
00109 c->setLMFLmrSubIOV(*subiov);
00110 m_data[subiov->getID()] = c;
00111 m_subiov[subiov->getID()] = subiov;
00112 return c;
00113 }
00114 }
00115
00116 void LMFCorrCoefDat::dump() {
00117 std::cout << std::endl;
00118 std::cout << "##################### LMF_CORR_COEF_DAT ########################" << std::endl;
00119 std::cout << "This structure contains " << m_data.size() << " LMR_SUB_IOV_ID" << std::endl;
00120 std::map<int, LMFCorrCoefDatComponent*>::const_iterator i = m_data.begin();
00121 std::map<int, LMFCorrCoefDatComponent*>::const_iterator e = m_data.end();
00122 int count = 0;
00123 while (i != e) {
00124 std::cout << "### SUB IOV ID: " << i->second->getLMFLmrSubIOVID() << std::endl;
00125 std::list<int> logic_ids = i->second->getLogicIds();
00126 std::cout << " Contains data for " << logic_ids.size() << " xtals" << std::endl;
00127 count += logic_ids.size();
00128 i++;
00129 }
00130 std::cout << "Total no. of xtals for which data are stored: " << count << std::endl;
00131 std::cout << "##################### LMF_CORR_COEF_DAT ########################" << std::endl;
00132 }
00133
00134 void LMFCorrCoefDat::writeDB() {
00135 std::map<int, LMFCorrCoefDatComponent*>::iterator i = m_data.begin();
00136 std::map<int, LMFCorrCoefDatComponent*>::iterator e = m_data.end();
00137 while (i != e) {
00138 if (m_debug) {
00139 std::cout << "Writing data for LMR_SUB_IOV_ID " << i->first << std::endl;
00140 }
00141 i->second->writeDB();
00142 i++;
00143 }
00144 }
00145
00146 void LMFCorrCoefDat::debug() {
00147 std::cout << "Set debug" << std::endl << std::flush;
00148 m_debug = true;
00149 }
00150
00151 void LMFCorrCoefDat::nodebug() {
00152 m_debug = false;
00153 }
00154
00155 RunIOV LMFCorrCoefDat::fetchLastInsertedRun() {
00156 RunIOV iov;
00157 if (m_conn == NULL) {
00158 throw std::runtime_error("[LMFCorrCoefDat::fetchLastInsertedRun] ERROR: "
00159 "Connection not set");
00160 }
00161 iov.setConnection(m_env, m_conn);
00162 std::string sql = "SELECT IOV_ID FROM CMS_ECAL_COND.RUN_IOV WHERE "
00163 "IOV_ID = (SELECT RUN_IOV_ID FROM LMF_SEQ_DAT WHERE SEQ_ID = "
00164 "(SELECT MAX(SEQ_ID) FROM LMF_CORR_COEF_DAT))";
00165 oracle::occi::Statement * stmt;
00166 try {
00167 stmt = m_conn->createStatement();
00168 stmt->setSQL(sql);
00169 }
00170 catch (oracle::occi::SQLException &e) {
00171 throw(std::runtime_error("[LMFCorrCoefDat::fetchLastInsertedRun]: " +
00172 e.getMessage()));
00173 }
00174 if (m_debug) {
00175 std::cout << "[LMFCorrCoefDat::fetchLastInsertedRun] executing query"
00176 << std::endl << sql << std::endl << std::flush;
00177 }
00178 oracle::occi::ResultSet *rset = stmt->executeQuery();
00179 if (m_debug) {
00180 std::cout << " done"
00181 << std::endl << std::flush;
00182 }
00183 int iov_id = -1;
00184 try {
00185 while (rset->next()) {
00186
00187 iov_id = rset->getInt(1);
00188 }
00189 }
00190 catch (oracle::occi::SQLException &e) {
00191 throw(std::runtime_error("[LMFCorrCoefDat::fetchLastInsertedRun]: " +
00192 e.getMessage()));
00193 }
00194 if (iov_id > 0) {
00195 iov.setByID(iov_id);
00196 }
00197 return iov;
00198 }
00199
00200 void LMFCorrCoefDat::fetchAfter(const Tm &t) {
00201 Tm tmax;
00202 tmax.setToString("9999-12-31 23:59:59");
00203 fetchBetween(t, tmax, 0);
00204 }
00205
00206 void LMFCorrCoefDat::fetchAfter(const Tm &t, int howmany) {
00207 Tm tmax;
00208 tmax.setToString("9999-12-31 23:59:59");
00209 fetchBetween(t, tmax, howmany);
00210 }
00211
00212 void LMFCorrCoefDat::fetchBetween(const Tm &tmin, const Tm &tmax) {
00213 fetchBetween(tmin, tmax, 0);
00214 }
00215
00216 void LMFCorrCoefDat::fetchBetween(const Tm &tmin, const Tm &tmax,
00217 int maxNumberOfIOVs) {
00218 LMFLmrSubIOV iov(m_env, m_conn);
00219 Tm tinf;
00220 tinf.setToString("9999-12-31 23:59:59");
00221 if (m_debug) {
00222 std::cout << "Searching for data collected after " << tmin.str();
00223 if (tmax != tinf) {
00224 std::cout << " and before " << tmax.str();
00225 }
00226 std::cout << ". Retrieving the first "
00227 << maxNumberOfIOVs << " records" << std::endl;
00228 iov.debug();
00229 }
00230 std::list<int> l = iov.getIOVIDsLaterThan(tmin, tmax, maxNumberOfIOVs);
00231 if (m_debug) {
00232 std::cout << "Now we are going to fetch details about "
00233 << "data collected within the above mentioned "
00234 << "LMR_SUB_IOV's" << std::endl;
00235 }
00236 fetch(l);
00237 if (m_debug) {
00238 std::cout << "Fetched a list of " << m_data.size() << " IOVs"
00239 << std::endl << std::flush;
00240 }
00241 }
00242
00243 void LMFCorrCoefDat::fetch(std::list<int> subiov_ids) {
00244 std::list<int>::const_iterator i = subiov_ids.begin();
00245 std::list<int>::const_iterator e = subiov_ids.end();
00246 int c = 0;
00247 while (i != e) {
00248 if (m_debug) {
00249 std::cout << "[LMFCorrCoefDat] Fetching data taken "
00250 << "during LMR_SUB_IOV no. " << ++c << std::endl;
00251 }
00252 fetch(*i);
00253 i++;
00254 }
00255 if (m_debug) {
00256 std::cout << "[LMFCorrCoefDat] fetch done for all sub iovs"
00257 << std::endl << std::flush;
00258 }
00259 }
00260
00261 void LMFCorrCoefDat::fetch(int subiov_id) {
00262 LMFLmrSubIOV iov(m_env, m_conn);
00263 iov.setByID(subiov_id);
00264 if (m_debug) {
00265 std::cout << "[LMFCorrCoefDat] Looking for LMR_SUB_IOV with ID "
00266 << iov.getID() << std::endl
00267 << std::flush;
00268 }
00269
00270 LMFLmrSubIOV *subiov = new LMFLmrSubIOV(m_env, m_conn);
00271 *subiov = iov;
00272 m_subiov[subiov_id] = subiov;
00273 if (m_debug) {
00274 std::cout << "[LMFCorrCoefDat] Latest LMR_SUB_IOV data follows"
00275 << std::endl;
00276 subiov->dump();
00277 std::cout << "[LMFCorrCoefDat] Fetching data taken "
00278 << "during LMR_SUB_IOV ID " << subiov_id << std::endl
00279 << std::flush;
00280 }
00281 fetch(iov);
00282 }
00283
00284 void LMFCorrCoefDat::fetch(const LMFLmrSubIOV &iov)
00285 {
00286
00287 if (m_data.find(iov.getID()) == m_data.end()) {
00288 if (m_debug) {
00289 std::cout << " Data collected in LMR_SUB_IOV "
00290 << iov.getID()
00291 << " not found in private data. "
00292 << "Getting it from DB " << std::endl
00293 << std::flush;
00294 }
00295 LMFCorrCoefDatComponent *comp = new LMFCorrCoefDatComponent(m_env, m_conn);
00296 if (m_debug) {
00297 comp->debug();
00298 }
00299
00300 comp->setLMFLmrSubIOV(iov);
00301 comp->fetch();
00302 if (m_debug) {
00303 std::cout << "====== DEBUGGING: Data collected during this LMR_SUB_IOV"
00304 << std::endl;
00305 comp->dump();
00306 std::cout << "====== DEBUGGING: ======================================"
00307 << std::endl << std::endl;
00308 }
00309 m_data[iov.getID()] = comp;
00310 } else if (m_debug) {
00311
00312 std::cout << " Data collected in LMR_SUB_IOV "
00313 << iov.getID()
00314 << " found in private data. "
00315 << std::endl << std::flush;
00316 }
00317 if (m_debug) {
00318 std::cout << "[LMFCorrCoefDat] Fetch done" << std::endl << std::endl << std::flush;
00319 }
00320 }
00321
00322 std::vector<Tm> LMFCorrCoefDat::getTimes(const LMFLmrSubIOV &iov) {
00323 return iov.getTimes();
00324 }
00325
00326 std::map<int, std::map<int, LMFSextuple> >
00327 LMFCorrCoefDat::getCorrections(const Tm &t) {
00328 return getCorrections(t, MAX_NUMBER_OF_SEQUENCES_TO_FETCH);
00329 }
00330
00331 void LMFCorrCoefDat::checkTriplets(int logic_id, const LMFSextuple &s,
00332 const std::map<int, LMFSextuple> &lastMap)
00333 {
00334
00335
00336 if (lastMap.find(logic_id) != lastMap.end()) {
00337 const LMFSextuple sold = lastMap.find(logic_id)->second;
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 } else {
00348 std::cout << ":-( Can't find crystal " << logic_id << " in last map"
00349 << std::endl;
00350 }
00351 }
00352
00353 std::map<int, std::map<int, LMFSextuple> >
00354 LMFCorrCoefDat::getCorrections(const Tm &t, int max) {
00355 return getCorrections(t, Tm().plusInfinity(), max);
00356 }
00357
00358 std::map<int, std::map<int, LMFSextuple> >
00359 LMFCorrCoefDat::getCorrections(const Tm &t, const Tm &t2, int max) {
00360
00361
00362
00363
00364
00365 if (m_conn == NULL) {
00366 throw std::runtime_error("[LMFCorrCoefDat::getCorrections] ERROR: "
00367 "Connection not set");
00368 }
00369
00370 if (max > MAX_NUMBER_OF_SEQUENCES_TO_FETCH) {
00371 if (m_debug) {
00372 std::cout << " Required to fetch " << max << " sequences from OMDS. "
00373 << MAX_NUMBER_OF_SEQUENCES_TO_FETCH << " allowed"
00374 << std::endl;
00375 }
00376 max = MAX_NUMBER_OF_SEQUENCES_TO_FETCH;
00377 }
00378
00379 std::map<int, std::map<int, LMFSextuple> > ret;
00380 std::string sql = "SELECT * FROM (SELECT LOGIC_ID, T1, T2, T3, P1, P2, P3, "
00381 "SEQ_ID FROM LMF_LMR_SUB_IOV JOIN LMF_CORR_COEF_DAT ON "
00382 "LMF_CORR_COEF_DAT.LMR_SUB_IOV_ID = LMF_LMR_SUB_IOV.LMR_SUB_IOV_ID "
00383 "WHERE T1 > :1 AND T1 <= :2 ORDER BY T1) WHERE ROWNUM <= :3";
00384 try {
00385 DateHandler dh(m_env, m_conn);
00386 oracle::occi::Statement * stmt = m_conn->createStatement();
00387 stmt->setSQL(sql);
00388 int toFetch = max * (61200 + 14648);
00389 stmt->setDate(1, dh.tmToDate(t));
00390 stmt->setDate(2, dh.tmToDate(t2));
00391 stmt->setInt(3, toFetch);
00392 stmt->setPrefetchRowCount(toFetch);
00393 if (m_debug) {
00394 std::cout << "[LMFCorrCoefDat::getCorrections] executing query"
00395 << std::endl << sql << std::endl
00396 << "Prefetching " << toFetch << " rows "
00397 << std::endl << std::flush;
00398 }
00399 oracle::occi::ResultSet *rset = stmt->executeQuery();
00400 if (m_debug) {
00401 std::cout << " done"
00402 << std::endl << std::flush;
00403 }
00404 int c = 0;
00405 std::map<int, LMFSextuple> theMap;
00406 int lastSeqId = 0;
00407 int previousSeqId = 0;
00408 LMFSextuple s;
00409 while (rset->next()) {
00410 int logic_id = rset->getInt(1);
00411 int seq_id = rset->getInt(8);
00412 if (seq_id != lastSeqId) {
00413 if (m_debug) {
00414 if (lastSeqId != 0) {
00415 std::cout << " Triplets in sequences: " << c
00416 << std::endl;
00417 std::cout << " T1: " << s.t[0].str() << " T2: " << s.t[1].str()
00418 << " T3: " << s.t[2].str() << std::endl;
00419 }
00420 c = 0;
00421 std::cout << " Found new sequence: " << seq_id
00422 << std::endl;
00423 }
00424
00425
00426 for (int i = 0; i < 3; i++) {
00427 oracle::occi::Date d = rset->getDate(i + 2);
00428 s.t[i] = dh.dateToTm(d);
00429 }
00430 if (lastSeqId > 0) {
00431 ret[lastSeqId] = theMap;
00432 }
00433 theMap.clear();
00434 previousSeqId = lastSeqId;
00435 lastSeqId = seq_id;
00436 }
00437 for (int i = 0; i <3; i++) {
00438 s.p[i] = rset->getDouble(i + 5);
00439 }
00440 theMap[logic_id] = s;
00441
00442 if (ret.size() > 0) {
00443 checkTriplets(logic_id, s, ret[previousSeqId]);
00444 }
00445 c++;
00446 }
00447
00448 ret[lastSeqId] = theMap;
00449 if (m_debug) {
00450 std::cout << " Triplets in sequences: " << c
00451 << std::endl;
00452 std::cout << " T1: " << s.t[0].str() << " T2: " << s.t[1].str()
00453 << " T3: " << s.t[2].str() << std::endl;
00454 std::cout << std::endl;
00455 }
00456 }
00457 catch (oracle::occi::SQLException &e) {
00458 throw(std::runtime_error("LMFCorrCoefDat::getCorrections: " +
00459 e.getMessage()));
00460 }
00461 if (m_debug) {
00462 std::cout << "[LMFCorrCoefDat::getCorrections] Map built" << std::endl
00463 << " Contains " << ret.size()
00464 << " sequences. These are the size of all sequences"
00465 << std::endl;
00466 std::map<int, std::map<int, LMFSextuple> >::const_iterator i = ret.begin();
00467 std::map<int, std::map<int, LMFSextuple> >::const_iterator e = ret.end();
00468 while (i != e) {
00469 std::cout << " SEQ " << i->first
00470 << " Size " << i->second.size() << std::endl;
00471 i++;
00472 }
00473 }
00474 return ret;
00475 }
00476
00477 std::list<std::vector<float> > LMFCorrCoefDat::getParameters(const EcalLogicID
00478 &id) {
00479 return getParameters(id.getLogicID());
00480 }
00481
00482
00483 std::list<std::vector<float> > LMFCorrCoefDat::getParameters(int id) {
00484 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
00485 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
00486 std::list<std::vector<float> > ret;
00487 while (i != e) {
00488 std::list<int> logic_ids = i->second->getLogicIds();
00489 std::list<int>::iterator p =
00490 std::find(logic_ids.begin(), logic_ids.end(), id);
00491 if (p != logic_ids.end()) {
00492
00493 std::vector<float> ppar;
00494 std::vector<Tm> tpar;
00495
00496 i->second->getData(id, ppar);
00497 tpar = m_subiov[i->first]->getTimes();
00498
00499 std::vector<float> par(6);
00500 for (int k = 0; k < 3; k++) {
00501 par[k + 3] = ppar[k];
00502 par[k] = tpar[k].microsTime();
00503 }
00504 ret.push_back(par);
00505 }
00506 i++;
00507 }
00508 return ret;
00509 }
00510
00511 std::vector<float> LMFCorrCoefDat::getParameters(const LMFLmrSubIOV &iov,
00512 const EcalLogicID &id) {
00513 std::vector<float> x(3);
00514 int key = iov.getID();
00515 fetch(iov);
00516 if (m_data.find(key) != m_data.end()) {
00517 x = (m_data.find(key)->second)->getParameters(id);
00518 }
00519 return x;
00520 }
00521
00522 std::vector<float> LMFCorrCoefDat::getParameterErrors(const LMFLmrSubIOV &iov,
00523 const EcalLogicID &id)
00524 {
00525 std::vector<float> x;
00526 int key = iov.getID();
00527 fetch(iov);
00528 if (m_data.find(key) != m_data.end()) {
00529 x = (m_data.find(key)->second)->getParameterErrors(id);
00530 }
00531 return x;
00532 }
00533
00534 int LMFCorrCoefDat::getFlag(const LMFLmrSubIOV &iov,
00535 const EcalLogicID &id) {
00536 int x = -1;
00537 fetch(iov);
00538 if (m_data.find(iov.getID()) != m_data.end()) {
00539 x = (m_data.find(iov.getID())->second)->getFlag(id);
00540 }
00541 return x;
00542 }
00543
00544 int LMFCorrCoefDat::getSeqID(const LMFLmrSubIOV &iov,
00545 const EcalLogicID &id) {
00546 int x = -1;
00547 fetch(iov);
00548 if (m_data.find(iov.getID()) != m_data.end()) {
00549 x = (m_data.find(iov.getID())->second)->getSeqID(id);
00550 }
00551 return x;
00552 }
00553
00554 LMFSeqDat LMFCorrCoefDat::getSequence(const LMFLmrSubIOV &iov,
00555 const EcalLogicID &id) {
00556 LMFSeqDat seq(m_env, m_conn);
00557 fetch(iov);
00558 if (m_data.find(iov.getID()) != m_data.end()) {
00559 seq = (m_data.find(iov.getID())->second)->getSequence(id);
00560 }
00561 return seq;
00562 }
00563
00564 int LMFCorrCoefDat::size() const {
00565 int c = 0;
00566 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
00567 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
00568 while (i != e) {
00569 c += i->second->size();
00570 i++;
00571 }
00572 return c;
00573 }
00574
00575 std::list<int> LMFCorrCoefDat::getSubIOVIDs() {
00576 std::list<int> ret;
00577 std::map<int, LMFCorrCoefDatComponent *>::const_iterator i = m_data.begin();
00578 std::map<int, LMFCorrCoefDatComponent *>::const_iterator e = m_data.end();
00579 while (i != e) {
00580 ret.push_back(i->first);
00581 i++;
00582 }
00583 return ret;
00584 }