Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:59

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             for (CIfeped p = dataset_TpgBadTT.begin(); p != dataset_TpgBadTT.end(); p++) {
0189               rd_badTT = *p;
0190 
0191               int tcc_num = rd_badTT.getTCCId();
0192               int tt_num = rd_badTT.getTTId();
0193 
0194               std::cout << " tcc/tt" << tcc_num << "/" << tt_num << std::endl;
0195 
0196               if (tcc_num > 36 && tcc_num <= 72) {
0197                 // SM number
0198                 int smid = tcc_num - 54;
0199                 if (tcc_num < 55)
0200                   smid = tcc_num - 18;
0201                 // TT number
0202                 int towerid = tt_num;
0203 
0204                 int tow_eta = (towerid - 1) / 4;
0205                 int tow_phi = ((towerid - 1) - tow_eta * 4);
0206                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0207 
0208                 EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
0209                 const EcalTrigTowerDetId towid = id.tower();
0210                 towerStatus->setValue(towid.rawId(), rd_badTT.getStatus());
0211 
0212               } else {
0213                 // EE data
0214 
0215                 // TCC number
0216                 int tccid = tcc_num;
0217                 // TT number
0218                 int towerid = tt_num;
0219 
0220                 bool set_the_tower = false;
0221                 int towid;
0222                 for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
0223                   if (!set_the_tower) {
0224                     if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
0225                         my_TTEcalLogicId_EE[itower].getID2() == towerid) {
0226                       towid = my_TTEcalLogicId_EE[itower].getLogicID();
0227                       set_the_tower = true;
0228                       break;
0229                     }
0230                   }
0231                 }
0232 
0233                 if (set_the_tower) {
0234                   towerStatus->setValue(towid, rd_badTT.getStatus());
0235 
0236                 } else {
0237                   std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
0238                 }
0239               }
0240             }
0241 
0242             edm::LogInfo("EcalTPGBadTTHandler") << "Finished badTT reading.";
0243 
0244             Time_t snc = (Time_t)irun;
0245 
0246             m_to_transfer.push_back(std::make_pair((EcalTPGTowerStatus*)towerStatus, snc));
0247 
0248             m_i_run_number = irun;
0249             m_i_tag = the_config_tag;
0250             m_i_version = the_config_version;
0251             m_i_badTT = badttId;
0252 
0253             writeFile("last_tpg_badTT_settings.txt");
0254 
0255           } else {
0256             m_i_run_number = irun;
0257             m_i_tag = the_config_tag;
0258             m_i_version = the_config_version;
0259 
0260             writeFile("last_tpg_badTT_settings.txt");
0261 
0262             //  std::cout<< " even if the tag/version is not the same, the badTT id is the same -> no transfer needed "<< std::endl;
0263           }
0264 
0265         }
0266 
0267         catch (std::exception& e) {
0268           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0269                     << std::endl;
0270           std::cout << e.what() << std::endl;
0271           m_i_run_number = irun;
0272         }
0273 
0274       } else if (nr == 0) {
0275         m_i_run_number = irun;
0276         //        std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
0277       } else {
0278         m_i_run_number = irun;
0279         m_i_tag = the_config_tag;
0280         m_i_version = the_config_version;
0281 
0282         writeFile("last_tpg_badTT_settings.txt");
0283       }
0284     }
0285   }
0286 
0287   delete econn;
0288 
0289   edm::LogInfo("EcalTPGBadTTHandler") << "Ecal - > end of getNewObjects -----------";
0290 }
0291 
0292 void popcon::EcalTPGBadTTHandler::readFromFile(const char* inputFile) {
0293   //-------------------------------------------------------------
0294 
0295   m_i_tag = "";
0296   m_i_version = 0;
0297   m_i_run_number = 0;
0298   m_i_badTT = 0;
0299 
0300   FILE* inpFile;  // input file
0301   inpFile = fopen(inputFile, "r");
0302   if (!inpFile) {
0303     edm::LogError("EcalTPGBadTTHandler") << "*** Can not open file: " << inputFile;
0304     return;
0305   }
0306 
0307   char line[256];
0308 
0309   std::ostringstream str;
0310 
0311   fgets(line, 255, inpFile);
0312   m_i_tag = to_string(line);
0313   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0314 
0315   fgets(line, 255, inpFile);
0316   m_i_version = atoi(line);
0317   str << "version= " << m_i_version << std::endl;
0318 
0319   fgets(line, 255, inpFile);
0320   m_i_run_number = atoi(line);
0321   str << "run_number= " << m_i_run_number << std::endl;
0322 
0323   fgets(line, 255, inpFile);
0324   m_i_badTT = atoi(line);
0325   str << "badTT_config= " << m_i_badTT << std::endl;
0326 
0327   fclose(inpFile);  // close inp. file
0328 }
0329 
0330 void popcon::EcalTPGBadTTHandler::writeFile(const char* inputFile) {
0331   //-------------------------------------------------------------
0332 
0333   std::ofstream myfile;
0334   myfile.open(inputFile);
0335   myfile << m_i_tag << std::endl;
0336   myfile << m_i_version << std::endl;
0337   myfile << m_i_run_number << std::endl;
0338   myfile << m_i_badTT << std::endl;
0339 
0340   myfile.close();
0341 }