Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 
0218             for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) {
0219               ecid_xt = p->first;
0220               rd_lut = p->second;
0221 
0222               std::string ecid_name = ecid_xt.getName();
0223 
0224               if (ecid_name == "EB_trigger_tower") {
0225                 // SM number
0226                 int smid = ecid_xt.getID1();
0227                 // TT number
0228                 int towerid = ecid_xt.getID2();
0229 
0230                 int tow_eta = (towerid - 1) / 4;
0231                 int tow_phi = ((towerid - 1) - tow_eta * 4);
0232 
0233                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0234 
0235                 EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
0236                 const EcalTrigTowerDetId towid = id.tower();
0237 
0238                 /*   
0239               char ch[10];
0240                   sprintf(ch,"%d%d", smid, towerid);
0241                   std::string S="";
0242                   S.insert(0,ch);
0243       
0244                   unsigned int towerEBId = 0;
0245                   towerEBId = atoi(S.c_str());
0246               */
0247 
0248                 lut->setValue(towid.rawId(), rd_lut.getLUTGroupId());
0249               } else if (ecid_name == "EE_trigger_tower") {
0250                 // EE data
0251                 // TCC number
0252                 int tccid = ecid_xt.getID1();
0253                 // TT number
0254                 int towerid = ecid_xt.getID2();
0255 
0256                 bool set_the_tower = false;
0257                 int towid;
0258                 for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
0259                   if (!set_the_tower) {
0260                     if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
0261                         my_TTEcalLogicId_EE[itower].getID2() == towerid) {
0262                       towid = my_TTEcalLogicId_EE[itower].getLogicID();
0263                       set_the_tower = true;
0264                       break;
0265                     }
0266                   }
0267                 }
0268 
0269                 if (set_the_tower) {
0270                   lut->setValue(towid, rd_lut.getLUTGroupId());
0271                 } else {
0272                   std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
0273                 }
0274               }
0275             }
0276 
0277             Time_t snc = (Time_t)irun;
0278 
0279             m_to_transfer.push_back(std::make_pair((EcalTPGLutGroup *)lut, snc));
0280 
0281             m_i_run_number = irun;
0282             m_i_tag = the_config_tag;
0283             m_i_version = the_config_version;
0284             m_i_lutGroup = lutId;
0285 
0286             writeFile("last_tpg_lutGroup_settings.txt");
0287 
0288           } else {
0289             m_i_run_number = irun;
0290             m_i_tag = the_config_tag;
0291             m_i_version = the_config_version;
0292 
0293             writeFile("last_tpg_lutGroup_settings.txt");
0294 
0295             std::cout << " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "
0296                       << std::endl;
0297           }
0298 
0299         }
0300 
0301         catch (std::exception &e) {
0302           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0303                     << std::endl;
0304           std::cout << e.what() << std::endl;
0305           m_i_run_number = irun;
0306         }
0307         std::cout << " **************** " << std::endl;
0308 
0309       } else if (nr == 0) {
0310         m_i_run_number = irun;
0311         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0312         std::cout << " **************** " << std::endl;
0313       } else {
0314         m_i_run_number = irun;
0315         m_i_tag = the_config_tag;
0316         m_i_version = the_config_version;
0317         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0318         std::cout << " **************** " << std::endl;
0319         writeFile("last_tpg_lutGroup_settings.txt");
0320       }
0321     }
0322   }
0323 
0324   delete econn;
0325 
0326   edm::LogInfo("EcalTPGLutGroupHandler") << "Ecal - > end of getNewObjects -----------";
0327 }
0328 
0329 void popcon::EcalTPGLutGroupHandler::readFromFile(const char *inputFile) {
0330   //-------------------------------------------------------------
0331 
0332   m_i_tag = "";
0333   m_i_version = 0;
0334   m_i_run_number = 0;
0335   m_i_lutGroup = 0;
0336 
0337   FILE *inpFile;  // input file
0338   inpFile = fopen(inputFile, "r");
0339   if (!inpFile) {
0340     edm::LogError("EcalTPGLutGroupHandler") << "*** Can not open file: " << inputFile;
0341     return;
0342   }
0343 
0344   char line[256];
0345 
0346   std::ostringstream str;
0347 
0348   fgets(line, 255, inpFile);
0349   m_i_tag = to_string(line);
0350   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0351 
0352   fgets(line, 255, inpFile);
0353   m_i_version = atoi(line);
0354   str << "version= " << m_i_version << std::endl;
0355 
0356   fgets(line, 255, inpFile);
0357   m_i_run_number = atoi(line);
0358   str << "run_number= " << m_i_run_number << std::endl;
0359 
0360   fgets(line, 255, inpFile);
0361   m_i_lutGroup = atoi(line);
0362   str << "lutGroup_config= " << m_i_lutGroup << std::endl;
0363 
0364   fclose(inpFile);  // close inp. file
0365 }
0366 
0367 void popcon::EcalTPGLutGroupHandler::writeFile(const char *inputFile) {
0368   //-------------------------------------------------------------
0369 
0370   std::ofstream myfile;
0371   myfile.open(inputFile);
0372   myfile << m_i_tag << std::endl;
0373   myfile << m_i_version << std::endl;
0374   myfile << m_i_run_number << std::endl;
0375   myfile << m_i_lutGroup << std::endl;
0376 
0377   myfile.close();
0378 }