00001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
00002 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00003 #include "OnlineDB/EcalCondDB/interface/LMFSextuple.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00005 #include "CondCore/DBCommon/interface/Time.h"
00006 #include "DataFormats/Provenance/interface/Timestamp.h"
00007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00008
00009 #include<iostream>
00010 #include<iomanip>
00011 #include <sstream>
00012
00013 popcon::EcalLaserHandler::EcalLaserHandler(const edm::ParameterSet & ps)
00014 : m_name(ps.getUntrackedParameter<std::string>("name","EcalLaserHandler")) {
00015
00016 std::cout << "EcalLaser Source handler constructor\n" << std::endl;
00017
00018 m_sequences = 1;
00019
00020 m_sid= ps.getParameter<std::string>("OnlineDBSID");
00021 m_user= ps.getParameter<std::string>("OnlineDBUser");
00022 m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00023 m_debug=ps.getParameter<bool>("debug");
00024 m_sequences=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("sequences").c_str()));
00025 m_maxtime=ps.getParameter<std::string>("maxtime").c_str();
00026 std::cout << "Starting O2O process on DB: " << m_sid
00027 << " User: "<< m_user << std::endl;
00028 }
00029
00030 popcon::EcalLaserHandler::~EcalLaserHandler()
00031 {
00032
00033 }
00034
00035 double popcon::EcalLaserHandler::diff(float a, float b) {
00036 return std::abs(b- a)/a;
00037 }
00038
00039 void popcon::EcalLaserHandler::notifyProblems(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old,
00040 const EcalLaserAPDPNRatios::EcalLaserAPDPNpair ¤t,
00041 int hashedIndex, const std::string &reason) {
00042 std::cout << "===== " << reason << " =====" << std::endl;
00043 if (hashedIndex < 0) {
00044 EEDetId ee;
00045 std::cout << "Triplets for " << ee.unhashIndex(-hashedIndex) << " bad: [" << old.p1 << ", "
00046 << old.p2 << ", " << old.p3 << "] ==> [" << current.p1 << ", "
00047 << current.p2 << ", " << current.p3 << "]" << std::endl;
00048 } else {
00049 EBDetId eb;
00050 std::cout << "Triplets for " << eb.unhashIndex(hashedIndex) << " bad: [" << old.p1 << ", "
00051 << old.p2 << ", " << old.p3 << "] ==> [" << current.p1 << ", "
00052 << current.p2 << ", " << current.p3 << "]" << std::endl;
00053 }
00054 }
00055
00056 bool popcon::EcalLaserHandler::checkAPDPN(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old,
00057 const EcalLaserAPDPNRatios::EcalLaserAPDPNpair ¤t,
00058 int hashedIndex) {
00059 bool ret = true;
00060 if ((current.p1 < 0) || (current.p2 < 0) || (current.p3 < 0)) {
00061 ret = false;
00062 notifyProblems(old, current, hashedIndex, "Negative values");
00063 } else if ((current.p1 > 10) || (current.p2 > 10) || (current.p3 > 0)) {
00064 ret = false;
00065 notifyProblems(old, current, hashedIndex, "Values too large");
00066 } else if (((diff(old.p1, current.p1) > 0.2) && (old.p1 != 0) && (old.p1 != 1)) ||
00067 ((diff(old.p2, current.p2) > 0.2) && (old.p2 != 0) && (old.p1 != 2)) ||
00068 ((diff(old.p3, current.p3) > 0.2) && (old.p3 != 0) && (old.p1 != 3))) {
00069 ret = false;
00070 notifyProblems(old, current, hashedIndex, "Difference w.r.t. previous too large");
00071 }
00072 return ret;
00073 }
00074
00075 bool popcon::EcalLaserHandler::checkAPDPNs(const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &laserMap,
00076 const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &apdpns_popcon) {
00077 bool ret = true;
00078 for (int hashedIndex = 0; hashedIndex < 61200; hashedIndex++) {
00079 EcalLaserAPDPNRatios::EcalLaserAPDPNpair old = laserMap.barrel(hashedIndex);
00080 EcalLaserAPDPNRatios::EcalLaserAPDPNpair current = apdpns_popcon.barrel(hashedIndex);
00081 ret = checkAPDPN(old, current, hashedIndex);
00082 }
00083 for (int hashedIndex = 0; hashedIndex < 14648; hashedIndex++) {
00084 EcalLaserAPDPNRatios::EcalLaserAPDPNpair old = laserMap.endcap(hashedIndex);
00085 EcalLaserAPDPNRatios::EcalLaserAPDPNpair current = apdpns_popcon.endcap(hashedIndex);
00086 ret = checkAPDPN(old, current, -hashedIndex);
00087 }
00088 return ret;
00089 }
00090
00091 void popcon::EcalLaserHandler::dumpBarrelPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap) {
00092 int c = 0;
00093 EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator i = laserMap.barrelItems().begin();
00094 EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator e = laserMap.barrelItems().end();
00095 EBDetId eb;
00096 try {
00097 EcalCondDBInterface *econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00098 while (i != e) {
00099 if (c % 1000 == 0) {
00100 std::cout << std::setw(5) << c << ": " << eb.unhashIndex(c) << " "
00101 << econn->getEcalLogicID("EB_crystal_angle", eb.unhashIndex(c).ieta(),
00102 eb.unhashIndex(c).iphi(), EcalLogicID::NULLID,
00103 "EB_crystal_number").getLogicID()
00104 << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9)
00105 << i->p1 << " " << i->p2 << " " << i->p3 << std::endl;
00106 }
00107 i++;
00108 c++;
00109 }
00110 delete econn;
00111 }
00112 catch (std::runtime_error &e) {
00113 std::cerr << e.what() << std::endl;
00114 delete econn;
00115 throw cms::Exception("OMDS not available");
00116 }
00117 }
00118
00119 void popcon::EcalLaserHandler::dumpEndcapPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap) {
00120 int c = 0;
00121 EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator i = laserMap.endcapItems().begin();
00122 EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator e = laserMap.endcapItems().end();
00123 EEDetId ee;
00124 try {
00125 EcalCondDBInterface *econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00126 while (i != e) {
00127 if (c % 1000 == 0) {
00128 std::cout << std::setw(5) << c << ": " << ee.unhashIndex(c) << " "
00129 << econn->getEcalLogicID("EE_crystal_number", ee.unhashIndex(c).zside(),
00130 ee.unhashIndex(c).ix(), ee.unhashIndex(c).iy(),
00131 "EE_crystal_number").getLogicID()
00132 << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9)
00133 << i->p1 << " " << i->p2 << " " << i->p3 << std::endl;
00134 }
00135 i++;
00136 c++;
00137 }
00138 delete econn;
00139 }
00140 catch (std::runtime_error &e) {
00141 std::cerr << e.what() << std::endl;
00142 delete econn;
00143 throw cms::Exception("OMDS not available");
00144 }
00145 }
00146
00147 void popcon::EcalLaserHandler::getNewObjects()
00148 {
00149 std::cerr << "------- " << m_name
00150 << " ---> getNewObjects" << std::endl;
00151
00152 std::cout << "------- Ecal -> getNewObjects\n";
00153
00154
00155 unsigned long long max_since= 1;
00156 Ref payload= lastPayload();
00157
00158
00159
00160 max_since=tagInfo().lastInterval.first;
00161 Tm max_since_tm(max_since);
00162
00163
00164 edm::Timestamp t_min= edm::Timestamp(18446744073709551615ULL);
00165
00166 const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& laserRatiosMap =
00167 payload->getLaserMap();
00168 std::cout << "payload->getLaserMap(): OK " << std::endl;
00169 std::cout << "Its size is " << laserRatiosMap.size() << std::endl;
00170 const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& laserTimeMap =
00171 payload->getTimeMap();
00172 std::cout << "payload->getTimeMap(): OK " << std::endl;
00173 std::cout << "Last Object in Offline DB has SINCE = " << max_since
00174 << " (" << max_since_tm.str() << ")"
00175 << " and SIZE = " << tagInfo().size
00176 << std::endl;
00177
00178
00179 dumpBarrelPayload(laserRatiosMap);
00180 dumpEndcapPayload(laserRatiosMap);
00181 for (int i=0; i<92; i++) {
00182 EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp = laserTimeMap[i];
00183 if( t_min > timestamp.t1) {
00184 t_min=timestamp.t1;
00185 }
00186 }
00187
00188 std::cout <<"WOW: we just retrieved the last valid record from DB "
00189 << std::endl;
00190 std::cout <<"Its tmin is "<< Tm(t_min.value()).str() << std::endl;
00191
00192
00193 try {
00194 std::cout << "Making connection..." << std::flush;
00195 econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00196 std::cout << "Done." << std::endl;
00197 } catch (std::runtime_error &e) {
00198 std::cout << " connection parameters " << m_sid << "/" << m_user;
00199 if (m_debug) {
00200 std::cout << "/" << m_pass <<std::endl;
00201 } else {
00202 std::cout << "/**********" <<std::endl;
00203 }
00204 std::cerr << e.what() << std::endl;
00205 throw cms::Exception("OMDS not available");
00206 }
00207
00208
00209 std::vector<EcalLogicID> crystals_EB =
00210 econn->getEcalLogicIDSetOrdered( "EB_crystal_angle",
00211 -85,85,1,360,
00212 EcalLogicID::NULLID,EcalLogicID::NULLID,
00213 "EB_crystal_number", 4 );
00214 std::vector<EcalLogicID> crystals_EE =
00215 econn->getEcalLogicIDSetOrdered( "EE_crystal_number",
00216 -1,1,1,100,
00217 1,100,
00218 "EE_crystal_number", 4 );
00219
00220 std::vector<EcalLogicID>::const_iterator ieb = crystals_EB.begin();
00221 std::vector<EcalLogicID>::const_iterator eeb = crystals_EB.end();
00222
00223 std::cout << "Got list of " << crystals_EB.size() << " crystals in EB"
00224 << std::endl;
00225 std::cout << "Got list of " << crystals_EE.size() << " crystals in EE"
00226 << std::endl;
00227
00228 int count = 0;
00229
00230 std::map<int, int> detids;
00231 while (ieb != eeb) {
00232 int iEta = ieb->getID1();
00233 int iPhi = ieb->getID2();
00234 count++;
00235 EBDetId ebdetid(iEta,iPhi);
00236
00237 detids[ieb->getLogicID()] = ebdetid;
00238 ieb++;
00239 }
00240 std::cout << "Validated " << count << " logic ID's for EB" << std::endl;
00241
00242
00243
00244 std::vector<EcalLogicID>::const_iterator iee = crystals_EE.begin();
00245 std::vector<EcalLogicID>::const_iterator eee = crystals_EE.end();
00246
00247 count = 0;
00248 while (iee != eee) {
00249 int iSide = iee->getID1();
00250 int iX = iee->getID2();
00251 int iY = iee->getID3();
00252 EEDetId eedetidpos(iX,iY,iSide);
00253
00254 detids[iee->getLogicID()] = eedetidpos;
00255 count ++;
00256 iee++;
00257 }
00258 std::cout << "Validated " << count << " logic ID's for EE" << std::endl;
00259
00260
00261 std::map<int, int> logicId2Lmr = econn->getEcalLogicID2LmrMap();
00262
00263 std::cout << "Retrieving corrections from ONLINE DB ... " << std::endl;
00264
00265 LMFCorrCoefDat data(econn);
00266 if (m_debug) {
00267 data.debug();
00268 }
00269
00270
00271
00272 Tm tmax;
00273 tmax.setToString(m_maxtime);
00274 Tm tmin = Tm(t_min.value());
00275
00276 Tm tgo;
00277 tgo.setToString("2011-02-24 23:59:59");
00278
00279 if (tmin.microsTime() < tgo.microsTime()) {
00280 tmin.setToMicrosTime(tgo.microsTime());
00281 }
00282 std::map<int, std::map<int, LMFSextuple> > d =
00283 data.getCorrections(tmin, tmax, m_sequences);
00284
00285 std::cout << "Data organized into " << d.size() << " sequences" << std::endl;
00286
00287 std::map<int, std::map<int, LMFSextuple> >::const_iterator iseq = d.begin();
00288 std::map<int, std::map<int, LMFSextuple> >::const_iterator eseq = d.end();
00289 std::cout << "===== Looping on Sequences" << std::endl;
00290 while (iseq != eseq) {
00291 std::cout << "==== SEQ_ID: " << iseq->first
00292 << " contains " << iseq->second.size() << " crystals"
00293 << std::endl << std::flush;
00294
00295 std::map<int, LMFSextuple>::const_iterator is = iseq->second.begin();
00296 std::map<int, LMFSextuple>::const_iterator es = iseq->second.end();
00297 EcalLaserAPDPNRatios* apdpns_popcon = new EcalLaserAPDPNRatios();
00298 Time_t t_last = t_min.value();
00299 while (is != es) {
00300 EcalLaserAPDPNRatios::EcalLaserAPDPNpair apdpnpair_temp;
00301 apdpnpair_temp.p1 = is->second.p[0];
00302 apdpnpair_temp.p2 = is->second.p[1];
00303 apdpnpair_temp.p3 = is->second.p[2];
00304 EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp_temp;
00305 timestamp_temp.t1 = edm::Timestamp(is->second.t[0].microsTime());
00306 timestamp_temp.t2 = edm::Timestamp(is->second.t[1].microsTime());
00307 timestamp_temp.t3 = edm::Timestamp(is->second.t[2].microsTime());
00308 apdpns_popcon->setValue(detids[is->first], apdpnpair_temp);
00309 if (logicId2Lmr.find(is->first) != logicId2Lmr.end()) {
00310 int hashedIndex = logicId2Lmr[is->first] - 1;
00311 if ((hashedIndex >= 0) && (hashedIndex <= 91)) {
00312 apdpns_popcon->setTime( hashedIndex , timestamp_temp);
00313 t_last = timestamp_temp.t1.value();
00314 } else {
00315 std::stringstream ss;
00316 ss << "LOGIC_ID: " << is->first << " LMR: " << hashedIndex + 1
00317 << " Out of range";
00318 throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
00319 ss.str()));
00320 }
00321 } else {
00322 std::stringstream ss;
00323 ss << "LOGIC_ID: " << is->first << " Cannot determine LMR";
00324 throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
00325 ss.str()));
00326 }
00327 is++;
00328 }
00329 if (iseq->second.size() > 0) {
00330 m_to_transfer.push_back(std::make_pair(apdpns_popcon, t_last));
00331 }
00332 iseq++;
00333 }
00334 std::cout << "==== END OF LOOP ON SEQUENCES" << std::endl << std::flush;
00335 delete econn;
00336 std::cout << "Ecal -> end of getNewObjects -----------\n";
00337
00338
00339 }