11#include " FWCore/ParameterSet/interface/FileInPath.h"
2+ #include " FWCore/MessageLogger/interface/MessageLogger.h"
23#include " FWCore/ParameterSet/interface/ParameterSet.h"
34#include " FWCore/Utilities/interface/ESGetToken.h"
45
2122#include < iostream>
2223#include < fstream>
2324#include < sstream>
25+ #include < charconv>
2426
2527namespace ALPAKA_ACCELERATOR_NAMESPACE {
2628
@@ -32,7 +34,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
3234 HGCalMappingCellESProducer (const edm::ParameterSet& iConfig)
3335 : ESProducer(iConfig),
3436 filelist_ (iConfig.getParameter<std::vector<std::string> >(" filelist" )),
35- offsetfile_(iConfig.getParameter<std::string >(" offsetfile" )) {
37+ offsetfile_(iConfig.getParameter<edm::FileInPath >(" offsetfile" )) {
3638 auto cc = setWhatProduced (this );
3739 cellIndexTkn_ = cc.consumes (iConfig.getParameter <edm::ESInputTag>(" cellindexer" ));
3840 }
@@ -43,14 +45,16 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
4345 desc.add <std::vector<std::string> >(" filelist" , std::vector<std::string>({}))
4446 ->setComment (" list of files with the readout cells of each module" );
4547 desc.add <edm::ESInputTag>(" cellindexer" , edm::ESInputTag (" " ))->setComment (" Dense cell index tool" );
46- desc.add <std::string>(" offsetfile" , std::string ({}))
48+ desc.add <edm::FileInPath>(
49+ " offsetfile" ,
50+ edm::FileInPath (" Geometry/HGCalMapping/data/CellMaps/calibration_to_surrounding_offsetMap.txt" ))
4751 ->setComment (" file containing the offsets between calibration and surrounding cells" );
4852 descriptions.addWithDefaultLabel (desc);
4953 }
5054
51- std::map<std::tuple<std::string, int , int >, int > makeOffsetMap (std::string input_offsetfile) {
55+ std::map<std::tuple<std::string, int , int >, int > makeOffsetMap (edm::FileInPath input_offsetfile) {
5256 std::map<std::tuple<std::string, int , int >, int > offsetMap;
53- auto offsetfile = edm::FileInPath ( input_offsetfile) .fullPath ();
57+ const auto & offsetfile = input_offsetfile.fullPath ();
5458 std::ifstream stream_offsetfile (offsetfile);
5559 std::string line;
5660 // Skip the first line (description of column content)
@@ -59,21 +63,27 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
5963 std::istringstream iss (line);
6064 std::string r_typecode_str, r_chip_str, r_half_str, offset_str;
6165 if (!(iss >> r_typecode_str >> r_chip_str >> r_half_str >> offset_str)) {
62- std::cout << " Error reading offset file" << std::endl;
66+ edm::LogError ( " HGCalMappingCellESProducer " ) << " Error reading offset file" << std::endl;
6367 break ;
6468 }
65- try {
66- int r_chip = std::stoi (r_chip_str);
67- int r_half = std::stoi (r_half_str);
68- int offset = std::stoi (offset_str);
69- offsetMap[std::make_tuple (r_typecode_str, r_chip, r_half)] = offset;
70- } catch (const std::invalid_argument& e) {
71- std::cout << " Invalid argument: " << e.what () << std::endl;
72- break ;
73- } catch (const std::out_of_range& e) {
74- std::cout << " Out of range: " << e.what () << std::endl;
69+
70+ int r_chip, r_half, offset;
71+ auto parse_entry = [](const std::string& str, int & value) -> bool {
72+ const char * begin = str.data ();
73+ const char * end = begin + str.size ();
74+ auto result = std::from_chars (begin, end, value);
75+ return result.ec == std::errc ();
76+ };
77+
78+ if (!parse_entry (r_chip_str, r_chip) || !parse_entry (r_half_str, r_half) ||
79+ !parse_entry (offset_str, offset)) {
80+ edm::LogError (" HGCalMappingCellESProducer" )
81+ << " Error parsing offset file entries (typecode, roc, halfroc, and offset): " << r_typecode_str << " , "
82+ << r_chip_str << " , " << r_half_str << " , " << offset_str << " \n " ;
7583 break ;
7684 }
85+
86+ offsetMap[std::make_tuple (r_typecode_str, r_chip, r_half)] = offset;
7787 }
7888 return offsetMap;
7989 }
@@ -153,7 +163,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
153163 // calibration cell-to-surrounding cell offset. Can be !=0 only for calibration cells
154164 auto mapKey = std::make_tuple (typecode, chip, half);
155165 int offset = (iscalib && offsetMap.find (mapKey) != offsetMap.end ()) ? offsetMap[mapKey] : 0 ;
156- cell.offset () = offset;
166+ cell.caliboffset () = offset;
157167
158168 } // end loop over entities
159169 } // end loop over cell types
@@ -164,7 +174,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
164174 private:
165175 edm::ESGetToken<HGCalMappingCellIndexer, HGCalElectronicsMappingRcd> cellIndexTkn_;
166176 const std::vector<std::string> filelist_;
167- std::string offsetfile_;
177+ edm::FileInPath offsetfile_;
168178 };
169179
170180 } // namespace hgcal
0 commit comments