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/EcalTPGTPModeHandler.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 "FWCore/Framework/interface/eventsetuprecord_registration_macro.h"
0013 
0014 #include <iostream>
0015 #include <fstream>
0016 #include <map>
0017 
0018 #include <ctime>
0019 #include <unistd.h>
0020 
0021 #include <string>
0022 #include <cstdio>
0023 #include <typeinfo>
0024 #include <sstream>
0025 
0026 popcon::EcalTPGTPModeHandler::EcalTPGTPModeHandler(const edm::ParameterSet& ps)
0027     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGTPModeHandler")) {
0028   edm::LogInfo("EcalTPGTPModeHandler") << "Ecal TPG TPMode Source handler constructor.";
0029   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0030   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0031   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0032   m_user = ps.getParameter<std::string>("OnlineDBUser");
0033   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0034   m_locationsource = ps.getParameter<std::string>("LocationSource");
0035   m_location = ps.getParameter<std::string>("Location");
0036   m_gentag = ps.getParameter<std::string>("GenTag");
0037   m_runtype = ps.getParameter<std::string>("RunType");
0038   m_file_type = ps.getParameter<std::string>("fileType");  // xml/txt
0039   m_file_name = ps.getParameter<std::string>("fileName");
0040 
0041   edm::LogInfo("EcalTPGTPModeHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0042 }
0043 
0044 popcon::EcalTPGTPModeHandler::~EcalTPGTPModeHandler() {}
0045 
0046 void popcon::EcalTPGTPModeHandler::getNewObjects() {
0047   if (m_file_type == "txt") {
0048     readtxtFile();
0049   } else {
0050     edm::LogVerbatim("EcalTPGTPModeHandler") << "Started GetNewObjects!!!";
0051 
0052     //check whats already inside of database
0053     if (tagInfo().size) {
0054       //check whats already inside of database
0055       edm::LogVerbatim("EcalTPGTPModeHandler") << "got offlineInfo = ";
0056       edm::LogVerbatim("EcalTPGTPModeHandler") << "tag name = " << tagInfo().name;
0057       edm::LogVerbatim("EcalTPGTPModeHandler") << "size = " << tagInfo().size;
0058     } else {
0059       edm::LogVerbatim("EcalTPGTPModeHandler") << " First object for this tag ";
0060     }
0061 
0062     unsigned int max_since = 0;
0063     max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0064     edm::LogVerbatim("EcalTPGTPModeHandler") << "max_since : " << max_since;
0065 
0066     edm::LogVerbatim("EcalTPGTPModeHandler") << "retrieved last payload ";
0067 
0068     // here we retrieve all the runs after the last from online DB
0069 
0070     edm::LogVerbatim("EcalTPGTPModeHandler") << "Retrieving run list from ONLINE DB ... ";
0071 
0072     edm::LogVerbatim("EcalTPGTPModeHandler") << "Making connection...";
0073     auto econn = std::make_shared<EcalCondDBInterface>(m_sid, m_user, m_pass);
0074     edm::LogVerbatim("EcalTPGTPModeHandler") << "Done.";
0075 
0076     if (!econn) {
0077       throw cms::Exception("OMDS not available") << " connection parameters " << m_sid << "/" << m_user;
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_TPMode_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::LogVerbatim("EcalTPGTPModeHandler")
0106         << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since;
0107 
0108     unsigned int max_run = m_lastRun;
0109     edm::LogVerbatim("EcalTPGTPModeHandler") << "min_run= " << min_run << " max_run= " << max_run;
0110 
0111     RunList my_list;
0112     my_list = econn->fetchGlobalRunListByLocation(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 
0117     edm::LogVerbatim("EcalTPGTPModeHandler") << "number of runs is : " << num_runs;
0118 
0119     unsigned int irun = 0;
0120     if (num_runs > 0) {
0121       for (size_t kr = 0; kr < run_vec.size(); kr++) {
0122         irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0123         edm::LogVerbatim("EcalTPGTPModeHandler") << " **************** ";
0124         edm::LogVerbatim("EcalTPGTPModeHandler") << " **************** ";
0125         edm::LogVerbatim("EcalTPGTPModeHandler") << " run= " << irun;
0126 
0127         // retrieve the data :
0128         std::map<EcalLogicID, RunTPGConfigDat> dataset;
0129         econn->fetchDataSet(&dataset, &run_vec[kr]);
0130 
0131         std::string the_config_tag = "";
0132         int the_config_version = 0;
0133 
0134         std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0135 
0136         int nr = 0;
0137         for (it = dataset.begin(); it != dataset.end(); it++) {
0138           ++nr;
0139           EcalLogicID ecalid = it->first;
0140           RunTPGConfigDat dat = it->second;
0141           the_config_tag = dat.getConfigTag();
0142           the_config_version = dat.getVersion();
0143         }
0144 
0145         // it is all the same for all SM... get the last one
0146         edm::LogVerbatim("EcalTPGTPModeHandler")
0147             << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version;
0148 
0149         // here we should check if it is the same as previous run.
0150 
0151         if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0152           edm::LogVerbatim("EcalTPGTPModeHandler")
0153               << "the tag is different from last transferred run ... retrieving last config set from DB";
0154 
0155           FEConfigMainInfo fe_main_info;
0156           fe_main_info.setConfigTag(the_config_tag);
0157           fe_main_info.setVersion(the_config_version);
0158 
0159           try {
0160             edm::LogVerbatim("EcalTPGTPModeHandler") << " before fetch config set";
0161             econn->fetchConfigSet(&fe_main_info);
0162             edm::LogVerbatim("EcalTPGTPModeHandler") << " after fetch config set";
0163 
0164             // now get TPGTPMode
0165             int wId = fe_main_info.getWei2Id();
0166             if (wId != m_i_TPMode) {
0167               FEConfigOddWeightInfo fe_odd_weight_info;
0168               fe_odd_weight_info.setId(wId);
0169               econn->fetchConfigSet(&fe_odd_weight_info);
0170               std::map<EcalLogicID, FEConfigOddWeightModeDat> dataset_mode;
0171               econn->fetchDataSet(&dataset_mode, &fe_odd_weight_info);
0172 
0173               typedef std::map<EcalLogicID, FEConfigOddWeightModeDat>::const_iterator CIfem;
0174               FEConfigOddWeightModeDat rd_mode;
0175 
0176               int rd_modev[19] = {0};
0177               int k = 0;
0178               for (CIfem p = dataset_mode.begin(); p != dataset_mode.end(); p++) {
0179                 rd_mode = p->second;
0180                 rd_modev[0] = rd_mode.getEnableEBOddFilter();
0181                 rd_modev[1] = rd_mode.getEnableEEOddFilter();
0182                 rd_modev[2] = rd_mode.getEnableEBOddPeakFinder();
0183                 rd_modev[3] = rd_mode.getEnableEEOddPeakFinder();
0184                 rd_modev[4] = rd_mode.getDisableEBEvenPeakFinder();
0185                 rd_modev[5] = rd_mode.getDisableEEEvenPeakFinder();
0186                 rd_modev[6] = rd_mode.getFenixEBStripOutput();
0187                 rd_modev[7] = rd_mode.getFenixEEStripOutput();
0188                 rd_modev[8] = rd_mode.getFenixEBStripInfobit2();
0189                 rd_modev[9] = rd_mode.getFenixEEStripInfobit2();
0190                 rd_modev[10] = rd_mode.getFenixEBTcpOutput();
0191                 rd_modev[11] = rd_mode.getFenixEBTcpInfobit1();
0192                 rd_modev[12] = rd_mode.getFenixEETcpOutput();
0193                 rd_modev[13] = rd_mode.getFenixEETcpInfobit1();
0194 
0195                 edm::LogVerbatim("EcalTPGTPModeHandler") << "here is the value for the weight mode: ";
0196                 edm::LogVerbatim("EcalTPGTPModeHandler") << " EnableEBOddFilter:" << rd_modev[0];
0197                 edm::LogVerbatim("EcalTPGTPModeHandler") << " EnableEEOddFilter:" << rd_modev[1];
0198                 edm::LogVerbatim("EcalTPGTPModeHandler") << " EnableEBOddPeakFinder:" << rd_modev[2];
0199                 edm::LogVerbatim("EcalTPGTPModeHandler") << " EnableEEOddPeakFinder:" << rd_modev[3];
0200                 edm::LogVerbatim("EcalTPGTPModeHandler") << " DisableEBEvenPeakFinder:" << rd_modev[4];
0201                 edm::LogVerbatim("EcalTPGTPModeHandler") << " DisableEEEvenPeakFinder:" << rd_modev[5];
0202                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEBStripOutput:" << rd_modev[6];
0203                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEEStripOutput:" << rd_modev[7];
0204                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEBStripInfobit2:" << rd_modev[8];
0205                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEEStripInfobit2:" << rd_modev[9];
0206                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEBTcpOutput:" << rd_modev[10];
0207                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEBTcpinfobit1:" << rd_modev[11];
0208                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEETcpOutput:" << rd_modev[12];
0209                 edm::LogVerbatim("EcalTPGTPModeHandler") << " FenixEETcpinfobit1:" << rd_modev[13];
0210                 k = k + 1;
0211               }
0212 
0213               edm::LogVerbatim("EcalTPGTPModeHandler") << "*****************************************";
0214               edm::LogVerbatim("EcalTPGTPModeHandler") << "read done " << wId;
0215               edm::LogVerbatim("EcalTPGTPModeHandler") << "*****************************************";
0216 
0217               EcalTPGTPMode* tpMode = new EcalTPGTPMode;
0218               tpMode->EnableEBOddFilter = rd_modev[0];
0219               tpMode->EnableEEOddFilter = rd_modev[1];
0220               tpMode->EnableEBOddPeakFinder = rd_modev[2];
0221               tpMode->EnableEEOddPeakFinder = rd_modev[3];
0222               tpMode->DisableEBEvenPeakFinder = rd_modev[4];
0223               tpMode->DisableEEEvenPeakFinder = rd_modev[5];
0224               tpMode->FenixEBStripOutput = rd_modev[6];
0225               tpMode->FenixEEStripOutput = rd_modev[7];
0226               tpMode->FenixEBStripInfobit2 = rd_modev[8];
0227               tpMode->FenixEEStripInfobit2 = rd_modev[9];
0228               tpMode->EBFenixTcpOutput = rd_modev[10];
0229               tpMode->EBFenixTcpInfobit1 = rd_modev[11];
0230               tpMode->EEFenixTcpOutput = rd_modev[12];
0231               tpMode->EEFenixTcpInfobit1 = rd_modev[13];
0232               tpMode->FenixPar15 = 0;
0233               tpMode->FenixPar16 = 0;
0234               tpMode->FenixPar17 = 0;
0235               tpMode->FenixPar18 = 0;
0236               Time_t snc = (Time_t)irun;
0237               m_to_transfer.push_back(std::make_pair((EcalTPGTPMode*)tpMode, snc));
0238 
0239               m_i_run_number = irun;
0240               m_i_tag = the_config_tag;
0241               m_i_version = the_config_version;
0242               m_i_TPMode = wId;
0243 
0244               writeFile("last_tpg_TPMode_settings.txt");
0245 
0246             } else {
0247               m_i_run_number = irun;
0248               m_i_tag = the_config_tag;
0249               m_i_version = the_config_version;
0250 
0251               writeFile("last_tpg_TPMode_settings.txt");
0252 
0253               edm::LogVerbatim("EcalTPGTPModeHandler")
0254                   << " even if the tag/version is not the same, the weight group id is the same -> no transfer needed ";
0255             }
0256 
0257           }
0258 
0259           catch (std::exception& e) {
0260             throw cms::Exception("FileReadError") << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag
0261                                                   << " version=" << the_config_version << "\n"
0262                                                   << e.what();
0263           }
0264           edm::LogVerbatim("EcalTPGTPModeHandler") << " **************** ";
0265 
0266         } else if (nr == 0) {
0267           m_i_run_number = irun;
0268           edm::LogVerbatim("EcalTPGTPModeHandler")
0269               << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ";
0270           edm::LogVerbatim("EcalTPGTPModeHandler") << " **************** ";
0271         } else {
0272           m_i_run_number = irun;
0273           m_i_tag = the_config_tag;
0274           m_i_version = the_config_version;
0275           edm::LogVerbatim("EcalTPGTPModeHandler") << " the tag/version is the same -> no transfer needed ";
0276           edm::LogVerbatim("EcalTPGTPModeHandler") << " **************** ";
0277           writeFile("last_tpg_TPMode_settings.txt");
0278         }
0279       }
0280     }
0281 
0282   }  // usual way
0283   edm::LogVerbatim("EcalTPGTPModeHandler") << "Ecal - > end of getNewObjects -----------";
0284 }
0285 
0286 void popcon::EcalTPGTPModeHandler::readtxtFile() {
0287   edm::LogVerbatim("EcalTPGTPModeHandler") << " reading the input file " << m_file_name;
0288   std::ifstream fInput;
0289   fInput.open(m_file_name);
0290   if (!fInput.is_open()) {
0291     throw cms::Exception("FileOpenError") << "ERROR : cannot open file " << m_file_name;
0292   }
0293   std::map<std::string, int> values;
0294   EcalTPGTPMode* tpMode = new EcalTPGTPMode;
0295 
0296   std::string key;
0297   int value;
0298   while (fInput.good()) {
0299     fInput >> key >> value;
0300     values[key] = value;
0301   }
0302 
0303   try {
0304     tpMode->EnableEBOddFilter = values["EnableEBOddFilter"];
0305     tpMode->EnableEEOddFilter = values["EnableEEOddFilter"];
0306     tpMode->EnableEBOddPeakFinder = values["EnableEBOddPeakFinder"];
0307     tpMode->EnableEEOddPeakFinder = values["EnableEEOddPeakFinder"];
0308     tpMode->DisableEBEvenPeakFinder = values["DisableEBEvenPeakFinder"];
0309     tpMode->DisableEEEvenPeakFinder = values["DisableEEEvenPeakFinder"];
0310     tpMode->FenixEBStripOutput = values["FenixEBStripOutput"];
0311     tpMode->FenixEEStripOutput = values["FenixEEStripOutput"];
0312     tpMode->FenixEBStripInfobit2 = values["FenixEBStripInfobit2"];
0313     tpMode->FenixEEStripInfobit2 = values["FenixEEStripInfobit2"];
0314     tpMode->EBFenixTcpOutput = values["EBFenixTcpOutput"];
0315     tpMode->EBFenixTcpInfobit1 = values["EBFenixTcpInfobit1"];
0316     tpMode->EEFenixTcpOutput = values["EEFenixTcpOutput"];
0317     tpMode->EEFenixTcpInfobit1 = values["EEFenixTcpInfobit1"];
0318 
0319     Time_t snc = (Time_t)m_firstRun;
0320     m_to_transfer.push_back(std::make_pair((EcalTPGTPMode*)tpMode, snc));
0321 
0322   } catch (std::exception& e) {
0323     throw cms::Exception("FileReadError") << "EcalTPGTPModeHandler::readtxtFile error : " << e.what();
0324   }
0325   edm::LogVerbatim("EcalTPGTPModeHandler") << " **************** ";
0326 }
0327 
0328 void popcon::EcalTPGTPModeHandler::readFromFile(const char* inputFile) {
0329   m_i_tag = "";
0330   m_i_version = 0;
0331   m_i_run_number = 0;
0332   m_i_TPMode = 0;
0333 
0334   FILE* inpFile;  // input file
0335   inpFile = fopen(inputFile, "r");
0336   if (!inpFile) {
0337     edm::LogError("EcalTPGTPModeHandler") << "*** Can not open file: " << inputFile;
0338     return;
0339   }
0340 
0341   char line[256];
0342 
0343   std::ostringstream str;
0344 
0345   fgets(line, 255, inpFile);
0346   m_i_tag = to_string(line);
0347   str << "gen tag " << m_i_tag << std::endl;
0348 
0349   fgets(line, 255, inpFile);
0350   m_i_version = atoi(line);
0351   str << "version= " << m_i_version << std::endl;
0352 
0353   fgets(line, 255, inpFile);
0354   m_i_run_number = atoi(line);
0355   str << "run_number= " << m_i_run_number << std::endl;
0356 
0357   fgets(line, 255, inpFile);
0358   m_i_TPMode = atoi(line);
0359   str << "TPMode_config= " << m_i_TPMode << std::endl;
0360 
0361   fclose(inpFile);  // close inp. file
0362 }
0363 
0364 void popcon::EcalTPGTPModeHandler::writeFile(const char* inputFile) {
0365   std::ofstream myfile;
0366   myfile.open(inputFile);
0367   myfile << m_i_tag << std::endl;
0368   myfile << m_i_version << std::endl;
0369   myfile << m_i_run_number << std::endl;
0370   myfile << m_i_TPMode << std::endl;
0371 
0372   myfile.close();
0373 }