Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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             for (CIfeped p = dataset_TpgBadStrip.begin(); p != dataset_TpgBadStrip.end(); p++) {
0185               rd_badStrip = *p;
0186 
0187               //int fed_num=rd_badStrip.getFedId();
0188               int tcc_num = rd_badStrip.getTCCId();
0189               int tt_num = rd_badStrip.getTTId();
0190               int strip_num = rd_badStrip.getStripId();
0191 
0192               //std::cout << fed_num << " " << tcc_num << " " << tt_num << " " << strip_num << std::endl;
0193 
0194               // EE data
0195               int stripid;
0196 
0197               bool set_the_strip = false;
0198               for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0199                 if (!set_the_strip) {
0200                   if (my_StripEcalLogicId_EE[istrip].getID1() == tcc_num &&
0201                       my_StripEcalLogicId_EE[istrip].getID2() == tt_num &&
0202                       my_StripEcalLogicId_EE[istrip].getID3() == strip_num) {
0203                     stripid = my_StripEcalLogicId_EE[istrip].getLogicID();
0204 
0205                     set_the_strip = true;
0206                     break;
0207                   }
0208                 }
0209               }
0210 
0211               if (set_the_strip) {
0212                 stripStatus->setValue(stripid, (unsigned int)rd_badStrip.getStatus());
0213               }
0214             }
0215 
0216             edm::LogInfo("EcalTPGBadStripHandler") << "Finished badStrip reading.";
0217 
0218             Time_t snc = (Time_t)irun;
0219 
0220             m_to_transfer.push_back(std::make_pair((EcalTPGStripStatus*)stripStatus, snc));
0221 
0222             m_i_run_number = irun;
0223             m_i_tag = the_config_tag;
0224             m_i_version = the_config_version;
0225             m_i_badStrip = badstripId;
0226 
0227             writeFile("last_tpg_badStrip_settings.txt");
0228 
0229           } else {
0230             m_i_run_number = irun;
0231             m_i_tag = the_config_tag;
0232             m_i_version = the_config_version;
0233 
0234             writeFile("last_tpg_badStrip_settings.txt");
0235 
0236             //  std::cout<< " even if the tag/version is not the same, the badStrip id is the same -> no transfer needed "<< std::endl;
0237           }
0238 
0239         }
0240 
0241         catch (std::exception& e) {
0242           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0243                     << std::endl;
0244           std::cout << e.what() << std::endl;
0245           m_i_run_number = irun;
0246         }
0247 
0248       } else if (nr == 0) {
0249         m_i_run_number = irun;
0250         //        std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
0251       } else {
0252         m_i_run_number = irun;
0253         m_i_tag = the_config_tag;
0254         m_i_version = the_config_version;
0255 
0256         writeFile("last_tpg_badStrip_settings.txt");
0257       }
0258     }
0259   }
0260 
0261   delete econn;
0262 
0263   edm::LogInfo("EcalTPGBadStripHandler") << "Ecal - > end of getNewObjects -----------";
0264 }
0265 
0266 void popcon::EcalTPGBadStripHandler::readFromFile(const char* inputFile) {
0267   //-------------------------------------------------------------
0268 
0269   m_i_tag = "";
0270   m_i_version = 0;
0271   m_i_run_number = 0;
0272   m_i_badStrip = 0;
0273 
0274   FILE* inpFile;  // input file
0275   inpFile = fopen(inputFile, "r");
0276   if (!inpFile) {
0277     edm::LogError("EcalTPGBadStripHandler") << "*** Can not open file: " << inputFile;
0278     return;
0279   }
0280 
0281   char line[256];
0282 
0283   std::ostringstream str;
0284 
0285   fgets(line, 255, inpFile);
0286   m_i_tag = to_string(line);
0287   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0288 
0289   fgets(line, 255, inpFile);
0290   m_i_version = atoi(line);
0291   str << "version= " << m_i_version << std::endl;
0292 
0293   fgets(line, 255, inpFile);
0294   m_i_run_number = atoi(line);
0295   str << "run_number= " << m_i_run_number << std::endl;
0296 
0297   fgets(line, 255, inpFile);
0298   m_i_badStrip = atoi(line);
0299   str << "badTT_config= " << m_i_badStrip << std::endl;
0300 
0301   fclose(inpFile);  // close inp. file
0302 }
0303 
0304 void popcon::EcalTPGBadStripHandler::writeFile(const char* inputFile) {
0305   //-------------------------------------------------------------
0306 
0307   std::ofstream myfile;
0308   myfile.open(inputFile);
0309   myfile << m_i_tag << std::endl;
0310   myfile << m_i_version << std::endl;
0311   myfile << m_i_run_number << std::endl;
0312   myfile << m_i_badStrip << std::endl;
0313 
0314   myfile.close();
0315 }