Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 
0187             for (CIfesli p = dataset_TpgSli.begin(); p != dataset_TpgSli.end(); p++) {
0188               ecid_xt = p->first;
0189               rd_sli = p->second;
0190 
0191               std::string ecid_name = ecid_xt.getName();
0192 
0193               // EB data
0194               if (ecid_name == "EB_VFE") {
0195                 int sm = ecid_xt.getID1();
0196                 int tt = ecid_xt.getID2();
0197                 int strip = ecid_xt.getID3();
0198                 int tcc = sm + 54;
0199                 if (sm > 18)
0200                   tcc = sm + 18;
0201 
0202                 // simple formula to calculate the Srip EB identifier
0203 
0204                 unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0205 
0206                 sliW->setValue(stripEBId, (unsigned int)rd_sli.getSliding());
0207 
0208               } else if (ecid_name == "ECAL_readout_strip") {
0209                 // EE data
0210                 int id1 = ecid_xt.getID1();  // dcc
0211                 int id2 = ecid_xt.getID2();  // ccu
0212                 int id3 = ecid_xt.getID3();  // strip
0213 
0214                 bool set_the_strip = false;
0215                 int stripid;
0216                 for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0217                   if (!set_the_strip) {
0218                     if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
0219                         my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
0220                         my_StripEcalLogicId_EE[istrip].getID3() == id3) {
0221                       stripid = my_StripEcalLogicId_EE[istrip].getLogicID();
0222                       set_the_strip = true;
0223                       break;
0224                     }
0225                   }
0226                 }
0227 
0228                 if (set_the_strip) {
0229                   sliW->setValue(stripid, (unsigned int)rd_sli.getSliding());
0230 
0231                 } else {
0232                   std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
0233                 }
0234               }
0235             }
0236 
0237             Time_t snc = (Time_t)irun;
0238             m_to_transfer.push_back(std::make_pair((EcalTPGSlidingWindow*)sliW, snc));
0239 
0240             m_i_run_number = irun;
0241             m_i_tag = the_config_tag;
0242             m_i_version = the_config_version;
0243             m_i_sliding = slidingId;
0244 
0245             writeFile("last_tpg_sliding_settings.txt");
0246 
0247           } else {
0248             m_i_run_number = irun;
0249             m_i_tag = the_config_tag;
0250             m_i_version = the_config_version;
0251 
0252             writeFile("last_tpg_sliding_settings.txt");
0253 
0254             std::cout
0255                 << " even if the tag/version is not the same, the sliding windows id is the same -> no transfer needed "
0256                 << std::endl;
0257           }
0258         } catch (std::exception& e) {
0259           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0260                     << std::endl;
0261           std::cout << e.what() << std::endl;
0262 
0263           m_i_run_number = irun;
0264         }
0265 
0266         std::cout << " **************** " << std::endl;
0267 
0268       } else if (nr == 0) {
0269         m_i_run_number = irun;
0270         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0271         std::cout << " **************** " << std::endl;
0272       } else {
0273         m_i_run_number = irun;
0274         m_i_tag = the_config_tag;
0275         m_i_version = the_config_version;
0276         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0277         std::cout << " **************** " << std::endl;
0278         writeFile("last_tpg_sliding_settings.txt");
0279       }
0280     }
0281   }
0282 
0283   delete econn;
0284 
0285   edm::LogInfo("EcalTPGSlidingWindowHandler") << "Ecal - > end of getNewObjects -----------";
0286 }
0287 
0288 void popcon::EcalTPGSlidingWindowHandler::readFromFile(const char* inputFile) {
0289   //-------------------------------------------------------------
0290 
0291   m_i_tag = "";
0292   m_i_version = 0;
0293   m_i_run_number = 0;
0294   m_i_sliding = 0;
0295 
0296   FILE* inpFile;  // input file
0297   inpFile = fopen(inputFile, "r");
0298   if (!inpFile) {
0299     edm::LogError("EcalTPGSlidingWindowHandler") << "*** Can not open file: " << inputFile;
0300     return;
0301   }
0302 
0303   char line[256];
0304 
0305   std::ostringstream str;
0306 
0307   fgets(line, 255, inpFile);
0308   m_i_tag = to_string(line);
0309   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0310 
0311   fgets(line, 255, inpFile);
0312   m_i_version = atoi(line);
0313   str << "version= " << m_i_version << std::endl;
0314 
0315   fgets(line, 255, inpFile);
0316   m_i_run_number = atoi(line);
0317   str << "run_number= " << m_i_run_number << std::endl;
0318 
0319   fgets(line, 255, inpFile);
0320   m_i_sliding = atoi(line);
0321   str << "sliding_config= " << m_i_sliding << std::endl;
0322 
0323   fclose(inpFile);  // close inp. file
0324 }
0325 
0326 void popcon::EcalTPGSlidingWindowHandler::writeFile(const char* inputFile) {
0327   //-------------------------------------------------------------
0328 
0329   std::ofstream myfile;
0330   myfile.open(inputFile);
0331   myfile << m_i_tag << std::endl;
0332   myfile << m_i_version << std::endl;
0333   myfile << m_i_run_number << std::endl;
0334   myfile << m_i_sliding << std::endl;
0335 
0336   myfile.close();
0337 }