File indexing completed on 2024-09-07 04:35:45
0001 #include "CondTools/Ecal/interface/EcalTPGBadXTHandler.h"
0002
0003 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0004 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigBadXTInfo.h"
0007 #include "OnlineDB/EcalCondDB/interface/RunList.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010
0011 #include "CondFormats/EcalObjects/interface/EcalTPGCrystalStatus.h"
0012
0013 #include <iostream>
0014 #include <fstream>
0015
0016 #include <ctime>
0017 #include <unistd.h>
0018
0019 #include <string>
0020 #include <cstdio>
0021 #include <typeinfo>
0022 #include <sstream>
0023
0024 popcon::EcalTPGBadXTHandler::EcalTPGBadXTHandler(const edm::ParameterSet& ps)
0025 : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGBadXTHandler")) {
0026 edm::LogInfo("EcalTPGBadXTHandler") << "EcalTPGBadXT Source handler constructor.";
0027 m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0028 m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0029 m_sid = ps.getParameter<std::string>("OnlineDBSID");
0030 m_user = ps.getParameter<std::string>("OnlineDBUser");
0031 m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0032 m_locationsource = ps.getParameter<std::string>("LocationSource");
0033 m_location = ps.getParameter<std::string>("Location");
0034 m_gentag = ps.getParameter<std::string>("GenTag");
0035 m_runtype = ps.getParameter<std::string>("RunType");
0036
0037 edm::LogInfo("EcalTPGBadXTHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0038 }
0039
0040 popcon::EcalTPGBadXTHandler::~EcalTPGBadXTHandler() {}
0041
0042 void popcon::EcalTPGBadXTHandler::getNewObjects() {
0043 edm::LogInfo("EcalTPGBadXTHandler") << "Started GetNewObjects!!!";
0044
0045 unsigned int max_since = 0;
0046 max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0047 edm::LogInfo("EcalTPGBadXTHandler") << "max_since : " << max_since;
0048 edm::LogInfo("EcalTPGBadXTHandler") << "retrieved last payload ";
0049
0050
0051 edm::LogInfo("EcalTPGBadXTHandler") << "Retrieving run list from ONLINE DB ... ";
0052
0053 edm::LogInfo("EcalTPGBadXTHandler") << "Making connection...";
0054 econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0055 edm::LogInfo("EcalTPGBadXTHandler") << "Done.";
0056
0057 if (!econn) {
0058 std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0059
0060 throw cms::Exception("OMDS not available");
0061 }
0062
0063 LocationDef my_locdef;
0064 my_locdef.setLocation(m_location);
0065
0066 RunTypeDef my_rundef;
0067 my_rundef.setRunType(m_runtype);
0068
0069 RunTag my_runtag;
0070 my_runtag.setLocationDef(my_locdef);
0071 my_runtag.setRunTypeDef(my_rundef);
0072 my_runtag.setGeneralTag(m_gentag);
0073
0074 readFromFile("last_tpg_badXT_settings.txt");
0075
0076 unsigned int min_run;
0077
0078 if (m_firstRun < m_i_run_number) {
0079 min_run = m_i_run_number + 1;
0080 } else {
0081 min_run = m_firstRun;
0082 }
0083 if (min_run < max_since) {
0084 min_run = max_since + 1;
0085 }
0086
0087 std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0088 << std::endl;
0089
0090 unsigned int max_run = m_lastRun;
0091 edm::LogInfo("EcalTPGBadXTHandler") << "min_run= " << min_run << "max_run= " << max_run;
0092
0093 RunList my_list;
0094 my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0095
0096
0097 std::vector<RunIOV> run_vec = my_list.getRuns();
0098 size_t num_runs = run_vec.size();
0099
0100 std::cout << "number of runs is : " << num_runs << std::endl;
0101
0102 std::vector<EcalLogicID> my_EcalLogicId;
0103 std::vector<EcalLogicID> my_EcalLogicId_EE;
0104
0105 unsigned int irun = 0;
0106 if (num_runs > 0) {
0107 my_EcalLogicId = econn->getEcalLogicIDSetOrdered(
0108 "ECAL_crystal_number_fedccuxt", 610, 650, 1, 100, 0, 100, "EB_crystal_number", 123);
0109
0110 my_EcalLogicId_EE = econn->getEcalLogicIDSetOrdered(
0111 "ECAL_crystal_number_fedccuxt", 600, 700, 1, 100, 0, 100, "EE_crystal_number", 123);
0112
0113 for (size_t kr = 0; kr < run_vec.size(); kr++) {
0114 std::cout << "here we are in run " << kr << std::endl;
0115 irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0116
0117 std::cout << " **************** " << std::endl;
0118 std::cout << " **************** " << std::endl;
0119 std::cout << " run= " << irun << std::endl;
0120
0121
0122 std::map<EcalLogicID, RunTPGConfigDat> dataset;
0123 econn->fetchDataSet(&dataset, &run_vec[kr]);
0124
0125 std::string the_config_tag = "";
0126 int the_config_version = 0;
0127
0128 std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0129
0130 int nr = 0;
0131 for (it = dataset.begin(); it != dataset.end(); it++) {
0132 ++nr;
0133
0134
0135 RunTPGConfigDat dat = it->second;
0136 the_config_tag = dat.getConfigTag();
0137 the_config_version = dat.getVersion();
0138 }
0139
0140
0141
0142 std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0143
0144
0145
0146 if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0147 std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0148 << std::endl;
0149
0150 FEConfigMainInfo fe_main_info;
0151 fe_main_info.setConfigTag(the_config_tag);
0152 fe_main_info.setVersion(the_config_version);
0153 std::cout << " version=" << fe_main_info.getVersion() << std::endl;
0154
0155 try {
0156 std::cout << " before fetch config set" << std::endl;
0157 econn->fetchConfigSet(&fe_main_info);
0158 std::cout << " after fetch config set" << std::endl;
0159
0160
0161 int badxtId = fe_main_info.getBxtId();
0162
0163 if (badxtId != m_i_badXT && badxtId != 0) {
0164 FEConfigBadXTInfo fe_badXt_info;
0165 fe_badXt_info.setId(badxtId);
0166 econn->fetchConfigSet(&fe_badXt_info);
0167 std::vector<FEConfigBadXTDat> dataset_TpgBadXT;
0168 econn->fetchConfigDataSet(&dataset_TpgBadXT, &fe_badXt_info);
0169
0170
0171
0172 EcalTPGCrystalStatus* badXt;
0173 badXt = produceEcalTrgChannelStatus();
0174
0175 typedef std::vector<FEConfigBadXTDat>::const_iterator CIfeped;
0176 EcalLogicID ecid_xt;
0177 FEConfigBadXTDat rd_badXt;
0178
0179 for (CIfeped p = dataset_TpgBadXT.begin(); p != dataset_TpgBadXT.end(); p++) {
0180 rd_badXt = *p;
0181
0182 int fed_id = rd_badXt.getFedId();
0183
0184 int tt_id = rd_badXt.getTTId();
0185 int xt_id = rd_badXt.getXTId();
0186
0187
0188 if (fed_id >= 610 && fed_id <= 645) {
0189
0190
0191 int sm_num = 0;
0192 if (fed_id <= 627)
0193 sm_num = fed_id - 609 + 18;
0194 if (fed_id > 627)
0195 sm_num = fed_id - 627;
0196
0197
0198 int xt_num = 0;
0199
0200 for (size_t ixt = 0; ixt < my_EcalLogicId.size(); ixt++) {
0201 if (my_EcalLogicId[ixt].getID1() == fed_id && my_EcalLogicId[ixt].getID2() == tt_id &&
0202 my_EcalLogicId[ixt].getID3() == xt_id) {
0203
0204 int ecid = my_EcalLogicId[ixt].getLogicID();
0205 xt_num = (ecid) - (101100 + sm_num) * 10000;
0206 }
0207 }
0208
0209 std::cout << " masking crystal " << sm_num << "/" << xt_num << " from fed/tt/xt" << fed_id << "/"
0210 << tt_id << "/" << xt_id << std::endl;
0211 if (sm_num == 0 && xt_num == 0) {
0212 std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
0213 << std::endl;
0214 }
0215 EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
0216
0217 badXt->setValue(ebdetid.rawId(), rd_badXt.getStatus());
0218 } else {
0219
0220
0221 long x = 0;
0222 long y = 0;
0223 long z = 0;
0224
0225 for (size_t ixt = 0; ixt < my_EcalLogicId_EE.size(); ixt++) {
0226 if (my_EcalLogicId_EE[ixt].getID1() == fed_id && my_EcalLogicId_EE[ixt].getID2() == tt_id &&
0227 my_EcalLogicId_EE[ixt].getID3() == xt_id) {
0228 long ecid = (long)my_EcalLogicId_EE[ixt].getLogicID();
0229
0230 y = ecid - ((long)(ecid / 1000)) * 1000;
0231 x = (ecid - y) / 1000;
0232 x = x - ((long)(x / 1000)) * 1000;
0233 z = (ecid - y - x * 1000) / 1000000 - 2010;
0234 if (z == 0)
0235 z = -1;
0236 if (z == 2)
0237 z = 1;
0238 }
0239 }
0240
0241 if (x == 0 && y == 0 && z == 0) {
0242 std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
0243 << std::endl;
0244 }
0245 EEDetId eedetid(x, y, z);
0246 badXt->setValue(eedetid.rawId(), rd_badXt.getStatus());
0247 }
0248 }
0249
0250 edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
0251
0252 Time_t snc = (Time_t)irun;
0253 m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt, snc));
0254
0255 m_i_run_number = irun;
0256 m_i_tag = the_config_tag;
0257 m_i_version = the_config_version;
0258 m_i_badXT = badxtId;
0259
0260 writeFile("last_tpg_badXT_settings.txt");
0261
0262 } else {
0263 m_i_run_number = irun;
0264 m_i_tag = the_config_tag;
0265 m_i_version = the_config_version;
0266
0267 writeFile("last_tpg_badXT_settings.txt");
0268
0269 std::cout << " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "
0270 << std::endl;
0271 }
0272
0273 }
0274
0275 catch (std::exception& e) {
0276 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0277 << std::endl;
0278 std::cout << e.what() << std::endl;
0279 m_i_run_number = irun;
0280 }
0281 std::cout << " **************** " << std::endl;
0282
0283 } else if (nr == 0) {
0284 m_i_run_number = irun;
0285 std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0286 std::cout << " **************** " << std::endl;
0287 } else {
0288 m_i_run_number = irun;
0289 m_i_tag = the_config_tag;
0290 m_i_version = the_config_version;
0291 std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0292 std::cout << " **************** " << std::endl;
0293 writeFile("last_tpg_badXT_settings.txt");
0294 }
0295
0296 }
0297 }
0298
0299 delete econn;
0300
0301 edm::LogInfo("EcalTPGBadXTHandler") << "Ecal - > end of getNewObjects -----------";
0302 }
0303
0304 void popcon::EcalTPGBadXTHandler::readFromFile(const char* inputFile) {
0305
0306
0307 m_i_tag = "";
0308 m_i_version = 0;
0309 m_i_run_number = 0;
0310 m_i_badXT = 0;
0311
0312 FILE* inpFile;
0313 inpFile = fopen(inputFile, "r");
0314 if (!inpFile) {
0315 edm::LogError("EcalTPGBadXTHandler") << "*** Can not open file: " << inputFile;
0316 return;
0317 }
0318
0319 char line[256];
0320
0321 std::ostringstream str;
0322
0323 fgets(line, 255, inpFile);
0324 m_i_tag = to_string(line);
0325 str << "gen tag " << m_i_tag << std::endl;
0326
0327 fgets(line, 255, inpFile);
0328 m_i_version = atoi(line);
0329 str << "version= " << m_i_version << std::endl;
0330
0331 fgets(line, 255, inpFile);
0332 m_i_run_number = atoi(line);
0333 str << "run_number= " << m_i_run_number << std::endl;
0334
0335 fgets(line, 255, inpFile);
0336 m_i_badXT = atoi(line);
0337 str << "badXT_config= " << m_i_badXT << std::endl;
0338
0339 fclose(inpFile);
0340 }
0341
0342 void popcon::EcalTPGBadXTHandler::writeFile(const char* inputFile) {
0343
0344
0345 std::ofstream myfile;
0346 myfile.open(inputFile);
0347 myfile << m_i_tag << std::endl;
0348 myfile << m_i_version << std::endl;
0349 myfile << m_i_run_number << std::endl;
0350 myfile << m_i_badXT << std::endl;
0351
0352 myfile.close();
0353 }
0354
0355 EcalTPGCrystalStatus* popcon::EcalTPGBadXTHandler::produceEcalTrgChannelStatus() {
0356 EcalTPGCrystalStatus* ical = new EcalTPGCrystalStatus();
0357
0358 for (int ieta = -EBDetId::MAX_IETA; ieta <= EBDetId::MAX_IETA; ++ieta) {
0359 if (ieta == 0)
0360 continue;
0361 for (int iphi = EBDetId::MIN_IPHI; iphi <= EBDetId::MAX_IPHI; ++iphi) {
0362 if (EBDetId::validDetId(ieta, iphi)) {
0363 EBDetId ebid(ieta, iphi);
0364 ical->setValue(ebid, 0);
0365 }
0366 }
0367 }
0368
0369 for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0370 for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0371
0372 if (EEDetId::validDetId(iX, iY, 1)) {
0373 EEDetId eedetidpos(iX, iY, 1);
0374 ical->setValue(eedetidpos, 0);
0375 }
0376 if (EEDetId::validDetId(iX, iY, -1)) {
0377 EEDetId eedetidneg(iX, iY, -1);
0378 ical->setValue(eedetidneg, 0);
0379 }
0380 }
0381 }
0382 return ical;
0383 }