Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGPhysicsConstHandler.h"
0002 
0003 #include "DataFormats/DetId/interface/DetId.h"
0004 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0005 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0007 #include "OnlineDB/EcalCondDB/interface/FEConfigParamDat.h"
0008 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTInfo.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include <iostream>
0013 
0014 popcon::EcalTPGPhysicsConstHandler::EcalTPGPhysicsConstHandler(const edm::ParameterSet& ps)
0015     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGPhysicsConstHandler")) {
0016   edm::LogInfo("EcalTPGPhysicsConstHandler") << "EcalTPGPhysicsConst Source handler constructor.";
0017   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0018   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0019   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0020   m_user = ps.getParameter<std::string>("OnlineDBUser");
0021   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0022   m_locationsource = ps.getParameter<std::string>("LocationSource");
0023   m_location = ps.getParameter<std::string>("Location");
0024   m_gentag = ps.getParameter<std::string>("GenTag");
0025   m_runtype = ps.getParameter<std::string>("RunType");
0026 
0027   edm::LogInfo("EcalTPGPhysicsConstHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0028 }
0029 
0030 popcon::EcalTPGPhysicsConstHandler::~EcalTPGPhysicsConstHandler() {}
0031 
0032 void popcon::EcalTPGPhysicsConstHandler::getNewObjects() {
0033   edm::LogInfo("EcalTPGPhysicsConstHandler") << "Started GetNewObjects!!!";
0034 
0035   //check whats already inside of database
0036   if (tagInfo().size) {
0037     //check whats already inside of database
0038     std::cout << "got offlineInfo = " << std::endl;
0039     std::cout << "tag name = " << tagInfo().name << std::endl;
0040     std::cout << "size = " << tagInfo().size << std::endl;
0041   } else {
0042     std::cout << " First object for this tag " << std::endl;
0043   }
0044 
0045   unsigned int max_since = 0;
0046   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0047   edm::LogInfo("EcalTPGPhysicsConstHandler") << "max_since : " << max_since;
0048   Ref physC_db = lastPayload();
0049 
0050   edm::LogInfo("EcalTPGPhysicsConstHandler") << "retrieved last payload ";
0051 
0052   // here we retrieve all the runs after the last from online DB
0053   edm::LogInfo("EcalTPGPhysicsConstHandler") << "Retrieving run list from ONLINE DB ... ";
0054 
0055   edm::LogInfo("EcalTPGPhysicsConstHandler") << "Making connection...";
0056   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0057   edm::LogInfo("EcalTPGPhysicsConstHandler") << "Done.";
0058 
0059   if (!econn) {
0060     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0061     //      cerr << e.what() << std::endl;
0062     throw cms::Exception("OMDS not available");
0063   }
0064 
0065   LocationDef my_locdef;
0066   my_locdef.setLocation(m_location);
0067 
0068   RunTypeDef my_rundef;
0069   my_rundef.setRunType(m_runtype);
0070 
0071   RunTag my_runtag;
0072   my_runtag.setLocationDef(my_locdef);
0073   my_runtag.setRunTypeDef(my_rundef);
0074   my_runtag.setGeneralTag(m_gentag);
0075 
0076   readFromFile("last_tpg_physC_settings.txt");
0077 
0078   unsigned int min_run;
0079 
0080   if (m_firstRun < m_i_run_number) {
0081     min_run = m_i_run_number + 1;
0082   } else {
0083     min_run = m_firstRun;
0084   }
0085 
0086   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0087             << std::endl;
0088 
0089   if (min_run < max_since) {
0090     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0091   }
0092 
0093   unsigned int max_run = m_lastRun;
0094   edm::LogInfo("EcalTPGPhysicsConstHandler") << "min_run= " << min_run << "max_run= " << max_run;
0095 
0096   RunList my_list;
0097   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0098   //    my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
0099 
0100   std::vector<RunIOV> run_vec = my_list.getRuns();
0101   size_t num_runs = run_vec.size();
0102 
0103   std::cout << "number of runs is : " << num_runs << std::endl;
0104 
0105   unsigned int irun;
0106   if (num_runs > 0) {
0107     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0108       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0109 
0110       std::cout << " **************** " << std::endl;
0111       std::cout << " **************** " << std::endl;
0112       std::cout << " run= " << irun << std::endl;
0113 
0114       // retrieve the data :
0115       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0116       econn->fetchDataSet(&dataset, &run_vec[kr]);
0117 
0118       std::string the_config_tag = "";
0119       int the_config_version = 0;
0120 
0121       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0122 
0123       int nr = 0;
0124       for (it = dataset.begin(); it != dataset.end(); it++) {
0125         ++nr;
0126         //EcalLogicID ecalid  = it->first;
0127         RunTPGConfigDat dat = it->second;
0128         the_config_tag = dat.getConfigTag();
0129         the_config_version = dat.getVersion();
0130       }
0131 
0132       // it is all the same for all SM... get the last one
0133 
0134       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0135 
0136       // here we should check if it is the same as previous run.
0137 
0138       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0139         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0140                   << std::endl;
0141 
0142         FEConfigMainInfo fe_main_info;
0143         fe_main_info.setConfigTag(the_config_tag);
0144         fe_main_info.setVersion(the_config_version);
0145 
0146         try {
0147           std::cout << " before fetch config set" << std::endl;
0148           econn->fetchConfigSet(&fe_main_info);
0149           std::cout << " after fetch config set" << std::endl;
0150 
0151           // now get TPGPhysicsConst
0152           int linId = fe_main_info.getLinId();
0153           int fgrId = fe_main_info.getFgrId();
0154           int lutId = fe_main_info.getLUTId();
0155 
0156           if ((linId != m_i_physClin) || (fgrId != m_i_physCfgr) || (lutId != m_i_physClut)) {
0157             std::cout << "one of the parameters: linId, LutId or fgrId is different from" << std::endl;
0158             std::cout << "last transferred run ..." << std::endl;
0159 
0160             FEConfigLinInfo fe_physLin_info;
0161             FEConfigFgrInfo fe_physFgr_info;
0162             FEConfigLUTInfo fe_physLut_info;
0163             fe_physLin_info.setId(linId);
0164             fe_physFgr_info.setId(fgrId);
0165             fe_physLut_info.setId(lutId);
0166 
0167             econn->fetchConfigSet(&fe_physLin_info);
0168             econn->fetchConfigSet(&fe_physFgr_info);
0169             econn->fetchConfigSet(&fe_physLut_info);
0170             std::map<EcalLogicID, FEConfigLinParamDat> dataset_TpgPhysicsLin;
0171             std::map<EcalLogicID, FEConfigLUTParamDat> dataset_TpgPhysicsLut;
0172             std::map<EcalLogicID, FEConfigFgrParamDat> dataset_TpgPhysicsFgr;
0173 
0174             econn->fetchDataSet(&dataset_TpgPhysicsLin, &fe_physLin_info);
0175             econn->fetchDataSet(&dataset_TpgPhysicsLut, &fe_physLut_info);
0176             econn->fetchDataSet(&dataset_TpgPhysicsFgr, &fe_physFgr_info);
0177 
0178             EcalTPGPhysicsConst* physC = new EcalTPGPhysicsConst;
0179             typedef std::map<EcalLogicID, FEConfigLinParamDat>::const_iterator CIfeLin;
0180             typedef std::map<EcalLogicID, FEConfigLUTParamDat>::const_iterator CIfeLUT;
0181             typedef std::map<EcalLogicID, FEConfigFgrParamDat>::const_iterator CIfeFgr;
0182 
0183             EcalLogicID ecidLin_xt;
0184             EcalLogicID ecidLut_xt;
0185             EcalLogicID ecidFgr_xt;
0186             FEConfigLinParamDat rd_physLin;
0187             FEConfigLUTParamDat rd_physLut;
0188             FEConfigFgrParamDat rd_physFgr;
0189 
0190             std::map<int, float> EtSatLinEB;
0191             std::map<int, float> EtSatLinEE;
0192             typedef std::map<int, float>::const_iterator itEtSat;
0193 
0194             std::map<int, EcalTPGPhysicsConst::Item> temporaryMapEB;
0195             std::map<int, EcalTPGPhysicsConst::Item> temporaryMapEE;
0196             typedef std::map<int, EcalTPGPhysicsConst::Item>::iterator iterEB;
0197             typedef std::map<int, EcalTPGPhysicsConst::Item>::iterator iterEE;
0198 
0199             for (CIfeLin p0 = dataset_TpgPhysicsLin.begin(); p0 != dataset_TpgPhysicsLin.end(); p0++) {
0200               ecidLin_xt = p0->first;
0201               rd_physLin = p0->second;
0202 
0203               std::string ecid_nameLin = ecidLin_xt.getName();
0204 
0205               if (ecid_nameLin == "EB") {
0206                 DetId eb(DetId::Ecal, EcalBarrel);
0207                 EtSatLinEB.insert(std::make_pair(eb.rawId(), rd_physLin.getETSat()));
0208               } else if (ecid_nameLin == "EE") {
0209                 DetId ee(DetId::Ecal, EcalEndcap);
0210                 EtSatLinEE.insert(std::make_pair(ee.rawId(), rd_physLin.getETSat()));
0211               }
0212             }
0213 
0214             int icells = 0;
0215             for (CIfeLUT p1 = dataset_TpgPhysicsLut.begin(); p1 != dataset_TpgPhysicsLut.end(); p1++) {
0216               ecidLut_xt = p1->first;
0217               rd_physLut = p1->second;
0218 
0219               std::string ecid_nameLut = ecidLut_xt.getName();
0220 
0221               // Ecal barrel detector
0222               if (ecid_nameLut == "EB") {
0223                 DetId eb(DetId::Ecal, EcalBarrel);
0224 
0225                 for (itEtSat it1 = EtSatLinEB.begin(); it1 != EtSatLinEB.end(); it1++) {
0226                   if (it1->first == (int)eb.rawId()) {
0227                     float ETSatLin = it1->second;
0228 
0229                     if (rd_physLut.getETSat() == ETSatLin) {
0230                       EcalTPGPhysicsConst::Item item;
0231                       item.EtSat = rd_physLut.getETSat();
0232                       item.ttf_threshold_Low = rd_physLut.getTTThreshlow();
0233                       item.ttf_threshold_High = rd_physLut.getTTThreshhigh();
0234                       item.FG_lowThreshold = 0;
0235                       item.FG_highThreshold = 0;
0236                       item.FG_lowRatio = 0;
0237                       item.FG_highRatio = 0;
0238                       temporaryMapEB.insert(std::make_pair(eb.rawId(), item));
0239                     } else
0240                       throw cms::Exception("The values of the ETSatLin and ETSatLut are different.");
0241                   }
0242                 }
0243 
0244                 ++icells;
0245               } else if (ecid_nameLut == "EE") {
0246                 // Ecal endcap detector
0247 
0248                 DetId ee(DetId::Ecal, EcalEndcap);
0249 
0250                 for (itEtSat it2 = EtSatLinEE.begin(); it2 != EtSatLinEE.end(); it2++) {
0251                   if (it2->first == (int)ee.rawId()) {
0252                     float ETSatLin = it2->second;
0253 
0254                     if (rd_physLut.getETSat() == ETSatLin) {
0255                       EcalTPGPhysicsConst::Item item;
0256                       item.EtSat = rd_physLut.getETSat();
0257                       item.ttf_threshold_Low = rd_physLut.getTTThreshlow();
0258                       item.ttf_threshold_High = rd_physLut.getTTThreshhigh();
0259                       item.FG_lowThreshold = 0;
0260                       item.FG_highThreshold = 0;
0261                       item.FG_lowRatio = 0;
0262                       item.FG_highRatio = 0;
0263                       temporaryMapEE.insert(std::make_pair(ee.rawId(), item));
0264                     } else
0265                       throw cms::Exception("The values of the ETSatLin and ETSatLut are different.");
0266                   }
0267                 }
0268 
0269                 ++icells;
0270               }
0271             }
0272 
0273             int icellsEB = 0;
0274             int icellsEE = 0;
0275             for (CIfeFgr p2 = dataset_TpgPhysicsFgr.begin(); p2 != dataset_TpgPhysicsFgr.end(); p2++) {
0276               ecidFgr_xt = p2->first;
0277               rd_physFgr = p2->second;
0278 
0279               std::string ecid_nameFgr = ecidFgr_xt.getName();
0280 
0281               // Ecal barrel detector
0282               if (ecid_nameFgr == "EB") {
0283                 DetId eb(DetId::Ecal, EcalBarrel);
0284 
0285                 for (iterEB itt = temporaryMapEB.begin(); itt != temporaryMapEB.end(); itt++) {
0286                   if (itt->first == (int)eb.rawId()) {
0287                     (itt->second).FG_lowThreshold = rd_physFgr.getFGlowthresh();
0288                     (itt->second).FG_highThreshold = rd_physFgr.getFGhighthresh();
0289                     (itt->second).FG_lowRatio = rd_physFgr.getFGlowratio();
0290                     (itt->second).FG_highRatio = rd_physFgr.getFGhighratio();
0291 
0292                     physC->setValue(eb.rawId(), itt->second);
0293                   }
0294                 }
0295 
0296                 ++icellsEB;
0297 
0298               } else if (ecid_nameFgr == "EE") {
0299                 // Ecal endcap detector
0300 
0301                 DetId ee(DetId::Ecal, EcalEndcap);
0302 
0303                 int countEE = 0;
0304                 for (iterEE itEE = temporaryMapEE.begin(); itEE != temporaryMapEE.end(); itEE++) {
0305                   if (itEE->first == (int)ee.rawId()) {
0306                     (itEE->second).FG_lowThreshold = rd_physFgr.getFGlowthresh();
0307                     (itEE->second).FG_highThreshold = rd_physFgr.getFGhighthresh();
0308                     // the last two is empty for the EE
0309                     (itEE->second).FG_lowRatio = rd_physFgr.getFGlowratio();
0310                     (itEE->second).FG_highRatio = rd_physFgr.getFGhighratio();
0311 
0312                     physC->setValue(ee.rawId(), itEE->second);
0313                   }
0314 
0315                   countEE++;
0316                 }
0317 
0318                 ++icellsEE;
0319               }
0320             }
0321 
0322             Time_t snc = (Time_t)irun;
0323 
0324             m_to_transfer.push_back(std::make_pair((EcalTPGPhysicsConst*)physC, snc));
0325 
0326             m_i_run_number = irun;
0327             m_i_tag = the_config_tag;
0328             m_i_version = the_config_version;
0329             m_i_physClin = linId;
0330             m_i_physClut = lutId;
0331             m_i_physCfgr = fgrId;
0332 
0333             writeFile("last_tpg_physC_settings.txt");
0334 
0335           } else {
0336             m_i_run_number = irun;
0337             m_i_tag = the_config_tag;
0338             m_i_version = the_config_version;
0339 
0340             writeFile("last_tpg_physC_settings.txt");
0341 
0342             std::cout << " even if the tag/version is not the same, the physics constants id is the same -> no "
0343                          "transfer needed "
0344                       << std::endl;
0345           }
0346 
0347         }
0348 
0349         catch (std::exception& e) {
0350           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0351                     << std::endl;
0352           std::cout << e.what() << std::endl;
0353           m_i_run_number = irun;
0354         }
0355         std::cout << " **************** " << std::endl;
0356 
0357       } else if (nr == 0) {
0358         m_i_run_number = irun;
0359         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0360         std::cout << " **************** " << std::endl;
0361       } else {
0362         m_i_run_number = irun;
0363         m_i_tag = the_config_tag;
0364         m_i_version = the_config_version;
0365         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0366         std::cout << " **************** " << std::endl;
0367         writeFile("last_tpg_physC_settings.txt");
0368       }
0369     }
0370   }
0371 
0372   delete econn;
0373   edm::LogInfo("EcalTPGPhysicsConstHandler") << "Ecal - > end of getNewObjects -----------";
0374 }
0375 
0376 void popcon::EcalTPGPhysicsConstHandler::readFromFile(const char* inputFile) {
0377   //-------------------------------------------------------------
0378 
0379   m_i_tag = "";
0380   m_i_version = 0;
0381   m_i_run_number = 0;
0382   m_i_physClin = 0;
0383   m_i_physClut = 0;
0384   m_i_physCfgr = 0;
0385 
0386   FILE* inpFile;  // input file
0387   inpFile = fopen(inputFile, "r");
0388   if (!inpFile) {
0389     edm::LogError("EcalTPGPhysicsConstHandler") << "*** Can not open file: " << inputFile;
0390     return;
0391   }
0392 
0393   char line[256];
0394 
0395   std::ostringstream str;
0396 
0397   fgets(line, 255, inpFile);
0398   m_i_tag = to_string(line);
0399   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0400 
0401   fgets(line, 255, inpFile);
0402   m_i_version = atoi(line);
0403   str << "version= " << m_i_version << std::endl;
0404 
0405   fgets(line, 255, inpFile);
0406   m_i_run_number = atoi(line);
0407   str << "run_number= " << m_i_run_number << std::endl;
0408 
0409   fgets(line, 255, inpFile);
0410   m_i_physClin = atoi(line);
0411   str << "physClin_config= " << m_i_physClin << std::endl;
0412 
0413   fgets(line, 255, inpFile);
0414   m_i_physClut = atoi(line);
0415   str << "physClut_config= " << m_i_physClut << std::endl;
0416 
0417   fgets(line, 255, inpFile);
0418   m_i_physCfgr = atoi(line);
0419   str << "physCfgr_config= " << m_i_physCfgr << std::endl;
0420 
0421   fclose(inpFile);  // close inp. file
0422 }
0423 
0424 void popcon::EcalTPGPhysicsConstHandler::writeFile(const char* inputFile) {
0425   //-------------------------------------------------------------
0426 
0427   std::ofstream myfile;
0428   myfile.open(inputFile);
0429   myfile << m_i_tag << std::endl;
0430   myfile << m_i_version << std::endl;
0431   myfile << m_i_run_number << std::endl;
0432   myfile << m_i_physClin << std::endl;
0433   myfile << m_i_physClut << std::endl;
0434   myfile << m_i_physCfgr << std::endl;
0435 
0436   myfile.close();
0437 }