Class Reference for E1039 Core & Analysis Software
TrigRoads.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <cstring>
5 #include <vector>
6 #include "TrigRoads.h"
7 using namespace std;
8 namespace UtilTrigger {
9 
10 TrigRoads::TrigRoads(const int pol, const int top_bot)
11  : m_file_name("")
12  , m_pol(pol)
13  , m_top_bot(top_bot)
14 {
15  ;
16 }
17 
19 {
20  if (idx < 0 || idx > (int)m_roads.size()) return 0;
21  return &m_roads[idx];
22 }
23 
24 TrigRoad* TrigRoads::FindRoad(const int road_id)
25 {
26  auto it = m_idx_map.find(road_id);
27  if (it != m_idx_map.end()) return &m_roads[it->second];
28  return 0;
29 }
30 
31 int TrigRoads::FindRoadIndex(const int road_id) const
32 {
33  auto it = m_idx_map.find(road_id);
34  if (it != m_idx_map.end()) return it->second;
35  return -1;
36 }
37 
38 const TrigRoad* TrigRoads::GetRoad(const int idx) const
39 {
40  if (idx < 0 || idx > (int)m_roads.size()) return 0;
41  return &m_roads[idx];
42 }
43 
44 const TrigRoad* TrigRoads::FindRoad(const int road_id) const
45 {
46  auto it = m_idx_map.find(road_id);
47  if (it != m_idx_map.end()) return &m_roads[it->second];
48  return 0;
49 }
50 
51 int TrigRoads::LoadConfig(const std::string file_name)
52 {
53  //cout << "TrigRoads::LoadConfig(" << file_name << ")\n";
54  m_roads.clear();
55  m_idx_map.clear();
56  m_file_name = file_name;
57  ifstream ifs(file_name);
58  if (! ifs) return 1;
59 
60  int idx = 0;
61  string buffer;
62  istringstream iss;
63  while (getline(ifs, buffer)) {
64  if (buffer[0] == '#' || buffer[0] == 'r') continue; // 'r' of 'roadID'
65  iss.clear(); // clear any error flags
66  iss.str(buffer);
67 
68  TrigRoad road;
69  if (! (iss >> road.road_id >> road.charge
70  >> road.H1X >> road.H2X >> road.H3X >> road.H4X)) continue;
71  if (road.road_id * m_top_bot <= 0) continue; // top-bottom mismatch
72  if (road.charge * m_pol <= 0) continue; // charge mismatch
73  m_roads.push_back(road);
74  m_idx_map[road.road_id] = idx++;
75  }
76  ifs.close();
77  return 0; // no validation so far
78 }
79 
80 std::string TrigRoads::str(const int level) const
81 {
82  ostringstream oss;
83  oss << (m_pol > 0 ? '+' : '-') << (m_top_bot > 0 ? 'T' : 'B');
84  if (level > 0) oss << "[" << m_roads.size() << "]";
85  if (level > 1) {
86  for (auto it = m_roads.begin(); it != m_roads.end(); it++) oss << " " << it->str(level - 2);
87  }
88  return oss.str();
89 }
90 
91 }; // End of "namespace UtilTrigger"
int LoadConfig(const std::string file_name)
Definition: TrigRoads.cc:51
TrigRoad * FindRoad(const int road_id)
Definition: TrigRoads.cc:24
int FindRoadIndex(const int road_id) const
Definition: TrigRoads.cc:31
std::string str(const int level=0) const
Definition: TrigRoads.cc:80
TrigRoad * GetRoad(const int idx)
Definition: TrigRoads.cc:18