Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGFineGrainEBIdMapHandler.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/FEConfigFgrInfo.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 #include <iostream>
0011 #include <fstream>
0012 
0013 #include <ctime>
0014 #include <unistd.h>
0015 
0016 #include <string>
0017 #include <cstdio>
0018 #include <typeinfo>
0019 #include <sstream>
0020 
0021 popcon::EcalTPGFineGrainEBIdMapHandler::EcalTPGFineGrainEBIdMapHandler(const edm::ParameterSet &ps)
0022     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGFineGrainEBIdMapHandler")) {
0023   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "EcalTPGFineGrainEBIdMap Source handler constructor.";
0024   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0025   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0026   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0027   m_user = ps.getParameter<std::string>("OnlineDBUser");
0028   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0029   m_locationsource = ps.getParameter<std::string>("LocationSource");
0030   m_location = ps.getParameter<std::string>("Location");
0031   m_gentag = ps.getParameter<std::string>("GenTag");
0032   m_runtype = ps.getParameter<std::string>("RunType");
0033 
0034   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0035 }
0036 
0037 popcon::EcalTPGFineGrainEBIdMapHandler::~EcalTPGFineGrainEBIdMapHandler() {}
0038 
0039 void popcon::EcalTPGFineGrainEBIdMapHandler::getNewObjects() {
0040   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "Started GetNewObjects!!!";
0041 
0042   //check whats already inside of database
0043   if (tagInfo().size) {
0044     //check whats already inside of database
0045     std::cout << "got offlineInfo = " << std::endl;
0046     std::cout << "tag name = " << tagInfo().name << std::endl;
0047     std::cout << "size = " << tagInfo().size << std::endl;
0048   } else {
0049     std::cout << " First object for this tag " << std::endl;
0050   }
0051 
0052   unsigned int max_since = 0;
0053   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0054   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "max_since : " << max_since;
0055   Ref fgrIdMap_db = lastPayload();
0056 
0057   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "retrieved last payload ";
0058 
0059   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "WOW: we just retrieved the last valid record from DB ";
0060 
0061   // here we retrieve all the runs after the last from online DB
0062 
0063   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
0064 
0065   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "Making connection...";
0066   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0067   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "Done.";
0068 
0069   if (!econn) {
0070     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0071     //      cerr << e.what() << std::endl;
0072     throw cms::Exception("OMDS not available");
0073   }
0074 
0075   LocationDef my_locdef;
0076   my_locdef.setLocation(m_location);
0077 
0078   RunTypeDef my_rundef;
0079   my_rundef.setRunType(m_runtype);
0080 
0081   RunTag my_runtag;
0082   my_runtag.setLocationDef(my_locdef);
0083   my_runtag.setRunTypeDef(my_rundef);
0084   my_runtag.setGeneralTag(m_gentag);
0085 
0086   readFromFile("last_tpg_fgrIdMap_settings.txt");
0087 
0088   unsigned int min_run;
0089 
0090   if (m_firstRun < m_i_run_number) {
0091     min_run = m_i_run_number + 1;
0092   } else {
0093     min_run = m_firstRun;
0094   }
0095   if (min_run < max_since) {
0096     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0097   }
0098 
0099   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0100             << std::endl;
0101 
0102   unsigned int max_run = m_lastRun;
0103   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "min_run= " << min_run << "max_run= " << max_run;
0104 
0105   RunList my_list;
0106   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0107   //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
0108   printf("after fetchRunList\n");
0109   fflush(stdout);
0110 
0111   std::vector<RunIOV> run_vec = my_list.getRuns();
0112   size_t num_runs = run_vec.size();
0113 
0114   std::cout << "number of runs is : " << num_runs << std::endl;
0115 
0116   unsigned int irun;
0117   if (num_runs > 0) {
0118     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0119       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0120 
0121       std::cout << " **************** " << std::endl;
0122       std::cout << " **************** " << std::endl;
0123       std::cout << " run= " << irun << std::endl;
0124 
0125       // retrieve the data :
0126       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0127       econn->fetchDataSet(&dataset, &run_vec[kr]);
0128 
0129       std::string the_config_tag = "";
0130       int the_config_version = 0;
0131 
0132       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0133 
0134       int nr = 0;
0135       for (it = dataset.begin(); it != dataset.end(); it++) {
0136         ++nr;
0137         EcalLogicID ecalid = it->first;
0138         RunTPGConfigDat dat = it->second;
0139         the_config_tag = dat.getConfigTag();
0140         the_config_version = dat.getVersion();
0141       }
0142 
0143       // it is all the same for all SM... get the last one
0144 
0145       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0146 
0147       // here we should check if it is the same as previous run.
0148 
0149       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0150         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0151                   << std::endl;
0152 
0153         FEConfigMainInfo fe_main_info;
0154         fe_main_info.setConfigTag(the_config_tag);
0155         fe_main_info.setVersion(the_config_version);
0156 
0157         try {
0158           std::cout << " before fetch config set" << std::endl;
0159           econn->fetchConfigSet(&fe_main_info);
0160           std::cout << " after fetch config set" << std::endl;
0161 
0162           // now get TPGFineGrainEBIdMap
0163           int fgrId = fe_main_info.getFgrId();
0164           if (fgrId != m_i_fgrIdMap) {
0165             FEConfigFgrInfo fe_fgr_info;
0166             fe_fgr_info.setId(fgrId);
0167             econn->fetchConfigSet(&fe_fgr_info);
0168             std::map<EcalLogicID, FEConfigFgrGroupDat> dataset_TpgFineGrainEB;
0169             econn->fetchDataSet(&dataset_TpgFineGrainEB, &fe_fgr_info);
0170             edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "Got object!";
0171             EcalTPGFineGrainEBIdMap *fgrMap = new EcalTPGFineGrainEBIdMap;
0172             typedef std::map<EcalLogicID, FEConfigFgrGroupDat>::const_iterator CIfefgr;
0173             EcalLogicID ecid_xt;
0174             FEConfigFgrGroupDat rd_fgr;
0175 
0176             int igroups = 0;
0177             for (CIfefgr p = dataset_TpgFineGrainEB.begin(); p != dataset_TpgFineGrainEB.end(); p++) {
0178               ecid_xt = p->first;
0179               rd_fgr = p->second;
0180 
0181               std::string ecid_name = ecid_xt.getName();
0182 
0183               EcalTPGFineGrainConstEB f;
0184               unsigned int ThrL = (unsigned int)rd_fgr.getThreshLow();
0185               unsigned int ThrH = (unsigned int)rd_fgr.getThreshHigh();
0186               unsigned int RatioL = (unsigned int)rd_fgr.getRatioLow();
0187               unsigned int RatioH = (unsigned int)rd_fgr.getRatioHigh();
0188               unsigned int LutConfId = (unsigned int)rd_fgr.getLUTValue();
0189               // I have exchanged the values to see if it works
0190               f.setValues(RatioL, RatioH, ThrL, ThrH, LutConfId);
0191               fgrMap->setValue(rd_fgr.getFgrGroupId(), f);
0192               ++igroups;
0193             }
0194 
0195             Time_t snc = (Time_t)irun;
0196             m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainEBIdMap *)fgrMap, 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_fgrIdMap = fgrId;
0202 
0203             writeFile("last_tpg_fgrIdMap_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_fgrIdMap_settings.txt");
0211 
0212             std::cout << " even if the tag/version is not the same, the fgrIdMap 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_fgrIdMap_settings.txt");
0237       }
0238     }
0239   }
0240 
0241   delete econn;
0242   edm::LogInfo("EcalTPGFineGrainEBIdMapHandler") << "Ecal - > end of getNewObjects -----------";
0243 }
0244 
0245 void popcon::EcalTPGFineGrainEBIdMapHandler::readFromFile(const char *inputFile) {
0246   //-------------------------------------------------------------
0247 
0248   m_i_tag = "";
0249   m_i_version = 0;
0250   m_i_run_number = 0;
0251   m_i_fgrIdMap = 0;
0252 
0253   FILE *inpFile;  // input file
0254   inpFile = fopen(inputFile, "r");
0255   if (!inpFile) {
0256     edm::LogError("EcalTPGFineGrainEBIdMapHandler") << "*** Can not open file: " << inputFile;
0257     return;
0258   }
0259 
0260   char line[256];
0261 
0262   std::ostringstream str;
0263 
0264   fgets(line, 255, inpFile);
0265   m_i_tag = to_string(line);
0266   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0267 
0268   fgets(line, 255, inpFile);
0269   m_i_version = atoi(line);
0270   str << "version= " << m_i_version << std::endl;
0271 
0272   fgets(line, 255, inpFile);
0273   m_i_run_number = atoi(line);
0274   str << "run_number= " << m_i_run_number << std::endl;
0275 
0276   fgets(line, 255, inpFile);
0277   m_i_fgrIdMap = atoi(line);
0278   str << "fgrIdMap_config= " << m_i_fgrIdMap << std::endl;
0279 
0280   fclose(inpFile);  // close inp. file
0281 }
0282 
0283 void popcon::EcalTPGFineGrainEBIdMapHandler::writeFile(const char *inputFile) {
0284   //-------------------------------------------------------------
0285 
0286   std::ofstream myfile;
0287   myfile.open(inputFile);
0288   myfile << m_i_tag << std::endl;
0289   myfile << m_i_version << std::endl;
0290   myfile << m_i_run_number << std::endl;
0291   myfile << m_i_fgrIdMap << std::endl;
0292 
0293   myfile.close();
0294 }