Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 
0171             for (CIfelin p = dataset_TpgLin.begin(); p != dataset_TpgLin.end(); p++) {
0172               ecid_xt = p->first;
0173               rd_lin = p->second;
0174               std::string ecid_name = ecid_xt.getName();
0175 
0176               //EB data
0177               if (ecid_name == "EB_crystal_number") {
0178                 int sm_num = ecid_xt.getID1();
0179                 int xt_num = ecid_xt.getID2();
0180                 EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
0181 
0182                 EcalTPGLinearizationConst::Item item;
0183                 item.mult_x1 = rd_lin.getMultX1();
0184                 item.mult_x6 = rd_lin.getMultX6();
0185                 item.mult_x12 = rd_lin.getMultX12();
0186                 item.shift_x1 = rd_lin.getShift1();
0187                 item.shift_x6 = rd_lin.getShift6();
0188                 item.shift_x12 = rd_lin.getShift12();
0189 
0190                 linC->insert(std::make_pair(ebdetid.rawId(), item));
0191               } else {
0192                 //EE data
0193                 int z = ecid_xt.getID1();
0194                 int x = ecid_xt.getID2();
0195                 int y = ecid_xt.getID3();
0196                 EEDetId eedetid(x, y, z, EEDetId::XYMODE);
0197 
0198                 EcalTPGLinearizationConst::Item item;
0199 
0200                 item.mult_x1 = rd_lin.getMultX1();
0201                 item.mult_x6 = rd_lin.getMultX6();
0202                 item.mult_x12 = rd_lin.getMultX12();
0203                 item.shift_x1 = rd_lin.getShift1();
0204                 item.shift_x6 = rd_lin.getShift6();
0205                 item.shift_x12 = rd_lin.getShift12();
0206 
0207                 linC->insert(std::make_pair(eedetid.rawId(), item));
0208               }
0209             }
0210 
0211             Time_t snc = (Time_t)irun;
0212             m_to_transfer.push_back(std::make_pair((EcalTPGLinearizationConst *)linC, snc));
0213 
0214             m_i_run_number = irun;
0215             m_i_tag = the_config_tag;
0216             m_i_version = the_config_version;
0217             m_i_lin = linId;
0218 
0219             writeFile("last_tpg_lin_settings.txt");
0220 
0221           } else {
0222             m_i_run_number = irun;
0223             m_i_tag = the_config_tag;
0224             m_i_version = the_config_version;
0225 
0226             writeFile("last_tpg_lin_settings.txt");
0227 
0228             std::cout << " even if the tag/version is not the same, the linearization constants id is the same -> no "
0229                          "transfer needed "
0230                       << std::endl;
0231           }
0232         } catch (std::exception &e) {
0233           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0234                     << std::endl;
0235           std::cout << e.what() << std::endl;
0236           m_i_run_number = irun;
0237         }
0238         std::cout << " **************** " << std::endl;
0239 
0240       } else if (nr == 0) {
0241         m_i_run_number = irun;
0242         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0243         std::cout << " **************** " << std::endl;
0244       } else {
0245         m_i_run_number = irun;
0246         m_i_tag = the_config_tag;
0247         m_i_version = the_config_version;
0248         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0249         std::cout << " **************** " << std::endl;
0250         writeFile("last_tpg_lin_settings.txt");
0251       }
0252     }
0253   }
0254 
0255   delete econn;
0256 
0257   edm::LogInfo("EcalTPGLinConstHandler") << "Ecal - > end of getNewObjects -----------";
0258 }
0259 
0260 void popcon::EcalTPGLinConstHandler::readFromFile(const char *inputFile) {
0261   //-------------------------------------------------------------
0262 
0263   m_i_tag = "";
0264   m_i_version = 0;
0265   m_i_run_number = 0;
0266   m_i_lin = 0;
0267 
0268   FILE *inpFile;  // input file
0269   inpFile = fopen(inputFile, "r");
0270   if (!inpFile) {
0271     edm::LogError("EcalTPGLinConstHandler") << "*** Can not open file: " << inputFile;
0272     return;
0273   }
0274 
0275   char line[256];
0276 
0277   std::ostringstream str;
0278 
0279   fgets(line, 255, inpFile);
0280   m_i_tag = to_string(line);
0281   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0282 
0283   fgets(line, 255, inpFile);
0284   m_i_version = atoi(line);
0285   str << "version= " << m_i_version << std::endl;
0286 
0287   fgets(line, 255, inpFile);
0288   m_i_run_number = atoi(line);
0289   str << "run_number= " << m_i_run_number << std::endl;
0290 
0291   fgets(line, 255, inpFile);
0292   m_i_lin = atoi(line);
0293   str << "lin_config= " << m_i_lin << std::endl;
0294 
0295   fclose(inpFile);  // close inp. file
0296 }
0297 
0298 void popcon::EcalTPGLinConstHandler::writeFile(const char *inputFile) {
0299   //-------------------------------------------------------------
0300 
0301   std::ofstream myfile;
0302   myfile.open(inputFile);
0303   myfile << m_i_tag << std::endl;
0304   myfile << m_i_version << std::endl;
0305   myfile << m_i_run_number << std::endl;
0306   myfile << m_i_lin << std::endl;
0307 
0308   myfile.close();
0309 }