Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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