Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 
0195               std::map<std::string, int> map;
0196               std::string str;
0197 
0198               for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
0199                 ecid_xt = p->first;
0200                 weightGroup = p->second.getWeightGroupId();
0201 
0202                 std::string ecid_name = ecid_xt.getName();
0203 
0204                 // EB data
0205                 if (ecid_name == "EB_VFE") {
0206                   int sm = ecid_xt.getID1();
0207                   int tt = ecid_xt.getID2();
0208                   int strip = ecid_xt.getID3();
0209                   int tcc = sm + 54;
0210                   if (sm > 18)
0211                     tcc = sm + 18;
0212 
0213                   // simple formula to calculate the Srip EB identifier
0214 
0215                   unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0216 
0217                   weightG->setValue(stripEBId, weightGroup);
0218                 } else if (ecid_name == "ECAL_readout_strip") {
0219                   // EE data to add
0220                   int id1 = ecid_xt.getID1();
0221                   int id2 = ecid_xt.getID2();
0222                   int id3 = ecid_xt.getID3();
0223 
0224                   bool set_the_strip = false;
0225                   int stripEEId;
0226                   for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0227                     if (!set_the_strip) {
0228                       if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
0229                           my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
0230                           my_StripEcalLogicId_EE[istrip].getID3() == id3) {
0231                         stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
0232                         set_the_strip = true;
0233                         break;
0234                       }
0235                     }
0236                   }
0237 
0238                   if (set_the_strip) {
0239                     weightG->setValue(stripEEId, weightGroup);
0240 
0241                   } else {
0242                     edm::LogInfo(" these may be the additional towers TCC/TT ") << id1 << "/" << id2;
0243                   }
0244                 }
0245               }
0246 
0247               Time_t snc = (Time_t)irun;
0248 
0249               m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
0250 
0251               m_i_run_number = irun;
0252               m_i_tag = the_config_tag;
0253               m_i_version = the_config_version;
0254               m_i_weightGroup = wId;
0255 
0256               writeFile("last_tpg_weightGroup_settings.txt");
0257 
0258             } else {
0259               m_i_run_number = irun;
0260               m_i_tag = the_config_tag;
0261               m_i_version = the_config_version;
0262 
0263               writeFile("last_tpg_weightGroup_settings.txt");
0264 
0265               edm::LogInfo(
0266                   " even if the tag/version is not the same, the weight group id is the same -> no transfer needed ");
0267             }
0268 
0269           }
0270 
0271           catch (std::exception& e) {
0272             edm::LogInfo("ERROR: THIS CONFIG DOES NOT EXIST: tag=")
0273                 << the_config_tag << " version=" << the_config_version;
0274             edm::LogInfo("Exception") << e.what();
0275             m_i_run_number = irun;
0276           }
0277           edm::LogInfo(" **************** ");
0278 
0279         } else if (nr == 0) {
0280           m_i_run_number = irun;
0281           edm::LogInfo(" no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ");
0282           edm::LogInfo(" **************** ");
0283         } else {
0284           m_i_run_number = irun;
0285           m_i_tag = the_config_tag;
0286           m_i_version = the_config_version;
0287           edm::LogInfo(" the tag/version is the same -> no transfer needed ");
0288           edm::LogInfo(" **************** ");
0289           writeFile("last_tpg_weightGroup_settings.txt");
0290         }
0291       }
0292     }
0293 
0294     delete econn;
0295   }  // usual way
0296   edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
0297 }
0298 
0299 void popcon::EcalTPGWeightGroupHandler::readtxtFile() {
0300   edm::LogInfo(" reading the input file ") << m_file_name;
0301   std::ifstream fInput;
0302   fInput.open(m_file_name);
0303   if (!fInput.is_open()) {
0304     edm::LogInfo("ERROR : cannot open file ") << m_file_name;
0305     exit(1);
0306   }
0307   int weightGroup, stripEBId, stripEEId;
0308   EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
0309   for (int strip = 0; strip < kEBStrips; strip++) {
0310     fInput >> stripEBId >> weightGroup;
0311     weightG->setValue(stripEBId, weightGroup);
0312   }
0313   for (int strip = 0; strip < kEEStrips; strip++) {
0314     fInput >> stripEEId >> weightGroup;
0315     weightG->setValue(stripEEId, weightGroup);
0316   }
0317   try {
0318     Time_t snc = (Time_t)m_firstRun;
0319     m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
0320   } catch (std::exception& e) {
0321     edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
0322   }
0323   edm::LogInfo(" **************** ");
0324 }
0325 
0326 void popcon::EcalTPGWeightGroupHandler::readxmlFile() {
0327   edm::LogInfo(" reading the input file ") << m_file_name;
0328   std::ifstream fxml;
0329   fxml.open(m_file_name);
0330   if (!fxml.is_open()) {
0331     edm::LogInfo("ERROR : cannot open file ") << m_file_name;
0332     exit(1);
0333   }
0334   std::string dummyLine, bid;
0335   int weightGroup, stripEBId, stripEEId;
0336   EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
0337   for (int i = 0; i < 6; i++)
0338     std::getline(fxml, dummyLine);  // skip first lines
0339   fxml >> bid;
0340   std::size_t found = bid.find("</");
0341   std::string stt = bid.substr(7, found - 7);
0342   for (int i = 0; i < 2; i++)
0343     std::getline(fxml, dummyLine);  //    <item_version>0</item_version>
0344   for (int strip = 0; strip < kEBStrips; strip++) {
0345     std::getline(fxml, dummyLine);  //    <item
0346     fxml >> bid;                    //    <first
0347     found = bid.find("</");
0348     stt = bid.substr(7, found - 7);
0349     std::istringstream sg1(stt);
0350     sg1 >> stripEBId;
0351     std::getline(fxml, dummyLine);
0352     fxml >> bid;  //    <second
0353     found = bid.find("</");
0354     stt = bid.substr(8, found - 8);
0355     std::istringstream sg2(stt);
0356     sg2 >> weightGroup;
0357     weightG->setValue(stripEBId, weightGroup);
0358     for (int i = 0; i < 2; i++)
0359       std::getline(fxml, dummyLine);  //    </item>
0360   }
0361   for (int strip = 0; strip < kEEStrips; strip++) {
0362     std::getline(fxml, dummyLine);  //    <item
0363     fxml >> bid;                    //    <first
0364     found = bid.find("</");
0365     stt = bid.substr(7, found - 7);
0366     std::istringstream sg1(stt);
0367     sg1 >> stripEEId;
0368     std::getline(fxml, dummyLine);
0369     fxml >> bid;  //    <second
0370     found = bid.find("</");
0371     stt = bid.substr(8, found - 8);
0372     std::istringstream sg2(stt);
0373     sg2 >> weightGroup;
0374     weightG->setValue(stripEEId, weightGroup);
0375     for (int i = 0; i < 2; i++)
0376       std::getline(fxml, dummyLine);  //    </item>
0377   }
0378   try {
0379     Time_t snc = (Time_t)m_firstRun;
0380     m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
0381   } catch (std::exception& e) {
0382     edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
0383   }
0384   edm::LogInfo(" **************** ");
0385 }
0386 
0387 void popcon::EcalTPGWeightGroupHandler::readFromFile(const char* inputFile) {
0388   //-------------------------------------------------------------
0389 
0390   m_i_tag = "";
0391   m_i_version = 0;
0392   m_i_run_number = 0;
0393   m_i_weightGroup = 0;
0394 
0395   FILE* inpFile;  // input file
0396   inpFile = fopen(inputFile, "r");
0397   if (!inpFile) {
0398     edm::LogError("EcalTPGWeightGroupHandler") << "*** Can not open file: " << inputFile;
0399     return;
0400   }
0401 
0402   char line[256];
0403 
0404   std::ostringstream str;
0405 
0406   fgets(line, 255, inpFile);
0407   m_i_tag = to_string(line);
0408   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0409 
0410   fgets(line, 255, inpFile);
0411   m_i_version = atoi(line);
0412   str << "version= " << m_i_version << std::endl;
0413 
0414   fgets(line, 255, inpFile);
0415   m_i_run_number = atoi(line);
0416   str << "run_number= " << m_i_run_number << std::endl;
0417 
0418   fgets(line, 255, inpFile);
0419   m_i_weightGroup = atoi(line);
0420   str << "weightGroup_config= " << m_i_weightGroup << std::endl;
0421 
0422   fclose(inpFile);  // close inp. file
0423 }
0424 
0425 void popcon::EcalTPGWeightGroupHandler::writeFile(const char* inputFile) {
0426   //-------------------------------------------------------------
0427 
0428   std::ofstream myfile;
0429   myfile.open(inputFile);
0430   myfile << m_i_tag << std::endl;
0431   myfile << m_i_version << std::endl;
0432   myfile << m_i_run_number << std::endl;
0433   myfile << m_i_weightGroup << std::endl;
0434 
0435   myfile.close();
0436 }