Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:28

0001 //#include "Calibration/EcalAlCaRecoProducers/interface/trivialParser.h"
0002 #include "Calibration/Tools/bin/trivialParser.h"
0003 #include <iostream>
0004 #include <cstdlib>
0005 
0006 trivialParser::trivialParser(std::string configFile) {
0007   parse(configFile);
0008   print("[ctor] ");
0009 }
0010 
0011 // ------------------------------------------------------------
0012 
0013 double trivialParser::getVal(std::string name) {
0014   if (m_config.count(name))
0015     return m_config[name];
0016   std::cerr << "[trivialParser] no value for " << name << " found\n";
0017   return -999999.;
0018 }
0019 
0020 // ------------------------------------------------------------
0021 
0022 void trivialParser::parse(std::string configFile) {
0023   std::ifstream input(configFile.c_str());
0024   do {
0025     std::string linea = getNextLine(input);
0026     if (linea.empty())
0027       continue;
0028     std::string name(linea, 0, linea.find('=', 0));
0029     eraseSpaces(name);
0030     std::string valuestring(linea, linea.find('=', 0) + 1, linea.size() - linea.find('=', 0) - 1);
0031     eraseSpaces(valuestring);
0032     double value = strtod(valuestring.c_str(), nullptr);
0033     m_config[name] = value;
0034   } while (!input.eof());
0035 }
0036 
0037 // ------------------------------------------------------------
0038 
0039 std::string trivialParser::getNextLine(std::ifstream& input) {
0040   //  std::cerr << "PG prima cerca " << std::endl ;
0041   std::string singleLine;
0042   do {
0043     getline(input, singleLine, '\n');
0044     //    std::cerr << "PG guardo " << singleLine << std::endl ;
0045   } while ((singleLine.find('#', 0) != std::string::npos || singleLine.find('=', 0) == std::string::npos ||
0046             singleLine.size() < 3) &&
0047            !input.eof());
0048   //  std::cerr << "PG trovato " << singleLine << std::endl ;
0049   return singleLine;
0050 }
0051 
0052 // ------------------------------------------------------------
0053 
0054 void trivialParser::print(std::string prefix) {
0055   std::cerr << "read parameters: " << std::endl;
0056   for (std::map<std::string, double>::const_iterator mapIT = m_config.begin(); mapIT != m_config.end(); ++mapIT) {
0057     std::cerr << prefix << mapIT->first << " = " << mapIT->second << "\n";
0058   }
0059 }
0060 
0061 // ------------------------------------------------------------
0062 
0063 void trivialParser::eraseSpaces(std::string& word) {
0064   while (word.find(' ', 0) != std::string::npos) {
0065     word.erase(word.find(' ', 0), 1);
0066   }
0067   return;
0068 }