Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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