Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 
0170             for (CIfefgr p = dataset_TpgFineGrainEE.begin(); p != dataset_TpgFineGrainEE.end(); p++) {
0171               ecid_xt = p->first;
0172               rd_fgr = p->second;
0173 
0174               std::string ecid_name = ecid_xt.getName();
0175 
0176               if (ecid_name == "EE_trigger_tower") {
0177                 int tccid = ecid_xt.getID1();
0178                 // TT number
0179                 int towerid = ecid_xt.getID2();
0180 
0181                 bool set_the_tower = false;
0182                 int towid;
0183                 for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
0184                   if (!set_the_tower) {
0185                     if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
0186                         my_TTEcalLogicId_EE[itower].getID2() == towerid) {
0187                       towid = my_TTEcalLogicId_EE[itower].getLogicID();
0188                       set_the_tower = true;
0189                       break;
0190                     }
0191                   }
0192                 }
0193 
0194                 if (set_the_tower) {
0195                   fgrMap->setValue(towid, rd_fgr.getLUTValue());
0196 
0197                 } else {
0198                   std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
0199                 }
0200               }
0201             }
0202 
0203             Time_t snc = (Time_t)irun;
0204 
0205             m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainTowerEE *)fgrMap, snc));
0206 
0207             m_i_run_number = irun;
0208             m_i_tag = the_config_tag;
0209             m_i_version = the_config_version;
0210             m_i_fgrTTEE = fgrId;
0211 
0212             writeFile("last_tpg_fgrTTEE_settings.txt");
0213 
0214           } else {
0215             m_i_run_number = irun;
0216             m_i_tag = the_config_tag;
0217             m_i_version = the_config_version;
0218 
0219             writeFile("last_tpg_fgrTTEE_settings.txt");
0220 
0221             std::cout << " even if the tag/version is not the same, the fgrTTEE id is the same -> no transfer needed "
0222                       << std::endl;
0223           }
0224 
0225         } catch (std::exception &e) {
0226           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0227                     << std::endl;
0228           std::cout << e.what() << std::endl;
0229           m_i_run_number = irun;
0230         }
0231         std::cout << " **************** " << std::endl;
0232 
0233       } else if (nr == 0) {
0234         m_i_run_number = irun;
0235         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0236         std::cout << " **************** " << std::endl;
0237       } else {
0238         m_i_run_number = irun;
0239         m_i_tag = the_config_tag;
0240         m_i_version = the_config_version;
0241         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0242         std::cout << " **************** " << std::endl;
0243         writeFile("last_tpg_fgrTTEE_settings.txt");
0244       }
0245     }
0246   }
0247 
0248   delete econn;
0249   edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "Ecal - > end of getNewObjects -----------";
0250 }
0251 
0252 void popcon::EcalTPGFineGrainTowerEEHandler::readFromFile(const char *inputFile) {
0253   //-------------------------------------------------------------
0254 
0255   m_i_tag = "";
0256   m_i_version = 0;
0257   m_i_run_number = 0;
0258   m_i_fgrTTEE = 0;
0259 
0260   FILE *inpFile;  // input file
0261   inpFile = fopen(inputFile, "r");
0262   if (!inpFile) {
0263     edm::LogError("EcalTPGFineGrainTowerEEHandler") << "*** Can not open file: " << inputFile;
0264     return;
0265   }
0266 
0267   char line[256];
0268 
0269   std::ostringstream str;
0270 
0271   fgets(line, 255, inpFile);
0272   m_i_tag = to_string(line);
0273   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0274 
0275   fgets(line, 255, inpFile);
0276   m_i_version = atoi(line);
0277   str << "version= " << m_i_version << std::endl;
0278 
0279   fgets(line, 255, inpFile);
0280   m_i_run_number = atoi(line);
0281   str << "run_number= " << m_i_run_number << std::endl;
0282 
0283   fgets(line, 255, inpFile);
0284   m_i_fgrTTEE = atoi(line);
0285   str << "fgrTTEE_config= " << m_i_fgrTTEE << std::endl;
0286 
0287   fclose(inpFile);  // close inp. file
0288 }
0289 
0290 void popcon::EcalTPGFineGrainTowerEEHandler::writeFile(const char *inputFile) {
0291   //-------------------------------------------------------------
0292 
0293   std::ofstream myfile;
0294   myfile.open(inputFile);
0295   myfile << m_i_tag << std::endl;
0296   myfile << m_i_version << std::endl;
0297   myfile << m_i_run_number << std::endl;
0298   myfile << m_i_fgrTTEE << std::endl;
0299 
0300   myfile.close();
0301 }