Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGLutGroupHandler.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/FEConfigLUTInfo.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTDat.h"
0007 
0008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0009 
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include "CondFormats/EcalObjects/interface/EcalTPGLutGroup.h"
0013 
0014 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0015 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0016 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
0017 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0018 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
0019 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
0020 
0021 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0022 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0023 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0024 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
0025 
0026 #include <iostream>
0027 #include <fstream>
0028 
0029 #include <ctime>
0030 #include <unistd.h>
0031 
0032 #include <string>
0033 #include <cstdio>
0034 #include <typeinfo>
0035 #include <sstream>
0036 
0037 popcon::EcalTPGLutGroupHandler::EcalTPGLutGroupHandler(const edm::ParameterSet &ps)
0038     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGLutGroupHandler")) {
0039   edm::LogInfo("EcalTPGLutGroupHandler") << "EcalTPGLutGroup Source handler constructor";
0040   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0041   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0042   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0043   m_user = ps.getParameter<std::string>("OnlineDBUser");
0044   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0045   m_locationsource = ps.getParameter<std::string>("LocationSource");
0046   m_location = ps.getParameter<std::string>("Location");
0047   m_gentag = ps.getParameter<std::string>("GenTag");
0048   m_runtype = ps.getParameter<std::string>("RunType");
0049 
0050   edm::LogInfo("EcalTPGLutGroupHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0051 }
0052 
0053 popcon::EcalTPGLutGroupHandler::~EcalTPGLutGroupHandler() {}
0054 
0055 void popcon::EcalTPGLutGroupHandler::getNewObjects() {
0056   using namespace edm;
0057   using namespace std;
0058 
0059   edm::LogInfo("EcalTPGLutGroupHandler") << "Started GetNewObjects!!!";
0060 
0061   /*
0062     // geometry
0063     ESHandle<CaloGeometry> theGeometry;
0064     ESHandle<CaloSubdetectorGeometry> theEndcapGeometry_handle, theBarrelGeometry_handle;
0065     evtSetup.get<CaloGeometryRecord>().get( theGeometry );
0066     evtSetup.get<EcalEndcapGeometryRecord>().get("EcalEndcap",theEndcapGeometry_handle);
0067     evtSetup.get<EcalBarrelGeometryRecord>().get("EcalBarrel",theBarrelGeometry_handle);
0068     evtSetup.get<IdealGeometryRecord>().get(eTTmap_);
0069     theEndcapGeometry_ = &(*theEndcapGeometry_handle);
0070     theBarrelGeometry_ = &(*theBarrelGeometry_handle);
0071 
0072     // electronics mapping
0073     ESHandle< EcalElectronicsMapping > ecalmapping;
0074     evtSetup.get< EcalMappingRcd >().get(ecalmapping);
0075     theMapping_ = ecalmapping.product();
0076 
0077     const std::vector<DetId> & eeCells = theEndcapGeometry_->getValidDetIds(DetId::Ecal, EcalEndcap);
0078 
0079     */
0080 
0081   //check whats already inside of database
0082   if (tagInfo().size) {
0083     //check whats already inside of database
0084     std::cout << "got offlineInfo = " << std::endl;
0085     std::cout << "tag name = " << tagInfo().name << std::endl;
0086     std::cout << "size = " << tagInfo().size << std::endl;
0087   } else {
0088     std::cout << " First object for this tag " << std::endl;
0089   }
0090 
0091   unsigned int max_since = 0;
0092   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0093   edm::LogInfo("EcalTPGLutGroupHandler") << "max_since : " << max_since;
0094   edm::LogInfo("EcalTPGLutGroupHandler") << "retrieved last payload ";
0095 
0096   // here we retrieve all the runs after the last from online DB
0097   edm::LogInfo("EcalTPGLutGroupHandler") << "Retrieving run list from ONLINE DB ... ";
0098 
0099   edm::LogInfo("EcalTPGLutGroupHandler") << "Making connection...";
0100   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0101   edm::LogInfo("EcalTPGLutGroupHandler") << "Done.";
0102 
0103   if (!econn) {
0104     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0105     //      cerr << e.what() << std::endl;
0106     throw cms::Exception("OMDS not available");
0107   }
0108 
0109   LocationDef my_locdef;
0110   my_locdef.setLocation(m_location);
0111 
0112   RunTypeDef my_rundef;
0113   my_rundef.setRunType(m_runtype);
0114 
0115   RunTag my_runtag;
0116   my_runtag.setLocationDef(my_locdef);
0117   my_runtag.setRunTypeDef(my_rundef);
0118   my_runtag.setGeneralTag(m_gentag);
0119 
0120   readFromFile("last_tpg_lutGroup_settings.txt");
0121 
0122   unsigned int min_run;
0123 
0124   if (m_firstRun < m_i_run_number) {
0125     min_run = m_i_run_number + 1;
0126   } else {
0127     min_run = m_firstRun;
0128   }
0129 
0130   if (min_run < max_since) {
0131     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0132   }
0133 
0134   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0135             << std::endl;
0136 
0137   unsigned int max_run = m_lastRun;
0138   edm::LogInfo("EcalTPGLutGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
0139 
0140   RunList my_list;
0141   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0142   //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
0143 
0144   std::vector<RunIOV> run_vec = my_list.getRuns();
0145   size_t num_runs = run_vec.size();
0146 
0147   std::cout << "number of runs is : " << num_runs << std::endl;
0148 
0149   std::string str = "";
0150 
0151   unsigned int irun;
0152   if (num_runs > 0) {
0153     // going to query the ecal logic id
0154     std::vector<EcalLogicID> my_TTEcalLogicId_EE;
0155     my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered(
0156         "EE_trigger_tower", 1, 200, 1, 70, EcalLogicID::NULLID, EcalLogicID::NULLID, "EE_offline_towerid", 12);
0157     std::cout << " GOT the logic ID for the EE trigger towers " << std::endl;
0158 
0159     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0160       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0161 
0162       std::cout << " **************** " << std::endl;
0163       std::cout << " run= " << irun << std::endl;
0164 
0165       // retrieve the data :
0166       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0167       econn->fetchDataSet(&dataset, &run_vec[kr]);
0168 
0169       std::string the_config_tag = "";
0170       int the_config_version = 0;
0171 
0172       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0173 
0174       int nr = 0;
0175       for (it = dataset.begin(); it != dataset.end(); it++) {
0176         ++nr;
0177         //EcalLogicID ecalid  = it->first;
0178 
0179         RunTPGConfigDat dat = it->second;
0180         the_config_tag = dat.getConfigTag();
0181         the_config_version = dat.getVersion();
0182       }
0183 
0184       // it is all the same for all SM... get the last one
0185 
0186       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0187 
0188       // here we should check if it is the same as previous run.
0189 
0190       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0191         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0192                   << std::endl;
0193 
0194         FEConfigMainInfo fe_main_info;
0195         fe_main_info.setConfigTag(the_config_tag);
0196         fe_main_info.setVersion(the_config_version);
0197 
0198         try {
0199           std::cout << " before fetch config set" << std::endl;
0200           econn->fetchConfigSet(&fe_main_info);
0201           std::cout << " after fetch config set" << std::endl;
0202 
0203           // now get TPGLutGroup
0204           int lutId = fe_main_info.getLUTId();
0205 
0206           if (lutId != m_i_lutGroup) {
0207             FEConfigLUTInfo fe_lut_info;
0208             fe_lut_info.setId(lutId);
0209             econn->fetchConfigSet(&fe_lut_info);
0210             std::map<EcalLogicID, FEConfigLUTDat> dataset_TpgLut;
0211             econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
0212 
0213             EcalTPGLutGroup *lut = new EcalTPGLutGroup();
0214             typedef std::map<EcalLogicID, FEConfigLUTDat>::const_iterator CIfelut;
0215             EcalLogicID ecid_xt;
0216             FEConfigLUTDat rd_lut;
0217             int itowers = 0;
0218 
0219             for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) {
0220               ecid_xt = p->first;
0221               rd_lut = p->second;
0222 
0223               std::string ecid_name = ecid_xt.getName();
0224 
0225               if (ecid_name == "EB_trigger_tower") {
0226                 // SM number
0227                 int smid = ecid_xt.getID1();
0228                 // TT number
0229                 int towerid = ecid_xt.getID2();
0230 
0231                 int tow_eta = (towerid - 1) / 4;
0232                 int tow_phi = ((towerid - 1) - tow_eta * 4);
0233 
0234                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0235 
0236                 EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
0237                 const EcalTrigTowerDetId towid = id.tower();
0238 
0239                 /*   
0240               char ch[10];
0241                   sprintf(ch,"%d%d", smid, towerid);
0242                   std::string S="";
0243                   S.insert(0,ch);
0244       
0245                   unsigned int towerEBId = 0;
0246                   towerEBId = atoi(S.c_str());
0247               */
0248 
0249                 lut->setValue(towid.rawId(), rd_lut.getLUTGroupId());
0250                 ++itowers;
0251               } else if (ecid_name == "EE_trigger_tower") {
0252                 // EE data
0253                 // TCC number
0254                 int tccid = ecid_xt.getID1();
0255                 // TT number
0256                 int towerid = ecid_xt.getID2();
0257 
0258                 bool set_the_tower = false;
0259                 int towid;
0260                 for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
0261                   if (!set_the_tower) {
0262                     if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
0263                         my_TTEcalLogicId_EE[itower].getID2() == towerid) {
0264                       towid = my_TTEcalLogicId_EE[itower].getLogicID();
0265                       set_the_tower = true;
0266                       break;
0267                     }
0268                   }
0269                 }
0270 
0271                 if (set_the_tower) {
0272                   lut->setValue(towid, rd_lut.getLUTGroupId());
0273                 } else {
0274                   std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
0275                 }
0276 
0277                 ++itowers;
0278               }
0279             }
0280 
0281             Time_t snc = (Time_t)irun;
0282 
0283             m_to_transfer.push_back(std::make_pair((EcalTPGLutGroup *)lut, snc));
0284 
0285             m_i_run_number = irun;
0286             m_i_tag = the_config_tag;
0287             m_i_version = the_config_version;
0288             m_i_lutGroup = lutId;
0289 
0290             writeFile("last_tpg_lutGroup_settings.txt");
0291 
0292           } else {
0293             m_i_run_number = irun;
0294             m_i_tag = the_config_tag;
0295             m_i_version = the_config_version;
0296 
0297             writeFile("last_tpg_lutGroup_settings.txt");
0298 
0299             std::cout << " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "
0300                       << std::endl;
0301           }
0302 
0303         }
0304 
0305         catch (std::exception &e) {
0306           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0307                     << std::endl;
0308           std::cout << e.what() << std::endl;
0309           m_i_run_number = irun;
0310         }
0311         std::cout << " **************** " << std::endl;
0312 
0313       } else if (nr == 0) {
0314         m_i_run_number = irun;
0315         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0316         std::cout << " **************** " << std::endl;
0317       } else {
0318         m_i_run_number = irun;
0319         m_i_tag = the_config_tag;
0320         m_i_version = the_config_version;
0321         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0322         std::cout << " **************** " << std::endl;
0323         writeFile("last_tpg_lutGroup_settings.txt");
0324       }
0325     }
0326   }
0327 
0328   delete econn;
0329 
0330   edm::LogInfo("EcalTPGLutGroupHandler") << "Ecal - > end of getNewObjects -----------";
0331 }
0332 
0333 void popcon::EcalTPGLutGroupHandler::readFromFile(const char *inputFile) {
0334   //-------------------------------------------------------------
0335 
0336   m_i_tag = "";
0337   m_i_version = 0;
0338   m_i_run_number = 0;
0339   m_i_lutGroup = 0;
0340 
0341   FILE *inpFile;  // input file
0342   inpFile = fopen(inputFile, "r");
0343   if (!inpFile) {
0344     edm::LogError("EcalTPGLutGroupHandler") << "*** Can not open file: " << inputFile;
0345     return;
0346   }
0347 
0348   char line[256];
0349 
0350   std::ostringstream str;
0351 
0352   fgets(line, 255, inpFile);
0353   m_i_tag = to_string(line);
0354   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0355 
0356   fgets(line, 255, inpFile);
0357   m_i_version = atoi(line);
0358   str << "version= " << m_i_version << std::endl;
0359 
0360   fgets(line, 255, inpFile);
0361   m_i_run_number = atoi(line);
0362   str << "run_number= " << m_i_run_number << std::endl;
0363 
0364   fgets(line, 255, inpFile);
0365   m_i_lutGroup = atoi(line);
0366   str << "lutGroup_config= " << m_i_lutGroup << std::endl;
0367 
0368   fclose(inpFile);  // close inp. file
0369 }
0370 
0371 void popcon::EcalTPGLutGroupHandler::writeFile(const char *inputFile) {
0372   //-------------------------------------------------------------
0373 
0374   std::ofstream myfile;
0375   myfile.open(inputFile);
0376   myfile << m_i_tag << std::endl;
0377   myfile << m_i_version << std::endl;
0378   myfile << m_i_run_number << std::endl;
0379   myfile << m_i_lutGroup << std::endl;
0380 
0381   myfile.close();
0382 }