Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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