Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-26 22:51:16

0001 #include "CondTools/Ecal/interface/EcalTimeCalibHandler.h"
0002 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0003 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include <cstddef>
0006 #include <cstdio>
0007 #include <fstream>
0008 #include <sstream>
0009 #include <utility>
0010 
0011 const Int_t kEBChannels = 61200, kEEChannels = 14648;
0012 
0013 popcon::EcalTimeCalibHandler::EcalTimeCalibHandler(const edm::ParameterSet& ps)
0014     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTimeCalibHandler")),
0015       m_firstRun(static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()))),
0016       m_file_name(ps.getParameter<std::string>("fileName")),
0017       m_file_type(ps.getParameter<std::string>("type"))  // xml/txt
0018 {
0019   edm::LogInfo("EcalTimeCalib Source handler constructor\n");
0020 }
0021 
0022 void popcon::EcalTimeCalibHandler::getNewObjects() {
0023   edm::LogInfo("going to open file ") << m_file_name;
0024 
0025   //      EcalCondHeader   header;
0026   EcalTimeCalibConstants* payload = new EcalTimeCalibConstants;
0027   if (m_file_type == "xml")
0028     readXML(m_file_name, *payload);
0029   else
0030     readTXT(m_file_name, *payload);
0031   Time_t snc = (Time_t)m_firstRun;
0032 
0033   popcon::PopConSourceHandler<EcalTimeCalibConstants>::m_to_transfer.push_back(std::make_pair(payload, snc));
0034 }
0035 
0036 void popcon::EcalTimeCalibHandler::readXML(const std::string& file_, EcalFloatCondObjectContainer& record) {
0037   std::string dummyLine, bid;
0038   std::ifstream fxml;
0039   fxml.open(file_);
0040   if (!fxml.is_open()) {
0041     throw cms::Exception("ERROR : cannot open file ") << file_;
0042   }
0043   // header
0044   for (int i = 0; i < 6; i++) {
0045     getline(fxml, dummyLine);  // skip first lines
0046   }
0047   fxml >> bid;
0048   std::string stt = bid.substr(7, 5);
0049   std::istringstream iEB(stt);
0050   int nEB;
0051   iEB >> nEB;
0052   if (nEB != kEBChannels) {
0053     throw cms::Exception("Strange number of EB channels ") << nEB;
0054   }
0055   fxml >> bid;  // <item_version>0</item_version>
0056   for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
0057     EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
0058     fxml >> bid;
0059     std::size_t found = bid.find("</");
0060     stt = bid.substr(6, found - 6);
0061     float val = std::stof(stt);
0062     record[myEBDetId] = val;
0063   }
0064   for (int i = 0; i < 5; i++) {
0065     getline(fxml, dummyLine);  // skip first lines
0066   }
0067   fxml >> bid;
0068   stt = bid.substr(7, 5);
0069   std::istringstream iEE(stt);
0070   int nEE;
0071   iEE >> nEE;
0072   if (nEE != kEEChannels) {
0073     throw cms::Exception("Strange number of EE channels ") << nEE;
0074   }
0075   fxml >> bid;  // <item_version>0</item_version>
0076   // now endcaps
0077   for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
0078     EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
0079     fxml >> bid;
0080     std::size_t found = bid.find("</");
0081     stt = bid.substr(6, found - 6);
0082     float val = std::stof(stt);
0083     record[myEEDetId] = val;
0084   }
0085 }
0086 
0087 void popcon::EcalTimeCalibHandler::readTXT(const std::string& file_, EcalFloatCondObjectContainer& record) {
0088   std::ifstream ftxt;
0089   ftxt.open(file_);
0090   if (!ftxt.is_open()) {
0091     throw cms::Exception("ERROR : cannot open file ") << file_;
0092   }
0093   int number_of_lines = 0, eta, phi, x, y, z;
0094   float val;
0095   std::string line;
0096   while (std::getline(ftxt, line)) {
0097     if (number_of_lines < kEBChannels) {  // barrel
0098       std::sscanf(line.c_str(), "%i %i %i %f", &eta, &phi, &z, &val);
0099       EBDetId ebdetid(eta, phi, EBDetId::ETAPHIMODE);
0100       record[ebdetid] = val;
0101     } else {  // endcaps
0102       std::sscanf(line.c_str(), "%i %i %i %f", &x, &y, &z, &val);
0103       EEDetId eedetid(x, y, z, EEDetId::XYMODE);
0104       record[eedetid] = val;
0105     }
0106     number_of_lines++;
0107   }
0108   edm::LogInfo("Number of lines in text file: ") << number_of_lines;
0109   int kChannels = kEBChannels + kEEChannels;
0110   if (number_of_lines != kChannels)
0111     throw cms::Exception("Wrong number of channels!  Please check ");
0112 }