FileChannelMappingService_service.cc
Go to the documentation of this file.
1 // FileChannelMappingService.cxx
2 
5 #include <limits>
6 #include <fstream>
7 
8 using std::cout;
9 using std::ostream;
10 using std::endl;
11 using std::string;
12 using std::ifstream;
13 
14 #undef UseSeedService
15 
17 
18 namespace {
19 
20 // Bad channel value.
21 Channel bad() {
23  return val;
24 }
25 
26 }
27 
28 //**********************************************************************
29 
32 : m_FilePathEnv("FW_SEARCH_PATH"), m_LogLevel(1) {
33  const string myname = "FileChannelMappingService::ctor: ";
34  m_FileName = pset.get<string>("FileName");
35  pset.get_if_present<string>("FilePathEnv", m_FilePathEnv);
36  pset.get_if_present<int>("LogLevel", m_LogLevel);
37  if ( m_LogLevel > 0 ) {
38  print(cout, myname);
39  cout << myname << "Reading map file." << endl;
40  }
41  // Read the map.
43  string fullname;
44  sp.find_file(m_FileName, fullname);
45  if ( fullname.empty() ) {
46  cout << "ERROR: " << "Input file not found: " << m_FileName << endl;
47  cout << "ERROR: " << "Search path: $" << m_FilePathEnv << endl;
48  throw cet::exception("File not found");
49  }
50  std::ifstream infile(fullname);
51  Channel chon;
52  Channel choff;
53  unsigned int count = 0;
54  while ( infile.good() ) {
55  infile >> chon >> choff;
56  if ( m_onMap.size() < choff+1 ) m_onMap.resize(choff+1, bad());
57  if ( m_offMap.size() < chon+1 ) m_offMap.resize(chon+1, bad());
58  m_onMap[choff] = chon;
59  m_offMap[chon] = choff;
60  ++count;
61  }
62  if ( m_LogLevel > 0 ) {
63  cout << myname << "Channel map file: " << fullname << endl;
64  cout << myname << " Number of map entries: " << count << endl;
65  cout << myname << " Maximum online channel: " << m_offMap.size()-1 << endl;
66  cout << myname << " Maximum offline channel: " << m_onMap.size()-1 << endl;
67  }
68 }
69 
70 //**********************************************************************
71 
74 : FileChannelMappingService(pset) { }
75 
76 //**********************************************************************
77 
79  const string myname = "FileChannelMappingService::offline: ";
80  Channel chout = bad();
81  if ( chin < m_offMap.size() ) chout = m_offMap[chin];
82  if ( chout == bad() ) {
83  if ( m_LogLevel > 1 ) {
84  cout << myname << "ERROR: " << "No offline channel mapped to online channel " << chin << endl;
85  }
86  throw cet::exception("Online channel not found.");
87  }
88  return chout;
89 }
90 
91 //**********************************************************************
92 
94  const string myname = "FileChannelMappingService::online: ";
95  Channel chout = bad();
96  if ( chin < m_onMap.size() ) chout = m_onMap[chin];
97  if ( chout == bad() ) {
98  if ( m_LogLevel > 1 ) {
99  cout << myname << "ERROR: " << "No online channel mapped to offline channel " << chin << endl;
100  }
101  throw cet::exception("Offline channel not found.");
102  }
103  return chout;
104 }
105 
106 //**********************************************************************
107 
108 ostream& FileChannelMappingService::print(ostream& out, string prefix) const {
109  string myname = prefix + " ";
110  cout << myname << " FileName: " << m_FileName << endl;
111  cout << myname << "FilePathEnv: " << m_FilePathEnv << endl;
112  cout << myname << " LogLevel: " << m_LogLevel << endl;
113  return out;
114 }
115 
116 //**********************************************************************
117 
119 
120 //**********************************************************************
FileChannelMappingService(fhicl::ParameterSet const &pset)
std::string string
Definition: nybbler.cc:12
string infile
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
static int max(int a, int b)
Channel online(Channel offlineChannel) const
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
std::string find_file(std::string const &filename) const
Definition: search_path.cc:96
Channel offline(Channel onlineChannel) const
unsigned int bad()
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
ChannelMappingService::Channel Channel
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)