|
| 1 | +#include "CondTools/Ecal/interface/EcalTimeCalibHandler.h" |
| 2 | +#include "DataFormats/EcalDetId/interface/EEDetId.h" |
| 3 | +#include "DataFormats/EcalDetId/interface/EBDetId.h" |
| 4 | +#include "FWCore/MessageLogger/interface/MessageLogger.h" |
| 5 | +#include <cstddef> |
| 6 | +#include <cstdio> |
| 7 | +#include <fstream> |
| 8 | +#include <sstream> |
| 9 | +#include <utility> |
| 10 | + |
| 11 | +const Int_t kEBChannels = 61200, kEEChannels = 14648; |
| 12 | + |
| 13 | +popcon::EcalTimeCalibHandler::EcalTimeCalibHandler(const edm::ParameterSet& ps) |
| 14 | + : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTimeCalibHandler")), |
| 15 | + m_firstRun(static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()))), |
| 16 | + m_file_name(ps.getParameter<std::string>("fileName")), |
| 17 | + m_file_type(ps.getParameter<std::string>("type")) // xml/txt |
| 18 | +{ |
| 19 | + edm::LogInfo("EcalTimeCalib Source handler constructor\n"); |
| 20 | +} |
| 21 | + |
| 22 | +void popcon::EcalTimeCalibHandler::getNewObjects() { |
| 23 | + edm::LogInfo("going to open file ") << m_file_name; |
| 24 | + |
| 25 | + // EcalCondHeader header; |
| 26 | + EcalTimeCalibConstants* payload = new EcalTimeCalibConstants; |
| 27 | + if (m_file_type == "xml") |
| 28 | + readXML(m_file_name, *payload); |
| 29 | + else |
| 30 | + readTXT(m_file_name, *payload); |
| 31 | + Time_t snc = (Time_t)m_firstRun; |
| 32 | + |
| 33 | + popcon::PopConSourceHandler<EcalTimeCalibConstants>::m_to_transfer.push_back(std::make_pair(payload, snc)); |
| 34 | +} |
| 35 | + |
| 36 | +void popcon::EcalTimeCalibHandler::readXML(const std::string& file_, EcalFloatCondObjectContainer& record) { |
| 37 | + std::string dummyLine, bid; |
| 38 | + std::ifstream fxml; |
| 39 | + fxml.open(file_); |
| 40 | + if (!fxml.is_open()) { |
| 41 | + throw cms::Exception("ERROR : cannot open file ") << file_; |
| 42 | + } |
| 43 | + // header |
| 44 | + for (int i = 0; i < 6; i++) { |
| 45 | + getline(fxml, dummyLine); // skip first lines |
| 46 | + } |
| 47 | + fxml >> bid; |
| 48 | + std::string stt = bid.substr(7, 5); |
| 49 | + std::istringstream iEB(stt); |
| 50 | + int nEB; |
| 51 | + iEB >> nEB; |
| 52 | + if (nEB != kEBChannels) { |
| 53 | + throw cms::Exception("Strange number of EB channels ") << nEB; |
| 54 | + } |
| 55 | + fxml >> bid; // <item_version>0</item_version> |
| 56 | + for (int iChannel = 0; iChannel < kEBChannels; iChannel++) { |
| 57 | + EBDetId myEBDetId = EBDetId::unhashIndex(iChannel); |
| 58 | + fxml >> bid; |
| 59 | + std::size_t found = bid.find("</"); |
| 60 | + stt = bid.substr(6, found - 6); |
| 61 | + float val = std::stof(stt); |
| 62 | + record[myEBDetId] = val; |
| 63 | + } |
| 64 | + for (int i = 0; i < 5; i++) { |
| 65 | + getline(fxml, dummyLine); // skip first lines |
| 66 | + } |
| 67 | + fxml >> bid; |
| 68 | + stt = bid.substr(7, 5); |
| 69 | + std::istringstream iEE(stt); |
| 70 | + int nEE; |
| 71 | + iEE >> nEE; |
| 72 | + if (nEE != kEEChannels) { |
| 73 | + throw cms::Exception("Strange number of EE channels ") << nEE; |
| 74 | + } |
| 75 | + fxml >> bid; // <item_version>0</item_version> |
| 76 | + // now endcaps |
| 77 | + for (int iChannel = 0; iChannel < kEEChannels; iChannel++) { |
| 78 | + EEDetId myEEDetId = EEDetId::unhashIndex(iChannel); |
| 79 | + fxml >> bid; |
| 80 | + std::size_t found = bid.find("</"); |
| 81 | + stt = bid.substr(6, found - 6); |
| 82 | + float val = std::stof(stt); |
| 83 | + record[myEEDetId] = val; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void popcon::EcalTimeCalibHandler::readTXT(const std::string& file_, EcalFloatCondObjectContainer& record) { |
| 88 | + std::ifstream ftxt; |
| 89 | + ftxt.open(file_); |
| 90 | + if (!ftxt.is_open()) { |
| 91 | + throw cms::Exception("ERROR : cannot open file ") << file_; |
| 92 | + } |
| 93 | + int number_of_lines = 0, eta, phi, x, y, z; |
| 94 | + float val; |
| 95 | + std::string line; |
| 96 | + while (std::getline(ftxt, line)) { |
| 97 | + if (number_of_lines < kEBChannels) { // barrel |
| 98 | + std::sscanf(line.c_str(), "%i %i %i %f", &eta, &phi, &z, &val); |
| 99 | + EBDetId ebdetid(eta, phi, EBDetId::ETAPHIMODE); |
| 100 | + record[ebdetid] = val; |
| 101 | + } else { // endcaps |
| 102 | + std::sscanf(line.c_str(), "%i %i %i %f", &x, &y, &z, &val); |
| 103 | + EEDetId eedetid(x, y, z, EEDetId::XYMODE); |
| 104 | + record[eedetid] = val; |
| 105 | + } |
| 106 | + number_of_lines++; |
| 107 | + } |
| 108 | + edm::LogInfo("Number of lines in text file: ") << number_of_lines; |
| 109 | + int kChannels = kEBChannels + kEEChannels; |
| 110 | + if (number_of_lines != kChannels) |
| 111 | + throw cms::Exception("Wrong number of channels! Please check "); |
| 112 | +} |
0 commit comments