Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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   // here we retrieve all the runs after the last from online DB
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     //      cerr << e.what() << std::endl;
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;  // we have to add 1 to the last transferred one
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   //    my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
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       // retrieve the data :
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         //EcalLogicID ecalid  = it->first;
0134 
0135         RunTPGConfigDat dat = it->second;
0136         the_config_tag = dat.getConfigTag();
0137         the_config_version = dat.getVersion();
0138       }
0139 
0140       // it is all the same for all SM... get the last one
0141 
0142       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0143 
0144       // here we should check if it is the same as previous run.
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           // now get TPGBadXT
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             // NB new
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             int icells = 0;
0179 
0180             for (CIfeped p = dataset_TpgBadXT.begin(); p != dataset_TpgBadXT.end(); p++) {
0181               rd_badXt = *p;
0182 
0183               int fed_id = rd_badXt.getFedId();
0184               //int tcc_id=rd_badXt.getTCCId();
0185               int tt_id = rd_badXt.getTTId();
0186               int xt_id = rd_badXt.getXTId();
0187 
0188               // EB data
0189               if (fed_id >= 610 && fed_id <= 645) {
0190                 // logic id is 1011ssxxxx
0191                 // get SM id
0192                 int sm_num = 0;
0193                 if (fed_id <= 627)
0194                   sm_num = fed_id - 609 + 18;
0195                 if (fed_id > 627)
0196                   sm_num = fed_id - 627;
0197 
0198                 // get crystal id
0199                 int xt_num = 0;
0200 
0201                 for (size_t ixt = 0; ixt < my_EcalLogicId.size(); ixt++) {
0202                   if (my_EcalLogicId[ixt].getID1() == fed_id && my_EcalLogicId[ixt].getID2() == tt_id &&
0203                       my_EcalLogicId[ixt].getID3() == xt_id) {
0204                     //1011060504
0205                     int ecid = my_EcalLogicId[ixt].getLogicID();
0206                     xt_num = (ecid) - (101100 + sm_num) * 10000;
0207                   }
0208                 }
0209 
0210                 std::cout << " masking crystal " << sm_num << "/" << xt_num << " from fed/tt/xt" << fed_id << "/"
0211                           << tt_id << "/" << xt_id << std::endl;
0212                 if (sm_num == 0 && xt_num == 0) {
0213                   std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
0214                             << std::endl;
0215                 }
0216                 EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
0217 
0218                 badXt->setValue(ebdetid.rawId(), rd_badXt.getStatus());
0219                 ++icells;
0220               } else {
0221                 // EE data
0222 
0223                 long x = 0;
0224                 long y = 0;
0225                 long z = 0;
0226 
0227                 for (size_t ixt = 0; ixt < my_EcalLogicId_EE.size(); ixt++) {
0228                   if (my_EcalLogicId_EE[ixt].getID1() == fed_id && my_EcalLogicId_EE[ixt].getID2() == tt_id &&
0229                       my_EcalLogicId_EE[ixt].getID3() == xt_id) {
0230                     long ecid = (long)my_EcalLogicId_EE[ixt].getLogicID();
0231                     // logic_id 201Zxxxyyy Z=0 / 2 -> z= -1 / 1 , x -> 1 100,  y -> 1 100
0232                     y = ecid - ((long)(ecid / 1000)) * 1000;
0233                     x = (ecid - y) / 1000;
0234                     x = x - ((long)(x / 1000)) * 1000;
0235                     z = (ecid - y - x * 1000) / 1000000 - 2010;
0236                     if (z == 0)
0237                       z = -1;
0238                     if (z == 2)
0239                       z = 1;
0240                   }
0241                 }
0242 
0243                 if (x == 0 && y == 0 && z == 0) {
0244                   std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
0245                             << std::endl;
0246                 }
0247                 EEDetId eedetid(x, y, z);
0248                 badXt->setValue(eedetid.rawId(), rd_badXt.getStatus());
0249                 ++icells;
0250               }
0251             }  //end for over data
0252 
0253             edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
0254 
0255             Time_t snc = (Time_t)irun;
0256             m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt, snc));
0257 
0258             m_i_run_number = irun;
0259             m_i_tag = the_config_tag;
0260             m_i_version = the_config_version;
0261             m_i_badXT = badxtId;
0262 
0263             writeFile("last_tpg_badXT_settings.txt");
0264 
0265           } else {
0266             m_i_run_number = irun;
0267             m_i_tag = the_config_tag;
0268             m_i_version = the_config_version;
0269 
0270             writeFile("last_tpg_badXT_settings.txt");
0271 
0272             std::cout << " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "
0273                       << std::endl;
0274           }
0275 
0276         }
0277 
0278         catch (std::exception& e) {
0279           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0280                     << std::endl;
0281           std::cout << e.what() << std::endl;
0282           m_i_run_number = irun;
0283         }
0284         std::cout << " **************** " << std::endl;
0285 
0286       } else if (nr == 0) {
0287         m_i_run_number = irun;
0288         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0289         std::cout << " **************** " << std::endl;
0290       } else {
0291         m_i_run_number = irun;
0292         m_i_tag = the_config_tag;
0293         m_i_version = the_config_version;
0294         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0295         std::cout << " **************** " << std::endl;
0296         writeFile("last_tpg_badXT_settings.txt");
0297       }
0298 
0299     }  //end for over kr (nr of runs)
0300   }    //end if
0301 
0302   delete econn;
0303 
0304   edm::LogInfo("EcalTPGBadXTHandler") << "Ecal - > end of getNewObjects -----------";
0305 }
0306 
0307 void popcon::EcalTPGBadXTHandler::readFromFile(const char* inputFile) {
0308   //-------------------------------------------------------------
0309 
0310   m_i_tag = "";
0311   m_i_version = 0;
0312   m_i_run_number = 0;
0313   m_i_badXT = 0;
0314 
0315   FILE* inpFile;  // input file
0316   inpFile = fopen(inputFile, "r");
0317   if (!inpFile) {
0318     edm::LogError("EcalTPGBadXTHandler") << "*** Can not open file: " << inputFile;
0319     return;
0320   }
0321 
0322   char line[256];
0323 
0324   std::ostringstream str;
0325 
0326   fgets(line, 255, inpFile);
0327   m_i_tag = to_string(line);
0328   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0329 
0330   fgets(line, 255, inpFile);
0331   m_i_version = atoi(line);
0332   str << "version= " << m_i_version << std::endl;
0333 
0334   fgets(line, 255, inpFile);
0335   m_i_run_number = atoi(line);
0336   str << "run_number= " << m_i_run_number << std::endl;
0337 
0338   fgets(line, 255, inpFile);
0339   m_i_badXT = atoi(line);
0340   str << "badXT_config= " << m_i_badXT << std::endl;
0341 
0342   fclose(inpFile);  // close inp. file
0343 }
0344 
0345 void popcon::EcalTPGBadXTHandler::writeFile(const char* inputFile) {
0346   //-------------------------------------------------------------
0347 
0348   std::ofstream myfile;
0349   myfile.open(inputFile);
0350   myfile << m_i_tag << std::endl;
0351   myfile << m_i_version << std::endl;
0352   myfile << m_i_run_number << std::endl;
0353   myfile << m_i_badXT << std::endl;
0354 
0355   myfile.close();
0356 }
0357 
0358 EcalTPGCrystalStatus* popcon::EcalTPGBadXTHandler::produceEcalTrgChannelStatus() {
0359   EcalTPGCrystalStatus* ical = new EcalTPGCrystalStatus();
0360   // barrel
0361   for (int ieta = -EBDetId::MAX_IETA; ieta <= EBDetId::MAX_IETA; ++ieta) {
0362     if (ieta == 0)
0363       continue;
0364     for (int iphi = EBDetId::MIN_IPHI; iphi <= EBDetId::MAX_IPHI; ++iphi) {
0365       if (EBDetId::validDetId(ieta, iphi)) {
0366         EBDetId ebid(ieta, iphi);
0367         ical->setValue(ebid, 0);
0368       }
0369     }
0370   }
0371   // endcap
0372   for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0373     for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0374       // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
0375       if (EEDetId::validDetId(iX, iY, 1)) {
0376         EEDetId eedetidpos(iX, iY, 1);
0377         ical->setValue(eedetidpos, 0);
0378       }
0379       if (EEDetId::validDetId(iX, iY, -1)) {
0380         EEDetId eedetidneg(iX, iY, -1);
0381         ical->setValue(eedetidneg, 0);
0382       }
0383     }
0384   }
0385   return ical;
0386 }