Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGBadStripHandler.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/FEConfigBadStripInfo.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/EcalTPGStripStatus.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::EcalTPGBadStripHandler::EcalTPGBadStripHandler(const edm::ParameterSet& ps)
0025     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGBadStripHandler")) {
0026   edm::LogInfo("EcalTPGBadStripHandler") << "EcalTPGStripStatus 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("EcalTPGBadStripHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0038 }
0039 
0040 popcon::EcalTPGBadStripHandler::~EcalTPGBadStripHandler() {}
0041 
0042 void popcon::EcalTPGBadStripHandler::getNewObjects() {
0043   edm::LogInfo("EcalTPGBadStripHandler") << "Started GetNewObjects!!!";
0044 
0045   unsigned int max_since = 0;
0046   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0047   edm::LogInfo("EcalTPGBadStripHandler") << "max_since : " << max_since;
0048   edm::LogInfo("EcalTPGBadStripHandler") << "retrieved last payload ";
0049 
0050   // here we retrieve all the runs after the last from online DB
0051   edm::LogInfo("EcalTPGBadStripHandler") << "Retrieving run list from ONLINE DB ... ";
0052 
0053   edm::LogInfo("EcalTPGBadStripHandler") << "Making connection...";
0054   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0055   edm::LogInfo("EcalTPGBadStripHandler") << "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_badStrip_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("EcalTPGBadStripHandler") << "min_run= " << min_run << "max_run= " << max_run;
0091 
0092   RunList my_list;
0093   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0094   //my_list=econn->fetchRunListByLocation(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 
0105   if (num_runs > 0) {
0106     // going to query the ecal logic id
0107     std::vector<EcalLogicID> my_StripEcalLogicId_EE;
0108     my_StripEcalLogicId_EE = econn->getEcalLogicIDSetOrdered("EE_trigger_strip",
0109                                                              1,
0110                                                              1000,  //"TCC"
0111                                                              1,
0112                                                              100,  //tower
0113                                                              0,
0114                                                              5,  //strip
0115                                                              "EE_offline_stripid",
0116                                                              123);
0117 
0118     std::cout << " GOT the logic ID for the EE trigger strips " << std::endl;
0119 
0120     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0121       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0122 
0123       std::cout << " **************** " << std::endl;
0124       std::cout << " **************** " << std::endl;
0125       std::cout << " run= " << irun << std::endl;
0126 
0127       // retrieve the data :
0128       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0129       econn->fetchDataSet(&dataset, &run_vec[kr]);
0130 
0131       std::string the_config_tag = "";
0132       int the_config_version = 0;
0133 
0134       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0135 
0136       int nr = 0;
0137       for (it = dataset.begin(); it != dataset.end(); it++) {
0138         ++nr;
0139         //EcalLogicID ecalid  = it->first;
0140 
0141         RunTPGConfigDat dat = it->second;
0142         the_config_tag = dat.getConfigTag();
0143         the_config_version = dat.getVersion();
0144       }
0145 
0146       // it is all the same for all SM... get the last one
0147 
0148       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0149 
0150       // here we should check if it is the same as previous run.
0151 
0152       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0153         std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0154         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0155                   << std::endl;
0156 
0157         FEConfigMainInfo fe_main_info;
0158         fe_main_info.setConfigTag(the_config_tag);
0159         fe_main_info.setVersion(the_config_version);
0160 
0161         try {
0162           econn->fetchConfigSet(&fe_main_info);
0163 
0164           // now get TPGStripStatus
0165           int badstripId = fe_main_info.getBstId();
0166 
0167           if (badstripId != m_i_badStrip) {
0168             FEConfigBadStripInfo fe_badStrip_info;
0169             fe_badStrip_info.setId(badstripId);
0170 
0171             econn->fetchConfigSet(&fe_badStrip_info);
0172 
0173             std::vector<FEConfigBadStripDat> dataset_TpgBadStrip;
0174 
0175             econn->fetchConfigDataSet(&dataset_TpgBadStrip, &fe_badStrip_info);
0176 
0177             EcalTPGStripStatus* stripStatus = new EcalTPGStripStatus;
0178             typedef std::vector<FEConfigBadStripDat>::const_iterator CIfeped;
0179 
0180             FEConfigBadStripDat rd_badStrip;
0181             //unsigned int  rd_stripStatus;
0182 
0183             // put at 1 the strip that are bad
0184             int icells = 0;
0185             int icellsFound = 0;
0186             for (CIfeped p = dataset_TpgBadStrip.begin(); p != dataset_TpgBadStrip.end(); p++) {
0187               rd_badStrip = *p;
0188 
0189               //int fed_num=rd_badStrip.getFedId();
0190               int tcc_num = rd_badStrip.getTCCId();
0191               int tt_num = rd_badStrip.getTTId();
0192               int strip_num = rd_badStrip.getStripId();
0193 
0194               //std::cout << fed_num << " " << tcc_num << " " << tt_num << " " << strip_num << std::endl;
0195 
0196               // EE data
0197               int stripid;
0198 
0199               bool set_the_strip = false;
0200               for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0201                 if (!set_the_strip) {
0202                   if (my_StripEcalLogicId_EE[istrip].getID1() == tcc_num &&
0203                       my_StripEcalLogicId_EE[istrip].getID2() == tt_num &&
0204                       my_StripEcalLogicId_EE[istrip].getID3() == strip_num) {
0205                     stripid = my_StripEcalLogicId_EE[istrip].getLogicID();
0206 
0207                     set_the_strip = true;
0208                     ++icellsFound;
0209                     break;
0210                   }
0211                 }
0212               }
0213 
0214               if (set_the_strip) {
0215                 stripStatus->setValue(stripid, (unsigned int)rd_badStrip.getStatus());
0216               }
0217 
0218               ++icells;
0219             }
0220 
0221             // std::cout << " icells = "  << icells << std::endl;
0222 
0223             edm::LogInfo("EcalTPGBadStripHandler") << "Finished badStrip reading.";
0224 
0225             Time_t snc = (Time_t)irun;
0226 
0227             m_to_transfer.push_back(std::make_pair((EcalTPGStripStatus*)stripStatus, snc));
0228 
0229             m_i_run_number = irun;
0230             m_i_tag = the_config_tag;
0231             m_i_version = the_config_version;
0232             m_i_badStrip = badstripId;
0233 
0234             writeFile("last_tpg_badStrip_settings.txt");
0235 
0236           } else {
0237             m_i_run_number = irun;
0238             m_i_tag = the_config_tag;
0239             m_i_version = the_config_version;
0240 
0241             writeFile("last_tpg_badStrip_settings.txt");
0242 
0243             //  std::cout<< " even if the tag/version is not the same, the badStrip id is the same -> no transfer needed "<< std::endl;
0244           }
0245 
0246         }
0247 
0248         catch (std::exception& e) {
0249           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0250                     << std::endl;
0251           std::cout << e.what() << std::endl;
0252           m_i_run_number = irun;
0253         }
0254 
0255       } else if (nr == 0) {
0256         m_i_run_number = irun;
0257         //        std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
0258       } else {
0259         m_i_run_number = irun;
0260         m_i_tag = the_config_tag;
0261         m_i_version = the_config_version;
0262 
0263         writeFile("last_tpg_badStrip_settings.txt");
0264       }
0265     }
0266   }
0267 
0268   delete econn;
0269 
0270   edm::LogInfo("EcalTPGBadStripHandler") << "Ecal - > end of getNewObjects -----------";
0271 }
0272 
0273 void popcon::EcalTPGBadStripHandler::readFromFile(const char* inputFile) {
0274   //-------------------------------------------------------------
0275 
0276   m_i_tag = "";
0277   m_i_version = 0;
0278   m_i_run_number = 0;
0279   m_i_badStrip = 0;
0280 
0281   FILE* inpFile;  // input file
0282   inpFile = fopen(inputFile, "r");
0283   if (!inpFile) {
0284     edm::LogError("EcalTPGBadStripHandler") << "*** Can not open file: " << inputFile;
0285     return;
0286   }
0287 
0288   char line[256];
0289 
0290   std::ostringstream str;
0291 
0292   fgets(line, 255, inpFile);
0293   m_i_tag = to_string(line);
0294   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0295 
0296   fgets(line, 255, inpFile);
0297   m_i_version = atoi(line);
0298   str << "version= " << m_i_version << std::endl;
0299 
0300   fgets(line, 255, inpFile);
0301   m_i_run_number = atoi(line);
0302   str << "run_number= " << m_i_run_number << std::endl;
0303 
0304   fgets(line, 255, inpFile);
0305   m_i_badStrip = atoi(line);
0306   str << "badTT_config= " << m_i_badStrip << std::endl;
0307 
0308   fclose(inpFile);  // close inp. file
0309 }
0310 
0311 void popcon::EcalTPGBadStripHandler::writeFile(const char* inputFile) {
0312   //-------------------------------------------------------------
0313 
0314   std::ofstream myfile;
0315   myfile.open(inputFile);
0316   myfile << m_i_tag << std::endl;
0317   myfile << m_i_version << std::endl;
0318   myfile << m_i_run_number << std::endl;
0319   myfile << m_i_badStrip << std::endl;
0320 
0321   myfile.close();
0322 }