Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGPedestalsHandler.h"
0002 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0003 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
0004 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTInfo.h"
0006 #include "OnlineDB/EcalCondDB/interface/RunList.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 #include "CondFormats/EcalObjects/interface/EcalTPGPedestals.h"
0011 
0012 #include <iostream>
0013 #include <fstream>
0014 
0015 #include <ctime>
0016 #include <unistd.h>
0017 
0018 #include <string>
0019 #include <cstdio>
0020 #include <typeinfo>
0021 #include <sstream>
0022 
0023 popcon::EcalTPGPedestalsHandler::EcalTPGPedestalsHandler(const edm::ParameterSet& ps)
0024     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGPedestalsHandler")) {
0025   edm::LogInfo("EcalTPGPedestalsHandler") << "EcalTPGPedestals Source handler constructor";
0026   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0027   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0028   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0029   m_user = ps.getParameter<std::string>("OnlineDBUser");
0030   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0031   m_locationsource = ps.getParameter<std::string>("LocationSource");
0032   m_location = ps.getParameter<std::string>("Location");
0033   m_gentag = ps.getParameter<std::string>("GenTag");
0034   m_runtype = ps.getParameter<std::string>("RunType");
0035 
0036   edm::LogInfo("EcalTPGPedestalsHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0037 }
0038 
0039 popcon::EcalTPGPedestalsHandler::~EcalTPGPedestalsHandler() {}
0040 
0041 void popcon::EcalTPGPedestalsHandler::getNewObjects() {
0042   edm::LogInfo("EcalTPGPedestalsHandler") << "Started getNewObjects";
0043 
0044   //check whats already inside of database
0045   if (tagInfo().size) {
0046     //check whats already inside of database
0047     std::cout << "got offlineInfo = " << std::endl;
0048     std::cout << "tag name = " << tagInfo().name << std::endl;
0049     std::cout << "size = " << tagInfo().size << std::endl;
0050   } else {
0051     std::cout << " First object for this tag " << std::endl;
0052   }
0053 
0054   unsigned int max_since = 0;
0055   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0056   edm::LogInfo("EcalTPGPedestalsHandler") << "max_since = " << max_since;
0057   edm::LogInfo("EcalTPGPedestalsHandler") << "Retrieved last payload ";
0058 
0059   // here we retrieve all the runs after the last from online DB
0060   edm::LogInfo("EcalTPGPedestalsHandler") << "Retrieving run list from ONLINE DB ... " << std::endl;
0061 
0062   edm::LogInfo("EcalTPGPedestalsHandler") << "Making connection..." << std::flush;
0063   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0064   edm::LogInfo("EcalTPGPedestalsHandler") << "Done." << std::endl;
0065 
0066   if (!econn) {
0067     std::cout << " Connection parameters " << m_sid << "/" << m_user << std::endl;
0068     throw cms::Exception("OMDS not available");
0069   }
0070 
0071   LocationDef my_locdef;
0072   my_locdef.setLocation(m_location);
0073 
0074   RunTypeDef my_rundef;
0075   my_rundef.setRunType(m_runtype);
0076 
0077   RunTag my_runtag;
0078   my_runtag.setLocationDef(my_locdef);
0079   my_runtag.setRunTypeDef(my_rundef);
0080   my_runtag.setGeneralTag(m_gentag);
0081 
0082   readFromFile("last_tpg_ped_settings.txt");
0083 
0084   unsigned int min_run;
0085 
0086   if (m_firstRun < m_i_run_number) {
0087     min_run = m_i_run_number + 1;
0088   } else {
0089     min_run = m_firstRun;
0090   }
0091   if (min_run < max_since) {
0092     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0093   }
0094 
0095   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0096             << std::endl;
0097 
0098   unsigned int max_run = m_lastRun;
0099   edm::LogInfo("EcalTPGPedestalsHandler") << "min_run= " << min_run << " max_run = " << max_run;
0100   RunList my_list;
0101   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0102   //    my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0103 
0104   std::vector<RunIOV> run_vec = my_list.getRuns();
0105   size_t num_runs = run_vec.size();
0106 
0107   std::cout << "number of runs is : " << num_runs << std::endl;
0108 
0109   unsigned int irun = 0;
0110 
0111   if (num_runs > 0) {
0112     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0113       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0114 
0115       std::cout << " **************** " << std::endl;
0116       std::cout << " **************** " << std::endl;
0117       std::cout << " run= " << irun << std::endl;
0118 
0119       // retrieve the data :
0120       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0121       econn->fetchDataSet(&dataset, &run_vec[kr]);
0122 
0123       std::string the_config_tag = "";
0124       int the_config_version = 0;
0125 
0126       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0127 
0128       int nr = 0;
0129       for (it = dataset.begin(); it != dataset.end(); it++) {
0130         ++nr;
0131         //EcalLogicID ecalid  = it->first;
0132 
0133         RunTPGConfigDat dat = it->second;
0134         the_config_tag = dat.getConfigTag();
0135         the_config_version = dat.getVersion();
0136       }
0137 
0138       // it is all the same for all SM... get the last one
0139 
0140       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0141 
0142       // here we should check if it is the same as previous run.
0143 
0144       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0145         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0146                   << std::endl;
0147 
0148         FEConfigMainInfo fe_main_info;
0149         fe_main_info.setConfigTag(the_config_tag);
0150         fe_main_info.setVersion(the_config_version);
0151 
0152         try {
0153           std::cout << " before fetch config set" << std::endl;
0154           econn->fetchConfigSet(&fe_main_info);
0155           std::cout << " after fetch config set" << std::endl;
0156 
0157           // now get TPGPedestals
0158           int pedId = fe_main_info.getPedId();
0159 
0160           if (pedId != m_i_ped) {
0161             FEConfigPedInfo fe_ped_info;
0162             fe_ped_info.setId(pedId);
0163             econn->fetchConfigSet(&fe_ped_info);
0164             std::map<EcalLogicID, FEConfigPedDat> dataset_TpgPed;
0165             econn->fetchDataSet(&dataset_TpgPed, &fe_ped_info);
0166 
0167             // NB new
0168             EcalTPGPedestals* peds = new EcalTPGPedestals;
0169             typedef std::map<EcalLogicID, FEConfigPedDat>::const_iterator CIfeped;
0170             EcalLogicID ecid_xt;
0171             FEConfigPedDat rd_ped;
0172             int icells = 0;
0173             for (CIfeped p = dataset_TpgPed.begin(); p != dataset_TpgPed.end(); p++) {
0174               ecid_xt = p->first;
0175               rd_ped = p->second;
0176 
0177               std::string ecid_name = ecid_xt.getName();
0178 
0179               // EB data
0180               if (ecid_name == "EB_crystal_number") {
0181                 if (icells < 10)
0182                   std::cout << " copy the EB data "
0183                             << " icells = " << icells << std::endl;
0184                 int sm_num = ecid_xt.getID1();
0185                 int xt_num = ecid_xt.getID2();
0186 
0187                 EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
0188                 EcalTPGPedestals::Item item;
0189                 item.mean_x1 = (unsigned int)rd_ped.getPedMeanG1();
0190                 item.mean_x6 = (unsigned int)rd_ped.getPedMeanG6();
0191                 item.mean_x12 = (unsigned int)rd_ped.getPedMeanG12();
0192 
0193                 peds->insert(std::make_pair(ebdetid.rawId(), item));
0194                 ++icells;
0195               } else if (ecid_name == "EE_crystal_number") {
0196                 // EE data
0197                 int z = ecid_xt.getID1();
0198                 int x = ecid_xt.getID2();
0199                 int y = ecid_xt.getID3();
0200                 EEDetId eedetid(x, y, z, EEDetId::XYMODE);
0201                 EcalTPGPedestals::Item item;
0202                 item.mean_x1 = (unsigned int)rd_ped.getPedMeanG1();
0203                 item.mean_x6 = (unsigned int)rd_ped.getPedMeanG6();
0204                 item.mean_x12 = (unsigned int)rd_ped.getPedMeanG12();
0205 
0206                 peds->insert(std::make_pair(eedetid.rawId(), item));
0207                 ++icells;
0208               }
0209             }
0210 
0211             Time_t snc = (Time_t)irun;
0212             m_to_transfer.push_back(std::make_pair((EcalTPGPedestals*)peds, snc));
0213 
0214             m_i_run_number = irun;
0215             m_i_tag = the_config_tag;
0216             m_i_version = the_config_version;
0217             m_i_ped = pedId;
0218 
0219             writeFile("last_tpg_ped_settings.txt");
0220 
0221           } else {
0222             m_i_run_number = irun;
0223             m_i_tag = the_config_tag;
0224             m_i_version = the_config_version;
0225 
0226             writeFile("last_tpg_ped_settings.txt");
0227 
0228             std::cout << " even if the tag/version is not the same, the pedestals id is the same -> no transfer needed "
0229                       << std::endl;
0230           }
0231 
0232         }
0233 
0234         catch (std::exception& e) {
0235           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0236                     << std::endl;
0237           std::cout << e.what() << std::endl;
0238           m_i_run_number = irun;
0239         }
0240         std::cout << " **************** " << std::endl;
0241 
0242       } else if (nr == 0) {
0243         m_i_run_number = irun;
0244         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0245         std::cout << " **************** " << std::endl;
0246       } else {
0247         m_i_run_number = irun;
0248         m_i_tag = the_config_tag;
0249         m_i_version = the_config_version;
0250         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0251         std::cout << " **************** " << std::endl;
0252         writeFile("last_tpg_ped_settings.txt");
0253       }
0254     }
0255   }
0256 
0257   delete econn;
0258 
0259   edm::LogInfo("EcalTPGPedestalsHandler") << "Ecal - > end of getNewObjects -----------";
0260 }
0261 
0262 void popcon::EcalTPGPedestalsHandler::readFromFile(const char* inputFile) {
0263   //-------------------------------------------------------------
0264 
0265   m_i_tag = "";
0266   m_i_version = 0;
0267   m_i_run_number = 0;
0268   m_i_ped = 0;
0269 
0270   FILE* inpFile;  // input file
0271   inpFile = fopen(inputFile, "r");
0272   if (!inpFile) {
0273     edm::LogError("EcalTPGPedestalsHandler") << "*** Can not open file: " << inputFile;
0274     return;
0275   }
0276 
0277   char line[256];
0278 
0279   std::ostringstream str;
0280 
0281   fgets(line, 255, inpFile);
0282   m_i_tag = to_string(line);
0283   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0284 
0285   fgets(line, 255, inpFile);
0286   m_i_version = atoi(line);
0287   str << "version= " << m_i_version << std::endl;
0288 
0289   fgets(line, 255, inpFile);
0290   m_i_run_number = atoi(line);
0291   str << "run_number= " << m_i_run_number << std::endl;
0292 
0293   fgets(line, 255, inpFile);
0294   m_i_ped = atoi(line);
0295   str << "ped_config= " << m_i_ped << std::endl;
0296 
0297   fclose(inpFile);  // close inp. file
0298 }
0299 
0300 void popcon::EcalTPGPedestalsHandler::writeFile(const char* inputFile) {
0301   //-------------------------------------------------------------
0302 
0303   std::ofstream myfile;
0304   myfile.open(inputFile);
0305   myfile << m_i_tag << std::endl;
0306   myfile << m_i_version << std::endl;
0307   myfile << m_i_run_number << std::endl;
0308   myfile << m_i_ped << std::endl;
0309 
0310   myfile.close();
0311 }