File indexing completed on 2024-04-06 12:02:56
0001 #include "CondTools/Ecal/interface/EcalIntercalibHandler.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0003 #include "CondFormats/EcalObjects/interface/EcalIntercalibConstants.h"
0004 #include "CondTools/Ecal/interface/EcalFloatCondObjectContainerXMLTranslator.h"
0005
0006 #include <iostream>
0007
0008 const Int_t kEBChannels = 61200, kEEChannels = 14648;
0009
0010 popcon::EcalIntercalibHandler::EcalIntercalibHandler(const edm::ParameterSet& ps)
0011 : m_name(ps.getUntrackedParameter<std::string>("name", "EcalIntercalibHandler")) {
0012 edm::LogInfo("EcalIntercalib Source handler constructor\n");
0013 m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0014 m_file_type = ps.getParameter<std::string>("type");
0015 m_file_name = ps.getParameter<std::string>("fileName");
0016 }
0017
0018 popcon::EcalIntercalibHandler::~EcalIntercalibHandler() {}
0019
0020 void popcon::EcalIntercalibHandler::getNewObjects() {
0021
0022 std::ostringstream ss;
0023 ss << "ECAL ";
0024
0025 unsigned long long irun;
0026 std::string file_ = m_file_name;
0027 edm::LogInfo("going to open file ") << file_;
0028
0029
0030 EcalIntercalibConstants* payload = new EcalIntercalibConstants;
0031 if (m_file_type == "xml")
0032 readXML(file_, *payload);
0033 else
0034 readTXT(file_, *payload);
0035 irun = m_firstRun;
0036 Time_t snc = (Time_t)irun;
0037
0038 popcon::PopConSourceHandler<EcalIntercalibConstants>::m_to_transfer.push_back(std::make_pair(payload, snc));
0039 }
0040
0041 void popcon::EcalIntercalibHandler::readXML(const std::string& file_, EcalFloatCondObjectContainer& record) {
0042 std::string dummyLine, bid;
0043 std::ifstream fxml;
0044 fxml.open(file_);
0045 if (!fxml.is_open()) {
0046 edm::LogInfo("ERROR : cannot open file ") << file_;
0047 exit(1);
0048 }
0049
0050 for (int i = 0; i < 6; i++) {
0051 getline(fxml, dummyLine);
0052
0053 }
0054 fxml >> bid;
0055 std::string stt = bid.substr(7, 5);
0056 std::istringstream iEB(stt);
0057 int nEB;
0058 iEB >> nEB;
0059 if (nEB != kEBChannels) {
0060 edm::LogInfo("strange number of EB channels ") << nEB;
0061 exit(-1);
0062 }
0063 fxml >> bid;
0064 for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
0065 EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
0066 fxml >> bid;
0067 std::size_t found = bid.find("</");
0068 stt = bid.substr(6, found - 6);
0069 float val = std::stof(stt);
0070 record[myEBDetId] = val;
0071 }
0072 for (int i = 0; i < 5; i++) {
0073 getline(fxml, dummyLine);
0074
0075 }
0076 fxml >> bid;
0077 stt = bid.substr(7, 5);
0078 std::istringstream iEE(stt);
0079 int nEE;
0080 iEE >> nEE;
0081 if (nEE != kEEChannels) {
0082 edm::LogInfo("strange number of EE channels ") << nEE;
0083 exit(-1);
0084 }
0085 fxml >> bid;
0086
0087 for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
0088 EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
0089 fxml >> bid;
0090 std::size_t found = bid.find("</");
0091 stt = bid.substr(6, found - 6);
0092 float val = std::stof(stt);
0093 record[myEEDetId] = val;
0094 }
0095 }
0096
0097 void popcon::EcalIntercalibHandler::readTXT(const std::string& file_, EcalFloatCondObjectContainer& record) {
0098 std::ifstream ftxt;
0099 ftxt.open(file_);
0100 if (!ftxt.is_open()) {
0101 edm::LogInfo("ERROR : cannot open file ") << file_;
0102 exit(1);
0103 }
0104 int number_of_lines = 0, eta, phi, x, y, z;
0105 float val;
0106 std::string line;
0107 while (std::getline(ftxt, line)) {
0108 if (number_of_lines < kEBChannels) {
0109 sscanf(line.c_str(), "%i %i %i %f", &eta, &phi, &z, &val);
0110 EBDetId ebdetid(eta, phi, EBDetId::ETAPHIMODE);
0111 record[ebdetid] = val;
0112 } else {
0113 sscanf(line.c_str(), "%i %i %i %f", &x, &y, &z, &val);
0114 EEDetId eedetid(x, y, z, EEDetId::XYMODE);
0115 record[eedetid] = val;
0116 }
0117 number_of_lines++;
0118 }
0119 edm::LogInfo("Number of lines in text file: ") << number_of_lines;
0120 int kChannels = kEBChannels + kEEChannels;
0121 if (number_of_lines != kChannels)
0122 edm::LogInfo("wrong number of channels! Please check ");
0123 }