Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalTPGSpikeThresholdHandler.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/FEConfigSpikeInfo.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigSpikeDat.h"
0007 
0008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0009 
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include "CondFormats/EcalObjects/interface/EcalTPGSpike.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::EcalTPGSpikeThresholdHandler::EcalTPGSpikeThresholdHandler(const edm::ParameterSet &ps)
0038     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGSpikeThresholdHandler")) {
0039   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "EcalTPGSpike 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("EcalTPGSpikeThresholdHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
0051 }
0052 
0053 popcon::EcalTPGSpikeThresholdHandler::~EcalTPGSpikeThresholdHandler() {}
0054 
0055 void popcon::EcalTPGSpikeThresholdHandler::getNewObjects() {
0056   using namespace edm;
0057   using namespace std;
0058 
0059   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Started GetNewObjects!!!";
0060 
0061   //check whats already inside of database
0062   if (tagInfo().size) {
0063     //check whats already inside of database
0064     std::cout << "got offlineInfo = " << std::endl;
0065     std::cout << "tag name = " << tagInfo().name << std::endl;
0066     std::cout << "size = " << tagInfo().size << std::endl;
0067   } else {
0068     std::cout << " First object for this tag " << std::endl;
0069   }
0070 
0071   unsigned int max_since = 0;
0072   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0073   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "max_since : " << max_since;
0074   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "retrieved last payload ";
0075 
0076   // here we retrieve all the runs after the last from online DB
0077   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Retrieving run list from ONLINE DB ... ";
0078 
0079   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Making connection...";
0080   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0081   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Done.";
0082 
0083   if (!econn) {
0084     std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
0085     //      cerr << e.what() << std::endl;
0086     throw cms::Exception("OMDS not available");
0087   }
0088 
0089   LocationDef my_locdef;
0090   my_locdef.setLocation(m_location);
0091 
0092   RunTypeDef my_rundef;
0093   my_rundef.setRunType(m_runtype);
0094 
0095   RunTag my_runtag;
0096   my_runtag.setLocationDef(my_locdef);
0097   my_runtag.setRunTypeDef(my_rundef);
0098   my_runtag.setGeneralTag(m_gentag);
0099 
0100   readFromFile("last_tpg_spikeThreshold_settings.txt");
0101 
0102   unsigned int min_run;
0103 
0104   if (m_firstRun < m_i_run_number) {
0105     min_run = m_i_run_number + 1;
0106   } else {
0107     min_run = m_firstRun;
0108   }
0109 
0110   if (min_run < max_since) {
0111     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0112   }
0113 
0114   std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
0115             << std::endl;
0116 
0117   unsigned int max_run = m_lastRun;
0118   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "min_run= " << min_run << " max_run= " << max_run;
0119 
0120   RunList my_list;
0121   my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0122   //my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
0123 
0124   std::vector<RunIOV> run_vec = my_list.getRuns();
0125   size_t num_runs = run_vec.size();
0126 
0127   std::cout << "number of runs is : " << num_runs << std::endl;
0128 
0129   std::string str = "";
0130 
0131   unsigned int irun;
0132   if (num_runs > 0) {
0133     for (size_t kr = 0; kr < run_vec.size(); kr++) {
0134       irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
0135 
0136       std::cout << " **************** " << std::endl;
0137       std::cout << " run= " << irun << std::endl;
0138 
0139       // retrieve the data :
0140       std::map<EcalLogicID, RunTPGConfigDat> dataset;
0141       econn->fetchDataSet(&dataset, &run_vec[kr]);
0142 
0143       std::string the_config_tag = "";
0144       int the_config_version = 0;
0145 
0146       std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
0147 
0148       int nr = 0;
0149       for (it = dataset.begin(); it != dataset.end(); it++) {
0150         ++nr;
0151         //EcalLogicID ecalid  = it->first;
0152 
0153         RunTPGConfigDat dat = it->second;
0154         the_config_tag = dat.getConfigTag();
0155         the_config_version = dat.getVersion();
0156       }
0157 
0158       // it is all the same for all SM... get the last one
0159 
0160       std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
0161 
0162       // here we should check if it is the same as previous run.
0163 
0164       if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
0165         std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
0166                   << std::endl;
0167 
0168         FEConfigMainInfo fe_main_info;
0169         fe_main_info.setConfigTag(the_config_tag);
0170         fe_main_info.setVersion(the_config_version);
0171 
0172         try {
0173           std::cout << " before fetch config set" << std::endl;
0174           econn->fetchConfigSet(&fe_main_info);
0175           std::cout << " after fetch config set" << std::endl;
0176 
0177           // now get TPGSpikeThreshold
0178           int spikeId = fe_main_info.getSpiId();
0179 
0180           if (spikeId != m_i_spikeTh) {
0181             FEConfigSpikeInfo fe_spike_info;
0182             fe_spike_info.setId(spikeId);
0183             econn->fetchConfigSet(&fe_spike_info);
0184             std::map<EcalLogicID, FEConfigSpikeDat> dataset_TpgSpike;
0185             econn->fetchDataSet(&dataset_TpgSpike, &fe_spike_info);
0186 
0187             EcalTPGSpike *lut = new EcalTPGSpike();
0188             typedef std::map<EcalLogicID, FEConfigSpikeDat>::const_iterator CIfelut;
0189             EcalLogicID ecid_xt;
0190             FEConfigSpikeDat rd_spike;
0191             int itowers = 0;
0192 
0193             for (CIfelut p = dataset_TpgSpike.begin(); p != dataset_TpgSpike.end(); p++) {
0194               ecid_xt = p->first;
0195               rd_spike = p->second;
0196 
0197               std::string ecid_name = ecid_xt.getName();
0198 
0199               if (ecid_name == "EB_trigger_tower") {
0200                 // SM number
0201                 int smid = ecid_xt.getID1();
0202                 // TT number
0203                 int towerid = ecid_xt.getID2();
0204 
0205                 int tow_eta = (towerid - 1) / 4;
0206                 int tow_phi = ((towerid - 1) - tow_eta * 4);
0207 
0208                 int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
0209 
0210                 EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
0211                 const EcalTrigTowerDetId towid = id.tower();
0212 
0213                 //std::cout << " TTid =  " << towid.rawId() << " SpikeTh = " <<rd_spike.getSpikeThreshold() << std::endl;
0214 
0215                 lut->setValue(towid.rawId(), rd_spike.getSpikeThreshold());
0216                 ++itowers;
0217               }
0218               ++itowers;
0219             }
0220 
0221             std::cout << " The number of towers is = " << itowers << std::endl;
0222 
0223             Time_t snc = (Time_t)irun;
0224 
0225             m_to_transfer.push_back(std::make_pair((EcalTPGSpike *)lut, snc));
0226 
0227             m_i_run_number = irun;
0228             m_i_tag = the_config_tag;
0229             m_i_version = the_config_version;
0230             m_i_spikeTh = spikeId;
0231 
0232             writeFile("last_tpg_spikeThreshold_settings.txt");
0233 
0234           } else {
0235             m_i_run_number = irun;
0236             m_i_tag = the_config_tag;
0237             m_i_version = the_config_version;
0238 
0239             writeFile("last_tpg_spikeThreshold_settings.txt");
0240 
0241             std::cout << " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "
0242                       << std::endl;
0243           }
0244 
0245         }
0246 
0247         catch (std::exception &e) {
0248           std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
0249                     << std::endl;
0250           std::cout << e.what() << std::endl;
0251           m_i_run_number = irun;
0252         }
0253         std::cout << " **************** " << std::endl;
0254 
0255       } else if (nr == 0) {
0256         m_i_run_number = irun;
0257         std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
0258         std::cout << " **************** " << std::endl;
0259       } else {
0260         m_i_run_number = irun;
0261         m_i_tag = the_config_tag;
0262         m_i_version = the_config_version;
0263         std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
0264         std::cout << " **************** " << std::endl;
0265         writeFile("last_tpg_spikeThreshold_settings.txt");
0266       }
0267     }
0268   }
0269 
0270   delete econn;
0271 
0272   edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Ecal - > end of getNewObjects -----------";
0273 }
0274 
0275 void popcon::EcalTPGSpikeThresholdHandler::readFromFile(const char *inputFile) {
0276   //-------------------------------------------------------------
0277 
0278   m_i_tag = "";
0279   m_i_version = 0;
0280   m_i_run_number = 0;
0281   m_i_spikeTh = 0;
0282 
0283   FILE *inpFile;  // input file
0284   inpFile = fopen(inputFile, "r");
0285   if (!inpFile) {
0286     edm::LogError("EcalTPGSpikeThresholdHandler") << "*** Can not open file: " << inputFile;
0287     return;
0288   }
0289 
0290   char line[256];
0291 
0292   std::ostringstream str;
0293 
0294   fgets(line, 255, inpFile);
0295   m_i_tag = to_string(line);
0296   str << "gen tag " << m_i_tag << std::endl;  // should I use this?
0297 
0298   fgets(line, 255, inpFile);
0299   m_i_version = atoi(line);
0300   str << "version= " << m_i_version << std::endl;
0301 
0302   fgets(line, 255, inpFile);
0303   m_i_run_number = atoi(line);
0304   str << "run_number= " << m_i_run_number << std::endl;
0305 
0306   fgets(line, 255, inpFile);
0307   m_i_spikeTh = atoi(line);
0308   str << "spikeTh_config= " << m_i_spikeTh << std::endl;
0309 
0310   fclose(inpFile);  // close inp. file
0311 }
0312 
0313 void popcon::EcalTPGSpikeThresholdHandler::writeFile(const char *inputFile) {
0314   //-------------------------------------------------------------
0315 
0316   std::ofstream myfile;
0317   myfile.open(inputFile);
0318   myfile << m_i_tag << std::endl;
0319   myfile << m_i_version << std::endl;
0320   myfile << m_i_run_number << std::endl;
0321   myfile << m_i_spikeTh << std::endl;
0322 
0323   myfile.close();
0324 }