Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
0002 
0003 #include "CondTools/Ecal/interface/EcalTPGOddWeightIdMapHandler.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/FEConfigWeightInfo.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 #include <iostream>
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::EcalTPGOddWeightIdMapHandler::EcalTPGOddWeightIdMapHandler(const edm::ParameterSet& ps)
0023     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGOddWeightIdMapHandler")) {
0024   edm::LogInfo("EcalTPGOddWeightIdMapHandler") << "EcalTPGOddWeightIdMap 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   m_file_type = ps.getParameter<std::string>("fileType");  // xml/txt
0035   m_file_name = ps.getParameter<std::string>("fileName");
0036 
0037   edm::LogInfo("EcalTPGOddWeightIdMapHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0038 }
0039 
0040 popcon::EcalTPGOddWeightIdMapHandler::~EcalTPGOddWeightIdMapHandler() {}
0041 
0042 void popcon::EcalTPGOddWeightIdMapHandler::getNewObjects() {
0043   if (m_file_type == "txt") {
0044     readtxtFile();
0045   } else if (m_file_type == "xml") {
0046     readxmlFile();
0047   } else {
0048     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Started GetNewObjects!!!";
0049 
0050     //check whats already inside of database
0051     if (tagInfo().size) {
0052       //check whats already inside of database
0053       edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "got offlineInfo = ";
0054       edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "tag name = " << tagInfo().name;
0055       edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "size = " << tagInfo().size;
0056 
0057     } else {
0058       edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " First object for this tag ";
0059     }
0060 
0061     unsigned int max_since = 0;
0062     max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0063     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "max_since : " << max_since;
0064 
0065     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "retrieved last payload ";
0066 
0067     // here we retrieve all the runs after the last from online DB
0068     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
0069 
0070     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Making connection...";
0071     auto econn = std::make_shared<EcalCondDBInterface>(m_sid, m_user, m_pass);
0072 
0073     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Done.";
0074 
0075     if (!econn) {
0076       edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " connection parameters " << m_sid << "/" << m_user;
0077       throw cms::Exception("OMDS not available");
0078     }
0079 
0080     LocationDef my_locdef;
0081     my_locdef.setLocation(m_location);
0082 
0083     RunTypeDef my_rundef;
0084     my_rundef.setRunType(m_runtype);
0085 
0086     RunTag my_runtag;
0087     my_runtag.setLocationDef(my_locdef);
0088     my_runtag.setRunTypeDef(my_rundef);
0089     my_runtag.setGeneralTag(m_gentag);
0090 
0091     readFromFile("last_tpg_OddweightIdMap_settings.txt");
0092 
0093     unsigned int min_run;
0094 
0095     if (m_firstRun < m_i_run_number) {
0096       min_run = m_i_run_number + 1;
0097     } else {
0098       min_run = m_firstRun;
0099     }
0100 
0101     if (min_run < max_since) {
0102       min_run = max_since + 1;  // we have to add 1 to the last transferred one
0103     }
0104     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
0105         << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since;
0106 
0107     unsigned int max_run = m_lastRun;
0108     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "min_run= " << min_run << "max_run= " << max_run;
0109 
0110     RunList my_list;
0111     my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0112 
0113     std::vector<RunIOV> run_vec = my_list.getRuns();
0114     size_t num_runs = run_vec.size();
0115     edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "number of Mon runs is : " << num_runs;
0116 
0117     unsigned int irun;
0118     if (num_runs > 0) {
0119       for (size_t kr = 0; kr < run_vec.size(); kr++) {
0120         irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0121 
0122         edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0123         edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0124         edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " run= " << irun;
0125 
0126         // retrieve the data :
0127         std::map<EcalLogicID, RunTPGConfigDat> dataset;
0128         econn->fetchDataSet(&dataset, &run_vec[kr]);
0129 
0130         std::string the_config_tag = "";
0131         int the_config_version = 0;
0132 
0133         std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0134 
0135         int nr = 0;
0136         for (it = dataset.begin(); it != dataset.end(); it++) {
0137           ++nr;
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         edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
0146             << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version;
0147 
0148         // here we should check if it is the same as previous run.
0149 
0150         if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0151           edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
0152               << "the tag is different from last transferred run ... retrieving last config set from DB";
0153 
0154           FEConfigMainInfo fe_main_info;
0155           fe_main_info.setConfigTag(the_config_tag);
0156           fe_main_info.setVersion(the_config_version);
0157 
0158           try {
0159             edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " before fetch config set";
0160             econn->fetchConfigSet(&fe_main_info);
0161             edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " after fetch config set";
0162 
0163             // now get TPGOddWeightIdMap
0164             int weightId = fe_main_info.getWei2Id();
0165 
0166             if (weightId != m_i_oddweightIdMap) {
0167               FEConfigOddWeightInfo fe_odd_weight_info;
0168               fe_odd_weight_info.setId(weightId);
0169               econn->fetchConfigSet(&fe_odd_weight_info);
0170               std::map<EcalLogicID, FEConfigOddWeightGroupDat> dataset_TpgWeight;
0171               econn->fetchDataSet(&dataset_TpgWeight, &fe_odd_weight_info);
0172               edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Got object!";
0173               EcalTPGOddWeightIdMap* weightMap = new EcalTPGOddWeightIdMap;
0174               typedef std::map<EcalLogicID, FEConfigOddWeightGroupDat>::const_iterator CIfeweight;
0175               EcalLogicID ecid_xt;
0176               FEConfigOddWeightGroupDat rd_w;
0177 
0178               int igroups = 0;
0179               for (CIfeweight p = dataset_TpgWeight.begin(); p != dataset_TpgWeight.end(); p++) {
0180                 rd_w = p->second;
0181                 // EB and EE data
0182                 EcalTPGWeights w;
0183                 unsigned int weight0 = static_cast<unsigned int>(rd_w.getWeight4());
0184                 unsigned int weight1 = static_cast<unsigned int>(rd_w.getWeight3());
0185                 unsigned int weight2 = static_cast<unsigned int>(rd_w.getWeight2());
0186                 unsigned int weight3 = static_cast<unsigned int>(rd_w.getWeight1() - 0x80);
0187                 unsigned int weight4 = static_cast<unsigned int>(rd_w.getWeight0());
0188 
0189                 w.setValues(weight0, weight1, weight2, weight3, weight4);
0190                 weightMap->setValue(rd_w.getWeightGroupId(), w);
0191 
0192                 ++igroups;
0193               }
0194 
0195               edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "found " << igroups << "Weight groups";
0196 
0197               Time_t snc = (Time_t)irun;
0198               m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightIdMap*)weightMap, snc));
0199 
0200               m_i_run_number = irun;
0201               m_i_tag = the_config_tag;
0202               m_i_version = the_config_version;
0203               m_i_oddweightIdMap = weightId;
0204 
0205               writeFile("last_tpg_OddweightIdMap_settings.txt");
0206 
0207             } else {
0208               m_i_run_number = irun;
0209               m_i_tag = the_config_tag;
0210               m_i_version = the_config_version;
0211 
0212               writeFile("last_tpg_OddweightIdMap_settings.txt");
0213 
0214               edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
0215                   << " even if the tag/version is not the same, the weightIdMap id is the same -> no transfer needed ";
0216             }
0217 
0218           } catch (std::exception& e) {
0219             throw cms::Exception("FileReadError") << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag
0220                                                   << " version=" << the_config_version << "\n"
0221                                                   << e.what();
0222           }
0223           edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0224 
0225         } else if (nr == 0) {
0226           m_i_run_number = irun;
0227           edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
0228               << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ";
0229           edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0230         } else {
0231           m_i_run_number = irun;
0232           m_i_tag = the_config_tag;
0233           m_i_version = the_config_version;
0234           edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " the tag/version is the same -> no transfer needed ";
0235           edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0236           writeFile("last_tpg_OddweightIdMap_settings.txt");
0237         }
0238       }
0239     }
0240 
0241   }  // usual way
0242   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Ecal - > end of getNewObjects -----------";
0243 }
0244 void popcon::EcalTPGOddWeightIdMapHandler::readtxtFile() {
0245   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " reading the input file " << m_file_name;
0246   std::ifstream fInput;
0247   fInput.open(m_file_name);
0248   if (!fInput.is_open()) {
0249     throw cms::Exception("FileReadError") << "ERROR : cannot open file " << m_file_name;
0250   }
0251   unsigned int wloc[5];
0252   EcalTPGWeights w;
0253   EcalTPGOddWeightIdMap* weightMap = new EcalTPGOddWeightIdMap;
0254 
0255   int igroups = 0;
0256   std::string line;
0257   while (!fInput.eof()) {
0258     getline(fInput, line);
0259     if (!line.empty()) {
0260       std::stringstream ss;
0261       ss << line;
0262       ss >> wloc[0] >> wloc[1] >> wloc[2] >> wloc[3] >> wloc[4];
0263       w.setValues(wloc[0], wloc[1], wloc[2], wloc[3], wloc[4]);
0264       weightMap->setValue(igroups, w);
0265       igroups++;
0266     }
0267   }
0268   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "found " << igroups << " Weight groups";
0269   try {
0270     Time_t snc = (Time_t)m_firstRun;
0271     m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightIdMap*)weightMap, snc));
0272   } catch (std::exception& e) {
0273     throw cms::Exception("FileReadError") << "EcalTPGOddWeightIdMapHandler::readtxtFile error : " << e.what();
0274   }
0275   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0276 }
0277 
0278 void popcon::EcalTPGOddWeightIdMapHandler::readxmlFile() {
0279   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " reading the input file " << m_file_name;
0280   std::ifstream fxml;
0281   fxml.open(m_file_name);
0282   if (!fxml.is_open()) {
0283     throw cms::Exception("FileReadError") << "ERROR: Cannot open file : " << m_file_name;
0284   }
0285   std::string dummyLine, bid;
0286   unsigned int wloc[5];
0287   EcalTPGWeights w;
0288   EcalTPGOddWeightIdMap* weightMap = new EcalTPGOddWeightIdMap;
0289 
0290   int ngroups, igroups;
0291   for (int i = 0; i < 5; i++)
0292     std::getline(fxml, dummyLine);  // skip first lines
0293   // get the Weight group number
0294   fxml >> bid;
0295   std::string stt = bid.substr(7, 1);
0296   std::istringstream sc(stt);
0297   sc >> ngroups;
0298   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "found " << ngroups << " Weight groups";
0299   for (int i = 0; i < 2; i++)
0300     std::getline(fxml, dummyLine);  //    <item_version>0</item_version>
0301   for (int i = 0; i < ngroups; i++) {
0302     std::getline(fxml, dummyLine);  //    <item
0303     fxml >> bid;                    //    <first
0304     std::size_t found = bid.find("</");
0305     stt = bid.substr(7, found - 7);
0306     std::istringstream sg1(stt);
0307     sg1 >> igroups;
0308     if (igroups != i) {
0309       throw cms::Exception("MismatchError") << "ERROR in group " << i << ": " << bid << " igroups " << igroups;
0310     }
0311     for (int i = 0; i < 2; i++)
0312       std::getline(fxml, dummyLine);  // < second
0313     for (int i = 0; i < 5; i++) {
0314       fxml >> bid;
0315       found = bid.find("</");
0316       stt = bid.substr(5, found - 5);
0317       std::istringstream w(stt);
0318       w >> wloc[i];
0319     }
0320     w.setValues(wloc[0], wloc[1], wloc[2], wloc[3], wloc[4]);
0321     weightMap->setValue(igroups, w);
0322     for (int i = 0; i < 3; i++)
0323       std::getline(fxml, dummyLine);  //    </item>
0324   }
0325   try {
0326     Time_t snc = (Time_t)m_firstRun;
0327     m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightIdMap*)weightMap, snc));
0328 
0329   } catch (std::exception& e) {
0330     throw cms::Exception("FileReadError") << "EcalTPGOddWeightIdMapHandler::readxmlFile error : " << e.what();
0331   }
0332   edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
0333 }
0334 
0335 void popcon::EcalTPGOddWeightIdMapHandler::readFromFile(const char* inputFile) {
0336   //-------------------------------------------------------------
0337 
0338   m_i_tag = "";
0339   m_i_version = 0;
0340   m_i_run_number = 0;
0341   m_i_oddweightIdMap = 0;
0342 
0343   FILE* inpFile;  // input file
0344   inpFile = fopen(inputFile, "r");
0345   if (!inpFile) {
0346     edm::LogError("EcalTPGOddWeightIdMapHandler") << "*** Can not open file: " << inputFile;
0347     return;
0348   }
0349 
0350   char line[256];
0351 
0352   std::ostringstream str;
0353 
0354   fgets(line, 255, inpFile);
0355   m_i_tag = to_string(line);
0356   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0357 
0358   fgets(line, 255, inpFile);
0359   m_i_version = atoi(line);
0360   str << "version= " << m_i_version << std::endl;
0361 
0362   fgets(line, 255, inpFile);
0363   m_i_run_number = atoi(line);
0364   str << "run_number= " << m_i_run_number << std::endl;
0365 
0366   fgets(line, 255, inpFile);
0367   m_i_oddweightIdMap = atoi(line);
0368   str << "weightIdMap_config= " << m_i_oddweightIdMap << std::endl;
0369 
0370   fclose(inpFile);  // close inp. file
0371 }
0372 
0373 void popcon::EcalTPGOddWeightIdMapHandler::writeFile(const char* inputFile) {
0374   //-------------------------------------------------------------
0375 
0376   std::ofstream myfile;
0377   myfile.open(inputFile);
0378   myfile << m_i_tag << std::endl;
0379   myfile << m_i_version << std::endl;
0380   myfile << m_i_run_number << std::endl;
0381   myfile << m_i_oddweightIdMap << std::endl;
0382 
0383   myfile.close();
0384 }