Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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