Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
0002 
0003 #include "CondTools/Ecal/interface/EcalTPGSlidingWindowHandler.h"
0004 
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 
0007 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0008 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
0009 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0010 #include "OnlineDB/EcalCondDB/interface/FEConfigSlidingInfo.h"
0011 #include "OnlineDB/EcalCondDB/interface/FEConfigSlidingDat.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 
0015 #include "CondFormats/EcalObjects/interface/EcalTPGSlidingWindow.h"
0016 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
0017 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
0018 
0019 #include <iostream>
0020 #include <fstream>
0021 
0022 #include <ctime>
0023 #include <unistd.h>
0024 
0025 #include <string>
0026 #include <cstdio>
0027 #include <typeinfo>
0028 #include <sstream>
0029 
0030 popcon::EcalTPGSlidingWindowHandler::EcalTPGSlidingWindowHandler(const edm::ParameterSet& ps)
0031     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGSlidingWindowHandler")) {
0032   edm::LogInfo("EcalTPGSlidingWindowHandler") << "EcalTPGSlidingWindow 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 
0043   edm::LogInfo("EcalTPGSlidingWindowHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0044 }
0045 
0046 popcon::EcalTPGSlidingWindowHandler::~EcalTPGSlidingWindowHandler() {}
0047 
0048 void popcon::EcalTPGSlidingWindowHandler::getNewObjects() {
0049   edm::LogInfo("EcalTPGSlidingWindowHandler") << "Started GetNewObjects!!!";
0050 
0051   //check whats already inside of database
0052   if (tagInfo().size) {
0053     //check whats already inside of database
0054     std::cout << "got offlineInfo = " << std::endl;
0055     std::cout << "tag name = " << tagInfo().name << std::endl;
0056     std::cout << "size = " << tagInfo().size << std::endl;
0057   } else {
0058     std::cout << " First object for this tag " << std::endl;
0059   }
0060 
0061   unsigned int max_since = 0;
0062   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0063   edm::LogInfo("EcalTPGSlidingWindowHandler") << "max_since : " << max_since;
0064   edm::LogInfo("EcalTPGSlidingWindowHandler") << "retrieved last payload ";
0065 
0066   // here we retrieve all the runs after the last from online DB
0067 
0068   edm::LogInfo("EcalTPGSlidingWindowHandler") << "Retrieving run list from ONLINE DB ... ";
0069 
0070   edm::LogInfo("EcalTPGSlidingWindowHandler") << "Making connection...";
0071   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0072   edm::LogInfo("EcalTPGSlidingWindowHandler") << "Done.";
0073 
0074   if (!econn) {
0075     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0076     throw cms::Exception("OMDS not available");
0077   }
0078 
0079   LocationDef my_locdef;
0080   my_locdef.setLocation(m_location);
0081 
0082   RunTypeDef my_rundef;
0083   my_rundef.setRunType(m_runtype);
0084 
0085   RunTag my_runtag;
0086   my_runtag.setLocationDef(my_locdef);
0087   my_runtag.setRunTypeDef(my_rundef);
0088   my_runtag.setGeneralTag(m_gentag);
0089 
0090   readFromFile("last_tpg_sliding_settings.txt");
0091 
0092   unsigned int min_run;
0093   if (m_firstRun < m_i_run_number) {
0094     min_run = m_i_run_number + 1;  // we have to add 1 to the last transferred one
0095   } else {
0096     min_run = m_firstRun;
0097   }
0098 
0099   if (min_run < max_since) {
0100     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0101   }
0102 
0103   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0104             << std::endl;
0105 
0106   unsigned int max_run = m_lastRun;
0107 
0108   edm::LogInfo("EcalTPGSlidingWindowHandler") << "min_run= " << min_run << "max_run= " << max_run;
0109 
0110   RunList my_list;
0111   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0112   //        my_list=econn->fetchRunListByLocation(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   std::cout << "number of runs is : " << num_runs << std::endl;
0118 
0119   unsigned int irun = 0;
0120   if (num_runs > 0) {
0121     // going to query the ecal logic id
0122     std::vector<EcalLogicID> my_StripEcalLogicId_EE;
0123     my_StripEcalLogicId_EE =
0124         econn->getEcalLogicIDSetOrdered("ECAL_readout_strip", 1, 1000, 1, 70, 0, 5, "EE_offline_stripid", 123);
0125     std::cout << " GOT the logic ID for the EE trigger strips " << std::endl;
0126 
0127     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0128       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0129 
0130       std::cout << " **************** " << std::endl;
0131       std::cout << " **************** " << std::endl;
0132       std::cout << " run= " << irun << std::endl;
0133 
0134       // retrieve the data :
0135       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0136       econn->fetchDataSet(&dataset, &run_vec[kr]);
0137 
0138       std::string the_config_tag = "";
0139       int the_config_version = 0;
0140 
0141       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0142       FEConfigMainInfo fe_main_info;
0143 
0144       int nr = 0;
0145       for (it = dataset.begin(); it != dataset.end(); it++) {
0146         ++nr;
0147         //EcalLogicID ecalid  = it->first;
0148         RunTPGConfigDat dat = it->second;
0149         the_config_tag = dat.getConfigTag();
0150         the_config_version = dat.getVersion();
0151       }
0152 
0153       // it is all the same for all SM... get the last one
0154 
0155       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0156 
0157       // here we should check if it is the same as previous run.
0158 
0159       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0160         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0161                   << std::endl;
0162 
0163         FEConfigMainInfo fe_main_info;
0164         fe_main_info.setConfigTag(the_config_tag);
0165         fe_main_info.setVersion(the_config_version);
0166 
0167         try {
0168           std::cout << " before fetch config set" << std::endl;
0169           econn->fetchConfigSet(&fe_main_info);
0170           std::cout << " after fetch config set" << std::endl;
0171 
0172           // now get TPGSlidingWindow
0173           int slidingId = fe_main_info.getSliId();
0174 
0175           if (slidingId != m_i_sliding) {
0176             FEConfigSlidingInfo fe_sli_info;
0177             fe_sli_info.setId(slidingId);
0178             econn->fetchConfigSet(&fe_sli_info);
0179             std::map<EcalLogicID, FEConfigSlidingDat> dataset_TpgSli;
0180             econn->fetchDataSet(&dataset_TpgSli, &fe_sli_info);
0181 
0182             EcalTPGSlidingWindow* sliW = new EcalTPGSlidingWindow;
0183             typedef std::map<EcalLogicID, FEConfigSlidingDat>::const_iterator CIfesli;
0184             EcalLogicID ecid_xt;
0185             FEConfigSlidingDat rd_sli;
0186             int icells = 0;
0187 
0188             for (CIfesli p = dataset_TpgSli.begin(); p != dataset_TpgSli.end(); p++) {
0189               ecid_xt = p->first;
0190               rd_sli = p->second;
0191 
0192               std::string ecid_name = ecid_xt.getName();
0193 
0194               // EB data
0195               if (ecid_name == "EB_VFE") {
0196                 int sm = ecid_xt.getID1();
0197                 int tt = ecid_xt.getID2();
0198                 int strip = ecid_xt.getID3();
0199                 int tcc = sm + 54;
0200                 if (sm > 18)
0201                   tcc = sm + 18;
0202 
0203                 // simple formula to calculate the Srip EB identifier
0204 
0205                 unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0206 
0207                 sliW->setValue(stripEBId, (unsigned int)rd_sli.getSliding());
0208                 ++icells;
0209 
0210               } else if (ecid_name == "ECAL_readout_strip") {
0211                 // EE data
0212                 int id1 = ecid_xt.getID1();  // dcc
0213                 int id2 = ecid_xt.getID2();  // ccu
0214                 int id3 = ecid_xt.getID3();  // strip
0215 
0216                 bool set_the_strip = false;
0217                 int stripid;
0218                 for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0219                   if (!set_the_strip) {
0220                     if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
0221                         my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
0222                         my_StripEcalLogicId_EE[istrip].getID3() == id3) {
0223                       stripid = my_StripEcalLogicId_EE[istrip].getLogicID();
0224                       set_the_strip = true;
0225                       break;
0226                     }
0227                   }
0228                 }
0229 
0230                 if (set_the_strip) {
0231                   sliW->setValue(stripid, (unsigned int)rd_sli.getSliding());
0232 
0233                 } else {
0234                   std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
0235                 }
0236 
0237                 ++icells;
0238               }
0239             }
0240 
0241             Time_t snc = (Time_t)irun;
0242             m_to_transfer.push_back(std::make_pair((EcalTPGSlidingWindow*)sliW, snc));
0243 
0244             m_i_run_number = irun;
0245             m_i_tag = the_config_tag;
0246             m_i_version = the_config_version;
0247             m_i_sliding = slidingId;
0248 
0249             writeFile("last_tpg_sliding_settings.txt");
0250 
0251           } else {
0252             m_i_run_number = irun;
0253             m_i_tag = the_config_tag;
0254             m_i_version = the_config_version;
0255 
0256             writeFile("last_tpg_sliding_settings.txt");
0257 
0258             std::cout
0259                 << " even if the tag/version is not the same, the sliding windows id is the same -> no transfer needed "
0260                 << std::endl;
0261           }
0262         } catch (std::exception& e) {
0263           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0264                     << std::endl;
0265           std::cout << e.what() << std::endl;
0266 
0267           m_i_run_number = irun;
0268         }
0269 
0270         std::cout << " **************** " << std::endl;
0271 
0272       } else if (nr == 0) {
0273         m_i_run_number = irun;
0274         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0275         std::cout << " **************** " << std::endl;
0276       } else {
0277         m_i_run_number = irun;
0278         m_i_tag = the_config_tag;
0279         m_i_version = the_config_version;
0280         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0281         std::cout << " **************** " << std::endl;
0282         writeFile("last_tpg_sliding_settings.txt");
0283       }
0284     }
0285   }
0286 
0287   delete econn;
0288 
0289   edm::LogInfo("EcalTPGSlidingWindowHandler") << "Ecal - > end of getNewObjects -----------";
0290 }
0291 
0292 void popcon::EcalTPGSlidingWindowHandler::readFromFile(const char* inputFile) {
0293   //-------------------------------------------------------------
0294 
0295   m_i_tag = "";
0296   m_i_version = 0;
0297   m_i_run_number = 0;
0298   m_i_sliding = 0;
0299 
0300   FILE* inpFile;  // input file
0301   inpFile = fopen(inputFile, "r");
0302   if (!inpFile) {
0303     edm::LogError("EcalTPGSlidingWindowHandler") << "*** Can not open file: " << inputFile;
0304     return;
0305   }
0306 
0307   char line[256];
0308 
0309   std::ostringstream str;
0310 
0311   fgets(line, 255, inpFile);
0312   m_i_tag = to_string(line);
0313   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0314 
0315   fgets(line, 255, inpFile);
0316   m_i_version = atoi(line);
0317   str << "version= " << m_i_version << std::endl;
0318 
0319   fgets(line, 255, inpFile);
0320   m_i_run_number = atoi(line);
0321   str << "run_number= " << m_i_run_number << std::endl;
0322 
0323   fgets(line, 255, inpFile);
0324   m_i_sliding = atoi(line);
0325   str << "sliding_config= " << m_i_sliding << std::endl;
0326 
0327   fclose(inpFile);  // close inp. file
0328 }
0329 
0330 void popcon::EcalTPGSlidingWindowHandler::writeFile(const char* inputFile) {
0331   //-------------------------------------------------------------
0332 
0333   std::ofstream myfile;
0334   myfile.open(inputFile);
0335   myfile << m_i_tag << std::endl;
0336   myfile << m_i_version << std::endl;
0337   myfile << m_i_run_number << std::endl;
0338   myfile << m_i_sliding << std::endl;
0339 
0340   myfile.close();
0341 }