Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGLinConstHandler.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/FEConfigLUTInfo.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 #include <iostream>
0011 
0012 #include <fstream>
0013 
0014 #include <ctime>
0015 #include <unistd.h>
0016 
0017 #include <string>
0018 #include <cstdio>
0019 #include <typeinfo>
0020 #include <sstream>
0021 
0022 popcon::EcalTPGLinConstHandler::EcalTPGLinConstHandler(const edm::ParameterSet &ps)
0023     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGLinConstHandler")) {
0024   edm::LogInfo("EcalTPGLinConstHandler") << "EcalTPGLinConst Source handler constructor";
0025   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0026   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0027   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0028   m_user = ps.getParameter<std::string>("OnlineDBUser");
0029   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0030   m_locationsource = ps.getParameter<std::string>("LocationSource");
0031   m_location = ps.getParameter<std::string>("Location");
0032   m_gentag = ps.getParameter<std::string>("GenTag");
0033   m_runtype = ps.getParameter<std::string>("RunType");
0034 
0035   edm::LogInfo("EcalTPGLinConstHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0036 }
0037 
0038 popcon::EcalTPGLinConstHandler::~EcalTPGLinConstHandler() {}
0039 
0040 void popcon::EcalTPGLinConstHandler::getNewObjects() {
0041   edm::LogInfo("EcalTPGLinConstHandler") << "Started getNewObjects";
0042 
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("EcalTPGLinConstHandler") << "max_since = " << max_since;
0055   edm::LogInfo("EcalTPGLinConstHandler") << "Retrieved last payload ";
0056 
0057   // here we retrieve all the runs after the last from online DB
0058   edm::LogInfo("EcalTPGLinConstHandler") << "Retrieving run list from ONLINE DB ... " << std::endl;
0059 
0060   edm::LogInfo("EcalTPGLinConstHandler") << "Making connection..." << std::flush;
0061   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0062   edm::LogInfo("EcalTPGLinConstHandler") << "Done." << std::endl;
0063 
0064   if (!econn) {
0065     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0066     //      cerr << e.what() << std::endl;
0067     throw cms::Exception("OMDS not available");
0068   }
0069 
0070   LocationDef my_locdef;
0071   my_locdef.setLocation(m_location);
0072 
0073   RunTypeDef my_rundef;
0074   my_rundef.setRunType(m_runtype);
0075 
0076   RunTag my_runtag;
0077   my_runtag.setLocationDef(my_locdef);
0078   my_runtag.setRunTypeDef(my_rundef);
0079   my_runtag.setGeneralTag(m_gentag);
0080 
0081   readFromFile("last_tpg_lin_settings.txt");
0082 
0083   unsigned int min_run;
0084 
0085   if (m_firstRun < m_i_run_number) {
0086     min_run = m_i_run_number + 1;  // we have to add 1 to the last transferred one
0087   } else {
0088     min_run = m_firstRun;
0089   }
0090 
0091   if (min_run < max_since) {
0092     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0093   }
0094 
0095   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0096             << std::endl;
0097 
0098   unsigned int max_run = m_lastRun;
0099   edm::LogInfo("EcalTPGLinConstHandler") << "min_run=  " << min_run << "max_run = " << max_run;
0100 
0101   RunList my_list;
0102   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0103   //        my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0104 
0105   std::vector<RunIOV> run_vec = my_list.getRuns();
0106   size_t num_runs = run_vec.size();
0107 
0108   std::cout << "number of runs is : " << num_runs << std::endl;
0109 
0110   unsigned int irun = 0;
0111   if (num_runs > 0) {
0112     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0113       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0114       std::cout << " **************** " << std::endl;
0115       std::cout << " **************** " << std::endl;
0116       std::cout << " run= " << irun << std::endl;
0117 
0118       // retrieve the data :
0119       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0120       econn->fetchDataSet(&dataset, &run_vec[kr]);
0121 
0122       std::string the_config_tag = "";
0123       int the_config_version = 0;
0124 
0125       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0126 
0127       int nr = 0;
0128       for (it = dataset.begin(); it != dataset.end(); it++) {
0129         ++nr;
0130         //EcalLogicID ecalid  = it->first;
0131 
0132         RunTPGConfigDat dat = it->second;
0133         the_config_tag = dat.getConfigTag();
0134         the_config_version = dat.getVersion();
0135       }
0136 
0137       // it is all the same for all SM... get the last one
0138 
0139       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0140 
0141       // here we should check if it is the same as previous run.
0142 
0143       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0144         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0145                   << std::endl;
0146 
0147         FEConfigMainInfo fe_main_info;
0148         fe_main_info.setConfigTag(the_config_tag);
0149         fe_main_info.setVersion(the_config_version);
0150 
0151         try {
0152           std::cout << " before fetch config set" << std::endl;
0153           econn->fetchConfigSet(&fe_main_info);
0154           std::cout << " after fetch config set" << std::endl;
0155 
0156           // now get TPGLinConst
0157           int linId = fe_main_info.getLinId();
0158 
0159           if (linId != m_i_lin) {
0160             FEConfigLinInfo fe_lin_info;
0161             fe_lin_info.setId(linId);
0162             econn->fetchConfigSet(&fe_lin_info);
0163             std::map<EcalLogicID, FEConfigLinDat> dataset_TpgLin;
0164             econn->fetchDataSet(&dataset_TpgLin, &fe_lin_info);
0165 
0166             EcalTPGLinearizationConst *linC = new EcalTPGLinearizationConst;
0167             typedef std::map<EcalLogicID, FEConfigLinDat>::const_iterator CIfelin;
0168             EcalLogicID ecid_xt;
0169             FEConfigLinDat rd_lin;
0170             int icells = 0;
0171 
0172             for (CIfelin p = dataset_TpgLin.begin(); p != dataset_TpgLin.end(); p++) {
0173               ecid_xt = p->first;
0174               rd_lin = p->second;
0175               std::string ecid_name = ecid_xt.getName();
0176 
0177               //EB data
0178               if (ecid_name == "EB_crystal_number") {
0179                 int sm_num = ecid_xt.getID1();
0180                 int xt_num = ecid_xt.getID2();
0181                 EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
0182 
0183                 EcalTPGLinearizationConst::Item item;
0184                 item.mult_x1 = rd_lin.getMultX1();
0185                 item.mult_x6 = rd_lin.getMultX6();
0186                 item.mult_x12 = rd_lin.getMultX12();
0187                 item.shift_x1 = rd_lin.getShift1();
0188                 item.shift_x6 = rd_lin.getShift6();
0189                 item.shift_x12 = rd_lin.getShift12();
0190 
0191                 linC->insert(std::make_pair(ebdetid.rawId(), item));
0192                 ++icells;
0193               } else {
0194                 //EE data
0195                 int z = ecid_xt.getID1();
0196                 int x = ecid_xt.getID2();
0197                 int y = ecid_xt.getID3();
0198                 EEDetId eedetid(x, y, z, EEDetId::XYMODE);
0199 
0200                 EcalTPGLinearizationConst::Item item;
0201 
0202                 item.mult_x1 = rd_lin.getMultX1();
0203                 item.mult_x6 = rd_lin.getMultX6();
0204                 item.mult_x12 = rd_lin.getMultX12();
0205                 item.shift_x1 = rd_lin.getShift1();
0206                 item.shift_x6 = rd_lin.getShift6();
0207                 item.shift_x12 = rd_lin.getShift12();
0208 
0209                 linC->insert(std::make_pair(eedetid.rawId(), item));
0210                 ++icells;
0211               }
0212             }
0213 
0214             Time_t snc = (Time_t)irun;
0215             m_to_transfer.push_back(std::make_pair((EcalTPGLinearizationConst *)linC, snc));
0216 
0217             m_i_run_number = irun;
0218             m_i_tag = the_config_tag;
0219             m_i_version = the_config_version;
0220             m_i_lin = linId;
0221 
0222             writeFile("last_tpg_lin_settings.txt");
0223 
0224           } else {
0225             m_i_run_number = irun;
0226             m_i_tag = the_config_tag;
0227             m_i_version = the_config_version;
0228 
0229             writeFile("last_tpg_lin_settings.txt");
0230 
0231             std::cout << " even if the tag/version is not the same, the linearization constants id is the same -> no "
0232                          "transfer needed "
0233                       << std::endl;
0234           }
0235         } catch (std::exception &e) {
0236           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0237                     << std::endl;
0238           std::cout << e.what() << std::endl;
0239           m_i_run_number = irun;
0240         }
0241         std::cout << " **************** " << std::endl;
0242 
0243       } else if (nr == 0) {
0244         m_i_run_number = irun;
0245         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0246         std::cout << " **************** " << std::endl;
0247       } else {
0248         m_i_run_number = irun;
0249         m_i_tag = the_config_tag;
0250         m_i_version = the_config_version;
0251         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0252         std::cout << " **************** " << std::endl;
0253         writeFile("last_tpg_lin_settings.txt");
0254       }
0255     }
0256   }
0257 
0258   delete econn;
0259 
0260   edm::LogInfo("EcalTPGLinConstHandler") << "Ecal - > end of getNewObjects -----------";
0261 }
0262 
0263 void popcon::EcalTPGLinConstHandler::readFromFile(const char *inputFile) {
0264   //-------------------------------------------------------------
0265 
0266   m_i_tag = "";
0267   m_i_version = 0;
0268   m_i_run_number = 0;
0269   m_i_lin = 0;
0270 
0271   FILE *inpFile;  // input file
0272   inpFile = fopen(inputFile, "r");
0273   if (!inpFile) {
0274     edm::LogError("EcalTPGLinConstHandler") << "*** Can not open file: " << inputFile;
0275     return;
0276   }
0277 
0278   char line[256];
0279 
0280   std::ostringstream str;
0281 
0282   fgets(line, 255, inpFile);
0283   m_i_tag = to_string(line);
0284   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0285 
0286   fgets(line, 255, inpFile);
0287   m_i_version = atoi(line);
0288   str << "version= " << m_i_version << std::endl;
0289 
0290   fgets(line, 255, inpFile);
0291   m_i_run_number = atoi(line);
0292   str << "run_number= " << m_i_run_number << std::endl;
0293 
0294   fgets(line, 255, inpFile);
0295   m_i_lin = atoi(line);
0296   str << "lin_config= " << m_i_lin << std::endl;
0297 
0298   fclose(inpFile);  // close inp. file
0299 }
0300 
0301 void popcon::EcalTPGLinConstHandler::writeFile(const char *inputFile) {
0302   //-------------------------------------------------------------
0303 
0304   std::ofstream myfile;
0305   myfile.open(inputFile);
0306   myfile << m_i_tag << std::endl;
0307   myfile << m_i_version << std::endl;
0308   myfile << m_i_run_number << std::endl;
0309   myfile << m_i_lin << std::endl;
0310 
0311   myfile.close();
0312 }