Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:00

0001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
0002 
0003 #include "CondTools/Ecal/interface/EcalTPGLutIdMapHandler.h"
0004 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0005 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0007 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTInfo.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0009 
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include <iostream>
0013 #include <fstream>
0014 
0015 #include <ctime>
0016 #include <unistd.h>
0017 
0018 #include <string>
0019 #include <cstdio>
0020 #include <typeinfo>
0021 #include <sstream>
0022 
0023 popcon::EcalTPGLutIdMapHandler::EcalTPGLutIdMapHandler(const edm::ParameterSet& ps)
0024     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGLutIdMapHandler")) {
0025   edm::LogInfo("EcalTPGLutIdMapHandler") << "EcalTPGLutIdMap Source handler constructor";
0026   m_firstRun = (unsigned int)atoi(ps.getParameter<std::string>("firstRun").c_str());
0027   m_lastRun = (unsigned int)atoi(ps.getParameter<std::string>("lastRun").c_str());
0028   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0029   m_user = ps.getParameter<std::string>("OnlineDBUser");
0030   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0031   m_locationsource = ps.getParameter<std::string>("LocationSource");
0032   m_location = ps.getParameter<std::string>("Location");
0033   m_gentag = ps.getParameter<std::string>("GenTag");
0034   m_runtype = ps.getParameter<std::string>("RunType");
0035 
0036   edm::LogInfo("EcalTPGLutIdMapHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0037 }
0038 
0039 popcon::EcalTPGLutIdMapHandler::~EcalTPGLutIdMapHandler() {}
0040 
0041 void popcon::EcalTPGLutIdMapHandler::getNewObjects() {
0042   edm::LogInfo("EcalTPGLutIdMapHandler") << "Started GetNewObjects!!!";
0043 
0044   //check whats already inside of database
0045   if (tagInfo().size) {
0046     //check whats already inside of database
0047     std::cout << "got offlineInfo = " << std::endl;
0048     std::cout << "tag name = " << tagInfo().name << std::endl;
0049     std::cout << "size = " << tagInfo().size << std::endl;
0050   } else {
0051     std::cout << " First object for this tag " << std::endl;
0052   }
0053 
0054   unsigned int max_since = 0;
0055   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0056   edm::LogInfo("EcalTPGLutIdMapHandler") << "max_since : " << max_since;
0057   edm::LogInfo("EcalTPGLutIdMapHandler") << "retrieved last payload ";
0058 
0059   // here we retrieve all the runs after the last from online DB
0060   edm::LogInfo("EcalTPGLutIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
0061 
0062   edm::LogInfo("EcalTPGLutIdMapHandler") << "Making connection...";
0063   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0064   edm::LogInfo("EcalTPGLutIdMapHandler") << "Done.";
0065 
0066   if (!econn) {
0067     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0068     //      cerr << e.what() << std::endl;
0069     throw cms::Exception("OMDS not available");
0070   }
0071 
0072   LocationDef my_locdef;
0073   my_locdef.setLocation(m_location);
0074 
0075   RunTypeDef my_rundef;
0076   my_rundef.setRunType(m_runtype);
0077 
0078   RunTag my_runtag;
0079   my_runtag.setLocationDef(my_locdef);
0080   my_runtag.setRunTypeDef(my_rundef);
0081   my_runtag.setGeneralTag(m_gentag);
0082 
0083   readFromFile("last_tpg_lutIdMap_settings.txt");
0084 
0085   unsigned int min_run;
0086 
0087   if (m_firstRun < m_i_run_number) {
0088     min_run = m_i_run_number + 1;
0089   } else {
0090     min_run = m_firstRun;
0091   }
0092 
0093   if (min_run < max_since) {
0094     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0095   }
0096 
0097   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0098             << std::endl;
0099 
0100   unsigned int max_run = m_lastRun;
0101   edm::LogInfo("EcalTPGLutIdMapHandler") << "min_run=  " << min_run << "max_run= " << max_run;
0102 
0103   RunList my_list;
0104   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0105   //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
0106 
0107   std::vector<RunIOV> run_vec = my_list.getRuns();
0108   size_t num_runs = run_vec.size();
0109 
0110   std::cout << "number of runs is : " << num_runs << std::endl;
0111 
0112   unsigned int irun;
0113   if (num_runs > 0) {
0114     for (size_t kr = 0; kr < run_vec.size(); kr++) {
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         RunTPGConfigDat dat = it->second;
0135         the_config_tag = dat.getConfigTag();
0136         the_config_version = dat.getVersion();
0137       }
0138 
0139       // it is all the same for all SM... get the last one
0140 
0141       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0142 
0143       // here we should check if it is the same as previous run.
0144 
0145       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0146         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0147                   << std::endl;
0148 
0149         FEConfigMainInfo fe_main_info;
0150         fe_main_info.setConfigTag(the_config_tag);
0151         fe_main_info.setVersion(the_config_version);
0152 
0153         try {
0154           std::cout << " before fetch config set" << std::endl;
0155           econn->fetchConfigSet(&fe_main_info);
0156           std::cout << " after fetch config set" << std::endl;
0157 
0158           // now get TPGLutIdMap
0159           int lutId = fe_main_info.getLUTId();
0160 
0161           if (lutId != m_i_lutIdMap) {
0162             FEConfigLUTInfo fe_lut_info;
0163             fe_lut_info.setId(lutId);
0164             econn->fetchConfigSet(&fe_lut_info);
0165             std::map<EcalLogicID, FEConfigLUTGroupDat> dataset_TpgLut;
0166 
0167             econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
0168             edm::LogInfo("EcalTPGLutIdMapHandler") << "Got object!";
0169 
0170             EcalTPGLutIdMap* lutMap = new EcalTPGLutIdMap;
0171 
0172             typedef std::map<EcalLogicID, FEConfigLUTGroupDat>::const_iterator CIfelut;
0173             EcalLogicID ecid_xt;
0174             FEConfigLUTGroupDat rd_lut;
0175 
0176             for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) {
0177               ecid_xt = p->first;
0178               rd_lut = p->second;
0179 
0180               //std::string ecid_name=ecid_xt.getName();
0181 
0182               unsigned int lutArray[1024];
0183               for (int ilut = 0; ilut < 1024; ++ilut) {
0184                 lutArray[ilut] = rd_lut.getLUTValue(ilut);
0185                 std::cout << ilut << "  " << rd_lut.getLUTValue(ilut) << std::endl;
0186               }
0187 
0188               EcalTPGLut mylut;
0189               mylut.setLut(lutArray);
0190               lutMap->setValue(rd_lut.getLUTGroupId(), mylut);
0191             }
0192 
0193             Time_t snc = (Time_t)irun;
0194             m_to_transfer.push_back(std::make_pair((EcalTPGLutIdMap*)lutMap, snc));
0195 
0196             m_i_run_number = irun;
0197             m_i_tag = the_config_tag;
0198             m_i_version = the_config_version;
0199             m_i_lutIdMap = lutId;
0200 
0201             writeFile("last_tpg_lutIdMap_settings.txt");
0202 
0203           } else {
0204             m_i_run_number = irun;
0205             m_i_tag = the_config_tag;
0206             m_i_version = the_config_version;
0207 
0208             writeFile("last_tpg_lutIdMap_settings.txt");
0209 
0210             std::cout << " even if the tag/version is not the same, the lutIdMap id is the same -> no transfer needed "
0211                       << std::endl;
0212           }
0213 
0214         }
0215 
0216         catch (std::exception& e) {
0217           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0218                     << std::endl;
0219           std::cout << e.what() << std::endl;
0220           m_i_run_number = irun;
0221         }
0222         std::cout << " **************** " << std::endl;
0223 
0224       } else if (nr == 0) {
0225         m_i_run_number = irun;
0226         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0227         std::cout << " **************** " << std::endl;
0228       } else {
0229         m_i_run_number = irun;
0230         m_i_tag = the_config_tag;
0231         m_i_version = the_config_version;
0232         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0233         std::cout << " **************** " << std::endl;
0234         writeFile("last_tpg_lutIdMap_settings.txt");
0235       }
0236     }
0237   }
0238 
0239   delete econn;
0240 
0241   edm::LogInfo("EcalTPGLutIdMapHandler") << "Ecal - > end of getNewObjects -----------";
0242 }
0243 
0244 void popcon::EcalTPGLutIdMapHandler::readFromFile(const char* inputFile) {
0245   //-------------------------------------------------------------
0246 
0247   m_i_tag = "";
0248   m_i_version = 0;
0249   m_i_run_number = 0;
0250   m_i_lutIdMap = 0;
0251 
0252   FILE* inpFile;  // input file
0253   inpFile = fopen(inputFile, "r");
0254   if (!inpFile) {
0255     edm::LogError("EcalTPGLutIdMapHandler") << "*** Can not open file: " << inputFile;
0256     return;
0257   }
0258 
0259   char line[256];
0260 
0261   std::ostringstream str;
0262 
0263   fgets(line, 255, inpFile);
0264   m_i_tag = to_string(line);
0265   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0266 
0267   fgets(line, 255, inpFile);
0268   m_i_version = atoi(line);
0269   str << "version= " << m_i_version << std::endl;
0270 
0271   fgets(line, 255, inpFile);
0272   m_i_run_number = atoi(line);
0273   str << "run_number= " << m_i_run_number << std::endl;
0274 
0275   fgets(line, 255, inpFile);
0276   m_i_lutIdMap = atoi(line);
0277   str << "lutIdMap_config= " << m_i_lutIdMap << std::endl;
0278 
0279   fclose(inpFile);  // close inp. file
0280 }
0281 
0282 void popcon::EcalTPGLutIdMapHandler::writeFile(const char* inputFile) {
0283   //-------------------------------------------------------------
0284 
0285   std::ofstream myfile;
0286   myfile.open(inputFile);
0287   myfile << m_i_tag << std::endl;
0288   myfile << m_i_version << std::endl;
0289   myfile << m_i_run_number << std::endl;
0290   myfile << m_i_lutIdMap << std::endl;
0291 
0292   myfile.close();
0293 }