Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-05-05 02:47:16

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 
0175             for (CIfefgr p = dataset_TpgFineGrainEB.begin(); p != dataset_TpgFineGrainEB.end(); p++) {
0176               ecid_xt = p->first;
0177               rd_fgr = p->second;
0178 
0179               std::string ecid_name = ecid_xt.getName();
0180 
0181               if (ecid_name == "EB_trigger_tower") {
0182                 // SM number
0183                 int smid = ecid_xt.getID1();
0184                 // TT number
0185                 int towerid = ecid_xt.getID2();
0186 
0187                 /*                
0188             char identTT[10];
0189             sprintf(identTT,"%d%d", smid, towerid);
0190             
0191             std::string S="";
0192             S.insert(0,identTT);
0193         
0194             unsigned int towerEBId = 0;
0195             towerEBId = atoi(S.c_str());
0196 
0197             */
0198 
0199                 int tow_eta = (towerid - 1) / 4;
0200                 int tow_phi = ((towerid - 1) - tow_eta * 4);
0201 
0202                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0203 
0204                 EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
0205                 const EcalTrigTowerDetId towid = id.tower();
0206 
0207                 fgrMap->setValue(towid.rawId(), rd_fgr.getFgrGroupId());
0208               }
0209             }
0210 
0211             Time_t snc = (Time_t)irun;
0212 
0213             m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainEBGroup *)fgrMap, snc));
0214 
0215             m_i_run_number = irun;
0216             m_i_tag = the_config_tag;
0217             m_i_version = the_config_version;
0218             m_i_fgrGroup = fgrId;
0219 
0220             writeFile("last_tpg_fgrGroup_settings.txt");
0221 
0222           } else {
0223             m_i_run_number = irun;
0224             m_i_tag = the_config_tag;
0225             m_i_version = the_config_version;
0226 
0227             writeFile("last_tpg_fgrGroup_settings.txt");
0228 
0229             std::cout << " even if the tag/version is not the same, the fgrGroup id is the same -> no transfer needed "
0230                       << std::endl;
0231           }
0232 
0233         }
0234 
0235         catch (std::exception &e) {
0236           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0237                     << std::endl;
0238           std::cout << e.what() << std::endl;
0239           m_i_run_number = irun;
0240         }
0241         std::cout << " **************** " << std::endl;
0242 
0243       } else if (nr == 0) {
0244         m_i_run_number = irun;
0245         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0246         std::cout << " **************** " << std::endl;
0247       } else {
0248         m_i_run_number = irun;
0249         m_i_tag = the_config_tag;
0250         m_i_version = the_config_version;
0251         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0252         std::cout << " **************** " << std::endl;
0253         writeFile("last_tpg_fgrGroup_settings.txt");
0254       }
0255     }
0256   }
0257 
0258   delete econn;
0259   edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "Ecal - > end of getNewObjects -----------";
0260 }
0261 
0262 void popcon::EcalTPGFineGrainEBGroupHandler::readFromFile(const char *inputFile) {
0263   //-------------------------------------------------------------
0264 
0265   m_i_tag = "";
0266   m_i_version = 0;
0267   m_i_run_number = 0;
0268   m_i_fgrGroup = 0;
0269 
0270   FILE *inpFile;  // input file
0271   inpFile = fopen(inputFile, "r");
0272   if (!inpFile) {
0273     edm::LogError("EcalTPGFineGrainEBGroupHandler") << "*** 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_fgrGroup = atoi(line);
0295   str << "fgrGroup_config= " << m_i_fgrGroup << std::endl;
0296 
0297   fclose(inpFile);  // close inp. file
0298 }
0299 
0300 void popcon::EcalTPGFineGrainEBGroupHandler::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_fgrGroup << std::endl;
0309 
0310   myfile.close();
0311 }