Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 
0197               std::map<std::string, int> map;
0198               std::string str;
0199 
0200               for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
0201                 ecid_xt = p->first;
0202                 weightGroup = p->second.getWeightGroupId();
0203 
0204                 std::string ecid_name = ecid_xt.getName();
0205 
0206                 // EB data
0207                 if (ecid_name == "EB_VFE") {
0208                   int sm = ecid_xt.getID1();
0209                   int tt = ecid_xt.getID2();
0210                   int strip = ecid_xt.getID3();
0211                   int tcc = sm + 54;
0212                   if (sm > 18)
0213                     tcc = sm + 18;
0214 
0215                   // simple formula to calculate the Srip EB identifier
0216 
0217                   unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0218 
0219                   weightG->setValue(stripEBId, weightGroup);
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                     std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
0245                   }
0246                 }
0247               }
0248 
0249               Time_t snc = (Time_t)irun;
0250 
0251               m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightGroup*)weightG, snc));
0252 
0253               m_i_run_number = irun;
0254               m_i_tag = the_config_tag;
0255               m_i_version = the_config_version;
0256               m_i_oddweightGroup = wId;
0257 
0258               writeFile("last_tpg_OddweightGroup_settings.txt");
0259 
0260             } else {
0261               m_i_run_number = irun;
0262               m_i_tag = the_config_tag;
0263               m_i_version = the_config_version;
0264 
0265               writeFile("last_tpg_OddweightGroup_settings.txt");
0266 
0267               std::cout
0268                   << " even if the tag/version is not the same, the weight group id is the same -> no transfer needed "
0269                   << std::endl;
0270             }
0271 
0272           }
0273 
0274           catch (std::exception& e) {
0275             std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag
0276                       << " version=" << the_config_version << std::endl;
0277             std::cout << e.what() << std::endl;
0278             m_i_run_number = irun;
0279           }
0280           std::cout << " **************** " << std::endl;
0281 
0282         } else if (nr == 0) {
0283           m_i_run_number = irun;
0284           std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0285           std::cout << " **************** " << std::endl;
0286         } else {
0287           m_i_run_number = irun;
0288           m_i_tag = the_config_tag;
0289           m_i_version = the_config_version;
0290           std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0291           std::cout << " **************** " << std::endl;
0292           writeFile("last_tpg_OddweightGroup_settings.txt");
0293         }
0294       }
0295     }
0296 
0297     delete econn;
0298   }  // usual way
0299   edm::LogInfo("EcalTPGOddWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
0300 }
0301 
0302 void popcon::EcalTPGOddWeightGroupHandler::readtxtFile() {
0303   std::cout << " reading the input file " << m_file_name << std::endl;
0304   std::ifstream fInput;
0305   fInput.open(m_file_name);
0306   if (!fInput.is_open()) {
0307     std::cout << "ERROR : cannot open file " << m_file_name << std::endl;
0308     exit(1);
0309   }
0310   int weightGroup, stripEBId, stripEEId;
0311   EcalTPGOddWeightGroup* weightG = new EcalTPGOddWeightGroup;
0312   for (int strip = 0; strip < kEBStrips; strip++) {
0313     fInput >> stripEBId >> weightGroup;
0314     weightG->setValue(stripEBId, weightGroup);
0315   }
0316   for (int strip = 0; strip < kEEStrips; strip++) {
0317     fInput >> stripEEId >> weightGroup;
0318     weightG->setValue(stripEEId, weightGroup);
0319   }
0320   try {
0321     Time_t snc = (Time_t)m_firstRun;
0322     m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightGroup*)weightG, snc));
0323   } catch (std::exception& e) {
0324     std::cout << "EcalTPGOddWeightGroupHandler::readtxtFile error : " << e.what() << std::endl;
0325   }
0326   std::cout << " **************** " << std::endl;
0327 }
0328 
0329 void popcon::EcalTPGOddWeightGroupHandler::readxmlFile() {
0330   std::cout << " reading the input file " << m_file_name << std::endl;
0331   std::ifstream fxml;
0332   fxml.open(m_file_name);
0333   if (!fxml.is_open()) {
0334     std::cout << "ERROR : cannot open file " << m_file_name << std::endl;
0335     exit(1);
0336   }
0337   std::string dummyLine, bid;
0338   int weightGroup, stripEBId, stripEEId;
0339   EcalTPGOddWeightGroup* weightG = new EcalTPGOddWeightGroup;
0340   for (int i = 0; i < 6; i++)
0341     std::getline(fxml, dummyLine);  // skip first lines
0342   fxml >> bid;
0343   //  std::cout << bid << std::endl;
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((EcalTPGOddWeightGroup*)weightG, snc));
0385   } catch (std::exception& e) {
0386     std::cout << "EcalTPGOddWeightGroupHandler::readtxtFile error : " << e.what() << std::endl;
0387   }
0388   std::cout << " **************** " << std::endl;
0389 }
0390 
0391 void popcon::EcalTPGOddWeightGroupHandler::readFromFile(const char* inputFile) {
0392   //-------------------------------------------------------------
0393 
0394   m_i_tag = "";
0395   m_i_version = 0;
0396   m_i_run_number = 0;
0397   m_i_oddweightGroup = 0;
0398 
0399   FILE* inpFile;  // input file
0400   inpFile = fopen(inputFile, "r");
0401   if (!inpFile) {
0402     edm::LogError("EcalTPGOddWeightGroupHandler") << "*** 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_oddweightGroup = atoi(line);
0424   str << "weightGroup_config= " << m_i_oddweightGroup << std::endl;
0425 
0426   fclose(inpFile);  // close inp. file
0427 }
0428 
0429 void popcon::EcalTPGOddWeightGroupHandler::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_oddweightGroup << std::endl;
0438 
0439   myfile.close();
0440 }