Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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             int icells = 0;
0183 
0184             for (CIfefgr p = dataset_TpgFineGrainStripEE.begin(); p != dataset_TpgFineGrainStripEE.end(); p++) {
0185               ecid_xt = p->first;
0186               rd_fgr = p->second;
0187 
0188               std::string ecid_name = ecid_xt.getName();
0189 
0190               // EB data
0191               if (ecid_name == "EB_VFE") {
0192                 int sm = ecid_xt.getID1();
0193                 int tt = ecid_xt.getID2();
0194                 int strip = ecid_xt.getID3();
0195                 int tcc = sm + 54;
0196                 if (sm > 18)
0197                   tcc = sm + 18;
0198 
0199                 // simple formula to calculate the Srip EB identifier
0200 
0201                 unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
0202 
0203                 EcalTPGFineGrainStripEE::Item item;
0204                 item.threshold = (unsigned int)rd_fgr.getThreshold();
0205                 item.lut = (unsigned int)rd_fgr.getLUTFgr();
0206 
0207                 fgrStripEE->setValue(stripEBId, item);
0208 
0209                 ++icells;
0210               } else if (ecid_name == "ECAL_readout_strip") {
0211                 // EE data
0212                 // fed
0213                 int id1 = ecid_xt.getID1();
0214                 // ccu
0215                 int id2 = ecid_xt.getID2();
0216                 // Strip
0217                 int id3 = ecid_xt.getID3();
0218 
0219                 bool set_the_strip = false;
0220                 int stripEEId;
0221                 for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
0222                   if (!set_the_strip) {
0223                     if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
0224                         my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
0225                         my_StripEcalLogicId_EE[istrip].getID3() == id3) {
0226                       stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
0227                       set_the_strip = true;
0228                       break;
0229                     }
0230                   }
0231                 }
0232 
0233                 EcalTPGFineGrainStripEE::Item item;
0234                 item.threshold = (unsigned int)rd_fgr.getThreshold();
0235                 item.lut = (unsigned int)rd_fgr.getLUTFgr();
0236 
0237                 if (set_the_strip) {
0238                   fgrStripEE->setValue(stripEEId, item);
0239                 } else {
0240                   std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
0241                 }
0242 
0243                 ++icells;
0244               }
0245             }
0246 
0247             Time_t snc = (Time_t)irun;
0248             m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainStripEE*)fgrStripEE, snc));
0249 
0250             m_i_run_number = irun;
0251             m_i_tag = the_config_tag;
0252             m_i_version = the_config_version;
0253             m_i_fgrStripEE = fgrId;
0254 
0255             writeFile("last_tpg_fgrStripEE_settings.txt");
0256 
0257           } else {
0258             m_i_run_number = irun;
0259             m_i_tag = the_config_tag;
0260             m_i_version = the_config_version;
0261 
0262             writeFile("last_tpg_fgrStripEE_settings.txt");
0263 
0264             std::cout << " even if the tag/version is not the same, the fgrStripEEestals id is the same -> no transfer "
0265                          "needed "
0266                       << std::endl;
0267           }
0268 
0269         }
0270 
0271         catch (std::exception& e) {
0272           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0273                     << std::endl;
0274           std::cout << e.what() << std::endl;
0275           m_i_run_number = irun;
0276         }
0277         std::cout << " **************** " << std::endl;
0278 
0279       } else if (nr == 0) {
0280         m_i_run_number = irun;
0281         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0282         std::cout << " **************** " << std::endl;
0283       } else {
0284         m_i_run_number = irun;
0285         m_i_tag = the_config_tag;
0286         m_i_version = the_config_version;
0287         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0288         std::cout << " **************** " << std::endl;
0289         writeFile("last_tpg_fgrStripEE_settings.txt");
0290       }
0291     }
0292   }
0293 
0294   delete econn;
0295 
0296   edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Ecal - > end of getNewObjects -----------";
0297 }
0298 
0299 void popcon::EcalTPGFineGrainStripEEHandler::readFromFile(const char* inputFile) {
0300   //-------------------------------------------------------------
0301 
0302   m_i_tag = "";
0303   m_i_version = 0;
0304   m_i_run_number = 0;
0305   m_i_fgrStripEE = 0;
0306 
0307   FILE* inpFile;  // input file
0308   inpFile = fopen(inputFile, "r");
0309   if (!inpFile) {
0310     edm::LogError("EcalTPGFineGrainStripEEHandler") << "*** Can not open file: " << inputFile;
0311     return;
0312   }
0313 
0314   char line[256];
0315 
0316   std::ostringstream str;
0317 
0318   fgets(line, 255, inpFile);
0319   m_i_tag = to_string(line);
0320   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0321 
0322   fgets(line, 255, inpFile);
0323   m_i_version = atoi(line);
0324   str << "version= " << m_i_version << std::endl;
0325 
0326   fgets(line, 255, inpFile);
0327   m_i_run_number = atoi(line);
0328   str << "run_number= " << m_i_run_number << std::endl;
0329 
0330   fgets(line, 255, inpFile);
0331   m_i_fgrStripEE = atoi(line);
0332   str << "fgrStripEE_config= " << m_i_fgrStripEE << std::endl;
0333 
0334   fclose(inpFile);  // close inp. file
0335 }
0336 
0337 void popcon::EcalTPGFineGrainStripEEHandler::writeFile(const char* inputFile) {
0338   //-------------------------------------------------------------
0339 
0340   std::ofstream myfile;
0341   myfile.open(inputFile);
0342   myfile << m_i_tag << std::endl;
0343   myfile << m_i_version << std::endl;
0344   myfile << m_i_run_number << std::endl;
0345   myfile << m_i_fgrStripEE << std::endl;
0346 
0347   myfile.close();
0348 }