Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-02 03:39:03

0001 #include "CondTools/Ecal/interface/EcalTPGBadTTHandler.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/FEConfigBadTTInfo.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/EcalTPGTowerStatus.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::EcalTPGBadTTHandler::EcalTPGBadTTHandler(const edm::ParameterSet& ps)
0025     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGBadTTHandler")) {
0026   edm::LogInfo("EcalTPGBadTTHandler") << "EcalTPGTowerStatus 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("EcalTPGBadTTHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0038 }
0039 
0040 popcon::EcalTPGBadTTHandler::~EcalTPGBadTTHandler() {}
0041 
0042 void popcon::EcalTPGBadTTHandler::getNewObjects() {
0043   edm::LogInfo("EcalTPGBadTTHandler") << "Started GetNewObjects!!!";
0044 
0045   unsigned int max_since = 0;
0046   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0047   edm::LogInfo("EcalTPGBadTTHandler") << "max_since : " << max_since;
0048   edm::LogInfo("EcalTPGBadTTHandler") << "retrieved last payload ";
0049 
0050   // here we retrieve all the runs after the last from online DB
0051   edm::LogInfo("EcalTPGBadTTHandler") << "Retrieving run list from ONLINE DB ... ";
0052 
0053   edm::LogInfo("EcalTPGBadTTHandler") << "Making connection...";
0054   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0055   edm::LogInfo("EcalTPGBadTTHandler") << "Done.";
0056 
0057   if (!econn) {
0058     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0059     throw cms::Exception("OMDS not available");
0060   }
0061 
0062   LocationDef my_locdef;
0063   my_locdef.setLocation(m_location);
0064 
0065   RunTypeDef my_rundef;
0066   my_rundef.setRunType(m_runtype);
0067 
0068   RunTag my_runtag;
0069   my_runtag.setLocationDef(my_locdef);
0070   my_runtag.setRunTypeDef(my_rundef);
0071   my_runtag.setGeneralTag(m_gentag);
0072 
0073   readFromFile("last_tpg_badTT_settings.txt");
0074 
0075   unsigned int min_run;
0076 
0077   if (m_firstRun < m_i_run_number) {
0078     min_run = m_i_run_number + 1;
0079   } else {
0080     min_run = m_firstRun;
0081   }
0082   if (min_run < max_since) {
0083     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0084   }
0085 
0086   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0087             << std::endl;
0088 
0089   unsigned int max_run = m_lastRun;
0090   edm::LogInfo("EcalTPGBadTTHandler") << "min_run= " << min_run << "max_run= " << max_run;
0091 
0092   RunList my_list;
0093   //    my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0094   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0095 
0096   std::vector<RunIOV> run_vec = my_list.getRuns();
0097   size_t num_runs = run_vec.size();
0098 
0099   std::cout << "number of runs is : " << num_runs << std::endl;
0100 
0101   std::string str = "";
0102 
0103   unsigned int irun = 0;
0104   if (num_runs > 0) {
0105     // going to query the ecal logic id
0106     std::vector<EcalLogicID> my_TTEcalLogicId_EE;
0107     my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered(
0108         "EE_trigger_tower", 1, 200, 1, 70, EcalLogicID::NULLID, EcalLogicID::NULLID, "EE_offline_towerid", 12);
0109     std::cout << " GOT the logic ID for the EE trigger towers " << std::endl;
0110 
0111     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0112       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0113 
0114       // retrieve the data :
0115       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0116       econn->fetchDataSet(&dataset, &run_vec[kr]);
0117 
0118       std::string the_config_tag = "";
0119       int the_config_version = 0;
0120 
0121       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0122 
0123       int nr = 0;
0124       for (it = dataset.begin(); it != dataset.end(); it++) {
0125         ++nr;
0126         //EcalLogicID ecalid  = it->first;
0127 
0128         RunTPGConfigDat dat = it->second;
0129         the_config_tag = dat.getConfigTag();
0130         the_config_version = dat.getVersion();
0131       }
0132 
0133       // it is all the same for all SM... get the last one
0134 
0135       // here we should check if it is the same as previous run.
0136 
0137       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0138         std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0139         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0140                   << std::endl;
0141 
0142         FEConfigMainInfo fe_main_info;
0143         fe_main_info.setConfigTag(the_config_tag);
0144         fe_main_info.setVersion(the_config_version);
0145 
0146         try {
0147           econn->fetchConfigSet(&fe_main_info);
0148 
0149           // now get TPGTowerStatus
0150           int badttId = fe_main_info.getBttId();
0151 
0152           if (badttId != m_i_badTT) {
0153             FEConfigBadTTInfo fe_badTT_info;
0154             fe_badTT_info.setId(badttId);
0155 
0156             econn->fetchConfigSet(&fe_badTT_info);
0157 
0158             std::vector<FEConfigBadTTDat> dataset_TpgBadTT;
0159 
0160             econn->fetchConfigDataSet(&dataset_TpgBadTT, &fe_badTT_info);
0161 
0162             EcalTPGTowerStatus* towerStatus = new EcalTPGTowerStatus;
0163             typedef std::vector<FEConfigBadTTDat>::const_iterator CIfeped;
0164             EcalLogicID ecid_xt;
0165             FEConfigBadTTDat rd_badTT;
0166 
0167             // reset the map
0168             // EB
0169             for (int ism = 1; ism <= 36; ism++) {
0170               for (int ito = 1; ito <= 68; ito++) {
0171                 int tow_eta = (ito - 1) / 4;
0172                 int tow_phi = ((ito - 1) - tow_eta * 4);
0173                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0174                 EBDetId id(ism, axt, EBDetId::SMCRYSTALMODE);
0175                 const EcalTrigTowerDetId towid = id.tower();
0176                 int tower_status = 0;
0177                 towerStatus->setValue(towid.rawId(), tower_status);
0178               }
0179             }
0180             //EE
0181             for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
0182               int towid = my_TTEcalLogicId_EE[itower].getLogicID();
0183               int tower_status = 0;
0184               towerStatus->setValue(towid, tower_status);
0185             }
0186 
0187             // now put at 1 those that are bad
0188             int icells = 0;
0189             for (CIfeped p = dataset_TpgBadTT.begin(); p != dataset_TpgBadTT.end(); p++) {
0190               rd_badTT = *p;
0191 
0192               int tcc_num = rd_badTT.getTCCId();
0193               int tt_num = rd_badTT.getTTId();
0194 
0195               std::cout << " tcc/tt" << tcc_num << "/" << tt_num << std::endl;
0196 
0197               if (tcc_num > 36 && tcc_num <= 72) {
0198                 // SM number
0199                 int smid = tcc_num - 54;
0200                 if (tcc_num < 55)
0201                   smid = tcc_num - 18;
0202                 // TT number
0203                 int towerid = tt_num;
0204 
0205                 int tow_eta = (towerid - 1) / 4;
0206                 int tow_phi = ((towerid - 1) - tow_eta * 4);
0207                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0208 
0209                 EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
0210                 const EcalTrigTowerDetId towid = id.tower();
0211                 towerStatus->setValue(towid.rawId(), rd_badTT.getStatus());
0212 
0213                 ++icells;
0214               } else {
0215                 // EE data
0216 
0217                 // TCC number
0218                 int tccid = tcc_num;
0219                 // TT number
0220                 int towerid = tt_num;
0221 
0222                 bool set_the_tower = false;
0223                 int towid;
0224                 for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
0225                   if (!set_the_tower) {
0226                     if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
0227                         my_TTEcalLogicId_EE[itower].getID2() == towerid) {
0228                       towid = my_TTEcalLogicId_EE[itower].getLogicID();
0229                       set_the_tower = true;
0230                       break;
0231                     }
0232                   }
0233                 }
0234 
0235                 if (set_the_tower) {
0236                   towerStatus->setValue(towid, rd_badTT.getStatus());
0237 
0238                 } else {
0239                   std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
0240                 }
0241 
0242                 ++icells;
0243               }
0244             }
0245 
0246             edm::LogInfo("EcalTPGBadTTHandler") << "Finished badTT reading.";
0247 
0248             Time_t snc = (Time_t)irun;
0249 
0250             m_to_transfer.push_back(std::make_pair((EcalTPGTowerStatus*)towerStatus, snc));
0251 
0252             m_i_run_number = irun;
0253             m_i_tag = the_config_tag;
0254             m_i_version = the_config_version;
0255             m_i_badTT = badttId;
0256 
0257             writeFile("last_tpg_badTT_settings.txt");
0258 
0259           } else {
0260             m_i_run_number = irun;
0261             m_i_tag = the_config_tag;
0262             m_i_version = the_config_version;
0263 
0264             writeFile("last_tpg_badTT_settings.txt");
0265 
0266             //  std::cout<< " even if the tag/version is not the same, the badTT id is the same -> no transfer needed "<< std::endl;
0267           }
0268 
0269         }
0270 
0271         catch (std::exception& e) {
0272           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0273                     << std::endl;
0274           std::cout << e.what() << std::endl;
0275           m_i_run_number = irun;
0276         }
0277 
0278       } else if (nr == 0) {
0279         m_i_run_number = irun;
0280         //        std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
0281       } else {
0282         m_i_run_number = irun;
0283         m_i_tag = the_config_tag;
0284         m_i_version = the_config_version;
0285 
0286         writeFile("last_tpg_badTT_settings.txt");
0287       }
0288     }
0289   }
0290 
0291   delete econn;
0292 
0293   edm::LogInfo("EcalTPGBadTTHandler") << "Ecal - > end of getNewObjects -----------";
0294 }
0295 
0296 void popcon::EcalTPGBadTTHandler::readFromFile(const char* inputFile) {
0297   //-------------------------------------------------------------
0298 
0299   m_i_tag = "";
0300   m_i_version = 0;
0301   m_i_run_number = 0;
0302   m_i_badTT = 0;
0303 
0304   FILE* inpFile;  // input file
0305   inpFile = fopen(inputFile, "r");
0306   if (!inpFile) {
0307     edm::LogError("EcalTPGBadTTHandler") << "*** Can not open file: " << inputFile;
0308     return;
0309   }
0310 
0311   char line[256];
0312 
0313   std::ostringstream str;
0314 
0315   fgets(line, 255, inpFile);
0316   m_i_tag = to_string(line);
0317   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0318 
0319   fgets(line, 255, inpFile);
0320   m_i_version = atoi(line);
0321   str << "version= " << m_i_version << std::endl;
0322 
0323   fgets(line, 255, inpFile);
0324   m_i_run_number = atoi(line);
0325   str << "run_number= " << m_i_run_number << std::endl;
0326 
0327   fgets(line, 255, inpFile);
0328   m_i_badTT = atoi(line);
0329   str << "badTT_config= " << m_i_badTT << std::endl;
0330 
0331   fclose(inpFile);  // close inp. file
0332 }
0333 
0334 void popcon::EcalTPGBadTTHandler::writeFile(const char* inputFile) {
0335   //-------------------------------------------------------------
0336 
0337   std::ofstream myfile;
0338   myfile.open(inputFile);
0339   myfile << m_i_tag << std::endl;
0340   myfile << m_i_version << std::endl;
0341   myfile << m_i_run_number << std::endl;
0342   myfile << m_i_badTT << std::endl;
0343 
0344   myfile.close();
0345 }