Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-05-05 02:47:17

0001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
0002 
0003 #include "CondTools/Ecal/interface/EcalTPGFineGrainStripEEHandler.h"
0004 
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 
0007 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0008 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
0009 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 
0013 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
0014 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
0015 
0016 #include <iostream>
0017 #include <fstream>
0018 
0019 #include <ctime>
0020 #include <unistd.h>
0021 
0022 #include <string>
0023 #include <cstdio>
0024 #include <typeinfo>
0025 #include <sstream>
0026 
0027 popcon::EcalTPGFineGrainStripEEHandler::EcalTPGFineGrainStripEEHandler(const edm::ParameterSet& ps)
0028     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGFineGrainStripEEHandler")) {
0029   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "EcalTPGFineGrainStripEEHandler Source handler constructor";
0030   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0031   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0032   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0033   m_user = ps.getParameter<std::string>("OnlineDBUser");
0034   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0035   m_locationsource = ps.getParameter<std::string>("LocationSource");
0036   m_location = ps.getParameter<std::string>("Location");
0037   m_gentag = ps.getParameter<std::string>("GenTag");
0038   m_runtype = ps.getParameter<std::string>("RunType");
0039 
0040   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0041 }
0042 
0043 popcon::EcalTPGFineGrainStripEEHandler::~EcalTPGFineGrainStripEEHandler() {}
0044 
0045 void popcon::EcalTPGFineGrainStripEEHandler::getNewObjects() {
0046   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Started GetNewObjects!!!";
0047 
0048   //check whats already inside of database
0049   if (tagInfo().size) {
0050     //check whats already inside of database
0051     std::cout << "got offlineInfo = " << std::endl;
0052     std::cout << "tag name = " << tagInfo().name << std::endl;
0053     std::cout << "size = " << tagInfo().size << std::endl;
0054   } else {
0055     std::cout << " First object for this tag " << std::endl;
0056   }
0057 
0058   unsigned int max_since = 0;
0059   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0060   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "max_since : " << max_since;
0061   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "retrieved last payload ";
0062 
0063   // here we retrieve all the runs after the last from online DB
0064 
0065   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Retrieving run list from ONLINE DB ... ";
0066 
0067   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Making connection...";
0068   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0069   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Done.";
0070 
0071   if (!econn) {
0072     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0073     throw cms::Exception("OMDS not available");
0074   }
0075 
0076   LocationDef my_locdef;
0077   my_locdef.setLocation(m_location);
0078 
0079   RunTypeDef my_rundef;
0080   my_rundef.setRunType(m_runtype);
0081 
0082   RunTag my_runtag;
0083   my_runtag.setLocationDef(my_locdef);
0084   my_runtag.setRunTypeDef(my_rundef);
0085   my_runtag.setGeneralTag(m_gentag);
0086 
0087   readFromFile("last_tpg_fgrStripEE_settings.txt");
0088 
0089   unsigned int min_run;
0090 
0091   if (m_firstRun < m_i_run_number) {
0092     min_run = m_i_run_number + 1;
0093   } else {
0094     min_run = m_firstRun;
0095   }
0096   if (min_run < max_since) {
0097     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0098   }
0099 
0100   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0101             << std::endl;
0102 
0103   unsigned int max_run = m_lastRun;
0104   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "min_run= " << min_run << "max_run= " << max_run;
0105 
0106   RunList my_list;
0107   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0108   //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run, my_locdef);
0109 
0110   std::vector<RunIOV> run_vec = my_list.getRuns();
0111   size_t num_runs = run_vec.size();
0112 
0113   std::cout << "number of runs is : " << num_runs << std::endl;
0114 
0115   unsigned int irun = 0;
0116   if (num_runs > 0) {
0117     // going to query the ecal logic id
0118     std::vector<EcalLogicID> my_StripEcalLogicId_EE;
0119     my_StripEcalLogicId_EE =
0120         econn->getEcalLogicIDSetOrdered("ECAL_readout_strip", 1, 1000, 1, 100, 0, 5, "EE_offline_stripid", 123);
0121     std::cout << " GOT the logic ID for the EE trigger strips " << std::endl;
0122 
0123     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0124       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0125 
0126       std::cout << " **************** " << std::endl;
0127       std::cout << " **************** " << std::endl;
0128       std::cout << " run= " << irun << std::endl;
0129 
0130       // retrieve the data :
0131       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0132       econn->fetchDataSet(&dataset, &run_vec[kr]);
0133 
0134       std::string the_config_tag = "";
0135       int the_config_version = 0;
0136 
0137       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0138 
0139       int nr = 0;
0140       for (it = dataset.begin(); it != dataset.end(); it++) {
0141         ++nr;
0142         //EcalLogicID ecalid  = it->first;
0143         RunTPGConfigDat dat = it->second;
0144         the_config_tag = dat.getConfigTag();
0145         the_config_version = dat.getVersion();
0146       }
0147 
0148       // it is all the same for all SM... get the last one
0149 
0150       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0151 
0152       // here we should check if it is the same as previous run.
0153 
0154       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0155         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0156                   << std::endl;
0157 
0158         FEConfigMainInfo fe_main_info;
0159         fe_main_info.setConfigTag(the_config_tag);
0160         fe_main_info.setVersion(the_config_version);
0161 
0162         try {
0163           std::cout << " before fetch config set" << std::endl;
0164           econn->fetchConfigSet(&fe_main_info);
0165           std::cout << " after fetch config set" << std::endl;
0166 
0167           // now get TPGFineGrainStripEE
0168           int fgrId = fe_main_info.getFgrId();
0169 
0170           if (fgrId != m_i_fgrStripEE) {
0171             FEConfigFgrInfo fe_fgr_info;
0172             fe_fgr_info.setId(fgrId);
0173             econn->fetchConfigSet(&fe_fgr_info);
0174             std::map<EcalLogicID, FEConfigFgrEEStripDat> dataset_TpgFineGrainStripEE;
0175             econn->fetchDataSet(&dataset_TpgFineGrainStripEE, &fe_fgr_info);
0176 
0177             EcalTPGFineGrainStripEE* fgrStripEE = new EcalTPGFineGrainStripEE;
0178             typedef std::map<EcalLogicID, FEConfigFgrEEStripDat>::const_iterator CIfefgr;
0179             EcalLogicID ecid_xt;
0180             FEConfigFgrEEStripDat rd_fgr;
0181 
0182             for (CIfefgr p = dataset_TpgFineGrainStripEE.begin(); p != dataset_TpgFineGrainStripEE.end(); p++) {
0183               ecid_xt = p->first;
0184               rd_fgr = p->second;
0185 
0186               std::string ecid_name = ecid_xt.getName();
0187 
0188               // EB data
0189               if (ecid_name == "EB_VFE") {
0190                 int sm = ecid_xt.getID1();
0191                 int tt = ecid_xt.getID2();
0192                 int strip = ecid_xt.getID3();
0193                 int tcc = sm + 54;
0194                 if (sm > 18)
0195                   tcc = sm + 18;
0196 
0197                 // simple formula to calculate the Srip EB identifier
0198 
0199                 unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0200 
0201                 EcalTPGFineGrainStripEE::Item item;
0202                 item.threshold = (unsigned int)rd_fgr.getThreshold();
0203                 item.lut = (unsigned int)rd_fgr.getLUTFgr();
0204 
0205                 fgrStripEE->setValue(stripEBId, item);
0206 
0207               } else if (ecid_name == "ECAL_readout_strip") {
0208                 // EE data
0209                 // fed
0210                 int id1 = ecid_xt.getID1();
0211                 // ccu
0212                 int id2 = ecid_xt.getID2();
0213                 // Strip
0214                 int id3 = ecid_xt.getID3();
0215 
0216                 bool set_the_strip = false;
0217                 int stripEEId;
0218                 for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0219                   if (!set_the_strip) {
0220                     if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
0221                         my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
0222                         my_StripEcalLogicId_EE[istrip].getID3() == id3) {
0223                       stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
0224                       set_the_strip = true;
0225                       break;
0226                     }
0227                   }
0228                 }
0229 
0230                 EcalTPGFineGrainStripEE::Item item;
0231                 item.threshold = (unsigned int)rd_fgr.getThreshold();
0232                 item.lut = (unsigned int)rd_fgr.getLUTFgr();
0233 
0234                 if (set_the_strip) {
0235                   fgrStripEE->setValue(stripEEId, item);
0236                 } else {
0237                   std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
0238                 }
0239               }
0240             }
0241 
0242             Time_t snc = (Time_t)irun;
0243             m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainStripEE*)fgrStripEE, snc));
0244 
0245             m_i_run_number = irun;
0246             m_i_tag = the_config_tag;
0247             m_i_version = the_config_version;
0248             m_i_fgrStripEE = fgrId;
0249 
0250             writeFile("last_tpg_fgrStripEE_settings.txt");
0251 
0252           } else {
0253             m_i_run_number = irun;
0254             m_i_tag = the_config_tag;
0255             m_i_version = the_config_version;
0256 
0257             writeFile("last_tpg_fgrStripEE_settings.txt");
0258 
0259             std::cout << " even if the tag/version is not the same, the fgrStripEEestals id is the same -> no transfer "
0260                          "needed "
0261                       << std::endl;
0262           }
0263 
0264         }
0265 
0266         catch (std::exception& e) {
0267           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0268                     << std::endl;
0269           std::cout << e.what() << std::endl;
0270           m_i_run_number = irun;
0271         }
0272         std::cout << " **************** " << std::endl;
0273 
0274       } else if (nr == 0) {
0275         m_i_run_number = irun;
0276         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0277         std::cout << " **************** " << std::endl;
0278       } else {
0279         m_i_run_number = irun;
0280         m_i_tag = the_config_tag;
0281         m_i_version = the_config_version;
0282         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0283         std::cout << " **************** " << std::endl;
0284         writeFile("last_tpg_fgrStripEE_settings.txt");
0285       }
0286     }
0287   }
0288 
0289   delete econn;
0290 
0291   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Ecal - > end of getNewObjects -----------";
0292 }
0293 
0294 void popcon::EcalTPGFineGrainStripEEHandler::readFromFile(const char* inputFile) {
0295   //-------------------------------------------------------------
0296 
0297   m_i_tag = "";
0298   m_i_version = 0;
0299   m_i_run_number = 0;
0300   m_i_fgrStripEE = 0;
0301 
0302   FILE* inpFile;  // input file
0303   inpFile = fopen(inputFile, "r");
0304   if (!inpFile) {
0305     edm::LogError("EcalTPGFineGrainStripEEHandler") << "*** Can not open file: " << inputFile;
0306     return;
0307   }
0308 
0309   char line[256];
0310 
0311   std::ostringstream str;
0312 
0313   fgets(line, 255, inpFile);
0314   m_i_tag = to_string(line);
0315   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0316 
0317   fgets(line, 255, inpFile);
0318   m_i_version = atoi(line);
0319   str << "version= " << m_i_version << std::endl;
0320 
0321   fgets(line, 255, inpFile);
0322   m_i_run_number = atoi(line);
0323   str << "run_number= " << m_i_run_number << std::endl;
0324 
0325   fgets(line, 255, inpFile);
0326   m_i_fgrStripEE = atoi(line);
0327   str << "fgrStripEE_config= " << m_i_fgrStripEE << std::endl;
0328 
0329   fclose(inpFile);  // close inp. file
0330 }
0331 
0332 void popcon::EcalTPGFineGrainStripEEHandler::writeFile(const char* inputFile) {
0333   //-------------------------------------------------------------
0334 
0335   std::ofstream myfile;
0336   myfile.open(inputFile);
0337   myfile << m_i_tag << std::endl;
0338   myfile << m_i_version << std::endl;
0339   myfile << m_i_run_number << std::endl;
0340   myfile << m_i_fgrStripEE << std::endl;
0341 
0342   myfile.close();
0343 }