Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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             int igroups = 0;
0176 
0177             for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) {
0178               ecid_xt = p->first;
0179               rd_lut = p->second;
0180 
0181               //std::string ecid_name=ecid_xt.getName();
0182 
0183               unsigned int lutArray[1024];
0184               for (int ilut = 0; ilut < 1024; ++ilut) {
0185                 lutArray[ilut] = rd_lut.getLUTValue(ilut);
0186                 std::cout << ilut << "  " << rd_lut.getLUTValue(ilut) << std::endl;
0187               }
0188 
0189               EcalTPGLut mylut;
0190               mylut.setLut(lutArray);
0191               lutMap->setValue(rd_lut.getLUTGroupId(), mylut);
0192               ++igroups;
0193             }
0194 
0195             Time_t snc = (Time_t)irun;
0196             m_to_transfer.push_back(std::make_pair((EcalTPGLutIdMap*)lutMap, snc));
0197 
0198             m_i_run_number = irun;
0199             m_i_tag = the_config_tag;
0200             m_i_version = the_config_version;
0201             m_i_lutIdMap = lutId;
0202 
0203             writeFile("last_tpg_lutIdMap_settings.txt");
0204 
0205           } else {
0206             m_i_run_number = irun;
0207             m_i_tag = the_config_tag;
0208             m_i_version = the_config_version;
0209 
0210             writeFile("last_tpg_lutIdMap_settings.txt");
0211 
0212             std::cout << " even if the tag/version is not the same, the lutIdMap id is the same -> no transfer needed "
0213                       << std::endl;
0214           }
0215 
0216         }
0217 
0218         catch (std::exception& e) {
0219           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0220                     << std::endl;
0221           std::cout << e.what() << std::endl;
0222           m_i_run_number = irun;
0223         }
0224         std::cout << " **************** " << std::endl;
0225 
0226       } else if (nr == 0) {
0227         m_i_run_number = irun;
0228         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0229         std::cout << " **************** " << std::endl;
0230       } else {
0231         m_i_run_number = irun;
0232         m_i_tag = the_config_tag;
0233         m_i_version = the_config_version;
0234         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0235         std::cout << " **************** " << std::endl;
0236         writeFile("last_tpg_lutIdMap_settings.txt");
0237       }
0238     }
0239   }
0240 
0241   delete econn;
0242 
0243   edm::LogInfo("EcalTPGLutIdMapHandler") << "Ecal - > end of getNewObjects -----------";
0244 }
0245 
0246 void popcon::EcalTPGLutIdMapHandler::readFromFile(const char* inputFile) {
0247   //-------------------------------------------------------------
0248 
0249   m_i_tag = "";
0250   m_i_version = 0;
0251   m_i_run_number = 0;
0252   m_i_lutIdMap = 0;
0253 
0254   FILE* inpFile;  // input file
0255   inpFile = fopen(inputFile, "r");
0256   if (!inpFile) {
0257     edm::LogError("EcalTPGLutIdMapHandler") << "*** Can not open file: " << inputFile;
0258     return;
0259   }
0260 
0261   char line[256];
0262 
0263   std::ostringstream str;
0264 
0265   fgets(line, 255, inpFile);
0266   m_i_tag = to_string(line);
0267   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0268 
0269   fgets(line, 255, inpFile);
0270   m_i_version = atoi(line);
0271   str << "version= " << m_i_version << std::endl;
0272 
0273   fgets(line, 255, inpFile);
0274   m_i_run_number = atoi(line);
0275   str << "run_number= " << m_i_run_number << std::endl;
0276 
0277   fgets(line, 255, inpFile);
0278   m_i_lutIdMap = atoi(line);
0279   str << "lutIdMap_config= " << m_i_lutIdMap << std::endl;
0280 
0281   fclose(inpFile);  // close inp. file
0282 }
0283 
0284 void popcon::EcalTPGLutIdMapHandler::writeFile(const char* inputFile) {
0285   //-------------------------------------------------------------
0286 
0287   std::ofstream myfile;
0288   myfile.open(inputFile);
0289   myfile << m_i_tag << std::endl;
0290   myfile << m_i_version << std::endl;
0291   myfile << m_i_run_number << std::endl;
0292   myfile << m_i_lutIdMap << std::endl;
0293 
0294   myfile.close();
0295 }