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/EcalTPGWeightGroupHandler.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/FEConfigSlidingInfo.h"
0008 #include "OnlineDB/EcalCondDB/interface/FEConfigSlidingDat.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0013 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
0014 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
0015 #include "FWCore/Framework/interface/eventsetuprecord_registration_macro.h"
0016 
0017 #include <iostream>
0018 #include <fstream>
0019 
0020 #include <ctime>
0021 #include <unistd.h>
0022 
0023 #include <string>
0024 #include <cstdio>
0025 #include <typeinfo>
0026 #include <sstream>
0027 
0028 const Int_t kEBStrips = 12240, kEEStrips = 2936;
0029 
0030 popcon::EcalTPGWeightGroupHandler::EcalTPGWeightGroupHandler(const edm::ParameterSet& ps)
0031     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGWeightGroupHandler")) {
0032   edm::LogInfo("EcalTPGWeightGroupHandler") << "EcalTPGWeightGroup Source handler constructor.";
0033   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0034   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0035   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0036   m_user = ps.getParameter<std::string>("OnlineDBUser");
0037   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0038   m_locationsource = ps.getParameter<std::string>("LocationSource");
0039   m_location = ps.getParameter<std::string>("Location");
0040   m_gentag = ps.getParameter<std::string>("GenTag");
0041   m_runtype = ps.getParameter<std::string>("RunType");
0042   m_file_type = ps.getParameter<std::string>("fileType");  // xml/txt
0043   m_file_name = ps.getParameter<std::string>("fileName");
0044 
0045   edm::LogInfo("EcalTPGWeightGroupHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0046 }
0047 
0048 popcon::EcalTPGWeightGroupHandler::~EcalTPGWeightGroupHandler() {}
0049 
0050 void popcon::EcalTPGWeightGroupHandler::getNewObjects() {
0051   if (m_file_type == "txt") {
0052     readtxtFile();
0053   } else if (m_file_type == "xml") {
0054     readxmlFile();
0055   } else {
0056     edm::LogInfo("EcalTPGWeightGroupHandler") << "Started GetNewObjects!!!";
0057 
0058     //check whats already inside of database
0059     if (tagInfo().size) {
0060       //check whats already inside of database
0061       edm::LogInfo(" got offlineInfo = ");
0062       edm::LogInfo(" tag name = ") << tagInfo().name;
0063       edm::LogInfo(" size = ") << tagInfo().size;
0064     } else {
0065       edm::LogInfo(" First object for this tag ");
0066     }
0067 
0068     unsigned int max_since = 0;
0069     max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0070     edm::LogInfo("EcalTPGWeightGroupHandler") << "max_since : " << max_since;
0071 
0072     edm::LogInfo("EcalTPGWeightGroupHandler") << "retrieved last payload ";
0073 
0074     // here we retrieve all the runs after the last from online DB
0075 
0076     edm::LogInfo("EcalTPGWeightGroupHandler") << "Retrieving run list from ONLINE DB ... ";
0077 
0078     edm::LogInfo("EcalTPGWeightGroupHandler") << "Making connection...";
0079     econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0080     edm::LogInfo("EcalTPGWeightGroupHandler") << "Done.";
0081 
0082     if (!econn) {
0083       edm::LogInfo(" connection parameters ") << m_sid << "/" << m_user;
0084       //        cerr << e.what();
0085       throw cms::Exception("OMDS not available");
0086     }
0087 
0088     LocationDef my_locdef;
0089     my_locdef.setLocation(m_location);
0090 
0091     RunTypeDef my_rundef;
0092     my_rundef.setRunType(m_runtype);
0093 
0094     RunTag my_runtag;
0095     my_runtag.setLocationDef(my_locdef);
0096     my_runtag.setRunTypeDef(my_rundef);
0097     my_runtag.setGeneralTag(m_gentag);
0098 
0099     readFromFile("last_tpg_weightGroup_settings.txt");
0100 
0101     unsigned int min_run;
0102 
0103     if (m_firstRun < m_i_run_number) {
0104       min_run = m_i_run_number + 1;
0105     } else {
0106       min_run = m_firstRun;
0107     }
0108 
0109     if (min_run < max_since) {
0110       min_run = max_since + 1;  // we have to add 1 to the last transferred one
0111     }
0112 
0113     edm::LogInfo("m_i_run_number") << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since;
0114 
0115     unsigned int max_run = m_lastRun;
0116     edm::LogInfo("EcalTPGWeightGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
0117 
0118     RunList my_list;
0119     my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0120     //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
0121 
0122     std::vector<RunIOV> run_vec = my_list.getRuns();
0123     size_t num_runs = run_vec.size();
0124 
0125     edm::LogInfo("number of runs is : ") << num_runs;
0126 
0127     unsigned int irun = 0;
0128     if (num_runs > 0) {
0129       // going to query the ecal logic id
0130       std::vector<EcalLogicID> my_StripEcalLogicId_EE;
0131       my_StripEcalLogicId_EE =
0132           econn->getEcalLogicIDSetOrdered("ECAL_readout_strip", 1, 2000, 1, 70, 0, 5, "EE_offline_stripid", 123);
0133 
0134       edm::LogInfo(" GOT the logic ID for the EE trigger strips ");
0135 
0136       for (size_t kr = 0; kr < run_vec.size(); kr++) {
0137         irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0138 
0139         edm::LogInfo(" **************** ");
0140         edm::LogInfo(" **************** ");
0141         edm::LogInfo(" run= ") << irun;
0142 
0143         // retrieve the data :
0144         std::map<EcalLogicID, RunTPGConfigDat> dataset;
0145         econn->fetchDataSet(&dataset, &run_vec[kr]);
0146 
0147         std::string the_config_tag = "";
0148         int the_config_version = 0;
0149 
0150         std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0151 
0152         int nr = 0;
0153         for (it = dataset.begin(); it != dataset.end(); it++) {
0154           ++nr;
0155           //EcalLogicID ecalid  = it->first;
0156 
0157           RunTPGConfigDat dat = it->second;
0158           the_config_tag = dat.getConfigTag();
0159           the_config_version = dat.getVersion();
0160         }
0161 
0162         // it is all the same for all SM... get the last one
0163 
0164         edm::LogInfo(" run= ") << irun << " tag " << the_config_tag << " version=" << the_config_version;
0165 
0166         // here we should check if it is the same as previous run.
0167 
0168         if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0169           edm::LogInfo("the tag is different from last transferred run ... retrieving last config set from DB");
0170 
0171           FEConfigMainInfo fe_main_info;
0172           fe_main_info.setConfigTag(the_config_tag);
0173           fe_main_info.setVersion(the_config_version);
0174 
0175           try {
0176             edm::LogInfo(" before fetch config set");
0177             econn->fetchConfigSet(&fe_main_info);
0178             edm::LogInfo(" after fetch config set");
0179 
0180             // now get TPGWeightGroup
0181             int wId = fe_main_info.getWeiId();
0182 
0183             if (wId != m_i_weightGroup) {
0184               FEConfigWeightInfo fe_w_info;
0185               fe_w_info.setId(wId);
0186               econn->fetchConfigSet(&fe_w_info);
0187               std::map<EcalLogicID, FEConfigWeightDat> dataset_TpgW;
0188               econn->fetchDataSet(&dataset_TpgW, &fe_w_info);
0189 
0190               EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
0191               typedef std::map<EcalLogicID, FEConfigWeightDat>::const_iterator CIfesli;
0192               EcalLogicID ecid_xt;
0193               int weightGroup;
0194               int icells = 0;
0195 
0196               std::map<std::string, int> map;
0197               std::string str;
0198 
0199               for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
0200                 ecid_xt = p->first;
0201                 weightGroup = p->second.getWeightGroupId();
0202 
0203                 std::string ecid_name = ecid_xt.getName();
0204 
0205                 // EB data
0206                 if (ecid_name == "EB_VFE") {
0207                   int sm = ecid_xt.getID1();
0208                   int tt = ecid_xt.getID2();
0209                   int strip = ecid_xt.getID3();
0210                   int tcc = sm + 54;
0211                   if (sm > 18)
0212                     tcc = sm + 18;
0213 
0214                   // simple formula to calculate the Srip EB identifier
0215 
0216                   unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0217 
0218                   weightG->setValue(stripEBId, weightGroup);
0219                   ++icells;
0220                 } else if (ecid_name == "ECAL_readout_strip") {
0221                   // EE data to add
0222                   int id1 = ecid_xt.getID1();
0223                   int id2 = ecid_xt.getID2();
0224                   int id3 = ecid_xt.getID3();
0225 
0226                   bool set_the_strip = false;
0227                   int stripEEId;
0228                   for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0229                     if (!set_the_strip) {
0230                       if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
0231                           my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
0232                           my_StripEcalLogicId_EE[istrip].getID3() == id3) {
0233                         stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
0234                         set_the_strip = true;
0235                         break;
0236                       }
0237                     }
0238                   }
0239 
0240                   if (set_the_strip) {
0241                     weightG->setValue(stripEEId, weightGroup);
0242 
0243                   } else {
0244                     edm::LogInfo(" these may be the additional towers TCC/TT ") << id1 << "/" << id2;
0245                   }
0246 
0247                   ++icells;
0248                 }
0249               }
0250 
0251               Time_t snc = (Time_t)irun;
0252 
0253               m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
0254 
0255               m_i_run_number = irun;
0256               m_i_tag = the_config_tag;
0257               m_i_version = the_config_version;
0258               m_i_weightGroup = wId;
0259 
0260               writeFile("last_tpg_weightGroup_settings.txt");
0261 
0262             } else {
0263               m_i_run_number = irun;
0264               m_i_tag = the_config_tag;
0265               m_i_version = the_config_version;
0266 
0267               writeFile("last_tpg_weightGroup_settings.txt");
0268 
0269               edm::LogInfo(
0270                   " even if the tag/version is not the same, the weight group id is the same -> no transfer needed ");
0271             }
0272 
0273           }
0274 
0275           catch (std::exception& e) {
0276             edm::LogInfo("ERROR: THIS CONFIG DOES NOT EXIST: tag=")
0277                 << the_config_tag << " version=" << the_config_version;
0278             edm::LogInfo("Exception") << e.what();
0279             m_i_run_number = irun;
0280           }
0281           edm::LogInfo(" **************** ");
0282 
0283         } else if (nr == 0) {
0284           m_i_run_number = irun;
0285           edm::LogInfo(" no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ");
0286           edm::LogInfo(" **************** ");
0287         } else {
0288           m_i_run_number = irun;
0289           m_i_tag = the_config_tag;
0290           m_i_version = the_config_version;
0291           edm::LogInfo(" the tag/version is the same -> no transfer needed ");
0292           edm::LogInfo(" **************** ");
0293           writeFile("last_tpg_weightGroup_settings.txt");
0294         }
0295       }
0296     }
0297 
0298     delete econn;
0299   }  // usual way
0300   edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
0301 }
0302 
0303 void popcon::EcalTPGWeightGroupHandler::readtxtFile() {
0304   edm::LogInfo(" reading the input file ") << m_file_name;
0305   std::ifstream fInput;
0306   fInput.open(m_file_name);
0307   if (!fInput.is_open()) {
0308     edm::LogInfo("ERROR : cannot open file ") << m_file_name;
0309     exit(1);
0310   }
0311   int weightGroup, stripEBId, stripEEId;
0312   EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
0313   for (int strip = 0; strip < kEBStrips; strip++) {
0314     fInput >> stripEBId >> weightGroup;
0315     weightG->setValue(stripEBId, weightGroup);
0316   }
0317   for (int strip = 0; strip < kEEStrips; strip++) {
0318     fInput >> stripEEId >> weightGroup;
0319     weightG->setValue(stripEEId, weightGroup);
0320   }
0321   try {
0322     Time_t snc = (Time_t)m_firstRun;
0323     m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
0324   } catch (std::exception& e) {
0325     edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
0326   }
0327   edm::LogInfo(" **************** ");
0328 }
0329 
0330 void popcon::EcalTPGWeightGroupHandler::readxmlFile() {
0331   edm::LogInfo(" reading the input file ") << m_file_name;
0332   std::ifstream fxml;
0333   fxml.open(m_file_name);
0334   if (!fxml.is_open()) {
0335     edm::LogInfo("ERROR : cannot open file ") << m_file_name;
0336     exit(1);
0337   }
0338   std::string dummyLine, bid;
0339   int weightGroup, stripEBId, stripEEId;
0340   EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
0341   for (int i = 0; i < 6; i++)
0342     std::getline(fxml, dummyLine);  // skip first lines
0343   fxml >> bid;
0344   std::size_t found = bid.find("</");
0345   std::string stt = bid.substr(7, found - 7);
0346   for (int i = 0; i < 2; i++)
0347     std::getline(fxml, dummyLine);  //    <item_version>0</item_version>
0348   for (int strip = 0; strip < kEBStrips; strip++) {
0349     std::getline(fxml, dummyLine);  //    <item
0350     fxml >> bid;                    //    <first
0351     found = bid.find("</");
0352     stt = bid.substr(7, found - 7);
0353     std::istringstream sg1(stt);
0354     sg1 >> stripEBId;
0355     std::getline(fxml, dummyLine);
0356     fxml >> bid;  //    <second
0357     found = bid.find("</");
0358     stt = bid.substr(8, found - 8);
0359     std::istringstream sg2(stt);
0360     sg2 >> weightGroup;
0361     weightG->setValue(stripEBId, weightGroup);
0362     for (int i = 0; i < 2; i++)
0363       std::getline(fxml, dummyLine);  //    </item>
0364   }
0365   for (int strip = 0; strip < kEEStrips; strip++) {
0366     std::getline(fxml, dummyLine);  //    <item
0367     fxml >> bid;                    //    <first
0368     found = bid.find("</");
0369     stt = bid.substr(7, found - 7);
0370     std::istringstream sg1(stt);
0371     sg1 >> stripEEId;
0372     std::getline(fxml, dummyLine);
0373     fxml >> bid;  //    <second
0374     found = bid.find("</");
0375     stt = bid.substr(8, found - 8);
0376     std::istringstream sg2(stt);
0377     sg2 >> weightGroup;
0378     weightG->setValue(stripEEId, weightGroup);
0379     for (int i = 0; i < 2; i++)
0380       std::getline(fxml, dummyLine);  //    </item>
0381   }
0382   try {
0383     Time_t snc = (Time_t)m_firstRun;
0384     m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
0385   } catch (std::exception& e) {
0386     edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
0387   }
0388   edm::LogInfo(" **************** ");
0389 }
0390 
0391 void popcon::EcalTPGWeightGroupHandler::readFromFile(const char* inputFile) {
0392   //-------------------------------------------------------------
0393 
0394   m_i_tag = "";
0395   m_i_version = 0;
0396   m_i_run_number = 0;
0397   m_i_weightGroup = 0;
0398 
0399   FILE* inpFile;  // input file
0400   inpFile = fopen(inputFile, "r");
0401   if (!inpFile) {
0402     edm::LogError("EcalTPGWeightGroupHandler") << "*** Can not open file: " << inputFile;
0403     return;
0404   }
0405 
0406   char line[256];
0407 
0408   std::ostringstream str;
0409 
0410   fgets(line, 255, inpFile);
0411   m_i_tag = to_string(line);
0412   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0413 
0414   fgets(line, 255, inpFile);
0415   m_i_version = atoi(line);
0416   str << "version= " << m_i_version << std::endl;
0417 
0418   fgets(line, 255, inpFile);
0419   m_i_run_number = atoi(line);
0420   str << "run_number= " << m_i_run_number << std::endl;
0421 
0422   fgets(line, 255, inpFile);
0423   m_i_weightGroup = atoi(line);
0424   str << "weightGroup_config= " << m_i_weightGroup << std::endl;
0425 
0426   fclose(inpFile);  // close inp. file
0427 }
0428 
0429 void popcon::EcalTPGWeightGroupHandler::writeFile(const char* inputFile) {
0430   //-------------------------------------------------------------
0431 
0432   std::ofstream myfile;
0433   myfile.open(inputFile);
0434   myfile << m_i_tag << std::endl;
0435   myfile << m_i_version << std::endl;
0436   myfile << m_i_run_number << std::endl;
0437   myfile << m_i_weightGroup << std::endl;
0438 
0439   myfile.close();
0440 }