Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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             for (CIfeLUT p1 = dataset_TpgPhysicsLut.begin(); p1 != dataset_TpgPhysicsLut.end(); p1++) {
0215               ecidLut_xt = p1->first;
0216               rd_physLut = p1->second;
0217 
0218               std::string ecid_nameLut = ecidLut_xt.getName();
0219 
0220               // Ecal barrel detector
0221               if (ecid_nameLut == "EB") {
0222                 DetId eb(DetId::Ecal, EcalBarrel);
0223 
0224                 for (itEtSat it1 = EtSatLinEB.begin(); it1 != EtSatLinEB.end(); it1++) {
0225                   if (it1->first == (int)eb.rawId()) {
0226                     float ETSatLin = it1->second;
0227 
0228                     if (rd_physLut.getETSat() == ETSatLin) {
0229                       EcalTPGPhysicsConst::Item item;
0230                       item.EtSat = rd_physLut.getETSat();
0231                       item.ttf_threshold_Low = rd_physLut.getTTThreshlow();
0232                       item.ttf_threshold_High = rd_physLut.getTTThreshhigh();
0233                       item.FG_lowThreshold = 0;
0234                       item.FG_highThreshold = 0;
0235                       item.FG_lowRatio = 0;
0236                       item.FG_highRatio = 0;
0237                       temporaryMapEB.insert(std::make_pair(eb.rawId(), item));
0238                     } else
0239                       throw cms::Exception("The values of the ETSatLin and ETSatLut are different.");
0240                   }
0241                 }
0242 
0243               } else if (ecid_nameLut == "EE") {
0244                 // Ecal endcap detector
0245 
0246                 DetId ee(DetId::Ecal, EcalEndcap);
0247 
0248                 for (itEtSat it2 = EtSatLinEE.begin(); it2 != EtSatLinEE.end(); it2++) {
0249                   if (it2->first == (int)ee.rawId()) {
0250                     float ETSatLin = it2->second;
0251 
0252                     if (rd_physLut.getETSat() == ETSatLin) {
0253                       EcalTPGPhysicsConst::Item item;
0254                       item.EtSat = rd_physLut.getETSat();
0255                       item.ttf_threshold_Low = rd_physLut.getTTThreshlow();
0256                       item.ttf_threshold_High = rd_physLut.getTTThreshhigh();
0257                       item.FG_lowThreshold = 0;
0258                       item.FG_highThreshold = 0;
0259                       item.FG_lowRatio = 0;
0260                       item.FG_highRatio = 0;
0261                       temporaryMapEE.insert(std::make_pair(ee.rawId(), item));
0262                     } else
0263                       throw cms::Exception("The values of the ETSatLin and ETSatLut are different.");
0264                   }
0265                 }
0266               }
0267             }
0268 
0269             for (CIfeFgr p2 = dataset_TpgPhysicsFgr.begin(); p2 != dataset_TpgPhysicsFgr.end(); p2++) {
0270               ecidFgr_xt = p2->first;
0271               rd_physFgr = p2->second;
0272 
0273               std::string ecid_nameFgr = ecidFgr_xt.getName();
0274 
0275               // Ecal barrel detector
0276               if (ecid_nameFgr == "EB") {
0277                 DetId eb(DetId::Ecal, EcalBarrel);
0278 
0279                 for (iterEB itt = temporaryMapEB.begin(); itt != temporaryMapEB.end(); itt++) {
0280                   if (itt->first == (int)eb.rawId()) {
0281                     (itt->second).FG_lowThreshold = rd_physFgr.getFGlowthresh();
0282                     (itt->second).FG_highThreshold = rd_physFgr.getFGhighthresh();
0283                     (itt->second).FG_lowRatio = rd_physFgr.getFGlowratio();
0284                     (itt->second).FG_highRatio = rd_physFgr.getFGhighratio();
0285 
0286                     physC->setValue(eb.rawId(), itt->second);
0287                   }
0288                 }
0289 
0290               } else if (ecid_nameFgr == "EE") {
0291                 // Ecal endcap detector
0292 
0293                 DetId ee(DetId::Ecal, EcalEndcap);
0294 
0295                 for (iterEE itEE = temporaryMapEE.begin(); itEE != temporaryMapEE.end(); itEE++) {
0296                   if (itEE->first == (int)ee.rawId()) {
0297                     (itEE->second).FG_lowThreshold = rd_physFgr.getFGlowthresh();
0298                     (itEE->second).FG_highThreshold = rd_physFgr.getFGhighthresh();
0299                     // the last two is empty for the EE
0300                     (itEE->second).FG_lowRatio = rd_physFgr.getFGlowratio();
0301                     (itEE->second).FG_highRatio = rd_physFgr.getFGhighratio();
0302 
0303                     physC->setValue(ee.rawId(), itEE->second);
0304                   }
0305                 }
0306               }
0307             }
0308 
0309             Time_t snc = (Time_t)irun;
0310 
0311             m_to_transfer.push_back(std::make_pair((EcalTPGPhysicsConst*)physC, snc));
0312 
0313             m_i_run_number = irun;
0314             m_i_tag = the_config_tag;
0315             m_i_version = the_config_version;
0316             m_i_physClin = linId;
0317             m_i_physClut = lutId;
0318             m_i_physCfgr = fgrId;
0319 
0320             writeFile("last_tpg_physC_settings.txt");
0321 
0322           } else {
0323             m_i_run_number = irun;
0324             m_i_tag = the_config_tag;
0325             m_i_version = the_config_version;
0326 
0327             writeFile("last_tpg_physC_settings.txt");
0328 
0329             std::cout << " even if the tag/version is not the same, the physics constants id is the same -> no "
0330                          "transfer needed "
0331                       << std::endl;
0332           }
0333 
0334         }
0335 
0336         catch (std::exception& e) {
0337           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0338                     << std::endl;
0339           std::cout << e.what() << std::endl;
0340           m_i_run_number = irun;
0341         }
0342         std::cout << " **************** " << std::endl;
0343 
0344       } else if (nr == 0) {
0345         m_i_run_number = irun;
0346         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0347         std::cout << " **************** " << std::endl;
0348       } else {
0349         m_i_run_number = irun;
0350         m_i_tag = the_config_tag;
0351         m_i_version = the_config_version;
0352         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0353         std::cout << " **************** " << std::endl;
0354         writeFile("last_tpg_physC_settings.txt");
0355       }
0356     }
0357   }
0358 
0359   delete econn;
0360   edm::LogInfo("EcalTPGPhysicsConstHandler") << "Ecal - > end of getNewObjects -----------";
0361 }
0362 
0363 void popcon::EcalTPGPhysicsConstHandler::readFromFile(const char* inputFile) {
0364   //-------------------------------------------------------------
0365 
0366   m_i_tag = "";
0367   m_i_version = 0;
0368   m_i_run_number = 0;
0369   m_i_physClin = 0;
0370   m_i_physClut = 0;
0371   m_i_physCfgr = 0;
0372 
0373   FILE* inpFile;  // input file
0374   inpFile = fopen(inputFile, "r");
0375   if (!inpFile) {
0376     edm::LogError("EcalTPGPhysicsConstHandler") << "*** Can not open file: " << inputFile;
0377     return;
0378   }
0379 
0380   char line[256];
0381 
0382   std::ostringstream str;
0383 
0384   fgets(line, 255, inpFile);
0385   m_i_tag = to_string(line);
0386   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0387 
0388   fgets(line, 255, inpFile);
0389   m_i_version = atoi(line);
0390   str << "version= " << m_i_version << std::endl;
0391 
0392   fgets(line, 255, inpFile);
0393   m_i_run_number = atoi(line);
0394   str << "run_number= " << m_i_run_number << std::endl;
0395 
0396   fgets(line, 255, inpFile);
0397   m_i_physClin = atoi(line);
0398   str << "physClin_config= " << m_i_physClin << std::endl;
0399 
0400   fgets(line, 255, inpFile);
0401   m_i_physClut = atoi(line);
0402   str << "physClut_config= " << m_i_physClut << std::endl;
0403 
0404   fgets(line, 255, inpFile);
0405   m_i_physCfgr = atoi(line);
0406   str << "physCfgr_config= " << m_i_physCfgr << std::endl;
0407 
0408   fclose(inpFile);  // close inp. file
0409 }
0410 
0411 void popcon::EcalTPGPhysicsConstHandler::writeFile(const char* inputFile) {
0412   //-------------------------------------------------------------
0413 
0414   std::ofstream myfile;
0415   myfile.open(inputFile);
0416   myfile << m_i_tag << std::endl;
0417   myfile << m_i_version << std::endl;
0418   myfile << m_i_run_number << std::endl;
0419   myfile << m_i_physClin << std::endl;
0420   myfile << m_i_physClut << std::endl;
0421   myfile << m_i_physCfgr << std::endl;
0422 
0423   myfile.close();
0424 }